forked from LPD-EPFL/ASCYLIB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseq.c
139 lines (125 loc) · 2.55 KB
/
seq.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
/*
* File: seq.c
* Author: Vasileios Trigonakis <vasileios.trigonakis@epfl.ch>
* Description:
* seq.c is part of ASCYLIB
*
* Copyright (c) 2014 Vasileios Trigonakis <vasileios.trigonakis@epfl.ch>,
* Tudor David <tudor.david@epfl.ch>
* 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 "seq.h"
node_t**
search(node_t** root, skey_t key)
{
node_t** node = root;
while (unlikely(*node != NULL))
{
sval_t node_key = (*node)->key;
if (key < node_key)
{
node = (node_t**) &(*node)->left;
}
else if (key > node_key)
{
node = (node_t**) &(*node)->right;
}
else
{
break;
}
}
return node;
}
void
seq_delete_node(node_t** node)
{
node_t* old_node = *node;
if ((*node)->left == NULL)
{
*node = (node_t*) (*node)->right;
node_delete(old_node);
}
else if ((*node)->right == NULL)
{
*node = (node_t*) (*node)->left;
node_delete(old_node);
}
else
{
/* delete node with two children */
node_t* pred = (node_t*) (*node)->left;
node_t* pred_pred = *node;
while(pred->right != NULL)
{
pred_pred=pred;
pred = (node_t*) pred->right;
}
skey_t tmp = pred->key;
(*node)->key = tmp;
if(pred_pred == *node)
{
(*node)->left = pred->left;
}
else
{
pred_pred->right = pred->left;
}
node_delete(pred);
}
}
sval_t
seq_delete(intset_t* set, skey_t key)
{
node_t** node = search(&set->head, key);
if (*node == NULL)
{
return 0;
}
seq_delete_node(node);
return 1;
}
sval_t
seq_find(intset_t* set, skey_t key)
{
node_t* node = set->head;
while (unlikely(node != NULL))
{
skey_t node_key = (node)->key;
if (key < node_key)
{
node = (node_t*) node->left;
}
else if (key > node_key)
{
node = (node_t*) node->right;
}
else
{
break;
}
}
return (node != NULL);
}
int
seq_insert(intset_t* set, skey_t key, sval_t val)
{
node_t** node = search(&set->head, key);
if (*node == NULL)
{
*node = new_node(key, val, 0);
return 1;
}
return 0;
}