Specialized NSMutableArray subclass for NSRanges?

 
Post new topic   Reply to topic    Mulle kybernetiK Optimization Forum Index :: Requests - Questions - Help
View previous topic :: View next topic  
Author Message
znek



Joined: 06 Aug 2004
Posts: 2

PostPosted: Mon Sep 06, 2004 1:29 pm    Post subject: Specialized NSMutableArray subclass for NSRanges? Reply with quote

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 Wink?

Someone please wise me up. Cheers!
_________________
too slow is a good reason to upgrade!
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address
Nat!



Joined: 06 Aug 2004
Posts: 42
Location: Bochum

PostPosted: Mon Sep 06, 2004 4:44 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
whitenoise



Joined: 07 Sep 2004
Posts: 5

PostPosted: Tue Sep 07, 2004 5:00 am    Post subject: Reply with quote

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
View user's profile Send private message
Nat!



Joined: 06 Aug 2004
Posts: 42
Location: Bochum

PostPosted: Tue Sep 07, 2004 9:53 pm    Post subject: Reply with quote

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 Wink)

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 Razz
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Mulle kybernetiK Optimization Forum Index :: Requests - Questions - Help All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum




Powered by phpBB © 2001, 2002 phpBB Group
Charcoal2 Theme © Zarron Media