diff options
author | Andrea Canciani <ranma42@gmail.com> | 2013-04-02 16:41:15 +0200 |
---|---|---|
committer | Andrea Canciani <ranma42@gmail.com> | 2013-04-03 11:05:27 +0200 |
commit | d2f8ae6c25517eab769970669e0a220e2969d0cc (patch) | |
tree | 7d7ef349dcdbd4008ba8b5f9670f341f652accd9 | |
parent | 39719ea7e5aebb77fb95f2c9fde2488131e7a1e9 (diff) |
Add list implementation
Add simple implementation of doubly-linked list.
-rw-r--r-- | list.h | 128 |
1 files changed, 128 insertions, 0 deletions
@@ -0,0 +1,128 @@ +/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */ +/* + * Copyright 2013 Andrea Canciani + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * Author(s): Andrea Canciani <ranma42@gmail.com> + */ + +#ifndef SIMPLEDATA_LIST_H +#define SIMPLEDATA_LIST_H + +#include "simpleops/compiler.h" + +typedef struct _simpledata_list { + struct _simpledata_list *next; + struct _simpledata_list *prev; +} simpledata_list_t; + +static simpleops_inline void +simpledata_list_init_head (simpledata_list_t *head) +{ + head->next = head->prev = head; +} + +static simpleops_inline simpleops_bool_t +simpledata_list_is_empty (const simpledata_list_t *head) +{ + return head == head->next; +} + +static simpleops_inline void +_simpledata_list_insert_helper (simpledata_list_t *prev, + simpledata_list_t *next, + simpledata_list_t *el) +{ + next->prev = el; + el->next = next; + el->prev = prev; + prev->next = el; +} + +static simpleops_inline void +_simpledata_list_remove_helper (simpledata_list_t *prev, + simpledata_list_t *next) +{ + next->prev = prev; + prev->next = next; +} + +static simpleops_inline void +simpledata_list_remove (simpledata_list_t *el) +{ + _simpledata_list_remove_helper (el->prev, el->next); +} + +static simpleops_inline void +simpledata_list_insert (simpledata_list_t *pos, + simpledata_list_t *el) +{ + _simpledata_list_insert_helper (pos, pos->next, el); +} + +static simpleops_inline void +simpledata_list_prepend (simpledata_list_t *head, + simpledata_list_t *el) +{ + _simpledata_list_insert (head, el); +} + +static simpleops_inline void +simpledata_list_append (simpledata_list_t *head, + simpledata_list_t *el) +{ + _simpledata_list_insert_helper (head->prev, head, el); +} + +static simpleops_inline simpledata_list_t * +simpledata_list_first (simpledata_list_t *head) +{ + /* This assumes that the list is not empty */ + return head->next; +} + +static simpleops_inline simpledata_list_t * +simpledata_list_last (simpledata_list_t *head) +{ + /* This assumes that the list is not empty */ + return head->prev; +} + +static simpleops_inline simpledata_list_t * +simpledata_list_last (simpledata_list_t *head) +{ + /* This assumes that the list is not empty */ + return head->prev; +} + +static simpleops_inline void +simpledata_list_foreach (const simpledata_list_t *head + void (*f) (const simpledata_list_t *el, void *arg), + void *arg) +{ + const simpledata_list_t *el; + + for (el = head->next; el != head; el = el->next) + f (el, arg); +} + +#endif |