2019-06-19 04:14:03 +00:00
|
|
|
/** Concrete implementation of GSOrderedSet and GSMutableOrderedSet
|
|
|
|
based on GNU NSOrderedSet and NSMutableOrderedSet classes
|
2019-06-07 15:46:46 +00:00
|
|
|
Copyright (C) 2019 Free Software Foundation, Inc.
|
2019-06-27 12:20:19 +00:00
|
|
|
|
2019-06-07 15:46:46 +00:00
|
|
|
Written by: Gregory Casamento <greg.casamento@gmail.com>
|
|
|
|
Created: May 17 2019
|
2019-06-27 12:20:19 +00:00
|
|
|
|
2019-06-07 15:46:46 +00:00
|
|
|
This file is part of the GNUstep Base Library.
|
2019-06-27 12:20:19 +00:00
|
|
|
|
2019-06-07 15:46:46 +00:00
|
|
|
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.
|
2019-06-27 12:20:19 +00:00
|
|
|
|
2019-06-07 15:46:46 +00:00
|
|
|
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
|
2019-12-09 23:36:00 +00:00
|
|
|
Lesser General Public License for more details.
|
2019-06-27 12:20:19 +00:00
|
|
|
|
2019-06-07 15:46:46 +00:00
|
|
|
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., 51 Franklin Street, Fifth Floor,
|
2019-12-09 23:36:00 +00:00
|
|
|
Boston, MA 02110 USA.
|
2019-06-07 15:46:46 +00:00
|
|
|
*/
|
2019-05-18 07:56:11 +00:00
|
|
|
|
|
|
|
#import "common.h"
|
2019-05-20 05:43:06 +00:00
|
|
|
#import "Foundation/NSOrderedSet.h"
|
2019-05-18 07:56:11 +00:00
|
|
|
#import "Foundation/NSAutoreleasePool.h"
|
|
|
|
#import "Foundation/NSArray.h"
|
|
|
|
#import "Foundation/NSEnumerator.h"
|
|
|
|
#import "Foundation/NSException.h"
|
|
|
|
#import "Foundation/NSPortCoder.h"
|
2019-05-20 05:43:06 +00:00
|
|
|
#import "Foundation/NSIndexSet.h"
|
2019-05-18 07:56:11 +00:00
|
|
|
#import "Foundation/NSKeyedArchiver.h"
|
2019-06-12 15:23:33 +00:00
|
|
|
#import "Foundation/NSValue.h"
|
|
|
|
#import "Foundation/NSDictionary.h"
|
2019-06-08 12:07:17 +00:00
|
|
|
#import "GNUstepBase/GSObjCRuntime.h"
|
2019-05-18 07:56:11 +00:00
|
|
|
#import "GSPrivate.h"
|
2019-05-20 05:43:06 +00:00
|
|
|
#import "GSFastEnumeration.h"
|
|
|
|
#import "GSDispatch.h"
|
|
|
|
#import "GSSorting.h"
|
2019-05-18 07:56:11 +00:00
|
|
|
|
2019-06-08 12:07:17 +00:00
|
|
|
#define GSI_ARRAY_TYPES GSUNION_OBJ
|
2019-05-18 07:56:11 +00:00
|
|
|
|
2019-06-08 12:07:17 +00:00
|
|
|
#import "GNUstepBase/GSIArray.h"
|
2019-05-18 07:56:11 +00:00
|
|
|
|
2019-06-28 15:20:21 +00:00
|
|
|
#define GSI_MAP_HAS_VALUE 0
|
|
|
|
#define GSI_MAP_KTYPES GSUNION_OBJ
|
|
|
|
#define GSI_MAP_RETAIN_KEY(M, X)
|
|
|
|
#define GSI_MAP_RELEASE_KEY(M, X)
|
|
|
|
|
|
|
|
#include "GNUstepBase/GSIMap.h"
|
|
|
|
|
2019-05-18 07:56:11 +00:00
|
|
|
@interface GSOrderedSet : NSOrderedSet
|
|
|
|
{
|
|
|
|
@public
|
2019-06-08 12:07:17 +00:00
|
|
|
GSIArray_t array;
|
2019-06-28 15:20:21 +00:00
|
|
|
GSIMapTable_t map;
|
2019-05-18 07:56:11 +00:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2019-05-20 05:43:06 +00:00
|
|
|
@interface GSMutableOrderedSet : NSMutableOrderedSet
|
2019-05-18 07:56:11 +00:00
|
|
|
{
|
|
|
|
@public
|
2019-06-08 12:07:17 +00:00
|
|
|
GSIArray_t array;
|
2019-06-28 15:20:21 +00:00
|
|
|
GSIMapTable_t map;
|
2019-05-18 07:56:11 +00:00
|
|
|
@private
|
|
|
|
NSUInteger _version;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2019-05-20 05:43:06 +00:00
|
|
|
@interface GSOrderedSetEnumerator : NSEnumerator
|
2019-05-18 07:56:11 +00:00
|
|
|
{
|
2019-06-08 12:07:17 +00:00
|
|
|
GSOrderedSet *set;
|
|
|
|
unsigned current;
|
|
|
|
unsigned count;
|
2019-05-18 07:56:11 +00:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2019-06-10 17:55:00 +00:00
|
|
|
@interface GSOrderedSetEnumeratorReverse : GSOrderedSetEnumerator
|
|
|
|
@end
|
|
|
|
|
2019-05-20 05:43:06 +00:00
|
|
|
@implementation GSOrderedSetEnumerator
|
2019-05-27 10:05:19 +00:00
|
|
|
- (id) initWithOrderedSet: (NSOrderedSet*)d
|
2019-05-18 07:56:11 +00:00
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self != nil)
|
|
|
|
{
|
2019-05-20 05:43:06 +00:00
|
|
|
set = (GSOrderedSet*)RETAIN(d);
|
2019-06-08 12:07:17 +00:00
|
|
|
current = 0;
|
|
|
|
count = GSIArrayCount(&set->array);
|
2019-05-18 07:56:11 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) nextObject
|
|
|
|
{
|
2019-06-27 12:20:19 +00:00
|
|
|
if (current < count)
|
2019-05-18 07:56:11 +00:00
|
|
|
{
|
2019-06-08 12:07:17 +00:00
|
|
|
GSIArrayItem item = GSIArrayItemAtIndex(&set->array, current);
|
|
|
|
current++;
|
|
|
|
return (id)(item.obj);
|
2019-05-18 07:56:11 +00:00
|
|
|
}
|
2019-06-08 12:07:17 +00:00
|
|
|
return nil;
|
2019-05-18 07:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
RELEASE(set);
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2019-06-10 17:55:00 +00:00
|
|
|
|
|
|
|
@implementation GSOrderedSetEnumeratorReverse
|
|
|
|
- (id) initWithOrderedSet: (GSOrderedSet*)d
|
|
|
|
{
|
|
|
|
self = [super initWithOrderedSet: d];
|
2019-06-27 12:20:19 +00:00
|
|
|
if (self != nil)
|
2019-06-10 17:55:00 +00:00
|
|
|
{
|
|
|
|
current = GSIArrayCount(&set->array);
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) nextObject
|
|
|
|
{
|
|
|
|
GSIArrayItem item;
|
|
|
|
|
|
|
|
if (current == 0)
|
2019-06-27 12:20:19 +00:00
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2019-06-10 17:55:00 +00:00
|
|
|
item = GSIArrayItemAtIndex(&set->array, --current);
|
|
|
|
return (id)(item.obj);
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2019-05-20 05:43:06 +00:00
|
|
|
@implementation GSOrderedSet
|
2019-05-18 07:56:11 +00:00
|
|
|
|
|
|
|
static Class setClass;
|
|
|
|
static Class mutableSetClass;
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
{
|
2019-05-20 05:43:06 +00:00
|
|
|
if (self == [GSOrderedSet class])
|
2019-05-18 07:56:11 +00:00
|
|
|
{
|
2019-05-20 05:43:06 +00:00
|
|
|
setClass = [GSOrderedSet class];
|
|
|
|
mutableSetClass = [GSMutableOrderedSet class];
|
2019-05-18 07:56:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) copyWithZone: (NSZone*)z
|
|
|
|
{
|
|
|
|
return RETAIN(self);
|
|
|
|
}
|
|
|
|
|
2019-06-08 12:07:17 +00:00
|
|
|
- (void) dealloc
|
2019-05-18 07:56:11 +00:00
|
|
|
{
|
2019-06-08 12:07:17 +00:00
|
|
|
GSIArrayEmpty(&array);
|
2019-06-28 15:20:21 +00:00
|
|
|
GSIMapEmptyMap(&map);
|
2019-06-08 12:07:17 +00:00
|
|
|
[super dealloc];
|
2019-05-18 07:56:11 +00:00
|
|
|
}
|
|
|
|
|
2019-06-08 12:07:17 +00:00
|
|
|
- (NSUInteger) hash
|
2019-05-18 07:56:11 +00:00
|
|
|
{
|
2019-06-08 12:07:17 +00:00
|
|
|
return [self count];
|
2019-05-18 07:56:11 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 15:57:18 +00:00
|
|
|
- (instancetype) init
|
2019-05-18 07:56:11 +00:00
|
|
|
{
|
2019-06-10 19:24:18 +00:00
|
|
|
return [self initWithObjects: NULL count: 0];
|
2019-05-18 07:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSEnumerator*) objectEnumerator
|
|
|
|
{
|
2019-05-20 08:01:20 +00:00
|
|
|
return AUTORELEASE([[GSOrderedSetEnumerator alloc] initWithOrderedSet: self]);
|
2019-05-18 07:56:11 +00:00
|
|
|
}
|
|
|
|
|
2019-06-10 17:55:00 +00:00
|
|
|
- (NSEnumerator*) reverseObjectEnumerator
|
|
|
|
{
|
|
|
|
return AUTORELEASE([[GSOrderedSetEnumeratorReverse alloc] initWithOrderedSet: self]);
|
|
|
|
}
|
|
|
|
|
2019-05-18 07:56:11 +00:00
|
|
|
- (NSUInteger) sizeInBytesExcluding: (NSHashTable*)exclude
|
|
|
|
{
|
|
|
|
NSUInteger size = GSPrivateMemorySize(self, exclude);
|
|
|
|
|
|
|
|
if (size > 0)
|
|
|
|
{
|
2019-06-08 12:07:17 +00:00
|
|
|
NSUInteger count = [self count];
|
|
|
|
NSUInteger i = 0;
|
|
|
|
|
2019-06-27 12:20:19 +00:00
|
|
|
for (i = 0; i < count; i++)
|
2019-06-08 12:07:17 +00:00
|
|
|
{
|
|
|
|
GSIArrayItem item = GSIArrayItemAtIndex(&array, i);
|
|
|
|
size += [item.obj sizeInBytesExcluding: exclude];
|
2019-05-18 07:56:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2019-06-07 15:46:46 +00:00
|
|
|
// Put required overrides here...
|
2019-06-28 15:20:21 +00:00
|
|
|
- (BOOL) containsObject: (id)anObject
|
|
|
|
{
|
|
|
|
if (anObject != nil)
|
|
|
|
{
|
|
|
|
GSIMapNode node = GSIMapNodeForKey(&map, (GSIMapKey)anObject);
|
|
|
|
|
|
|
|
if (node != 0)
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2019-06-08 12:41:45 +00:00
|
|
|
- (NSUInteger) count
|
|
|
|
{
|
|
|
|
return GSIArrayCount(&array);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) objectAtIndex: (NSUInteger)index
|
|
|
|
{
|
|
|
|
GSIArrayItem item = GSIArrayItemAtIndex(&array, index);
|
|
|
|
return item.obj;
|
|
|
|
}
|
|
|
|
|
2019-06-29 18:08:01 +00:00
|
|
|
- (void) getObjects: (__unsafe_unretained id[])aBuffer range: (NSRange)aRange
|
|
|
|
{
|
|
|
|
NSUInteger i, j = 0;
|
|
|
|
NSUInteger c = GSIArrayCount(&array);
|
|
|
|
NSUInteger e = NSMaxRange(aRange);
|
|
|
|
|
|
|
|
GS_RANGE_CHECK(aRange, c);
|
|
|
|
|
|
|
|
for (i = aRange.location; i < e; i++)
|
|
|
|
{
|
|
|
|
GSIArrayItem item = GSIArrayItemAtIndex(&array, i);
|
|
|
|
aBuffer[j++] = item.obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-08 12:41:45 +00:00
|
|
|
/* Designated initialiser */
|
|
|
|
- (id) initWithObjects: (const id*)objs count: (NSUInteger)c
|
|
|
|
{
|
2019-06-09 11:17:57 +00:00
|
|
|
NSUInteger i = 0;
|
|
|
|
|
|
|
|
// Initialize and fill the set.
|
2019-06-08 12:41:45 +00:00
|
|
|
GSIArrayInitWithZoneAndCapacity(&array, [self zone], c);
|
2019-06-28 15:20:21 +00:00
|
|
|
GSIMapInitWithZoneAndCapacity(&map, [self zone], c);
|
2019-06-08 12:41:45 +00:00
|
|
|
for (i = 0; i < c; i++)
|
|
|
|
{
|
|
|
|
id obj = objs[i];
|
2019-06-27 12:20:19 +00:00
|
|
|
|
2019-06-28 15:20:21 +00:00
|
|
|
if (obj == nil)
|
2019-06-08 12:41:45 +00:00
|
|
|
{
|
|
|
|
DESTROY(self);
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"Tried to init set with nil value"];
|
|
|
|
}
|
2019-06-09 11:17:57 +00:00
|
|
|
|
2019-06-27 12:20:19 +00:00
|
|
|
if (![self containsObject: obj])
|
2019-06-09 11:17:57 +00:00
|
|
|
{
|
2019-06-28 15:20:21 +00:00
|
|
|
GSIArrayItem item;
|
|
|
|
|
|
|
|
item.obj = obj;
|
2019-06-09 11:17:57 +00:00
|
|
|
GSIArrayAddItem(&array, item);
|
2019-06-28 15:20:21 +00:00
|
|
|
GSIMapAddKey(&map, (GSIMapKey)obj);
|
2019-06-09 11:17:57 +00:00
|
|
|
}
|
2019-06-28 15:20:21 +00:00
|
|
|
}
|
2019-06-08 12:41:45 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2019-05-18 07:56:11 +00:00
|
|
|
@end
|
|
|
|
|
2019-05-20 05:43:06 +00:00
|
|
|
@implementation GSMutableOrderedSet
|
2019-05-18 07:56:11 +00:00
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
{
|
2019-05-20 05:43:06 +00:00
|
|
|
if (self == [GSMutableOrderedSet class])
|
2019-05-18 07:56:11 +00:00
|
|
|
{
|
2019-05-20 05:43:06 +00:00
|
|
|
GSObjCAddClassBehavior(self, [GSOrderedSet class]);
|
2019-05-18 07:56:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-08 12:41:45 +00:00
|
|
|
- (void) insertObject: (id)object atIndex: (NSUInteger)index
|
|
|
|
{
|
2019-06-27 12:20:19 +00:00
|
|
|
if (object == nil)
|
2019-06-10 18:15:41 +00:00
|
|
|
{
|
2019-06-19 02:07:11 +00:00
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"Tried to add nil to set"];
|
2019-06-17 15:57:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-06-27 12:20:19 +00:00
|
|
|
if ([self containsObject: object] == NO)
|
2019-06-17 15:57:18 +00:00
|
|
|
{
|
2019-06-28 15:20:21 +00:00
|
|
|
GSIArrayItem item;
|
|
|
|
|
|
|
|
_version++;
|
2019-06-17 15:57:18 +00:00
|
|
|
item.obj = object;
|
|
|
|
GSIArrayInsertItem(&array, item, index);
|
2019-06-28 15:20:21 +00:00
|
|
|
GSIMapAddKey(&map, (GSIMapKey)object);
|
2019-06-17 15:57:18 +00:00
|
|
|
_version++;
|
|
|
|
}
|
2019-06-10 18:15:41 +00:00
|
|
|
}
|
2019-06-08 12:41:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) removeObjectAtIndex: (NSUInteger)index
|
|
|
|
{
|
2019-06-28 15:20:21 +00:00
|
|
|
GSIArrayItem item = GSIArrayItemAtIndex(&array, index);
|
|
|
|
|
2019-06-11 09:45:37 +00:00
|
|
|
_version++;
|
2019-06-08 12:41:45 +00:00
|
|
|
GSIArrayRemoveItemAtIndex(&array, index);
|
2019-06-28 15:20:21 +00:00
|
|
|
GSIMapRemoveKey(&map, (GSIMapKey)item.obj);
|
|
|
|
_version++;
|
2019-06-08 12:41:45 +00:00
|
|
|
}
|
|
|
|
|
2019-05-18 07:56:11 +00:00
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
return [self initWithCapacity: 0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Designated initialiser */
|
|
|
|
- (id) initWithCapacity: (NSUInteger)cap
|
|
|
|
{
|
2019-06-08 12:07:17 +00:00
|
|
|
GSIArrayInitWithZoneAndCapacity(&array, [self zone], cap);
|
2019-06-28 15:20:21 +00:00
|
|
|
GSIMapInitWithZoneAndCapacity(&map, [self zone], cap);
|
2019-05-18 07:56:11 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithObjects: (const id*)objects
|
|
|
|
count: (NSUInteger)count
|
|
|
|
{
|
2019-06-08 12:07:17 +00:00
|
|
|
NSUInteger i = 0;
|
2019-05-18 07:56:11 +00:00
|
|
|
|
2019-06-09 11:17:57 +00:00
|
|
|
// Init and fill set
|
|
|
|
self = [self initWithCapacity: count];
|
2019-06-27 12:20:19 +00:00
|
|
|
if (self != nil)
|
2019-06-09 11:17:57 +00:00
|
|
|
{
|
2019-06-27 12:20:19 +00:00
|
|
|
for (i = 0; i < count; i++)
|
2019-05-18 07:56:11 +00:00
|
|
|
{
|
2019-06-27 12:20:19 +00:00
|
|
|
id anObject = objects[i];
|
2019-06-19 04:10:20 +00:00
|
|
|
[self addObject: anObject];
|
2019-05-18 07:56:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) makeImmutable
|
|
|
|
{
|
2019-05-20 05:43:06 +00:00
|
|
|
GSClassSwizzle(self, [GSOrderedSet class]);
|
2019-05-18 07:56:11 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) makeImmutableCopyOnFail: (BOOL)force
|
|
|
|
{
|
2019-05-20 05:43:06 +00:00
|
|
|
GSClassSwizzle(self, [GSOrderedSet class]);
|
2019-05-18 07:56:11 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2019-06-29 18:08:01 +00:00
|
|
|
- (NSUInteger) countByEnumeratingWithState: (NSFastEnumerationState *)state
|
|
|
|
objects: (__unsafe_unretained id[])stackbuf
|
|
|
|
count: (NSUInteger)len
|
|
|
|
{
|
|
|
|
NSInteger count;
|
|
|
|
|
|
|
|
/* This is cached in the caller at the start and compared at each
|
|
|
|
* iteration. If it changes during the iteration then
|
|
|
|
* objc_enumerationMutation() will be called, throwing an exception.
|
|
|
|
*/
|
|
|
|
state->mutationsPtr = &_version;
|
|
|
|
count = MIN(len, [self count] - state->state);
|
|
|
|
/* If a mutation has occurred then it's possible that we are being asked to
|
|
|
|
* get objects from after the end of the array. Don't pass negative values
|
|
|
|
* to memcpy.
|
|
|
|
*/
|
|
|
|
if (count > 0)
|
|
|
|
{
|
|
|
|
[self getObjects: stackbuf range: NSMakeRange(state->state, count)];
|
|
|
|
state->state += count;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
state->itemsPtr = stackbuf;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2019-05-18 07:56:11 +00:00
|
|
|
@end
|