michael@0: /****************************************************************************** michael@0: * michael@0: * Copyright (C) 2002 Jason Evans . michael@0: * All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice(s), this list of conditions and the following disclaimer michael@0: * unmodified other than the allowable addition of one or more michael@0: * copyright notices. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice(s), this list of conditions and the following disclaimer in michael@0: * the documentation and/or other materials provided with the michael@0: * distribution. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY michael@0: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE michael@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR michael@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE michael@0: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR michael@0: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF michael@0: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR michael@0: * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, michael@0: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE michael@0: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, michael@0: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: * michael@0: ******************************************************************************/ michael@0: michael@0: /* michael@0: * List definitions. michael@0: */ michael@0: #define ql_head(a_type) \ michael@0: struct { \ michael@0: a_type *qlh_first; \ michael@0: } michael@0: michael@0: #define ql_head_initializer(a_head) {NULL} michael@0: michael@0: #define ql_elm(a_type) qr(a_type) michael@0: michael@0: /* List functions. */ michael@0: #define ql_new(a_head) do { \ michael@0: (a_head)->qlh_first = NULL; \ michael@0: } while (0) michael@0: michael@0: #define ql_elm_new(a_elm, a_field) qr_new((a_elm), a_field) michael@0: michael@0: #define ql_first(a_head) ((a_head)->qlh_first) michael@0: michael@0: #define ql_last(a_head, a_field) \ michael@0: ((ql_first(a_head) != NULL) \ michael@0: ? qr_prev(ql_first(a_head), a_field) : NULL) michael@0: michael@0: #define ql_next(a_head, a_elm, a_field) \ michael@0: ((ql_last(a_head, a_field) != (a_elm)) \ michael@0: ? qr_next((a_elm), a_field) : NULL) michael@0: michael@0: #define ql_prev(a_head, a_elm, a_field) \ michael@0: ((ql_first(a_head) != (a_elm)) ? qr_prev((a_elm), a_field) \ michael@0: : NULL) michael@0: michael@0: #define ql_before_insert(a_head, a_qlelm, a_elm, a_field) do { \ michael@0: qr_before_insert((a_qlelm), (a_elm), a_field); \ michael@0: if (ql_first(a_head) == (a_qlelm)) { \ michael@0: ql_first(a_head) = (a_elm); \ michael@0: } \ michael@0: } while (0) michael@0: michael@0: #define ql_after_insert(a_qlelm, a_elm, a_field) \ michael@0: qr_after_insert((a_qlelm), (a_elm), a_field) michael@0: michael@0: #define ql_head_insert(a_head, a_elm, a_field) do { \ michael@0: if (ql_first(a_head) != NULL) { \ michael@0: qr_before_insert(ql_first(a_head), (a_elm), a_field); \ michael@0: } \ michael@0: ql_first(a_head) = (a_elm); \ michael@0: } while (0) michael@0: michael@0: #define ql_tail_insert(a_head, a_elm, a_field) do { \ michael@0: if (ql_first(a_head) != NULL) { \ michael@0: qr_before_insert(ql_first(a_head), (a_elm), a_field); \ michael@0: } \ michael@0: ql_first(a_head) = qr_next((a_elm), a_field); \ michael@0: } while (0) michael@0: michael@0: #define ql_remove(a_head, a_elm, a_field) do { \ michael@0: if (ql_first(a_head) == (a_elm)) { \ michael@0: ql_first(a_head) = qr_next(ql_first(a_head), a_field); \ michael@0: } \ michael@0: if (ql_first(a_head) != (a_elm)) { \ michael@0: qr_remove((a_elm), a_field); \ michael@0: } else { \ michael@0: ql_first(a_head) = NULL; \ michael@0: } \ michael@0: } while (0) michael@0: michael@0: #define ql_head_remove(a_head, a_type, a_field) do { \ michael@0: a_type *t = ql_first(a_head); \ michael@0: ql_remove((a_head), t, a_field); \ michael@0: } while (0) michael@0: michael@0: #define ql_tail_remove(a_head, a_type, a_field) do { \ michael@0: a_type *t = ql_last(a_head, a_field); \ michael@0: ql_remove((a_head), t, a_field); \ michael@0: } while (0) michael@0: michael@0: #define ql_foreach(a_var, a_head, a_field) \ michael@0: qr_foreach((a_var), ql_first(a_head), a_field) michael@0: michael@0: #define ql_reverse_foreach(a_var, a_head, a_field) \ michael@0: qr_reverse_foreach((a_var), ql_first(a_head), a_field)