]> git.decadent.org.uk Git - ion3.git/blob - ioncore/llist.c
657a9e3e2e9be85a97e4890a65b9f887b465a487
[ion3.git] / ioncore / llist.c
1 /*
2  * ion/ioncore/llist.c
3  *
4  * Copyright (c) Tuomo Valkonen 1999-2007. 
5  *
6  * Ion is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  */
11
12 #include "common.h"
13 #include "llist.h"
14 #include "activity.h"
15
16
17 /*{{{ WMPlex numbered/mut.ex. list stuff */
18
19
20 void llist_iter_init(WLListIterTmp *tmp, WLListNode *llist)
21 {
22     *tmp=llist;
23 }
24
25
26 WLListNode *llist_iter(WLListIterTmp *tmp)
27 {
28     WLListNode *mgd=*tmp;
29     if(mgd!=NULL)
30         *tmp=mgd->next;
31     return mgd;
32 }
33
34
35 WRegion *llist_iter_regions(WLListIterTmp *tmp)
36 {
37     WLListNode *lnode=llist_iter(tmp);
38     return (lnode==NULL ? NULL : lnode->st->reg);
39 }
40
41
42 WLListNode *llist_nth_node(WLListNode *list, uint n)
43 {
44     WLListIterTmp tmp;
45     llist_iter_init(&tmp, list);
46     return (WLListNode*)iterable_nth(n, (VoidIterator*)llist_iter, &tmp);
47 }
48
49
50 void llist_link_after(WLListNode **list, 
51                       WLListNode *after, WLListNode *node)
52 {
53     if(after!=NULL){
54         LINK_ITEM_AFTER(*list, after, node, next, prev);
55     }else{
56         LINK_ITEM_FIRST(*list, node, next, prev);
57     }
58 }
59
60
61 void llist_link_last(WLListNode **list, WLListNode *node)
62 {
63     LINK_ITEM_LAST(*list, node, next, prev);
64 }
65
66
67 WLListNode *llist_index_to_after(WLListNode *list, 
68                                  WLListNode *current,
69                                  int index)
70 {
71     if(index==LLIST_INDEX_AFTER_CURRENT_ACT){
72         WLListNode *after=current;
73         while(after!=NULL){
74             WLListNode *nxt=after->next;
75             if(nxt==NULL || nxt->st==NULL || nxt->st->reg==NULL)
76                 break;
77             if(!region_is_activity_r(nxt->st->reg))
78                 break;
79             after=nxt;
80         }
81         return after;
82     }else if(index==LLIST_INDEX_AFTER_CURRENT){
83         return current;
84     }else if(index<0){
85         return (list!=NULL ? list->prev : NULL);
86     }else if(index==0){
87         return NULL;
88     }else{ /* index>0 */
89         return llist_nth_node(list, index-1);
90     }
91 }
92
93
94 void llist_unlink(WLListNode **list, WLListNode *node)
95 {
96     UNLINK_ITEM(*list, node, next, prev);
97 }
98
99
100 /*}}}*/