diff -r 000000000000 -r 8fad19466c59 ValueArray.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ValueArray.m Sun Apr 06 19:13:27 2008 -0700 @@ -0,0 +1,93 @@ +// +// ValueArray.m +// MYUtilities +// +// Copyright 2008 Jens Alfke. All rights reserved. +// + +#import "ValueArray.h" + + +@implementation ValueArray + + +- (id) initWithCount: (unsigned)count valueSize: (size_t)valueSize +{ + self = [super init]; + if( self ) { + _count = count; + _valueSize = valueSize; + } + return self; +} + ++ (ValueArray*) valueArrayWithCount: (unsigned)count valueSize: (size_t)valueSize +{ + return [[(ValueArray*)NSAllocateObject(self,count*valueSize,nil) + initWithCount: count valueSize: valueSize] + autorelease]; +} + +- (unsigned) count {return _count;} +- (size_t) valueSize {return _valueSize;} + +- (const void*) valueAtIndex: (unsigned)i +{ + NSParameterAssert(i<_count); + return (const char*)object_getIndexedIvars(self) + i*_valueSize; +} + +- (void) getValue: (void*)value atIndex: (unsigned)i +{ + NSParameterAssert(i<_count); + NSParameterAssert(value!=NULL); + memcpy(value, object_getIndexedIvars(self) + i*_valueSize, _valueSize); +} + +- (void) setValue: (const void*)value atIndex: (unsigned)i +{ + NSParameterAssert(i<_count); + NSParameterAssert(value!=NULL); + memcpy(object_getIndexedIvars(self) + i*_valueSize, value, _valueSize); +} + + + +- (id)initWithCoder:(NSCoder *)decoder +{ + NSParameterAssert([decoder allowsKeyedCoding]); + NSKeyedUnarchiver *arch = (NSKeyedUnarchiver*)decoder; + unsigned count = [arch decodeIntForKey: @"count"]; + size_t valueSize = [arch decodeIntForKey: @"valueSize"]; + + [super release]; + self = [[[self class] valueArrayWithCount: count valueSize: valueSize] retain]; + if( self ) { + unsigned nBytes; + const void *bytes = [arch decodeBytesForKey: @"values" returnedLength: &nBytes]; + NSAssert(nBytes==count*valueSize,@"Value size mismatch"); + memcpy( object_getIndexedIvars(self), bytes, nBytes ); + } + return self; +} + + +- (void)encodeWithCoder:(NSCoder *)coder +{ + NSParameterAssert([coder allowsKeyedCoding]); + NSKeyedArchiver *arch = (NSKeyedArchiver*)coder; + + [arch encodeInt: _count forKey: @"count"]; + [arch encodeInt: _valueSize forKey: @"valueSize"]; + [arch encodeBytes: object_getIndexedIvars(self) + length: _count*_valueSize + forKey: @"values"]; +} + + +@end + + +ImplementValueArrayOf(Int,int) + +ImplementValueArrayOf(Double,double)