spot  1.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
bitvect.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2013 Laboratoire de Recherche et Développement
3 // de l'Epita (LRDE).
4 //
5 // This file is part of Spot, a model checking library.
6 //
7 // Spot is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // Spot is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 
20 #ifndef SPOT_MISC_BITVECT_HH
21 # define SPOT_MISC_BITVECT_HH
22 
23 # include "common.hh"
24 # include <cstddef>
25 # include <cstdlib>
26 # include <cassert>
27 # include <iosfwd>
28 # include <iostream>
29 namespace spot
30 {
33 
34  class SPOT_API bitvect;
35  class SPOT_API bitvect_array;
36 
40  SPOT_API bitvect* make_bitvect(size_t bitcount);
41 
45  SPOT_API bitvect_array* make_bitvect_array(size_t bitcount,
46  size_t vectcount);
47 
49  class SPOT_API bitvect
50  {
51  private:
52  // Used by make_bitvect to construct a large bitvect in place.
53  bitvect(size_t size, size_t block_count);
54  bitvect(size_t size, size_t block_count, bool);
55 
56  public:
57  typedef unsigned long block_t;
58 
59  bitvect():
60  size_(0),
61  block_count_(1),
62  storage_(&local_storage_),
63  local_storage_(0)
64  {
65  }
66 
67  bitvect(const bitvect& other):
68  size_(other.size_),
69  block_count_(1),
70  storage_(&local_storage_)
71  {
72  *this = other;
73  }
74 
75  bitvect* clone() const;
76 
77  void make_empty()
78  {
79  size_ = 0;
80  }
81 
82  bitvect& operator=(const bitvect& other)
83  {
84  reserve_blocks(other.block_count_);
85  size_ = other.size();
86  for (size_t i = 0; i < block_count_; ++i)
87  storage_[i] = other.storage_[i];
88  return *this;
89  }
90 
91  ~bitvect()
92  {
93  if (storage_ != &local_storage_)
94  free(storage_);
95  }
96 
100  void reserve_blocks(size_t new_block_count)
101  {
102  if (new_block_count < block_count_)
103  return;
104  if (storage_ == &local_storage_)
105  {
106  block_t* new_storage_ = static_cast<block_t*>
107  (malloc(new_block_count * sizeof(block_t)));
108  for (size_t i = 0; i < block_count_; ++i)
109  new_storage_[i] = storage_[i];
110  storage_ = new_storage_;
111  }
112  else
113  {
114  storage_ = static_cast<block_t*>
115  (realloc(storage_, new_block_count * sizeof(block_t)));
116  }
117  block_count_ = new_block_count;
118  }
119 
120  private:
121  void grow()
122  {
123  size_t new_block_count_ = (block_count_ + 1) * 7 / 5;
124  reserve_blocks(new_block_count_);
125  }
126 
127  public:
129  void push_back(bool val)
130  {
131  if (size() == capacity())
132  grow();
133  size_t pos = size_++;
134  if (val)
135  set(pos);
136  else
137  clear(pos);
138  }
139 
141  void push_back(block_t data, unsigned count)
142  {
143  if (size() + count > capacity())
144  grow();
145  const size_t bpb = 8 * sizeof(block_t);
146 
147  // Clear the higher bits.
148  if (count != bpb)
149  data &= (1UL << count) - 1;
150 
151  size_t n = size() % bpb;
152  size_t i = size_ / bpb;
153  size_ += count;
154  if (n == 0) // Aligned on block_t boundary
155  {
156  storage_[i] = data;
157  }
158  else // Only (bpb-n) bits free in this block.
159  {
160  // Take the lower bpb-n bits of data...
161  block_t mask = (1UL << (bpb - n)) - 1;
162  block_t data1 = (data & mask) << n;
163  mask <<= n;
164  // ... write them on the higher bpb-n bits of last block.
165  storage_[i] = (storage_[i] & ~mask) | data1;
166  // Write the remaining bits in the next block.
167  if (bpb - n < count)
168  storage_[i + 1] = data >> (bpb - n);
169  }
170  }
171 
172  size_t size() const
173  {
174  return size_;
175  }
176 
177  size_t capacity() const
178  {
179  return 8 * block_count_ * sizeof(block_t);
180  }
181 
182  size_t hash() const;
183 
184  bool get(size_t pos) const
185  {
186  assert(pos < size_);
187  const size_t bpb = 8 * sizeof(block_t);
188  return storage_[pos / bpb] & (1UL << (pos % bpb));
189  }
190 
191  void clear_all()
192  {
193  for (size_t i = 0; i < block_count_; ++i)
194  storage_[i] = 0;
195  }
196 
197  bool is_fully_clear() const
198  {
199  size_t i;
200  for (i = 0; i < block_count_ - 1; ++i)
201  if (storage_[i] != 0)
202  return false;
203  // The last block might not be fully used, compare only the
204  // relevant portion.
205  const size_t bpb = 8 * sizeof(bitvect::block_t);
206  block_t mask = (1UL << (size() % bpb)) - 1;
207  return (storage_[i] & mask) == 0;
208  }
209 
210  bool is_fully_set() const
211  {
212  size_t i;
213  for (i = 0; i < block_count_ - 1; ++i)
214  if (storage_[i] != -1UL)
215  return false;
216  // The last block might not be fully used, compare only the
217  // relevant portion.
218  const size_t bpb = 8 * sizeof(bitvect::block_t);
219  block_t mask = (1UL << (size() % bpb)) - 1;
220  return ((~storage_[i]) & mask) == 0;
221  }
222 
223  void set_all()
224  {
225  for (size_t i = 0; i < block_count_; ++i)
226  storage_[i] = -1UL;
227  }
228 
229  void flip_all()
230  {
231  for (size_t i = 0; i < block_count_; ++i)
232  storage_[i] = ~storage_[i];
233  }
234 
235  void set(size_t pos)
236  {
237  assert(pos < size_);
238  const size_t bpb = 8 * sizeof(block_t);
239  storage_[pos / bpb] |= 1UL << (pos % bpb);
240  }
241 
242  void clear(size_t pos)
243  {
244  assert(pos < size_);
245  const size_t bpb = 8 * sizeof(block_t);
246  storage_[pos / bpb] &= ~(1UL << (pos % bpb));
247  }
248 
249  void flip(size_t pos)
250  {
251  assert(pos < size_);
252  const size_t bpb = 8 * sizeof(block_t);
253  storage_[pos / bpb] ^= (1UL << (pos % bpb));
254  }
255 
256 
257  bitvect& operator|=(const bitvect& other)
258  {
259  assert(other.block_count_ <= block_count_);
260  for (size_t i = 0; i < other.block_count_; ++i)
261  storage_[i] |= other.storage_[i];
262  return *this;
263  }
264 
265  bitvect& operator&=(const bitvect& other)
266  {
267  assert(other.block_count_ <= block_count_);
268  for (size_t i = 0; i < other.block_count_; ++i)
269  storage_[i] &= other.storage_[i];
270  return *this;
271  }
272 
273  bitvect& operator^=(const bitvect& other)
274  {
275  assert(other.block_count_ <= block_count_);
276  for (size_t i = 0; i < other.block_count_; ++i)
277  storage_[i] ^= other.storage_[i];
278  return *this;
279  }
280 
281  bitvect& operator-=(const bitvect& other)
282  {
283  assert(other.block_count_ <= block_count_);
284  for (size_t i = 0; i < other.block_count_; ++i)
285  storage_[i] &= ~other.storage_[i];
286  return *this;
287  }
288 
289  bool operator==(const bitvect& other) const
290  {
291  if (other.block_count_ != block_count_)
292  return false;
293  size_t i;
294  for (i = 0; i < other.block_count_ - 1; ++i)
295  if (storage_[i] != other.storage_[i])
296  return false;
297  // The last block might not be fully used, compare only the
298  // relevant portion.
299  const size_t bpb = 8 * sizeof(bitvect::block_t);
300  block_t mask = (1UL << (size() % bpb)) - 1;
301  return (storage_[i] & mask) == (other.storage_[i] & mask);
302  }
303 
304  bool operator!=(const bitvect& other) const
305  {
306  return !(*this == other);
307  }
308 
309  bool operator<(const bitvect& other) const
310  {
311  if (block_count_ != other.block_count_)
312  return block_count_ < other.block_count_;
313  size_t i;
314  for (i = 0; i < other.block_count_ - 1; ++i)
315  if (storage_[i] > other.storage_[i])
316  return false;
317  // The last block might not be fully used, compare only the
318  // relevant portion.
319  const size_t bpb = 8 * sizeof(bitvect::block_t);
320  block_t mask = (1UL << (size() % bpb)) - 1;
321  return (storage_[i] & mask) < (other.storage_[i] & mask);
322  }
323 
324  bool operator>=(const bitvect& other) const
325  {
326  return !(*this < other);
327  }
328 
329  bool operator>(const bitvect& other) const
330  {
331  return other < *this;
332  }
333 
334  bool operator<=(const bitvect& other) const
335  {
336  return !(other < *this);
337  }
338 
339  // \brief Extract a range of bits.
340  //
341  // Build a new bit-vector using the bits from \a begin (included)
342  // to \a end (excluded).
343  bitvect* extract_range(size_t begin, size_t end)
344  {
345  assert(begin <= end);
346  assert(end <= size());
347  size_t count = end - begin;
348  bitvect* res = make_bitvect(count);
349  res->make_empty();
350 
351  if (end == begin)
352  return res;
353 
354  const size_t bpb = 8 * sizeof(bitvect::block_t);
355 
356  size_t indexb = begin / bpb;
357  unsigned bitb = begin % bpb;
358  size_t indexe = (end - 1) / bpb;
359 
360  if (indexb == indexe)
361  {
362  block_t data = storage_[indexb];
363  data >>= bitb;
364  res->push_back(data, count);
365  }
366  else
367  {
368  block_t data = storage_[indexb];
369  data >>= bitb;
370  res->push_back(data, bpb - bitb);
371  count -= bpb - bitb;
372  while (count >= bpb)
373  {
374  ++indexb;
375  res->push_back(storage_[indexb], bpb);
376  count -= bpb;
377  assert(indexb != indexe || bpb == 0);
378  }
379  if (count > 0)
380  {
381  ++indexb;
382  assert(indexb == indexe);
383  assert(count == end % bpb);
384  res->push_back(storage_[indexb], count);
385  }
386  }
387  return res;
388  }
389 
390  friend SPOT_API bitvect*
391  ::spot::make_bitvect(size_t bitcount);
392 
394  friend SPOT_API std::ostream& operator<<(std::ostream&,
395  const bitvect&);
396 
397  private:
398  friend SPOT_API bitvect_array*
399  ::spot::make_bitvect_array(size_t bitcount,
400  size_t vectcount);
401 
402  size_t size_;
403  size_t block_count_;
404  // storage_ points to local_storage_ as long as size_ <= block_count_ * 8.
405  block_t* storage_;
406  // Keep this at the end of the structure: when make_bitvect is used,
407  // it may allocate more block_t at the end of this structure.
408  block_t local_storage_;
409  };
410 
411  class SPOT_API bitvect_array
412  {
413  private:
415  bitvect_array(size_t size, size_t bvsize):
416  size_(size),
417  bvsize_(bvsize)
418  {
419  }
420 
422  SPOT_LOCAL bitvect_array(const bitvect_array&);
424  SPOT_LOCAL void operator=(const bitvect_array&);
425 
426 
427  public:
428  ~bitvect_array()
429  {
430  for (size_t i = 0; i < size_; ++i)
431  at(i).~bitvect();
432  }
433 
435  size_t size() const
436  {
437  return size_;
438  }
439 
441  bitvect& at(const size_t index)
442  {
443  assert(index < size_);
444  return *reinterpret_cast<bitvect*>(storage_ + index * bvsize_);
445  }
446 
448  const bitvect& at(const size_t index) const
449  {
450  assert(index < size_);
451  return *reinterpret_cast<const bitvect*>(storage_ + index * bvsize_);
452  }
453 
454  friend SPOT_API bitvect_array*
455  ::spot::make_bitvect_array(size_t bitcount,
456  size_t vectcount);
457 
458 
460  friend SPOT_API std::ostream& operator<<(std::ostream&,
461  const bitvect_array&);
462 
463  private:
464  size_t size_;
465  size_t bvsize_;
466  char storage_[0];
467  };
468 
470 }
471 
472 
473 #endif // SPOT_MISC_BITVECT_HH

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Wed Dec 11 2013 10:35:46 for spot by doxygen 1.8.4