-
Notifications
You must be signed in to change notification settings - Fork 3
/
sortedtable.c
175 lines (144 loc) · 3.62 KB
/
sortedtable.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* cproxy - Copyright (C) 2012 Aaron Riekenberg ([email protected]).
cproxy is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cproxy is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cproxy. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <stdio.h>
#include "memutil.h"
#include "rb.h"
#include "sortedtable.h"
struct SortedTableEntry
{
int key;
void* data;
};
static int compareSortedTableEntries(
const void* p1, const void* p2, void* rbParam)
{
const struct SortedTableEntry* ste1 = p1;
const struct SortedTableEntry* ste2 = p2;
if (ste1->key < ste2->key)
{
return -1;
}
else if (ste1->key == ste2->key)
{
return 0;
}
else
{
return 1;
}
}
static void* sortedTableTreeMalloc(
struct libavl_allocator* allocator,
size_t size)
{
return checkedMalloc(size);
}
static void sortedTableTreeFree(
struct libavl_allocator* allocator,
void* ptr)
{
free(ptr);
}
static struct libavl_allocator sortedTableTreeAllocator =
{
sortedTableTreeMalloc,
sortedTableTreeFree
};
void initializeSortedTable(
struct SortedTable* sortedTable)
{
assert(sortedTable != NULL);
sortedTable->pRBTable =
rb_create(&compareSortedTableEntries, NULL, &sortedTableTreeAllocator);
}
void addToSortedTable(
struct SortedTable* sortedTable,
int key,
void* data)
{
struct SortedTableEntry* pEntry;
assert(sortedTable != NULL);
pEntry = checkedMalloc(sizeof(struct SortedTableEntry));
pEntry->key = key;
pEntry->data = data;
if (rb_insert(sortedTable->pRBTable, pEntry))
{
printf("addToSortedTable attempt to insert duplicate key %d\n", key);
abort();
}
}
void* replaceDataInSortedTable(
struct SortedTable* sortedTable,
int key,
void* data)
{
void* oldData = NULL;
struct SortedTableEntry* pReplacedEntry;
struct SortedTableEntry* pEntry;
assert(sortedTable != NULL);
pEntry = checkedMalloc(sizeof(struct SortedTableEntry));
pEntry->key = key;
pEntry->data = data;
pReplacedEntry = rb_replace(sortedTable->pRBTable, pEntry);
if (pReplacedEntry)
{
oldData = pReplacedEntry->data;
free(pReplacedEntry);
}
return oldData;
}
bool sortedTableContainsKey(
const struct SortedTable* sortedTable,
int key)
{
bool retVal = false;
const struct SortedTableEntry entryToFind = {key, NULL};
assert(sortedTable != NULL);
if (rb_find(sortedTable->pRBTable, &entryToFind))
{
retVal = true;
}
return retVal;
}
void* findDataInSortedTable(
const struct SortedTable* sortedTable,
int key)
{
void* retVal = NULL;
const struct SortedTableEntry entryToFind = {key, NULL};
struct SortedTableEntry* pEntryFound;
assert(sortedTable != NULL);
pEntryFound = rb_find(sortedTable->pRBTable, &entryToFind);
if (pEntryFound)
{
retVal = pEntryFound->data;
}
return retVal;
}
void* removeFromSortedTable(
struct SortedTable* sortedTable,
int key)
{
void* retVal = NULL;
const struct SortedTableEntry entryToFind = {key, NULL};
struct SortedTableEntry* pEntryFound;
assert(sortedTable != NULL);
pEntryFound = rb_delete(sortedTable->pRBTable, &entryToFind);
if (pEntryFound)
{
retVal = pEntryFound->data;
free(pEntryFound);
}
return retVal;
}