ios - Any ideas for a sorted array that is easily indexed by key value range? -
scenaro: ios/objective-c application. have anywhere few dozen few thousand entries must collect , place in order timestamp. easy -- nsarray sorted descriptor.
however, need able access array , select multiple entries time range (where start/end times may not correspond entry). , modestly performance sensitive, both setup time , access time.
best can come sort array , binary search start/end points. doable , sufficiently fast. however, doesn't quite "feel" right (though can't articulate why).
any other ideas?
1) sort array (optional, said needed)
2) instead of binary search, use nspredicate find entries interested in.
here sample code 1 of projects have adapt own class, 1 has timestamp.
// property wher store data @property nsarray *data; // custom struct hold timestamp values, min , max typedef struct cmttimestamprange { uint64 min, max; } cmttimestamprange; // return sub array objects between 2 time stamps - (nsarray *)samplesintimestamprange:(cmttimestamprange)timestamprange { nsarray *tsrange = @[@(timestamprange.min), @(timestamprange.max)]; nspredicate *filter = [nspredicate predicatewithformat:@"timestamp between %@",tsrange]; nsarray *samples = [self.data filteredarrayusingpredicate:filter]; return samples; }
update
this snippet above intended offer simple solution question posted, not intended high performance code. high performance i'd suggest using core foundation (cfarray) , c functions. cfarray has function cfarraybsearchvalues binary search in sorted cfarray, don't have own function.
Comments
Post a Comment