Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/thirdparty/tesseract/src/ccutil/unicity_table.h @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 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 ~UnicityTable() { | |
| 41 clear(); | |
| 42 } | |
| 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 int size() const { | |
| 52 return table_.size(); | |
| 53 } | |
| 54 | |
| 55 /// Return the object from an id. | |
| 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 | |
| 72 /// Return the id of the T object. | |
| 73 /// This method NEEDS a compare_callback to be passed to | |
| 74 /// set_compare_callback. | |
| 75 int get_index(T object) const { | |
| 76 return table_.get_index(object); | |
| 77 } | |
| 78 | |
| 79 /// Add an element in the table | |
| 80 int push_back(T object) { | |
| 81 auto idx = get_index(object); | |
| 82 if (idx == -1) { | |
| 83 idx = table_.push_back(std::move(object)); | |
| 84 } | |
| 85 return idx; | |
| 86 } | |
| 87 | |
| 88 /// Add a callback to be called to delete the elements when the table took | |
| 89 /// their ownership. | |
| 90 void set_clear_callback(const std::function<void(T)> &cb) { | |
| 91 table_.set_clear_callback(cb); | |
| 92 } | |
| 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 void clear() { | |
| 99 table_.clear(); | |
| 100 } | |
| 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 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 | |
| 119 private: | |
| 120 GenericVector<T> table_; | |
| 121 }; | |
| 122 | |
| 123 } // namespace tesseract | |
| 124 | |
| 125 #endif // TESSERACT_CCUTIL_UNICITY_TABLE_H_ |
