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
unicity_table.h
Go to the documentation of this file.
1
2
// File: UnicityTable.h
3
// Description: a class to uniquify objects, manipulating them using integers
4
// ids.
5
// Author: Samuel Charron
6
//
7
// (C) Copyright 2006, Google Inc.
8
// Licensed under the Apache License, Version 2.0 (the "License");
9
// you may not use this file except in compliance with the License.
10
// You may obtain a copy of the License at
11
// http://www.apache.org/licenses/LICENSE-2.0
12
// Unless required by applicable law or agreed to in writing, software
13
// distributed under the License is distributed on an "AS IS" BASIS,
14
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
// See the License for the specific language governing permissions and
16
// limitations under the License.
17
//
19
20
#ifndef TESSERACT_CCUTIL_UNICITY_TABLE_H_
21
#define TESSERACT_CCUTIL_UNICITY_TABLE_H_
22
23
#include "
tesscallback.h
"
24
#include "
errcode.h
"
25
#include "
genericvector.h
"
26
27
// A class to uniquify objects, manipulating them using integers ids.
28
// T requirements:
29
// operator= to add an element
30
// default-constructible: allocating the internal table will call the default
31
// constructor.
32
template
<
typename
T>
33
class
UnicityTable
{
34
public
:
35
UnicityTable
();
37
~UnicityTable
();
38
41
void
reserve
(
int
size
);
42
44
int
size
()
const
;
45
47
const
T &
get
(
int
id)
const
;
48
49
// Return the pointer to an object with the given id.
50
T *
get_mutable
(
int
id
);
51
55
int
get_id
(T
object
)
const
;
56
58
bool
contains
(T
object
)
const
;
59
61
T
contains_id
(
int
id
)
const
;
62
64
int
push_back
(T
object
);
65
68
void
set_clear_callback
(
TessCallback1<T>
* cb);
69
72
void
set_compare_callback
(
TessResultCallback2<bool, T const &, T const &>
* cb);
73
78
void
clear
();
79
82
void
move
(
UnicityTable<T>
* from);
83
88
bool
write
(FILE*
f
,
TessResultCallback2<bool, FILE*, T const &>
* cb)
const
;
90
bool
read
(FILE* f,
TessResultCallback3<bool, FILE*, T*, bool>
* cb,
bool
swap);
91
92
private
:
93
GenericVector<T>
table_;
94
// Mutable because Run method is not const
95
mutable
TessResultCallback2<bool, T const &, T const &>
* compare_cb_;
96
};
97
98
template
<
typename
T>
99
class
UnicityTableEqEq
:
public
UnicityTable
<T> {
100
public
:
101
UnicityTableEqEq
() {
102
UnicityTable<T>::set_compare_callback
(
103
NewPermanentTessCallback
(tesseract::cmp_eq<T>));
104
}
105
};
106
107
template
<
typename
T>
108
UnicityTable<T>::UnicityTable
() :
109
compare_cb_(0) {
110
}
111
112
113
template
<
typename
T>
114
UnicityTable<T>::~UnicityTable
() {
115
clear();
116
}
117
118
template
<
typename
T>
119
int
UnicityTable<T>::size
()
const
{
120
return
table_.size();
121
}
122
123
// Reserve some memory. If there is size or more elements, the table will
124
// then allocate size * 2 elements.
125
template
<
typename
T>
126
void
UnicityTable<T>::reserve
(
int
size) {
127
table_.reserve(size);
128
}
129
130
// Return the object from an id.
131
template
<
typename
T>
132
const
T &
UnicityTable<T>::get
(
int
id
)
const
{
133
return
table_.get(
id
);
134
}
135
// Returns the pointer to the object with the given id.
136
template
<
typename
T>
137
T *
UnicityTable<T>::get_mutable
(
int
id
) {
138
return
&(table_.get(
id
));
139
}
140
// Return true if the id is valid
141
template
<
typename
T>
142
T
UnicityTable<T>::contains_id
(
int
id
)
const
{
143
return
table_.contains_index(
id
);
144
}
145
146
// Return the id of the T object.
147
template
<
typename
T>
148
int
UnicityTable<T>::get_id
(T
object
)
const
{
149
return
table_.get_index(
object
);
150
}
151
152
// Return true if T is in the table
153
template
<
typename
T>
154
bool
UnicityTable<T>::contains
(T
object
)
const
{
155
return
get_id(
object
) != -1;
156
}
157
158
// Add an element in the table
159
template
<
typename
T>
160
int
UnicityTable<T>::push_back
(T
object
) {
161
int
idx = get_id(
object
);
162
if
(idx == -1) {
163
idx = table_.push_back(
object
);
164
}
165
return
idx;
166
}
167
168
// Add a callback to be called to delete the elements when the table took
169
// their ownership.
170
template
<
typename
T>
171
void
UnicityTable<T>::set_clear_callback
(
TessCallback1<T>
* cb) {
172
table_.set_clear_callback(cb);
173
}
174
175
// Add a callback to be called to delete the elements when the table took
176
// their ownership.
177
template
<
typename
T>
178
void
UnicityTable<T>::set_compare_callback
(
TessResultCallback2<bool, T const &, T const &>
* cb) {
179
table_.set_compare_callback(cb);
180
compare_cb_ = cb;
181
}
182
183
// Clear the table, calling the callback function if any.
184
template
<
typename
T>
185
void
UnicityTable<T>::clear
() {
186
table_.clear();
187
}
188
189
template
<
typename
T>
190
bool
UnicityTable<T>::write
(
191
FILE*
f
,
TessResultCallback2<bool, FILE*, T const &>
* cb)
const
{
192
return
table_.write(f, cb);
193
}
194
195
template
<
typename
T>
196
bool
UnicityTable<T>::read
(
197
FILE*
f
,
TessResultCallback3<bool, FILE*, T*, bool>
* cb,
bool
swap) {
198
return
table_.read(f, cb, swap);
199
}
200
201
// This method clear the current object, then, does a shallow copy of
202
// its argument, and finally invalidate its argument.
203
template
<
typename
T>
204
void
UnicityTable<T>::move
(
UnicityTable<T>
* from) {
205
table_.move(&from->table_);
206
}
207
208
#endif // TESSERACT_CCUTIL_UNICITY_TABLE_H_
mnt
data
src
tesseract-ocr
ccutil
unicity_table.h
Generated on Thu Nov 1 2012 20:19:46 for Tesseract by
1.8.1