00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
00028
00029
00030
00031
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
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
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
00124
00125 template <typename T>
00126 void UnicityTable<T>::reserve(int size) {
00127 table_.reserve(size);
00128 }
00129
00130
00131 template <typename T>
00132 const T &UnicityTable<T>::get(int id) const {
00133 return table_.get(id);
00134 }
00135
00136 template <typename T>
00137 T *UnicityTable<T>::get_mutable(int id) {
00138 return &(table_.get(id));
00139 }
00140
00141 template <typename T>
00142 T UnicityTable<T>::contains_id(int id) const {
00143 return table_.contains_index(id);
00144 }
00145
00146
00147 template <typename T>
00148 int UnicityTable<T>::get_id(T object) const {
00149 return table_.get_index(object);
00150 }
00151
00152
00153 template <typename T>
00154 bool UnicityTable<T>::contains(T object) const {
00155 return get_id(object) != -1;
00156 }
00157
00158
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
00169
00170 template <typename T>
00171 void UnicityTable<T>::set_clear_callback(TessCallback1<T>* cb) {
00172 table_.set_clear_callback(cb);
00173 }
00174
00175
00176
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
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
00202
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_