tesseract  5.0.0
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 "errcode.h"
24 
25 #include "genericvector.h"
26 
27 #include <functional> // for std::function
28 
29 namespace tesseract {
30 
31 // A class to uniquify objects, manipulating them using integers ids.
32 // T requirements:
33 // operator= to add an element
34 // default-constructible: allocating the internal table will call the default
35 // constructor.
36 template <typename T>
37 class UnicityTable {
38 public:
41  clear();
42  }
43 
46  void reserve(int size) {
47  table_.reserve(size);
48  }
49 
51  int size() const {
52  return table_.size();
53  }
54 
56  const T &at(int id) const {
57  return table_.at(id);
58  }
59 
60  // Return the pointer to an object with the given id.
61  T &at(int id) {
62  return table_.at(id);
63  }
64 
68  int get_index(T object) const {
69  return table_.get_index(object);
70  }
71 
73  int push_back(T object) {
74  auto idx = get_index(object);
75  if (idx == -1) {
76  table_.push_back(object);
77  idx = size();
78  }
79  return idx;
80  }
81 
84  void set_clear_callback(std::function<void(T)> cb) {
85  table_.set_clear_callback(cb);
86  }
87 
92  void clear() {
93  table_.clear();
94  }
95 
98  void move(UnicityTable<T> *from) {
99  table_.move(&from->table_);
100  }
101 
106  bool write(FILE *f, std::function<bool(FILE *, const T &)> cb) const {
107  return table_.write(f, cb);
108  }
109  bool read(tesseract::TFile *f, std::function<bool(tesseract::TFile *, T *)> cb) {
110  return table_.read(f, cb);
111  }
112 
113 private:
114  GenericVector<T> table_;
115 };
116 
117 } // namespace tesseract
118 
119 #endif // TESSERACT_CCUTIL_UNICITY_TABLE_H_
int size() const
Return the size used.
Definition: unicity_table.h:51
~UnicityTable()
Clear the structures and deallocate internal structures.
Definition: unicity_table.h:40
void set_clear_callback(std::function< void(T)> cb)
Definition: unicity_table.h:84
int push_back(T object)
Add an element in the table.
Definition: unicity_table.h:73
bool write(FILE *f, std::function< bool(FILE *, const T &)> cb) const
int get_index(T object) const
Definition: unicity_table.h:68
void move(UnicityTable< T > *from)
Definition: unicity_table.h:98
const T & at(int id) const
Return the object from an id.
Definition: unicity_table.h:56
bool read(tesseract::TFile *f, std::function< bool(tesseract::TFile *, T *)> cb)
void reserve(int size)
Definition: unicity_table.h:46