Debian Codegraph @

src/bool-array.icc

raw
1 /* Inline Functions for bool-array.{h,cc}.
2
3 Copyright (C) 1989-1998, 2002, 2025 Free Software Foundation, Inc.
4 Written by Douglas C. Schmidt <[email protected]>
5 and Bruno Haible <[email protected]>.
6
7 This file is part of GNU GPERF.
8
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <https://www.gnu.org/licenses/>. */
21
22 // This needs:
23 //#include <stdio.h>
24 //#include <string.h>
25 //#include "options.h"
26
27 /* Initializes the bit array with room for SIZE bits, numbered from
28 0 to SIZE-1. */
29 INLINE
30 Bool_Array::Bool_Array (unsigned int size)
31 : _size (size),
32 _iteration_number (1),
33 _storage_array (new unsigned char [size])
34 {
35 memset (_storage_array, 0, size * sizeof (_storage_array[0]));
36 if (option[DEBUG])
37 fprintf (stderr, "\nbool array size = %d, total bytes = %d\n",
38 _size,
39 static_cast<unsigned int> (_size * sizeof (_storage_array[0])));
40 }
41
42 /* Sets the specified bit to true.
43 Returns its previous value (false or true). */
44 INLINE bool
45 Bool_Array::set_bit (unsigned int index)
46 {
47 if (_storage_array[index] == _iteration_number)
48 /* The bit was set since the last clear() call. */
49 return true;
50 else
51 {
52 /* The last operation on this bit was clear(). Set it now. */
53 _storage_array[index] = _iteration_number;
54 return false;
55 }
56 }
57
58 /* Resets all bits to zero. */
59 INLINE void
60 Bool_Array::clear ()
61 {
62 /* If we wrap around it's time to zero things out again! However, this only
63 occurs once about every 2^8 iterations, so it does not take much time on
64 average. */
65
66 if (++_iteration_number == 0)
67 {
68 _iteration_number = 1;
69 memset (_storage_array, 0, _size * sizeof (_storage_array[0]));
70 if (option[DEBUG])
71 {
72 fprintf (stderr, "(re-initialized bool_array)\n");
73 fflush (stderr);
74 }
75 }
76 }