ftp.nice.ch/Attic/openStep/implementation/gnustep/sources/libFoundation.0.7.tgz#/libFoundation-0.7/libFoundation/Foundation/NSConcreteDictionary.m

This is NSConcreteDictionary.m in view mode; [Download] [Up]

/* 
   NSConcreteDictionary.m

   Copyright (C) 1995, 1996 Ovidiu Predescu and Mircea Oancea.
   All rights reserved.

   Author: Mircea Oancea <mircea@jupiter.elcom.pub.ro>

   This file is part of libFoundation.

   Permission to use, copy, modify, and distribute this software and its
   documentation for any purpose and without fee is hereby granted, provided
   that the above copyright notice appear in all copies and that both that
   copyright notice and this permission notice appear in supporting
   documentation.

   We disclaim all warranties with regard to this software, including all
   implied warranties of merchantability and fitness, in no event shall
   we be liable for any special, indirect or consequential damages or any
   damages whatsoever resulting from loss of use, data or profits, whether in
   an action of contract, negligence or other tortious action, arising out of
   or in connection with the use or performance of this software.
*/

#include <stdarg.h>

#include <Foundation/NSObject.h>
#include <Foundation/NSDictionary.h>
#include <Foundation/NSArray.h>
#include <Foundation/NSString.h>
#include <Foundation/NSException.h>
#include <Foundation/exceptions/GeneralExceptions.h>

#include <extensions/objc-runtime.h>

#include "NSConcreteDictionary.h"

/*
 * NSConcreteDictionary class
 */

@implementation NSConcreteDictionary

/* Allocating and Initializing */

- (id)init
{
    return [self initWithDictionary:nil];
}

- (id)initWithObjects:(id*)objects
    forKeys:(id*)keys 
    count:(unsigned int)count
{
    table = NSCreateMapTableWithZone(NSObjectMapKeyCallBacks,
		NSObjectMapValueCallBacks, 
		(count * 4) / 3,
		[self zone]);
    if (!count)
	return self;
    while(count--) {
	if (!keys[count] || !objects[count])
	    THROW([[InvalidArgumentException alloc] 
		    initWithReason:@"Nil object to be added in dictionary"]);
	NSMapInsert(table, keys[count], objects[count]);
    }
    return self;
}

- (id)initWithDictionary:(NSDictionary*)dictionary
{
    id keys = [dictionary keyEnumerator];
    id key;

    table = NSCreateMapTableWithZone(NSObjectMapKeyCallBacks,
		NSObjectMapValueCallBacks, 
		([dictionary count] * 4) / 3,
		[self zone]);
	    
    while ((key = [keys nextObject]))
	NSMapInsert(table, key, [dictionary objectForKey:key]);
    
    return self;
}

- (void)dealloc
{
    if (table)
	NSFreeMapTable(table);
    [super dealloc];
}

/* Accessing keys and values */

- (NSEnumerator *)keyEnumerator
{
    return [[[_NSConcreteDictionaryKeyEnumerator alloc]
		initWithDictionary:self]
		autorelease];
}

- (id)objectForKey:(id)aKey
{
    return (NSObject*)NSMapGet(table, aKey);
}

- (unsigned int)count
{
    return NSCountMapTable(table);
}

/* Private */

- (NSMapEnumerator)__keyEnumerator;
{
    return NSEnumerateMapTable(table);
}

@end /* NSConcreteDictionary */

/*
* NSConcreteMutableDictionary class
*/

@implementation NSConcreteMutableDictionary

+ (void)initialize
{
    static BOOL initialized = NO;
    if(!initialized) {
	initialized = YES;
	class_add_behavior(self, [NSConcreteDictionary class]);
    }
}

/* Allocating and Initializing */

- (id)init
{
    return [self initWithCapacity:0];
}

- (id)initWithCapacity:(unsigned int)aNumItems
{
    table = NSCreateMapTableWithZone(NSObjectMapKeyCallBacks,
		NSObjectMapValueCallBacks, 
		(aNumItems * 4) / 3,
		[self zone]);
    return self;
}

/* Modifying dictionary */

- (void)setObject:(id)anObject forKey:(id)aKey
{
    if (!anObject || !aKey)
	THROW ([[InvalidArgumentException alloc]
		    initWithFormat:@"nil object to be added in dictionary"]);
    NSMapInsert(table, aKey, anObject);
}

- (void)removeObjectForKey:(id)aKey
{
    NSMapRemove(table, aKey);
}

- (void)removeAllObjects
{
    NSResetMapTable(table);
}

@end /* NSConcreteMutableDictionary */

/*
* NSConcreteDictionary Enumerator classes
*/

@implementation _NSConcreteDictionaryKeyEnumerator

- initWithDictionary:(NSDictionary*)_dict
{
    dict = [_dict retain];
    enumerator = [(NSConcreteDictionary*)dict __keyEnumerator];
    return self;
}

- (void)dealloc
{
    [dict release];
    [super dealloc];
}

- nextObject
{
    id key, value;
    return NSNextMapEnumeratorPair(&enumerator, 
		(void**)&key, (void**)&value) == YES ? 
		key : nil;
}

@end /* _NSConcreteDictionaryKeyEnumerator */

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.