Tesseract  3.02
tesseract-ocr/ccutil/bitvector.cpp
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.cpp
00005 // Description: Class replacement for BITVECTOR.
00006 // Author:      Ray Smith
00007 // Created:     Mon Jan 10 17:45: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 #include "bitvector.h"
00023 #include <string.h>
00024 #include "helpers.h"
00025 
00026 namespace tesseract {
00027 
00028 BitVector::BitVector() : bit_size_(0), array_(NULL) {}
00029 
00030 BitVector::BitVector(int length) : bit_size_(length) {
00031   array_ = new uinT32[WordLength()];
00032   SetAllFalse();
00033 }
00034 
00035 BitVector::BitVector(const BitVector& src) : bit_size_(src.bit_size_) {
00036   array_ = new uinT32[WordLength()];
00037   memcpy(array_, src.array_, ByteLength());
00038 }
00039 
00040 BitVector& BitVector::operator=(const BitVector& src) {
00041   Alloc(src.bit_size_);
00042   memcpy(array_, src.array_, ByteLength());
00043   return *this;
00044 }
00045 
00046 BitVector::~BitVector() {
00047   delete [] array_;
00048 }
00049 
00050 // Initializes the array to length * false.
00051 void BitVector::Init(int length) {
00052   Alloc(length);
00053   SetAllFalse();
00054 }
00055 
00056 // Writes to the given file. Returns false in case of error.
00057 bool BitVector::Serialize(FILE* fp) const {
00058   if (fwrite(&bit_size_, sizeof(bit_size_), 1, fp) != 1) return false;
00059   int wordlen = WordLength();
00060   if (fwrite(array_, sizeof(*array_), wordlen, fp) != wordlen) return false;
00061   return true;
00062 }
00063 
00064 // Reads from the given file. Returns false in case of error.
00065 // If swap is true, assumes a big/little-endian swap is needed.
00066 bool BitVector::DeSerialize(bool swap, FILE* fp) {
00067   uinT32 new_bit_size;
00068   if (fread(&new_bit_size, sizeof(new_bit_size), 1, fp) != 1) return false;
00069   if (swap) {
00070     ReverseN(&new_bit_size, sizeof(new_bit_size));
00071   }
00072   Alloc(new_bit_size);
00073   int wordlen = WordLength();
00074   if (fread(array_, sizeof(*array_), wordlen, fp) != wordlen) return false;
00075   if (swap) {
00076     for (int i = 0; i < wordlen; ++i)
00077       ReverseN(&array_[i], sizeof(array_[i]));
00078   }
00079   return true;
00080 }
00081 
00082 void BitVector::SetAllFalse() {
00083   memset(array_, 0, ByteLength());
00084 }
00085 void BitVector::SetAllTrue() {
00086   memset(array_, ~0, ByteLength());
00087 }
00088 
00089 // Allocates memory for a vector of the given length.
00090 // Reallocates if the array is a different size, larger or smaller.
00091 void BitVector::Alloc(int length) {
00092   int initial_wordlength = WordLength();
00093   bit_size_ = length;
00094   int new_wordlength = WordLength();
00095   if (new_wordlength != initial_wordlength) {
00096     delete [] array_;
00097     array_ = new uinT32[new_wordlength];
00098   }
00099 }
00100 
00101 
00102 }  // namespace tesseract.
00103 
00104