Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | a771a51 | 2007-02-19 01:13:53 +0000 | [diff] [blame] | 3 | * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org> |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | |
| 21 | /* Internal */ |
| 22 | PJ_IDEF(void) pj_link_node(pj_list_type *prev, pj_list_type *next) |
| 23 | { |
| 24 | ((pj_list*)prev)->next = next; |
| 25 | ((pj_list*)next)->prev = prev; |
| 26 | } |
| 27 | |
| 28 | /* |
| 29 | PJ_IDEF(void) |
| 30 | pj_list_init(pj_list_type * node) |
| 31 | { |
| 32 | ((pj_list*)node)->next = ((pj_list*)node)->prev = node; |
| 33 | } |
| 34 | |
| 35 | PJ_IDEF(int) pj_list_empty(const pj_list_type * node) |
| 36 | { |
| 37 | return ((pj_list*)node)->next == node; |
| 38 | } |
| 39 | */ |
| 40 | |
| 41 | PJ_IDEF(void) |
| 42 | pj_list_insert_after(pj_list_type *pos, pj_list_type *node) |
| 43 | { |
| 44 | ((pj_list*)node)->prev = pos; |
| 45 | ((pj_list*)node)->next = ((pj_list*)pos)->next; |
| 46 | ((pj_list*) ((pj_list*)pos)->next) ->prev = node; |
| 47 | ((pj_list*)pos)->next = node; |
| 48 | } |
| 49 | |
| 50 | |
| 51 | PJ_IDEF(void) |
| 52 | pj_list_insert_before(pj_list_type *pos, pj_list_type *node) |
| 53 | { |
| 54 | pj_list_insert_after(((pj_list*)pos)->prev, node); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | PJ_IDEF(void) |
| 59 | pj_list_insert_nodes_after(pj_list_type *pos, pj_list_type *lst) |
| 60 | { |
| 61 | pj_list *lst_last = (pj_list *) ((pj_list*)lst)->prev; |
| 62 | pj_list *pos_next = (pj_list *) ((pj_list*)pos)->next; |
| 63 | |
| 64 | pj_link_node(pos, lst); |
| 65 | pj_link_node(lst_last, pos_next); |
| 66 | } |
| 67 | |
| 68 | PJ_IDEF(void) |
| 69 | pj_list_insert_nodes_before(pj_list_type *pos, pj_list_type *lst) |
| 70 | { |
| 71 | pj_list_insert_nodes_after(((pj_list*)pos)->prev, lst); |
| 72 | } |
| 73 | |
| 74 | PJ_IDEF(void) |
| 75 | pj_list_merge_last(pj_list_type *lst1, pj_list_type *lst2) |
| 76 | { |
Benny Prijono | 8badb01 | 2006-11-22 14:47:45 +0000 | [diff] [blame] | 77 | if (!pj_list_empty(lst2)) { |
| 78 | pj_link_node(((pj_list*)lst1)->prev, ((pj_list*)lst2)->next); |
| 79 | pj_link_node(((pj_list*)lst2)->prev, lst1); |
| 80 | pj_list_init(lst2); |
| 81 | } |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | PJ_IDEF(void) |
| 85 | pj_list_merge_first(pj_list_type *lst1, pj_list_type *lst2) |
| 86 | { |
Benny Prijono | 8badb01 | 2006-11-22 14:47:45 +0000 | [diff] [blame] | 87 | if (!pj_list_empty(lst2)) { |
| 88 | pj_link_node(((pj_list*)lst2)->prev, ((pj_list*)lst1)->next); |
| 89 | pj_link_node(((pj_list*)lst1), ((pj_list*)lst2)->next); |
| 90 | pj_list_init(lst2); |
| 91 | } |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | PJ_IDEF(void) |
| 95 | pj_list_erase(pj_list_type *node) |
| 96 | { |
| 97 | pj_link_node( ((pj_list*)node)->prev, ((pj_list*)node)->next); |
| 98 | } |
| 99 | |
| 100 | |
| 101 | PJ_IDEF(pj_list_type*) |
| 102 | pj_list_find_node(pj_list_type *list, pj_list_type *node) |
| 103 | { |
| 104 | pj_list *p = (pj_list *) ((pj_list*)list)->next; |
| 105 | while (p != list && p != node) |
| 106 | p = (pj_list *) p->next; |
| 107 | |
| 108 | return p==node ? p : NULL; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | PJ_IDEF(pj_list_type*) |
| 113 | pj_list_search(pj_list_type *list, void *value, |
| 114 | int (*comp)(void *value, const pj_list_type *node)) |
| 115 | { |
| 116 | pj_list *p = (pj_list *) ((pj_list*)list)->next; |
| 117 | while (p != list && (*comp)(value, p) != 0) |
| 118 | p = (pj_list *) p->next; |
| 119 | |
| 120 | return p==list ? NULL : p; |
| 121 | } |
| 122 | |
Benny Prijono | a5f4ff2 | 2006-02-19 01:28:21 +0000 | [diff] [blame] | 123 | |
| 124 | PJ_IDEF(pj_size_t) pj_list_size(pj_list_type *list) |
| 125 | { |
| 126 | pj_list *node = (pj_list*) ((pj_list*)list)->next; |
| 127 | pj_size_t count = 0; |
| 128 | |
| 129 | while (node != list) { |
| 130 | ++count; |
| 131 | node = node->next; |
| 132 | } |
| 133 | |
| 134 | return count; |
| 135 | } |
| 136 | |