forked from LPD-EPFL/ASCYLIB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintset.c
124 lines (108 loc) · 2.49 KB
/
intset.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
/*
* File: intset.c
* Author: Vincent Gramoli <[email protected]>,
* Vasileios Trigonakis <[email protected]>
* Description:
* intset.c is part of ASCYLIB
*
* Copyright (c) 2014 Vasileios Trigonakis <[email protected]>,
* Tudor David <[email protected]>
* Distributed Programming Lab (LPD), EPFL
*
* ASCYLIB 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, version 2
* of the License.
*
* 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.
*
*/
#include "intset.h"
sval_t
set_contains(intset_t *set, skey_t key)
{
sval_t result;
#ifdef DEBUG_PRINT
printf("++> set_contains(%d)\n", (int)val);
IO_FLUSH;
#endif
#ifdef SEQUENTIAL
node_t *prev, *next;
prev = set->head;
next = prev->next;
while (next->key < keyl)
{
prev = next;
next = prev->next;
}
result = (next->key == key) ? next->val : 0;
#elif defined LOCKFREE
result = harris_find(set, key);
#endif
return result;
}
inline int
set_seq_add(intset_t* set, skey_t key, sval_t val)
{
int result;
node_t *prev, *next;
prev = set->head;
next = prev->next;
while (next->key < key)
{
prev = next;
next = prev->next;
}
result = (next->key != key);
if (result)
{
prev->next = new_node(key, val, next, 0);
}
return result;
}
int
set_add(intset_t *set, skey_t key, skey_t val)
{
int result;
#ifdef DEBUG_PRINT
printf("++> set_add(%d)\n", (int)val);
IO_FLUSH;
#endif
#ifdef SEQUENTIAL /* Unprotected */
result = set_seq_add(set, key, val);
#elif defined LOCKFREE
result = harris_insert(set, key, val);
#endif
return result;
}
sval_t
set_remove(intset_t *set, skey_t key)
{
sval_t result = 0;
#ifdef DEBUG_PRINT
printf("++> set_remove(%d)\n", (int)val);
IO_FLUSH;
#endif
#ifdef SEQUENTIAL /* Unprotected */
node_t *prev, *next;
prev = set->head;
next = prev->next;
while (next->key < key)
{
prev = next;
next = prev->next;
}
result = (next->key == key) ? next->val : 0;
if (result)
{
prev->next = next->next;
free(next);
}
#elif defined LOCKFREE
result = harris_delete(set, key);
#endif
return result;
}