bool-array.icc revision 230237
128257Smsmith/* Inline Functions for bool-array.{h,cc}. 228257Smsmith 328257Smsmith Copyright (C) 1989-1998, 2002 Free Software Foundation, Inc. 428257Smsmith Written by Douglas C. Schmidt <schmidt@ics.uci.edu> 528257Smsmith and Bruno Haible <bruno@clisp.org>. 628257Smsmith 728257Smsmith This file is part of GNU GPERF. 828257Smsmith 928257Smsmith GNU GPERF is free software; you can redistribute it and/or modify 1028257Smsmith it under the terms of the GNU General Public License as published by 1128257Smsmith the Free Software Foundation; either version 2, or (at your option) 1228257Smsmith any later version. 1328257Smsmith 1428257Smsmith GNU GPERF is distributed in the hope that it will be useful, 1528257Smsmith but WITHOUT ANY WARRANTY; without even the implied warranty of 1628257Smsmith MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1728257Smsmith GNU General Public License for more details. 1828257Smsmith 1928257Smsmith You should have received a copy of the GNU General Public License 2028257Smsmith along with this program; see the file COPYING. 2128257Smsmith If not, write to the Free Software Foundation, Inc., 2228257Smsmith 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 2328257Smsmith 2428257Smsmith// This needs: 2528257Smsmith//#include <stdio.h> 2628257Smsmith//#include <string.h> 2728257Smsmith//#include "options.h" 2828257Smsmith 2928257Smsmith/* Initializes the bit array with room for SIZE bits, numbered from 3028257Smsmith 0 to SIZE-1. */ 3128257SmsmithINLINE 3228257SmsmithBool_Array::Bool_Array (unsigned int size) 3328257Smsmith : _size (size), 3428257Smsmith _iteration_number (1), 3528257Smsmith _storage_array (new unsigned int [size]) 3628257Smsmith{ 3728257Smsmith memset (_storage_array, 0, size * sizeof (_storage_array[0])); 3828257Smsmith if (option[DEBUG]) 3928257Smsmith fprintf (stderr, "\nbool array size = %d, total bytes = %d\n", 4028257Smsmith _size, 4128257Smsmith static_cast<unsigned int> (_size * sizeof (_storage_array[0]))); 4228257Smsmith} 4328257Smsmith 4428257Smsmith/* Sets the specified bit to true. 4528257Smsmith Returns its previous value (false or true). */ 4628257SmsmithINLINE bool 4728257SmsmithBool_Array::set_bit (unsigned int index) 4828257Smsmith{ 4928257Smsmith if (_storage_array[index] == _iteration_number) 5028257Smsmith /* The bit was set since the last clear() call. */ 5128257Smsmith return true; 5228257Smsmith else 5328257Smsmith { 5428257Smsmith /* The last operation on this bit was clear(). Set it now. */ 5528257Smsmith _storage_array[index] = _iteration_number; 5628257Smsmith return false; 5728257Smsmith } 5828257Smsmith} 5928257Smsmith 6028257Smsmith/* Resets all bits to zero. */ 6128257SmsmithINLINE void 6228257SmsmithBool_Array::clear () 6328257Smsmith{ 6428257Smsmith /* If we wrap around it's time to zero things out again! However, this only 6528257Smsmith occurs once about every 2^32 iterations, so it will not happen more 6628257Smsmith frequently than once per second. */ 6728257Smsmith 6828257Smsmith if (++_iteration_number == 0) 6928257Smsmith { 7028257Smsmith _iteration_number = 1; 7128257Smsmith memset (_storage_array, 0, _size * sizeof (_storage_array[0])); 7228257Smsmith if (option[DEBUG]) 7328257Smsmith { 7428257Smsmith fprintf (stderr, "(re-initialized bool_array)\n"); 7528257Smsmith fflush (stderr); 7628257Smsmith } 7728257Smsmith } 7828257Smsmith} 7928257Smsmith