Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
bitvector.h
Go to the documentation of this file.
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 // Author: rays@google.com (Ray Smith)
4 // File: bitvector.h
5 // Description: Class replacement for BITVECTOR.
6 // Author: Ray Smith
7 // Created: Mon Jan 10 17:44:01 PST 2011
8 //
9 // (C) Copyright 2011, Google Inc.
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
13 // http://www.apache.org/licenses/LICENSE-2.0
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 //
21 
22 
23 #ifndef TESSERACT_CCUTIL_BITVECTOR_H__
24 #define TESSERACT_CCUTIL_BITVECTOR_H__
25 
26 #include <assert.h>
27 #include <stdio.h>
28 #include "host.h"
29 
30 namespace tesseract {
31 
32 // Trivial class to encapsulate a fixed-length array of bits, with
33 // Serialize/DeSerialize. Replaces the old macros.
34 class BitVector {
35  public:
36  BitVector();
37  // Initializes the array to length * false.
38  explicit BitVector(int length);
39  BitVector(const BitVector& src);
40  BitVector& operator=(const BitVector& src);
41  ~BitVector();
42 
43  // Initializes the array to length * false.
44  void Init(int length);
45 
46  // Returns the number of bits that are accessible in the vector.
47  int size() const {
48  return bit_size_;
49  }
50 
51  // Writes to the given file. Returns false in case of error.
52  bool Serialize(FILE* fp) const;
53  // Reads from the given file. Returns false in case of error.
54  // If swap is true, assumes a big/little-endian swap is needed.
55  bool DeSerialize(bool swap, FILE* fp);
56 
57  void SetAllFalse();
58  void SetAllTrue();
59 
60  // Accessors to set/reset/get bits.
61  // The range of index is [0, size()-1].
62  // There is debug-only bounds checking.
63  void SetBit(int index) {
64  array_[WordIndex(index)] |= BitMask(index);
65  }
66  void ResetBit(int index) {
67  array_[WordIndex(index)] &= ~BitMask(index);
68  }
69  void SetValue(int index, bool value) {
70  if (value)
71  SetBit(index);
72  else
73  ResetBit(index);
74  }
75  bool At(int index) const {
76  return (array_[WordIndex(index)] & BitMask(index)) != 0;
77  }
78  bool operator[](int index) const {
79  return (array_[WordIndex(index)] & BitMask(index)) != 0;
80  }
81 
82  private:
83  // Allocates memory for a vector of the given length.
84  void Alloc(int length);
85 
86  // Computes the index to array_ for the given index, with debug range
87  // checking.
88  int WordIndex(int index) const {
89  assert(0 <= index && index < bit_size_);
90  return index / kBitFactor;
91  }
92  // Returns a mask to select the appropriate bit for the given index.
93  uinT32 BitMask(int index) const {
94  return 1 << (index & (kBitFactor - 1));
95  }
96  // Returns the number of array elements needed to represent the current
97  // bit_size_.
98  int WordLength() const {
99  return (bit_size_ + kBitFactor - 1) / kBitFactor;
100  }
101  // Returns the number of bytes consumed by the array_.
102  int ByteLength() const {
103  return WordLength() * sizeof(*array_);
104  }
105 
106  // Number of bits in this BitVector.
107  uinT32 bit_size_;
108  // Array of words used to pack the bits.
109  uinT32* array_;
110  // Number of bits in an array_ element.
111  static const int kBitFactor = sizeof(uinT32) * 8;
112 };
113 
114 } // namespace tesseract.
115 
116 #endif // TESSERACT_CCUTIL_BITVECTOR_H__