summaryrefslogtreecommitdiff
path: root/portable/src/phashtable.c
blob: 4940c609930c1cc1710e75daacf61701747f92f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/*---------------------------------------------------------------------------*
 *  phashtable.c  *
 *                                                                           *
 *  Copyright 2007, 2008 Nuance Communciations, Inc.                               *
 *                                                                           *
 *  Licensed under the Apache License, Version 2.0 (the 'License');          *
 *  you may not use this file except in compliance with the License.         *
 *                                                                           *
 *  You may obtain a copy of the License at                                  *
 *      http://www.apache.org/licenses/LICENSE-2.0                           *
 *                                                                           *
 *  Unless required by applicable law or agreed to in writing, software      *
 *  distributed under the License is distributed on an 'AS IS' BASIS,        *
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 
 *  See the License for the specific language governing permissions and      *
 *  limitations under the License.                                           *
 *                                                                           *
 *---------------------------------------------------------------------------*/






#include <string.h>

#include "phashtable.h"
#include "plog.h"
#include "pmemory.h"
#include "pstdio.h"

//extern int strcmp(const char * s1,  const char * s2);

#define ALLOC_SIZE 16

struct PHashTableEntry_t
{
  const void *key;
  const void *value;
  PHashTable *table;
  unsigned int idx;
  PHashTableEntry *next;
  PHashTableEntry *prev;
  unsigned int hashCode;
};

typedef struct PHashTableEntryBlock_t
{
  PHashTableEntry entries[ALLOC_SIZE];
  struct PHashTableEntryBlock_t *next;
}
PHashTableEntryBlock;


struct PHashTable_t
{
  PHashTableArgs args;
  const LCHAR *memoryTag;
  unsigned int size;
  float maxLoadFactor;
  PHashTableEntry **entries;
  unsigned int threshold;
  PHashTableEntry *freeList;
  PHashTableEntryBlock *entryBlock;
};

#include "pcrc.h"

static unsigned int hashString(const void *key)
{
  return ~pcrcComputeString(key);
}

ESR_ReturnCode PHashTableCreate(PHashTableArgs *args,
                                const LCHAR *memTag,
                                PHashTable **table)
{
  PHashTable *tmp;
  unsigned int i;
  
  if (table == NULL ||
      (args != NULL && args->maxLoadFactor <= 0.0))
    return ESR_INVALID_ARGUMENT;
    
    
  if ((tmp = NEW(PHashTable, memTag)) == NULL)
    return ESR_OUT_OF_MEMORY;
    
  if (args == NULL)
  {
    tmp->args.capacity = PHASH_TABLE_DEFAULT_CAPACITY;
    tmp->args.maxLoadFactor = PHASH_TABLE_DEFAULT_MAX_LOAD_FACTOR;
    tmp->args.hashFunction = PHASH_TABLE_DEFAULT_HASH_FUNCTION;
    tmp->args.compFunction = PHASH_TABLE_DEFAULT_COMP_FUNCTION;
  }
  else
  {
    memcpy(&tmp->args, args, sizeof(PHashTableArgs));
  }
  if (tmp->args.hashFunction == PHASH_TABLE_DEFAULT_HASH_FUNCTION)
    tmp->args.hashFunction = hashString;
    
  if (tmp->args.compFunction == PHASH_TABLE_DEFAULT_COMP_FUNCTION)
    tmp->args.compFunction = LSTRCMP;
    
  tmp->entries = NEW_ARRAY(PHashTableEntry *, tmp->args.capacity, memTag);
  
  if (tmp->entries == NULL)
  {
    FREE(tmp);
    return ESR_OUT_OF_MEMORY;
  }
  
  for (i = tmp->args.capacity; i > 0;)
  {
    tmp->entries[--i] = NULL;
  }
  
  tmp->memoryTag = memTag;
  tmp->size = 0;
  tmp->threshold = (unsigned int)(tmp->args.capacity * tmp->args.maxLoadFactor);
  tmp->freeList = NULL;
  tmp->entryBlock = NULL;
  
  *table = tmp;
  return ESR_SUCCESS;
}

