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.2 2010/05/26 01:20:23 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             const char *sep="";
00124             o << "{";
00125             for(int i=0; i < BITS; i++) 
00126             {
00127                 if(is_set(i)) { o << sep << i; sep="."; }
00128             }
00129             o << "}";
00130         }
00131         {
00132 
00133             const char *sep="";
00134             o << "(~{";
00135             for(int i=0; i < BITS; i++) 
00136             {
00137                 if( !is_set(i)) { o << sep << i; sep="."; }
00138             }
00139             o << "})";
00140             return o;
00141         }
00142     }
00143 
00144     /// clear all bits
00145     void clear() {
00146         for(long i=0; i < WORDS; i++)
00147             data[i] = 0;
00148     }
00149 
00150     /// true if all bits are clear
00151     bool is_empty() const {
00152         Word hash = 0;
00153         for(long i=0; i < WORDS; i++)
00154             hash |= data[i];
00155         return hash == 0;
00156     }
00157 
00158     int num_bits_set() const {
00159         int j=0;
00160         for(int i=0; i < BITS; i++) 
00161         {
00162             if(is_set(i)) j++;
00163         }
00164         return j;
00165     }
00166 
00167     /// true if all bits are set
00168     bool is_full() const {
00169         return num_bits_set() == BIT_COUNT;
00170     }
00171 
00172     /// copy operator
00173     void copy(const w_bitvector_t &other)  {
00174         for(long i=0; i < WORDS; i++)
00175             data[i] = other.data[i];
00176     }
00177 
00178 #define BIT_VECTOR_PROLOGUE(idx)        \
00179     w_assert1(idx < BITS);                \
00180     Word wdex = idx / BITS_PER_WORD;        \
00181     Word bdex = idx % BITS_PER_WORD
00182     
00183     /// Should use is_set()
00184     Word get_bit(Word idx) const {
00185         BIT_VECTOR_PROLOGUE(idx);
00186         return (data[wdex] >> bdex) & 0x1;
00187     }
00188     /// true if bit at index idx is set
00189     bool is_set(Word idx) const {
00190         return (get_bit(idx) == 0x1);
00191     }
00192     /// set bit at index idx 
00193     void set_bit(Word idx) {
00194         BIT_VECTOR_PROLOGUE(idx);
00195         data[wdex] |= (1ul << bdex);
00196     }
00197     /// clear bit at index idx 
00198     void clear_bit(Word idx) {
00199         BIT_VECTOR_PROLOGUE(idx);
00200         data[wdex] &= ~(1ul << bdex);
00201     }
00202 #undef BIT_VECTOR_PROLOGUE
00203 };

Generated on Wed Jul 7 17:22:32 2010 for Shore Storage Manager by  doxygen 1.4.7