+ Fixes
[opsoft.git] / silentbob / gclib / src / dlist.cxx
bloba622356da80fded9ef85ef1e0c9998fca83c6123
1 /*
2 * (c) Oleg Puchinin 2006
3 * graycardinalster@gmail.com
5 */
7 /* 2006-05-25 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <gclib/gclib.h>
14 DList::~DList ()
16 __dlist_entry_t * one;
17 __dlist_entry_t * d_del;
18 if (! head || dont_free)
19 return;
21 one = head;
22 while (one) {
23 d_del = one;
24 one = one->next;
25 free (d_del);
27 head = NULL;
28 tail = NULL;
31 char * DList::init (__dlist_entry_t * one)
33 head = one;
34 tail = one;
35 return one->data;
38 char * DList::add_head (char * one)
40 __dlist_entry_t *entry;
42 if (head == NULL)
43 return init (one);
45 entry = new_one (one);
46 head->prev = entry;
47 entry->next = head;
48 head = entry;
50 return one;
53 char * DList::add_tail (char * one)
55 __dlist_entry_t * entry;
56 if (tail == NULL)
57 return init (one);
59 entry = new_one (one);
60 tail->next = entry;
61 entry->prev = tail;
62 tail = entry;
64 return one;
67 void DList::rm (__dlist_entry_t * one)
69 if (! one)
70 return;
72 --ecount;
73 if (one == tail && one == head) {
74 free (one);
75 head = NULL;
76 tail = NULL;
77 return;
80 if (one == head) {
81 head = one->next;
82 head->prev = NULL;
83 free (one);
84 return;
87 if (one == tail) {
88 tail = one->prev;
89 tail->next = NULL;
90 free (one);
91 return;
94 one->prev->next = one->next;
95 one->next->prev = one->prev;
96 free (one);
99 void DList::detach (__dlist_entry_t * one)
101 if (! one)
102 return;
104 if (one == tail && one == head) {
105 head = NULL;
106 tail = NULL;
107 return;
110 if (one == head) {
111 head = one->next;
112 head->prev = NULL;
113 return;
116 if (one == tail) {
117 tail = one->prev;
118 tail->next = NULL;
119 return;
122 one->prev->next = one->next;
123 one->next->prev = one->prev;
127 char * DList::add_after (__dlist_entry_t *two, char * one)
129 __dlist_entry_t * entry;
131 if (! two || ! one)
132 return NULL;
134 if (two == tail)
135 return add_tail (one);
137 entry = new_one (one);
138 if (two->next) {
139 entry->prev = two;
140 entry->next = two->next;
141 two->next->prev = entry;
142 two->next = entry;
143 } else
144 return NULL;
146 return one;
149 char * DList::add_before (__dlist_entry_t *two, char * one)
151 __dlist_entry_t * entry;
153 if (! two || ! one)
154 return NULL;
156 if (two == head)
157 return add_head (one);
159 entry = new_one (one);
160 if (two->prev) {
161 entry->next = two;
162 entry->prev = two->prev;
163 two->prev->next = entry;
164 two->prev = entry;
165 } else
166 return NULL;
168 return one;
171 void DList::foreach (Dfunc_t f)
173 __dlist_entry_t * entry;
174 entry = head;
175 while (entry) {
176 f((void *) entry->data);
177 entry = entry->next;
181 char * DList::add_entry_head (__dlist_entry_t * one)
183 if (head == NULL)
184 return init (one);
185 head->prev = one;
186 one->next = head;
187 one->prev = NULL;
188 head = one;
189 return one->data;
192 char * DList::add_entry_tail (__dlist_entry_t * one)
194 if (tail == NULL)
195 return init (one);
196 tail->next = one;
197 one->prev = tail;
198 one->next = NULL;
199 tail = one;
200 return one->data;
203 __dlist_entry_t * DList::get_entry_at (int N)
205 __dlist_entry_t * one;
206 one = get_head ();
207 if (! one || ! N)
208 return NULL;
209 while ((one = one->next) && one && --N);
210 return NULL;
213 char * DList::get_at (int N)
215 __dlist_entry_t * one;
216 one = get_entry_at (N);
217 if (! one)
218 return NULL;
219 else
220 return one->data;