ESR_ReturnCode PHashTableDestroy(PHashTable *table)
{
  PHashTableEntryBlock *tmp, *block;
  
  if (table == NULL)
    return ESR_INVALID_ARGUMENT;
    
  block = table->entryBlock;
  while (block != NULL)
  {
    tmp = block->next;
    FREE(block);
    block = tmp;
  }
  
  FREE(table->entries);
  FREE(table);
  return ESR_SUCCESS;
}

ESR_ReturnCode PHashTableGetSize(PHashTable *table,
                                 size_t *size)
{
  if (table == NULL || size == NULL)
    return ESR_INVALID_ARGUMENT;
    
  *size = table->size;
  return ESR_SUCCESS;
}

static PHashTableEntry *getEntry(PHashTable *table,
                                 const void *key,
                                 unsigned int hashCode,
                                 unsigned int idx)
{
  PHashTableEntry *entry = table->entries[idx];
  
  if (key == NULL)
  {
    while (entry != NULL)
    {
      if (entry->key == NULL)
        return entry;
        
      entry = entry->next;
    }
  }
  else
  {
    while (entry != NULL)
    {
      if (entry->hashCode == hashCode && table->args.compFunction(key, entry->key) == 0)
        return entry;
        
      entry = entry->next;
    }
  }
  
  return NULL;
}

static void removeEntry(PHashTableEntry *entry)
{
  if (entry->prev == NULL)
    entry->table->entries[entry->idx] = entry->next;
  else
    entry->prev->next = entry->next;
    
  if (entry->next != NULL)
    entry->next->prev = entry->prev;
    
  entry->table->size--;
  
  entry->next = entry->table->freeList;
  entry->table->freeList = entry;
  /* clean up entry for re-use. */
  entry->key = entry->value = NULL;
}

ESR_ReturnCode PHashTableGetValue(PHashTable *table, const void *key, void **value)
{
  PHashTableEntry *entry;
  unsigned int hashCode;
  unsigned int idx;
  
  if (table == NULL || value == NULL)
    return ESR_INVALID_ARGUMENT;
    
  hashCode = table->args.hashFunction(key);
  idx = hashCode % table->args.capacity;
  if ((entry = getEntry(table, key, hashCode, idx)) != NULL)
  {
    *value = (void *) entry->value;
    return ESR_SUCCESS;
  }
  else
  {
    *value = NULL;
    return ESR_NO_MATCH_ERROR;
  }
}

ESR_ReturnCode PHashTableContainsKey(PHashTable *table, const void *key, ESR_BOOL* exists)
{
  ESR_ReturnCode rc;
  PHashTableEntry* entry;
  
  if (table == NULL || exists == NULL)
  {
    rc = ESR_INVALID_ARGUMENT;
    PLogError(ESR_rc2str(rc));
    goto CLEANUP;
  }
  rc = PHashTableGetEntry(table, key, &entry);
  if (rc == ESR_SUCCESS)
    *exists = ESR_TRUE;
  else if (rc == ESR_NO_MATCH_ERROR)
    *exists = ESR_FALSE;
  else
    goto CLEANUP;
    
  return ESR_SUCCESS;
CLEANUP:
  return rc;
}

ESR_ReturnCode PHashTableGetEntry(PHashTable *table, const void *key, PHashTableEntry **entry)
{
  unsigned int hashCode;
  unsigned int idx;
  PHashTableEntry* result;
  
  if (table == NULL || entry == NULL)
    return ESR_INVALID_ARGUMENT;
    
  hashCode = table->args.hashFunction(key);
  idx = hashCode % table->args.capacity;
  
  result = getEntry(table, key, hashCode, idx);
  if (result == NULL)
    return ESR_NO_MATCH_ERROR;
  *entry = result;
  return ESR_SUCCESS;
}

