| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- /**
- * @file LinkedList4.h
- * @author Ambroz Bizjak <ambrop7@gmail.com>
- *
- * @section LICENSE
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the author nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * @section DESCRIPTION
- *
- * Doubly linked list without a central object to represent the list.
- * Like {@link LinkedList3}, but without iterators.
- */
- #ifndef BADVPN_STRUCTURE_LINKEDLIST4_H
- #define BADVPN_STRUCTURE_LINKEDLIST4_H
- #include <stddef.h>
- #include <stdint.h>
- #include <misc/debug.h>
- /**
- * Linked list node.
- */
- typedef struct _LinkedList4Node {
- struct _LinkedList4Node *p;
- struct _LinkedList4Node *n;
- } LinkedList4Node;
- /**
- * Initializes a list node to form a new list consisting of a
- * single node.
- *
- * @param node list node structure to initialize. The node must remain
- * available until it is freed with {@link LinkedList4Node_Free},
- * or the list is no longer required.
- */
- static void LinkedList4Node_InitLonely (LinkedList4Node *node);
- /**
- * Initializes a list node to go after an existing node.
- *
- * @param node list node structure to initialize. The node must remain
- * available until it is freed with {@link LinkedList4Node_Free},
- * or the list is no longer required.
- * @param ref existing list node
- */
- static void LinkedList4Node_InitAfter (LinkedList4Node *node, LinkedList4Node *ref);
- /**
- * Initializes a list node to go before an existing node.
- *
- * @param node list node structure to initialize. The node must remain
- * available until it is freed with {@link LinkedList4Node_Free},
- * or the list is no longer required.
- * @param ref existing list node
- */
- static void LinkedList4Node_InitBefore (LinkedList4Node *node, LinkedList4Node *ref);
- /**
- * Frees a list node, removing it a list (if there were other nodes
- * in the list).
- *
- * @param node list node to free
- */
- static void LinkedList4Node_Free (LinkedList4Node *node);
- /**
- * Determines if a list node is a single node in a list.
- *
- * @param node list node
- * @return 1 if the node ia a single node, 0 if not
- */
- static int LinkedList4Node_IsLonely (LinkedList4Node *node);
- /**
- * Returnes the node preceding this node (if there is one),
- * the node following this node (if there is one), or NULL,
- * respectively.
- *
- * @param node list node
- * @return neighbour node or NULL if none
- */
- static LinkedList4Node * LinkedList4Node_PrevOrNext (LinkedList4Node *node);
- /**
- * Returnes the node following this node (if there is one),
- * the node preceding this node (if there is one), or NULL,
- * respectively.
- *
- * @param node list node
- * @return neighbour node or NULL if none
- */
- static LinkedList4Node * LinkedList4Node_NextOrPrev (LinkedList4Node *node);
- /**
- * Returns the node preceding this node, or NULL if there is none.
- *
- * @param node list node
- * @return left neighbour, or NULL if none
- */
- static LinkedList4Node * LinkedList4Node_Prev (LinkedList4Node *node);
- /**
- * Returns the node following this node, or NULL if there is none.
- *
- * @param node list node
- * @return right neighbour, or NULL if none
- */
- static LinkedList4Node * LinkedList4Node_Next (LinkedList4Node *node);
- /**
- * Returns the first node in the list which this node is part of.
- * It is found by iterating the list from this node to the beginning.
- *
- * @param node list node
- * @return first node in the list
- */
- static LinkedList4Node * LinkedList4Node_First (LinkedList4Node *node);
- /**
- * Returns the last node in the list which this node is part of.
- * It is found by iterating the list from this node to the end.
- *
- * @param node list node
- * @return last node in the list
- */
- static LinkedList4Node * LinkedList4Node_Last (LinkedList4Node *node);
- void LinkedList4Node_InitLonely (LinkedList4Node *node)
- {
- node->p = NULL;
- node->n = NULL;
- }
- void LinkedList4Node_InitAfter (LinkedList4Node *node, LinkedList4Node *ref)
- {
- ASSERT(ref)
-
- node->p = ref;
- node->n = ref->n;
- ref->n = node;
- if (node->n) {
- node->n->p = node;
- }
- }
- void LinkedList4Node_InitBefore (LinkedList4Node *node, LinkedList4Node *ref)
- {
- ASSERT(ref)
-
- node->n = ref;
- node->p = ref->p;
- ref->p = node;
- if (node->p) {
- node->p->n = node;
- }
- }
- void LinkedList4Node_Free (LinkedList4Node *node)
- {
- if (node->p) {
- node->p->n = node->n;
- }
- if (node->n) {
- node->n->p = node->p;
- }
- }
- int LinkedList4Node_IsLonely (LinkedList4Node *node)
- {
- return (!node->p && !node->n);
- }
- LinkedList4Node * LinkedList4Node_PrevOrNext (LinkedList4Node *node)
- {
- if (node->p) {
- return node->p;
- }
- if (node->n) {
- return node->n;
- }
- return NULL;
- }
- LinkedList4Node * LinkedList4Node_NextOrPrev (LinkedList4Node *node)
- {
- if (node->n) {
- return node->n;
- }
- if (node->p) {
- return node->p;
- }
- return NULL;
- }
- LinkedList4Node * LinkedList4Node_Prev (LinkedList4Node *node)
- {
- return node->p;
- }
- LinkedList4Node * LinkedList4Node_Next (LinkedList4Node *node)
- {
- return node->n;
- }
- LinkedList4Node * LinkedList4Node_First (LinkedList4Node *node)
- {
- while (node->p) {
- node = node->p;
- }
-
- return node;
- }
- LinkedList4Node * LinkedList4Node_Last (LinkedList4Node *node)
- {
- while (node->n) {
- node = node->n;
- }
-
- return node;
- }
- #endif
|