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 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) (limited to 'MLKArray.m') 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 -- cgit v1.2.3