static ESR_ReturnCode PHashTableRehash(PHashTable *table)
{
  unsigned int i, idx;
  unsigned int oldCapacity = table->args.capacity;
  unsigned int newCapacity = ((oldCapacity << 1) | 0x01);
  PHashTableEntry *entry, *tmp, *next;
  
  PHashTableEntry **newEntries =
    (PHashTableEntry **)
    REALLOC(table->entries,
            sizeof(PHashTableEntry *) * newCapacity);
            
  if (newEntries == NULL)
    return ESR_OUT_OF_MEMORY;
    
  table->entries = newEntries;
  table->args.capacity = newCapacity;
  table->threshold = (unsigned int)(newCapacity * table->args.maxLoadFactor);
  
  for (i = oldCapacity; i < newCapacity; ++i)
  {
    table->entries[i] = NULL;
  }
  
  for (i = 0; i < oldCapacity; i++)
  {
    for (entry = table->entries[i]; entry != NULL;)
    {
      idx = entry->hashCode % newCapacity;
      if (idx != i)
      {
        /* Need to change location. */
        entry->idx = idx;
        
        next = entry->next;
        
        if (entry->prev != NULL)
          entry->prev->next = next;
        else
          table->entries[i] = next;
          
        if (next != NULL)
          next->prev = entry->prev;
          
        tmp = table->entries[idx];
        entry->next = tmp;
        entry->prev = NULL;
        if (tmp != NULL)
          tmp->prev = entry;
        table->entries[idx] = entry;
        
        entry = next;
      }
      else
      {
        /* Already in the right slot. */
        entry = entry->next;
      }
    }
  }
  return ESR_SUCCESS;
}


ESR_ReturnCode PHashTablePutValue(PHashTable *table,
                                  const void *key,
                                  const void *value,
                                  void **oldValue)
{
  ESR_ReturnCode rc = ESR_SUCCESS;
  unsigned int hashCode, idx;
  PHashTableEntry *entry;
  
  if (table == NULL) return ESR_INVALID_ARGUMENT;
  hashCode = table->args.hashFunction(key);
  idx = hashCode % table->args.capacity;
  
  entry = getEntry(table, key, hashCode, idx);
  if (entry != NULL)
  {
    if (oldValue != NULL) *oldValue = (void *) entry->value;
    entry->value = value;
    return ESR_SUCCESS;
  }
  
  /* If we get here, we need to add a new entry.  But first, verify if we need
     to rehash. */
  if (table->size >= table->threshold)
  {
    if ((rc = PHashTableRehash(table)) != ESR_SUCCESS)
      return rc;
    idx = hashCode % table->args.capacity;
  }
  
  if (table->freeList == NULL)
  {
    /* Allocate a new block and put all entries on the free list. */
    PHashTableEntryBlock *block;
    int i;
    
    block = NEW(PHashTableEntryBlock, table->memoryTag);
    if (block == NULL)
      return ESR_OUT_OF_MEMORY;
      
    block->next = table->entryBlock;
    table->entryBlock = block;
    
    for (i = 0; i < ALLOC_SIZE - 1; ++i)
    {
      block->entries[i].next = &block->entries[i+1];
    }
    block->entries[ALLOC_SIZE-1].next = NULL;
    
    /* do not see any bug in following code. But on the VxWorks with optimization option -O3
      it produces wrong result: block->entries[0].next is correct but block->entries[1].next = NULL
      it causes lot of memory wastes.
    for (i = 0, entry = block->entries; i < ALLOC_SIZE - 1; ++i, ++entry)
    {
      entry->next = entry+1;
    }
    entry->next = table->freeList;
    */
    
    table->freeList = block->entries;
  }
  
  /* Get an entry from the freeList. */
  entry = table->freeList;
  table->freeList = entry->next;
  
  /* Initialize entry data structure. */
  entry->table = table;
  entry->idx = idx;
  entry->key = key;
  entry->value = value;
  entry->hashCode = hashCode;
  entry->next = table->entries[idx];
  entry->prev = NULL;
  if (entry->next != NULL)
    entry->next->prev = entry;
  table->entries[idx] = entry;
  table->size++;
  
  if (oldValue != NULL) *oldValue = NULL;
  return ESR_SUCCESS;
}


