Rizin
unix-like reverse engineering framework and cli tools
ls.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2007-2017 pancake <pancake@nopcode.org>
2 // SPDX-FileCopyrightText: 2007-2017 alvaro
3 // SPDX-License-Identifier: MIT
4 
5 #include <string.h>
6 #include "ls.h"
7 
9  SdbList *list = ls_new();
10  if (list) {
11  list->free = freefn;
12  }
13  return list;
14 }
15 
18  if (!list) {
19  return NULL;
20  }
21  return list;
22 }
23 
25  SdbListIter *it, *it2;
26  for (it = iter; it && it->data; it = it->n) {
27  for (it2 = it->n; it2 && it2->data; it2 = it2->n) {
28  if (cmp(it->data, it2->data) > 0) {
29  void *t = it->data;
30  it->data = it2->data;
31  it2->data = t;
32  }
33  }
34  }
35 }
36 
39 }
40 
42  SdbListIter *next = NULL, *result = NULL, *head = NULL;
43  while (first || second) {
44  if (!second) {
45  next = first;
46  first = first->n;
47  } else if (!first) {
48  next = second;
49  second = second->n;
50  } else if (cmp(first->data, second->data) <= 0) {
51  next = first;
52  first = first->n;
53  } else {
54  next = second;
55  second = second->n;
56  }
57  if (!head) {
58  result = next;
59  head = result;
60  head->p = NULL;
61  } else {
62  result->n = next;
63  next->p = result;
64  result = result->n;
65  }
66  }
67  head->p = NULL;
68  next->n = NULL;
69  return head;
70 }
71 
76  if (!head || !head->n) {
77  return head;
78  }
79  slow = head;
80  fast = head;
81  while (fast && fast->n && fast->n->n) {
82  fast = fast->n->n;
83  slow = slow->n;
84  }
85  tmp = slow->n;
86  slow->n = NULL;
87  return tmp;
88 }
89 
91  SdbListIter *second;
92  if (!head || !head->n) {
93  return head;
94  }
95  second = _sdb_list_split(head);
97  second = _merge_sort(second, cmp);
98  return _merge(head, second, cmp);
99 }
100 
102  if (!cmp) {
103  return false;
104  }
105  if (list && list->head && cmp) {
106  SdbListIter *iter;
107  list->head = _merge_sort(list->head, cmp);
108  // update tail reference
109  iter = list->head;
110  while (iter && iter->n) {
111  iter = iter->n;
112  }
113  list->tail = iter;
114  list->sorted = true;
115  }
116  return true;
117 }
118 
120  if (!cmp || list->cmp == cmp) {
121  return false;
122  }
123  if (list->length > 43) {
125  } else {
127  }
128  list->cmp = cmp;
129  list->sorted = true;
130  return true;
131 }
132 
134  if (!list || !iter) {
135  return;
136  }
138  if (list->free && iter->data) {
139  list->free(iter->data);
140  iter->data = NULL;
141  }
142  free(iter);
143 }
144 
145 RZ_API bool ls_delete_data(SdbList *list, void *ptr) {
146  void *kvp;
147  SdbListIter *iter;
148  ls_foreach (list, iter, kvp) {
149  if (ptr == kvp) {
150  ls_delete(list, iter);
151  return true;
152  }
153  }
154  return false;
155 }
156 
158  if (!list || !iter) {
159  return;
160  }
161  if (list->head == iter) {
162  list->head = iter->n;
163  }
164  if (list->tail == iter) {
165  list->tail = iter->p;
166  }
167  if (iter->p) {
168  iter->p->n = iter->n;
169  }
170  if (iter->n) {
171  iter->n->p = iter->p;
172  }
173  list->length--;
174 }
175 
177  SdbListIter *it;
178  if (!list) {
179  return;
180  }
181  it = list->head;
182  while (it) {
183  SdbListIter *next = it->n;
184  ls_delete(list, it);
185  it = next;
186  }
187  list->head = list->tail = NULL;
188  list->length = 0;
189 }
190 
192  if (!list) {
193  return;
194  }
195  ls_destroy(list);
196  list->free = NULL;
197  free(list);
198 }
199 
201  SdbListIter *it;
202  if (!list) {
203  return NULL;
204  }
205  it = RZ_NEW(SdbListIter);
206  if (!it) {
207  return NULL;
208  }
209  if (list->tail) {
210  list->tail->n = it;
211  }
212  it->data = data;
213  it->p = list->tail;
214  it->n = NULL;
215  list->tail = it;
216  if (!list->head) {
217  list->head = it;
218  }
219  list->length++;
220  list->sorted = false;
221  return it;
222 }
223 
226  if (!it) {
227  return NULL;
228  }
229  if (list->head) {
230  list->head->p = it;
231  }
232  it->data = data;
233  it->n = list->head;
234  it->p = NULL;
235  list->head = it;
236  if (!list->tail) {
237  list->tail = it;
238  }
239  list->length++;
240  list->sorted = false;
241  return it;
242 }
243 
245  void *data = NULL;
246  SdbListIter *iter;
247  if (list) {
248  if (list->tail) {
249  iter = list->tail;
250  if (list->head == list->tail) {
251  list->head = list->tail = NULL;
252  } else {
253  list->tail = iter->p;
254  list->tail->n = NULL;
255  }
256  data = iter->data;
257  free(iter);
258  list->length--;
259  }
260  return data;
261  }
262  return NULL;
263 }
264 
266  if (!list) {
267  return NULL;
268  }
269  SdbList *r = ls_new(); // ownership of elements stays in original list
270  if (!r) {
271  return NULL;
272  }
273  void *v;
274  SdbListIter *iter;
275  ls_foreach (list, iter, v) {
276  ls_append(r, v);
277  }
278  return r;
279 }
280 
281 RZ_API int ls_join(SdbList *list1, SdbList *list2) {
282  if (!list1 || !list2) {
283  return 0;
284  }
285  if (!(list2->length)) {
286  return 0;
287  }
288  if (!(list1->length)) {
289  list1->head = list2->head;
290  list1->tail = list2->tail;
291  } else {
292  list1->tail->n = list2->head;
293  list2->head->p = list1->tail;
294  list1->tail = list2->tail;
295  list1->tail->n = NULL;
296  }
297  list1->length += list2->length;
298  list2->head = list2->tail = NULL;
299  list1->sorted = false;
300  return 1;
301 }
302 
303 RZ_API SdbListIter *ls_insert(SdbList *list, int n, void *data) {
304  SdbListIter *it, *item;
305  int i;
306  if (list) {
307  if (!list->head || !n) {
308  return ls_prepend(list, data);
309  }
310  for (it = list->head, i = 0; it && it->data; it = it->n, i++) {
311  if (i == n) {
312  item = RZ_NEW0(SdbListIter);
313  if (!item) {
314  return NULL;
315  }
316  item->data = data;
317  item->n = it;
318  item->p = it->p;
319  if (it->p) {
320  it->p->n = item;
321  }
322  it->p = item;
323  list->length++;
324  list->sorted = false;
325  return item;
326  }
327  }
328  }
329  return ls_append(list, data);
330 }
331 
333  void *data = NULL;
334  SdbListIter *iter;
335  if (list) {
336  if (list->head) {
337  iter = list->head;
338  if (list->head == list->tail) {
339  list->head = list->tail = NULL;
340  } else {
341  list->head = iter->n;
342  list->head->p = NULL;
343  }
344  data = iter->data;
345  free(iter);
346  }
347  list->length--;
348  return data;
349  }
350  return NULL;
351 }
352 
354  SdbListIter *it;
355  int i;
356  if (!list) {
357  return false;
358  }
359  for (it = list->head, i = 0; it && it->data; it = it->n, i++)
360  if (i == n) {
361  if (!it->p && !it->n) {
362  list->head = list->tail = NULL;
363  } else if (!it->p) {
364  it->n->p = NULL;
365  list->head = it->n;
366  } else if (!it->n) {
367  it->p->n = NULL;
368  list->tail = it->p;
369  } else {
370  it->p->n = it->n;
371  it->n->p = it->p;
372  }
373  free(it);
374  list->length--;
375  return true;
376  }
377  return false;
378 }
lzma_index ** i
Definition: index.h:629
static RzILOpEffect * cmp(cs_insn *insn, bool is_thumb)
Definition: arm_il32.c:942
#define RZ_API
#define NULL
Definition: cris-opc.c:27
#define r
Definition: crypto_rc6.c:12
const char * v
Definition: dsignal.c:12
static char * slow(struct match *, char *, char *, sopno, sopno)
Definition: engine.c:806
static char * fast(struct match *, char *, char *, sopno, sopno)
Definition: engine.c:717
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
static void freefn(HtName_(Ht) *ht, HT_(Kv) *kv)
Definition: ht_inc.c:46
static void list(RzEgg *egg)
Definition: rz-gg.c:52
RZ_API void ls_split_iter(SdbList *list, SdbListIter *iter)
Definition: ls.c:157
static void ls_insertion_sort_iter(SdbListIter *iter, SdbListComparator cmp)
Definition: ls.c:24
RZ_API int ls_join(SdbList *list1, SdbList *list2)
Definition: ls.c:281
RZ_API void * ls_pop_head(SdbList *list)
Definition: ls.c:332
RZ_API SdbList * ls_new(void)
Definition: ls.c:16
RZ_API void ls_destroy(SdbList *list)
Definition: ls.c:176
RZ_API bool ls_delete_data(SdbList *list, void *ptr)
Definition: ls.c:145
RZ_API SdbListIter * ls_append(SdbList *list, void *data)
Definition: ls.c:200
RZ_API SdbListIter * ls_prepend(SdbList *list, void *data)
Definition: ls.c:224
RZ_API void * ls_pop(SdbList *list)
Definition: ls.c:244
RZ_API void ls_free(SdbList *list)
Definition: ls.c:191
RZ_API SdbListIter * ls_insert(SdbList *list, int n, void *data)
Definition: ls.c:303
static SdbListIter * _sdb_list_split(SdbListIter *head)
Definition: ls.c:72
static void ls_insertion_sort(SdbList *list, SdbListComparator cmp)
Definition: ls.c:37
RZ_API int ls_del_n(SdbList *list, int n)
Definition: ls.c:353
RZ_API bool ls_merge_sort(SdbList *list, SdbListComparator cmp)
Definition: ls.c:101
RZ_API SdbList * ls_clone(SdbList *list)
Definition: ls.c:265
RZ_API bool ls_sort(SdbList *list, SdbListComparator cmp)
Definition: ls.c:119
static SdbListIter * _merge_sort(SdbListIter *head, SdbListComparator cmp)
Definition: ls.c:90
static SdbListIter * _merge(SdbListIter *first, SdbListIter *second, SdbListComparator cmp)
Definition: ls.c:41
RZ_API void ls_delete(SdbList *list, SdbListIter *iter)
Definition: ls.c:133
RZ_API SdbList * ls_newf(SdbListFree freefn)
Definition: ls.c:8
int(* SdbListComparator)(const void *a, const void *b)
Definition: ls.h:15
void(* SdbListFree)(void *ptr)
Definition: ls.h:14
#define ls_foreach(list, it, pos)
Definition: ls.h:31
int n
Definition: mipsasm.c:19
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_NEW(x)
Definition: rz_types.h:285
Definition: ls.h:17
struct ls_iter_t * n
Definition: ls.h:19
void * data
Definition: ls.h:18
struct ls_iter_t * p
Definition: ls.h:19
Definition: ls.h:22
SdbListIter * head
Definition: ls.h:24
size_t length
Definition: ls.h:23
SdbListIter * tail
Definition: ls.h:25
bool sorted
Definition: ls.h:28