Tesseract 3.02

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
bitvector.cpp
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.cpp
5 // Description: Class replacement for BITVECTOR.
6 // Author: Ray Smith
7 // Created: Mon Jan 10 17:45: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 #include "bitvector.h"
23 #include <string.h>
24 #include "helpers.h"
25
26 namespace tesseract {
27
28 BitVector::BitVector() : bit_size_(0), array_(NULL) {}
29
30 BitVector::BitVector(int length) : bit_size_(length) {
31 array_ = new uinT32[WordLength()];
32 SetAllFalse();
33 }
34
35 BitVector::BitVector(const BitVector& src) : bit_size_(src.bit_size_) {
36 array_ = new uinT32[WordLength()];
37 memcpy(array_, src.array_, ByteLength());
38 }
39
40 BitVector& BitVector::operator=(const BitVector& src) {
41 Alloc(src.bit_size_);
42 memcpy(array_, src.array_, ByteLength());
43 return *this;
44 }
45
46 BitVector::~BitVector() {
47 delete [] array_;
48 }
49
50 // Initializes the array to length * false.
51 void BitVector::Init(int length) {
52 Alloc(length);
53 SetAllFalse();
54 }
55
56 // Writes to the given file. Returns false in case of error.
57 bool BitVector::Serialize(FILE* fp) const {
58 if (fwrite(&bit_size_, sizeof(bit_size_), 1, fp) != 1) return false;
59 int wordlen = WordLength();
60 if (fwrite(array_, sizeof(*array_), wordlen, fp) != wordlen) return false;
61 return true;
62 }
63
64 // Reads from the given file. Returns false in case of error.
65 // If swap is true, assumes a big/little-endian swap is needed.
66 bool BitVector::DeSerialize(bool swap, FILE* fp) {
67 uinT32 new_bit_size;
68 if (fread(&new_bit_size, sizeof(new_bit_size), 1, fp) != 1) return false;
69 if (swap) {
70 ReverseN(&new_bit_size, sizeof(new_bit_size));
71 }
72 Alloc(new_bit_size);
73 int wordlen = WordLength();
74 if (fread(array_, sizeof(*array_), wordlen, fp) != wordlen) return false;
75 if (swap) {
76 for (int i = 0; i < wordlen; ++i)
77 ReverseN(&array_[i], sizeof(array_[i]));
78 }
79 return true;
80 }
81
82 void BitVector::SetAllFalse() {
83 memset(array_, 0, ByteLength());
84 }
85 void BitVector::SetAllTrue() {
86 memset(array_, ~0, ByteLength());
87 }
88
89 // Allocates memory for a vector of the given length.
90 // Reallocates if the array is a different size, larger or smaller.
91 void BitVector::Alloc(int length) {
92 int initial_wordlength = WordLength();
93 bit_size_ = length;
94 int new_wordlength = WordLength();
95 if (new_wordlength != initial_wordlength) {
96 delete [] array_;
97 array_ = new uinT32[new_wordlength];
98 }
99 }
100
101
102 } // namespace tesseract.
103
104