ESR_ReturnCode PHashTableRemoveValue(PHashTable *table,
                                     const void *key,
                                     void **oldValue)
{
  unsigned int hashCode, idx;
  PHashTableEntry *entry;
  ESR_ReturnCode rc;
  
  if (table == NULL)
  {
    rc = ESR_INVALID_ARGUMENT;
    PLogError(ESR_rc2str(rc));
    goto CLEANUP;
  }
  
  hashCode = table->args.hashFunction(key);
  idx = hashCode % table->args.capacity;
  
  entry = getEntry(table, key, hashCode, idx);
  if (entry != NULL)
  {
    if (oldValue != NULL)
      *oldValue = (void*) entry->value;
    removeEntry(entry);
  }
  else
  {
    if (oldValue != NULL)
      *oldValue = NULL;
  }
  return ESR_SUCCESS;
CLEANUP:
  return rc;
}

ESR_ReturnCode PHashTableEntryGetKeyValue(PHashTableEntry *entry,
    void **key,
    void **value)
{
  if (entry == NULL) return ESR_INVALID_ARGUMENT;
  
  if (key != NULL) *key = (void *) entry->key;
  if (value != NULL) *value = (void *) entry->value;
  return ESR_SUCCESS;
}


/**
 * Sets the value associated with this entry.
 * @param entry The hashtable entry.
 * @param value The value to associate with the entry.
 * @param oldvalue If this pointer is non-NULL, it will be set to the previous value associated with this entry.
 **/
ESR_ReturnCode PHashTableEntrySetValue(PHashTableEntry *entry,
                                       const void *value,
                                       void **oldValue)
{
  if (entry == NULL) return ESR_INVALID_ARGUMENT;
  
  if (oldValue != NULL) *oldValue = (void *) entry->value;
  entry->value = value;
  return ESR_SUCCESS;
}


/**
 * Removes the entry from its hash table.
 *
 * @param entry The hashtable entry.
 **/
ESR_ReturnCode PHashTableEntryRemove(PHashTableEntry *entry)
{
  if (entry == NULL)
    return ESR_INVALID_ARGUMENT;
    
  removeEntry(entry);
  
  return ESR_SUCCESS;
}

static PHashTableEntry* iteratorAdvance(PHashTable *table, PHashTableEntry *entry)
{
  unsigned int idx;
  
  if (entry != NULL)
  {
    idx = entry->idx;
    entry = entry->next;
    if (entry == NULL)
    {
      while (++idx < table->args.capacity)
      {
        if (table->entries[idx] != NULL)
        {
          entry = table->entries[idx];
          break;
        }
      }
    }
  }
  else
  {
    for (idx = 0; idx < table->args.capacity; ++idx)
    {
      if (table->entries[idx] != NULL)
      {
        entry = table->entries[idx];
        break;
      }
    }
  }
  return entry;
}


ESR_ReturnCode PHashTableEntryGetFirst(PHashTable *table, PHashTableEntry **entry)
{
  if (table == NULL || entry == NULL)
    return ESR_INVALID_ARGUMENT;
    
  *entry = iteratorAdvance(table, NULL);
  return ESR_SUCCESS;
}

/**
 * Iterates on the next key and value.  Returns a NULL key when at the end of the hash table.
 *
 * @param iter The iterator on which the iteration is performed.
 * @param key Returns the key associated with the entry, cannot be NULL.
 * @param value If non-NULL, returns the value associated with the entry.
 **/
ESR_ReturnCode PHashTableEntryAdvance(PHashTableEntry **entry)
{
  if (entry == NULL || *entry == NULL)
    return ESR_INVALID_ARGUMENT;
    
  *entry = iteratorAdvance((*entry)->table, *entry);
  return ESR_SUCCESS;
}