tesseract  v4.0.0-17-g361f3264
Open Source OCR Engine
unicity_table.h
1 // File: unicity_table.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 
69 
73 
78  void clear();
79 
82  void move(UnicityTable<T>* from);
83 
88  bool write(FILE* f, TessResultCallback2<bool, FILE*, T const &>* cb) const;
89  bool read(tesseract::TFile* f,
91 
92  private:
94  // Mutable because Run method is not const
96 };
97 
98 template <typename T>
99 class UnicityTableEqEq : public UnicityTable<T> {
100  public:
103  NewPermanentTessCallback(tesseract::cmp_eq<T>));
104  }
105 };
106 
107 template <typename T>
109  compare_cb_(nullptr) {
110 }
111 
112 
113 template <typename T>
115  clear();
116 }
117 
118 template <typename T>
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>
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>
138  return &(table_.get(id));
139 }
140 // Return true if the id is valid
141 template <typename T>
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>
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>
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>
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>
186  table_.clear();
187 }
188 
189 template <typename T>
191  FILE* f, TessResultCallback2<bool, FILE*, T const &>* cb) const {
192  return table_.write(f, cb);
193 }
194 
195 template <typename T>
198  return table_.read(f, cb);
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>
205  table_.move(&from->table_);
206 }
207 
208 #endif // TESSERACT_CCUTIL_UNICITY_TABLE_H_
GenericVector< T > table_
Definition: unicity_table.h:93
UnicityTableEqEq()
Definition: unicity_table.h:101
UnicityTable()
Definition: unicity_table.h:108
int push_back(T object)
Add an element in the table.
Definition: unicity_table.h:160
bool read(tesseract::TFile *f, TessResultCallback2< bool, tesseract::TFile *, T *> *cb)
Definition: unicity_table.h:196
T contains_id(int id) const
Return true if the id is valid.
Definition: unicity_table.h:142
void clear()
Definition: unicity_table.h:185
Definition: serialis.h:77
void reserve(int size)
Definition: unicity_table.h:126
Definition: fontinfo.h:30
bool contains(T object) const
Return true if T is in the table.
Definition: unicity_table.h:154
const T & get(int id) const
Return the object from an id.
Definition: unicity_table.h:132
T * get_mutable(int id)
Definition: unicity_table.h:137
~UnicityTable()
Clear the structures and deallocate internal structures.
Definition: unicity_table.h:114
Definition: baseapi.h:37
Definition: unicity_table.h:99
void set_compare_callback(TessResultCallback2< bool, T const &, T const &> *cb)
Definition: unicity_table.h:178
TessResultCallback2< bool, T const &, T const & > * compare_cb_
Definition: unicity_table.h:95
bool write(FILE *f, TessResultCallback2< bool, FILE *, T const &> *cb) const
Definition: unicity_table.h:190
void move(UnicityTable< T > *from)
Definition: unicity_table.h:204
Definition: blamer.h:43
void set_clear_callback(TessCallback1< T > *cb)
Definition: unicity_table.h:171
int size() const
Return the size used.
Definition: unicity_table.h:119
int get_id(T object) const
Definition: unicity_table.h:148