Tesseract
3.02
|
00001 00002 // File: UnicityTable.h 00003 // Description: a class to uniquify objects, manipulating them using integers 00004 // ids. 00005 // Author: Samuel Charron 00006 // 00007 // (C) Copyright 2006, Google Inc. 00008 // Licensed under the Apache License, Version 2.0 (the "License"); 00009 // you may not use this file except in compliance with the License. 00010 // You may obtain a copy of the License at 00011 // http://www.apache.org/licenses/LICENSE-2.0 00012 // Unless required by applicable law or agreed to in writing, software 00013 // distributed under the License is distributed on an "AS IS" BASIS, 00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 // See the License for the specific language governing permissions and 00016 // limitations under the License. 00017 // 00019 00020 #ifndef TESSERACT_CCUTIL_UNICITY_TABLE_H_ 00021 #define TESSERACT_CCUTIL_UNICITY_TABLE_H_ 00022 00023 #include "tesscallback.h" 00024 #include "errcode.h" 00025 #include "genericvector.h" 00026 00027 // A class to uniquify objects, manipulating them using integers ids. 00028 // T requirements: 00029 // operator= to add an element 00030 // default-constructible: allocating the internal table will call the default 00031 // constructor. 00032 template <typename T> 00033 class UnicityTable { 00034 public: 00035 UnicityTable(); 00037 ~UnicityTable(); 00038 00041 void reserve(int size); 00042 00044 int size() const; 00045 00047 const T &get(int id) const; 00048 00049 // Return the pointer to an object with the given id. 00050 T *get_mutable(int id); 00051 00055 int get_id(T object) const; 00056 00058 bool contains(T object) const; 00059 00061 T contains_id(int id) const; 00062 00064 int push_back(T object); 00065 00068 void set_clear_callback(TessCallback1<T>* cb); 00069 00072 void set_compare_callback(TessResultCallback2<bool, T const &, T const &>* cb); 00073 00078 void clear(); 00079 00082 void move(UnicityTable<T>* from); 00083 00088 bool write(FILE* f, TessResultCallback2<bool, FILE*, T const &>* cb) const; 00090 bool read(FILE* f, TessResultCallback3<bool, FILE*, T*, bool>* cb, bool swap); 00091 00092 private: 00093 GenericVector<T> table_; 00094 // Mutable because Run method is not const 00095 mutable TessResultCallback2<bool, T const &, T const &>* compare_cb_; 00096 }; 00097 00098 template <typename T> 00099 class UnicityTableEqEq : public UnicityTable<T> { 00100 public: 00101 UnicityTableEqEq() { 00102 UnicityTable<T>::set_compare_callback( 00103 NewPermanentTessCallback(tesseract::cmp_eq<T>)); 00104 } 00105 }; 00106 00107 template <typename T> 00108 UnicityTable<T>::UnicityTable() : 00109 compare_cb_(0) { 00110 } 00111 00112 00113 template <typename T> 00114 UnicityTable<T>::~UnicityTable() { 00115 clear(); 00116 } 00117 00118 template <typename T> 00119 int UnicityTable<T>::size() const{ 00120 return table_.size(); 00121 } 00122 00123 // Reserve some memory. If there is size or more elements, the table will 00124 // then allocate size * 2 elements. 00125 template <typename T> 00126 void UnicityTable<T>::reserve(int size) { 00127 table_.reserve(size); 00128 } 00129 00130 // Return the object from an id. 00131 template <typename T> 00132 const T &UnicityTable<T>::get(int id) const { 00133 return table_.get(id); 00134 } 00135 // Returns the pointer to the object with the given id. 00136 template <typename T> 00137 T *UnicityTable<T>::get_mutable(int id) { 00138 return &(table_.get(id)); 00139 } 00140 // Return true if the id is valid 00141 template <typename T> 00142 T UnicityTable<T>::contains_id(int id) const { 00143 return table_.contains_index(id); 00144 } 00145 00146 // Return the id of the T object. 00147 template <typename T> 00148 int UnicityTable<T>::get_id(T object) const { 00149 return table_.get_index(object); 00150 } 00151 00152 // Return true if T is in the table 00153 template <typename T> 00154 bool UnicityTable<T>::contains(T object) const { 00155 return get_id(object) != -1; 00156 } 00157 00158 // Add an element in the table 00159 template <typename T> 00160 int UnicityTable<T>::push_back(T object) { 00161 int idx = get_id(object); 00162 if (idx == -1) { 00163 idx = table_.push_back(object); 00164 } 00165 return idx; 00166 } 00167 00168 // Add a callback to be called to delete the elements when the table took 00169 // their ownership. 00170 template <typename T> 00171 void UnicityTable<T>::set_clear_callback(TessCallback1<T>* cb) { 00172 table_.set_clear_callback(cb); 00173 } 00174 00175 // Add a callback to be called to delete the elements when the table took 00176 // their ownership. 00177 template <typename T> 00178 void UnicityTable<T>::set_compare_callback(TessResultCallback2<bool, T const &, T const &>* cb) { 00179 table_.set_compare_callback(cb); 00180 compare_cb_ = cb; 00181 } 00182 00183 // Clear the table, calling the callback function if any. 00184 template <typename T> 00185 void UnicityTable<T>::clear() { 00186 table_.clear(); 00187 } 00188 00189 template <typename T> 00190 bool UnicityTable<T>::write( 00191 FILE* f, TessResultCallback2<bool, FILE*, T const &>* cb) const { 00192 return table_.write(f, cb); 00193 } 00194 00195 template <typename T> 00196 bool UnicityTable<T>::read( 00197 FILE* f, TessResultCallback3<bool, FILE*, T*, bool>* cb, bool swap) { 00198 return table_.read(f, cb, swap); 00199 } 00200 00201 // This method clear the current object, then, does a shallow copy of 00202 // its argument, and finally invalidate its argument. 00203 template <typename T> 00204 void UnicityTable<T>::move(UnicityTable<T>* from) { 00205 table_.move(&from->table_); 00206 } 00207 00208 #endif // TESSERACT_CCUTIL_UNICITY_TABLE_H_