2001-12-17 14:31:42 +00:00
|
|
|
/** NSMapTable implementation for GNUStep.
|
2009-03-16 10:54:59 +00:00
|
|
|
* Copyright (C) 2009 Free Software Foundation, Inc.
|
2005-02-22 11:22:44 +00:00
|
|
|
*
|
2009-03-16 10:54:59 +00:00
|
|
|
* Author: Richard Frith-Macdonald <rfm@gnu.org>
|
2005-02-22 11:22:44 +00:00
|
|
|
*
|
1996-05-12 00:56:10 +00:00
|
|
|
* This file is part of the GNUstep Base Library.
|
2005-02-22 11:22:44 +00:00
|
|
|
*
|
1996-02-13 02:31:48 +00:00
|
|
|
* This library is free software; you can redistribute it and/or
|
2007-09-14 11:36:11 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
1996-02-13 02:31:48 +00:00
|
|
|
* License as published by the Free Software Foundation; either
|
2008-06-08 10:38:33 +00:00
|
|
|
* version 2 of the License, or (at your option) any later version.
|
2005-02-22 11:22:44 +00:00
|
|
|
*
|
1996-02-13 02:31:48 +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.
|
2005-02-22 11:22:44 +00:00
|
|
|
*
|
2007-09-14 11:36:11 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
1996-02-13 02:31:48 +00:00
|
|
|
* License along with this library; if not, write to the Free
|
2006-06-16 15:21:39 +00:00
|
|
|
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
2019-12-09 23:36:00 +00:00
|
|
|
* Boston, MA 02110 USA.
|
2002-01-30 13:05:35 +00:00
|
|
|
*
|
|
|
|
* <title>NSMapTable class reference</title>
|
|
|
|
* $Date$ $Revision$
|
2001-12-18 16:54:15 +00:00
|
|
|
*/
|
1996-02-13 02:31:48 +00:00
|
|
|
|
2010-02-19 08:12:46 +00:00
|
|
|
#import "common.h"
|
2010-02-14 10:48:10 +00:00
|
|
|
#import "Foundation/NSArray.h"
|
|
|
|
#import "Foundation/NSDictionary.h"
|
|
|
|
#import "Foundation/NSException.h"
|
|
|
|
#import "Foundation/NSPointerFunctions.h"
|
|
|
|
#import "Foundation/NSMapTable.h"
|
|
|
|
#import "NSCallBacks.h"
|
1996-02-13 02:31:48 +00:00
|
|
|
|
2010-01-11 12:38:37 +00:00
|
|
|
@interface NSConcreteMapTable : NSMapTable
|
|
|
|
@end
|
|
|
|
|
2009-03-16 10:54:59 +00:00
|
|
|
@implementation NSMapTable
|
1996-02-13 02:31:48 +00:00
|
|
|
|
2009-03-16 10:54:59 +00:00
|
|
|
static Class abstractClass = 0;
|
|
|
|
static Class concreteClass = 0;
|
2009-03-09 15:11:51 +00:00
|
|
|
|
2009-03-16 10:54:59 +00:00
|
|
|
+ (id) allocWithZone: (NSZone*)aZone
|
2009-03-09 15:11:51 +00:00
|
|
|
{
|
2009-03-16 10:54:59 +00:00
|
|
|
if (self == abstractClass)
|
2009-03-09 15:11:51 +00:00
|
|
|
{
|
2009-03-16 10:54:59 +00:00
|
|
|
return NSAllocateObject(concreteClass, 0, aZone);
|
2009-03-09 15:11:51 +00:00
|
|
|
}
|
2009-03-16 10:54:59 +00:00
|
|
|
return NSAllocateObject(self, 0, aZone);
|
2009-03-09 15:11:51 +00:00
|
|
|
}
|
|
|
|
|
2009-03-16 10:54:59 +00:00
|
|
|
+ (void) initialize
|
1996-03-22 00:36:13 +00:00
|
|
|
{
|
2009-03-16 10:54:59 +00:00
|
|
|
if (abstractClass == 0)
|
2002-01-31 19:15:21 +00:00
|
|
|
{
|
2009-03-16 10:54:59 +00:00
|
|
|
abstractClass = [NSMapTable class];
|
2010-01-11 12:38:37 +00:00
|
|
|
concreteClass = [NSConcreteMapTable class];
|
2002-01-31 19:15:21 +00:00
|
|
|
}
|
1996-02-13 02:31:48 +00:00
|
|
|
}
|
|
|
|
|
2009-03-16 10:54:59 +00:00
|
|
|
+ (id) mapTableWithKeyOptions: (NSPointerFunctionsOptions)keyOptions
|
|
|
|
valueOptions: (NSPointerFunctionsOptions)valueOptions
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
2009-03-16 10:54:59 +00:00
|
|
|
NSMapTable *t;
|
1996-02-13 02:31:48 +00:00
|
|
|
|
2009-03-16 10:54:59 +00:00
|
|
|
t = [self allocWithZone: NSDefaultMallocZone()];
|
|
|
|
t = [t initWithKeyOptions: keyOptions
|
|
|
|
valueOptions: valueOptions
|
|
|
|
capacity: 0];
|
|
|
|
return AUTORELEASE(t);
|
1996-02-13 02:31:48 +00:00
|
|
|
}
|
|
|
|
|
2009-03-16 10:54:59 +00:00
|
|
|
+ (id) mapTableWithStrongToStrongObjects
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
2024-07-25 16:11:34 +00:00
|
|
|
GSOnceMLog(@"Garbage Collection no longer supported."
|
|
|
|
@" Using +strongToStrongObjectsMapTable");
|
|
|
|
return [self strongToStrongObjectsMapTable];
|
1996-02-13 02:31:48 +00:00
|
|
|
}
|
|
|
|
|
2009-03-16 10:54:59 +00:00
|
|
|
+ (id) mapTableWithStrongToWeakObjects
|
1996-03-22 00:36:13 +00:00
|
|
|
{
|
2024-07-25 16:11:34 +00:00
|
|
|
GSOnceMLog(@"Garbage Collection no longer supported."
|
|
|
|
@" Using +strongToWeakObjectsMapTable");
|
|
|
|
return [self strongToWeakObjectsMapTable];
|
1996-03-22 00:36:13 +00:00
|
|
|
}
|
|
|
|
|
2009-03-16 10:54:59 +00:00
|
|
|
+ (id) mapTableWithWeakToStrongObjects
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
2024-07-25 16:11:34 +00:00
|
|
|
GSOnceMLog(@"Garbage Collection no longer supported."
|
|
|
|
@" Using +weakToStringObjectsMapTable");
|
2024-07-31 09:22:48 +00:00
|
|
|
return [self weakToStrongObjectsMapTable];
|
1996-02-13 02:31:48 +00:00
|
|
|
}
|
|
|
|
|
2009-03-16 10:54:59 +00:00
|
|
|
+ (id) mapTableWithWeakToWeakObjects
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
2024-07-25 16:11:34 +00:00
|
|
|
GSOnceMLog(@"Garbage Collection no longer supported."
|
|
|
|
@" Using +weakToWeakObjectsMapTable");
|
|
|
|
return [self weakToWeakObjectsMapTable];
|
1996-02-13 02:31:48 +00:00
|
|
|
}
|
2012-12-12 13:51:07 +00:00
|
|
|
|
2012-12-11 17:49:28 +00:00
|
|
|
+ (id) strongToStrongObjectsMapTable
|
|
|
|
{
|
2017-12-08 17:32:36 +00:00
|
|
|
return [self mapTableWithKeyOptions: NSPointerFunctionsObjectPersonality
|
|
|
|
valueOptions: NSPointerFunctionsObjectPersonality];
|
2012-12-11 17:49:28 +00:00
|
|
|
}
|
2012-12-12 13:51:07 +00:00
|
|
|
|
2012-12-11 17:49:28 +00:00
|
|
|
+ (id) strongToWeakObjectsMapTable
|
|
|
|
{
|
2017-12-08 17:32:36 +00:00
|
|
|
return [self mapTableWithKeyOptions: NSPointerFunctionsObjectPersonality
|
|
|
|
valueOptions: NSPointerFunctionsObjectPersonality |
|
2012-12-11 17:49:28 +00:00
|
|
|
NSMapTableWeakMemory];
|
|
|
|
}
|
2012-12-12 13:51:07 +00:00
|
|
|
|
2012-12-11 17:49:28 +00:00
|
|
|
+ (id) weakToStrongObjectsMapTable
|
|
|
|
{
|
2017-12-08 17:32:36 +00:00
|
|
|
return [self mapTableWithKeyOptions: NSPointerFunctionsObjectPersonality |
|
2012-12-11 17:49:28 +00:00
|
|
|
NSMapTableWeakMemory
|
2017-12-08 17:32:36 +00:00
|
|
|
valueOptions: NSPointerFunctionsObjectPersonality];
|
2012-12-11 17:49:28 +00:00
|
|
|
}
|
2012-12-12 13:51:07 +00:00
|
|
|
|
2012-12-11 17:49:28 +00:00
|
|
|
+ (id) weakToWeakObjectsMapTable
|
|
|
|
{
|
2017-12-08 17:32:36 +00:00
|
|
|
return [self mapTableWithKeyOptions: NSPointerFunctionsObjectPersonality |
|
2012-12-11 17:49:28 +00:00
|
|
|
NSMapTableWeakMemory
|
2017-12-08 17:32:36 +00:00
|
|
|
valueOptions: NSPointerFunctionsObjectPersonality |
|
2012-12-11 17:49:28 +00:00
|
|
|
NSMapTableWeakMemory];
|
|
|
|
}
|
|
|
|
|
2009-03-16 10:54:59 +00:00
|
|
|
- (id) initWithKeyOptions: (NSPointerFunctionsOptions)keyOptions
|
|
|
|
valueOptions: (NSPointerFunctionsOptions)valueOptions
|
|
|
|
capacity: (NSUInteger)initialCapacity
|
2002-01-30 13:05:35 +00:00
|
|
|
{
|
2009-03-16 10:54:59 +00:00
|
|
|
NSPointerFunctions *k;
|
|
|
|
NSPointerFunctions *v;
|
|
|
|
id o;
|
|
|
|
|
|
|
|
k = [[NSPointerFunctions alloc] initWithOptions: keyOptions];
|
|
|
|
v = [[NSPointerFunctions alloc] initWithOptions: valueOptions];
|
|
|
|
o = [self initWithKeyPointerFunctions: k
|
|
|
|
valuePointerFunctions: v
|
|
|
|
capacity: initialCapacity];
|
|
|
|
[k release];
|
|
|
|
[v release];
|
|
|
|
return o;
|
1996-02-13 02:31:48 +00:00
|
|
|
}
|
|
|
|
|
2009-03-16 10:54:59 +00:00
|
|
|
- (id) initWithKeyPointerFunctions: (NSPointerFunctions*)keyFunctions
|
|
|
|
valuePointerFunctions: (NSPointerFunctions*)valueFunctions
|
|
|
|
capacity: (NSUInteger)initialCapacity
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
2011-02-11 14:51:47 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return nil;
|
1996-02-13 02:31:48 +00:00
|
|
|
}
|
|
|
|
|
2009-03-16 10:54:59 +00:00
|
|
|
- (id) copyWithZone: (NSZone*)aZone
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
2011-02-11 14:51:47 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return nil;
|
1996-02-13 02:31:48 +00:00
|
|
|
}
|
|
|
|
|
2009-03-16 10:54:59 +00:00
|
|
|
- (NSUInteger) count
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
2010-02-25 10:00:48 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return (NSUInteger)0;
|
1996-02-13 02:31:48 +00:00
|
|
|
}
|
|
|
|
|
2009-03-16 10:54:59 +00:00
|
|
|
- (NSUInteger) countByEnumeratingWithState: (NSFastEnumerationState*)state
|
|
|
|
objects: (id*)stackbuf
|
|
|
|
count: (NSUInteger)len
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
2010-02-25 10:00:48 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return (NSUInteger)0;
|
1996-02-13 02:31:48 +00:00
|
|
|
}
|
|
|
|
|
2009-03-16 10:54:59 +00:00
|
|
|
- (NSDictionary*) dictionaryRepresentation
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
2009-04-19 10:03:18 +00:00
|
|
|
NSEnumerator *enumerator;
|
|
|
|
NSMutableDictionary *dictionary;
|
|
|
|
id key;
|
|
|
|
|
|
|
|
dictionary = [NSMutableDictionary dictionaryWithCapacity: [self count]];
|
|
|
|
enumerator = [self keyEnumerator];
|
|
|
|
while ((key = [enumerator nextObject]) != nil)
|
|
|
|
{
|
|
|
|
[dictionary setObject: [self objectForKey: key] forKey: key];
|
|
|
|
}
|
|
|
|
return [[dictionary copy] autorelease];
|
1996-02-13 02:31:48 +00:00
|
|
|
}
|
|
|
|
|
2009-03-16 10:54:59 +00:00
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
2009-03-16 10:54:59 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
1996-02-13 02:31:48 +00:00
|
|
|
}
|
|
|
|
|
2009-03-16 10:54:59 +00:00
|
|
|
- (NSUInteger) hash
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
2009-04-19 10:03:18 +00:00
|
|
|
return [self count];
|
1996-02-13 02:31:48 +00:00
|
|
|
}
|
|
|
|
|
2009-03-16 10:54:59 +00:00
|
|
|
- (id) initWithCoder: (NSCoder*)aCoder
|
2002-01-30 13:05:35 +00:00
|
|
|
{
|
2011-02-11 14:51:47 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return nil;
|
2002-01-30 13:05:35 +00:00
|
|
|
}
|
1996-02-13 02:31:48 +00:00
|
|
|
|
2009-03-16 10:54:59 +00:00
|
|
|
- (BOOL) isEqual: (id)other
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
2009-04-17 08:12:52 +00:00
|
|
|
if ([other isKindOfClass: abstractClass] == NO) return NO;
|
|
|
|
return NSCompareMapTables(self, other);
|
1996-02-13 02:31:48 +00:00
|
|
|
}
|
|
|
|
|
2009-03-16 10:54:59 +00:00
|
|
|
- (NSEnumerator*) keyEnumerator
|
2002-01-30 13:05:35 +00:00
|
|
|
{
|
2009-03-16 10:54:59 +00:00
|
|
|
return [self subclassResponsibility: _cmd];
|
2002-01-30 13:05:35 +00:00
|
|
|
}
|
1996-02-13 02:31:48 +00:00
|
|
|
|
2009-03-16 10:54:59 +00:00
|
|
|
- (NSPointerFunctions*) keyPointerFunctions
|
2002-01-30 13:05:35 +00:00
|
|
|
{
|
2009-03-16 10:54:59 +00:00
|
|
|
return [self subclassResponsibility: _cmd];
|
2002-01-30 13:05:35 +00:00
|
|
|
}
|
|
|
|
|
2009-03-16 10:54:59 +00:00
|
|
|
- (NSEnumerator*) objectEnumerator
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
2009-03-16 10:54:59 +00:00
|
|
|
return [self subclassResponsibility: _cmd];
|
1996-02-13 02:31:48 +00:00
|
|
|
}
|
2002-01-30 13:05:35 +00:00
|
|
|
|
2009-03-16 10:54:59 +00:00
|
|
|
- (id) objectForKey: (id)aKey
|
2002-12-31 10:09:54 +00:00
|
|
|
{
|
2009-03-16 10:54:59 +00:00
|
|
|
return [self subclassResponsibility: _cmd];
|
|
|
|
}
|
2002-12-31 10:09:54 +00:00
|
|
|
|
2009-03-16 10:54:59 +00:00
|
|
|
- (void) removeAllObjects
|
2002-12-31 10:09:54 +00:00
|
|
|
{
|
2010-02-25 10:00:48 +00:00
|
|
|
NSUInteger count = [self count];
|
2009-04-19 10:03:18 +00:00
|
|
|
|
|
|
|
if (count > 0)
|
|
|
|
{
|
|
|
|
NSEnumerator *enumerator;
|
|
|
|
NSMutableArray *array;
|
|
|
|
id key;
|
|
|
|
|
|
|
|
array = [[NSMutableArray alloc] initWithCapacity: count];
|
|
|
|
enumerator = [self objectEnumerator];
|
|
|
|
while ((key = [enumerator nextObject]) != nil)
|
|
|
|
{
|
|
|
|
[array addObject: key];
|
|
|
|
}
|
|
|
|
enumerator = [array objectEnumerator];
|
|
|
|
while ((key = [enumerator nextObject]) != nil)
|
|
|
|
{
|
|
|
|
[self removeObjectForKey: key];
|
|
|
|
}
|
|
|
|
[array release];
|
|
|
|
}
|
2009-03-16 10:54:59 +00:00
|
|
|
}
|
2002-12-31 10:09:54 +00:00
|
|
|
|
2009-03-16 10:54:59 +00:00
|
|
|
- (void) removeObjectForKey: (id)aKey
|
2002-12-31 10:09:54 +00:00
|
|
|
{
|
2009-03-16 10:54:59 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
}
|
2002-12-31 10:09:54 +00:00
|
|
|
|
2009-03-16 10:54:59 +00:00
|
|
|
- (void) setObject: (id)anObject forKey: (id)aKey
|
2002-12-31 10:09:54 +00:00
|
|
|
{
|
2009-03-16 10:54:59 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
}
|
2002-12-31 10:09:54 +00:00
|
|
|
|
2009-03-16 10:54:59 +00:00
|
|
|
- (NSPointerFunctions*) valuePointerFunctions
|
2002-12-31 10:09:54 +00:00
|
|
|
{
|
2009-03-16 10:54:59 +00:00
|
|
|
return [self subclassResponsibility: _cmd];
|
|
|
|
}
|
|
|
|
@end
|
2002-01-30 13:05:35 +00:00
|
|
|
|