libwinpr-utils: add HashTable

This commit is contained in:
Marc-André Moreau 2014-02-09 21:55:21 -05:00
parent 1f18c27ec3
commit ad86d3c333
5 changed files with 647 additions and 1 deletions

View File

@ -257,12 +257,15 @@ WINPR_API void LinkedList_Free(wLinkedList* list);
/* System.Collections.Generic.KeyValuePair<TKey,TValue> */
typedef struct _wKeyValuePair wKeyValuePair;
struct _wKeyValuePair
{
void* key;
void* value;
wKeyValuePair* next;
};
typedef struct _wKeyValuePair wKeyValuePair;
/* Reference Table */
@ -315,6 +318,37 @@ WINPR_API void CountdownEvent_Reset(wCountdownEvent* countdown, DWORD count);
WINPR_API wCountdownEvent* CountdownEvent_New(DWORD initialCount);
WINPR_API void CountdownEvent_Free(wCountdownEvent* countdown);
/* Hash Table */
struct _wHashTable
{
long numOfBuckets;
long numOfElements;
float idealRatio;
float lowerRehashThreshold;
float upperRehashThreshold;
wKeyValuePair** bucketArray;
int (*keycmp)(void* key1, void* key2);
int (*valuecmp)(void* value1, void* value2);
unsigned long (*hashFunction)(void* key);
void (*keyDeallocator)(void* key);
void (*valueDeallocator)(void* value);
};
typedef struct _wHashTable wHashTable;
WINPR_API int HashTable_Count(wHashTable* table);
WINPR_API int HashTable_Add(wHashTable* table, void* key, void* value);
WINPR_API BOOL HashTable_Remove(wHashTable* table, void* key);
WINPR_API void HashTable_Clear(wHashTable* table);
WINPR_API BOOL HashTable_Contains(wHashTable* table, void* key);
WINPR_API BOOL HashTable_ContainsKey(wHashTable* table, void* key);
WINPR_API BOOL HashTable_ContainsValue(wHashTable* table, void* value);
WINPR_API void* HashTable_GetItemValue(wHashTable* table, void* key);
WINPR_API BOOL HashTable_SetItemValue(wHashTable* table, void* key, void* value);
WINPR_API wHashTable* HashTable_New();
WINPR_API void HashTable_Free(wHashTable* table);
/* BufferPool */
struct _wBufferPoolItem

View File

