/* * Copyright (c) 2005 Tama Communications Corporation * * This file is part of GNU GLOBAL. * * GNU GLOBAL 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, or (at your option) * any later version. * * GNU GLOBAL 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */ #ifdef HAVE_CONFIG_H #include #endif #include #ifdef HAVE_LIMITS_H #include #endif #include "idset.h" /* Idset: usage and memory status idset->set [] 00000000 00111111 11112222 01234567 89012345 67890123 idset = idset_open(21) [00000000][00000000][000000__](3bytes) v idset_add(idset, 1) [01000000][00000000][000000__] v idset_add(idset, 2) [01100000][00000000][000000__] v idset_add(idset, 20) [01100000][00000000][000010__] idset_contains(idset, 2) == true idset_contains(idset, 3) == false close_idset(idset) [] */ /* * Allocate memory for new idset. */ IDSET * idset_open(size) unsigned int size; { IDSET *idset = malloc(sizeof(IDSET)); if (idset == NULL) die("short of memory."); idset->set = (unsigned char *)calloc((size + CHAR_BIT - 1) / CHAR_BIT, 1); if (idset->set == NULL) die("short of memory."); idset->max = 0; idset->size = size; return idset; } /* * Add id to the idset. * * i) idset idset structure * i) id id number */ void idset_add(idset, id) IDSET *idset; unsigned int id; { if (id > idset->size) die("idset_add: id is out of range."); idset->set[id / CHAR_BIT] |= 1 << (id % CHAR_BIT); if (id >= idset->max) idset->max = id + 1; } /* * Whether or not idset includes specified id. * * i) idset idset structure * i) id id number * r) true: contains, false: doesn't contain */ int idset_contains(idset, id) IDSET *idset; unsigned int id; { /* if (id > idset->size) die("idset_contains: id is out of range."); */ return (id >= idset->max) ? 0 : (idset->set[id / CHAR_BIT] & (1 << (id % CHAR_BIT))); } /* * Free memory for the idset. */ void idset_close(idset) IDSET *idset; { free(idset->set); free(idset); }