Merge pull request #10 from gunyarakun/fix-invalid-return
[cocotron.git] / objc / ObjCHashTable.c
blob3585ebaef8b261c73e436d3df7ca7b8430883ddc
1 /* Copyright (c) 2006-2007 Christopher J. W. Lloyd
3 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
8 #import "ObjCHashTable.h"
9 #import "objc_malloc.h"
11 OBJCHashTable *OBJCCreateHashTable(unsigned capacity) {
12 OBJCHashTable *result = objc_calloc(1, sizeof(OBJCHashTable));
14 result->count = 0;
15 result->nBuckets = 10;
16 result->buckets = objc_calloc(result->nBuckets, sizeof(OBJCHashBucket *));
18 return result;
21 const void *OBJCHashInsertValueForKey(OBJCHashTable *table, const char *key, const void *value) {
22 unsigned hash = OBJCHashString(key);
23 int i = hash % table->nBuckets;
24 OBJCHashBucket *j;
26 for(j = table->buckets[i]; j != NULL; j = j->next)
27 if(OBJCIsStringEqual(j->key, key)) {
28 j->key = key;
29 j->value = value;
30 return value;
33 if(table->count >= table->nBuckets) {
34 int nBuckets = table->nBuckets;
35 OBJCHashBucket **buckets = table->buckets, *next;
37 table->nBuckets = nBuckets * 2;
38 table->buckets = objc_calloc(table->nBuckets, sizeof(OBJCHashBucket *));
40 for(i = 0; i < nBuckets; i++)
41 for(j = buckets[i]; j != NULL; j = next) {
42 int newi = OBJCHashString(j->key) % table->nBuckets;
44 next = j->next;
45 j->next = table->buckets[newi];
46 table->buckets[newi] = j;
48 objc_free(buckets);
49 i = hash % table->nBuckets;
52 j = objc_malloc(sizeof(OBJCHashBucket));
53 j->key = key;
54 j->value = value;
55 j->next = table->buckets[i];
56 table->buckets[i] = j;
57 table->count++;
59 return value;
62 OBJCHashEnumerator OBJCEnumerateHashTable(OBJCHashTable *table) {
63 OBJCHashEnumerator state;
65 state.table = table;
66 for(state.i = 0; state.i < table->nBuckets; state.i++)
67 if(table->buckets[state.i] != NULL)
68 break;
69 state.j = (state.i < table->nBuckets) ? table->buckets[state.i] : NULL;
71 return state;
74 const char *OBJCNextHashEnumeratorKey(OBJCHashEnumerator *state) {
75 const char *key;
77 if(state->j == NULL)
78 return NULL;
80 key = state->j->key;
82 if((state->j = state->j->next) != NULL)
83 return key;
85 for(state->i++; state->i < state->table->nBuckets; state->i++)
86 if((state->j = state->table->buckets[state->i]) != NULL)
87 return key;
89 state->j = NULL;
91 return key;
94 const void *OBJCNextHashEnumeratorValue(OBJCHashEnumerator *state) {
95 const void *value;
97 if(state->j == NULL)
98 return NULL;
100 value = state->j->value;
102 if((state->j = state->j->next) != NULL)
103 return value;
105 for(state->i++; state->i < state->table->nBuckets; state->i++)
106 if((state->j = state->table->buckets[state->i]) != NULL)
107 return value;
109 state->j = NULL;
111 return value;