@ -28,6 +28,7 @@ set(${MODULE_PREFIX}_COLLECTIONS_SRCS
collections/ArrayList.c
collections/Dictionary.c
collections/LinkedList.c
collections/HashTable.c
collections/ListDictionary.c
collections/KeyValuePair.c
collections/CountdownEvent.c

View File

@ -0,0 +1,469 @@
/**
* WinPR: Windows Portable Runtime
* System.Collections.Hashtable
*
* Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <winpr/crt.h>
#include <winpr/collections.h>
/**
* This implementation is based on the public domain
* hash table implementation made by Keith Pomakis:
*
* http://www.pomakis.com/hashtable/hashtable.c
* http://www.pomakis.com/hashtable/hashtable.h
*/
static int isProbablePrime(long oddNumber)
{
long i;
for (i = 3; i < 51; i += 2)
{
if (oddNumber == i)
return 1;
else if (oddNumber % i == 0)
return 0;
}
return 1; /* maybe */
}
static long calculateIdealNumOfBuckets(wHashTable* table)
{
long idealNumOfBuckets = table->numOfElements / table->idealRatio;
if (idealNumOfBuckets < 5)
idealNumOfBuckets = 5;
else
idealNumOfBuckets |= 0x01;
while (!isProbablePrime(idealNumOfBuckets))
idealNumOfBuckets += 2;
return idealNumOfBuckets;
}
void HashTable_Rehash(wHashTable* table, long numOfBuckets)
{
int index;
long hashValue;
wKeyValuePair* pair;
wKeyValuePair* nextPair;
wKeyValuePair** newBucketArray;
if (numOfBuckets == 0)
numOfBuckets = calculateIdealNumOfBuckets(table);
if (numOfBuckets == table->numOfBuckets)
return; /* already the right size! */
newBucketArray = (wKeyValuePair**) malloc(numOfBuckets * sizeof(wKeyValuePair*));
if (newBucketArray == NULL)
{
/* Couldn't allocate memory for the new array. This isn't a fatal
* error; we just can't perform the rehash. */
return;
}
for (index = 0; index < numOfBuckets; index++)
newBucketArray[index] = NULL;
for (index = 0; index < table->numOfBuckets; index++)
{
pair = table->bucketArray[index];
while (pair != NULL)
{
nextPair = pair->next;
hashValue = table->hashFunction(pair->key) % numOfBuckets;
pair->next = newBucketArray[hashValue];
newBucketArray[hashValue] = pair;
pair = nextPair;
}
}
free(table->bucketArray);
table->bucketArray = newBucketArray;
table->numOfBuckets = numOfBuckets;
}
void HashTable_SetIdealRatio(wHashTable* table, float idealRatio,
float lowerRehashThreshold, float upperRehashThreshold)
{
table->idealRatio = idealRatio;
table->lowerRehashThreshold = lowerRehashThreshold;
table->upperRehashThreshold = upperRehashThreshold;
}
unsigned long HashTable_StringHashFunction(void* key)
{
int c;
unsigned long hash = 5381;
unsigned char* str = (unsigned char*) key;
/* djb2 algorithm */
while ((c = *str++) != '\0')
hash = (hash * 33) + c;
return hash;
}
wKeyValuePair* HashTable_Get(wHashTable* table, void* key)
{
long hashValue = table->hashFunction(key) % table->numOfBuckets;
wKeyValuePair* pair = table->bucketArray[hashValue];
while (pair != NULL && table->keycmp(key, pair->key) != 0)
pair = pair->next;
return pair;
}
static int pointercmp(void* pointer1, void* pointer2)
{
return (pointer1 != pointer2);
}
static unsigned long pointerHashFunction(void* pointer)
{
return ((unsigned long) pointer) >> 4;
}
/**
* C equivalent of the C# Hashtable Class:
* http://msdn.microsoft.com/en-us/library/system.collections.hashtable.aspx
*/
/**
* Properties
*/
/**
* Gets the number of key/value pairs contained in the HashTable.
*/
int HashTable_Count(wHashTable* table)
{
return table->numOfElements;
}
/**
* Methods
*/
/**
* Adds an element with the specified key and value into the HashTable.
*/
int HashTable_Add(wHashTable* table, void* key, void* value)
{
long hashValue;
wKeyValuePair* pair;
wKeyValuePair* newPair;
if (!key || !value)
return -1;
hashValue = table->hashFunction(key) % table->numOfBuckets;
pair = table->bucketArray[hashValue];
while (pair != NULL && table->keycmp(key, pair->key) != 0)
pair = pair->next;
if (pair)
{
if (pair->key != key)
{
if (table->keyDeallocator)
table->keyDeallocator((void*) pair->key);
pair->key = key;
}
if (pair->value != value)
{
if (table->valueDeallocator)
table->valueDeallocator(pair->value);
pair->value = value;
}
}
else
{
newPair = (wKeyValuePair*) malloc(sizeof(wKeyValuePair));
if (!newPair)
{
return -1;
}
else
{
newPair->key = key;
newPair->value = value;
newPair->next = table->bucketArray[hashValue];
table->bucketArray[hashValue] = newPair;
table->numOfElements++;
if (table->upperRehashThreshold > table->idealRatio)
{
float elementToBucketRatio = (float) table->numOfElements / (float) table->numOfBuckets;
if (elementToBucketRatio > table->upperRehashThreshold)
HashTable_Rehash(table, 0);
}
}
}
return 0;
}
/**
* Removes the element with the specified key from the HashTable.
*/
BOOL HashTable_Remove(wHashTable* table, void* key)
{
long hashValue = table->hashFunction(key) % table->numOfBuckets;
wKeyValuePair* pair = table->bucketArray[hashValue];
wKeyValuePair* previousPair = NULL;
while (pair != NULL && table->keycmp(key, pair->key) != 0)
{
previousPair = pair;
pair = pair->next;
}
if (!pair)
return FALSE;
if (table->keyDeallocator)
table->keyDeallocator((void*) pair->key);
if (table->valueDeallocator)
table->valueDeallocator(pair->value);
if (previousPair != NULL)
previousPair->next = pair->next;
else
table->bucketArray[hashValue] = pair->next;
free(pair);
table->numOfElements--;
if (table->lowerRehashThreshold > 0.0)
{
float elementToBucketRatio = (float) table->numOfElements / (float) table->numOfBuckets;
if (elementToBucketRatio < table->lowerRehashThreshold)
HashTable_Rehash(table, 0);
}
return TRUE;
}
/**
* Get an item value using key
*/
void* HashTable_GetItemValue(wHashTable* table, void* key)
{
wKeyValuePair* pair;
pair = HashTable_Get(table, key);
if (!pair)
return NULL;
return pair->value;
}
/**
* Set an item value using key
*/
BOOL HashTable_SetItemValue(wHashTable* table, void* key, void* value)
{
wKeyValuePair* pair;
pair = HashTable_Get(table, key);
if (!pair)
return FALSE;
pair->value = value;
return TRUE;
}
/**
* Removes all elements from the HashTable.
*/
void HashTable_Clear(wHashTable* table)
{
int index;
wKeyValuePair* pair;
wKeyValuePair* nextPair;
for (index = 0; index < table->numOfBuckets; index++)
{
pair = table->bucketArray[index];
while (pair != NULL)
{
nextPair = pair->next;
if (table->keyDeallocator)
table->keyDeallocator((void*) pair->key);
if (table->valueDeallocator)
table->valueDeallocator(pair->value);
free(pair);
pair = nextPair;
}
table->bucketArray[index] = NULL;
}
table->numOfElements = 0;
HashTable_Rehash(table, 5);
}
/**
* Determines whether the HashTable contains a specific key.
*/
BOOL HashTable_Contains(wHashTable* table, void* key)
{
return (HashTable_Get(table, key) != NULL) ? TRUE : FALSE;
}
/**
* Determines whether the HashTable contains a specific key.
*/
BOOL HashTable_ContainsKey(wHashTable* table, void* key)
{
return (HashTable_Get(table, key) != NULL) ? TRUE : FALSE;
}
/**
* Determines whether the HashTable contains a specific value.
*/
BOOL HashTable_ContainsValue(wHashTable* table, void* value)
{
int index;
wKeyValuePair* pair;
for (index = 0; index < table->numOfBuckets; index++)
{
pair = table->bucketArray[index];
while (pair != NULL)
{
if (table->valuecmp(value, pair->value) == 0)
return TRUE;
pair = pair->next;
}
}
return FALSE;
}
/**
* Construction, Destruction
*/
wHashTable* HashTable_New()
{
int index;
wHashTable* table;
table = (wHashTable*) calloc(1, sizeof(wHashTable));
if (table)
{
table->numOfBuckets = 64;
table->numOfElements = 0;
table->bucketArray = (wKeyValuePair**) malloc(table->numOfBuckets * sizeof(wKeyValuePair*));
if (!table->bucketArray)
{
free(table);
return NULL;
}
for (index = 0; index < table->numOfBuckets; index++)
table->bucketArray[index] = NULL;
table->idealRatio = 3.0;
table->lowerRehashThreshold = 0.0;
table->upperRehashThreshold = 15.0;
table->keycmp = pointercmp;
table->valuecmp = pointercmp;
table->hashFunction = pointerHashFunction;
table->keyDeallocator = NULL;
table->valueDeallocator = NULL;
}
return table;
}
void HashTable_Free(wHashTable* table)
{
int index;
wKeyValuePair* pair;
wKeyValuePair* nextPair;
if (table)
{
for (index = 0; index < table->numOfBuckets; index++)
{
pair = table->bucketArray[index];
while (pair)
{
nextPair = pair->next;
if (table->keyDeallocator)
table->keyDeallocator((void*) pair->key);
if (table->valueDeallocator)
table->valueDeallocator(pair->value);
free(pair);
pair = nextPair;
}
}
free(table->bucketArray);
free(table);
}
}

View File

@ -13,6 +13,7 @@ set(${MODULE_PREFIX}_TESTS
TestListDictionary.c
TestCmdLine.c
TestWLog.c
TestHashTable.c
TestBufferPool.c
TestStreamPool.c
TestMessageQueue.c

View File

@ -0,0 +1,141 @@
#include <winpr/crt.h>
#include <winpr/tchar.h>
#include <winpr/collections.h>
static char* key1 = "key1";
static char* key2 = "key2";
static char* key3 = "key3";
static char* val1 = "val1";
static char* val2 = "val2";
static char* val3 = "val3";
int TestHashTable(int argc, char* argv[])
{
int count;
char* value;
wHashTable* table;
table = HashTable_New();
HashTable_Add(table, key1, val1);
HashTable_Add(table, key2, val2);
HashTable_Add(table, key3, val3);
count = HashTable_Count(table);
if (count != 3)
{
printf("HashTable_Count: Expected : %d, Actual: %d\n", 3, count);
return -1;
}
HashTable_Remove(table, key2);
count = HashTable_Count(table);
if (count != 2)
{
printf("HashTable_Count: Expected : %d, Actual: %d\n", 2, count);
return -1;
}
HashTable_Remove(table, key3);
count = HashTable_Count(table);
if (count != 1)
{
printf("HashTable_Count: Expected : %d, Actual: %d\n", 1, count);
return -1;
}
HashTable_Remove(table, key1);
count = HashTable_Count(table);
if (count != 0)
{
printf("HashTable_Count: Expected : %d, Actual: %d\n", 0, count);
return -1;
}
HashTable_Add(table, key1, val1);
HashTable_Add(table, key2, val2);
HashTable_Add(table, key3, val3);
count = HashTable_Count(table);
if (count != 3)
{
printf("HashTable_Count: Expected : %d, Actual: %d\n", 3, count);
return -1;
}
value = (char*) HashTable_GetItemValue(table, key1);
if (strcmp(value, val1) != 0)
{
printf("HashTable_GetItemValue: Expected : %d, Actual: %d\n", val1, value);
return -1;
}
value = (char*) HashTable_GetItemValue(table, key2);
if (strcmp(value, val2) != 0)
{
printf("HashTable_GetItemValue: Expected : %d, Actual: %d\n", val2, value);
return -1;
}
value = (char*) HashTable_GetItemValue(table, key3);
if (strcmp(value, val3) != 0)
{
printf("HashTable_GetItemValue: Expected : %d, Actual: %d\n", val3, value);
return -1;
}
HashTable_SetItemValue(table, key2, "apple");
value = (char*) HashTable_GetItemValue(table, key2);
if (strcmp(value, "apple") != 0)
{
printf("HashTable_GetItemValue: Expected : %d, Actual: %d\n", "apple", value);
return -1;
}
if (!HashTable_Contains(table, key2))
{
printf("HashTable_Contains: Expected : %d, Actual: %d\n", TRUE, FALSE);
return -1;
}
if (!HashTable_Remove(table, key2))
{
printf("HashTable_Remove: Expected : %d, Actual: %d\n", TRUE, FALSE);
return -1;
}
if (HashTable_Remove(table, key2))
{
printf("HashTable_Remove: Expected : %d, Actual: %d\n", FALSE, TRUE);
return -1;
}
HashTable_Clear(table);
count = HashTable_Count(table);
if (count != 0)
{
printf("HashTable_Count: Expected : %d, Actual: %d\n", 0, count);
return -1;
}
HashTable_Free(table);
return 0;
}