Merge pull request #12 from bmiklautz/listdict

Improvements and new function for ListDictionary
This commit is contained in:
Marc-André Moreau 2013-10-15 08:02:42 -07:00
commit dfcab616f8
3 changed files with 76 additions and 2 deletions

View File

@ -191,13 +191,15 @@ struct _wListDictionary
CRITICAL_SECTION lock; CRITICAL_SECTION lock;
wListDictionaryItem* head; wListDictionaryItem* head;
wObject object;
}; };
typedef struct _wListDictionary wListDictionary; typedef struct _wListDictionary wListDictionary;
WINPR_API int ListDictionary_Count(wListDictionary* listDictionary); WINPR_API int ListDictionary_Count(wListDictionary* listDictionary);
WINPR_API void ListDictionary_Add(wListDictionary* listDictionary, void* key, void* value); WINPR_API void ListDictionary_Add(wListDictionary* listDictionary, void* key, void* value);
WINPR_API void ListDictionary_Remove(wListDictionary* listDictionary, void* key); WINPR_API void* ListDictionary_Remove(wListDictionary* listDictionary, void* key);
WINPR_API void* ListDictionary_Remove_Head(wListDictionary* listDictionary);
WINPR_API void ListDictionary_Clear(wListDictionary* listDictionary); WINPR_API void ListDictionary_Clear(wListDictionary* listDictionary);
WINPR_API BOOL ListDictionary_Contains(wListDictionary* listDictionary, void* key); WINPR_API BOOL ListDictionary_Contains(wListDictionary* listDictionary, void* key);

View File

@ -22,6 +22,7 @@
#endif #endif
#include <winpr/collections.h> #include <winpr/collections.h>
#include <winpr/memory.h>
/** /**
* C equivalent of the C# ListDictionary Class: * C equivalent of the C# ListDictionary Class:
@ -150,6 +151,8 @@ void ListDictionary_Clear(wListDictionary* listDictionary)
while (item) while (item)
{ {
nextItem = item->next; nextItem = item->next;
if (listDictionary->object.fnObjectFree)
listDictionary->object.fnObjectFree(item);
free(item); free(item);
item = nextItem; item = nextItem;
} }
@ -198,10 +201,11 @@ BOOL ListDictionary_Contains(wListDictionary* listDictionary, void* key)
* Removes the entry with the specified key from the ListDictionary. * Removes the entry with the specified key from the ListDictionary.
*/ */
void ListDictionary_Remove(wListDictionary* listDictionary, void* key) void *ListDictionary_Remove(wListDictionary* listDictionary, void* key)
{ {
wListDictionaryItem* item; wListDictionaryItem* item;
wListDictionaryItem* prevItem; wListDictionaryItem* prevItem;
void *value = NULL;
if (listDictionary->synchronized) if (listDictionary->synchronized)
EnterCriticalSection(&listDictionary->lock); EnterCriticalSection(&listDictionary->lock);
@ -213,6 +217,7 @@ void ListDictionary_Remove(wListDictionary* listDictionary, void* key)
if (listDictionary->head->key == key) if (listDictionary->head->key == key)
{ {
listDictionary->head = listDictionary->head->next; listDictionary->head = listDictionary->head->next;
value = item->value;
free(item); free(item);
} }
else else
@ -227,6 +232,7 @@ void ListDictionary_Remove(wListDictionary* listDictionary, void* key)
if (item->key == key) if (item->key == key)
{ {
prevItem->next = item->next; prevItem->next = item->next;
value = item->value;
free(item); free(item);
break; break;
} }
@ -239,6 +245,32 @@ void ListDictionary_Remove(wListDictionary* listDictionary, void* key)
if (listDictionary->synchronized) if (listDictionary->synchronized)
LeaveCriticalSection(&listDictionary->lock); LeaveCriticalSection(&listDictionary->lock);
return value;
}
/**
* Removes the first (head) entry from the list
*/
void *ListDictionary_Remove_Head(wListDictionary* listDictionary)
{
wListDictionaryItem* item;
void *value = NULL;
if (listDictionary->synchronized)
EnterCriticalSection(&listDictionary->lock);
if (listDictionary->head)
{
item = listDictionary->head;
listDictionary->head = listDictionary->head->next;
value = item->value;
free(item);
}
if (listDictionary->synchronized)
LeaveCriticalSection(&listDictionary->lock);
return value;
} }
/** /**
@ -329,6 +361,7 @@ wListDictionary* ListDictionary_New(BOOL synchronized)
InitializeCriticalSectionAndSpinCount(&listDictionary->lock, 4000); InitializeCriticalSectionAndSpinCount(&listDictionary->lock, 4000);
} }
ZeroMemory(&listDictionary->object, sizeof(wObject));
return listDictionary; return listDictionary;
} }

View File

@ -113,6 +113,45 @@ int TestListDictionary(int argc, char* argv[])
return -1; return -1;
} }
if (!ListDictionary_Remove(list, key2))
{
printf("ListDictionary_Remove: Expected : %d, Actual: %d\n", TRUE, FALSE);
return -1;
}
if (ListDictionary_Remove(list, key2))
{
printf("ListDictionary_Remove: Expected : %d, Actual: %d\n", FALSE, TRUE);
return -1;
}
value = ListDictionary_Remove_Head(list);
count = ListDictionary_Count(list);
if (strncmp(value, val1, 4) || count != 1)
{
printf("ListDictionary_Remove_Head: Expected : %s, Actual: %s Count: %d\n", val1, value, count);
return -1;
}
value = ListDictionary_Remove_Head(list);
count = ListDictionary_Count(list);
if (strncmp(value, val3, 4) || count != 0)
{
printf("ListDictionary_Remove_Head: Expected : %s, Actual: %s Count: %d\n", val3, value, count);
return -1;
}
value = ListDictionary_Remove_Head(list);
if (value)
{
printf("ListDictionary_Remove_Head: Expected : (null), Actual: %s Count: %d\n", value);
return -1;
}
ListDictionary_Add(list, key1, val1);
ListDictionary_Add(list, key2, val2);
ListDictionary_Add(list, key3, val3);
ListDictionary_Clear(list); ListDictionary_Clear(list);
count = ListDictionary_Count(list); count = ListDictionary_Count(list);