summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Andreas Benkard <matthias@benkard.de>2008-08-06 20:30:23 +0200
committerMatthias Andreas Benkard <matthias@benkard.de>2008-08-06 20:30:23 +0200
commit22a297ed98d667f66931a2031acbf748677bd01d (patch)
treedffd80d87076e709a85b3007f8ad8cd3ff67f76b
parent30c1591ec870d8ebb7f2d0562c54aa88cd149693 (diff)
Implement MLKArray#-setSize:ofDimension:.
-rw-r--r--MLKArray.m50
-rw-r--r--MLKRoot.m7
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
{