--- /dev/null
+/* PSPP - a program for statistical analysis.
+ Copyright (C) 2019 Free Software Foundation, Inc.
+
+ 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 3 of the License, 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, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+
+#include "libpspp/bit-vector.h"
+
+#include "libpspp/misc.h"
+
+#include "gl/xalloc.h"
+
+unsigned long int *
+bitvector_allocate(size_t n)
+{
+ return xcalloc (sizeof (unsigned long int), DIV_RND_UP (n, BITS_PER_ULONG));
+}
+
+size_t
+bitvector_count (const unsigned long int *vec, size_t n)
+{
+ /* XXX This can be optimized. */
+ size_t count = 0;
+ for (size_t i = 0; i < n; i++)
+ count += bitvector_is_set (vec, i);
+ return count;
+}
+
/* PSPP - a program for statistical analysis.
- Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
+ Copyright (C) 1997-9, 2000, 2019 Free Software Foundation, Inc.
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
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
-#if !bitvector_h
-#define bitvector_h 1
+#ifndef BITVECTOR_H
+#define BITVECTOR_H 1
#include <limits.h>
+#include <stdbool.h>
+#include <stddef.h>
-/* Sets bit Y starting at address X. */
-#define SET_BIT(X, Y) \
- (((unsigned char *) X)[(Y) / CHAR_BIT] |= 1 << ((Y) % CHAR_BIT))
+enum { BITS_PER_ULONG = CHAR_BIT * sizeof (unsigned long int) };
-/* Clears bit Y starting at address X. */
-#define CLEAR_BIT(X, Y) \
- (((unsigned char *) X)[(Y) / CHAR_BIT] &= ~(1 << ((Y) % CHAR_BIT)))
+unsigned long int *bitvector_allocate(size_t n);
+size_t bitvector_count (const unsigned long int *, size_t);
-/* Sets bit Y starting at address X to Z, which is zero/nonzero */
-#define SET_BIT_TO(X, Y, Z) \
- ((Z) ? SET_BIT(X, Y) : CLEAR_BIT(X, Y))
+static unsigned long int
+bitvector_mask (size_t idx)
+{
+ return 1UL << (idx % BITS_PER_ULONG);
+}
-/* Nonzero if bit Y starting at address X is set. */
-#define TEST_BIT(X, Y) \
- (((unsigned char *) X)[(Y) / CHAR_BIT] & (1 << ((Y) % CHAR_BIT)))
+static const unsigned long int *
+bitvector_unit (const unsigned long int *vec, size_t idx)
+{
+ return &vec[idx / BITS_PER_ULONG];
+}
+
+static unsigned long int *
+bitvector_unit_rw (unsigned long int *vec, size_t idx)
+{
+ return &vec[idx / BITS_PER_ULONG];
+}
+
+static inline void
+bitvector_set1 (unsigned long int *vec, size_t idx)
+{
+ *bitvector_unit_rw (vec, idx) |= bitvector_mask (idx);
+}
+
+static inline void
+bitvector_set0 (unsigned long int *vec, size_t idx)
+{
+ *bitvector_unit_rw (vec, idx) &= ~bitvector_mask (idx);
+}
+
+static inline bool
+bitvector_is_set (const unsigned long int *vec, size_t idx)
+{
+ return (*bitvector_unit (vec, idx) & bitvector_mask (idx)) != 0;
+}
/* Returns 2**X, 0 <= X < 32. */
#define BIT_INDEX(X) (1ul << (X))
/* Array of 2**(options->hash_bits) bits representing states
already visited. */
- unsigned char *hash;
+ unsigned long int *hash;
/* State queue. */
struct mc_state **queue; /* Array of pointers to states. */
if (!mc->state_error && mc->options->hash_bits > 0)
{
hash &= (1u << mc->options->hash_bits) - 1;
- if (TEST_BIT (mc->hash, hash))
+ if (bitvector_is_set (mc->hash, hash))
{
if (mc->options->verbosity > 2)
fprintf (mc->options->output_file,
next_operation (mc);
return true;
}
- SET_BIT (mc->hash, hash);
+ bitvector_set1 (mc->hash, hash);
}
return false;
}
mc->results = mc_results_create ();
mc->hash = (mc->options->hash_bits > 0
- ? xcalloc (1, DIV_RND_UP (1 << mc->options->hash_bits, CHAR_BIT))
+ ? bitvector_allocate (1 << mc->options->hash_bits)
: NULL);
mc->queue = NULL;