diff options
author | Matthias Andreas Benkard <matthias@benkard.de> | 2008-08-06 20:30:23 +0200 |
---|---|---|
committer | Matthias Andreas Benkard <matthias@benkard.de> | 2008-08-06 20:30:23 +0200 |
commit | 22a297ed98d667f66931a2031acbf748677bd01d (patch) | |
tree | dffd80d87076e709a85b3007f8ad8cd3ff67f76b | |
parent | 30c1591ec870d8ebb7f2d0562c54aa88cd149693 (diff) |
Implement MLKArray#-setSize:ofDimension:.
-rw-r--r-- | MLKArray.m | 50 | ||||
-rw-r--r-- | MLKRoot.m | 7 |
2 files changed, 54 insertions, 3 deletions
@@ -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 @@ -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 { |