2015-06-19 10:27:46 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: Skiplist implementation for the ReactOS Project
|
2017-09-29 17:18:19 +00:00
|
|
|
* LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
|
2015-06-19 10:27:46 +00:00
|
|
|
* PURPOSE: A simple program for testing the Skiplist implementation
|
2017-09-29 17:18:19 +00:00
|
|
|
* COPYRIGHT: Copyright 2015 Colin Finck (colin@reactos.org)
|
2015-06-19 10:27:46 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "skiplist.h"
|
|
|
|
|
|
|
|
void
|
|
|
|
DumpSkiplist(PSKIPLIST Skiplist)
|
|
|
|
{
|
|
|
|
CHAR i;
|
|
|
|
DWORD j;
|
|
|
|
PSKIPLIST_NODE pNode;
|
|
|
|
|
|
|
|
printf("======= DUMPING SKIPLIST =======\n");
|
|
|
|
|
|
|
|
for (i = Skiplist->MaximumLevel + 1; --i >= 0;)
|
|
|
|
{
|
|
|
|
pNode = &Skiplist->Head;
|
|
|
|
printf("H");
|
|
|
|
|
|
|
|
while (pNode->Next[i])
|
|
|
|
{
|
|
|
|
printf("-");
|
|
|
|
|
|
|
|
// By using the Distance array for painting the lines, we verify both the links and the distances for correctness.
|
|
|
|
for (j = 1; j < pNode->Distance[i]; j++)
|
|
|
|
printf("---");
|
|
|
|
|
|
|
|
printf("%02lu", (DWORD)pNode->Next[i]->Element);
|
|
|
|
|
|
|
|
pNode = pNode->Next[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("================================\n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
PVOID WINAPI
|
|
|
|
MyAlloc(DWORD Size)
|
|
|
|
{
|
|
|
|
return HeapAlloc(GetProcessHeap(), 0, Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
int WINAPI
|
|
|
|
MyCompare(PVOID A, PVOID B)
|
|
|
|
{
|
|
|
|
return (DWORD)A - (DWORD)B;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WINAPI
|
|
|
|
MyFree(PVOID Ptr)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, Ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main()
|
|
|
|
{
|
|
|
|
DWORD Element;
|
|
|
|
DWORD ElementIndex;
|
|
|
|
DWORD i;
|
|
|
|
SKIPLIST Skiplist;
|
2015-06-25 13:12:01 +00:00
|
|
|
PSKIPLIST_NODE pNode;
|
2015-06-19 10:27:46 +00:00
|
|
|
|
|
|
|
system("mode con cols=300");
|
|
|
|
InitializeSkiplist(&Skiplist, MyAlloc, MyCompare, MyFree);
|
|
|
|
|
|
|
|
// Insert some random elements with random numbers.
|
|
|
|
for (i = 0; i < 40; i++)
|
|
|
|
InsertElementSkiplist(&Skiplist, (PVOID)(rand() % 100));
|
|
|
|
|
|
|
|
// Delete all with index 0 to 29.
|
|
|
|
for (i = 0; i < 30; i++)
|
|
|
|
DeleteElementSkiplist(&Skiplist, (PVOID)i);
|
|
|
|
|
|
|
|
// Insert some more random elements.
|
|
|
|
for (i = 0; i < 40; i++)
|
|
|
|
InsertElementSkiplist(&Skiplist, (PVOID)(rand() % 100));
|
|
|
|
|
2015-06-25 13:12:01 +00:00
|
|
|
// Output the third element (with zero-based index 2).
|
|
|
|
pNode = LookupNodeByIndexSkiplist(&Skiplist, 2);
|
|
|
|
printf("Element = %lu for index 2\n", (DWORD)pNode->Element);
|
|
|
|
|
2015-06-19 10:27:46 +00:00
|
|
|
// Check if an element with number 44 is in the list and output its index.
|
|
|
|
Element = (DWORD)LookupElementSkiplist(&Skiplist, (PVOID)44, &ElementIndex);
|
|
|
|
printf("Element = %lu, ElementIndex = %lu\n\n", Element, ElementIndex);
|
|
|
|
|
|
|
|
DumpSkiplist(&Skiplist);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|