| View previous topic :: View next topic |
| Author |
Message |
znek

Joined: 06 Aug 2004 Posts: 2
|
Posted: Mon Sep 06, 2004 1:29 pm Post subject: Specialized NSMutableArray subclass for NSRanges? |
|
|
Hi there,
my current pet project - an editor for SOPE templates (think of a specialized HTML editor) needs to deal with arrays of ranges for a variety of reasons (finding tags quickly, associating character locations with line numbers, etc.). Currently I'm doing this encapsulating NSRange structs into objects (by means of EDRange). I'm happy with the results that gives me, especially I can sort these ranges using various comparisons (by location, by length) to speed up certain use cases. However some DOUBT remains whether this is the most efficient thing to do it ... I don't really know how much hassle it would be to implement a subclass of NSMutableArray that operates on structs (NSRange) rather than on objects? Probably the best thing would be to have something like this defined generically (probably with some macros). Or should I go with ObjC++ and use STL for this (I don't really have a clue about STL, so probably this idea is plain sick ?
Someone please wise me up. Cheers! _________________ too slow is a good reason to upgrade! |
|
| Back to top |
|
 |
Nat!

Joined: 06 Aug 2004 Posts: 42 Location: Bochum
|
Posted: Mon Sep 06, 2004 4:44 pm Post subject: |
|
|
I don't think it would be good to make this a NSMutableArray subclass. Techically it isn't really possible, as you can't call retain on NSRange and neither is NSRange an id so you would get lots of casting pressure for that hack.
description on that subclass would also fail pretty badly. |
|
| Back to top |
|
 |
whitenoise
Joined: 07 Sep 2004 Posts: 5
|
Posted: Tue Sep 07, 2004 5:00 am Post subject: |
|
|
| STL is not generally going to help you with optimization if your algorithms are already sane. It is likely to make things much, much worse by adding to your VM working set. |
|
| Back to top |
|
 |
Nat!

Joined: 06 Aug 2004 Posts: 42 Location: Bochum
|
Posted: Tue Sep 07, 2004 9:53 pm Post subject: |
|
|
Here is a rudimentary implementation of an array that contains NSRanges, I think it works, but haven't tested it much. (Maybe a I should try Mock Objects )
The immutable class.
| Code: | #import <Foundation/Foundation.h>
@interface MulleNSRangeArray : NSObject
{
NSRange *array_;
unsigned int count_;
}
- (id) initWithRanges:(NSRange *) ranges
count:(unsigned int) count;
- (unsigned int) count;
- (NSRange) rangeAtIndex:(unsigned int) index;
@end |
| Code: | #import "MulleNSRangeArray.h"
@implementation MulleNSRangeArray
- (id) initWithRanges:(NSRange *) ranges
count:(unsigned int) count
{
size_t len;
[super init];
len = sizeof( NSRange) * count;
array_ = malloc( len);
memcpy( array_, ranges, len);
count_ = count;
return( self);
}
- (void) dealloc
{
free( array_);
[super dealloc];
}
- (unsigned int) count
{
return( count_);
}
- (NSRange) rangeAtIndex:(unsigned int) index
{
NSParameterAssert( index < count_);
return( array_[ index]);
}
@end
|
and the mutable subclass
| Code: | #import "MulleNSRangeArray.h"
@interface MulleMutableNSRangeArray : MulleNSRangeArray
{
unsigned int capacity_;
}
- (void) addRange:(NSRange) range;
- (void) removeRangeAtIndex:(unsigned int) index;
@end
|
| Code: | #import "MulleMutableNSRangeArray.h"
@implementation MulleMutableNSRangeArray
#define MIN_CAPACITY 4
- (id) initWithRanges:(NSRange *) ranges
count:(unsigned int) count
{
[super initWithRanges:ranges
count:count];
capacity_ = count_;
return( self);
}
- (void) addRange:(NSRange) range
{
if( capacity_ == count_)
{
capacity_ <<= 1;
if( capacity_ < MIN_CAPACITY)
capacity_ = MIN_CAPACITY;
array_ = realloc( array_, sizeof( NSRange) * capacity_);
}
array_[ count_++] = range;
return;
}
- (void) removeRangeAtIndex:(unsigned int) index
{
NSParameterAssert( index <= count_);
--count_;
if( index < count_)
memcpy( &array_[ index], &array_[ index + 1], sizeof( NSRange) * (count_ - index));
}
@end
|
And sadly, totally unoptimized  |
|
| Back to top |
|
 |
|