/* src/toolbox/bitvector.c - bitvector implementation Copyright (C) 2005, 2006 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO Copyright (C) 2008 Theobroma Systems Ltd. This file is part of CACAO. 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, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "config.h" #include "mm/memory.hpp" #include "toolbox/bitvector.h" /****************************************************************************** Bitvector Implementation ******************************************************************************/ #ifdef BV_DEBUG_CHECK /* Number of ints needed for size bits */ # define BV_NUM_INTS(size) (((((size) + 7)/ 8) + sizeof(int) - 1)\ / sizeof(int) + 1) /* Get index in bitvector */ # define BV_INT_INDEX(bit) ( ((bit) / 8) / sizeof(int) + 1) /* Get bit index inside int */ # define BV_BIT_INDEX(bit, index) ( (bit) - (index - 1) * sizeof(int) * 8 ); #else /* Number of ints needed for size bits */ # define BV_NUM_INTS(size) (((((size) + 7)/ 8) + sizeof(int) - 1) / sizeof(int)) /* Get index in bitvector */ # define BV_INT_INDEX(bit) ( ((bit) / 8) / sizeof(int) ) /* Get bit index inside int */ # define BV_BIT_INDEX(bit, index) ( (bit) - (index) * sizeof(int) * 8 ); #endif /************************************************************************ bv_to_string Transforms the bitvector bv to a string of 1's and 0's. IN: bitvector bv bitvector created with bv_new() int size size of bitvector bv IN/OUT: char *string allocated buffer, at least size + 1 elements RETURN:pointer to string ******************************************************************************/ char *bv_to_string(bitvector bv, char *string, int size) { int i; _BV_ASSERT(bv[0] == size); for(i = 0; i < size; i++) if (bv_get_bit(bv, i)) string[i]='1'; else string[i]='0'; string[i]=0; return string; } /****************************************************************************** bv_new Creates a new bitvector and initializes all bits to 0. IN: int size size of bitvector bv RETURN: bitvector *******************************************************************************/ bitvector bv_new(int size) { int i,n; int *bv; /* Number of ints needed for size bits */ /* n = (((size+7)/8) + sizeof(int) - 1)/sizeof(int); */ n = BV_NUM_INTS(size); bv = DumpMemory_allocate(sizeof(int) * n); for(i = 0; i < n; i++) bv[i] = 0; #ifdef BV_DEBUG_CHECK bv[0] = size; #endif return bv; } /****************************************************************************** bv_get_bit Checks if a specific bit of the bitvector is set. IN: bitvector bv int bit Index of bit to check (0..size( RETURN: bool true if bit is set otherwise false *******************************************************************************/ bool bv_get_bit(bitvector bv, int bit) { int i, n; _BV_ASSERT(bit >= 0); i = BV_INT_INDEX(bit); n = BV_BIT_INDEX(bit, i); _BV_ASSERT(i < (BV_NUM_INTS(bv[0]))); return (bv[i] & (1<= 0); i = BV_INT_INDEX(bit); n = BV_BIT_INDEX(bit, i); _BV_ASSERT(i < BV_NUM_INTS(bv[0])); bv[i] |= 1<= 0); i = BV_INT_INDEX(bit); n = BV_BIT_INDEX(bit, i); _BV_ASSERT(i < BV_NUM_INTS(bv[0])); bv[i] &= ~(1<