2019-03-03 22:41:19 +00:00
|
|
|
/* radare - LGPL - Copyright 2007-2019 - pancake, alvarofe */
|
2011-12-16 15:33:06 +00:00
|
|
|
// TODO: RRef - reference counting
|
2011-03-17 18:05:39 +00:00
|
|
|
|
2012-06-07 01:41:21 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2012-07-20 15:23:24 +00:00
|
|
|
#define _R_LIST_C_
|
|
|
|
#include "r_util.h"
|
2012-06-07 01:41:21 +00:00
|
|
|
|
2018-07-06 13:06:13 +00:00
|
|
|
inline RListIter *r_list_iter_new() {
|
2016-10-06 00:40:26 +00:00
|
|
|
return calloc (1, sizeof (RListIter));
|
2012-06-07 01:41:21 +00:00
|
|
|
}
|
|
|
|
|
2018-10-01 13:33:30 +00:00
|
|
|
R_API void r_list_iter_free(RListIter *list) {
|
2012-09-17 15:49:23 +00:00
|
|
|
/* do nothing? */
|
|
|
|
}
|
|
|
|
|
2018-10-01 13:33:30 +00:00
|
|
|
R_API RListIter *r_list_iter_get_next(RListIter *list) {
|
|
|
|
r_return_val_if_fail (list, NULL);
|
|
|
|
return list->n;
|
2012-06-07 01:41:21 +00:00
|
|
|
}
|
2014-05-02 14:54:27 +00:00
|
|
|
|
2018-10-01 13:33:30 +00:00
|
|
|
R_API void *r_list_iter_get_data(RListIter *list) {
|
|
|
|
r_return_val_if_fail (list, NULL);
|
|
|
|
return list->data;
|
2012-06-07 01:41:21 +00:00
|
|
|
}
|
|
|
|
|
2018-10-01 13:33:30 +00:00
|
|
|
R_API RListIter *r_list_iterator(const RList *list) {
|
|
|
|
r_return_val_if_fail (list, NULL);
|
|
|
|
return list->head;
|
2012-06-07 01:41:21 +00:00
|
|
|
}
|
|
|
|
|
2018-10-01 13:33:30 +00:00
|
|
|
R_API RListIter *r_list_push(RList *list, void *item) {
|
2012-06-07 01:41:21 +00:00
|
|
|
return r_list_append (list, item);
|
|
|
|
}
|
|
|
|
|
2018-10-01 13:33:30 +00:00
|
|
|
R_API RListIter *r_list_get_next(RListIter *list) {
|
|
|
|
r_return_val_if_fail (list, NULL);
|
|
|
|
return list->n;
|
2012-06-07 01:41:21 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 15:08:54 +00:00
|
|
|
// rename to head/last
|
2018-10-01 13:33:30 +00:00
|
|
|
R_API void *r_list_first(const RList *list) {
|
|
|
|
r_return_val_if_fail (list, NULL);
|
|
|
|
return list->head ? list->head->data : NULL;
|
2015-03-22 23:32:31 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 15:08:54 +00:00
|
|
|
R_API void *r_list_last(const RList *list) {
|
|
|
|
r_return_val_if_fail (list, NULL);
|
|
|
|
return list->tail ? list->tail->data : NULL;
|
|
|
|
}
|
|
|
|
|
2010-02-04 12:23:53 +00:00
|
|
|
R_API void r_list_init(RList *list) {
|
2010-01-14 11:13:48 +00:00
|
|
|
list->head = NULL;
|
|
|
|
list->tail = NULL;
|
|
|
|
list->free = NULL;
|
2016-10-05 13:59:41 +00:00
|
|
|
list->length = 0;
|
2016-10-24 23:12:06 +00:00
|
|
|
list->sorted = false;
|
2010-01-14 11:13:48 +00:00
|
|
|
}
|
|
|
|
|
2015-06-12 09:08:05 +00:00
|
|
|
R_API int r_list_length(const RList *list) {
|
2018-10-01 13:33:30 +00:00
|
|
|
r_return_val_if_fail (list, 0);
|
|
|
|
return list->length;
|
2010-05-21 15:35:05 +00:00
|
|
|
}
|
|
|
|
|
2014-05-02 14:54:27 +00:00
|
|
|
/* remove all elements of a list */
|
2016-10-05 13:59:41 +00:00
|
|
|
R_API void r_list_purge(RList *list) {
|
2018-10-01 13:33:30 +00:00
|
|
|
r_return_if_fail (list);
|
|
|
|
|
2019-08-25 20:43:34 +00:00
|
|
|
RListIter *it = list->head;
|
2018-10-01 13:33:30 +00:00
|
|
|
while (it) {
|
|
|
|
RListIter *next = it->n;
|
|
|
|
r_list_delete (list, it);
|
|
|
|
it = next;
|
2014-05-02 14:54:27 +00:00
|
|
|
}
|
2018-10-01 13:33:30 +00:00
|
|
|
list->head = list->tail = NULL;
|
2014-05-02 14:54:27 +00:00
|
|
|
}
|
|
|
|
|
2014-05-02 16:29:00 +00:00
|
|
|
/* free the list */
|
2016-10-05 13:59:41 +00:00
|
|
|
R_API void r_list_free(RList *list) {
|
2014-05-02 14:54:27 +00:00
|
|
|
if (list) {
|
|
|
|
r_list_purge (list);
|
2016-06-29 21:57:59 +00:00
|
|
|
R_FREE (list);
|
2014-05-02 14:54:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-05 13:59:41 +00:00
|
|
|
R_API bool r_list_delete_data(RList *list, void *ptr) {
|
2014-05-02 14:54:27 +00:00
|
|
|
void *p;
|
|
|
|
RListIter *iter;
|
2018-10-01 13:33:30 +00:00
|
|
|
|
|
|
|
r_return_val_if_fail (list, false);
|
|
|
|
|
2014-05-02 14:54:27 +00:00
|
|
|
r_list_foreach (list, iter, p) {
|
|
|
|
if (ptr == p) {
|
2017-11-28 15:52:33 +00:00
|
|
|
r_list_delete (list, iter);
|
2016-04-03 23:59:30 +00:00
|
|
|
return true;
|
2014-05-02 14:54:27 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-03 23:59:30 +00:00
|
|
|
return false;
|
2014-05-02 14:54:27 +00:00
|
|
|
}
|
|
|
|
|
2016-10-05 13:59:41 +00:00
|
|
|
R_API void r_list_delete(RList *list, RListIter *iter) {
|
2018-10-01 13:33:30 +00:00
|
|
|
r_return_if_fail (list && iter);
|
|
|
|
r_list_split_iter (list, iter);
|
|
|
|
if (list->free && iter->data) {
|
|
|
|
list->free (iter->data);
|
2015-04-08 21:06:25 +00:00
|
|
|
}
|
2018-10-01 13:33:30 +00:00
|
|
|
iter->data = NULL;
|
|
|
|
R_FREE (iter);
|
2014-05-02 14:54:27 +00:00
|
|
|
}
|
|
|
|
|
2016-10-05 13:59:41 +00:00
|
|
|
R_API void r_list_split(RList *list, void *ptr) {
|
2018-10-01 13:33:30 +00:00
|
|
|
r_return_if_fail (list);
|
|
|
|
|
2019-08-25 20:43:34 +00:00
|
|
|
RListIter *iter = r_list_iterator (list);
|
2018-10-01 13:33:30 +00:00
|
|
|
while (iter) {
|
|
|
|
void *item = iter->data;
|
|
|
|
if (ptr == item) {
|
|
|
|
r_list_split_iter (list, iter);
|
|
|
|
free (iter);
|
|
|
|
break;
|
2010-02-27 14:56:07 +00:00
|
|
|
}
|
2018-10-01 13:33:30 +00:00
|
|
|
iter = iter->n;
|
2010-02-27 14:56:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-05 13:59:41 +00:00
|
|
|
R_API void r_list_split_iter(RList *list, RListIter *iter) {
|
2018-10-01 13:33:30 +00:00
|
|
|
r_return_if_fail (list);
|
|
|
|
|
2016-10-05 13:59:41 +00:00
|
|
|
if (list->head == iter) {
|
|
|
|
list->head = iter->n;
|
|
|
|
}
|
|
|
|
if (list->tail == iter) {
|
|
|
|
list->tail = iter->p;
|
|
|
|
}
|
|
|
|
if (iter->p) {
|
|
|
|
iter->p->n = iter->n;
|
|
|
|
}
|
|
|
|
if (iter->n) {
|
|
|
|
iter->n->p = iter->p;
|
|
|
|
}
|
|
|
|
list->length--;
|
2011-03-17 18:05:39 +00:00
|
|
|
}
|
|
|
|
|
2011-04-24 11:46:28 +00:00
|
|
|
//Warning: free functions must be compatible
|
2016-10-05 13:59:41 +00:00
|
|
|
R_API int r_list_join(RList *list1, RList *list2) {
|
2018-10-01 13:33:30 +00:00
|
|
|
r_return_val_if_fail (list1 && list2, 0);
|
|
|
|
|
2016-11-16 14:24:08 +00:00
|
|
|
if (!(list2->length)) {
|
2015-01-21 12:33:52 +00:00
|
|
|
return 0;
|
2016-10-05 13:59:41 +00:00
|
|
|
}
|
2016-11-16 14:24:08 +00:00
|
|
|
if (!(list1->length)) {
|
2015-06-01 17:42:47 +00:00
|
|
|
list1->head = list2->head;
|
|
|
|
list1->tail = list2->tail;
|
2016-11-16 14:24:08 +00:00
|
|
|
} else {
|
2011-04-24 11:46:28 +00:00
|
|
|
list1->tail->n = list2->head;
|
|
|
|
list2->head->p = list1->tail;
|
2016-11-16 14:24:08 +00:00
|
|
|
list1->tail = list2->tail;
|
|
|
|
list1->tail->n = NULL;
|
|
|
|
list1->sorted = false;
|
2011-04-24 11:46:28 +00:00
|
|
|
}
|
2016-10-05 13:59:41 +00:00
|
|
|
list1->length += list2->length;
|
2015-01-18 01:49:19 +00:00
|
|
|
list2->head = list2->tail = NULL;
|
2015-01-21 12:33:52 +00:00
|
|
|
return 1;
|
2011-04-24 11:46:28 +00:00
|
|
|
}
|
|
|
|
|
2010-02-04 12:23:53 +00:00
|
|
|
R_API RList *r_list_new() {
|
2016-10-05 13:59:41 +00:00
|
|
|
RList *list = R_NEW0 (RList);
|
|
|
|
if (!list) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-01-14 11:13:48 +00:00
|
|
|
r_list_init (list);
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2012-12-13 17:32:26 +00:00
|
|
|
R_API RList *r_list_newf(RListFree f) {
|
2012-12-09 00:39:27 +00:00
|
|
|
RList *l = r_list_new ();
|
2016-10-05 13:59:41 +00:00
|
|
|
if (l) {
|
|
|
|
l->free = f;
|
|
|
|
}
|
2012-12-09 00:39:27 +00:00
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2016-10-05 13:59:41 +00:00
|
|
|
R_API RListIter *r_list_item_new(void *data) {
|
2016-10-06 00:40:26 +00:00
|
|
|
RListIter *item = R_NEW0 (RListIter);
|
2019-03-03 22:41:19 +00:00
|
|
|
if (item) {
|
|
|
|
item->data = data;
|
2016-10-05 13:59:41 +00:00
|
|
|
}
|
|
|
|
return item;
|
2010-01-14 11:13:48 +00:00
|
|
|
}
|
|
|
|
|
2011-03-23 19:35:40 +00:00
|
|
|
R_API RListIter *r_list_append(RList *list, void *data) {
|
2016-10-05 13:59:41 +00:00
|
|
|
RListIter *item = NULL;
|
2018-10-01 13:33:30 +00:00
|
|
|
|
|
|
|
r_return_val_if_fail (list && data, NULL);
|
|
|
|
|
|
|
|
item = R_NEW (RListIter);
|
|
|
|
if (!item) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
if (list->tail) {
|
|
|
|
list->tail->n = item;
|
|
|
|
}
|
|
|
|
item->data = data;
|
|
|
|
item->p = list->tail;
|
|
|
|
item->n = NULL;
|
|
|
|
list->tail = item;
|
|
|
|
if (!list->head) {
|
|
|
|
list->head = item;
|
2010-02-04 12:23:53 +00:00
|
|
|
}
|
2018-10-01 13:33:30 +00:00
|
|
|
list->length++;
|
|
|
|
list->sorted = false;
|
2016-10-05 13:59:41 +00:00
|
|
|
return item;
|
2010-01-14 11:13:48 +00:00
|
|
|
}
|
|
|
|
|
2011-03-23 19:35:40 +00:00
|
|
|
R_API RListIter *r_list_prepend(RList *list, void *data) {
|
2018-10-01 13:33:30 +00:00
|
|
|
r_return_val_if_fail (list, NULL);
|
|
|
|
|
2019-08-25 20:43:34 +00:00
|
|
|
RListIter *item = R_NEW0 (RListIter);
|
2018-10-01 13:33:30 +00:00
|
|
|
if (!item) {
|
|
|
|
return NULL;
|
2016-10-05 13:59:41 +00:00
|
|
|
}
|
2018-10-01 13:33:30 +00:00
|
|
|
if (list->head) {
|
|
|
|
list->head->p = item;
|
|
|
|
}
|
|
|
|
item->data = data;
|
|
|
|
item->n = list->head;
|
|
|
|
item->p = NULL;
|
|
|
|
list->head = item;
|
|
|
|
if (!list->tail) {
|
|
|
|
list->tail = item;
|
|
|
|
}
|
|
|
|
list->length++;
|
|
|
|
list->sorted = true;
|
|
|
|
return item;
|
2010-01-14 11:13:48 +00:00
|
|
|
}
|
|
|
|
|
2015-07-13 12:18:28 +00:00
|
|
|
R_API RListIter *r_list_insert(RList *list, int n, void *data) {
|
2016-10-05 13:59:41 +00:00
|
|
|
RListIter *it, *item;
|
2015-07-13 12:18:28 +00:00
|
|
|
int i;
|
2018-10-01 13:33:30 +00:00
|
|
|
|
|
|
|
r_return_val_if_fail (list, NULL);
|
|
|
|
|
|
|
|
if (!list->head || !n) {
|
|
|
|
return r_list_prepend (list, data);
|
|
|
|
}
|
|
|
|
for (it = list->head, i = 0; it && it->data; it = it->n, i++) {
|
|
|
|
if (i == n) {
|
|
|
|
item = R_NEW (RListIter);
|
|
|
|
if (!item) {
|
|
|
|
return NULL;
|
2015-07-13 12:18:28 +00:00
|
|
|
}
|
2018-10-01 13:33:30 +00:00
|
|
|
item->data = data;
|
|
|
|
item->n = it;
|
|
|
|
item->p = it->p;
|
|
|
|
if (it->p) {
|
|
|
|
it->p->n = item;
|
|
|
|
}
|
|
|
|
it->p = item;
|
|
|
|
list->length++;
|
|
|
|
list->sorted = true;
|
|
|
|
return item;
|
2015-07-13 12:18:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return r_list_append (list, data);
|
|
|
|
}
|
|
|
|
|
2011-03-10 23:09:35 +00:00
|
|
|
R_API void *r_list_pop(RList *list) {
|
|
|
|
void *data = NULL;
|
|
|
|
RListIter *iter;
|
2018-10-01 13:33:30 +00:00
|
|
|
|
|
|
|
r_return_val_if_fail (list, NULL);
|
|
|
|
|
|
|
|
if (list->tail) {
|
|
|
|
iter = list->tail;
|
|
|
|
if (list->head == list->tail) {
|
|
|
|
list->head = list->tail = NULL;
|
|
|
|
} else {
|
|
|
|
list->tail = iter->p;
|
|
|
|
list->tail->n = NULL;
|
2011-03-10 23:09:35 +00:00
|
|
|
}
|
2018-10-01 13:33:30 +00:00
|
|
|
data = iter->data;
|
|
|
|
free (iter);
|
2011-03-10 23:09:35 +00:00
|
|
|
}
|
2018-10-01 13:33:30 +00:00
|
|
|
list->length--;
|
|
|
|
return data;
|
2011-03-10 23:09:35 +00:00
|
|
|
}
|
|
|
|
|
2015-11-23 23:58:03 +00:00
|
|
|
R_API void *r_list_pop_head(RList *list) {
|
|
|
|
void *data = NULL;
|
2018-10-01 13:33:30 +00:00
|
|
|
|
|
|
|
r_return_val_if_fail (list, NULL);
|
|
|
|
|
|
|
|
if (list->head) {
|
2019-08-25 20:43:34 +00:00
|
|
|
RListIter *iter = list->head;
|
2018-10-01 13:33:30 +00:00
|
|
|
if (list->head == list->tail) {
|
|
|
|
list->head = list->tail = NULL;
|
|
|
|
} else {
|
|
|
|
list->head = iter->n;
|
|
|
|
list->head->p = NULL;
|
2015-11-23 23:58:03 +00:00
|
|
|
}
|
2018-10-01 13:33:30 +00:00
|
|
|
data = iter->data;
|
|
|
|
free (iter);
|
2015-11-23 23:58:03 +00:00
|
|
|
}
|
2018-10-01 13:33:30 +00:00
|
|
|
list->length--;
|
|
|
|
return data;
|
2015-11-23 23:58:03 +00:00
|
|
|
}
|
|
|
|
|
2011-03-12 14:04:48 +00:00
|
|
|
R_API int r_list_del_n(RList *list, int n) {
|
|
|
|
RListIter *it;
|
|
|
|
int i;
|
2018-10-01 13:33:30 +00:00
|
|
|
|
|
|
|
r_return_val_if_fail (list, false);
|
|
|
|
|
2018-09-13 08:17:26 +00:00
|
|
|
for (it = list->head, i = 0; it && it->data; it = it->n, i++) {
|
2011-05-21 19:05:21 +00:00
|
|
|
if (i == n) {
|
2016-09-19 12:44:47 +00:00
|
|
|
if (!it->p && !it->n) {
|
2011-05-21 19:05:21 +00:00
|
|
|
list->head = list->tail = NULL;
|
2016-09-19 12:44:47 +00:00
|
|
|
} else if (!it->p) {
|
2011-05-21 19:05:21 +00:00
|
|
|
it->n->p = NULL;
|
|
|
|
list->head = it->n;
|
2016-09-19 12:44:47 +00:00
|
|
|
} else if (!it->n) {
|
2011-05-21 19:05:21 +00:00
|
|
|
it->p->n = NULL;
|
|
|
|
list->tail = it->p;
|
|
|
|
} else {
|
|
|
|
it->p->n = it->n;
|
|
|
|
it->n->p = it->p;
|
2011-03-12 14:04:48 +00:00
|
|
|
}
|
2011-05-21 19:05:21 +00:00
|
|
|
free (it);
|
2016-10-05 13:59:41 +00:00
|
|
|
list->length--;
|
2016-07-12 20:15:19 +00:00
|
|
|
return true;
|
2011-05-21 19:05:21 +00:00
|
|
|
}
|
2018-09-13 08:17:26 +00:00
|
|
|
}
|
2016-07-12 20:15:19 +00:00
|
|
|
return false;
|
2011-03-12 14:04:48 +00:00
|
|
|
}
|
|
|
|
|
2015-06-12 09:08:05 +00:00
|
|
|
R_API void *r_list_get_top(const RList *list) {
|
2018-10-01 13:33:30 +00:00
|
|
|
r_return_val_if_fail (list, NULL);
|
|
|
|
|
|
|
|
return list->tail ? list->tail->data : NULL;
|
2011-03-12 14:04:48 +00:00
|
|
|
}
|
2015-11-23 23:58:03 +00:00
|
|
|
|
2015-06-12 09:08:05 +00:00
|
|
|
R_API void *r_list_get_bottom(const RList *list) {
|
2018-10-01 13:33:30 +00:00
|
|
|
r_return_val_if_fail (list, NULL);
|
|
|
|
|
|
|
|
return list->head ? list->head->data : NULL;
|
2014-08-23 05:23:55 +00:00
|
|
|
}
|
2011-03-12 14:04:48 +00:00
|
|
|
|
|
|
|
R_API void r_list_reverse(RList *list) {
|
|
|
|
RListIter *it, *tmp;
|
2018-10-01 13:33:30 +00:00
|
|
|
|
|
|
|
r_return_if_fail (list);
|
|
|
|
|
|
|
|
for (it = list->head; it && it->data; it = it->p) {
|
|
|
|
tmp = it->p;
|
|
|
|
it->p = it->n;
|
|
|
|
it->n = tmp;
|
2011-03-12 14:04:48 +00:00
|
|
|
}
|
2018-10-01 13:33:30 +00:00
|
|
|
tmp = list->head;
|
|
|
|
list->head = list->tail;
|
|
|
|
list->tail = tmp;
|
2011-03-12 14:04:48 +00:00
|
|
|
}
|
|
|
|
|
2016-10-05 13:59:41 +00:00
|
|
|
R_API RList *r_list_clone(RList *list) {
|
2011-03-12 14:04:48 +00:00
|
|
|
RList *l = NULL;
|
|
|
|
RListIter *iter;
|
|
|
|
void *data;
|
|
|
|
|
2018-10-01 13:33:30 +00:00
|
|
|
r_return_val_if_fail (list, NULL);
|
|
|
|
|
|
|
|
l = r_list_new ();
|
|
|
|
if (!l) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
l->free = NULL;
|
|
|
|
r_list_foreach (list, iter, data) {
|
|
|
|
r_list_append (l, data);
|
2011-03-12 14:04:48 +00:00
|
|
|
}
|
2018-10-01 13:33:30 +00:00
|
|
|
l->sorted = list->sorted;
|
2011-03-12 14:04:48 +00:00
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2014-05-05 10:48:57 +00:00
|
|
|
R_API RListIter *r_list_add_sorted(RList *list, void *data, RListComparator cmp) {
|
2016-10-05 13:59:41 +00:00
|
|
|
RListIter *it, *item = NULL;
|
2018-10-01 13:33:30 +00:00
|
|
|
|
|
|
|
r_return_val_if_fail (list && data && cmp, NULL);
|
|
|
|
|
|
|
|
for (it = list->head; it && it->data && cmp (data, it->data) > 0; it = it->n) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
if (it) {
|
|
|
|
item = R_NEW0 (RListIter);
|
|
|
|
if (!item) {
|
|
|
|
return NULL;
|
2018-09-13 08:17:26 +00:00
|
|
|
}
|
2018-10-01 13:33:30 +00:00
|
|
|
item->n = it;
|
|
|
|
item->p = it->p;
|
|
|
|
item->data = data;
|
|
|
|
item->n->p = item;
|
|
|
|
if (!item->p) {
|
|
|
|
list->head = item;
|
2014-05-08 15:38:29 +00:00
|
|
|
} else {
|
2018-10-01 13:33:30 +00:00
|
|
|
item->p->n = item;
|
2014-05-08 15:38:29 +00:00
|
|
|
}
|
2018-10-01 13:33:30 +00:00
|
|
|
list->length++;
|
|
|
|
} else {
|
|
|
|
r_list_append (list, data);
|
2014-05-08 15:38:29 +00:00
|
|
|
}
|
2018-10-01 13:33:30 +00:00
|
|
|
list->sorted = true;
|
|
|
|
return item;
|
2010-07-21 23:14:13 +00:00
|
|
|
}
|
|
|
|
|
2013-10-20 00:09:03 +00:00
|
|
|
R_API int r_list_set_n(RList *list, int n, void *p) {
|
|
|
|
RListIter *it;
|
|
|
|
int i;
|
2018-10-01 13:33:30 +00:00
|
|
|
|
|
|
|
r_return_val_if_fail (list, false);
|
|
|
|
for (it = list->head, i = 0; it && it->data; it = it->n, i++) {
|
|
|
|
if (i == n) {
|
|
|
|
if (list->free) {
|
|
|
|
list->free (it->data);
|
2016-10-05 13:59:41 +00:00
|
|
|
}
|
2018-10-01 13:33:30 +00:00
|
|
|
it->data = p;
|
|
|
|
list->sorted = false;
|
|
|
|
return true;
|
2013-10-20 00:09:03 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-12 20:15:19 +00:00
|
|
|
return false;
|
2013-10-20 00:09:03 +00:00
|
|
|
}
|
|
|
|
|
2015-06-12 09:08:05 +00:00
|
|
|
R_API void *r_list_get_n(const RList *list, int n) {
|
2010-07-17 16:35:47 +00:00
|
|
|
RListIter *it;
|
|
|
|
int i;
|
2018-10-01 13:33:30 +00:00
|
|
|
|
|
|
|
r_return_val_if_fail (list, NULL);
|
|
|
|
|
|
|
|
for (it = list->head, i = 0; it && it->data; it = it->n, i++) {
|
|
|
|
if (i == n) {
|
|
|
|
return it->data;
|
2016-10-05 13:59:41 +00:00
|
|
|
}
|
|
|
|
}
|
2010-07-17 16:35:47 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-10-05 13:59:41 +00:00
|
|
|
R_API RListIter *r_list_contains(const RList *list, const void *p) {
|
2011-03-23 19:35:40 +00:00
|
|
|
void *q;
|
|
|
|
RListIter *iter;
|
2018-10-01 13:33:30 +00:00
|
|
|
|
|
|
|
r_return_val_if_fail (list, NULL);
|
|
|
|
|
2011-03-23 19:35:40 +00:00
|
|
|
r_list_foreach (list, iter, q) {
|
2016-10-05 13:59:41 +00:00
|
|
|
if (p == q) {
|
2011-11-25 03:32:32 +00:00
|
|
|
return iter;
|
2016-10-05 13:59:41 +00:00
|
|
|
}
|
2011-03-23 19:35:40 +00:00
|
|
|
}
|
2011-11-25 03:32:32 +00:00
|
|
|
return NULL;
|
2011-03-23 19:35:40 +00:00
|
|
|
}
|
|
|
|
|
2016-10-05 13:59:41 +00:00
|
|
|
R_API RListIter *r_list_find(const RList *list, const void *p, RListComparator cmp) {
|
2013-11-24 00:44:00 +00:00
|
|
|
void *q;
|
|
|
|
RListIter *iter;
|
2018-10-01 13:33:30 +00:00
|
|
|
|
|
|
|
r_return_val_if_fail (list, NULL);
|
|
|
|
|
2013-11-24 00:44:00 +00:00
|
|
|
r_list_foreach (list, iter, q) {
|
2016-10-05 13:59:41 +00:00
|
|
|
if (!cmp (p, q)) {
|
2013-11-24 00:44:00 +00:00
|
|
|
return iter;
|
2016-10-05 13:59:41 +00:00
|
|
|
}
|
2013-11-24 00:44:00 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-10-05 13:59:41 +00:00
|
|
|
static RListIter *_merge(RListIter *first, RListIter *second, RListComparator cmp) {
|
2017-03-26 22:37:18 +00:00
|
|
|
RListIter *next = NULL, *result = NULL, *head = NULL;
|
|
|
|
while (first || second) {
|
|
|
|
if (!second) {
|
|
|
|
next = first;
|
|
|
|
first = first->n;
|
|
|
|
} else if (!first) {
|
|
|
|
next = second;
|
|
|
|
second = second->n;
|
|
|
|
} else if (cmp (first->data, second->data) < 0) {
|
|
|
|
next = first;
|
|
|
|
first = first->n;
|
|
|
|
} else {
|
|
|
|
next = second;
|
|
|
|
second = second->n;
|
|
|
|
}
|
|
|
|
if (!head) {
|
|
|
|
result = next;
|
|
|
|
head = result;
|
|
|
|
head->p = NULL;
|
|
|
|
} else {
|
|
|
|
result->n = next;
|
|
|
|
next->p = result;
|
|
|
|
result = result->n;
|
|
|
|
}
|
2017-03-26 22:25:32 +00:00
|
|
|
}
|
2017-03-26 22:37:18 +00:00
|
|
|
head->p = NULL;
|
|
|
|
next->n = NULL;
|
|
|
|
return head;
|
2016-10-05 13:59:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static RListIter * _r_list_half_split(RListIter *head) {
|
|
|
|
RListIter *tmp;
|
|
|
|
RListIter *fast;
|
|
|
|
RListIter *slow;
|
|
|
|
if (!head || !head->n) {
|
|
|
|
return head;
|
2016-11-16 14:24:08 +00:00
|
|
|
}
|
2016-10-05 13:59:41 +00:00
|
|
|
slow = head;
|
|
|
|
fast = head;
|
|
|
|
while (fast && fast->n && fast->n->n) {
|
|
|
|
fast = fast->n->n;
|
|
|
|
slow = slow->n;
|
|
|
|
}
|
|
|
|
tmp = slow->n;
|
|
|
|
slow->n = NULL;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2017-03-26 22:37:18 +00:00
|
|
|
static RListIter * _merge_sort(RListIter *head, RListComparator cmp) {
|
2016-10-05 13:59:41 +00:00
|
|
|
RListIter *second;
|
|
|
|
if (!head || !head->n) {
|
|
|
|
return head;
|
|
|
|
}
|
|
|
|
second = _r_list_half_split (head);
|
2017-03-26 22:37:18 +00:00
|
|
|
head = _merge_sort (head, cmp);
|
|
|
|
second = _merge_sort (second, cmp);
|
2016-10-05 13:59:41 +00:00
|
|
|
return _merge (head, second, cmp);
|
|
|
|
}
|
|
|
|
|
2016-10-05 22:34:52 +00:00
|
|
|
R_API void r_list_merge_sort(RList *list, RListComparator cmp) {
|
2018-10-01 13:33:30 +00:00
|
|
|
r_return_if_fail (list);
|
|
|
|
|
2016-10-29 11:06:11 +00:00
|
|
|
if (!list->sorted && list->head && cmp) {
|
2016-10-26 09:01:42 +00:00
|
|
|
RListIter *iter;
|
2017-03-26 22:37:18 +00:00
|
|
|
list->head = _merge_sort (list->head, cmp);
|
2016-10-26 09:01:42 +00:00
|
|
|
//update tail reference
|
|
|
|
iter = list->head;
|
|
|
|
while (iter && iter->n) {
|
|
|
|
iter = iter->n;
|
2016-10-05 13:59:41 +00:00
|
|
|
}
|
2016-10-26 09:01:42 +00:00
|
|
|
list->tail = iter;
|
2016-10-05 13:59:41 +00:00
|
|
|
}
|
2016-10-26 09:01:42 +00:00
|
|
|
list->sorted = true;
|
2016-10-05 13:59:41 +00:00
|
|
|
}
|
2016-10-05 22:34:52 +00:00
|
|
|
|
|
|
|
R_API void r_list_insertion_sort(RList *list, RListComparator cmp) {
|
2018-10-01 13:33:30 +00:00
|
|
|
r_return_if_fail (list);
|
|
|
|
|
|
|
|
if (!list->sorted) {
|
2016-10-24 23:12:06 +00:00
|
|
|
RListIter *it;
|
|
|
|
RListIter *it2;
|
2016-10-26 09:01:42 +00:00
|
|
|
if (cmp) {
|
2016-10-24 23:12:06 +00:00
|
|
|
for (it = list->head; it && it->data; it = it->n) {
|
|
|
|
for (it2 = it->n; it2 && it2->data; it2 = it2->n) {
|
|
|
|
if (cmp (it->data, it2->data) > 0) {
|
|
|
|
void *t = it->data;
|
|
|
|
it->data = it2->data;
|
|
|
|
it2->data = t;
|
|
|
|
}
|
2016-10-05 22:34:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-24 23:12:06 +00:00
|
|
|
list->sorted = true;
|
2016-10-05 22:34:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//chose wisely based on length
|
|
|
|
R_API void r_list_sort(RList *list, RListComparator cmp) {
|
2018-10-01 13:33:30 +00:00
|
|
|
r_return_if_fail (list);
|
|
|
|
if (list->length > 43) {
|
|
|
|
r_list_merge_sort (list, cmp);
|
|
|
|
} else {
|
|
|
|
r_list_insertion_sort (list, cmp);
|
2016-10-05 22:34:52 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-16 14:24:08 +00:00
|
|
|
|
2018-02-12 20:53:35 +00:00
|
|
|
R_API RList *r_list_uniq(const RList *list, RListComparator cmp) {
|
2018-08-09 15:31:12 +00:00
|
|
|
RListIter *iter, *iter2;
|
|
|
|
void *item, *item2;
|
2018-10-01 13:33:30 +00:00
|
|
|
|
|
|
|
r_return_val_if_fail (list && cmp, NULL);
|
|
|
|
|
2018-02-12 22:00:14 +00:00
|
|
|
RList *nl = r_list_newf (NULL);
|
2018-08-09 15:31:12 +00:00
|
|
|
if (!nl) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-02-12 20:53:35 +00:00
|
|
|
r_list_foreach (list, iter, item) {
|
|
|
|
bool found = false;
|
|
|
|
r_list_foreach (nl, iter2, item2) {
|
2018-08-09 15:31:12 +00:00
|
|
|
if (cmp (item, item2) == 0) {
|
2018-02-12 20:53:35 +00:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
r_list_append (nl, item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nl;
|
|
|
|
}
|
2019-06-20 11:11:37 +00:00
|
|
|
R_API char *r_list_to_str(RList *list, char ch) {
|
|
|
|
RListIter *iter;
|
|
|
|
RStrBuf *buf = r_strbuf_new ("");
|
|
|
|
if (!buf) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
char *item;
|
|
|
|
r_list_foreach (list, iter, item) {
|
|
|
|
r_strbuf_appendf (buf, "%s%c", item, ch);
|
|
|
|
}
|
|
|
|
return r_strbuf_drain (buf);
|
|
|
|
}
|
2016-10-05 13:59:41 +00:00
|
|
|
|
2010-01-14 11:13:48 +00:00
|
|
|
#if TEST
|
2010-07-21 23:14:13 +00:00
|
|
|
|
|
|
|
// TODO: move into t/list.c
|
2010-01-14 11:13:48 +00:00
|
|
|
int main () {
|
2010-02-04 12:23:53 +00:00
|
|
|
RListIter *iter, *it;
|
|
|
|
RList *l = r_list_new ();
|
2010-01-14 11:13:48 +00:00
|
|
|
|
|
|
|
r_list_append (l, "foo");
|
|
|
|
r_list_append (l, "bar");
|
|
|
|
r_list_append (l, "cow");
|
|
|
|
r_list_prepend (l, "HEAD");
|
|
|
|
r_list_prepend (l, "HEAD 00");
|
|
|
|
it = r_list_append (l, "LAST");
|
|
|
|
|
2010-09-24 14:45:56 +00:00
|
|
|
{
|
|
|
|
char *str;
|
2018-07-17 12:13:55 +00:00
|
|
|
r_list_foreach (l, iter, str) {
|
2010-09-24 14:45:56 +00:00
|
|
|
printf("-- %s\n", str);
|
|
|
|
}
|
|
|
|
printf("--**--\n");
|
2018-07-17 12:13:55 +00:00
|
|
|
r_list_foreach_prev (l, iter, str) {
|
2010-09-24 14:45:56 +00:00
|
|
|
printf("-- %s\n", str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-21 23:14:13 +00:00
|
|
|
iter = r_list_iterator (l);
|
|
|
|
while (r_list_iter_next (iter)) {
|
|
|
|
const char *str = r_list_iter_get (iter);
|
|
|
|
printf ("-> %s\n", str);
|
|
|
|
}
|
|
|
|
eprintf ("--sort--\n");
|
|
|
|
r_list_sort (l, (RListComparator)strcmp);
|
|
|
|
iter = r_list_iterator (l);
|
|
|
|
while (r_list_iter_next (iter)) {
|
|
|
|
const char *str = r_list_iter_get (iter);
|
|
|
|
printf ("-> %s\n", str);
|
|
|
|
}
|
|
|
|
|
2010-01-14 11:13:48 +00:00
|
|
|
r_list_delete (l, it);
|
|
|
|
|
2010-07-17 16:35:47 +00:00
|
|
|
char *foo = (char*) r_list_get_n (l, 2);
|
|
|
|
printf (" - n=2 => %s\n", foo);
|
2010-01-14 11:13:48 +00:00
|
|
|
iter = r_list_iterator (l);
|
|
|
|
while (r_list_iter_next (iter)) {
|
2010-02-04 12:23:53 +00:00
|
|
|
RListIter *cur = iter;
|
2010-01-14 11:13:48 +00:00
|
|
|
char *str = r_list_iter_get (iter);
|
|
|
|
if (!strcmp (str, "bar"))
|
|
|
|
r_list_delete (l, cur);
|
|
|
|
}
|
|
|
|
|
|
|
|
iter = r_list_iterator (l);
|
|
|
|
while (r_list_iter_next (iter)) {
|
|
|
|
char *str = r_list_iter_get (iter);
|
2010-01-18 12:45:02 +00:00
|
|
|
//XXX r_list_delete (l, iter);
|
2010-01-14 11:13:48 +00:00
|
|
|
printf (" - %s\n", str);
|
|
|
|
}
|
|
|
|
|
|
|
|
r_list_free (l);
|
|
|
|
|
|
|
|
/* ------------- */
|
|
|
|
l = r_list_new ();
|
2010-03-08 18:40:21 +00:00
|
|
|
l->free = free;
|
2010-01-14 11:13:48 +00:00
|
|
|
|
|
|
|
r_list_append (l, strdup ("one"));
|
|
|
|
r_list_append (l, strdup ("two"));
|
|
|
|
r_list_append (l, strdup ("tri"));
|
|
|
|
it = r_list_append (l, strdup ("LAST"));
|
|
|
|
|
|
|
|
r_list_delete (l, it);
|
|
|
|
|
|
|
|
{
|
2010-02-04 12:23:53 +00:00
|
|
|
RListIter* i = r_list_iterator (l);
|
2010-02-15 21:59:26 +00:00
|
|
|
for (; i; i = i->n) {
|
2010-01-18 12:45:02 +00:00
|
|
|
char *str = i->data;
|
2010-01-14 11:13:48 +00:00
|
|
|
printf (" * %s\n", str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-08 18:40:21 +00:00
|
|
|
r_list_free (l);
|
2010-01-14 11:13:48 +00:00
|
|
|
|
2011-03-12 14:04:48 +00:00
|
|
|
l = r_list_new ();
|
|
|
|
l->free = free;
|
|
|
|
|
|
|
|
r_list_append (l, strdup ("one"));
|
|
|
|
r_list_append (l, strdup ("two"));
|
|
|
|
r_list_append (l, strdup ("tri"));
|
|
|
|
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
r_list_foreach (l, it, str)
|
|
|
|
printf (" - %s\n", str);
|
|
|
|
|
|
|
|
RList *list;
|
|
|
|
list = r_list_clone (l);
|
|
|
|
|
|
|
|
r_list_foreach (list, it, str)
|
|
|
|
printf (" - %s\n", str);
|
|
|
|
|
|
|
|
r_list_reverse (l);
|
|
|
|
|
|
|
|
r_list_foreach (l, it, str)
|
|
|
|
printf (" * %s\n", str);
|
|
|
|
}
|
|
|
|
|
|
|
|
r_list_free (l);
|
|
|
|
|
2010-01-14 11:13:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|