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
|
/*
* Copyright © 2018 Red Hat, Inc
*
* This program 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, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Christian J. Kellner <christian@kellner.me>
*/
#pragma once
#include <glib.h>
#include "bolt-macros.h"
G_BEGIN_DECLS
typedef struct BoltList_ BoltList;
struct BoltList_
{
BoltList *next;
BoltList *prev;
};
static inline void
bolt_list_init (BoltList *node)
{
node->next = node;
node->prev = node;
}
static inline void
bolt_list_add_internal (BoltList *prev, BoltList *next, BoltList *node)
{
node->next = next;
node->prev = prev;
next->prev = node;
prev->next = node;
}
static inline BoltList *
bolt_list_add_before (BoltList *pos, BoltList *node)
{
if (pos == NULL)
return node;
bolt_list_add_internal (pos->prev, pos, node);
return pos;
}
static inline BoltList *
bolt_list_add_after (BoltList *pos, BoltList *node)
{
if (pos == NULL)
return node;
bolt_list_add_internal (pos, pos->next, node);
return pos;
}
static inline void
bolt_list_del_internal (BoltList *prev, BoltList *next)
{
prev->next = next;
next->prev = prev;
}
#define bolt_list_entry(node, type, member) bolt_container_of (node, type, member)
/* no head list */
static inline guint
bolt_nhlist_len (BoltList *list)
{
BoltList *i = list;
guint count = 0;
if (list == NULL)
return count;
do
{
count++;
i = i->next;
}
while (i != list);
return count;
}
static inline BoltList *
bolt_nhlist_del (BoltList *list, BoltList *node)
{
if (node == NULL || list == NULL)
return list;
bolt_list_del_internal (node->prev, node->next);
if (node == list)
{
if (list->next == list)
return NULL;
else
return list->next;
}
return list;
}
/*
* Using a BoltList struct an iterator, where:
* next the *current* node
* prev the *head* of the list
*
* The following special conditions are encoded:
* sym | next | prev | state
* S | * | NULL | first iteration, initial state
* E | NULL | * | end of iteration, final state
*/
static inline BoltList *
bolt_nhlist_iter_init (BoltList *iter, BoltList *list)
{
iter->next = list;
iter->prev = NULL;
return iter;
}
#define bolt_nhlist_iter_head(iter_) ((iter_)->prev ? : (iter_)->next)
#define bolt_nhlist_iter_node(iter_) ((iter_ != NULL) ? (iter_)->next : NULL)
#define bolt_nhlist_iter_entry(iter_, type, member) \
bolt_container_of (bolt_nhlist_iter_node (iter_), type, member)
static inline BoltList *
bolt_nhlist_iter_next (BoltList *iter)
{
g_return_val_if_fail (iter != NULL, NULL);
if (iter->next == NULL) /* E */
return NULL;
if (iter->prev == NULL) /* S */
{
iter->prev = iter->next;
return iter->next;
}
iter->next = iter->next->next;
if (iter->next == iter->prev)
iter->next = NULL;
return iter->next;
}
G_END_DECLS
|