1996-02-13 02:31:48 +00:00
|
|
|
/* NSHashTable implementation for GNUStep.
|
|
|
|
* Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* Author: Albin L. Jones <Albin.L.Jones@Dartmouth.EDU>
|
|
|
|
* Created: Mon Dec 12 23:54:09 EST 1994
|
1996-02-22 15:25:44 +00:00
|
|
|
* Updated: Mon Feb 12 22:55:15 EST 1996
|
|
|
|
* Serial: 96.02.12.01
|
1996-02-13 02:31:48 +00:00
|
|
|
*
|
|
|
|
* This file is part of the GNU Objective C Class Library.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library 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 Library General Public
|
|
|
|
* License along with this library; if not, write to the Free
|
|
|
|
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**** Included Headers *******************************************************/
|
|
|
|
|
|
|
|
#include <Foundation/NSString.h>
|
|
|
|
#include <Foundation/NSArray.h>
|
|
|
|
#include <Foundation/NSException.h>
|
|
|
|
#include <Foundation/NSHashTable.h>
|
1996-02-22 15:25:44 +00:00
|
|
|
#include <NSCallBacks.h>
|
1996-02-13 02:31:48 +00:00
|
|
|
#include <Foundation/atoz.h>
|
|
|
|
#include <objects/hash.h>
|
|
|
|
|
|
|
|
/**** Type, Constant, and Macro Definitions **********************************/
|
|
|
|
|
|
|
|
/* These are to increase readabilty locally. */
|
|
|
|
typedef unsigned int (*NSHT_hash_func_t)(NSHashTable *, const void *);
|
1996-02-22 15:25:44 +00:00
|
|
|
typedef BOOL (*NSHT_isEqual_func_t)(NSHashTable *, const void *, const void *);
|
1996-02-13 02:31:48 +00:00
|
|
|
typedef void (*NSHT_retain_func_t)(NSHashTable *, const void *);
|
|
|
|
typedef void (*NSHT_release_func_t)(NSHashTable *, void *);
|
|
|
|
typedef NSString *(*NSHT_describe_func_t)(NSHashTable *, const void *);
|
|
|
|
|
|
|
|
/** Standard NSHashTable callbacks **/
|
|
|
|
|
|
|
|
const NSHashTableCallBacks NSIntHashCallBacks =
|
|
|
|
{
|
1996-02-22 15:25:44 +00:00
|
|
|
(NSHT_hash_func_t) _NS_int_hash,
|
|
|
|
(NSHT_isEqual_func_t) _NS_int_is_equal,
|
|
|
|
(NSHT_retain_func_t) _NS_int_retain,
|
|
|
|
(NSHT_release_func_t) _NS_int_release,
|
|
|
|
(NSHT_describe_func_t) _NS_int_describe
|
1996-02-13 02:31:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const NSHashTableCallBacks NSNonOwnedPointerHashCallBacks =
|
|
|
|
{
|
1996-02-22 15:25:44 +00:00
|
|
|
(NSHT_hash_func_t) _NS_owned_void_p_hash,
|
|
|
|
(NSHT_isEqual_func_t) _NS_owned_void_p_is_equal,
|
|
|
|
(NSHT_retain_func_t) _NS_owned_void_p_retain,
|
|
|
|
(NSHT_release_func_t) _NS_owned_void_p_release,
|
|
|
|
(NSHT_describe_func_t) _NS_owned_void_p_describe
|
1996-02-13 02:31:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const NSHashTableCallBacks NSNonRetainedObjectsHashCallBacks =
|
|
|
|
{
|
1996-02-22 15:25:44 +00:00
|
|
|
(NSHT_hash_func_t) _NS_non_retained_id_hash,
|
|
|
|
(NSHT_isEqual_func_t) _NS_non_retained_id_is_equal,
|
|
|
|
(NSHT_retain_func_t) _NS_non_retained_id_retain,
|
|
|
|
(NSHT_release_func_t) _NS_non_retained_id_release,
|
|
|
|
(NSHT_describe_func_t) _NS_non_retained_id_describe
|
1996-02-13 02:31:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const NSHashTableCallBacks NSObjectsHashCallBacks =
|
|
|
|
{
|
1996-02-22 15:25:44 +00:00
|
|
|
(NSHT_hash_func_t) _NS_id_hash,
|
|
|
|
(NSHT_isEqual_func_t) _NS_id_is_equal,
|
|
|
|
(NSHT_retain_func_t) _NS_id_retain,
|
|
|
|
(NSHT_release_func_t) _NS_id_release,
|
|
|
|
(NSHT_describe_func_t) _NS_id_describe
|
1996-02-13 02:31:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const NSHashTableCallBacks NSOwnedPointerHashCallBacks =
|
|
|
|
{
|
1996-02-22 15:25:44 +00:00
|
|
|
(NSHT_hash_func_t) _NS_owned_void_p_hash,
|
|
|
|
(NSHT_isEqual_func_t) _NS_owned_void_p_is_equal,
|
|
|
|
(NSHT_retain_func_t) _NS_owned_void_p_retain,
|
|
|
|
(NSHT_release_func_t) _NS_owned_void_p_release,
|
|
|
|
(NSHT_describe_func_t) _NS_owned_void_p_describe
|
1996-02-13 02:31:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const NSHashTableCallBacks NSPointerToStructHashCallBacks =
|
|
|
|
{
|
1996-02-22 15:25:44 +00:00
|
|
|
(NSHT_hash_func_t) _NS_int_p_hash,
|
|
|
|
(NSHT_isEqual_func_t) _NS_int_p_is_equal,
|
|
|
|
(NSHT_retain_func_t) _NS_int_p_retain,
|
|
|
|
(NSHT_release_func_t) _NS_int_p_release,
|
|
|
|
(NSHT_describe_func_t) _NS_int_p_describe
|
1996-02-13 02:31:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Macros **/
|
|
|
|
|
|
|
|
#define NSHT_ZONE(T) \
|
1996-02-22 15:25:44 +00:00
|
|
|
((NSZone *)((objects_hash_allocs((objects_hash_t *)(T))).user_data))
|
1996-02-13 02:31:48 +00:00
|
|
|
|
|
|
|
#define NSHT_CALLBACKS(T) \
|
1996-02-22 15:25:44 +00:00
|
|
|
(*((NSHashTableCallBacks *)(objects_hash_extra((objects_hash_t *)(T)))))
|
1996-02-13 02:31:48 +00:00
|
|
|
|
|
|
|
#define NSHT_DESCRIBE(T, P) \
|
|
|
|
NSHT_CALLBACKS((T)).describe((T), (P))
|
|
|
|
|
|
|
|
/** Dummy callbacks **/
|
|
|
|
|
|
|
|
size_t
|
1996-02-22 15:25:44 +00:00
|
|
|
_NSHT_hash (const void *element, const void *table)
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
1996-02-22 15:25:44 +00:00
|
|
|
return (NSHT_CALLBACKS(table)).hash ((NSHashTable *)table, element);
|
1996-02-13 02:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1996-02-22 15:25:44 +00:00
|
|
|
_NSHT_compare (const void *element1, const void *element2, const void *table)
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
1996-02-22 15:25:44 +00:00
|
|
|
return !((NSHT_CALLBACKS(table)).isEqual ((NSHashTable *)table,
|
|
|
|
element1,
|
|
|
|
element2));
|
1996-02-13 02:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1996-02-22 15:25:44 +00:00
|
|
|
_NSHT_is_equal (const void *element1, const void *element2, const void *table)
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
1996-02-22 15:25:44 +00:00
|
|
|
return (NSHT_CALLBACKS(table)).isEqual ((NSHashTable *) table,
|
|
|
|
element1,
|
|
|
|
element2);
|
1996-02-13 02:31:48 +00:00
|
|
|
}
|
|
|
|
|
1996-02-22 15:25:44 +00:00
|
|
|
void *
|
|
|
|
_NSHT_retain (const void *element, const void *table)
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
1996-02-22 15:25:44 +00:00
|
|
|
(NSHT_CALLBACKS(table)).retain ((NSHashTable *)table, element);
|
|
|
|
return (void*) element;
|
1996-02-13 02:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1996-02-22 15:25:44 +00:00
|
|
|
_NSHT_release (void *element, const void *table)
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
1996-02-22 15:25:44 +00:00
|
|
|
(NSHT_CALLBACKS(table)).release ((NSHashTable*)table, (void*)element);
|
1996-02-13 02:31:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* These are wrappers for getting at the real callbacks. */
|
1996-02-22 15:25:44 +00:00
|
|
|
const objects_callbacks_t _NSHT_callbacks =
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
|
|
|
_NSHT_hash,
|
|
|
|
_NSHT_compare,
|
|
|
|
_NSHT_is_equal,
|
|
|
|
_NSHT_retain,
|
|
|
|
_NSHT_release,
|
1996-02-22 15:25:44 +00:00
|
|
|
0,
|
1996-02-13 02:31:48 +00:00
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Extra, extra **/
|
|
|
|
|
|
|
|
/* Make a copy of a hash table's callbacks. */
|
1996-02-22 15:25:44 +00:00
|
|
|
void *
|
|
|
|
_NSHT_extra_retain (const void *extra, const void *table)
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
|
|
|
/* Pick out the callbacks in EXTRA. */
|
1996-02-22 15:25:44 +00:00
|
|
|
NSHashTableCallBacks *callBacks = (NSHashTableCallBacks *)(extra);
|
1996-02-13 02:31:48 +00:00
|
|
|
|
|
|
|
/* Find our zone. */
|
|
|
|
NSZone *zone = NSHT_ZONE(table);
|
|
|
|
|
|
|
|
/* A pointer to some new callbacks. */
|
1996-02-22 15:25:44 +00:00
|
|
|
NSHashTableCallBacks *newCallBacks;
|
1996-02-13 02:31:48 +00:00
|
|
|
|
|
|
|
/* Set aside space for our new callbacks in the right zone. */
|
1996-02-22 15:25:44 +00:00
|
|
|
newCallBacks = (NSHashTableCallBacks *)
|
|
|
|
NSZoneMalloc(zone,
|
|
|
|
sizeof(NSHashTableCallBacks));
|
1996-02-13 02:31:48 +00:00
|
|
|
|
|
|
|
/* Copy CALLBACKS into NEWCALLBACKS. */
|
|
|
|
*newCallBacks = *callBacks;
|
|
|
|
|
|
|
|
/* Return our new EXTRA. */
|
1996-02-22 15:25:44 +00:00
|
|
|
return (void*) extra;
|
1996-02-13 02:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1996-02-22 15:25:44 +00:00
|
|
|
_NSHT_extra_release (void *extra, const void *table)
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
|
|
|
NSZone *zone = NSHT_ZONE(table);
|
|
|
|
|
1996-02-22 15:25:44 +00:00
|
|
|
if (extra != NULL)
|
|
|
|
NSZoneFree(zone, (void*)extra);
|
1996-02-13 02:31:48 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The idea here is that these callbacks ensure that the
|
|
|
|
* NSHashTableCallbacks which are associated with a given NSHashTable
|
1996-02-22 15:25:44 +00:00
|
|
|
* remain so associated throughout the life of the table and its copies. */
|
|
|
|
objects_callbacks_t _NSHT_extra_callbacks =
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
1996-02-22 15:25:44 +00:00
|
|
|
(objects_hash_func_t) objects_void_p_hash,
|
|
|
|
(objects_compare_func_t) objects_void_p_compare,
|
|
|
|
(objects_is_equal_func_t) objects_void_p_is_equal,
|
1996-02-13 02:31:48 +00:00
|
|
|
_NSHT_extra_retain,
|
|
|
|
_NSHT_extra_release,
|
1996-02-22 15:25:44 +00:00
|
|
|
0,
|
1996-02-13 02:31:48 +00:00
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
/**** Function Implementations ***********************************************/
|
|
|
|
|
|
|
|
/** Creating NSHashTables **/
|
|
|
|
|
|
|
|
NSHashTable *
|
1996-02-22 15:25:44 +00:00
|
|
|
NSCreateHashTableWithZone (NSHashTableCallBacks callBacks,
|
|
|
|
unsigned int capacity,
|
|
|
|
NSZone *zone)
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
|
|
|
NSHashTable *table;
|
1996-02-22 15:25:44 +00:00
|
|
|
objects_callbacks_t callbacks;
|
|
|
|
objects_allocs_t allocs;
|
1996-02-13 02:31:48 +00:00
|
|
|
|
|
|
|
/* These callbacks just look in the TABLE's extra and uses the
|
|
|
|
* callbacks there. See above for precise definitions. */
|
|
|
|
callbacks = _NSHT_callbacks;
|
1996-02-22 15:25:44 +00:00
|
|
|
allocs = objects_allocs_for_zone(zone);
|
1996-02-13 02:31:48 +00:00
|
|
|
|
|
|
|
/* Then we build the table. */
|
1996-02-22 15:25:44 +00:00
|
|
|
table = objects_hash_with_allocs_with_callbacks(allocs, callbacks);
|
1996-02-13 02:31:48 +00:00
|
|
|
|
|
|
|
if (table != NULL)
|
|
|
|
{
|
1996-02-22 15:25:44 +00:00
|
|
|
const void *extra;
|
1996-02-13 02:31:48 +00:00
|
|
|
|
|
|
|
/* Resize TABLE to CAPACITY. */
|
1996-02-22 15:25:44 +00:00
|
|
|
objects_hash_resize(table, capacity);
|
1996-02-13 02:31:48 +00:00
|
|
|
|
|
|
|
/* Set aside space for the NSHashTableExtra. */
|
1996-02-22 15:25:44 +00:00
|
|
|
extra = &callBacks;
|
1996-02-13 02:31:48 +00:00
|
|
|
|
|
|
|
/* Add EXTRA to TABLE. This takes care of everything for us. */
|
1996-02-22 15:25:44 +00:00
|
|
|
objects_hash_set_extra_callbacks(table, _NSHT_extra_callbacks);
|
|
|
|
objects_hash_set_extra(table, extra);
|
1996-02-13 02:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Wah-hoo! */
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSHashTable *
|
1996-02-22 15:25:44 +00:00
|
|
|
NSCreateHashTable (NSHashTableCallBacks callBacks,
|
|
|
|
unsigned int capacity)
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
|
|
|
return NSCreateHashTableWithZone(callBacks, capacity, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Copying **/
|
|
|
|
|
|
|
|
NSHashTable *
|
1996-02-22 15:25:44 +00:00
|
|
|
NSCopyHashTableWithZone (NSHashTable *table, NSZone *zone)
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
1996-02-22 15:25:44 +00:00
|
|
|
objects_allocs_t allocs;
|
1996-02-13 02:31:48 +00:00
|
|
|
NSHashTable *new_table;
|
|
|
|
|
|
|
|
/* Due to the wonders of modern Libfn technology, everything we care
|
|
|
|
* about is automagically transferred. */
|
1996-02-22 15:25:44 +00:00
|
|
|
allocs = objects_allocs_for_zone(zone);
|
|
|
|
new_table = objects_hash_copy_with_allocs(table, allocs);
|
1996-02-13 02:31:48 +00:00
|
|
|
|
|
|
|
return new_table;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Destroying **/
|
|
|
|
|
|
|
|
void
|
1996-02-22 15:25:44 +00:00
|
|
|
NSFreeHashTable (NSHashTable *table)
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
1996-02-22 15:25:44 +00:00
|
|
|
/* Due to the wonders of modern Libobjects structure technology,
|
|
|
|
* everything we care about is automagically and safely destroyed. */
|
|
|
|
objects_hash_dealloc(table);
|
1996-02-13 02:31:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Resetting **/
|
|
|
|
|
|
|
|
void
|
1996-02-22 15:25:44 +00:00
|
|
|
NSResetHashTable (NSHashTable *table)
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
1996-02-22 15:25:44 +00:00
|
|
|
objects_hash_empty(table);
|
1996-02-13 02:31:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Comparing **/
|
|
|
|
|
|
|
|
BOOL
|
1996-02-22 15:25:44 +00:00
|
|
|
NSCompareHashTables (NSHashTable *table1, NSHashTable *table2)
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
1996-02-22 15:25:44 +00:00
|
|
|
return (objects_hash_is_equal_to_hash(table1, table2) ? YES : NO);
|
1996-02-13 02:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Counting **/
|
|
|
|
|
|
|
|
unsigned int
|
1996-02-22 15:25:44 +00:00
|
|
|
NSCountHashTable (NSHashTable *table)
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
1996-02-22 15:25:44 +00:00
|
|
|
return (unsigned int) objects_hash_count(table);
|
1996-02-13 02:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Retrieving **/
|
|
|
|
|
|
|
|
void *
|
1996-02-22 15:25:44 +00:00
|
|
|
NSHashGet (NSHashTable *table, const void *pointer)
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
1996-02-22 15:25:44 +00:00
|
|
|
/* Just make the call. You know the number. */
|
|
|
|
return (void*) objects_hash_element (table, pointer);
|
1996-02-13 02:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NSArray *
|
1996-02-22 15:25:44 +00:00
|
|
|
NSAllHashTableObjects (NSHashTable *table)
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
1996-02-22 15:25:44 +00:00
|
|
|
NSMutableArray *array;
|
|
|
|
NSHashEnumerator enumerator;
|
|
|
|
id element;
|
1996-02-13 02:31:48 +00:00
|
|
|
|
|
|
|
/* FIXME: We should really be locking TABLE somehow, to insure
|
|
|
|
* the thread-safeness of this method. */
|
|
|
|
|
1996-02-22 15:25:44 +00:00
|
|
|
/* Get us a mutable array with plenty of space. */
|
|
|
|
array = [NSMutableArray arrayWithCapacity:NSCountHashTable(table)];
|
1996-02-13 02:31:48 +00:00
|
|
|
|
1996-02-22 15:25:44 +00:00
|
|
|
/* Get an enumerator for TABLE. */
|
|
|
|
enumerator = NSEnumerateHashTable(table);
|
1996-02-13 02:31:48 +00:00
|
|
|
|
1996-02-22 15:25:44 +00:00
|
|
|
while ((element = NSNextHashEnumeratorItem(&enumerator)) != 0)
|
|
|
|
[array addObject:element];
|
1996-02-13 02:31:48 +00:00
|
|
|
|
|
|
|
/* FIXME: Should ARRAY returned be `autorelease'd? */
|
|
|
|
return [array autorelease];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Enumerating **/
|
|
|
|
|
|
|
|
NSHashEnumerator
|
1996-02-22 15:25:44 +00:00
|
|
|
NSEnumerateHashTable (NSHashTable *table)
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
1996-02-22 15:25:44 +00:00
|
|
|
return objects_hash_enumerator (table);
|
1996-02-13 02:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
1996-02-22 15:25:44 +00:00
|
|
|
NSNextHashEnumeratorItem (NSHashEnumerator *enumerator)
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
1996-02-22 15:25:44 +00:00
|
|
|
const void *element;
|
1996-02-13 02:31:48 +00:00
|
|
|
|
|
|
|
/* Grab the next element. */
|
1996-02-22 15:25:44 +00:00
|
|
|
objects_hash_enumerator_next_element (enumerator, &element);
|
1996-02-13 02:31:48 +00:00
|
|
|
|
1996-02-22 15:25:44 +00:00
|
|
|
/* Return ELEMENT. */
|
|
|
|
return (void*) element;
|
1996-02-13 02:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Adding **/
|
|
|
|
|
|
|
|
void
|
1996-02-22 15:25:44 +00:00
|
|
|
NSHashInsert (NSHashTable *table, const void *pointer)
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
1996-02-22 15:25:44 +00:00
|
|
|
/* Place POINTER in TABLE. */
|
|
|
|
objects_hash_add_element (table, pointer);
|
1996-02-13 02:31:48 +00:00
|
|
|
|
1996-02-22 15:25:44 +00:00
|
|
|
/* OpenStep doesn't care for any return value, so... */
|
1996-02-13 02:31:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1996-02-22 15:25:44 +00:00
|
|
|
NSHashInsertKnownAbsent (NSHashTable *table, const void *element)
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
1996-02-22 15:25:44 +00:00
|
|
|
if (objects_hash_contains_element(table, element))
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
|
|
|
/* FIXME: I should make this give the user/programmer more
|
|
|
|
* information. Not difficult to do, just something for a later
|
|
|
|
* date. */
|
|
|
|
[NSException raise:NSInvalidArgumentException
|
|
|
|
format:@"Attempted reinsertion of \"%@\" into a hash table.",
|
1996-02-22 15:25:44 +00:00
|
|
|
NSHT_DESCRIBE(table, element)];
|
1996-02-13 02:31:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1996-02-22 15:25:44 +00:00
|
|
|
objects_hash_add_element_known_absent(table, element);
|
1996-02-13 02:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
1996-02-22 15:25:44 +00:00
|
|
|
NSHashInsertIfAbsent (NSHashTable *table, const void *element)
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
1996-02-22 15:25:44 +00:00
|
|
|
const void *old_element;
|
1996-02-13 02:31:48 +00:00
|
|
|
|
|
|
|
/* Place ELEMENT in TABLE. */
|
1996-02-22 15:25:44 +00:00
|
|
|
old_element = objects_hash_add_element_if_absent(table, element);
|
1996-02-13 02:31:48 +00:00
|
|
|
|
1996-02-22 15:25:44 +00:00
|
|
|
/* Return the version of ELEMENT in TABLE now. */
|
|
|
|
return (void*) old_element;
|
1996-02-13 02:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Removing **/
|
|
|
|
|
|
|
|
void
|
1996-02-22 15:25:44 +00:00
|
|
|
NSHashRemove (NSHashTable *table, const void *element)
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
|
|
|
/* Remove ELEMENT from TABLE. */
|
1996-02-22 15:25:44 +00:00
|
|
|
objects_hash_remove_element(table, element);
|
1996-02-13 02:31:48 +00:00
|
|
|
|
1996-02-22 15:25:44 +00:00
|
|
|
/* OpenStep doesn't care about return values here, so... */
|
1996-02-13 02:31:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Describing **/
|
|
|
|
|
|
|
|
/* FIXME: Make this nicer. I don't know what is desired here, though.
|
|
|
|
* If somebody has a clear idea of what this string should look like,
|
|
|
|
* please tell me, and I'll make it happen. */
|
|
|
|
NSString *
|
1996-02-22 15:25:44 +00:00
|
|
|
NSStringFromHashTable (NSHashTable *table)
|
1996-02-13 02:31:48 +00:00
|
|
|
{
|
1996-02-22 15:25:44 +00:00
|
|
|
NSMutableString *string;
|
1996-02-13 02:31:48 +00:00
|
|
|
NSHashEnumerator enumerator;
|
1996-02-22 15:25:44 +00:00
|
|
|
const void *pointer;
|
1996-02-13 02:31:48 +00:00
|
|
|
|
|
|
|
/* This will be our string. */
|
1996-02-22 15:25:44 +00:00
|
|
|
string = [NSMutableString stringWithCapacity:0];
|
1996-02-13 02:31:48 +00:00
|
|
|
|
|
|
|
/* Get an enumerator for TABLE. */
|
|
|
|
enumerator = NSEnumerateHashTable(table);
|
|
|
|
|
|
|
|
/* Iterate over the elements of TABLE, appending the description of
|
|
|
|
* each to the mutable string STRING. */
|
1996-02-22 15:25:44 +00:00
|
|
|
while ((pointer = NSNextHashEnumeratorItem(&enumerator)) != 0)
|
1996-02-13 02:31:48 +00:00
|
|
|
[string appendFormat:@"%@;", NSHT_DESCRIBE(table, pointer)];
|
|
|
|
|
|
|
|
/* Note that this string'll need to be `retain'ed. */
|
|
|
|
/* FIXME: Should I `autorelease' STRING? I think so. */
|
1996-02-22 15:25:44 +00:00
|
|
|
return (NSString *)[string autorelease];
|
1996-02-13 02:31:48 +00:00
|
|
|
}
|