| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- /**
- * @file LinkedList2.h
- * @author Ambroz Bizjak <ambrop7@gmail.com>
- *
- * @section LICENSE
- *
- * This file is part of BadVPN.
- *
- * BadVPN is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2
- * as published by the Free Software Foundation.
- *
- * BadVPN 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.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * @section DESCRIPTION
- *
- * Doubly linked list that support multiple iterations and removing
- * aritrary elements during iteration.
- */
- #ifndef BADVPN_STRUCTURE_LINKEDLIST2_H
- #define BADVPN_STRUCTURE_LINKEDLIST2_H
- #include <stddef.h>
- #include <misc/debug.h>
- struct LinkedList2Iterator_t;
- /**
- * Linked list node.
- */
- typedef struct LinkedList2Node_t
- {
- struct LinkedList2Node_t *p;
- struct LinkedList2Node_t *n;
- struct LinkedList2Iterator_t *it;
- } LinkedList2Node;
- /**
- * Doubly linked list that support multiple iterations and removing
- * aritrary elements during iteration.
- */
- typedef struct
- {
- LinkedList2Node *first;
- LinkedList2Node *last;
- } LinkedList2;
- /**
- * Linked list iterator.
- */
- typedef struct LinkedList2Iterator_t
- {
- LinkedList2 *list;
- int dir;
- struct LinkedList2Node_t *e;
- struct LinkedList2Iterator_t *pi;
- struct LinkedList2Iterator_t *ni;
- } LinkedList2Iterator;
- /**
- * Initializes the linked list.
- *
- * @param list list to initialize
- */
- static void LinkedList2_Init (LinkedList2 *list);
- /**
- * Determines if the list is empty.
- *
- * @param list the list
- * @return 1 if empty, 0 if not
- */
- static int LinkedList2_IsEmpty (LinkedList2 *list);
- /**
- * Returns the first node of the list.
- *
- * @param list the list
- * @return first node of the list, or NULL if the list is empty
- */
- static LinkedList2Node * LinkedList2_GetFirst (LinkedList2 *list);
- /**
- * Returns the last node of the list.
- *
- * @param list the list
- * @return last node of the list, or NULL if the list is empty
- */
- static LinkedList2Node * LinkedList2_GetLast (LinkedList2 *list);
- /**
- * Inserts a node to the beginning of the list.
- *
- * @param list the list
- * @param node uninitialized node to insert
- */
- static void LinkedList2_Prepend (LinkedList2 *list, LinkedList2Node *node);
- /**
- * Inserts a node to the end of the list.
- *
- * @param list the list
- * @param node uninitialized node to insert
- */
- static void LinkedList2_Append (LinkedList2 *list, LinkedList2Node *node);
- /**
- * Inserts a node before a given node.
- *
- * @param list the list
- * @param node uninitialized node to insert
- * @param target node in the list to insert before
- */
- static void LinkedList2_InsertBefore (LinkedList2 *list, LinkedList2Node *node, LinkedList2Node *target);
- /**
- * Inserts a node after a given node.
- *
- * @param list the list
- * @param node uninitialized node to insert
- * @param target node in the list to insert after
- */
- static void LinkedList2_InsertAfter (LinkedList2 *list, LinkedList2Node *node, LinkedList2Node *target);
- /**
- * Removes a node from the list.
- *
- * @param list the list
- * @param node node to remove
- */
- static void LinkedList2_Remove (LinkedList2 *list, LinkedList2Node *node);
- /**
- * Returns the next node of a given node.
- *
- * @param node reference node
- * @return next node, or NULL if none
- */
- static LinkedList2Node * LinkedList2Node_Next (LinkedList2Node *node);
- /**
- * Returns the previous node of a given node.
- *
- * @param node reference node
- * @return previous node, or NULL if none
- */
- static LinkedList2Node * LinkedList2Node_Prev (LinkedList2Node *node);
- /**
- * Initializes a linked list iterator.
- * The iterator memory must remain available until either of these occurs:
- * - the list is no longer needed, or
- * - the iterator is freed with {@link LinkedList2Iterator_Free}, or
- * - the iterator reaches the end of iteration.
- *
- * @param it uninitialized iterator to initialize
- * @param list the list
- * @param dir direction of iteration. Must be 1 (forward) or -1 (backward).
- * @param node current position of the iterator. Can be a node in the list
- * or NULL for end of iteration.
- */
- static void LinkedList2Iterator_Init (LinkedList2Iterator *it, LinkedList2 *list, int dir, LinkedList2Node *node);
- /**
- * Frees a linked list iterator.
- *
- * @param it iterator to free
- */
- static void LinkedList2Iterator_Free (LinkedList2Iterator *it);
- /**
- * Initializes a linked list iterator with forward direction, with the current
- * position at the first node (or at the end of iteration if there are no nodes).
- * The iterator memory must remain available until either of these occurs:
- * - the list is no longer needed, or
- * - the iterator is freed with {@link LinkedList2Iterator_Free}, or
- * - the iterator reaches the end of iteration.
- *
- * @param it uninitialized iterator to initialize
- * @param list the list
- */
- static void LinkedList2Iterator_InitForward (LinkedList2Iterator *it, LinkedList2 *list);
- /**
- * Initializes a linked list iterator with backward direction, with the current
- * position at the last node (or at the end of iteration if there are no nodes).
- * The iterator memory must remain available until either of these occurs:
- * - the list is no longer needed, or
- * - the iterator is freed with {@link LinkedList2Iterator_Free}, or
- * - the iterator reaches the end of iteration.
- *
- * @param it uninitialized iterator to initialize
- * @param list the list
- */
- static void LinkedList2Iterator_InitBackward (LinkedList2Iterator *it, LinkedList2 *list);
- /**
- * Moves the iterator one node forward or backward (depending on its direction), or,
- * if it's at the last or first node (depending on the direction), it reaches
- * the end of iteration, or, if it's at the end of iteration, it remains there.
- * Returns the the previous position.
- *
- * @param it the iterator
- * @return node on the position of iterator before it was (possibly) moved, or NULL
- * if it was at the end of iteration
- */
- static LinkedList2Node * LinkedList2Iterator_Next (LinkedList2Iterator *it);
- void LinkedList2_Init (LinkedList2 *list)
- {
- list->first = NULL;
- list->last = NULL;
- }
- int LinkedList2_IsEmpty (LinkedList2 *list)
- {
- return (!list->first);
- }
- LinkedList2Node * LinkedList2_GetFirst (LinkedList2 *list)
- {
- return (list->first);
- }
- LinkedList2Node * LinkedList2_GetLast (LinkedList2 *list)
- {
- return (list->last);
- }
- void LinkedList2_Prepend (LinkedList2 *list, LinkedList2Node *node)
- {
- node->p = NULL;
- node->n = list->first;
- if (list->first) {
- list->first->p = node;
- } else {
- list->last = node;
- }
- list->first = node;
-
- node->it = NULL;
- }
- void LinkedList2_Append (LinkedList2 *list, LinkedList2Node *node)
- {
- node->p = list->last;
- node->n = NULL;
- if (list->last) {
- list->last->n = node;
- } else {
- list->first = node;
- }
- list->last = node;
-
- node->it = NULL;
- }
- void LinkedList2_InsertBefore (LinkedList2 *list, LinkedList2Node *node, LinkedList2Node *target)
- {
- node->p = target->p;
- node->n = target;
- if (target->p) {
- target->p->n = node;
- } else {
- list->first = node;
- }
- target->p = node;
-
- node->it = NULL;
- }
- void LinkedList2_InsertAfter (LinkedList2 *list, LinkedList2Node *node, LinkedList2Node *target)
- {
- node->p = target;
- node->n = target->n;
- if (target->n) {
- target->n->p = node;
- } else {
- list->last = node;
- }
- target->n = node;
-
- node->it = NULL;
- }
- void LinkedList2_Remove (LinkedList2 *list, LinkedList2Node *node)
- {
- // jump iterators
- while (node->it) {
- LinkedList2Iterator_Next(node->it);
- }
-
- // remove from list
- if (node->p) {
- node->p->n = node->n;
- } else {
- list->first = node->n;
- }
- if (node->n) {
- node->n->p = node->p;
- } else {
- list->last = node->p;
- }
- }
- LinkedList2Node * LinkedList2Node_Next (LinkedList2Node *node)
- {
- return node->n;
- }
- LinkedList2Node * LinkedList2Node_Prev (LinkedList2Node *node)
- {
- return node->p;
- }
- void LinkedList2Iterator_Init (LinkedList2Iterator *it, LinkedList2 *list, int dir, LinkedList2Node *node)
- {
- ASSERT(dir == 1 || dir == -1)
-
- it->list = list;
- it->dir = dir;
- it->e = node;
-
- if (it->e) {
- // link into node's iterator list
- it->pi = NULL;
- it->ni = it->e->it;
- if (it->e->it) {
- it->e->it->pi = it;
- }
- it->e->it = it;
- }
- }
- void LinkedList2Iterator_Free (LinkedList2Iterator *it)
- {
- if (it->e) {
- // remove from node's iterator list
- if (it->ni) {
- it->ni->pi = it->pi;
- }
- if (it->pi) {
- it->pi->ni = it->ni;
- } else {
- it->e->it = it->ni;
- }
- }
- }
- void LinkedList2Iterator_InitForward (LinkedList2Iterator *it, LinkedList2 *list)
- {
- LinkedList2Iterator_Init(it, list, 1, list->first);
- }
- void LinkedList2Iterator_InitBackward (LinkedList2Iterator *it, LinkedList2 *list)
- {
- LinkedList2Iterator_Init(it, list, -1, list->last);
- }
- LinkedList2Node * LinkedList2Iterator_Next (LinkedList2Iterator *it)
- {
- // remember original entry
- LinkedList2Node *orig = it->e;
-
- // jump to next entry
- if (it->e) {
- // get next entry
- LinkedList2Node *next;
- switch (it->dir) {
- case 1:
- next = it->e->n;
- break;
- case -1:
- next = it->e->p;
- break;
- default:
- ASSERT(0);
- }
- // destroy interator
- LinkedList2Iterator_Free(it);
- // re-initialize at next entry
- LinkedList2Iterator_Init(it, it->list, it->dir, next);
- }
-
- // return original entry
- return orig;
- }
- #endif
|