/* 
   NSConcreteSet.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/NSValue.h>

#include <Foundation/NSException.h>
#include "NSConcreteSet.h"

/*
 * NSConcreteSet
 */

@implementation NSConcreteSet

/* Allocating and Initializing */

- (id)init
{
    table = NSCreateHashTableWithZone(NSObjectHashCallBacks, 0, [self zone]);
    return self;
}

- (id)initWithObjects:(id*)objects count:(unsigned int)count
{
    int i;
    table = NSCreateHashTableWithZone(NSObjectHashCallBacks, 
				      count, [self zone]);
    for (i = 0; i < count; i++)
	NSHashInsert(table, objects[i]);
    return self;
}

- (id)initWithSet:(NSSet *)set copyItems:(BOOL)flag
{
    id en = [set objectEnumerator];
    id obj;
    
    table = NSCreateHashTableWithZone(NSObjectHashCallBacks, 
					[set count], [self zone]);
    while((obj = [en nextObject]))
	NSHashInsert(table, flag ? [obj copy] : obj);
    return self;
}

/* Destroying */

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

/* Copying */

- (id)copy
{
    return [self retain];
}

- (id)copyWithZone:(NSZone*)zone
{
    return (zone == [self zone]) ? [self retain] : 
	    [[NSConcreteSet allocWithZone:zone] 
	    initWithSet:self copyItems:YES];
}

/* Accessing keys and values */

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

- (id)member:(id)anObject
{
    return (NSObject*)NSHashGet(table, anObject);
}

/* Entries */

- (NSEnumerator *)objectEnumerator
{
    return [[[_NSConcreteSetEnumerator alloc]
	    initWithSet:self mode:setEnumHash]
	    autorelease];
}

/* Private methods */

- (void)__setObjectEnumerator:(void*)en;
{
    *((NSHashEnumerator*)en) = NSEnumerateHashTable(table);
}

@end /* NSConcreteSet */

/*
 * NSConcreteMutableSet
 */
 
@implementation NSConcreteMutableSet

/* Allocating and Initializing */

- (id)init
{
    table = NSCreateHashTableWithZone(NSObjectHashCallBacks, 0, [self zone]);
    return self;
}

- (id)initWithObjects:(id*)objects count:(unsigned int)count
{
    int i;
    table = NSCreateHashTableWithZone(NSObjectHashCallBacks, 
				      count, [self zone]);
    for (i = 0; i < count; i++)
	NSHashInsert(table, objects[i]);
    return self;
}

- (id)initWithSet:(NSSet *)set copyItems:(BOOL)flag
{
    id en = [set objectEnumerator];
    id obj;
    
    table = NSCreateHashTableWithZone(NSObjectHashCallBacks, 
					[set count], [self zone]);
    while((obj = [en nextObject]))
	NSHashInsert(table, flag ? [obj copy] : obj);
    return self;
}

/* Destroying */

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

/* Accessing keys and values */

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

- (id)member:(id)anObject
{
    return (NSObject*)NSHashGet(table, anObject);
}

/* Entries */

- (NSEnumerator *)objectEnumerator
{
    return [[[_NSConcreteSetEnumerator alloc]
	    initWithSet:self mode:setEnumHash]
	    autorelease];
}

/* Add and remove entries */

- (void)addObject:(id)object
{
    NSHashInsert(table, object);
}

- (void)removeObject:(id)object
{
    NSHashRemove(table, object);
}

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

/* Private methods */

- (void)__setObjectEnumerator:(void*)en;
{
    *((NSHashEnumerator*)en) = NSEnumerateHashTable(table);
}

@end /* NSConcreteMutableSet */

/*
 * NSCountedSet
 */

@implementation NSCountedSet

/* Allocating and Initializing */

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

- (id)initWithObjects:(id*)objects count:(unsigned int)count
{
    int i;
    [self initWithCapacity:count];
    for (i = 0; i < count; i++)
	[self addObject:objects[i]];
    return self;
}

- (id)initWithSet:(NSSet *)set copyItems:(BOOL)flag
{
    id en = [set objectEnumerator];
    id obj;
    
    [self initWithCapacity:[set count]];
    while((obj = [en nextObject]))
	[self addObject:flag ? [obj copy] : obj];
    return self;
}

- (id)initWithCapacity:(unsigned int)aNumItems
{
    table = NSCreateMapTableWithZone(NSObjectMapKeyCallBacks,
				     NSIntMapValueCallBacks,
				     aNumItems,
				     [self zone]);
    return self;
}

/* Destroying */

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

/* Copying */

- (id)mutableCopyWithZone:(NSZone*)zone
{
    return [[NSCountedSet allocWithZone:zone] 
	    initWithSet:self];
}

/* Accessing keys and values */

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

- (id)member:(id)anObject
{
    id key, value;
    return NSMapMember(table, (void*)anObject, (void**)&key, (void**)&value) ?
	    key : nil;
}

- (unsigned int)countForObject:(id)anObject
{
    return (unsigned)NSMapGet(table, anObject);
}

- (NSEnumerator *)objectEnumerator
{
    return [[[_NSConcreteSetEnumerator alloc]
	    initWithSet:self mode:setEnumMap]
	    autorelease];
}

/* Add and remove entries */

- (void)addObject:(id)object
{
    NSMapInsert(table, object, (void*)((unsigned)NSMapGet(table, object)+1));
}

- (void)removeObject:(id)object
{
    NSMapRemove(table, object);
}

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

- (NSString*)descriptionWithLocale:(NSDictionary*)locale
   indent:(unsigned int)level;
{
    NSMutableDictionary* dict = [[[NSMutableDictionary alloc] 
	    initWithCapacity:[self count]] autorelease];
    NSEnumerator* enumerator = [self objectEnumerator];
    id key;
    
    while((key = [enumerator nextObject]))
	[dict setObject:
		[NSNumber numberWithUnsignedInt:[self countForObject:key]] 
	    forKey:key];
    
    return [dict descriptionWithLocale:locale indent:level];
}

/* Private methods */

- (void)__setObjectEnumerator:(void*)en;
{
    *((NSMapEnumerator*)en) = NSEnumerateMapTable(table);
}

@end

/*
* _NSConcreteSetEnumerator
*/

@implementation _NSConcreteSetEnumerator

- initWithSet:(NSSet*)_set mode:(SetEnumMode)_mode;
{
    set = [_set retain];
    mode = _mode;
    if (mode == setEnumHash)
	[set __setObjectEnumerator:&(enumerator.hash)];
    if (mode == setEnumMap)
	[set __setObjectEnumerator:&(enumerator.map)];
    return self;
}

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

- nextObject
{
    if (mode == setEnumHash) {
	return (id)NSNextHashEnumeratorItem(&(enumerator.hash));
    }
    if (mode == setEnumMap) {
	id key, value;
	return NSNextMapEnumeratorPair(&(enumerator.map),
					(void**)&key,(void**)&value)==YES ?
					key : nil;
    }
    return nil;
}

@end /* _NSConcreteSetEnumerator */
