summary refs log tree commit diff stats
path: root/test
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2010-04-26 21:37:31 +0200
committerhut <hut@lavabit.com>2010-04-26 21:37:31 +0200
commit4c5e60cbe14cde745d8590cfcc3f098d3e060ca4 (patch)
treee68c0e2c3aeba6d14e024e6b755a9e2177a77519 /test
parent292b447675bff7af2f4221da50b20dc49bbe66b6 (diff)
downloadranger-4c5e60cbe14cde745d8590cfcc3f098d3e060ca4.tar.gz
defaults.commands: fixed :find with uppercase search strings
Diffstat (limited to 'test')
0 files changed, 0 insertions, 0 deletions
ef='#n92'>92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
#include "hashtable.h"
#include "alloc.h"

typedef struct DataItem {
   uint32 data;
   uint32 key;
   uint8 used;
} DataItem;

typedef struct HashTable {
   DataItem* items;
   uint32 capacity;
} HashTable;

static uint32 hashCode(HashTable* hashTable, uint32 key) {
   return key % hashTable->capacity;
}

HashTable* HashTable_create(uint32 capacity) {
    HashTable* hashTable = kmalloc(sizeof(HashTable));
    memset((uint8*)hashTable, 0, sizeof(HashTable));
    hashTable->capacity = capacity;
    hashTable->items = kmalloc(sizeof(DataItem) * capacity);

    return hashTable;
}

void HashTable_destroy(HashTable* hashTable) {
    kfree(hashTable->items);
    kfree(hashTable);
}

DataItem* HashTable_search_internal(HashTable* hashTable, uint32 key) {
   //get the hash
   uint32 hashIndex = hashCode(hashTable, key);

   uint32 counter = 0;
   while(counter < hashTable->capacity) {
      if(hashTable->items[hashIndex].key == key) {
          if(hashTable->items[hashIndex].used == TRUE) {
              return &(hashTable->items[hashIndex]);
          }
      }

      //go to next cell
      ++hashIndex;

      //wrap around the table
      hashIndex %= hashTable->capacity;

      ++counter;
   }

   return NULL;
}

BOOL HashTable_search(HashTable* hashTable, uint32 key, uint32* value) {
    DataItem* existing = HashTable_search_internal(hashTable, key);

    if (existing) {
        *value = existing->data;

        return TRUE;
    }

    return FALSE;
}

BOOL HashTable_insert(HashTable* hashTable, uint32 key, uint32 data) {
    DataItem* existing = HashTable_search_internal(hashTable, key);

    if (existing) {
        existing->data = data;

        return TRUE;
    }

    //get the hash
    uint32 hashIndex = hashCode(hashTable, key);

    uint32 counter = 0;
    //move in array until an empty or deleted cell
    while(counter < hashTable->capacity) {
        if (hashTable->items[hashIndex].used == FALSE) {
            hashTable->items[hashIndex].key = key;
            hashTable->items[hashIndex].data = data;
            hashTable->items[hashIndex].used = TRUE;

            return TRUE;
        }


        //go to next cell
        ++hashIndex;

        //wrap around the table
        hashIndex %= hashTable->capacity;

        ++counter;
    }

    return FALSE;
}

BOOL HashTable_remove(HashTable* hashTable, uint32 key) {
    DataItem* existing = HashTable_search_internal(hashTable, key);

    if (existing) {
        existing->used = FALSE;

        return TRUE;
    }

    return FALSE;
}