libwinpr-utils: fix ArrayList
This commit is contained in:
parent
2f6192532f
commit
be98cffbd2
@ -102,6 +102,9 @@ WINPR_API BOOL ArrayList_IsFixedSized(wArrayList* arrayList);
|
||||
WINPR_API BOOL ArrayList_IsReadOnly(wArrayList* arrayList);
|
||||
WINPR_API BOOL ArrayList_IsSynchronized(wArrayList* arrayList);
|
||||
|
||||
WINPR_API BOOL ArrayList_Lock(wArrayList* arrayList);
|
||||
WINPR_API BOOL ArrayList_Unlock(wArrayList* arrayList);
|
||||
|
||||
WINPR_API void* ArrayList_GetItem(wArrayList* arrayList, int index);
|
||||
WINPR_API void ArrayList_SetItem(wArrayList* arrayList, int index, void* obj);
|
||||
|
||||
@ -112,7 +115,7 @@ WINPR_API int ArrayList_Add(wArrayList* arrayList, void* obj);
|
||||
WINPR_API void ArrayList_Insert(wArrayList* arrayList, int index, void* obj);
|
||||
|
||||
WINPR_API void ArrayList_Remove(wArrayList* arrayList, void* obj);
|
||||
WINPR_API void ArrayList_RemoveAt(wArrayList* arrayList, int index, void* obj);
|
||||
WINPR_API void ArrayList_RemoveAt(wArrayList* arrayList, int index);
|
||||
|
||||
WINPR_API int ArrayList_IndexOf(wArrayList* arrayList, void* obj, int startIndex, int count);
|
||||
WINPR_API int ArrayList_LastIndexOf(wArrayList* arrayList, void* obj, int startIndex, int count);
|
||||
|
@ -79,6 +79,24 @@ BOOL ArrayList_IsSynchronized(wArrayList* arrayList)
|
||||
return arrayList->synchronized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lock access to the ArrayList
|
||||
*/
|
||||
|
||||
BOOL ArrayList_Lock(wArrayList* arrayList)
|
||||
{
|
||||
return (WaitForSingleObject(arrayList->mutex, INFINITE) == WAIT_OBJECT_0) ? TRUE : FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlock access to the ArrayList
|
||||
*/
|
||||
|
||||
BOOL ArrayList_Unlock(wArrayList* arrayList)
|
||||
{
|
||||
return ReleaseMutex(arrayList->mutex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the element at the specified index.
|
||||
*/
|
||||
@ -87,17 +105,11 @@ void* ArrayList_GetItem(wArrayList* arrayList, int index)
|
||||
{
|
||||
void* obj = NULL;
|
||||
|
||||
if (arrayList->synchronized)
|
||||
WaitForSingleObject(arrayList->mutex, INFINITE);
|
||||
|
||||
if ((index >= 0) && (index < arrayList->size))
|
||||
{
|
||||
obj = arrayList->array[index];
|
||||
}
|
||||
|
||||
if (arrayList->synchronized)
|
||||
ReleaseMutex(arrayList->mutex);
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
@ -107,16 +119,10 @@ void* ArrayList_GetItem(wArrayList* arrayList, int index)
|
||||
|
||||
void ArrayList_SetItem(wArrayList* arrayList, int index, void* obj)
|
||||
{
|
||||
if (arrayList->synchronized)
|
||||
WaitForSingleObject(arrayList->mutex, INFINITE);
|
||||
|
||||
if ((index >= 0) && (index < arrayList->size))
|
||||
{
|
||||
arrayList->array[index] = obj;
|
||||
}
|
||||
|
||||
if (arrayList->synchronized)
|
||||
ReleaseMutex(arrayList->mutex);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -129,9 +135,6 @@ void ArrayList_SetItem(wArrayList* arrayList, int index, void* obj)
|
||||
|
||||
void ArrayList_Shift(wArrayList* arrayList, int index, int count)
|
||||
{
|
||||
if (arrayList->synchronized)
|
||||
WaitForSingleObject(arrayList->mutex, INFINITE);
|
||||
|
||||
if (count > 0)
|
||||
{
|
||||
if (arrayList->size + count > arrayList->capacity)
|
||||
@ -140,17 +143,14 @@ void ArrayList_Shift(wArrayList* arrayList, int index, int count)
|
||||
arrayList->array = (void**) realloc(arrayList->array, sizeof(void*) * arrayList->capacity);
|
||||
}
|
||||
|
||||
MoveMemory(&arrayList->array[index + count], &arrayList->array[index], count);
|
||||
MoveMemory(&arrayList->array[index + count], &arrayList->array[index], (arrayList->size - index) * sizeof(void*));
|
||||
arrayList->size += count;
|
||||
}
|
||||
else if (count < 0)
|
||||
{
|
||||
MoveMemory(&arrayList->array[index + count], &arrayList->array[index], count);
|
||||
MoveMemory(&arrayList->array[index + count], &arrayList->array[index], (arrayList->size - index) * sizeof(void*));
|
||||
arrayList->size += count;
|
||||
}
|
||||
|
||||
if (arrayList->synchronized)
|
||||
ReleaseMutex(arrayList->mutex);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -252,7 +252,7 @@ void ArrayList_Remove(wArrayList* arrayList, void* obj)
|
||||
* Removes the element at the specified index of the ArrayList.
|
||||
*/
|
||||
|
||||
void ArrayList_RemoveAt(wArrayList* arrayList, int index, void* obj)
|
||||
void ArrayList_RemoveAt(wArrayList* arrayList, int index)
|
||||
{
|
||||
if (arrayList->synchronized)
|
||||
WaitForSingleObject(arrayList->mutex, INFINITE);
|
||||
|
@ -6,6 +6,7 @@ set(${MODULE_PREFIX}_DRIVER ${MODULE_NAME}.c)
|
||||
|
||||
set(${MODULE_PREFIX}_TESTS
|
||||
TestQueue.c
|
||||
TestArrayList.c
|
||||
TestCmdLine.c)
|
||||
|
||||
create_test_sourcelist(${MODULE_PREFIX}_SRCS
|
||||
|
50
winpr/libwinpr/utils/test/TestArrayList.c
Normal file
50
winpr/libwinpr/utils/test/TestArrayList.c
Normal file
@ -0,0 +1,50 @@
|
||||
|
||||
#include <winpr/crt.h>
|
||||
#include <winpr/tchar.h>
|
||||
#include <winpr/collections.h>
|
||||
|
||||
int TestArrayList(int argc, char* argv[])
|
||||
{
|
||||
int index;
|
||||
int count;
|
||||
wArrayList* arrayList;
|
||||
|
||||
arrayList = ArrayList_New(TRUE);
|
||||
|
||||
for (index = 0; index < 10; index++)
|
||||
{
|
||||
ArrayList_Add(arrayList, (void*) (size_t) index);
|
||||
}
|
||||
|
||||
count = ArrayList_Count(arrayList);
|
||||
|
||||
printf("ArrayList count: %d\n", count);
|
||||
|
||||
index = ArrayList_IndexOf(arrayList, (void*) (size_t) 6, -1, -1);
|
||||
|
||||
printf("ArrayList index: %d\n", index);
|
||||
|
||||
if (index != 6)
|
||||
return -1;
|
||||
|
||||
ArrayList_Insert(arrayList, 5, (void*) (size_t) 100);
|
||||
|
||||
index = ArrayList_IndexOf(arrayList, (void*) (size_t) 6, -1, -1);
|
||||
printf("ArrayList index: %d\n", index);
|
||||
|
||||
if (index != 7)
|
||||
return -1;
|
||||
|
||||
ArrayList_Remove(arrayList, (void*) (size_t) 100);
|
||||
|
||||
index = ArrayList_IndexOf(arrayList, (void*) (size_t) 6, -1, -1);
|
||||
printf("ArrayList index: %d\n", index);
|
||||
|
||||
if (index != 6)
|
||||
return -1;
|
||||
|
||||
ArrayList_Clear(arrayList);
|
||||
ArrayList_Free(arrayList);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user