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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
|
/************************************************************************
*
* SKIPLIST.C - Skiplist functions for use in Nagios event/object lists
*
*
* Notes:
*
* These function implement a slightly modified skiplist from that
* described by William Pugh (ftp://ftp.cs.umd.edu/pub/skipLists/skiplists.pdf).
* The structures and function were modified to allow the list to act
* like a priority queue for the Nagios event list/queue(s). Multiple nodes with
* the same key value are allowed on the list to accommodate multiple events
* occurring at the same (second) point in time. Implemented peek() and pop()
* functions to allow for quick event queue processing, and a method to delete
* a specific list item, based on its pointer, rather than its data value. Again,
* this is useful for the Nagios event queue.
*
* License:
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
************************************************************************/
#include <unistd.h>
#include <stdlib.h>
#include "skiplist.h"
typedef struct skiplistnode_struct {
void *data;
struct skiplistnode_struct *forward[1]; /* this must be the last element of the struct, as we allocate # of elements during runtime*/
} skiplistnode;
struct skiplist_struct {
int current_level;
int max_levels;
float level_probability;
unsigned long items;
int allow_duplicates;
int append_duplicates;
int (*compare_function)(void *, void *);
skiplistnode *head;
};
unsigned long skiplist_num_items(skiplist *list) {
return list ? list->items : 0;
}
static skiplistnode *skiplist_new_node(skiplist *list, int node_levels) {
skiplistnode *newnode = NULL;
register int x = 0;
if(list == NULL)
return NULL;
if(node_levels < 0 || node_levels > list->max_levels)
return NULL;
/* allocate memory for node + variable number of level pointers */
if((newnode = (skiplistnode *)malloc(sizeof(skiplistnode) + (node_levels * sizeof(skiplistnode *))))) {
/* initialize forward pointers */
for(x = 0; x < node_levels; x++)
newnode->forward[x] = NULL;
/* initialize data pointer */
newnode->data = NULL;
}
return newnode;
}
skiplist *skiplist_new(int max_levels, float level_probability, int allow_duplicates, int append_duplicates, int (*compare_function)(void *, void *)) {
skiplist *newlist = NULL;
/* alloc memory for new list structure */
if((newlist = (skiplist *)malloc(sizeof(skiplist)))) {
/* initialize levels, etc. */
newlist->current_level = 0;
newlist->max_levels = max_levels;
newlist->level_probability = level_probability;
newlist->allow_duplicates = allow_duplicates;
newlist->append_duplicates = append_duplicates;
newlist->items = 0;
newlist->compare_function = compare_function;
/* initialize head node */
newlist->head = skiplist_new_node(newlist, max_levels);
}
return newlist;
}
static int skiplist_random_level(skiplist *list) {
int level = 0;
float r = 0.0;
if(list == NULL)
return -1;
for(level = 0; level < list->max_levels; level++) {
r = ((float)rand() / (float)RAND_MAX);
if(r > list->level_probability)
break;
}
return (level >= list->max_levels) ? list->max_levels - 1 : level;
}
int skiplist_insert(skiplist *list, void *data) {
skiplistnode **update = NULL;
skiplistnode *thisnode = NULL;
skiplistnode *nextnode = NULL;
skiplistnode *newnode = NULL;
int level = 0;
int x = 0;
if(list == NULL || data == NULL) {
return SKIPLIST_ERROR_ARGS;
}
/* check to make sure we don't have duplicates */
/* NOTE: this could made be more efficient */
if(list->allow_duplicates == FALSE) {
if(skiplist_find_first(list, data, NULL))
return SKIPLIST_ERROR_DUPLICATE;
}
/* initialize update vector */
if((update = (skiplistnode **)malloc(sizeof(skiplistnode *) * list->max_levels)) == NULL) {
return SKIPLIST_ERROR_MEMORY;
}
for(x = 0; x < list->max_levels; x++)
update[x] = NULL;
/* find proper position for insert, remember pointers with an update vector */
thisnode = list->head;
for(level = list->current_level; level >= 0; level--) {
while((nextnode = thisnode->forward[level])) {
if(list->append_duplicates == TRUE) {
if(list->compare_function(nextnode->data, data) > 0)
break;
}
else {
if(list->compare_function(nextnode->data, data) >= 0)
break;
}
thisnode = nextnode;
}
update[level] = thisnode;
}
/* get a random level the new node should be inserted at */
level = skiplist_random_level(list);
/* we're adding a new level... */
if(level > list->current_level) {
/*printf("NEW LEVEL!\n");*/
list->current_level++;
level = list->current_level;
update[level] = list->head;
}
/* create a new node */
if((newnode = skiplist_new_node(list, level)) == NULL) {
/*printf("NODE ERROR\n");*/
free(update);
return SKIPLIST_ERROR_MEMORY;
}
newnode->data = data;
/* update pointers to insert node at proper location */
do {
thisnode = update[level];
newnode->forward[level] = thisnode->forward[level];
thisnode->forward[level] = newnode;
}
while(--level >= 0);
/* update counters */
list->items++;
/* free memory */
free(update);
return SKIPLIST_OK;
}
int skiplist_empty(skiplist *list) {
skiplistnode *this = NULL;
skiplistnode *next = NULL;
int level = 0;
if(list == NULL)
return ERROR;
/* free all list nodes (but not header) */
for(this = list->head->forward[0]; this != NULL; this = next) {
next = this->forward[0];
free(this);
}
/* reset level pointers */
for(level = list->current_level; level >= 0; level--)
list->head->forward[level] = NULL;
/* reset list level */
list->current_level = 0;
/* reset items */
list->items = 0;
return OK;
}
int skiplist_free(skiplist **list) {
skiplistnode *this = NULL;
skiplistnode *next = NULL;
if(list == NULL)
return ERROR;
if(*list == NULL)
return OK;
/* free header and all list nodes */
for(this = (*list)->head; this != NULL; this = next) {
next = this->forward[0];
free(this);
}
/* free list structure */
free(*list);
*list = NULL;
return OK;
}
/* get first item in list */
void *skiplist_peek(skiplist *list) {
if(list == NULL)
return NULL;
/* return first item */
return list->head->forward[0]->data;
}
/* get/remove first item in list */
void *skiplist_pop(skiplist *list) {
skiplistnode *thisnode = NULL;
void *data = NULL;
int level = 0;
if(list == NULL)
return NULL;
/* get first item */
thisnode = list->head->forward[0];
if(thisnode == NULL)
return NULL;
/* get data for first item */
data = thisnode->data;
/* remove first item from queue - update forward links from head to first node */
for(level = 0; level <= list->current_level; level++) {
if(list->head->forward[level] == thisnode)
list->head->forward[level] = thisnode->forward[level];
}
/* free deleted node */
free(thisnode);
/* adjust items */
list->items--;
return data;
}
/* get first item in list */
void *skiplist_get_first(skiplist *list, void **node_ptr) {
skiplistnode *thisnode = NULL;
if(list == NULL)
return NULL;
/* get first node */
thisnode = list->head->forward[0];
/* return pointer to node */
if(node_ptr)
*node_ptr = (void *)thisnode;
if(thisnode)
return thisnode->data;
else
return NULL;
}
/* get next item in list */
void *skiplist_get_next(void **node_ptr) {
skiplistnode *thisnode = NULL;
skiplistnode *nextnode = NULL;
if(node_ptr == NULL || *node_ptr == NULL)
return NULL;
thisnode = (skiplistnode *)(*node_ptr);
nextnode = thisnode->forward[0];
*node_ptr = (void *)nextnode;
if(nextnode)
return nextnode->data;
else
return NULL;
}
/* first first item in list */
void *skiplist_find_first(skiplist *list, void *data, void **node_ptr) {
skiplistnode *thisnode = NULL;
skiplistnode *nextnode = NULL;
int level = 0;
if(list == NULL || data == NULL)
return NULL;
thisnode = list->head;
for(level = list->current_level; level >= 0; level--) {
while((nextnode = thisnode->forward[level])) {
if(list->compare_function(nextnode->data, data) >= 0)
break;
thisnode = nextnode;
}
}
/* we found it! */
if(nextnode && list->compare_function(nextnode->data, data) == 0) {
if(node_ptr)
*node_ptr = (void *)nextnode;
return nextnode->data;
}
else {
if(node_ptr)
*node_ptr = NULL;
}
return NULL;
}
/* find next match */
void *skiplist_find_next(skiplist *list, void *data, void **node_ptr) {
skiplistnode *thisnode = NULL;
skiplistnode *nextnode = NULL;
if(list == NULL || data == NULL || node_ptr == NULL)
return NULL;
if(*node_ptr == NULL)
return NULL;
thisnode = (skiplistnode *)(*node_ptr);
nextnode = thisnode->forward[0];
if(nextnode) {
if(list->compare_function(nextnode->data, data) == 0) {
*node_ptr = (void *)nextnode;
return nextnode->data;
}
}
*node_ptr = NULL;
return NULL;
}
/* delete first matching item from list */
int skiplist_delete_first(skiplist *list, void *data) {
skiplistnode **update = NULL;
skiplistnode *thisnode = NULL;
skiplistnode *nextnode = NULL;
int level = 0;
int top_level = 0;
int deleted = FALSE;
int x = 0;
if(list == NULL || data == NULL)
return ERROR;
/* initialize update vector */
if((update = (skiplistnode **)malloc(sizeof(skiplistnode *) * list->max_levels)) == NULL)
return ERROR;
for(x = 0; x < list->max_levels; x++)
update[x] = NULL;
/* find location in list */
thisnode = list->head;
for(top_level = level = list->current_level; level >= 0; level--) {
while((nextnode = thisnode->forward[level])) {
if(list->compare_function(nextnode->data, data) >= 0)
break;
thisnode = nextnode;
}
update[level] = thisnode;
}
/* we found a match! */
if(list->compare_function(nextnode->data, data) == 0) {
/* adjust level pointers to bypass (soon to be) removed node */
for(level = 0; level <= top_level; level++) {
thisnode = update[level];
if(thisnode->forward[level] != nextnode)
break;
thisnode->forward[level] = nextnode->forward[level];
}
/* free node memory */
free(nextnode);
/* adjust top/current level of list is necessary */
while(list->head->forward[top_level] == NULL && top_level > 0)
top_level--;
list->current_level = top_level;
/* adjust items */
list->items--;
deleted = TRUE;
}
/* free memory */
free(update);
return deleted;
}
/* delete all matching items from list */
int skiplist_delete(skiplist *list, void *data) {
int deleted = 0;
int total_deleted = 0;
/* NOTE: there is a more efficient way to do this... */
while((deleted = skiplist_delete_first(list, data)) == 1)
total_deleted++;
return total_deleted;
}
/* delete specific node from list */
int skiplist_delete_node(skiplist *list, void *node_ptr) {
void *data = NULL;
skiplistnode **update = NULL;
skiplistnode *thenode = NULL;
skiplistnode *thisnode = NULL;
skiplistnode *nextnode = NULL;
int level = 0;
int top_level = 0;
int deleted = FALSE;
int x = 0;
if(list == NULL || node_ptr == NULL)
return ERROR;
/* we'll need the data from the node to first find the node */
thenode = (skiplistnode *)node_ptr;
data = thenode->data;
/* initialize update vector */
if((update = (skiplistnode **)malloc(sizeof(skiplistnode *) * list->max_levels)) == NULL)
return ERROR;
for(x = 0; x < list->max_levels; x++)
update[x] = NULL;
/* find location in list */
thisnode = list->head;
for(top_level = level = list->current_level; level >= 0; level--) {
while((nextnode = thisnode->forward[level])) {
/* next node would be too far */
if(list->compare_function(nextnode->data, data) > 0)
break;
/* this is the exact node we want */
if(list->compare_function(nextnode->data, data) == 0 && nextnode == thenode)
break;
thisnode = nextnode;
}
update[level] = thisnode;
}
/* we found a match! (value + pointers match) */
if(nextnode && list->compare_function(nextnode->data, data) == 0 && nextnode == thenode) {
/* adjust level pointers to bypass (soon to be) removed node */
for(level = 0; level <= top_level; level++) {
thisnode = update[level];
if(thisnode->forward[level] != nextnode)
break;
thisnode->forward[level] = nextnode->forward[level];
}
/* free node memory */
free(nextnode);
/* adjust top/current level of list is necessary */
while(list->head->forward[top_level] == NULL && top_level > 0)
top_level--;
list->current_level = top_level;
/* adjust items */
list->items--;
deleted = TRUE;
}
/* free memory */
free(update);
return deleted;
}
|