Tesseract  3.02
tesseract-ocr/ccutil/bitvector.h
Go to the documentation of this file.
00001 // Copyright 2011 Google Inc. All Rights Reserved.
00002 // Author: rays@google.com (Ray Smith)
00004 // File:        bitvector.h
00005 // Description: Class replacement for BITVECTOR.
00006 // Author:      Ray Smith
00007 // Created:     Mon Jan 10 17:44:01 PST 2011
00008 //
00009 // (C) Copyright 2011, Google Inc.
00010 // Licensed under the Apache License, Version 2.0 (the "License");
00011 // you may not use this file except in compliance with the License.
00012 // You may obtain a copy of the License at
00013 // http://www.apache.org/licenses/LICENSE-2.0
00014 // Unless required by applicable law or agreed to in writing, software
00015 // distributed under the License is distributed on an "AS IS" BASIS,
00016 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00017 // See the License for the specific language governing permissions and
00018 // limitations under the License.
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 // Trivial class to encapsulate a fixed-length array of bits, with
00033 // Serialize/DeSerialize. Replaces the old macros.
00034 class BitVector {
00035  public:
00036   BitVector();
00037   // Initializes the array to length * false.
00038   explicit BitVector(int length);
00039   BitVector(const BitVector& src);
00040   BitVector& operator=(const BitVector& src);
00041   ~BitVector();
00042 
00043   // Initializes the array to length * false.
00044   void Init(int length);
00045 
00046   // Returns the number of bits that are accessible in the vector.
00047   int size() const {
00048     return bit_size_;
00049   }
00050 
00051   // Writes to the given file. Returns false in case of error.
00052   bool Serialize(FILE* fp) const;
00053   // Reads from the given file. Returns false in case of error.
00054   // If swap is true, assumes a big/little-endian swap is needed.
00055   bool DeSerialize(bool swap, FILE* fp);
00056 
00057   void SetAllFalse();
00058   void SetAllTrue();
00059 
00060   // Accessors to set/reset/get bits.
00061   // The range of index is [0, size()-1].
00062   // There is debug-only bounds checking.
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   // Allocates memory for a vector of the given length.
00084   void Alloc(int length);
00085 
00086   // Computes the index to array_ for the given index, with debug range
00087   // checking.
00088   int WordIndex(int index) const {
00089     assert(0 <= index && index < bit_size_);
00090     return index / kBitFactor;
00091   }
00092   // Returns a mask to select the appropriate bit for the given index.
00093   uinT32 BitMask(int index) const {
00094     return 1 << (index & (kBitFactor - 1));
00095   }
00096   // Returns the number of array elements needed to represent the current
00097   // bit_size_.
00098   int WordLength() const {
00099     return (bit_size_ + kBitFactor - 1) / kBitFactor;
00100   }
00101   // Returns the number of bytes consumed by the array_.
00102   int ByteLength() const {
00103     return WordLength() * sizeof(*array_);
00104   }
00105 
00106   // Number of bits in this BitVector.
00107   uinT32 bit_size_;
00108   // Array of words used to pack the bits.
00109   uinT32* array_;
00110   // Number of bits in an array_ element.
00111   static const int kBitFactor = sizeof(uinT32) * 8;
00112 };
00113 
00114 }  // namespace tesseract.
00115 
00116 #endif  // TESSERACT_CCUTIL_BITVECTOR_H__