Zero  0.1.0
w_bitvector.h
Go to the documentation of this file.
1 /* -*- mode:C++; c-basic-offset:4 -*-
2  Shore-MT -- Multi-threaded port of the SHORE storage manager
3 
4  Copyright (c) 2007-2009
5  Data Intensive Applications and Systems Labaratory (DIAS)
6  Ecole Polytechnique Federale de Lausanne
7 
8  All Rights Reserved.
9 
10  Permission to use, copy, modify and distribute this software and
11  its documentation is hereby granted, provided that both the
12  copyright notice and this permission notice appear in all copies of
13  the software, derivative works or modified versions, and any
14  portions thereof, and that both notices appear in supporting
15  documentation.
16 
17  This code is distributed in the hope that it will be useful, but
18  WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS
20  DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER
21  RESULTING FROM THE USE OF THIS SOFTWARE.
22 */
23 
24 /*<std-header orig-src='shore' incl-file-exclusion='W_BASE_H'>
25 
26  $Id: w_bitvector.h,v 1.2 2010/05/26 01:20:23 nhall Exp $
27 
28 SHORE -- Scalable Heterogeneous Object REpository
29 
30 Copyright (c) 1994-99 Computer Sciences Department, University of
31  Wisconsin -- Madison
32 All Rights Reserved.
33 
34 Permission to use, copy, modify and distribute this software and its
35 documentation is hereby granted, provided that both the copyright
36 notice and this permission notice appear in all copies of the
37 software, derivative works or modified versions, and any portions
38 thereof, and that both notices appear in supporting documentation.
39 
40 THE AUTHORS AND THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY
41 OF WISCONSIN - MADISON ALLOW FREE USE OF THIS SOFTWARE IN ITS
42 "AS IS" CONDITION, AND THEY DISCLAIM ANY LIABILITY OF ANY KIND
43 FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
44 
45 This software was developed with support by the Advanced Research
46 Project Agency, ARPA order number 018 (formerly 8230), monitored by
47 the U.S. Army Research Laboratory under contract DAAB07-91-C-Q518.
48 Further funding for this work was provided by DARPA through
49 Rome Research Laboratory Contract No. F30602-97-2-0247.
50 
51 */
52 
53 #ifndef __W_BITVECTOR_H
54 #define __W_BITVECTOR_H
55 
56 #include "w.h"
57 
61 template<int BIT_COUNT>
63 public:
64  enum {
65  BITS = BIT_COUNT
66  };
67 
68  typedef uint64_t Word;
69 
70 private:
71  enum {
72  BITS_PER_WORD = 8 * sizeof(Word)
73  };
74  enum {
76  };
77 
78  uint64_t data[WORDS];
79 
80 public:
81 
83  clear();
84  }
85 
87  int num_bits() const {
88  return BIT_COUNT;
89  }
90 
92  int num_words() const {
93  int n = sizeof(data) / sizeof(data[0]);
94  w_assert1(n == WORDS);
95  return n;
96  }
97 
104  bool overlap(w_bitvector_t& merged, const w_bitvector_t& other) const {
105  return words_overlap(merged, other) == num_words();
106  }
107 
115  int words_overlap(w_bitvector_t& merged, const w_bitvector_t& other) const {
116  const uint64_t* mine = &data[0];
117  const uint64_t* theirs = &other.data[0];
118  uint64_t* newvec = &merged.data[0];
119 
120  int matches = 0;
121  for (int i = 0; i < num_words(); i++) {
122  if (*mine == (*mine & *theirs)) {
123  matches++;
124  }
125  *newvec = (*mine | *theirs);
126  newvec++;
127  mine++;
128  theirs++;
129  }
130  return matches;
131  }
132 
136  bool contains(const w_bitvector_t& subset) const {
137  for (int i = 0; i < WORDS; ++i) {
138  if ((data[i] & subset.data[i]) != subset.data[i]) {
139  return false;
140  }
141  }
142  return true;
143  }
144 
148  void merge(const w_bitvector_t& added) {
149  for (int i = 0; i < WORDS; ++i) {
150  data[i] |= added.data[i];
151  }
152  }
153 
154  ostream& print(ostream& o) const {
155  {
156  const char* sep = "";
157  o << "{";
158  for (int i = 0; i < BITS; i++) {
159  if (is_set(i)) {
160  o << sep << i;
161  sep = ".";
162  }
163  }
164  o << "}";
165  }
166  /*
167  {
168 
169  const char *sep="";
170  o << "(~{";
171  for(int i=0; i < BITS; i++)
172  {
173  if( !is_set(i)) { o << sep << i; sep="."; }
174  }
175  o << "})";
176  }
177  */
178  return o;
179  }
180 
182  void clear() {
183  for (int i = 0; i < WORDS; i++) {
184  data[i] = 0;
185  }
186  }
187 
189  bool is_empty() const {
190  Word hash = 0;
191  for (int i = 0; i < WORDS; i++) {
192  hash |= data[i];
193  }
194  return hash == 0;
195  }
196 
197  int num_bits_set() const {
198  int j = 0;
199  for (int i = 0; i < BITS; i++) {
200  if (is_set(i)) {
201  j++;
202  }
203  }
204  return j;
205  }
206 
208  bool is_full() const {
209  return num_bits_set() == BIT_COUNT;
210  }
211 
213  void copy(const w_bitvector_t& other) {
214  for (int i = 0; i < WORDS; i++) {
215  data[i] = other.data[i];
216  }
217  }
218 
219 #define BIT_VECTOR_PROLOGUE(idx) \
220  w_assert1(idx < BITS); \
221  Word wdex = idx / BITS_PER_WORD; \
222  Word bdex = idx % BITS_PER_WORD
223 
225  Word get_bit(Word idx) const {
226  BIT_VECTOR_PROLOGUE(idx);
227  return (data[wdex] >> bdex) & 0x1;
228  }
229 
231  bool is_set(Word idx) const {
232  return (get_bit(idx) == 0x1);
233  }
234 
236  void set_bit(Word idx) {
237  BIT_VECTOR_PROLOGUE(idx);
238  data[wdex] |= (1ul << bdex);
239  }
240 
242  void clear_bit(Word idx) {
243  BIT_VECTOR_PROLOGUE(idx);
244  data[wdex] &= ~(1ul << bdex);
245  }
246 
247 #undef BIT_VECTOR_PROLOGUE
248 };
249 
250 template<int BIT_COUNT>
251 ostream& operator<<(ostream& o, const w_bitvector_t<BIT_COUNT>& t) {
252  const char* sep = "";
253  o << "{";
254  for (int i = 0; i < BIT_COUNT; i++) {
255  if (t.is_set(i)) {
256  o << sep << i;
257  sep = ".";
258  }
259  }
260  o << "}";
261  return o;
262 }
263 
264 #endif // __W_BITVECTOR_H
int words_overlap(w_bitvector_t &merged, const w_bitvector_t &other) const
OR-together and return merged vector.
Definition: w_bitvector.h:115
uint64_t Word
Definition: w_bitvector.h:68
#define w_assert1(x)
Level 1 should not add significant extra time.
Definition: w_base.h:198
void copy(const w_bitvector_t &other)
copy operator
Definition: w_bitvector.h:213
#define BIT_VECTOR_PROLOGUE(idx)
Definition: w_bitvector.h:219
bool overlap(w_bitvector_t &merged, const w_bitvector_t &other) const
OR-together and return merged vector.
Definition: w_bitvector.h:104
bool is_empty() const
true if all bits are clear
Definition: w_bitvector.h:189
bool is_set(Word idx) const
true if bit at index idx is set
Definition: w_bitvector.h:231
int num_bits_set() const
Definition: w_bitvector.h:197
Definition: w_bitvector.h:75
void clear_bit(Word idx)
clear bit at index idx
Definition: w_bitvector.h:242
w_bitvector_t()
Definition: w_bitvector.h:82
Definition: w_bitvector.h:65
int num_words() const
return size in words (unsigned long)
Definition: w_bitvector.h:92
Templated bitmap for arbitrary size in bits.
Definition: w_bitvector.h:62
void clear()
clear all bits
Definition: w_bitvector.h:182
Word get_bit(Word idx) const
Should use is_set()
Definition: w_bitvector.h:225
int num_bits() const
return size in bits
Definition: w_bitvector.h:87
Definition: w_bitvector.h:72
bool contains(const w_bitvector_t &subset) const
Definition: w_bitvector.h:136
uint64_t data[WORDS]
Definition: w_bitvector.h:78
bool is_full() const
true if all bits are set
Definition: w_bitvector.h:208
void merge(const w_bitvector_t &added)
Definition: w_bitvector.h:148
void set_bit(Word idx)
set bit at index idx
Definition: w_bitvector.h:236
ostream & print(ostream &o) const
Definition: w_bitvector.h:154