bool-array.icc revision 228060
158551Skris/* Inline Functions for bool-array.{h,cc}. 258551Skris 3228060Sbapt Copyright (C) 1989-1998, 2002 Free Software Foundation, Inc. 4228060Sbapt Written by Douglas C. Schmidt <schmidt@ics.uci.edu> 5228060Sbapt and Bruno Haible <bruno@clisp.org>. 658551Skris 7228060Sbapt This file is part of GNU GPERF. 858551Skris 9228060Sbapt GNU GPERF is free software; you can redistribute it and/or modify 10228060Sbapt it under the terms of the GNU General Public License as published by 11228060Sbapt the Free Software Foundation; either version 2, or (at your option) 12228060Sbapt any later version. 1358551Skris 14228060Sbapt GNU GPERF is distributed in the hope that it will be useful, 15228060Sbapt but WITHOUT ANY WARRANTY; without even the implied warranty of 16228060Sbapt MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17228060Sbapt GNU General Public License for more details. 1858551Skris 19228060Sbapt You should have received a copy of the GNU General Public License 20228060Sbapt along with this program; see the file COPYING. 21228060Sbapt If not, write to the Free Software Foundation, Inc., 22228060Sbapt 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 2358551Skris 2458551Skris// This needs: 2558551Skris//#include <stdio.h> 2658551Skris//#include <string.h> 2758551Skris//#include "options.h" 2858551Skris 29228060Sbapt/* Initializes the bit array with room for SIZE bits, numbered from 30228060Sbapt 0 to SIZE-1. */ 3158551SkrisINLINE 32228060SbaptBool_Array::Bool_Array (unsigned int size) 33228060Sbapt : _size (size), 34228060Sbapt _iteration_number (1), 35228060Sbapt _storage_array (new unsigned int [size]) 3658551Skris{ 37228060Sbapt memset (_storage_array, 0, size * sizeof (_storage_array[0])); 3858551Skris if (option[DEBUG]) 3958551Skris fprintf (stderr, "\nbool array size = %d, total bytes = %d\n", 40228060Sbapt _size, 41228060Sbapt static_cast<unsigned int> (_size * sizeof (_storage_array[0]))); 4258551Skris} 4358551Skris 44228060Sbapt/* Sets the specified bit to true. 45228060Sbapt Returns its previous value (false or true). */ 46228060SbaptINLINE bool 47228060SbaptBool_Array::set_bit (unsigned int index) 4858551Skris{ 49228060Sbapt if (_storage_array[index] == _iteration_number) 50228060Sbapt /* The bit was set since the last clear() call. */ 51228060Sbapt return true; 5258551Skris else 5358551Skris { 54228060Sbapt /* The last operation on this bit was clear(). Set it now. */ 55228060Sbapt _storage_array[index] = _iteration_number; 56228060Sbapt return false; 5758551Skris } 5858551Skris} 5958551Skris 60228060Sbapt/* Resets all bits to zero. */ 6158551SkrisINLINE void 62228060SbaptBool_Array::clear () 6358551Skris{ 6458551Skris /* If we wrap around it's time to zero things out again! However, this only 65228060Sbapt occurs once about every 2^32 iterations, so it will not happen more 66228060Sbapt frequently than once per second. */ 6758551Skris 68228060Sbapt if (++_iteration_number == 0) 6958551Skris { 70228060Sbapt _iteration_number = 1; 71228060Sbapt memset (_storage_array, 0, _size * sizeof (_storage_array[0])); 7258551Skris if (option[DEBUG]) 7358551Skris { 74228060Sbapt fprintf (stderr, "(re-initialized bool_array)\n"); 7558551Skris fflush (stderr); 7658551Skris } 7758551Skris } 7858551Skris} 79