/src/tesseract/src/ccutil/unicity_table.h
Line | Count | Source (jump to first uncovered line) |
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 | | // |
18 | | /////////////////////////////////////////////////////////////////////// |
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: |
39 | | /// Clear the structures and deallocate internal structures. |
40 | 2 | ~UnicityTable() { |
41 | 2 | clear(); |
42 | 2 | } tesseract::UnicityTable<tesseract::FontInfo>::~UnicityTable() Line | Count | Source | 40 | 2 | ~UnicityTable() { | 41 | 2 | clear(); | 42 | 2 | } |
Unexecuted instantiation: tesseract::UnicityTable<std::__1::vector<int, std::__1::allocator<int> > >::~UnicityTable() Unexecuted instantiation: tesseract::UnicityTable<int>::~UnicityTable() |
43 | | |
44 | | /// Reserve some memory. If there is size or more elements, the table will |
45 | | /// then allocate size * 2 elements. |
46 | | void reserve(int size) { |
47 | | table_.reserve(size); |
48 | | } |
49 | | |
50 | | /// Return the size used. |
51 | 33.7M | int size() const { |
52 | 33.7M | return table_.size(); |
53 | 33.7M | } tesseract::UnicityTable<tesseract::FontInfo>::size() const Line | Count | Source | 51 | 33.7M | int size() const { | 52 | 33.7M | return table_.size(); | 53 | 33.7M | } |
Unexecuted instantiation: tesseract::UnicityTable<int>::size() const |
54 | | |
55 | | /// Return the object from an id. |
56 | 184M | const T &at(int id) const { |
57 | 184M | return table_.at(id); |
58 | 184M | } tesseract::UnicityTable<tesseract::FontInfo>::at(int) const Line | Count | Source | 56 | 24.8M | const T &at(int id) const { | 57 | 24.8M | return table_.at(id); | 58 | 24.8M | } |
tesseract::UnicityTable<std::__1::vector<int, std::__1::allocator<int> > >::at(int) const Line | Count | Source | 56 | 159M | const T &at(int id) const { | 57 | 159M | return table_.at(id); | 58 | 159M | } |
|
59 | | |
60 | | // Return the pointer to an object with the given id. |
61 | 85.4M | T &at(int id) { |
62 | 85.4M | return table_.at(id); |
63 | 85.4M | } tesseract::UnicityTable<tesseract::FontInfo>::at(int) Line | Count | Source | 61 | 104k | T &at(int id) { | 62 | 104k | return table_.at(id); | 63 | 104k | } |
tesseract::UnicityTable<std::__1::vector<int, std::__1::allocator<int> > >::at(int) Line | Count | Source | 61 | 85.3M | T &at(int id) { | 62 | 85.3M | return table_.at(id); | 63 | 85.3M | } |
|
64 | | |
65 | 0 | T &operator[](size_t id) { |
66 | 0 | return table_[id]; |
67 | 0 | } |
68 | | const T &operator[](size_t id) const { |
69 | | return table_[id]; |
70 | | } |
71 | | |
72 | | /// Return the id of the T object. |
73 | | /// This method NEEDS a compare_callback to be passed to |
74 | | /// set_compare_callback. |
75 | 2.16k | int get_index(T object) const { |
76 | 2.16k | return table_.get_index(object); |
77 | 2.16k | } tesseract::UnicityTable<tesseract::FontInfo>::get_index(tesseract::FontInfo) const Line | Count | Source | 75 | 2.16k | int get_index(T object) const { | 76 | 2.16k | return table_.get_index(object); | 77 | 2.16k | } |
Unexecuted instantiation: tesseract::UnicityTable<std::__1::vector<int, std::__1::allocator<int> > >::get_index(std::__1::vector<int, std::__1::allocator<int> >) const |
78 | | |
79 | | /// Add an element in the table |
80 | 854 | int push_back(T object) { |
81 | 854 | auto idx = get_index(object); |
82 | 854 | if (idx == -1) { |
83 | 854 | idx = table_.push_back(std::move(object)); |
84 | 854 | } |
85 | 854 | return idx; |
86 | 854 | } tesseract::UnicityTable<tesseract::FontInfo>::push_back(tesseract::FontInfo) Line | Count | Source | 80 | 854 | int push_back(T object) { | 81 | 854 | auto idx = get_index(object); | 82 | 854 | if (idx == -1) { | 83 | 854 | idx = table_.push_back(std::move(object)); | 84 | 854 | } | 85 | 854 | return idx; | 86 | 854 | } |
Unexecuted instantiation: tesseract::UnicityTable<std::__1::vector<int, std::__1::allocator<int> > >::push_back(std::__1::vector<int, std::__1::allocator<int> >) |
87 | | |
88 | | /// Add a callback to be called to delete the elements when the table took |
89 | | /// their ownership. |
90 | 2 | void set_clear_callback(const std::function<void(T)> &cb) { |
91 | 2 | table_.set_clear_callback(cb); |
92 | 2 | } |
93 | | |
94 | | /// Clear the table, calling the callback function if any. |
95 | | /// All the owned Callbacks are also deleted. |
96 | | /// If you don't want the Callbacks to be deleted, before calling clear, set |
97 | | /// the callback to nullptr. |
98 | 2 | void clear() { |
99 | 2 | table_.clear(); |
100 | 2 | } tesseract::UnicityTable<tesseract::FontInfo>::clear() Line | Count | Source | 98 | 2 | void clear() { | 99 | 2 | table_.clear(); | 100 | 2 | } |
Unexecuted instantiation: tesseract::UnicityTable<std::__1::vector<int, std::__1::allocator<int> > >::clear() Unexecuted instantiation: tesseract::UnicityTable<int>::clear() |
101 | | |
102 | | /// This method clear the current object, then, does a shallow copy of |
103 | | /// its argument, and finally invalidate its argument. |
104 | | void move(UnicityTable<T> *from) { |
105 | | table_.move(&from->table_); |
106 | | } |
107 | | |
108 | | /// Read/Write the table to a file. This does _NOT_ read/write the callbacks. |
109 | | /// The Callback given must be permanent since they will be called more than |
110 | | /// once. The given callback will be deleted at the end. |
111 | | /// Returns false on read/write error. |
112 | 0 | bool write(FILE *f, const std::function<bool(FILE *, const T &)> &cb) const { |
113 | 0 | return table_.write(f, cb); |
114 | 0 | } Unexecuted instantiation: tesseract::UnicityTable<tesseract::FontInfo>::write(_IO_FILE*, std::__1::function<bool (_IO_FILE*, tesseract::FontInfo const&)> const&) const Unexecuted instantiation: tesseract::UnicityTable<std::__1::vector<int, std::__1::allocator<int> > >::write(_IO_FILE*, std::__1::function<bool (_IO_FILE*, std::__1::vector<int, std::__1::allocator<int> > const&)> const&) const |
115 | 6 | bool read(tesseract::TFile *f, const std::function<bool(tesseract::TFile *, T *)> &cb) { |
116 | 6 | return table_.read(f, cb); |
117 | 6 | } tesseract::UnicityTable<tesseract::FontInfo>::read(tesseract::TFile*, std::__1::function<bool (tesseract::TFile*, tesseract::FontInfo*)> const&) Line | Count | Source | 115 | 4 | bool read(tesseract::TFile *f, const std::function<bool(tesseract::TFile *, T *)> &cb) { | 116 | 4 | return table_.read(f, cb); | 117 | 4 | } |
tesseract::UnicityTable<std::__1::vector<int, std::__1::allocator<int> > >::read(tesseract::TFile*, std::__1::function<bool (tesseract::TFile*, std::__1::vector<int, std::__1::allocator<int> >*)> const&) Line | Count | Source | 115 | 2 | bool read(tesseract::TFile *f, const std::function<bool(tesseract::TFile *, T *)> &cb) { | 116 | 2 | return table_.read(f, cb); | 117 | 2 | } |
|
118 | | |
119 | | private: |
120 | | GenericVector<T> table_; |
121 | | }; |
122 | | |
123 | | } // namespace tesseract |
124 | | |
125 | | #endif // TESSERACT_CCUTIL_UNICITY_TABLE_H_ |