w_bitmap.cpp

00001 /*<std-header orig-src='shore'>
00002 
00003  $Id: w_bitmap.cpp,v 1.18 2010/12/08 17:37:37 nhall Exp $
00004 
00005 SHORE -- Scalable Heterogeneous Object REpository
00006 
00007 Copyright (c) 1994-99 Computer Sciences Department, University of
00008                       Wisconsin -- Madison
00009 All Rights Reserved.
00010 
00011 Permission to use, copy, modify and distribute this software and its
00012 documentation is hereby granted, provided that both the copyright
00013 notice and this permission notice appear in all copies of the
00014 software, derivative works or modified versions, and any portions
00015 thereof, and that both notices appear in supporting documentation.
00016 
00017 THE AUTHORS AND THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY
00018 OF WISCONSIN - MADISON ALLOW FREE USE OF THIS SOFTWARE IN ITS
00019 "AS IS" CONDITION, AND THEY DISCLAIM ANY LIABILITY OF ANY KIND
00020 FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
00021 
00022 This software was developed with support by the Advanced Research
00023 Project Agency, ARPA order number 018 (formerly 8230), monitored by
00024 the U.S. Army Research Laboratory under contract DAAB07-91-C-Q518.
00025 Further funding for this work was provided by DARPA through
00026 Rome Research Laboratory Contract No. F30602-97-2-0247.
00027 
00028 */
00029 
00030 #include "w_defines.h"
00031 
00032 /*  -- do not edit anything above this line --   </std-header>*/
00033 
00034 #ifdef __GNUC__
00035 #   pragma implementation
00036 #endif
00037 
00038 #include "w_base.h"
00039 #include <cstring>
00040 #include "w_bitmap.h"
00041 
00042 inline int div8(long x)         { return x >> 3; }
00043 inline int mod8(long x)         { return x & 7; }
00044 inline int div32(long x)        { return x >> 5; }
00045 inline int mod32(long x)        { return x & 31; }
00046 
00047 int
00048 w_bitmap_t::bytesForBits(uint4_t numBits)
00049 {
00050     return (div8(numBits -1) + 1);
00051 }
00052 
00053 
00054 NORET
00055 w_bitmap_t::w_bitmap_t(uint4_t size)
00056     : sz(size), mem_alloc(true)
00057 {
00058     int n = bytesForBits(size);
00059     ptr = new uint1_t[n] ;
00060     if (!ptr) W_FATAL(fcOUTOFMEMORY) ; 
00061 }
00062 
00063 void 
00064 w_bitmap_t::zero()
00065 {
00066     int n = bytesForBits(sz);
00067     memset(ptr, 0, n);
00068 }
00069 
00070 void
00071 w_bitmap_t::fill()
00072 {
00073     int n = bytesForBits(sz);
00074     memset(ptr, 0xff, n);
00075 }
00076 
00077 bool 
00078 w_bitmap_t::is_set(uint4_t offset) const
00079 {
00080     return (ptr[div8(offset)] & (1 << mod8(offset))) != 0; 
00081 }
00082 
00083 void 
00084 w_bitmap_t::set(uint4_t offset)
00085 {
00086     ptr[div8(offset)] |= (1 << mod8(offset));
00087 }
00088 
00089 void
00090 w_bitmap_t::clr(uint4_t offset)
00091 {
00092     ptr[div8(offset)] &= ~(1 << mod8(offset));
00093 }
00094 
00095 w_base_t::int4_t
00096 w_bitmap_t::first_set(uint4_t start) const
00097 {
00098     w_assert9(start < sz);
00099     register uint1_t* p = ptr + div8(start);
00100     register uint4_t mask = 1 << mod8(start);
00101     register uint4_t size = sz;
00102     for (size -= start; size; start++, size--)  {
00103     if (*p & mask)  {
00104         w_assert9(is_set(start));
00105         return start;
00106     }
00107     if ((mask <<= 1) == 0x100)  {
00108         mask = 1;
00109         p++;
00110     }
00111     }
00112     
00113     return -1;
00114 }
00115 
00116 w_base_t::int4_t
00117 w_bitmap_t::first_clr(uint4_t start) const
00118 {
00119     w_assert9(start < sz);
00120     register uint1_t* p = ptr + div8(start);
00121     register uint4_t mask = 1 << mod8(start);
00122     register uint4_t size = sz;
00123     for (size -= start; size; start++, size--) {
00124     if ((*p & mask) == 0)    {
00125         return start;
00126     }
00127     if ((mask <<= 1) == 0x100)  {
00128         mask = 1;
00129         p++;
00130     }
00131     }
00132     
00133     return -1;
00134 }
00135 
00136 w_base_t::uint4_t
00137 w_bitmap_t::num_set() const
00138 {
00139     uint1_t* p = ptr;
00140     uint4_t size = sz;
00141     int count;
00142     int mask;
00143     for (count = 0, mask = 1; size; size--)  {
00144     if (*p & mask)    count++;
00145     if ((mask <<= 1) == 0x100)  {
00146         mask = 1;
00147         p++;
00148     }
00149     }
00150     return count;
00151 }
00152 
00153 ostream& operator<<(ostream& o, const w_bitmap_t& obj)
00154 {
00155     for (register unsigned i = 0; i < obj.sz; i++)  {
00156     o << (obj.is_set(i) != 0);
00157     }
00158     return o;
00159 }
00160 

Generated on Thu Dec 9 08:42:27 2010 for Shore Storage Manager by  doxygen 1.4.7