tesseract 4.1.1
Loading...
Searching...
No Matches
unicity_table.h
Go to the documentation of this file.
1
2// 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.
32template <typename T>
34 public:
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
91
92 private:
93 GenericVector<T> table_;
94 // Mutable because Run method is not const
96};
97
98template <typename T>
99class UnicityTableEqEq : public UnicityTable<T> {
100 public:
103 NewPermanentTessCallback(tesseract::cmp_eq<T>));
104 }
105};
106
107template <typename T>
109 compare_cb_(nullptr) {
110}
111
112
113template <typename T>
115 clear();
116}
117
118template <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.
125template <typename T>
127 table_.reserve(size);
128}
129
130// Return the object from an id.
131template <typename T>
132const T &UnicityTable<T>::get(int id) const {
133 return table_.get(id);
134}
135// Returns the pointer to the object with the given id.
136template <typename T>
138 return &(table_.get(id));
139}
140// Return true if the id is valid
141template <typename T>
143 return table_.contains_index(id);
144}
145
146// Return the id of the T object.
147template <typename T>
148int UnicityTable<T>::get_id(T object) const {
149 return table_.get_index(object);
150}
151
152// Return true if T is in the table
153template <typename T>
154bool UnicityTable<T>::contains(T object) const {
155 return get_id(object) != -1;
156}
157
158// Add an element in the table
159template <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.
170template <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.
177template <typename T>
179 table_.set_compare_callback(cb);
180 compare_cb_ = cb;
181}
182
183// Clear the table, calling the callback function if any.
184template <typename T>
186 table_.clear();
187}
188
189template <typename T>
192 return table_.write(f, cb);
193}
194
195template <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.
203template <typename T>
205 table_.move(&from->table_);
206}
207
208#endif // TESSERACT_CCUTIL_UNICITY_TABLE_H_
_ConstTessMemberResultCallback_5_0< false, R, T1, P1, P2, P3, P4, P5 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)(P1, P2, P3, P4, P5) const, typename Identity< P1 >::type p1, typename Identity< P2 >::type p2, typename Identity< P3 >::type p3, typename Identity< P4 >::type p4, typename Identity< P5 >::type p5)
Definition: tesscallback.h:258
void move(UnicityTable< T > *from)
int get_id(T object) const
T contains_id(int id) const
Return true if the id is valid.
bool write(FILE *f, TessResultCallback2< bool, FILE *, T const & > *cb) const
int push_back(T object)
Add an element in the table.
T * get_mutable(int id)
bool read(tesseract::TFile *f, TessResultCallback2< bool, tesseract::TFile *, T * > *cb)
void set_compare_callback(TessResultCallback2< bool, T const &, T const & > *cb)
int size() const
Return the size used.
~UnicityTable()
Clear the structures and deallocate internal structures.
const T & get(int id) const
Return the object from an id.
void set_clear_callback(TessCallback1< T > *cb)
bool contains(T object) const
Return true if T is in the table.
void reserve(int size)