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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
/* list.c
*/
/* This software is copyrighted as detailed in the LICENSE file. */
#include "EXTERN.h"
#include "common.h"
#include "util.h"
#include "INTERN.h"
#include "list.h"
#include "list.ih"
void
list_init()
{
;
}
/* Create the header for a dynamic list of items.
*/
LIST*
new_list(low, high, item_size, items_per_node, flags, init_node)
long low;
long high;
int item_size;
int items_per_node;
int flags;
void (*init_node) _((LIST*,LISTNODE*));
{
LIST* list = (LIST*)safemalloc(sizeof (LIST));
list->first = list->recent = NULL;
list->init_node = init_node? init_node : def_init_node;
list->low = low;
list->high = high;
list->item_size = item_size;
list->items_per_node = items_per_node;
list->flags = flags;
return list;
}
/* The default way to initialize a node */
static void
def_init_node(list, node)
LIST* list;
LISTNODE* node;
{
if (list->flags & LF_ZERO_MEM)
bzero(node->data, list->items_per_node * list->item_size);
}
/* Take the number of a list element and return its pointer. This
** will allocate new list items as needed, keeping the list->high
** value up to date.
*/
char*
listnum2listitem(list, num)
LIST* list;
long num;
{
LISTNODE* node = list->recent;
LISTNODE* prevnode = NULL;
if (node && num < node->low)
node = list->first;
for (;;) {
if (!node || num < node->low) {
node = (LISTNODE*)safemalloc(list->items_per_node*list->item_size
+ sizeof (LISTNODE) - 1);
if (list->flags & LF_SPARSE)
node->low = ((num - list->low) / list->items_per_node)
* list->items_per_node + list->low;
else
node->low = num;
node->high = node->low + list->items_per_node - 1;
node->data_high = node->data
+ (list->items_per_node - 1) * list->item_size;
if (node->high > list->high)
list->high = node->high;
if (prevnode) {
node->next = prevnode->next;
prevnode->next = node;
}
else {
node->next = list->first;
list->first = node;
}
/*node->mid = $$;*/
list->init_node(list, node);
break;
}
if (num <= node->high)
break;
prevnode = node;
node = node->next;
}
list->recent = node;
return node->data + (num - node->low) * list->item_size;
}
/* Take the pointer of a list element and return its number. The item
** must already exist or this will infinite loop.
*/
long
listitem2listnum(list, ptr)
LIST* list;
char* ptr;
{
LISTNODE* node;
char* cp;
int i;
int item_size = list->item_size;
for (node = list->recent; ; node = node->next) {
if (!node)
node = list->first;
i = node->high - node->low + 1;
for (cp = node->data; i--; cp += item_size) {
if (ptr == cp) {
list->recent = node;
return (ptr - node->data) / list->item_size + node->low;
}
}
}
return -1;
}
/* Execute the indicated callback function on every item in the list.
*/
bool
walk_list(list, callback, arg)
LIST* list;
bool (*callback) _((char*,int));
int arg;
{
LISTNODE* node;
char* cp;
int i;
int item_size = list->item_size;
for (node = list->first; node; node = node->next) {
i = node->high - node->low + 1;
for (cp = node->data; i--; cp += item_size)
if (callback(cp, arg))
return 1;
}
return 0;
}
/* Since the list can be sparsely allocated, find the nearest number
** that is already allocated, rounding in the indicated direction from
** the initial list number.
*/
long
existing_listnum(list, num, direction)
LIST* list;
long num;
int direction;
{
register LISTNODE* node = list->recent;
LISTNODE* prevnode = NULL;
if (node && num < node->low)
node = list->first;
while (node) {
if (num <= node->high) {
if (direction > 0) {
if (num < node->low)
num = node->low;
}
else if (direction == 0) {
if (num < node->low)
num = 0;
}
else if (num < node->low) {
if (!prevnode)
break;
list->recent = prevnode;
return prevnode->high;
}
list->recent = node;
return num;
}
prevnode = node;
node = node->next;
}
if (!direction)
return 0;
if (direction > 0)
return list->high + 1;
return list->low - 1;
}
/* Increment the item pointer to the next allocated item.
** Returns NULL if ptr is the last one.
*/
char*
next_listitem(list, ptr)
LIST* list;
char* ptr;
{
register LISTNODE* node = list->recent;
if (ptr == node->data_high) {
node = node->next;
if (!node)
return NULL;
list->recent = node;
return node->data;
}
#if 0
if (node->high > list->high) {
if ((ptr - node->data) / list->item_size + node->low >= list->high)
return NULL;
}
#endif
return ptr += list->item_size;
}
/* Decrement the item pointer to the prev allocated item.
** Returns NULL if ptr is the first one.
*/
char*
prev_listitem(list, ptr)
LIST* list;
char* ptr;
{
register LISTNODE* node = list->recent;
if (ptr == node->data) {
LISTNODE* prev = list->first;
if (prev == node)
return NULL;
while (prev->next != node)
prev = prev->next;
list->recent = prev;
return prev->data_high;
}
return ptr -= list->item_size;
}
/* Delete the list and all its allocated nodes. If you need to cleanup
** the individual nodes, call walk_list() with a cleanup function before
** calling this.
*/
void
delete_list(list)
LIST* list;
{
LISTNODE* node = list->first;
LISTNODE* prevnode = NULL;
while (node) {
prevnode = node;
node = node->next;
free((char*)prevnode);
}
free((char*)list);
}
|