Merge pull request #12 from bmiklautz/listdict
Improvements and new function for ListDictionary
This commit is contained in:
commit
dfcab616f8
@ -191,13 +191,15 @@ struct _wListDictionary
|
||||
CRITICAL_SECTION lock;
|
||||
|
||||
wListDictionaryItem* head;
|
||||
wObject object;
|
||||
};
|
||||
typedef struct _wListDictionary wListDictionary;
|
||||
|
||||
WINPR_API int ListDictionary_Count(wListDictionary* listDictionary);
|
||||
|
||||
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 BOOL ListDictionary_Contains(wListDictionary* listDictionary, void* key);
|
||||
|
@ -22,6 +22,7 @@
|
||||
#endif
|
||||
|
||||
#include <winpr/collections.h>
|
||||
#include <winpr/memory.h>
|
||||
|
||||
/**
|
||||
* C equivalent of the C# ListDictionary Class:
|
||||
@ -150,6 +151,8 @@ void ListDictionary_Clear(wListDictionary* listDictionary)
|
||||
while (item)
|
||||
{
|
||||
nextItem = item->next;
|
||||
if (listDictionary->object.fnObjectFree)
|
||||
listDictionary->object.fnObjectFree(item);
|
||||
free(item);
|
||||
item = nextItem;
|
||||
}
|
||||
@ -198,10 +201,11 @@ BOOL ListDictionary_Contains(wListDictionary* listDictionary, void* key)
|
||||
* 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* prevItem;
|
||||
void *value = NULL;
|
||||
|
||||
if (listDictionary->synchronized)
|
||||
EnterCriticalSection(&listDictionary->lock);
|
||||
@ -213,6 +217,7 @@ void ListDictionary_Remove(wListDictionary* listDictionary, void* key)
|
||||
if (listDictionary->head->key == key)
|
||||
{
|
||||
listDictionary->head = listDictionary->head->next;
|
||||
value = item->value;
|
||||
free(item);
|
||||
}
|
||||
else
|
||||
@ -227,6 +232,7 @@ void ListDictionary_Remove(wListDictionary* listDictionary, void* key)
|
||||
if (item->key == key)
|
||||
{
|
||||
prevItem->next = item->next;
|
||||
value = item->value;
|
||||
free(item);
|
||||
break;
|
||||
}
|
||||
@ -239,6 +245,32 @@ void ListDictionary_Remove(wListDictionary* listDictionary, void* key)
|
||||
|
||||
if (listDictionary->synchronized)
|
||||
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);
|
||||
}
|
||||
|
||||
ZeroMemory(&listDictionary->object, sizeof(wObject));
|
||||
return listDictionary;
|
||||
}
|
||||
|
||||
|
@ -113,6 +113,45 @@ int TestListDictionary(int argc, char* argv[])
|
||||
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);
|
||||
|
||||
count = ListDictionary_Count(list);
|
||||
|
Loading…
x
Reference in New Issue
Block a user