tesseract v5.3.3.20231005
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 "errcode.h"
24
25#include "genericvector.h"
26
27#include <functional> // for std::function
28
29namespace 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.
36template <typename T>
38public:
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
65 T &operator[](size_t id) {
66 return table_[id];
67 }
68 const T &operator[](size_t id) const {
69 return table_[id];
70 }
71
75 int get_index(T object) const {
76 return table_.get_index(object);
77 }
78
80 int push_back(T object) {
81 auto idx = get_index(object);
82 if (idx == -1) {
83 idx = table_.push_back(object);
84 }
85 return idx;
86 }
87
90 void set_clear_callback(const std::function<void(T)> &cb) {
91 table_.set_clear_callback(cb);
92 }
93
98 void clear() {
99 table_.clear();
100 }
101
104 void move(UnicityTable<T> *from) {
105 table_.move(&from->table_);
106 }
107
112 bool write(FILE *f, const std::function<bool(FILE *, const T &)> &cb) const {
113 return table_.write(f, cb);
114 }
115 bool read(tesseract::TFile *f, const std::function<bool(tesseract::TFile *, T *)> &cb) {
116 return table_.read(f, cb);
117 }
118
119private:
120 GenericVector<T> table_;
121};
122
123} // namespace tesseract
124
125#endif // TESSERACT_CCUTIL_UNICITY_TABLE_H_
bool write(FILE *f, const std::function< bool(FILE *, const T &)> &cb) const
T & operator[](size_t id)
Definition: unicity_table.h:65
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
bool read(tesseract::TFile *f, const std::function< bool(tesseract::TFile *, T *)> &cb)
int push_back(T object)
Add an element in the table.
Definition: unicity_table.h:80
const T & at(int id) const
Return the object from an id.
Definition: unicity_table.h:56
int get_index(T object) const
Definition: unicity_table.h:75
void move(UnicityTable< T > *from)
const T & operator[](size_t id) const
Definition: unicity_table.h:68
void set_clear_callback(const std::function< void(T)> &cb)
Definition: unicity_table.h:90
void reserve(int size)
Definition: unicity_table.h:46