]> git.decadent.org.uk Git - ion3.git/blob - ioncore/llist.c
Imported Upstream version 20090110
[ion3.git] / ioncore / llist.c
1 /*
2  * ion/ioncore/llist.c
3  *
4  * Copyright (c) Tuomo Valkonen 1999-2009. 
5  *
6  * See the included file LICENSE for details.
7  */
8
9 #include "common.h"
10 #include "llist.h"
11 #include "activity.h"
12
13
14 /*{{{ WMPlex numbered/mut.ex. list stuff */
15
16
17 void llist_iter_init(WLListIterTmp *tmp, WLListNode *llist)
18 {
19     *tmp=llist;
20 }
21
22
23 WLListNode *llist_iter(WLListIterTmp *tmp)
24 {
25     WLListNode *mgd=*tmp;
26     if(mgd!=NULL)
27         *tmp=mgd->next;
28     return mgd;
29 }
30
31
32 WRegion *llist_iter_regions(WLListIterTmp *tmp)
33 {
34     WLListNode *lnode=llist_iter(tmp);
35     return (lnode==NULL ? NULL : lnode->st->reg);
36 }
37
38
39 WLListNode *llist_nth_node(WLListNode *list, uint n)
40 {
41     WLListIterTmp tmp;
42     llist_iter_init(&tmp, list);
43     return (WLListNode*)iterable_nth(n, (VoidIterator*)llist_iter, &tmp);
44 }
45
46
47 void llist_link_after(WLListNode **list, 
48                       WLListNode *after, WLListNode *node)
49 {
50     if(after!=NULL){
51         LINK_ITEM_AFTER(*list, after, node, next, prev);
52     }else{
53         LINK_ITEM_FIRST(*list, node, next, prev);
54     }
55 }
56
57
58 void llist_link_last(WLListNode **list, WLListNode *node)
59 {
60     LINK_ITEM_LAST(*list, node, next, prev);
61 }
62
63
64 WLListNode *llist_index_to_after(WLListNode *list, 
65                                  WLListNode *current,
66                                  int index)
67 {
68     if(index==LLIST_INDEX_AFTER_CURRENT_ACT){
69         WLListNode *after=current;
70         while(after!=NULL){
71             WLListNode *nxt=after->next;
72             if(nxt==NULL || nxt->st==NULL || nxt->st->reg==NULL)
73                 break;
74             if(!region_is_activity_r(nxt->st->reg))
75                 break;
76             after=nxt;
77         }
78         return after;
79     }else if(index==LLIST_INDEX_AFTER_CURRENT){
80         return current;
81     }else if(index<0){
82         return (list!=NULL ? list->prev : NULL);
83     }else if(index==0){
84         return NULL;
85     }else{ /* index>0 */
86         return llist_nth_node(list, index-1);
87     }
88 }
89
90
91 void llist_unlink(WLListNode **list, WLListNode *node)
92 {
93     UNLINK_ITEM(*list, node, next, prev);
94 }
95
96
97 /*}}}*/