00001 /* file "bit_vector.h" */ 00002 00003 00004 /* 00005 Copyright (c) 1997 Stanford University 00006 00007 All rights reserved. 00008 00009 This software is provided under the terms described in 00010 the "suif_copyright.h" include file. 00011 */ 00012 00013 //#include <suif_copyright.h> 00014 00015 00016 #ifndef STY_BIT_VECTOR_H 00017 #define STY_BIT_VECTOR_H 00018 00019 //#ifndef SUPPRESS_PRAGMA_INTERFACE 00020 //#pragma interface 00021 //#endif 00022 00023 00024 /* 00025 This is the definition of the bit_vector class for sty, the 00026 first-level main library of the SUIF system. 00027 */ 00028 #include <stddef.h> 00029 #include <common/MString.h> 00030 #include <common/i_integer.h> 00031 #include <ion/ion.h> 00032 00033 class SuifEnv; 00034 class BitVectorBlock; 00035 00036 extern "C" void init_bit_vector(SuifEnv *suif_env); 00037 00038 // There is a unique one-to-one mapping from 00039 // a bitvector to an IInteger. 00040 // When the infinity_bit is 0, the integer is the 00041 // binary digit where the 0th bit is the least significant. 00042 // When the infinity_bit is 1, the IInteger is negative. 00043 // The interpretation is value of the 2s complement-form 00044 // bit pattern 00045 // 00046 00047 class BitVector 00048 { 00049 friend void init_bit_vector(SuifEnv *suif_env); 00050 00051 private: 00052 static BitVectorBlock *_zero_block; 00053 static BitVectorBlock *_ones_block; 00054 00055 BitVectorBlock *_block; 00056 size_t _count; // cache the count MUTABLE 00057 bool _has_count; // count cached - MUTABLE 00058 00059 static void do_initialization(void); 00060 00061 void add_reference(BitVectorBlock *the_block); 00062 void remove_reference(BitVectorBlock *the_block); 00063 void make_changable(void); 00064 00065 static unsigned char convert_from_hex(char hex_char); 00066 00067 public: 00068 typedef unsigned long ChunkT; 00069 00070 BitVector(void); 00071 BitVector(const BitVector &other); 00072 ~BitVector(void); 00073 00074 bool get_bit(size_t bit_num) const; 00075 bool get_infinity_bit(void) const; // i.e. sign bit 00076 size_t num_significant_bits(void) const; // number of highest bit 00077 size_t get_chunk_count() const; 00078 ChunkT get_chunk(size_t chunk_num) const; 00079 00080 void set_bit(size_t bit_num, bool new_value); 00081 void set_chunk(size_t chunk_num, ChunkT new_chunk); 00082 void set_to_zero(void); 00083 void set_to_ones(void); 00084 00085 void operator=(const BitVector &other); 00086 00087 String to_string(void) const; 00088 void from_string(String new_value); 00089 00090 IInteger to_i_integer(void) const; 00091 void from_i_integer(IInteger new_value); 00092 00093 size_t written_length(void) const; 00094 void write(char *location) const; 00095 00096 void read(const char *location); 00097 00098 void print(FILE *fp = stdout) const; 00099 void print(ion *the_ion) const; 00100 00101 bool is_equal_to(const BitVector &other) const; 00102 bool is_not_equal_to(const BitVector &other) const 00103 { return !is_equal_to(other); } 00104 bool is_less_than(const BitVector &other) const; 00105 bool is_greater_than(const BitVector &other) const 00106 { return other.is_less_than(*this); } 00107 bool is_less_than_or_equal_to(const BitVector &other) const 00108 { return !is_greater_than(other); } 00109 bool is_greater_than_or_equal_to(const BitVector &other) const 00110 { return !is_less_than(other); } 00111 00112 BitVector invert(void) const; 00113 00114 bool operator==(const BitVector &other) const 00115 { return is_equal_to(other); } 00116 bool operator!=(const BitVector &other) const 00117 { return !(*this == other); } 00118 bool operator<(const BitVector &other) const 00119 { return is_less_than(other); } 00120 bool operator>(const BitVector &other) const 00121 { return (other < *this); } 00122 bool operator<=(const BitVector &other) const 00123 { return !(*this > other); } 00124 bool operator>=(const BitVector &other) const 00125 { return !(*this < other); } 00126 00127 BitVector operator^(const BitVector &other) const; 00128 BitVector operator&(const BitVector &other) const; 00129 BitVector operator|(const BitVector &other) const; 00130 BitVector operator~(void) const; 00131 BitVector operator<<(size_t shift_amount) const; 00132 BitVector operator>>(size_t shift_amount) const; 00133 00134 BitVector operator-(const BitVector &other) const; 00135 00136 void subtract(const BitVector &other); 00137 00138 bool operator!(void) const; 00139 00140 void operator^=(const BitVector &other); 00141 void operator&=(const BitVector &other); 00142 void operator|=(const BitVector &other); 00143 void operator>>=(size_t shift_amount); 00144 void operator<<=(size_t shift_amount); 00145 00146 /* The following two operations are identical to operator&=() and 00147 * operator|=() respectively except that they also return a 00148 * bool value which is true if and only if this bit vector was 00149 * changed by the operation. */ 00150 bool do_and_with_test(const BitVector &other); 00151 bool do_or_with_test(const BitVector &other); 00152 bool do_subtract_with_test(const BitVector &other); 00153 00154 size_t count() const; 00155 }; 00156 00157 // when the sign bit is 0, iterate over the 1s 00158 // when the sign bit is 1, iterate over the 0s 00159 // 00160 class BitVectorIter { 00161 public: 00162 BitVectorIter(const BitVector *bv); 00163 BitVectorIter(const BitVectorIter &other); 00164 BitVectorIter &operator=(const BitVectorIter &other); 00165 // support the old-style increment(), done() 00166 // and the new-style is_valid(); next() 00167 bool is_valid() const; 00168 void next(); 00169 size_t current() const; 00170 void first(); // reset 00171 00172 void increment(); // next() 00173 bool done() const; // !is_valid() 00174 size_t get() const; // current() 00175 void reset(); // first() 00176 00177 private: 00178 bool _done; 00179 // unsigned _current_chunk; 00180 // unsigned _current_byte; 00181 size_t _current_bit; // this is the current bit number. 00182 unsigned char _remaining_byte; // the value remaining for the byte 00183 // the current bit is in. The current bit has been removed from this. 00184 // also it is the negation when the ones bit of the bitvector is 1. 00185 00186 // size_t _current; 00187 const BitVector *_bv; 00188 // bool _sign_bit; 00189 }; 00190 00191 00192 #endif /* STY_BIT_VECTOR_H */