1 |
|
---|
2 | /* singularly linked-list */
|
---|
3 |
|
---|
4 | #ifndef OSCAM_LLIST_H_
|
---|
5 | #define OSCAM_LLIST_H_
|
---|
6 |
|
---|
7 | typedef struct llnode LL_NODE;
|
---|
8 | struct llnode {
|
---|
9 | void *obj;
|
---|
10 | LL_NODE *nxt;
|
---|
11 | };
|
---|
12 |
|
---|
13 | typedef struct llist LLIST;
|
---|
14 | struct llist {
|
---|
15 | // void *obj;
|
---|
16 | LL_NODE *initial;
|
---|
17 | LL_NODE *last;
|
---|
18 | int32_t count;
|
---|
19 | CS_MUTEX_LOCK lock;
|
---|
20 | int32_t flag;
|
---|
21 | uint32_t version; // updated on every modification of the list - exception is on prepends and appends as this should not have impacts on iterations!
|
---|
22 | };
|
---|
23 |
|
---|
24 | typedef struct lliter LL_ITER;
|
---|
25 | struct lliter {
|
---|
26 | LLIST *l;
|
---|
27 | LL_NODE *cur, *prv;
|
---|
28 | uint32_t ll_version;
|
---|
29 | };
|
---|
30 |
|
---|
31 | typedef struct llistlockiter LL_LOCKITER;
|
---|
32 | struct llistlockiter {
|
---|
33 | LLIST *l;
|
---|
34 | int32_t writelock;
|
---|
35 | LL_ITER it;
|
---|
36 | };
|
---|
37 |
|
---|
38 | LLIST *ll_create(const char *name); // create llist, return ptr to llist
|
---|
39 | void ll_destroy(LLIST *l); // same as ll_clear_abstract() but frees up LLIST mem as well
|
---|
40 | void ll_destroy_data(LLIST *l); // same as ll_clear_data() but frees up obj allocations as well
|
---|
41 | void ll_clear(LLIST *l); // frees up all llnodes nodes but not data held in obj ptrs
|
---|
42 | void ll_clear_data(LLIST *l); // same as ll_clear_data() but frees up obj allocations as well
|
---|
43 |
|
---|
44 | #define ll_destroy_NULL(X) {if (X) {LLIST *tmpX=X; X=NULL;ll_destroy(tmpX);}}
|
---|
45 | #define ll_destroy_data_NULL(X) {if (X) {LLIST *tmpX=X; X=NULL;ll_destroy_data(tmpX);}}
|
---|
46 |
|
---|
47 | void **ll_sort(const LLIST *l, void *compare, int32_t *size); // sorts the list, compare = int func(const T *a, const T *b)
|
---|
48 | LL_NODE *ll_append(LLIST *l, void *obj); // append obj to llist
|
---|
49 | LL_NODE *ll_prepend(LLIST *l, void *obj); // prepend obj to llist
|
---|
50 |
|
---|
51 | //clones a list, duplicates data
|
---|
52 | LLIST *ll_clone(LLIST *l, uint32_t copysize);
|
---|
53 |
|
---|
54 | //New type of lock, list is locked during iterate! create=lock, destroy=unlock
|
---|
55 | LL_LOCKITER *ll_li_create(LLIST *l, int32_t writelock);
|
---|
56 | void ll_li_destroy(LL_LOCKITER *li);
|
---|
57 | void *ll_li_next(LL_LOCKITER *li);
|
---|
58 |
|
---|
59 | //Old Iterators:
|
---|
60 | LL_ITER ll_iter_create(LLIST *l); // return ptr to iterator obj
|
---|
61 | void *ll_iter_next(LL_ITER *it); // iterate to and return next llnode obj, returns NULL at end
|
---|
62 | void *ll_iter_next_remove(LL_ITER *it); // iterate to and return next llnode obj, returns NULL at end, removing it
|
---|
63 | void *ll_iter_peek(const LL_ITER *it, int32_t offset); // return obj at offset from iterator but do not iterate
|
---|
64 | void ll_iter_reset(LL_ITER *it); // reset itrerator to first llnode
|
---|
65 | void ll_iter_insert(LL_ITER *it, void *obj); // insert obj at iterator node
|
---|
66 | void *ll_iter_remove(LL_ITER *it); // remove llnode at iterator, returns ptr to the llnode obj removed
|
---|
67 | void ll_iter_remove_data(LL_ITER *it); // remove llnode and free llnode obj
|
---|
68 | void *ll_iter_move(LL_ITER *it, int32_t offset);// moves the iterator position
|
---|
69 | int32_t ll_iter_move_first(LL_ITER *it); // moves an entry to top
|
---|
70 | static inline int32_t ll_count(const LLIST *l) // return number of items in list
|
---|
71 | {
|
---|
72 | if (!l || l->flag)
|
---|
73 | return 0;
|
---|
74 |
|
---|
75 | return l->count;
|
---|
76 | }
|
---|
77 | void *ll_has_elements(const LLIST *l); // returns first obj if has one
|
---|
78 | void *ll_last_element(const LLIST *l);
|
---|
79 | int32_t ll_contains(const LLIST *l, const void *obj);
|
---|
80 | const void *ll_contains_data(const LLIST *l, const void *obj, uint32_t size);
|
---|
81 | int32_t ll_remove(LLIST *l, const void *obj);
|
---|
82 | void ll_remove_data(LLIST *l, void *obj);
|
---|
83 | int32_t ll_remove_all(LLIST *l, const LLIST *elements_to_remove); // removes all elements from l where elements are in elements_to_remove
|
---|
84 |
|
---|
85 | void ll_putall(LLIST *dest, LLIST *src);
|
---|
86 |
|
---|
87 | void *ll_remove_first(LLIST *l);
|
---|
88 | void ll_remove_first_data(LLIST *l);
|
---|
89 |
|
---|
90 | #endif
|
---|