iphone - Using/storing an array of floats in Cocoa -
in c, if wanted array of floats (for instance) define fixed size , allocate it, , access each element math operations. however, want ability have arrays mutable since continually grow in size (as app running, , array surpass 10000+ elements) , idea of nsmutablearray sounds great. however, (if understand correctly) stores objects, have wrap numbers in nsnumber, , put array. seems ridiculous amount of overhead store array of floats (or doubles or integers).
on other hand, see there flout attributes core data, don't see how access in same way ( array[index] ). missing here? should heavy-lifting math done in fixed-sized c arrays, there ways of using foundation classes haven't stumbled upon, or there ways of accessing core data objects 1 access array?
you aren't missing here; there's not formal objc interface arrays of c scalar types.
the simple way (as westsider mentioned) use std::vector
, , implement serialization/deserialization using mechanism such cf/ns-data.
you wrap std::vector in objc interface, if wanted:
/* mondoublearray.h */ /* using pimpl, i'm assuming not building objc++ */ struct t_mondoublearray_data; @interface mondoublearray : nsobject < nscoding, nscopying, nsmutablecopying > { t_mondoublearray_data* data; } - (double)doubleatindex; - (void)setdoubleatiindex:(double)index; - (nsuinteger)count; /*...*/ @end /* mondoublearray.mm */ struct t_mondoublearray_data { std::vector<double> array; }; @implementation mondoublebuffer - (id)init { self = [super init]; if (0 != self) { /* remember c++ error handling (e.g., handle exceptions here) */ array = new t_mondoublearray_data; if (0 == array) { [self release]; return 0; } } return self; } /*...more variants...*/ - (void)dealloc { delete array; [super dealloc]; } - (nsdata *)datarepresentationofdoubledata { /*...*/ } - (void)setdoubledatafromdatarepresentation:(nsdata *)data { /*...*/ } /*...*/ @end
then, you'd have accomplished objc serialization without hassle.
there's way use cf/ns_mutablearray scalars, using pointer (or narrower) sized entries:
@interface monfloatbuffer : nsobject { nsmutablearray * floats; } @end @implementation monfloatbuffer - (id)init { self = [super init]; if (0 != self) { cfallocatorref allocator = 0; /* default */ cfindex capacity = 0; /* resizable */ /* implement of this, if wanted */ const cfarraycallbacks callbacks = { 0 /* version */ , 0 /* retain */ , 0 /* release */ , 0 /* copydescription */ , 0 /* equal */ }; floats = (nsmutablearray*)cfarraycreatemutable(allocator, capacity, &callbacks); // can read/write pointer sized values `floats`, // , values won't passed cfretain/cfrelease. } return self; } @end
however, still fail deserialize without customization. so... nspointerarray accomplish more easily... still fixed pointer sized values, have write yourself. not terribly hard. downside number of variants may end with.
Comments
Post a Comment