00001
00002
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00021
00022
00023 #ifndef TESSERACT_CCUTIL_BITVECTOR_H__
00024 #define TESSERACT_CCUTIL_BITVECTOR_H__
00025
00026 #include <assert.h>
00027 #include <stdio.h>
00028 #include "host.h"
00029
00030 namespace tesseract {
00031
00032
00033
00034 class BitVector {
00035 public:
00036 BitVector();
00037
00038 explicit BitVector(int length);
00039 BitVector(const BitVector& src);
00040 BitVector& operator=(const BitVector& src);
00041 ~BitVector();
00042
00043
00044 void Init(int length);
00045
00046
00047 int size() const {
00048 return bit_size_;
00049 }
00050
00051
00052 bool Serialize(FILE* fp) const;
00053
00054
00055 bool DeSerialize(bool swap, FILE* fp);
00056
00057 void SetAllFalse();
00058 void SetAllTrue();
00059
00060
00061
00062
00063 void SetBit(int index) {
00064 array_[WordIndex(index)] |= BitMask(index);
00065 }
00066 void ResetBit(int index) {
00067 array_[WordIndex(index)] &= ~BitMask(index);
00068 }
00069 void SetValue(int index, bool value) {
00070 if (value)
00071 SetBit(index);
00072 else
00073 ResetBit(index);
00074 }
00075 bool At(int index) const {
00076 return (array_[WordIndex(index)] & BitMask(index)) != 0;
00077 }
00078 bool operator[](int index) const {
00079 return (array_[WordIndex(index)] & BitMask(index)) != 0;
00080 }
00081
00082 private:
00083
00084 void Alloc(int length);
00085
00086
00087
00088 int WordIndex(int index) const {
00089 assert(0 <= index && index < bit_size_);
00090 return index / kBitFactor;
00091 }
00092
00093 uinT32 BitMask(int index) const {
00094 return 1 << (index & (kBitFactor - 1));
00095 }
00096
00097
00098 int WordLength() const {
00099 return (bit_size_ + kBitFactor - 1) / kBitFactor;
00100 }
00101
00102 int ByteLength() const {
00103 return WordLength() * sizeof(*array_);
00104 }
00105
00106
00107 uinT32 bit_size_;
00108
00109 uinT32* array_;
00110
00111 static const int kBitFactor = sizeof(uinT32) * 8;
00112 };
00113
00114 }
00115
00116 #endif // TESSERACT_CCUTIL_BITVECTOR_H__