aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/eina/src/include/eina_inline_clist.x
blob: 66223fedb3fa1435c716ac0feb07afe998086246 (plain)
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
/*
 * Linked lists support
 *
 * Copyright (C) 2002 Alexandre Julliard
 * Copyright (C) 2011 Mike McCormack (adapted for Eina)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#ifndef __EINA_CLIST_INLINE_H__
#define __EINA_CLIST_INLINE_H__

#include <stddef.h>

static inline void eina_clist_add_after(Eina_Clist *elem, Eina_Clist *to_add)
{
   to_add->next = elem->next;
   to_add->prev = elem;
   elem->next->prev = to_add;
   elem->next = to_add;
}

static inline void eina_clist_add_before(Eina_Clist *elem, Eina_Clist *to_add)
{
   to_add->next = elem;
   to_add->prev = elem->prev;
   elem->prev->next = to_add;
   elem->prev = to_add;
}

static inline void eina_clist_add_head(Eina_Clist *list, Eina_Clist *elem)
{
   eina_clist_add_after(list, elem);
}

static inline void eina_clist_add_tail(Eina_Clist *list, Eina_Clist *elem)
{
   eina_clist_add_before(list, elem);
}

static inline void eina_clist_element_init(Eina_Clist *elem)
{
   elem->next = NULL;
   elem->prev = NULL;
}

static inline int eina_clist_element_is_linked(Eina_Clist *elem)
{
   return (elem->next != NULL && elem->prev != NULL);
}

static inline void eina_clist_remove(Eina_Clist *elem)
{
   elem->next->prev = elem->prev;
   elem->prev->next = elem->next;
   eina_clist_element_init(elem);
}

static inline Eina_Clist *eina_clist_next(const Eina_Clist *list, const Eina_Clist *elem)
{
   Eina_Clist *ret = elem->next;
   if (elem->next == list) ret = NULL;
   return ret;
}

static inline Eina_Clist *eina_clist_prev(const Eina_Clist *list, const Eina_Clist *elem)
{
   Eina_Clist *ret = elem->prev;
   if (elem->prev == list) ret = NULL;
   return ret;
}

static inline Eina_Clist *eina_clist_head(const Eina_Clist *list)
{
   return eina_clist_next(list, list);
}

static inline Eina_Clist *eina_clist_tail(const Eina_Clist *list)
{
   return eina_clist_prev(list, list);
}

static inline int eina_clist_empty(const Eina_Clist *list)
{
   return list->next == list;
}

static inline void eina_clist_init(Eina_Clist *list)
{
   list->next = list->prev = list;
}

static inline unsigned int eina_clist_count(const Eina_Clist *list)
{
   unsigned count = 0;
   const Eina_Clist *ptr;
   for (ptr = list->next; ptr != list; ptr = ptr->next) count++;
   return count;
}

static inline void eina_clist_move_tail(Eina_Clist *dst, Eina_Clist *src)
{
   if (eina_clist_empty(src)) return;

   dst->prev->next = src->next;
   src->next->prev = dst->prev;
   dst->prev = src->prev;
   src->prev->next = dst;
   eina_clist_init(src);
}

static inline void eina_clist_move_head(Eina_Clist *dst, Eina_Clist *src)
{
   if (eina_clist_empty(src)) return;

   dst->next->prev = src->prev;
   src->prev->next = dst->next;
   dst->next = src->next;
   src->next->prev = dst;
   eina_clist_init(src);
}

#endif