• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Array of NSRange
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Array of NSRange


  • Subject: Re: Array of NSRange
  • From: Andy Lee <email@hidden>
  • Date: Wed, 10 Jul 2002 09:13:31 -0400

At 10:34 AM +0200 7/10/02, Nicola Vitacolonna wrote:
Why doesn't Cocoa have an NSRange class? I suppose it is for efficiency,
but then, how could I efficiently implement an array of NSRange? Is
plain C the only solution?

The array-of-NSRange question has come up before. It seems there is a wide range of reasons (so to speak) why one would want such a thing.

It's easy enough to write a wrapper class around a C array if that's the right solution for your case (see also Nicholas Riley's reply), but perhaps I can save you a small bit of trouble. Here's a class I wrote for this purpose called DIGSRangeArray. It's only a week old, but I've relied on it heavily in that time. Note that it lacks some obvious methods like deletes and inserts, because I don't need them. If you do need those methods, just get right in there and add them.

--Andy

==========

-----DIGSRangeArray.h-----
// $Revision: 1.1.1.1 $
//

#import <Foundation/Foundation.h>

@interface DIGSRangeArray : NSObject
{
unsigned _count;
NSMutableData *_rangeData;
}

//----------------------------------------------------------------------------
// Init/awake/dealloc
//----------------------------------------------------------------------------

- (id)init;

// Designated initializer.
- (id)initWithElementCapacity:(unsigned)capacity;

- (void)dealloc;

//----------------------------------------------------------------------------
// Getters and setters
//----------------------------------------------------------------------------

- (int)numRanges;

- (NSRange)rangeAtIndex:(unsigned)index;

- (NSRange)lastRange;

- (void)addRange:(NSRange)range;

- (void)setRange:(NSRange)range atIndex:(unsigned)index;

@end
-----end DIGSRangeArray.h-----

-----DIGSRangeArray.m-----
// $Revision: 1.1.1.1 $
//

#import "DIGSRangeArray.h"

//----------------------------------------------------------------------------
// Forward declarations of "private" methods
//----------------------------------------------------------------------------

@interface DIGSRangeArray (Private)
- (void)_expandElementCapacityAsNeeded:(unsigned)requiredCapacity;
@end

@implementation DIGSRangeArray

//----------------------------------------------------------------------------
// Init/awake/dealloc
//----------------------------------------------------------------------------

- (id)init
{
return [self initWithElementCapacity:16];
}

- (id)initWithElementCapacity:(unsigned)capacity
{
if ((self = [super init]))
{
_count = 0;
_rangeData = [[NSMutableData alloc] init];

[self _expandElementCapacityAsNeeded:capacity];
}

return self;
}

- (void)dealloc
{
[_rangeData release];

[super dealloc];
}

//----------------------------------------------------------------------------
// Getters and setters
//----------------------------------------------------------------------------

- (int)numRanges { return _count; }

- (NSRange)rangeAtIndex:(unsigned)index
{
if (index >= _count)
{
[[NSException
exceptionWithName:NSRangeException
reason:@"DIGSRangeArray index out of range"
userInfo:nil] raise];
}

return ((NSRange *)[_rangeData bytes])[index];
}

- (NSRange)lastRange
{
return [self rangeAtIndex:(_count - 1)];
}

- (void)addRange:(NSRange)range
{
if ([_rangeData length] <= _count * sizeof(NSRange))
{
[self _expandElementCapacityAsNeeded:(_count + 16)];
}
((NSRange *)[_rangeData mutableBytes])[_count] = range;
_count++;
}

- (void)setRange:(NSRange)range atIndex:(unsigned)index
{
if (index >= _count)
{
[[NSException
exceptionWithName:NSRangeException
reason:@"DIGSRangeArray index out of range"
userInfo:nil] raise];
}

((NSRange *)[_rangeData mutableBytes])[index] = range;
}

@end

//----------------------------------------------------------------------------
// "Private" methods
//----------------------------------------------------------------------------

@implementation DIGSRangeArray (Private)

- (void)_expandElementCapacityAsNeeded:(unsigned)requiredCapacity
{
if ([_rangeData length] < requiredCapacity * sizeof(NSRange))
{
[_rangeData setLength:(requiredCapacity * sizeof(NSRange))];
}
}

@end
-----end DIGSRangeArray.m-----
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Array of NSRange (From: Nicola Vitacolonna <email@hidden>)

  • Prev by Date: Re: "Morphing" Sheets
  • Next by Date: Re: Database Suggestions
  • Previous by thread: Re: Array of NSRange
  • Next by thread: Why is my window losing focus?
  • Index(es):
    • Date
    • Thread