-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlinkedlistseq.c
171 lines (138 loc) · 2.78 KB
/
linkedlistseq.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
/* (C) Jesper Larsson Traff, May 2020 */
/* Improved lock-free linked list implementations */
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <assert.h>
#include "linkedlist.h"
//#define DOUBLY
//#define CURSOR / only with DOUBLY
#ifdef COUNTERS
#define INC(_c) ((_c)++)
#else
#define INC(_c)
#endif
void init(node_t *head, node_t *tail, list_t *list)
{
list->head = head;
list->tail = tail;
// the sentinels
list->head->key = LONG_MIN;
list->head->next = tail;
list->head->prev = NULL;
list->tail->key = LONG_MAX;
list->tail->next = NULL;
list->tail->prev = head;
list->pred = head;
list->curr = NULL;
list->free = NULL;
#ifdef COUNTERS
list->adds = 0;
list->rems = 0;
list->cons = 0;
list->trav = 0;
list->fail = 0;
list->rtry = 0;
#endif
}
void clean(list_t *list)
{
node_t *next, *node;
next = list->free;
while (next!=NULL) {
node = next; next = next->free;
free(node);
}
list->free = NULL;
}
void pos(long key, list_t *list)
{
node_t *pred, *curr;
#ifdef DOUBLY
#ifdef CURSOR
curr = list->pred;
#else
curr = list->head;
#endif
if (curr->key>=key) {
pred = curr->prev;
while (key<pred->key) {
curr = pred; pred = pred->prev;
INC(list->trav);
}
} else
#else
curr = list->head;
#endif
{
do {
pred = curr; curr = curr->next;
INC(list->trav);
} while (key>curr->key);
}
list->pred = pred;
list->curr = curr;
assert(pred->key<curr->key);
}
int add(long key, list_t *list)
{
node_t *pred, *curr, *node;
pos(key,list);
pred = list->pred; curr = list->curr;
if (curr->key==key) return 0; // already there
INC(list->adds);
node = (node_t*)malloc(sizeof(node_t));
assert(node!=NULL);
node->key = key;
node->next = curr;
pred->next = node;
#ifdef DOUBLY
node->prev = pred;
curr->prev = node;
#endif
return 1;
}
int rem(long key, list_t *list)
{
node_t *pred, *node;
pos(key,list);
pred = list->pred;
node = list->curr;
if (node->key!=key) return 0; // not there
INC(list->rems);
pred->next = node->next;
#ifdef DOUBLY
node->next->prev = pred;
#endif
node->free = list->free;
list->free = node;
return 1;
}
int con(long key, list_t *list)
{
node_t *curr;
#ifdef DOUBLY
#ifdef CURSOR
curr = list->pred;
#else
curr = list->head;
#endif
INC(list->cons);
while (key<curr->key) {
curr = curr->prev;
INC(list->cons);
}
#else
curr = list->head;
#endif
while (key>curr->key) {
curr = curr->next;
INC(list->cons);
}
#ifdef DOUBLY
#ifdef CURSOR
list->pred = curr;
#endif
#endif
return (curr->key==key);
}