From 22a297ed98d667f66931a2031acbf748677bd01d Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Wed, 6 Aug 2008 20:30:23 +0200 Subject: Implement MLKArray#-setSize:ofDimension:. --- MLKArray.m | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- MLKRoot.m | 7 +++++-- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/MLKArray.m b/MLKArray.m index 995dfa8..65e8930 100644 --- a/MLKArray.m +++ b/MLKArray.m @@ -129,7 +129,55 @@ static int equalp (const void *x, const void *y) -(void) setSize:(int)size ofDimension:(int)dimension { - // FIXME: ??? + // I'd love to comment the following code, but I can't find the right + // words to do so. There's this picture of jumping pointers in + // parallel in my head. How to describe it? I pass. + + int i; + int old_size; + id *sourcePointer, *destPointer; + int subblock_length, old_block_length, new_block_length; + NSEnumerator *e; + id el; + id *old_data; + + old_size = _size; + + subblock_length = 1; + for (i = dimension + 1; i < [_dimensions count]; i++) + subblock_length *= [[_dimensions objectAtIndex:i] intValue]; + + old_block_length = subblock_length * [[_dimensions objectAtIndex:dimension] intValue]; + new_block_length = subblock_length * size; + + [_dimensions replaceObjectAtIndex:dimension + withObject:[MLKInteger integerWithInt:size]]; + + _size = 1; + e = [_dimensions objectEnumerator]; + while ((el = [e nextObject])) + { + el = denullify (el); + _size *= MLKIntWithInteger (el); + } + + old_data = _data; + _data = calloc (_size, sizeof (id)); + + sourcePointer = old_data; + destPointer = _data; + while (destPointer < _data + size - 1) + { + memmove (destPointer, sourcePointer, + (old_block_length < new_block_length + ? old_block_length + : new_block_length) + * sizeof(id)); + sourcePointer += old_block_length; + destPointer += new_block_length; + } + + free (old_data); } -(void) setFillPointer:(int)fillPointer diff --git a/MLKRoot.m b/MLKRoot.m index 80ba4ab..8add77e 100644 --- a/MLKRoot.m +++ b/MLKRoot.m @@ -425,8 +425,11 @@ static id truify (BOOL value) suffix = MLKPrintToString(x); } else - [NSException raise:@"MLKTypeError" - format:@"%@ is not of type (OR INTEGER STRING).", x]; + { + [NSException raise:@"MLKTypeError" + format:@"%@ is not of type (OR INTEGER STRING).", x]; + return nil; + } } else { -- cgit v1.2.3