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
|
/** Concrete implementation of NSCountedSet based on GNU Set class
Copyright (C) 1998,2000 Free Software Foundation, Inc.
Written by: Richard Frith-Macdonald <richard@brainstorm.co.uk>
Created: October 1998
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"
#import "Foundation/NSSet.h"
#import "Foundation/NSAutoreleasePool.h"
#import "Foundation/NSEnumerator.h"
#import "Foundation/NSException.h"
#import "Foundation/NSPortCoder.h"
#import "GSPrivate.h"
#define GSI_MAP_RETAIN_VAL(M, X)
#define GSI_MAP_RELEASE_VAL(M, X)
#define GSI_MAP_KTYPES GSUNION_OBJ
#define GSI_MAP_VTYPES GSUNION_NSINT
#include "GNUstepBase/GSIMap.h"
@interface GSCountedSet : NSCountedSet
{
@public
GSIMapTable_t map;
@private
unsigned long _version;
}
@end
@interface GSCountedSetEnumerator : NSEnumerator
{
GSCountedSet *set;
GSIMapEnumerator_t enumerator;
}
@end
@implementation GSCountedSetEnumerator
- (void) dealloc
{
GSIMapEndEnumerator(&enumerator);
RELEASE(set);
[super dealloc];
}
- (id) initWithSet: (NSSet*)d
{
self = [super init];
if (self != nil)
{
set = RETAIN((GSCountedSet*)d);
enumerator = GSIMapEnumeratorForMap(&set->map);
}
return self;
}
- (id) nextObject
{
GSIMapNode node = GSIMapEnumeratorNextNode(&enumerator);
if (node == 0)
{
return nil;
}
return node->key.obj;
}
@end
@implementation GSCountedSet
- (NSUInteger) sizeOfContentExcluding: (NSHashTable*)exclude
{
/* Can't safely calculate for mutable object; just buffer size
*/
return map.nodeCount * sizeof(GSIMapNode);
}
+ (void) initialize
{
if (self == [GSCountedSet class])
{
}
}
/**
* Adds an object to the set. If the set already contains an object
* equal to the specified object (as determined by the [-isEqual:]
* method) then the count for that object is incremented rather
* than the new object being added.
*/
- (void) addObject: (id)anObject
{
GSIMapNode node;
if (anObject == nil)
{
[NSException raise: NSInvalidArgumentException
format: @"Tried to nil value to counted set"];
}
_version++;
node = GSIMapNodeForKey(&map, (GSIMapKey)anObject);
if (node == 0)
{
GSIMapAddPair(&map,(GSIMapKey)anObject,(GSIMapVal)(NSUInteger)1);
}
else
{
node->value.nsu++;
}
_version++;
}
- (NSUInteger) count
{
return map.nodeCount;
}
- (NSUInteger) countForObject: (id)anObject
{
if (anObject)
{
GSIMapNode node = GSIMapNodeForKey(&map, (GSIMapKey)anObject);
if (node)
{
return node->value.nsu;
}
}
return 0;
}
- (void) dealloc
{
GSIMapEmptyMap(&map);
[super dealloc];
}
- (void) encodeWithCoder: (NSCoder*)aCoder
{
unsigned count = map.nodeCount;
SEL sel1 = @selector(encodeObject:);
IMP imp1 = [aCoder methodForSelector: sel1];
SEL sel2 = @selector(encodeValueOfObjCType:at:);
IMP imp2 = [aCoder methodForSelector: sel2];
const char *type = @encode(unsigned);
GSIMapEnumerator_t enumerator = GSIMapEnumeratorForMap(&map);
GSIMapNode node = GSIMapEnumeratorNextNode(&enumerator);
(*imp2)(aCoder, sel2, type, &count);
while (node != 0)
{
(*imp1)(aCoder, sel1, node->key.obj);
(*imp2)(aCoder, sel2, type, &node->value.nsu);
node = GSIMapEnumeratorNextNode(&enumerator);
}
GSIMapEndEnumerator(&enumerator);
}
- (NSUInteger) hash
{
return map.nodeCount;
}
- (id) init
{
return [self initWithCapacity: 0];
}
/* Designated initialiser */
- (id) initWithCapacity: (NSUInteger)cap
{
GSIMapInitWithZoneAndCapacity(&map, [self zone], cap);
return self;
}
- (id) initWithCoder: (NSCoder*)aCoder
{
unsigned count;
id value;
NSUInteger valcnt;
SEL sel = @selector(decodeValueOfObjCType:at:);
IMP imp = [aCoder methodForSelector: sel];
const char *utype = @encode(unsigned);
const char *otype = @encode(id);
(*imp)(aCoder, sel, utype, &count);
GSIMapInitWithZoneAndCapacity(&map, [self zone], count);
while (count-- > 0)
{
(*imp)(aCoder, sel, otype, &value);
(*imp)(aCoder, sel, utype, &valcnt);
GSIMapAddPairNoRetain(&map, (GSIMapKey)value, (GSIMapVal)valcnt);
}
return self;
}
- (id) initWithObjects: (const id[])objs count: (NSUInteger)c
{
NSUInteger i;
self = [self initWithCapacity: c];
if (self == nil)
{
return nil;
}
for (i = 0; i < c; i++)
{
GSIMapNode node;
if (objs[i] == nil)
{
DESTROY(self);
[NSException raise: NSInvalidArgumentException
format: @"Tried to init counted set with nil value"];
}
node = GSIMapNodeForKey(&map, (GSIMapKey)objs[i]);
if (node == 0)
{
GSIMapAddPair(&map,(GSIMapKey)objs[i],(GSIMapVal)(NSUInteger)1);
}
else
{
node->value.nsu++;
}
}
return self;
}
- (id) member: (id)anObject
{
if (anObject != nil)
{
GSIMapNode node = GSIMapNodeForKey(&map, (GSIMapKey)anObject);
if (node != 0)
{
return node->key.obj;
}
}
return nil;
}
- (NSEnumerator*) objectEnumerator
{
return AUTORELEASE([[GSCountedSetEnumerator allocWithZone:
NSDefaultMallocZone()] initWithSet: self]);
}
/**
* Removes all objcts which have not been added more than level times
* from the counted set.<br />
* Note to GNUstep maintainers ... this method depends on the characteristic
* of the GSIMap enumeration that, once enumerated, an object can be removed
* from the map. If GSIMap ever loses that characterstic, this will break.
*/
- (void) purge: (NSInteger)level
{
if (level > 0)
{
GSIMapEnumerator_t enumerator = GSIMapEnumeratorForMap(&map);
GSIMapBucket bucket = GSIMapEnumeratorBucket(&enumerator);
GSIMapNode node = GSIMapEnumeratorNextNode(&enumerator);
while (node != 0)
{
if (node->value.nsu <= (NSUInteger)level)
{
_version++;
GSIMapRemoveNodeFromMap(&map, bucket, node);
GSIMapFreeNode(&map, node);
_version++;
}
bucket = GSIMapEnumeratorBucket(&enumerator);
node = GSIMapEnumeratorNextNode(&enumerator);
}
GSIMapEndEnumerator(&enumerator);
}
}
- (void) removeAllObjects
{
_version++;
GSIMapCleanMap(&map);
_version++;
}
/**
* Decrements the count of the number of times that the specified
* object (or an object equal to it as determined by the
* [-isEqual:] method) has been added to the set. If the count
* becomes zero, the object is removed from the set.
*/
- (void) removeObject: (id)anObject
{
GSIMapBucket bucket;
if (anObject == nil)
{
NSWarnMLog(@"attempt to remove nil object");
return;
}
_version++;
bucket = GSIMapBucketForKey(&map, (GSIMapKey)anObject);
if (bucket != 0)
{
GSIMapNode node;
node = GSIMapNodeForKeyInBucket(&map, bucket, (GSIMapKey)anObject);
if (node != 0)
{
if (--node->value.nsu == 0)
{
GSIMapRemoveNodeFromMap(&map, bucket, node);
GSIMapFreeNode(&map, node);
}
}
}
_version++;
}
- (id) unique: (id)anObject
{
GSIMapNode node;
id result;
_version++;
if (anObject == nil)
{
[NSException raise: NSInvalidArgumentException
format: @"Tried to unique nil value in counted set"];
}
node = GSIMapNodeForKey(&map, (GSIMapKey)anObject);
if (node == 0)
{
result = anObject;
GSIMapAddPair(&map,(GSIMapKey)anObject,(GSIMapVal)(NSUInteger)1);
}
else
{
result = node->key.obj;
node->value.nsu++;
if (result != anObject)
{
[anObject release];
[result retain];
}
}
_version++;
return result;
}
- (NSUInteger) countByEnumeratingWithState: (NSFastEnumerationState*)state
objects: (id*)stackbuf
count: (NSUInteger)len
{
state->mutationsPtr = &_version;
return GSIMapCountByEnumeratingWithStateObjectsCount
(&map, state, stackbuf, len);
}
@end
|