Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* simple list set - moecmks .*.**/
- #if !defined (LIST_C_H)
- # define LIST_C_H
- # define __NULLPTR (void *) 0
- # define list_bool int
- # define list_true 1
- # define list_false 0
- # include <malloc.h>
- # include <assert.h>
- struct list_v {
- struct list_v *level_n;
- struct list_v *level_p;
- void *obj;
- };
- struct list_ {
- struct list_v *set;
- int nums;
- };
- static
- void list_init (struct list_ **s) {
- struct list_ *u = (struct list_ *) malloc (sizeof (struct list_));
- u->nums =0;
- u->set = __NULLPTR;
- *s = u;
- }
- static
- struct list_v *list_get (struct list_ *s, int pos) {
- int id;
- struct list_v *u = __NULLPTR;
- assert (s != __NULLPTR);
- if (pos == -1)
- pos = s->nums - 1;
- if (pos <= (s->nums - 1))
- for (id =0, u = s->set; id != pos; id++)
- u =u->level_n;
- else ;
- return u;
- }
- static
- void *list_getnode (struct list_ *s, int pos) {
- struct list_v *p= list_get (s, pos);
- if (p) return p->obj;
- else return __NULLPTR;
- }
- static
- int list_insert (struct list_ *s, int pos, void *obj) {
- struct list_v *u = __NULLPTR;
- struct list_v *v;
- assert (s != __NULLPTR);
- if (pos == -1) pos = s->nums;
- if (pos > (s->nums)) return -1;
- u = (struct list_v *) malloc (sizeof (struct list_v));
- u->obj = obj;
- if (s->nums == 0) {
- u->level_n = u;
- u->level_p = u;
- s->set = u;
- s->nums++;
- return 0;
- }
- v = list_get (s, pos == s->nums ?0: pos);
- u->level_n = v;
- u->level_p = v->level_p;
- v->level_p->level_n = u;
- v->level_p = u;
- if (pos ==0)
- s->set = u;
- s->nums++;
- return 0;
- }
- static
- int list_insert_tail (struct list_ *s, void *obj) {
- struct list_v *u;
- assert (s != __NULLPTR);
- u = (struct list_v *) malloc (sizeof (struct list_v));
- u->obj = obj;
- if (s->nums == 0) {
- u->level_n = u;
- u->level_p = u;
- s->set = u;
- } else {
- u->level_n = s->set;
- u->level_p = s->set->level_p;
- s->set->level_p->level_n = u;
- s->set->level_p = u;
- }
- s->nums++;
- return 0;
- }
- static
- int list_insert_tail_L (struct list_ *s, struct list_v *u) {
- // struct list_v *u;
- assert (s != __NULLPTR);
- if (s->nums == 0) {
- u->level_n = u;
- u->level_p = u;
- s->set = u;
- } else {
- u->level_n = s->set;
- u->level_p = s->set->level_p;
- s->set->level_p->level_n = u;
- s->set->level_p = u;
- }
- s->nums++;
- return 0;
- }
- static
- int list_insert_L (struct list_ *s, int pos, struct list_v *liobj) {
- struct list_v *u = liobj;
- struct list_v *v;
- assert (s != __NULLPTR);
- if (pos == -1)
- pos = s->nums;
- if (pos > (s->nums))
- return -1;
- if (s->nums == 0) {
- u->level_n = u;
- u->level_p = u;
- s->set = u;
- s->nums++;
- return 0;
- }
- v = list_get (s, pos == s->nums ?0: pos);
- u->level_n = v;
- u->level_p = v->level_p;
- v->level_p->level_n = u;
- v->level_p = u;
- if (pos ==0)
- s->set = u;
- s->nums++;
- return 0;
- }
- static
- void list_single_free (struct list_v *sv) {
- if (sv != __NULLPTR) {
- free (sv);
- }
- }
- static
- struct list_v *list_pop (struct list_ *s, int pos) {
- struct list_v *u = __NULLPTR;
- struct list_v *v;
- assert (s != __NULLPTR);
- if (pos == -1)
- pos = s->nums - 1;
- if (pos >= (s->nums) || (s->nums <= 0))
- return __NULLPTR;
- v = list_get (s, pos);
- u = v;
- if (pos ==0)
- s->set = v->level_n;
- v->level_p->level_n = v->level_n;
- v->level_n->level_p = v->level_p;
- if ( (-- s->nums) == 0)
- s->set = __NULLPTR;
- return v;
- }
- static
- void *list_pop2 (struct list_ *s, int pos) {
- struct list_v *u = __NULLPTR;
- struct list_v *v;
- void *obj;
- assert (s != __NULLPTR);
- if (pos == -1)
- pos = s->nums - 1;
- if (pos >= (s->nums) || (s->nums <= 0))
- return __NULLPTR;
- v = list_get (s, pos);
- u = v;
- if (pos ==0)
- s->set = v->level_n;
- v->level_p->level_n = v->level_n;
- v->level_n->level_p = v->level_p;
- if ( (-- s->nums) == 0)
- s->set = __NULLPTR;
- obj = v->obj;
- free (v);
- return obj;
- }
- static
- void list_remove_ISQ (struct list_ *s, int pos, void (*pernode_)(void *node)) {
- struct list_v *u = __NULLPTR;
- struct list_v *v;
- assert (s != __NULLPTR);
- if (pos == -1)
- pos = s->nums - 1;
- if (pos >= (s->nums) || (s->nums <= 0))
- return ;
- v = list_get (s, pos);
- u = v;
- if (pos ==0)
- s->set = v->level_n;
- v->level_p->level_n = v->level_n;
- v->level_n->level_p = v->level_p;
- if (pernode_ != __NULLPTR)
- pernode_ (& v->obj);
- free (v);
- if ( (-- s->nums) == 0)
- s->set = __NULLPTR;
- }
- static
- void list_remove_ISQ_L (struct list_ *s, struct list_v *c, void (*pernode_)(void *node)) {
- struct list_v *u = __NULLPTR;
- assert (s != __NULLPTR);
- if ((s->nums <= 0))
- return ;
- if (s->set ==c)
- s->set = c->level_n;
- c->level_p->level_n = c->level_n;
- c->level_n->level_p = c->level_p;
- if (pernode_ != __NULLPTR)
- pernode_ (& c->obj);
- free (c);
- if ( (-- s->nums) == 0)
- s->set = __NULLPTR;
- }
- static
- void list_pop_L (struct list_ *s, struct list_v *c) {
- struct list_v *u = __NULLPTR;
- assert (s != __NULLPTR);
- if ((s->nums <= 0))
- return ;
- if (s->set ==c)
- s->set = c->level_n;
- c->level_p->level_n = c->level_n;
- c->level_n->level_p = c->level_p;
- if ( (-- s->nums) == 0)
- s->set = __NULLPTR;
- }
- static
- void *list_pop_L2 (struct list_ *s, struct list_v *c) {
- struct list_v *u = __NULLPTR;
- void *no;
- assert (s != __NULLPTR);
- if ((s->nums <= 0))
- return __NULLPTR;
- if (s->set ==c)
- s->set = c->level_n;
- c->level_p->level_n = c->level_n;
- c->level_n->level_p = c->level_p;
- if ( (-- s->nums) == 0)
- s->set = __NULLPTR;
- no = c->obj;
- free (c);
- return no;
- }
- static int
- list_retidx (struct list_ *s, struct list_v *litemv) {
- if (s && litemv) {
- int id;
- struct list_v *c = s->set;
- for ( id= 0; id != s->nums; id++) {
- if ( c == litemv)
- return id;
- c= c->level_n;
- }
- }
- return -1;
- }
- static int
- list_retidx2 (struct list_ *s, void *node) {
- if (s && node) {
- int id;
- struct list_v *c = s->set;
- for ( id= 0; id != s->nums; id++) {
- if ( c->obj == node)
- return id;
- c= c->level_n;
- }
- }
- return -1;
- }
- static
- void list_remove (struct list_ *s, int pos) {
- list_remove_ISQ (s, pos, __NULLPTR);
- }
- static
- void list_remove_ISQ2 (struct list_ *s, void (*pernode_)(void *base), struct list_v *base, int delnums) {
- if (s && base && (delnums > 0 || (delnums == -1))) {
- struct list_v *c = s->set;
- int pos = list_retidx (s, base);
- if (delnums == -1)
- delnums = s->nums - pos;
- if (pos != -1) {
- __retry:
- list_remove_ISQ(s, pos, pernode_);
- if (-- delnums > 0) {
- goto __retry;
- }
- }
- }
- }
- static
- void list_remove_ISQ3 (struct list_ *s, void (*pernode_)(void *node), void *node, int delnums) {
- if (s && node && (delnums > 0 || (delnums == -1))) {
- struct list_v *c = s->set;
- int pos = list_retidx2 (s, node);
- if (delnums == -1)
- delnums = s->nums - pos;
- if (pos != -1) {
- __retry:
- list_remove_ISQ(s, pos, pernode_);
- if (-- delnums > 0) {
- goto __retry;
- }
- }
- }
- }
- static
- void list_remove_ISQ3a (struct list_ *s, void (*pernode_)(void **node)) {
- if (s ) {
- while (s->nums > 0)
- list_remove_ISQ(s, 0, pernode_);
- }
- }
- static
- void list_uninit_ISQ (struct list_ **s, void (*pernode_)(void *node)) {
- if (s != __NULLPTR && (*s != __NULLPTR)) {
- while ( (*s)->nums > 0)
- list_remove_ISQ (* s, 0, pernode_);
- free (*s);
- * s = __NULLPTR;
- }
- }
- static
- void list_removeALL_ISQ (struct list_ *s, void (*pernode_)(void *node)) {
- if (s != __NULLPTR) {
- while ( s->nums > 0)
- list_remove_ISQ (s, 0, pernode_);
- s->set = __NULLPTR;
- }
- }
- static
- void list_uninit (struct list_ **s) {
- list_uninit_ISQ (s, __NULLPTR);
- }
- static
- struct list_v *list_get2 (struct list_v *base, int pos) {
- if (base && (pos >= 0)) {
- int id;
- struct list_v *u = base;
- for ( id= 0; id != pos; id++) {
- u =u->level_n;
- }
- return u;
- }
- return __NULLPTR;
- }
- static
- void *list_getnode2 (struct list_v *base, int pos) {
- if (base && (pos >= 0)) {
- int id;
- struct list_v *u = base;
- for ( id= 0; id != pos; id++)
- u =u->level_n;
- return u->obj;
- }
- return __NULLPTR;
- }
- static
- list_bool list_getnode3 (struct list_v *base, int pos, int lens, void **accepta) {
- if (base && (pos >= 0) && (lens > 0) && accepta) {
- int id;
- for ( id =0; id != lens; id++) {
- void *apts = list_getnode2 (base, pos+ id);
- if (apts != __NULLPTR) {
- accepta[id] = apts;
- } else {
- return list_false;
- }
- }
- return list_true;
- }
- return list_false;
- }
- static
- list_bool list_getlist3 (struct list_v *base, int pos, int lens, struct list_v **accepta) {
- if (base && (pos >= 0) && (lens > 0) && accepta) {
- int id;
- for ( id =0; id != lens; id++) {
- struct list_v *apts = list_get2 (base, pos+ id);
- if (apts != __NULLPTR)
- accepta[id] = apts;
- else return list_false;
- }
- return list_true;
- }
- return list_false;
- }
- /*
- not mine, from linux kernel but it looks pretty bad in this version....
- */
- #define LIST_FOREACH(/* list_ * */ head, /* int*/ id_, /* list_v */ node) \
- for ( (id_) = 0, (node) = (head)->set; \
- (id_) != (head)->nums; \
- (node) = (node)->level_n, (id_)++)
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement