1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
|
/** Implementation for NSIndexPath for GNUStep
Copyright (C) 2006 Free Software Foundation, Inc.
Written by: Richard Frith-Macdonald <rfm@gnu.org>
Created: Feb 2006
This file is part of the GNUstep Base Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 31 Milk Street #960789 Boston, MA 02196 USA.
*/
#import "common.h"
#define EXPOSE_NSIndexPath_IVARS 1
#import "Foundation/NSByteOrder.h"
#import "Foundation/NSData.h"
#import "Foundation/NSException.h"
#import "Foundation/NSHashTable.h"
#import "Foundation/NSIndexPath.h"
#import "Foundation/NSKeyedArchiver.h"
#import "Foundation/NSLock.h"
static NSRecursiveLock *lock = nil;
static NSHashTable *shared = 0;
static Class myClass = 0;
static NSIndexPath *empty = nil;
static NSIndexPath *dummy = nil;
@implementation NSIndexPath
+ (id) allocWithZone: (NSZone*)aZone
{
if (self == myClass)
{
return empty;
}
return [super allocWithZone: aZone];
}
+ (id) indexPathWithIndex: (NSUInteger)anIndex
{
return [self indexPathWithIndexes: &anIndex length: 1];
}
+ (id) indexPathWithIndexes: (NSUInteger*)indexes length: (NSUInteger)length
{
id o = [self allocWithZone: NSDefaultMallocZone()];
o = [o initWithIndexes: indexes length: length];
return AUTORELEASE(o);
}
+ (NSIndexPath *) indexPathForItem: (NSInteger)item inSection: (NSInteger)section
{
NSUInteger idxs[2];
idxs[0] = (NSUInteger)section;
idxs[1] = (NSUInteger)item;
return [self indexPathWithIndexes: idxs length: 2];
}
+ (NSIndexPath *) indexPathForRow: (NSInteger)row inSection: (NSInteger)section
{
NSUInteger idxs[2];
idxs[0] = (NSUInteger)section;
idxs[1] = (NSUInteger)row;
return [self indexPathWithIndexes: idxs length: 2];
}
+ (void) initialize
{
if (empty == nil)
{
myClass = self;
empty = (NSIndexPath*)NSAllocateObject(self, 0, NSDefaultMallocZone());
[[NSObject leakAt: &empty] release];
dummy = (NSIndexPath*)NSAllocateObject(self, 0, NSDefaultMallocZone());
[[NSObject leakAt: &dummy] release];
shared = NSCreateHashTable(NSNonRetainedObjectHashCallBacks, 1024);
[[NSObject leakAt: &shared] release];
NSHashInsert(shared, empty);
lock = [NSRecursiveLock new];
[[NSObject leakAt: &lock] release];
}
}
- (NSComparisonResult) compare: (NSIndexPath*)other
{
if (other != self)
{
NSUInteger olength = other->_length;
NSUInteger *oindexes = other->_indexes;
NSUInteger end = (_length > olength) ? _length : olength;
NSUInteger pos;
for (pos = 0; pos < end; pos++)
{
if (pos >= _length)
{
return NSOrderedDescending;
}
else if (pos >= olength)
{
return NSOrderedAscending;
}
if (oindexes[pos] < _indexes[pos])
{
return NSOrderedDescending;
}
if (oindexes[pos] > _indexes[pos])
{
return NSOrderedAscending;
}
}
/*
* Should never get here.
*/
NSLog(@"Argh ... two identical index paths exist!");
}
return NSOrderedSame;
}
- (id) copyWithZone: (NSZone*)aZone
{
return RETAIN(self);
}
- (void) dealloc
{
if (self != empty)
{
[lock lock];
if (shared != nil)
{
NSHashRemove(shared, self);
}
[lock unlock];
if (_indexes != 0)
{
NSZoneFree(NSDefaultMallocZone(), _indexes);
}
[super dealloc];
}
GSNOSUPERDEALLOC;
}
- (NSString*) description
{
NSMutableString *m = [[super description] mutableCopy];
NSUInteger i;
[m appendFormat: @"%"PRIuPTR" indexes [", _length];
for (i = 0; i < _length; i++)
{
if (i > 0)
{
[m appendString: @", "];
}
[m appendFormat: @"%"PRIuPTR, _indexes[i]];
}
[m appendString: @"]"];
return AUTORELEASE(m);
}
- (void) encodeWithCoder: (NSCoder*)aCoder
{
if ([aCoder allowsKeyedCoding] == YES)
{
[aCoder encodeInt: (NSInteger)_length forKey: @"NSIndexPathLength"];
if (_length == 1)
{
[aCoder encodeInt: (NSInteger)_indexes[0]
forKey: @"NSIndexPathValue"];
}
else if (_length > 1)
{
NSMutableData *m;
NSUInteger *buf;
NSUInteger i;
m = [NSMutableData new];
[m setLength: _length * sizeof(NSUInteger)];
buf = [m mutableBytes];
for (i = 0; i < _length; i++)
{
buf[i] = NSSwapHostIntToBig(_indexes[i]);
}
[aCoder encodeObject: m forKey: @"NSIndexPathData"];
RELEASE(m);
}
}
else
{
[aCoder encodeValueOfObjCType: @encode(NSUInteger) at: &_length];
if (_length > 0)
{
[aCoder encodeArrayOfObjCType: @encode(NSUInteger)
count: _length
at: _indexes];
}
}
}
- (NSInteger) row
{
return (NSInteger)[self indexAtPosition: 1];
}
- (NSInteger) item
{
return (NSInteger)[self indexAtPosition: 1];
}
- (NSInteger) section
{
return (NSInteger)[self indexAtPosition: 0];
}
- (void) getIndexes: (NSUInteger*)aBuffer
{
memcpy(aBuffer, _indexes, _length * sizeof(NSUInteger));
}
- (NSUInteger) hash
{
return _hash;
}
- (NSUInteger) indexAtPosition: (NSUInteger)position
{
if (position >= _length)
{
return NSNotFound;
}
return _indexes[position];
}
/**
* Return path formed by adding the index to the receiver.
*/
- (NSIndexPath *) indexPathByAddingIndex: (NSUInteger)anIndex
{
NSUInteger buffer[_length + 1];
[self getIndexes: buffer];
buffer[_length] = anIndex;
return [[self class] indexPathWithIndexes: buffer length: _length + 1];
}
- (NSIndexPath *) indexPathByRemovingLastIndex
{
if (_length <= 1)
{
return empty;
}
else
{
return [[self class] indexPathWithIndexes: _indexes length: _length - 1];
}
}
- (id) initWithCoder: (NSCoder*)aCoder
{
if ([aCoder allowsKeyedCoding] == YES)
{
NSUInteger length;
NSUInteger index;
length = [aCoder decodeIntegerForKey: @"NSIndexPathLength"];
if (length == 1)
{
index = [aCoder decodeIntegerForKey: @"NSIndexPathValue"];
self = [self initWithIndex: index];
}
else if (length > 1)
{
// FIXME ... not MacOS-X
NSMutableData *d = [aCoder decodeObjectForKey: @"NSIndexPathData"];
NSUInteger l = [d length];
NSUInteger s = l / length;
NSUInteger i;
void *src = [d mutableBytes];
NSUInteger *dst;
if (s == sizeof(NSUInteger))
{
dst = (NSUInteger*)src;
}
else
{
dst = (NSUInteger*)NSZoneMalloc(NSDefaultMallocZone(),
length * sizeof(NSUInteger));
}
if (s == sizeof(long))
{
long *ptr = (long*)src;
for (i = 0; i < _length; i++)
{
dst[i] = (NSUInteger)NSSwapBigLongToHost(ptr[i]);
}
}
else if (s == sizeof(short))
{
short *ptr = (short*)src;
for (i = 0; i < _length; i++)
{
dst[i] = (NSUInteger)NSSwapBigShortToHost(ptr[i]);
}
}
else if (s == sizeof(long long))
{
long long *ptr = (long long*)src;
for (i = 0; i < _length; i++)
{
dst[i] = (NSUInteger)NSSwapBigLongLongToHost(ptr[i]);
}
}
else
{
if ((void*)dst != src)
{
NSZoneFree(NSDefaultMallocZone(), dst);
}
[NSException raise: NSGenericException format:
@"Unable to decode unsigned integers of size %"PRIuPTR, s];
}
self = [self initWithIndexes: dst length: length];
if ((void*)dst != src)
{
NSZoneFree(NSDefaultMallocZone(), dst);
}
}
}
else
{
NSUInteger length;
[aCoder decodeValueOfObjCType: @encode(NSUInteger) at: &length];
if (length == 0)
{
ASSIGN(self, empty);
}
else
{
NSUInteger buf[16];
NSUInteger *indexes = buf;
if (length > 16)
{
indexes = NSZoneMalloc(NSDefaultMallocZone(),
length * sizeof(NSUInteger));
}
[aCoder decodeArrayOfObjCType: @encode(NSUInteger)
count: length
at: indexes];
self = [self initWithIndexes: indexes length: length];
if (indexes != buf)
{
NSZoneFree(NSDefaultMallocZone(), indexes);
}
}
}
return self;
}
- (id) initWithIndex: (NSUInteger)anIndex
{
return [self initWithIndexes: &anIndex length: 1];
}
/** <init />
* Initialise the receiver to contain the specified indexes.<br />
* May return an existing index path.
*/
- (id) initWithIndexes: (NSUInteger*)indexes length: (NSUInteger)length
{
NSIndexPath *found;
NSUInteger h = 0;
NSUInteger i;
if (_length != 0)
{
[NSException raise: NSGenericException
format: @"Attempt to re-initialize NSIndexPath"];
}
// FIXME ... need better hash function?
for (i = 0; i < length; i++)
{
h = (h << 5) ^ indexes[i];
}
[lock lock];
dummy->_hash = h;
dummy->_length = length;
dummy->_indexes = indexes;
found = NSHashGet(shared, dummy);
if (found == nil)
{
if (self == empty)
{
RELEASE(self);
self = (NSIndexPath*)NSAllocateObject([self class],
0, NSDefaultMallocZone());
}
_hash = dummy->_hash;
_length = dummy->_length;
_indexes = NSZoneMalloc(NSDefaultMallocZone(),
_length * sizeof(NSUInteger));
memcpy(_indexes, dummy->_indexes, _length * sizeof(NSUInteger));
NSHashInsert(shared, self);
}
else
{
ASSIGN(self, found);
}
dummy->_indexes = 0; // Don't want static indexes deallocated atExit
[lock unlock];
return self;
}
- (BOOL) isEqual: (id)other
{
if (other == self)
{
return YES;
}
if (other == nil || GSObjCIsKindOf(object_getClass(other), myClass) == NO)
{
return NO;
}
if (((NSIndexPath*)other)->_length != _length)
{
return NO;
}
else
{
NSUInteger *oindexes = ((NSIndexPath*)other)->_indexes;
NSUInteger pos = _length;
while (pos-- > 0)
{
if (_indexes[pos] != oindexes[pos])
{
return NO;
}
}
}
return YES;
}
- (NSUInteger) length
{
return _length;
}
- (oneway void) release
{
if (self != empty)
{
/* We lock the table while checking, to prevent
* another thread from grabbing this object while we are
* checking it.
* If we are going to deallocate the object, we first remove
* it from the table so that no other thread will find it
* and try to use it while it is being deallocated.
*/
[lock lock];
if (NSDecrementExtraRefCountWasZero(self))
{
[self dealloc];
}
[lock unlock];
}
}
@end
|