Imported upstream 3.0.8
[tools/xdelta3.git] / xdelta3-list.h
1 /* xdelta 3 - delta compression tools and library
2  * Copyright (C) 2002, 2006, 2007.  Joshua P. MacDonald
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #ifndef __XDELTA3_LIST__
20 #define __XDELTA3_LIST__
21
22 #define XD3_MAKELIST(LTYPE,ETYPE,LNAME)                                 \
23                                                                         \
24 static inline ETYPE*                                                    \
25 LTYPE ## _entry (LTYPE* l)                                              \
26 {                                                                       \
27   return (ETYPE*) ((char*) l - (ptrdiff_t) &((ETYPE*) 0)->LNAME);       \
28 }                                                                       \
29                                                                         \
30 static inline void                                                      \
31 LTYPE ## _init (LTYPE *l)                                               \
32 {                                                                       \
33   l->next = l;                                                          \
34   l->prev = l;                                                          \
35 }                                                                       \
36                                                                         \
37 static inline void                                                      \
38 LTYPE ## _add (LTYPE *prev, LTYPE *next, LTYPE *ins)                    \
39 {                                                                       \
40   next->prev = ins;                                                     \
41   prev->next = ins;                                                     \
42   ins->next  = next;                                                    \
43   ins->prev  = prev;                                                    \
44 }                                                                       \
45                                                                         \
46 static inline void                                                      \
47 LTYPE ## _push_back (LTYPE *l, ETYPE *i)                                \
48 {                                                                       \
49   LTYPE ## _add (l->prev, l, & i->LNAME);                               \
50 }                                                                       \
51                                                                         \
52 static inline void                                                      \
53 LTYPE ## _del (LTYPE *next,                                             \
54                LTYPE *prev)                                             \
55 {                                                                       \
56   next->prev = prev;                                                    \
57   prev->next = next;                                                    \
58 }                                                                       \
59                                                                         \
60 static inline ETYPE*                                                    \
61 LTYPE ## _remove (ETYPE *f)                                             \
62 {                                                                       \
63   LTYPE *i = f->LNAME.next;                                             \
64   LTYPE ## _del (f->LNAME.next, f->LNAME.prev);                         \
65   return LTYPE ## _entry (i);                                           \
66 }                                                                       \
67                                                                         \
68 static inline ETYPE*                                                    \
69 LTYPE ## _pop_back (LTYPE *l)                                           \
70 {                                                                       \
71   LTYPE *i = l->prev;                                                   \
72   LTYPE ## _del (i->next, i->prev);                                     \
73   return LTYPE ## _entry (i);                                           \
74 }                                                                       \
75                                                                         \
76 static inline ETYPE*                                                    \
77 LTYPE ## _pop_front (LTYPE *l)                                          \
78 {                                                                       \
79   LTYPE *i = l->next;                                                   \
80   LTYPE ## _del (i->next, i->prev);                                     \
81   return LTYPE ## _entry (i);                                           \
82 }                                                                       \
83                                                                         \
84 static inline int                                                       \
85 LTYPE ## _empty (LTYPE *l)                                              \
86 {                                                                       \
87   return l == l->next;                                                  \
88 }                                                                       \
89                                                                         \
90 static inline ETYPE*                                                    \
91 LTYPE ## _front (LTYPE *f)                                              \
92 {                                                                       \
93   return LTYPE ## _entry (f->next);                                     \
94 }                                                                       \
95                                                                         \
96 static inline ETYPE*                                                    \
97 LTYPE ## _back (LTYPE *f)                                               \
98 {                                                                       \
99   return LTYPE ## _entry (f->prev);                                     \
100 }                                                                       \
101                                                                         \
102 static inline int                                                       \
103 LTYPE ## _end (LTYPE *f, ETYPE *i)                                      \
104 {                                                                       \
105   return f == & i->LNAME;                                               \
106 }                                                                       \
107                                                                         \
108 static inline ETYPE*                                                    \
109 LTYPE ## _next (ETYPE *f)                                               \
110 {                                                                       \
111   return LTYPE ## _entry (f->LNAME.next);                               \
112 }                                                                       \
113                                                                         \
114 static inline usize_t                                                   \
115 LTYPE ## _length (LTYPE *l)                                             \
116 {                                                                       \
117   LTYPE *p;                                                             \
118   int c = 0;                                                            \
119                                                                         \
120   for (p = l->next; p != l; p = p->next)                                \
121     {                                                                   \
122       c += 1;                                                           \
123     }                                                                   \
124                                                                         \
125   return c;                                                             \
126 }                                                                       \
127                                                                         \
128 typedef int unused_ ## LTYPE
129
130 #endif