/////////////////////////////////////////////////////////////////////// // File: unicity_table.h // Description: a class to uniquify objects, manipulating them using integers // ids. // Author: Samuel Charron // // (C) Copyright 2006, Google Inc. // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // http://www.apache.org/licenses/LICENSE-2.0 // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // /////////////////////////////////////////////////////////////////////// #ifndef TESSERACT_CCUTIL_UNICITY_TABLE_H_ #define TESSERACT_CCUTIL_UNICITY_TABLE_H_ #include "tesscallback.h" #include "errcode.h" #include "genericvector.h" // A class to uniquify objects, manipulating them using integers ids. // T requirements: // operator= to add an element // default-constructible: allocating the internal table will call the default // constructor. template class UnicityTable { public: UnicityTable(); /// Clear the structures and deallocate internal structures. ~UnicityTable(); /// Reserve some memory. If there is size or more elements, the table will /// then allocate size * 2 elements. void reserve(int size); /// Return the size used. int size() const; /// Return the object from an id. const T &get(int id) const; // Return the pointer to an object with the given id. T *get_mutable(int id); /// Return the id of the T object. /// This method NEEDS a compare_callback to be passed to /// set_compare_callback. int get_id(T object) const; /// Return true if T is in the table bool contains(T object) const; /// Return true if the id is valid T contains_id(int id) const; /// Add an element in the table int push_back(T object); /// Add a callback to be called to delete the elements when the table took /// their ownership. void set_clear_callback(TessCallback1* cb); /// Add a callback to be called to compare the elements when needed (contains, /// get_id, ...) void set_compare_callback(TessResultCallback2* cb); /// Clear the table, calling the callback function if any. /// All the owned Callbacks are also deleted. /// If you don't want the Callbacks to be deleted, before calling clear, set /// the callback to NULL. void clear(); /// This method clear the current object, then, does a shallow copy of /// its argument, and finally invalidate its argument. void move(UnicityTable* from); /// Read/Write the table to a file. This does _NOT_ read/write the callbacks. /// The Callback given must be permanent since they will be called more than /// once. The given callback will be deleted at the end. /// Returns false on read/write error. bool write(FILE* f, TessResultCallback2* cb) const; bool read(tesseract::TFile* f, TessResultCallback2* cb); private: GenericVector table_; // Mutable because Run method is not const mutable TessResultCallback2* compare_cb_; }; template class UnicityTableEqEq : public UnicityTable { public: UnicityTableEqEq() { UnicityTable::set_compare_callback( NewPermanentTessCallback(tesseract::cmp_eq)); } }; template UnicityTable::UnicityTable() : compare_cb_(0) { } template UnicityTable::~UnicityTable() { clear(); } template int UnicityTable::size() const{ return table_.size(); } // Reserve some memory. If there is size or more elements, the table will // then allocate size * 2 elements. template void UnicityTable::reserve(int size) { table_.reserve(size); } // Return the object from an id. template const T &UnicityTable::get(int id) const { return table_.get(id); } // Returns the pointer to the object with the given id. template T *UnicityTable::get_mutable(int id) { return &(table_.get(id)); } // Return true if the id is valid template T UnicityTable::contains_id(int id) const { return table_.contains_index(id); } // Return the id of the T object. template int UnicityTable::get_id(T object) const { return table_.get_index(object); } // Return true if T is in the table template bool UnicityTable::contains(T object) const { return get_id(object) != -1; } // Add an element in the table template int UnicityTable::push_back(T object) { int idx = get_id(object); if (idx == -1) { idx = table_.push_back(object); } return idx; } // Add a callback to be called to delete the elements when the table took // their ownership. template void UnicityTable::set_clear_callback(TessCallback1* cb) { table_.set_clear_callback(cb); } // Add a callback to be called to delete the elements when the table took // their ownership. template void UnicityTable::set_compare_callback(TessResultCallback2* cb) { table_.set_compare_callback(cb); compare_cb_ = cb; } // Clear the table, calling the callback function if any. template void UnicityTable::clear() { table_.clear(); } template bool UnicityTable::write( FILE* f, TessResultCallback2* cb) const { return table_.write(f, cb); } template bool UnicityTable::read( tesseract::TFile* f, TessResultCallback2* cb) { return table_.read(f, cb); } // This method clear the current object, then, does a shallow copy of // its argument, and finally invalidate its argument. template void UnicityTable::move(UnicityTable* from) { table_.move(&from->table_); } #endif // TESSERACT_CCUTIL_UNICITY_TABLE_H_