]>
Commit | Line | Data |
---|---|---|
13d88034 A |
1 | /* |
2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights | |
7 | * Reserved. This file contains Original Code and/or Modifications of | |
8 | * Original Code as defined in and that are subject to the Apple Public | |
9 | * Source License Version 1.1 (the "License"). You may not use this file | |
10 | * except in compliance with the License. Please obtain a copy of the | |
11 | * License at http://www.apple.com/publicsource and read it before using | |
12 | * this file. | |
13 | * | |
14 | * The Original Code and all software distributed under the License are | |
15 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the | |
19 | * License for the specific language governing rights and limitations | |
20 | * under the License. | |
21 | * | |
22 | * @APPLE_LICENSE_HEADER_END@ | |
23 | */ | |
24 | /* maptable.h | |
25 | Scalable hash table of mappings. | |
26 | Bertrand, August 1990 | |
27 | Copyright 1990-1996 NeXT Software, Inc. | |
28 | */ | |
29 | ||
30 | #warning the API in this header is obsolete | |
31 | ||
32 | #ifndef _OBJC_MAPTABLE_H_ | |
33 | #define _OBJC_MAPTABLE_H_ | |
34 | ||
35 | #import <objc/objc.h> | |
36 | ||
37 | /*************** Definitions ***************/ | |
38 | ||
39 | /* This module allows hashing of arbitrary associations [key -> value]. Keys and values must be pointers or integers, and client is responsible for allocating/deallocating this data. A deallocation call-back is provided. | |
40 | NX_MAPNOTAKEY (-1) is used internally as a marker, and therefore keys must always be different from -1. | |
41 | As well-behaved scalable data structures, hash tables double in size when they start becoming full, thus guaranteeing both average constant time access and linear size. */ | |
42 | ||
43 | typedef struct _NXMapTable { | |
44 | /* private data structure; may change */ | |
45 | const struct _NXMapTablePrototype *prototype; | |
46 | unsigned count; | |
47 | unsigned nbBuckets; | |
48 | void *buckets; | |
49 | } NXMapTable; | |
50 | ||
51 | typedef struct _NXMapTablePrototype { | |
52 | unsigned (*hash)(NXMapTable *, const void *key); | |
53 | int (*isEqual)(NXMapTable *, const void *key1, const void *key2); | |
54 | void (*free)(NXMapTable *, void *key, void *value); | |
55 | int style; /* reserved for future expansion; currently 0 */ | |
56 | } NXMapTablePrototype; | |
57 | ||
58 | /* invariants assumed by the implementation: | |
59 | A - key != -1 | |
60 | B - key1 == key2 => hash(key1) == hash(key2) | |
61 | when key varies over time, hash(key) must remain invariant | |
62 | e.g. if string key, the string must not be changed | |
63 | C - isEqual(key1, key2) => key1 == key2 | |
64 | */ | |
65 | ||
66 | #define NX_MAPNOTAKEY ((void *)(-1)) | |
67 | ||
68 | /*************** Functions ***************/ | |
69 | ||
70 | OBJC_EXPORT NXMapTable *NXCreateMapTableFromZone(NXMapTablePrototype prototype, unsigned capacity, void *z); | |
71 | OBJC_EXPORT NXMapTable *NXCreateMapTable(NXMapTablePrototype prototype, unsigned capacity); | |
72 | /* capacity is only a hint; 0 creates a small table */ | |
73 | ||
74 | OBJC_EXPORT void NXFreeMapTable(NXMapTable *table); | |
75 | /* call free for each pair, and recovers table */ | |
76 | ||
77 | OBJC_EXPORT void NXResetMapTable(NXMapTable *table); | |
78 | /* free each pair; keep current capacity */ | |
79 | ||
80 | OBJC_EXPORT BOOL NXCompareMapTables(NXMapTable *table1, NXMapTable *table2); | |
81 | /* Returns YES if the two sets are equal (each member of table1 in table2, and table have same size) */ | |
82 | ||
83 | OBJC_EXPORT unsigned NXCountMapTable(NXMapTable *table); | |
84 | /* current number of data in table */ | |
85 | ||
86 | OBJC_EXPORT void *NXMapMember(NXMapTable *table, const void *key, void **value); | |
87 | /* return original table key or NX_MAPNOTAKEY. If key is found, value is set */ | |
88 | ||
89 | OBJC_EXPORT void *NXMapGet(NXMapTable *table, const void *key); | |
90 | /* return original corresponding value or NULL. When NULL need be stored as value, NXMapMember can be used to test for presence */ | |
91 | ||
92 | OBJC_EXPORT void *NXMapInsert(NXMapTable *table, const void *key, const void *value); | |
93 | /* override preexisting pair; Return previous value or NULL. */ | |
94 | ||
95 | OBJC_EXPORT void *NXMapRemove(NXMapTable *table, const void *key); | |
96 | /* previous value or NULL is returned */ | |
97 | ||
98 | /* Iteration over all elements of a table consists in setting up an iteration state and then to progress until all entries have been visited. An example of use for counting elements in a table is: | |
99 | unsigned count = 0; | |
100 | const MyKey *key; | |
101 | const MyValue *value; | |
102 | NXMapState state = NXInitMapState(table); | |
103 | while(NXNextMapState(table, &state, &key, &value)) { | |
104 | count++; | |
105 | } | |
106 | */ | |
107 | ||
108 | typedef struct {int index;} NXMapState; | |
109 | /* callers should not rely on actual contents of the struct */ | |
110 | ||
111 | OBJC_EXPORT NXMapState NXInitMapState(NXMapTable *table); | |
112 | ||
113 | OBJC_EXPORT int NXNextMapState(NXMapTable *table, NXMapState *state, const void **key, const void **value); | |
114 | /* returns 0 when all elements have been visited */ | |
115 | ||
116 | /*************** Conveniences ***************/ | |
117 | ||
118 | OBJC_EXPORT const NXMapTablePrototype NXPtrValueMapPrototype; | |
119 | /* hashing is pointer/integer hashing; | |
120 | isEqual is identity; | |
121 | free is no-op. */ | |
122 | OBJC_EXPORT const NXMapTablePrototype NXStrValueMapPrototype; | |
123 | /* hashing is string hashing; | |
124 | isEqual is strcmp; | |
125 | free is no-op. */ | |
126 | OBJC_EXPORT const NXMapTablePrototype NXObjectMapPrototype; | |
127 | /* for objects; uses methods: hash, isEqual:, free, all for key. */ | |
128 | ||
129 | #endif /* _OBJC_MAPTABLE_H_ */ |