/*************************************************************************** * * Copyright (c) 1998-1999 Niels Möller * Copyright (c) 1999 BalaBit Computing * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program 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., 675 Mass Ave, Cambridge, MA 02139, USA. * * $Id: alist.c,v 1.2 1999/07/10 13:23:09 bazsi Exp $ * ***************************************************************************/ #include "alist.h" #include "werror.h" #include "xalloc.h" #include #define CLASS_DEFINE #include "alist.h.x" #undef CLASS_DEFINE struct alist *alist_addv(struct alist *a, unsigned n, va_list args) { unsigned i; for (i=0; i= 0); assert(atom < MAX_LINEAR_ALIST_SIZE); return self->table[atom]; } static void do_linear_set(struct alist *c, int atom, void *value) { CAST(alist_linear, self, c); assert(atom >= 0); assert(atom < MAX_LINEAR_ALIST_SIZE); self->super.size += !self->table[atom] - !value; self->table[atom] = value; } struct alist *make_linear_alist(unsigned n, ...) { int i; va_list args; NEW(alist_linear, res); res->super.size = 0; for (i = 0; itable[i] = NULL; va_start(args, n); #if 0 for (i=0; isuper.size++; res->table[atom] = value; } assert(va_arg(args, int) == -1); #endif alist_addv(&res->super, n, args); va_end(args); return &res->super; } /* NOTE: A linked alist does not have any limit on the size of its keys. */ /* CLASS: (class (name alist_linked) (super alist) (meta alist) (vars (head special "struct alist_node *" do_mark_list do_free_list)) (methods do_linked_get do_linked_set)) */ static void do_mark_list(struct alist_node *n, void (*mark)(struct ol_object *o)) { while(n) { mark(n->value); n = n->next; } } static void do_free_list(struct alist_node *n) { while(n) { struct alist_node *old = n; n = n->next; ol_space_free(old); } } static void *do_linked_get(struct alist *c, int atom) { CAST(alist_linked, self, c); struct alist_node *p; assert(atom >= 0); for (p = self->head; p; p = p->next) if (p->atom == atom) return p->value; return NULL; } static void do_linked_set(struct alist *c, int atom, void *value) { CAST(alist_linked, self, c); if (value) { struct alist_node *p; for (p = self->head; p; p = p->next) if (p->atom == atom) { p->value = value; return; } NEW_SPACE(p); p->next = self->head; p->atom = atom; p->value = value; self->head = p; self->super.size++; } else { /* Remove atom */ struct alist_node **p; for(p = &self->head; *p; ) { struct alist_node *o = *p; if (o->atom == atom) { *p = o->next; ol_space_free(o); self->super.size--; return; } p = &o->next; } /* Not found */ } } struct alist *make_linked_alist(unsigned n, ...) { va_list args; struct alist *res; NEW(alist_linked, self); res = &self->super; res->size = 0; self->head = NULL; va_start(args, n); #if 0 for (i=0; i