libs-base/Source/GSOrderedSet.m

408 lines
8.6 KiB
Mathematica
Raw Normal View History

/** Concrete implementation of GSOrderedSet based on GNU Set class
Copyright (C) 2019 Free Software Foundation, Inc.
Written by: Gregory Casamento <greg.casamento@gmail.com>
Created: May 17 2019
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
Library 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., 51 Franklin Street, Fifth Floor,
Boston, MA 02111 USA.
*/
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"
#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
#define GSI_ARRAY_TYPE NSRange
#define GSI_ARRAY_NO_RELEASE 1
#define GSI_ARRAY_NO_RETAIN 1
#define GSI_ARRAY_TYPES GSUNION_OBJ
2019-05-18 07:56:11 +00:00
#define GSI_ARRAY_RELEASE(A, X) [(X).obj release]
#define GSI_ARRAY_RETAIN(A, X) [(X).obj retain]
2019-05-18 07:56:11 +00:00
#import "GNUstepBase/GSIArray.h"
2019-05-18 07:56:11 +00:00
// static SEL memberSel;
2019-05-18 07:56:11 +00:00
static SEL privateCountOfSel;
@interface GSOrderedSet : NSOrderedSet
{
@public
GSIArray_t array;
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
GSIArray_t array;
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
{
GSOrderedSet *set;
unsigned current;
unsigned count;
2019-05-18 07:56:11 +00:00
}
@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);
current = 0;
count = GSIArrayCount(&set->array);
2019-05-18 07:56:11 +00:00
}
return self;
}
- (id) nextObject
{
if(current < count)
2019-05-18 07:56:11 +00:00
{
GSIArrayItem item = GSIArrayItemAtIndex(&set->array, current);
current++;
return (id)(item.obj);
2019-05-18 07:56:11 +00:00
}
return nil;
2019-05-18 07:56:11 +00:00
}
- (void) dealloc
{
RELEASE(set);
[super dealloc];
}
@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
privateCountOfSel = @selector(_countForObject:);
}
}
- (id) copyWithZone: (NSZone*)z
{
return RETAIN(self);
}
- (void) dealloc
2019-05-18 07:56:11 +00:00
{
GSIArrayEmpty(&array);
[super dealloc];
2019-05-18 07:56:11 +00:00
}
- (NSUInteger) hash
2019-05-18 07:56:11 +00:00
{
return [self count];
2019-05-18 07:56:11 +00:00
}
- (id) init
2019-05-18 07:56:11 +00:00
{
return [self initWithObjects: 0 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
}
- (NSUInteger) countByEnumeratingWithState: (NSFastEnumerationState*)state
objects: (id*)stackbuf
count: (NSUInteger)len
{
//state->mutationsPtr = (unsigned long *)self;
//return GSIMapCountByEnumeratingWithStateObjectsCount
// (&map, state, stackbuf, len);
return [self count];
2019-05-18 07:56:11 +00:00
}
- (NSUInteger) sizeInBytesExcluding: (NSHashTable*)exclude
{
NSUInteger size = GSPrivateMemorySize(self, exclude);
if (size > 0)
{
NSUInteger count = [self count];
NSUInteger i = 0;
for(i = 0; i < count; i++)
{
GSIArrayItem item = GSIArrayItemAtIndex(&array, i);
size += [item.obj sizeInBytesExcluding: exclude];
2019-05-18 07:56:11 +00:00
}
}
return size;
}
// Put required overrides here...
- (NSUInteger) count
{
return GSIArrayCount(&array);
}
- (id) objectAtIndex: (NSUInteger)index
{
GSIArrayItem item = GSIArrayItemAtIndex(&array, index);
return item.obj;
}
- (id) objectAtIndexedSubscript: (NSUInteger)index
{
return[self objectAtIndex: index];
}
- (BOOL) containsObject: (id)anObject
{
NSUInteger i = 0;
for (i = 0; i < [self count]; i++)
{
id obj = [self objectAtIndex: i];
if([anObject isEqual: obj])
{
return YES;
}
}
return NO;
}
/* Designated initialiser */
- (id) initWithObjects: (const id*)objs count: (NSUInteger)c
{
NSUInteger i = 0;
// Reduce C if it doesn't match. This is apparently how
// macOS behaves.
if(c > sizeof(objs))
{
c = sizeof(objs);
}
// Initialize and fill the set.
GSIArrayInitWithZoneAndCapacity(&array, [self zone], c);
for (i = 0; i < c; i++)
{
id obj = objs[i];
GSIArrayItem item;
if (objs[i] == nil)
{
DESTROY(self);
[NSException raise: NSInvalidArgumentException
format: @"Tried to init set with nil value"];
}
item.obj = obj;
if(![self containsObject: obj])
{
GSIArrayAddItem(&array, item);
RETAIN(obj);
}
}
return self;
}
- (id) initWithObject: (id)obj
{
id objs[] = {obj};
self = [self initWithObjects: objs count: 1];
if(self == nil)
{
NSLog(@"Problem initializing with one element");
}
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
}
}
- (void) addObject: (id)anObject
{
GSIArrayItem item;
2019-05-18 07:56:11 +00:00
if (anObject == nil)
{
[NSException raise: NSInvalidArgumentException
format: @"Tried to add nil to set"];
}
if([self containsObject: anObject] == NO)
{
item.obj = anObject;
GSIArrayAddItem(&array, item);
RETAIN(anObject);
_version++;
}
2019-05-18 07:56:11 +00:00
}
- (void) insertObject: (id)object atIndex: (NSUInteger)index
{
GSIArrayItem item;
item.obj = object;
GSIArrayInsertItem(&array, item, index);
}
- (void) removeObjectAtIndex: (NSUInteger)index
{
GSIArrayRemoveItemAtIndex(&array, index);
}
- (void) replaceObjectAtIndex: (NSUInteger)index
withObject: (id)obj
{
[self removeObjectAtIndex: index];
[self insertObject: obj atIndex: index];
}
2019-05-18 07:56:11 +00:00
- (id) init
{
return [self initWithCapacity: 0];
}
/* Designated initialiser */
- (id) initWithCapacity: (NSUInteger)cap
{
GSIArrayInitWithZoneAndCapacity(&array, [self zone], cap);
2019-05-18 07:56:11 +00:00
return self;
}
- (id) initWithObjects: (const id*)objects
count: (NSUInteger)count
{
NSUInteger i = 0;
2019-05-18 07:56:11 +00:00
// Reduce count if more then size of list of objects
if(sizeof(objects) < count)
2019-05-18 07:56:11 +00:00
{
count = sizeof(objects);
}
// Init and fill set
self = [self initWithCapacity: count];
if(self != nil)
{
for(i = 0; i < count; i++)
2019-05-18 07:56:11 +00:00
{
id anObject = objects[i];
if (anObject == nil)
{
NSLog(@"Tried to init an orderedset with a nil object");
continue;
}
else
{
GSIArrayItem item;
if(![self containsObject: anObject])
{
item.obj = anObject;
GSIArrayAddItem(&array, item);
RETAIN(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;
}
- (NSUInteger) countByEnumeratingWithState: (NSFastEnumerationState*)state
objects: (id*)stackbuf
count: (NSUInteger)len
{
//state->mutationsPtr = (unsigned long *)&_version;
//return GSIMapCountByEnumeratingWithStateObjectsCount
// (&map, state, stackbuf, len);
return [self count];
2019-05-18 07:56:11 +00:00
}
@end
2019-05-20 05:43:06 +00:00
@interface NSGOrderedSet : NSOrderedSet
2019-05-18 07:56:11 +00:00
@end
2019-05-20 05:43:06 +00:00
@implementation NSGOrderedSet
2019-05-18 07:56:11 +00:00
- (id) initWithCoder: (NSCoder*)aCoder
{
NSLog(@"Warning - decoding archive containing obsolete %@ object - please delete/replace this archive", NSStringFromClass([self class]));
DESTROY(self);
2019-05-20 05:43:06 +00:00
self = (id)NSAllocateObject([GSOrderedSet class], 0, NSDefaultMallocZone());
2019-05-18 07:56:11 +00:00
self = [self initWithCoder: aCoder];
return self;
}
@end
2019-05-20 05:43:06 +00:00
@interface NSGMutableOrderedSet : NSMutableOrderedSet
2019-05-18 07:56:11 +00:00
@end
2019-05-20 05:43:06 +00:00
@implementation NSGMutableOrderedSet
2019-05-18 07:56:11 +00:00
- (id) initWithCoder: (NSCoder*)aCoder
{
NSLog(@"Warning - decoding archive containing obsolete %@ object - please delete/replace this archive", NSStringFromClass([self class]));
DESTROY(self);
2019-05-20 05:43:06 +00:00
self = (id)NSAllocateObject([GSMutableOrderedSet class], 0, NSDefaultMallocZone());
2019-05-18 07:56:11 +00:00
self = [self initWithCoder: aCoder];
return self;
}
@end