• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //  HashMap.h
3 //  ANTLR
4 //
5 // Copyright (c) 2010 Alan Condit
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
10 // are met:
11 // 1. Redistributions of source code must retain the above copyright
12 //    notice, this list of conditions and the following disclaimer.
13 // 2. Redistributions in binary form must reproduce the above copyright
14 //    notice, this list of conditions and the following disclaimer in the
15 //    documentation and/or other materials provided with the distribution.
16 // 3. The name of the author may not be used to endorse or promote products
17 //    derived from this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 #import <Foundation/Foundation.h>
31 #import "AMutableArray.h"
32 #import "AMutableDictionary.h"
33 #import "ArrayIterator.h"
34 #import "LinkBase.h"
35 #import "MapElement.h"
36 #import "PtrBuffer.h"
37 
38 #define GLOBAL_SCOPE       0
39 #define LOCAL_SCOPE        1
40 #define HASHSIZE         101
41 #define HBUFSIZE      0x2000
42 
43 @class HashMap;
44 
45 /**
46  * HashMap entry.
47  */
48 
49 @interface HMEntry : NSObject {
50     HMEntry  *next;
51     NSInteger hash;
52     NSString *key;
53     id value;
54 }
55 
56 @property(nonatomic, retain) HMEntry  *next;
57 @property(assign)            NSInteger  hash;
58 @property(nonatomic, retain) NSString *key;
59 @property(nonatomic, retain) id value;
60 
61 + (HMEntry *)newEntry:(NSInteger)h key:(NSString *)k value:(id)v next:(HMEntry *) n;
62 - (id) init:(NSInteger)h key:(NSString *)k value:(id)v next:(HMEntry *)n;
63 - (void) setValue:(id)newValue;
64 - (BOOL) isEqualTo:(id)o;
65 - (NSInteger) hashCode;
66 - (NSString *) description;
67 - (void) recordAccess:(HashMap *)m;
68 - (void) recordRemoval:(HashMap *)m;
69 @end
70 
71 @interface HashIterator : ArrayIterator {
72     HMEntry  *next;
73     NSInteger expectedModCount;
74     NSInteger idx;
75     HMEntry  *current;
76     HashMap  *hm;
77 }
78 
79 + (HashIterator *) newIterator:(HashMap *)aHM;
80 
81 - (id) init:(HashMap *)aHM;
82 - (BOOL) hasNext;
83 - (HMEntry *) next;
84 - (void) remove;
85 @end
86 
87 @interface HMEntryIterator : HashIterator
88 {
89 }
90 
91 + (HMEntryIterator *)newIterator:(HashMap *)aHM;
92 
93 - (id) init:(HashMap *)aHM;
94 - (HMEntry *) next;
95 @end
96 
97 @interface HMValueIterator : HashIterator
98 {
99 }
100 
101 + (HMValueIterator *)newIterator:(HashMap *)aHM;
102 
103 - (id) init:(HashMap *)aHM;
104 - (id) next;
105 @end
106 
107 @interface HMKeyIterator : HashIterator
108 {
109 }
110 
111 + (HMKeyIterator *)newIterator:(HashMap *)aHM;
112 
113 - (id) init:(HashMap *)aHM;
114 - (NSString *) next;
115 @end
116 
117 @interface HMKeySet : NSSet
118 {
119     HashMap *hm;
120     AMutableArray *anArray;
121 }
122 
123 @property (retain) HashMap *hm;
124 @property (retain) AMutableArray *anArray;
125 
126 + (HMKeySet *)newKeySet:(HashMap *)aHM;
127 
128 - (id) init:(HashMap *)aHM;
129 - (HashIterator *) iterator;
130 - (NSUInteger) count;
131 - (BOOL) contains:(id)o;
132 - (BOOL) remove:(id)o;
133 - (void) clear;
134 - (AMutableArray *)toArray;
135 @end
136 
137 @interface Values : PtrBuffer
138 {
139     HashMap *hm;
140     AMutableArray *anArray;
141 }
142 
143 @property (retain) HashMap *hm;
144 @property (retain) AMutableArray *anArray;
145 
146 + (Values *)newValueSet:(HashMap *)aHM;
147 
148 - (id) init:(HashMap *)aHM;
149 - (HashIterator *) iterator;
150 - (NSUInteger) count;
151 - (BOOL) contains:(id)o;
152 - (void) clear;
153 - (AMutableArray *)toArray;
154 @end
155 
156 @interface HMEntrySet : NSSet
157 {
158     HashMap *hm;
159     AMutableArray *anArray;
160 }
161 
162 @property (retain) HashMap *hm;
163 @property (retain) AMutableArray *anArray;
164 
165 + (HMEntrySet *)newEntrySet:(HashMap *)aHM;
166 
167 - (id) init:(HashMap *)aHM;
168 - (HashIterator *) iterator;
169 - (BOOL) contains:(id)o;
170 - (BOOL) remove:(id)o;
171 - (NSUInteger) count;
172 - (void) clear;
173 - (NSArray *)toArray;
174 @end
175 
176 @interface HashMap : LinkBase {
177     //    TStringPool *fPool;
178     NSInteger Scope;
179     NSInteger LastHash;
180     NSInteger BuffSize;
181     NSInteger Capacity;
182     /**
183      * The number of key-value mappings contained in this map.
184      */
185     NSUInteger count;
186     NSUInteger ptr;
187     __strong NSMutableData *buffer;
188     __strong MapElement **ptrBuffer;
189     NSInteger mode;
190     /**
191      * The table, resized as necessary. Length MUST Always be a power of two.
192      */
193 //    AMutableArray *table;
194 
195     /**
196      * The next size value at which to resize (capacity * load factor).
197      * @serial
198      */
199     NSInteger threshold;
200 
201     /**
202      * The load factor for the hash table.
203      *
204      * @serial
205      */
206     float loadFactor;
207     /**
208      * The number of times this HashMap has been structurally modified
209      * Structural modifications are those that change the number of mappings in
210      * the HashMap or otherwise modify its internal structure (e.g.,
211      * rehash).  This field is used to make iterators on Collection-views of
212      * the HashMap fail-fast.  (See ConcurrentModificationException).
213      */
214     NSInteger modCount;
215     HMEntrySet *entrySet;
216     BOOL empty;
217     HMKeySet *keySet;
218     Values *values;
219 }
220 
221 //@property (copy) TStringPool *fPool;
222 @property (getter=getScope, setter=setScope:) NSInteger Scope;
223 @property (getter=getLastHash, setter=setLastHash:) NSInteger LastHash;
224 
225 @property (getter=getMode,setter=setMode:) NSInteger mode;
226 @property (assign) NSInteger BuffSize;
227 @property (assign) NSInteger Capacity;
228 @property (getter=getCount, setter=setCount:) NSUInteger count;
229 @property (assign) NSUInteger ptr;
230 @property (retain, getter=getBuffer, setter=setBuffer:) NSMutableData *buffer;
231 @property (assign, getter=getPtrBuffer, setter=setPtrBuffer:) MapElement **ptrBuffer;
232 @property (assign) NSInteger threshold;
233 @property (assign) float loadFactor;
234 @property (assign) NSInteger modCount;
235 @property (retain) HMEntrySet *entrySet;
236 @property (nonatomic, readonly) BOOL empty;
237 @property (retain) HMKeySet *keySet;
238 @property (retain) Values *values;
239 
240 // Contruction/Destruction
241 + (id) newHashMap;
242 + (id) newHashMap:(NSInteger)anInitialCapacity loadFactor:(float)loadFactor;
243 + (id) newHashMap:(NSInteger)anInitialCapacity;
244 + (id) newHashMapWithLen:(NSInteger)aBuffSize;
245 - (id) init;
246 - (id) initWithLen:(NSInteger)aBuffSize;
247 - (id) init:(NSInteger)anInitialCapacity;
248 - (id) init:(NSInteger)anInitialCapacity loadFactor:(float)loadFactor;
249 - (id) initWithM:(HashMap *)m;
250 - (void)dealloc;
251 - (HashMap *)PushScope:( HashMap **)map;
252 - (HashMap *)PopScope:( HashMap **)map;
253 
254 - (NSUInteger)count;
255 - (NSInteger)size;
256 
257 // Instance Methods
258 /*    form hash value for string s */
259 - (NSInteger)hash:(NSString *)s;
260 - (NSInteger)hashInt:(NSInteger)anInt;
261 - (NSInteger) indexFor:(NSInteger)h length:(NSInteger)length;
262 /*   look for s in ptrBuffer  */
263 - (HashMap *)findscope:(NSInteger)level;
264 /*   look for s in ptrBuffer  */
265 - (id)lookup:(NSString *)s Scope:(NSInteger)scope;
266 /*   look for s in ptrBuffer  */
267 - (id)install:(MapElement *)sym Scope:(NSInteger)scope;
268 /*   look for s in ptrBuffer  */
269 - (void)deleteHashMap:(MapElement *)np;
270 - (NSInteger)RemoveSym:(NSString *)s;
271 - (void)delete_chain:(MapElement *)np;
272 #ifdef DONTUSEYET
273 - (int)bld_symtab:(KW_TABLE *)toknams;
274 #endif
275 - (MapElement **)getptrBuffer;
276 - (MapElement *)getptrBufferEntry:(NSInteger)idx;
277 - (void)setptrBuffer:(MapElement *)np Index:(NSInteger)idx;
278 - (NSInteger)getScope;
279 - (void)setScope:(NSInteger)i;
280 - (MapElement *)getTType:(NSString *)name;
281 - (MapElement *)getNameInList:(NSInteger)ttype;
282 - (void)putNode:(NSString *)name TokenType:(NSInteger)ttype;
283 - (NSInteger)getMode;
284 - (void)setMode:(NSInteger)aMode;
285 - (void) insertObject:(id)aRule atIndex:(NSInteger)idx;
286 - (id) objectAtIndex:(NSInteger)idx;
287 - (void) setObject:(id)aRule atIndex:(NSInteger)idx;
288 - (void)addObject:(id)anObject;
289 - (MapElement *) getName:(NSString *)aName;
290 - (void) putName:(NSString *)name Node:(id)aNode;
291 
292 - (NSEnumerator *)objectEnumerator;
293 - (BOOL) hasNext;
294 - (MapElement *)nextObject;
295 
296 - (NSUInteger) count;
297 - (id) get:(NSString *)key;
298 - (id) getForNullKey;
299 - (BOOL) containsKey:(NSString *)key;
300 - (HMEntry *) getEntry:(NSString *)key;
301 - (id) put:(NSString *)key value:(id)value;
302 - (id) putForNullKey:(id)value;
303 - (void) putForCreate:(NSString *)key value:(id)value;
304 - (void) putAllForCreate:(HashMap *)m;
305 - (void) resize:(NSInteger)newCapacity;
306 - (void) transfer:(NSArray *)newTable;
307 - (void) putAll:(HashMap *)m;
308 - (id) remove:(NSString *)key;
309 - (HMEntry *) removeEntryForKey:(NSString *)key;
310 - (HMEntry *) removeMapping:(id)o;
311 - (void) clear;
312 - (BOOL) containsValue:(id)value;
313 - (id) copyWithZone:(NSZone *)zone;
314 - (NSString *) description;
315 - (void) addEntry:(NSInteger)hash key:(NSString *)key value:(id)value bucketIndex:(NSInteger)bucketIndex;
316 - (void) createEntry:(NSInteger)hash key:(NSString *)key value:(id)value bucketIndex:(NSInteger)bucketIndex;
317 - (HMKeyIterator *) newKeyIterator;
318 - (HMValueIterator *) newValueIterator;
319 - (HMEntryIterator *) newEntryIterator;
320 - (HMKeySet *) keySet;
321 - (Values *) values;
322 - (HMEntrySet *) entrySet;
323 - (NSInteger) capacity;
324 - (float) loadFactor;
325 
326 @end
327