-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathomlist.c
More file actions
191 lines (181 loc) · 4.99 KB
/
omlist.c
File metadata and controls
191 lines (181 loc) · 4.99 KB
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
/**
* @file omlist.c
* Offset based List implementation
*
* Copyright 2017, ECLB Ltd
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <glib.h>
#include "omem.h"
omlist omlist_prepend(om_block * om, omlist l, omlistentry * e)
{
omlistentry *list = omo2p(om, l);
e->next = omp2o(om, list);
if (list) {
if (list->prev) {
omlistentry *prev = omo2p(om, list->prev);
prev->next = omp2o(om, e);
}
e->prev = list->prev;
list->prev = omp2o(om, e);
} else {
e->prev = 0;
}
return omp2o(om, e);
}
omlist omlist_append(om_block * om, omlist l, omlistentry * e)
{
omlistentry *list = omo2p(om, l);
if (list) {
omlistentry *last = list;
while (last && last->next)
last = omo2p(om, last->next);
last->next = omp2o(om, e);
e->prev = omp2o(om, last);
return omp2o(om, list);
} else {
e->prev = 0;
return omp2o(om, e);
}
}
omlist omlist_remove(om_block * om, omlist l, omlistentry * e)
{
omlistentry *list = omo2p(om, l);
if (e == NULL)
return omp2o(om, list);
if (e->prev) {
omlistentry *prev = omo2p(om, e->prev);
assert(prev->next == omp2o(om, e));
prev->next = e->next;
}
if (e->next) {
omlistentry *next = omo2p(om, e->next);
assert(next->prev == omp2o(om, e));
next->prev = e->prev;
}
if (e == list)
list = omo2p(om, list->next);
e->next = 0;
e->prev = 0;
return omp2o(om, list);
}
size_t omlist_length(om_block * om, omlist l)
{
omlistentry *list = omo2p(om, l);
size_t length = 0;
while (list) {
length++;
list = omo2p(om, list->next);
}
return length;
}
omlistentry *omlist_get(om_block * om, omlist l, unsigned int offset)
{
omlistentry *list = omo2p(om, l);
while ((offset-- > 0) && list)
list = omo2p(om, list->next);
return list;
}
omlist omlist_reverse(om_block * om, omlist l)
{
omlistentry *list = omo2p(om, l);
omlistentry *last = NULL;
while (list) {
last = list;
list = omo2p(om, list->next);
last->next = last->prev;
last->prev = omp2o(om, list);
}
return omp2o(om, last);
}
omlist omlist_concat(om_block * om, omlist l1, omlist l2)
{
omlistentry *list1 = omo2p(om, l1);
omlistentry *list2 = omo2p(om, l2);
omlistentry *last = list1;
while (last && last->next)
last = omo2p(om, last->next);
if (last)
last->next = omp2o(om, list2);
else
list1 = list2;
list2->prev = omp2o(om, last);
return omp2o(om, list1);
}
omlistentry *omlist_find(om_block * om, omlist l, omlist_find_fn func, void *data)
{
assert(func);
omlistentry *list = omo2p(om, l);
while (list) {
if (func(om, list, data))
return list;
list = omo2p(om, list->next);
}
return NULL;
}
static omlist omlist_sort_merge(om_block * om, omlist list1, omlist list2,
omlist_cmp_fn func)
{
omlistentry *l1 = omo2p(om, list1);
omlistentry *l2 = omo2p(om, list2);
omlistentry list, *l, *lprev;
int cmp;
l = &list;
lprev = NULL;
while (l1 && l2) {
cmp = func(om, l1, l2);
if (cmp <= 0) {
l->next = omp2o(om, l1);
l1 = omo2p(om, l1->next);
} else {
l->next = omp2o(om, l2);
l2 = omo2p(om, l2->next);
}
l = omo2p(om, l->next);
l->prev = omp2o(om, lprev);
lprev = l;
}
l->next = l1 ? omp2o(om, l1) : omp2o(om, l2);
lprev = omo2p(om, l->next);
lprev->prev = omp2o(om, l);
return list.next;
}
omlist omlist_sort(om_block * om, omlist l, omlist_cmp_fn func)
{
omlistentry *list = omo2p(om, l);
omlistentry *l1, *l2;
if (!list)
return 0;
if (!list->next)
return omp2o(om, list);
l1 = list;
l2 = omo2p(om, list->next);
while ((l2 = omo2p(om, l2->next)) != NULL) {
if ((l2 = omo2p(om, l2->next)) == NULL)
break;
l1 = omo2p(om, l1->next);
}
l2 = omo2p(om, l1->next);
l1->next = 0;
return omlist_sort_merge(om,
omlist_sort(om, omp2o(om, list), func),
omlist_sort(om, omp2o(om, l2), func), func);
}