Permalink
Browse files

Add missing files.

  • Loading branch information...
1 parent 24526b9 commit fe03c0d4a638aad463fd0594cbb7fad9579d048e @root670 committed Mar 22, 2016
Showing with 95 additions and 0 deletions.
  1. +73 −0 hash.c
  2. +22 −0 hash.h
View
@@ -0,0 +1,73 @@
+#include <stdio.h>
+#include <tamtypes.h>
+#include "hash.h"
+
+hashTable_t *hashNewTable(int numEntries)
+{
+ hashTable_t *table;
+
+ table = calloc(1, sizeof(hashTable_t));
+ table->keyHashes = calloc(numEntries, sizeof(unsigned int));
+ table->values = calloc(numEntries, sizeof(void*));
+ table->numEntries = numEntries;
+
+ return table;
+}
+
+// http://www.eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
+unsigned int hashFunction(void *key, int len)
+{
+ unsigned char *p = key;
+ unsigned int h = 0;
+ int i;
+
+ for (i = 0; i < len; i++)
+ {
+ h += p[i];
+ h += (h << 10);
+ h ^= (h >> 6);
+ }
+
+ h += (h << 3);
+ h ^= (h >> 11);
+ h += (h << 15);
+
+ return h;
+}
+
+void *hashFind(hashTable_t *table, unsigned int keyHash)
+{
+ unsigned int i;
+
+ i = keyHash % table->numEntries;
+
+ while(table->keyHashes[i] != keyHash)
+ {
+ if(table->keyHashes[i] == 0) // not in table
+ return NULL;
+ else if(i == table->numEntries - 1) // last entry in table
+ i = 0;
+ else // try next entry
+ i++;
+ }
+
+ return table->values[i];
+}
+
+void hashAdd(hashTable_t *table, void *ptr, unsigned int keyHash)
+{
+ unsigned int i;
+
+ i = keyHash % table->numEntries;
+
+ while(table->keyHashes[i] != 0)
+ {
+ if(i == table->numEntries - 1) // last entry in table
+ i = 0;
+ else // try next entry
+ i++;
+ }
+
+ table->keyHashes[i] = keyHash;
+ table->values[i] = ptr;
+}
View
@@ -0,0 +1,22 @@
+/*
+ * Hash Table
+ */
+
+ #ifndef HASH_H
+ #define HASH_H
+
+ typedef struct hashTable {
+ unsigned int *keyHashes;
+ void **values;
+ int numEntries;
+ } hashTable_t;
+
+// Create new hash table with numEntries maximum entries
+hashTable_t *hashNewTable(int numEntries);
+// Get pointer to data associated with keyHash
+ void *hashFind(hashTable_t *table, unsigned int keyHash);
+ // Add key-pointer pair to table
+ void hashAdd(hashTable_t *table, void *ptr, unsigned int keyHash);
+// Get hash value for key
+unsigned int hashFunction(void *key, int len);
+ #endif

0 comments on commit fe03c0d

Please sign in to comment.