-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortedList.h
79 lines (73 loc) · 2.48 KB
/
SortedList.h
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
/*
* SortedList (and SortedListElement)
*
* A doubly linked list, kept sorted by a specified key.
* This structure is used for a list head, and each element
* of the list begins with this structure.
*
* The list head is in the list, and an empty list contains
* only a list head. The list head is also recognizable because
* it has a NULL key pointer.
*/
struct SortedListElement {
struct SortedListElement *prev;
struct SortedListElement *next;
const char *key;
};
typedef struct SortedListElement SortedList_t;
typedef struct SortedListElement SortedListElement_t;
/**
* SortedList_insert ... insert an element into a sorted list
*
* The specified element will be inserted in to
* the specified list, which will be kept sorted
* in ascending order based on associated keys
*
* @param SortedList_t *list ... header for the list
* @param SortedListElement_t *element ... element to be added to the list
*/
void SortedList_insert(SortedList_t *list, SortedListElement_t *element);
/**
* SortedList_delete ... remove an element from a sorted list
*
* The specified element will be removed from whatever
* list it is currently in.
*
* Before doing the deletion, we check to make sure that
* next->prev and prev->next both point to this node
*
* @param SortedListElement_t *element ... element to be removed
*
* @return 0: element deleted successfully, 1: corrtuped prev/next pointers
*
*/
int SortedList_delete( SortedListElement_t *element);
/**
* SortedList_lookup ... search sorted list for a key
*
* The specified list will be searched for an
* element with the specified key.
*
* @param SortedList_t *list ... header for the list
* @param const char * key ... the desired key
*
* @return pointer to matching element, or NULL if none is found
*/
SortedListElement_t *SortedList_lookup(SortedList_t *list, const char *key);
/**
* SortedList_length ... count elements in a sorted list
* While enumeratign list, it checks all prev/next pointers
*
* @param SortedList_t *list ... header for the list
*
* @return int number of elements in list (excluding head)
* -1 if the list is corrupted
*/
int SortedList_length(SortedList_t *list);
/**
* variable to enable diagnositc yield calls
*/
extern int opt_yield;
#define INSERT_YIELD 0x01 // yield in insert critical section
#define DELETE_YIELD 0x02 // yield in delete critical section
#define LOOKUP_YIELD 0x04 // yield in lookup/length critical esction