summaryrefslogtreecommitdiff
path: root/sos-code-article6.5/sos/list.h
blob: 67e72f3962ae31a32465d87a2fc7c53a2e11b39a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* Copyright (C) 2001  David Decotigny
 
   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.
    
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
    
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
   USA.
*/
#ifndef _SOS_LIST_H_
#define _SOS_LIST_H_

/**
 * @file list.h
 *
 * Circular doubly-linked lists implementation entirely based on C
 * macros
 */


/* *_named are used when next and prev links are not exactly next
   and prev. For instance when we have next_in_team, prev_in_team,
   prev_global and next_global */

#define list_init_named(list,prev,next) \
  ((list) = NULL)

#define list_singleton_named(list,item,prev,next) ({ \
  (item)->next = (item)->prev = (item); \
  (list) = (item); \
})

#define list_is_empty_named(list,prev,next) \
  ((list) == NULL)

#define list_get_head_named(list,prev,next) \
  (list)

#define list_get_tail_named(list,prev,next) \
  ((list)?((list)->prev):NULL)

/* Internal macro : insert before the head == insert at tail */
#define __list_insert_atleft_named(before_this,item,prev,next) ({ \
   (before_this)->prev->next = (item); \
   (item)->prev = (before_this)->prev; \
   (before_this)->prev = (item); \
   (item)->next = (before_this); \
})

/* @note Before_this and item are expected to be valid ! */
#define list_insert_before_named(list,before_this,item,prev,next) ({ \
   __list_insert_atleft_named(before_this,item,prev,next); \
   if ((list) == (before_this)) (list) = (item); \
})    

/** @note After_this and item are expected to be valid ! */
#define list_insert_after_named(list,after_this,item,prev,next) ({ \
   (after_this)->next->prev = (item); \
   (item)->next = (after_this)->next; \
   (after_this)->next = (item); \
   (item)->prev = (after_this); \
})

#define list_add_head_named(list,item,prev,next) ({ \
  if (list) \
    list_insert_before_named(list,list,item,prev,next); \
  else \
    list_singleton_named(list,item,prev,next); \
  (list) = (item); \
})

#define list_add_tail_named(list,item,prev,next) ({ \
  if (list) \
    __list_insert_atleft_named(list,item,prev,next); \
  else \
    list_singleton_named(list,item,prev,next); \
})

/** @note NO check whether item really is in list ! */
#define list_delete_named(list,item,prev,next) ({ \
  if ( ((item)->next == (item)) && ((item)->prev == (item)) ) \
    (item)->next = (item)->prev = (list) = NULL; \
  else { \
    (item)->prev->next = (item)->next; \
    (item)->next->prev = (item)->prev; \
    if ((item) == (list)) (list) = (item)->next; \
    (item)->prev = (item)->next = NULL; \
  } \
})

#define list_pop_head_named(list,prev,next) ({ \
  typeof(list) __ret_elt = (list); \
  list_delete_named(list,__ret_elt,prev,next); \
  __ret_elt; })

/** Loop statement that iterates through all of its elements, from
    head to tail */
#define list_foreach_forward_named(list,iterator,nb_elements,prev,next) \
        for (nb_elements=0, (iterator) = (list) ; \
             (iterator) && (!nb_elements || ((iterator) != (list))) ; \
             nb_elements++, (iterator) = (iterator)->next )

/** Loop statement that iterates through all of its elements, from
    tail back to head */
#define list_foreach_backward_named(list,iterator,nb_elements,prev,next) \
        for (nb_elements=0, (iterator) = list_get_tail_named(list,prev,next) ; \
             (iterator) && (!nb_elements || \
               ((iterator) != list_get_tail_named(list,prev,next))) ; \
             nb_elements++, (iterator) = (iterator)->prev )

#define list_foreach_named list_foreach_forward_named

/** True when we exitted early from the foreach loop (ie break) */
#define list_foreach_early_break(list,iterator,nb_elements) \
  ((list) && ( \
    ((list) != (iterator)) || \
    ( ((list) == (iterator)) && (nb_elements == 0)) ))

/** Loop statement that also removes the item at each iteration */
#define list_collapse_named(list,iterator,prev,next) \
        for ( ; ({ ((iterator) = (list)) ; \
                   if (list) list_delete_named(list,iterator,prev,next) ; \
                   (iterator); }) ; )


/*
 * the same macros : assume that the prev and next fields are really
 * named "prev" and "next"
 */

#define list_init(list) \
  list_init_named(list,prev,next)

#define list_singleton(list,item) \
  list_singleton_named(list,item,prev,next)

#define list_is_empty(list) \
  list_is_empty_named(list,prev,next)

#define list_get_head(list) \
  list_get_head_named(list,prev,next) \

#define list_get_tail(list) \
  list_get_tail_named(list,prev,next) \

/* @note Before_this and item are expected to be valid ! */
#define list_insert_after(list,after_this,item) \
  list_insert_after_named(list,after_this,item,prev,next)

/* @note After_this and item are expected to be valid ! */
#define list_insert_before(list,before_this,item) \
  list_insert_before_named(list,before_this,item,prev,next)

#define list_add_head(list,item) \
  list_add_head_named(list,item,prev,next)

#define list_add_tail(list,item) \
  list_add_tail_named(list,item,prev,next)

/* @note NO check whether item really is in list ! */
#define list_delete(list,item) \
  list_delete_named(list,item,prev,next)

#define list_pop_head(list) \
  list_pop_head_named(list,prev,next)

#define list_foreach_forward(list,iterator,nb_elements) \
  list_foreach_forward_named(list,iterator,nb_elements,prev,next)

#define list_foreach_backward(list,iterator,nb_elements) \
  list_foreach_backward_named(list,iterator,nb_elements,prev,next)

#define list_foreach list_foreach_forward

#define list_collapse(list,iterator) \
  list_collapse_named(list,iterator,prev,next)

#endif /* _SOS_LIST_H_ */