tesseract  5.0.0-alpha-619-ge9db
unicity_table.h
Go to the documentation of this file.
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 <functional> // for std::function
24 #include "errcode.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(std::function<void(T)> cb) {
69  table_.set_clear_callback(cb);
70  }
71 
74  void set_compare_callback(std::function<bool(const T&, const T&)> cb) {
75  table_.set_compare_callback(cb);
76  compare_cb_ = cb;
77  }
78 
83  void clear();
84 
87  void move(UnicityTable<T>* from);
88 
93  bool write(FILE* f, std::function<bool(FILE*, const T&)> cb) const {
94  return table_.write(f, cb);
95  }
96  bool read(tesseract::TFile* f, std::function<bool(tesseract::TFile*, T*)> cb) {
97  return table_.read(f, cb);
98  }
99 
100  private:
101  GenericVector<T> table_;
102  std::function<bool(const T&, const T&)> compare_cb_;
103 };
104 
105 template <typename T>
106 class UnicityTableEqEq : public UnicityTable<T> {
107  public:
109  using namespace std::placeholders; // for _1, _2
111  std::bind(tesseract::cmp_eq<T>, _1, _2));
112  }
113 };
114 
115 template <typename T>
117  compare_cb_(nullptr) {
118 }
119 
120 
121 template <typename T>
123  clear();
124 }
125 
126 template <typename T>
128  return table_.size();
129 }
130 
131 // Reserve some memory. If there is size or more elements, the table will
132 // then allocate size * 2 elements.
133 template <typename T>
134 void UnicityTable<T>::reserve(int size) {
135  table_.reserve(size);
136 }
137 
138 // Return the object from an id.
139 template <typename T>
140 const T &UnicityTable<T>::get(int id) const {
141  return table_.get(id);
142 }
143 // Returns the pointer to the object with the given id.
144 template <typename T>
146  return &(table_.get(id));
147 }
148 // Return true if the id is valid
149 template <typename T>
151  return table_.contains_index(id);
152 }
153 
154 // Return the id of the T object.
155 template <typename T>
156 int UnicityTable<T>::get_id(T object) const {
157  return table_.get_index(object);
158 }
159 
160 // Return true if T is in the table
161 template <typename T>
162 bool UnicityTable<T>::contains(T object) const {
163  return get_id(object) != -1;
164 }
165 
166 // Add an element in the table
167 template <typename T>
169  int idx = get_id(object);
170  if (idx == -1) {
171  idx = table_.push_back(object);
172  }
173  return idx;
174 }
175 
176 // Clear the table, calling the callback function if any.
177 template <typename T>
179  table_.clear();
180 }
181 
182 // This method clear the current object, then, does a shallow copy of
183 // its argument, and finally invalidate its argument.
184 template <typename T>
186  table_.move(&from->table_);
187 }
188 
189 #endif // TESSERACT_CCUTIL_UNICITY_TABLE_H_
UnicityTable::reserve
void reserve(int size)
Definition: unicity_table.h:134
UnicityTableEqEq
Definition: unicity_table.h:106
UnicityTable::contains
bool contains(T object) const
Return true if T is in the table.
Definition: unicity_table.h:162
UnicityTable::get_id
int get_id(T object) const
Definition: unicity_table.h:156
UnicityTable::push_back
int push_back(T object)
Add an element in the table.
Definition: unicity_table.h:168
genericvector.h
UnicityTable::set_compare_callback
void set_compare_callback(std::function< bool(const T &, const T &)> cb)
Definition: unicity_table.h:74
UnicityTable::read
bool read(tesseract::TFile *f, std::function< bool(tesseract::TFile *, T *)> cb)
Definition: unicity_table.h:96
tesseract::TFile
Definition: serialis.h:75
UnicityTable::get
const T & get(int id) const
Return the object from an id.
Definition: unicity_table.h:140
GenericVector
Definition: baseapi.h:40
UnicityTable
Definition: fontinfo.h:30
UnicityTable::size
int size() const
Return the size used.
Definition: unicity_table.h:127
UnicityTable::clear
void clear()
Definition: unicity_table.h:178
UnicityTable::write
bool write(FILE *f, std::function< bool(FILE *, const T &)> cb) const
Definition: unicity_table.h:93
UnicityTable::get_mutable
T * get_mutable(int id)
Definition: unicity_table.h:145
UnicityTable::set_clear_callback
void set_clear_callback(std::function< void(T)> cb)
Definition: unicity_table.h:68
UnicityTable::contains_id
T contains_id(int id) const
Return true if the id is valid.
Definition: unicity_table.h:150
UnicityTable::~UnicityTable
~UnicityTable()
Clear the structures and deallocate internal structures.
Definition: unicity_table.h:122
errcode.h
UnicityTable::UnicityTable
UnicityTable()
Definition: unicity_table.h:116
UnicityTable::move
void move(UnicityTable< T > *from)
Definition: unicity_table.h:185
UnicityTableEqEq::UnicityTableEqEq
UnicityTableEqEq()
Definition: unicity_table.h:108