w_bitvector.h

00001 /* -*- mode:C++; c-basic-offset:4 -*-
00002      Shore-MT -- Multi-threaded port of the SHORE storage manager
00003    
00004                        Copyright (c) 2007-2009
00005       Data Intensive Applications and Systems Labaratory (DIAS)
00006                Ecole Polytechnique Federale de Lausanne
00007    
00008                          All Rights Reserved.
00009    
00010    Permission to use, copy, modify and distribute this software and
00011    its documentation is hereby granted, provided that both the
00012    copyright notice and this permission notice appear in all copies of
00013    the software, derivative works or modified versions, and any
00014    portions thereof, and that both notices appear in supporting
00015    documentation.
00016    
00017    This code is distributed in the hope that it will be useful, but
00018    WITHOUT ANY WARRANTY; without even the implied warranty of
00019    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS
00020    DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER
00021    RESULTING FROM THE USE OF THIS SOFTWARE.
00022 */
00023 
00024 /*<std-header orig-src='shore' incl-file-exclusion='W_BASE_H'>
00025 
00026  $Id: w_bitvector.h,v 1.3 2012/01/02 17:02:13 nhall Exp $
00027 
00028 SHORE -- Scalable Heterogeneous Object REpository
00029 
00030 Copyright (c) 1994-99 Computer Sciences Department, University of
00031                       Wisconsin -- Madison
00032 All Rights Reserved.
00033 
00034 Permission to use, copy, modify and distribute this software and its
00035 documentation is hereby granted, provided that both the copyright
00036 notice and this permission notice appear in all copies of the
00037 software, derivative works or modified versions, and any portions
00038 thereof, and that both notices appear in supporting documentation.
00039 
00040 THE AUTHORS AND THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY
00041 OF WISCONSIN - MADISON ALLOW FREE USE OF THIS SOFTWARE IN ITS
00042 "AS IS" CONDITION, AND THEY DISCLAIM ANY LIABILITY OF ANY KIND
00043 FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
00044 
00045 This software was developed with support by the Advanced Research
00046 Project Agency, ARPA order number 018 (formerly 8230), monitored by
00047 the U.S. Army Research Laboratory under contract DAAB07-91-C-Q518.
00048 Further funding for this work was provided by DARPA through
00049 Rome Research Laboratory Contract No. F30602-97-2-0247.
00050 
00051 */
00052 #include <w.h>
00053 
00054 /**\brief Templated bitmap for arbitrary size in bits
00055  *
00056  */
00057 template<int BIT_COUNT>
00058 class w_bitvector_t 
00059 {
00060 public:
00061     enum { BITS = BIT_COUNT };
00062     typedef unsigned long Word;
00063 private:
00064     enum { BITS_PER_WORD=8*sizeof(Word) };
00065     enum { WORDS = (BITS+BITS_PER_WORD-1)/BITS_PER_WORD };
00066     unsigned long data[WORDS];
00067 
00068 public:
00069 
00070     w_bitvector_t() { clear(); }
00071 
00072     /// return size in bits
00073     int num_bits() const { 
00074         return BIT_COUNT;
00075     }
00076 
00077     /// return size in words (unsigned long)
00078     int num_words() const { 
00079         int n= sizeof(data)/sizeof(data[0]); 
00080         w_assert1(n==WORDS);
00081         return n;
00082     }
00083 
00084     /** \brief OR-together and return merged vector.
00085      *
00086      * OR-together this bitmap with other bitmap and stuff result into
00087      * merged.
00088      * Return true if this entire bitmap is found in the other.
00089      */
00090     bool overlap(w_bitvector_t &merged, const w_bitvector_t &other)  const
00091     {
00092         return words_overlap(merged, other) == num_words();
00093     }
00094 
00095     /** \brief OR-together and return merged vector.
00096      *
00097      * OR-together this bitmap with other bitmap and stuff result into
00098      * merged.
00099      * Return the number of words in which this bitmap is found in
00100      * the other bitmap.  
00101      */
00102     int words_overlap(w_bitvector_t &merged, const w_bitvector_t &other)  const
00103     {
00104         const unsigned long *mine=&data[0];
00105         const unsigned long *theirs=&other.data[0];
00106         unsigned long *newvec=&merged.data[0];
00107 
00108         int matches=0;
00109         for(int i=0; i < num_words(); i++)
00110         {
00111             if (*mine == (*mine & *theirs)) matches++;
00112             *newvec = (*mine | *theirs);
00113             newvec++;
00114             mine++;
00115             theirs++;
00116         }
00117         return matches;
00118     }
00119 
00120     ostream &print(ostream &o) const 
00121     {
00122         {
00123             int s = num_bits_set();
00124             const char *sep="";
00125             o << "set " << s << "/" << num_bits()  << " bits={";
00126             for(int i=0; i < BITS; i++) 
00127             {
00128                 if(is_set(i)) { o << sep << i; sep="."; }
00129             }
00130             o << "}";
00131         }
00132         return o;
00133     }
00134 
00135     /// clear all bits
00136     void clear() {
00137         for(long i=0; i < WORDS; i++)
00138             data[i] = 0;
00139     }
00140 
00141     /// true if all bits are clear
00142     bool is_empty() const {
00143         Word hash = 0;
00144         for(long i=0; i < WORDS; i++)
00145             hash |= data[i];
00146         return hash == 0;
00147     }
00148 
00149     /// true if all bits are set - used in unit tests
00150     bool is_full() const {
00151         Word hash = ~0x0;
00152         for(long i=0; i < WORDS; i++) {
00153             if(hash & data[i] != hash) {
00154                 w_assert3( num_bits_set() < BIT_COUNT);
00155                 return false;
00156             }
00157         }
00158         w_assert3( num_bits_set() == BIT_COUNT);
00159         return true;
00160     }
00161 
00162     int num_bits_set() const {
00163         int j=0;
00164         for(int i=0; i < WORDS; i++) 
00165         {
00166             j+= w_base_t::pop_count(data[i]);
00167         }
00168         return j;
00169     }
00170 
00171     /// copy operator
00172     void copy(const w_bitvector_t &other)  {
00173         for(long i=0; i < WORDS; i++)
00174             data[i] = other.data[i];
00175     }
00176 
00177 #define BIT_VECTOR_PROLOGUE(idx)        \
00178     w_assert1(idx < BITS);                \
00179     Word wdex = idx / BITS_PER_WORD;        \
00180     Word bdex = idx % BITS_PER_WORD
00181     
00182     /// Should use is_set()
00183     Word get_bit(Word idx) const {
00184         BIT_VECTOR_PROLOGUE(idx);
00185         return (data[wdex] >> bdex) & 0x1;
00186     }
00187     /// true if bit at index idx is set
00188     bool is_set(Word idx) const {
00189         return (get_bit(idx) == 0x1);
00190     }
00191     /// set bit at index idx 
00192     void set_bit(Word idx) {
00193         BIT_VECTOR_PROLOGUE(idx);
00194         data[wdex] |= (1ul << bdex);
00195     }
00196     /// clear bit at index idx 
00197     void clear_bit(Word idx) {
00198         BIT_VECTOR_PROLOGUE(idx);
00199         data[wdex] &= ~(1ul << bdex);
00200     }
00201 #undef BIT_VECTOR_PROLOGUE
00202 };

Generated on Mon Jan 2 15:13:56 2012 for Shore Storage Manager by  doxygen 1.4.7