tesseract  3.03
/usr/local/google/home/jbreiden/tesseract-ocr-read-only/ccutil/kdpair.h
Go to the documentation of this file.
00001 // Copyright 2012 Google Inc. All Rights Reserved.
00002 // Author: rays@google.com (Ray Smith)
00004 // File:        kdpair.h
00005 // Description: Template pair class like STL pair but geared towards
00006 //              the Key+Data design pattern in which some data needs
00007 //              to be sorted or kept in a heap sorted on some separate key.
00008 // Author:      Ray Smith.
00009 // Created:     Thu Mar 15 14:48:05 PDT 2012
00010 //
00011 // (C) Copyright 2012, Google Inc.
00012 // Licensed under the Apache License, Version 2.0 (the "License");
00013 // you may not use this file except in compliance with the License.
00014 // You may obtain a copy of the License at
00015 // http://www.apache.org/licenses/LICENSE-2.0
00016 // Unless required by applicable law or agreed to in writing, software
00017 // distributed under the License is distributed on an "AS IS" BASIS,
00018 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00019 // See the License for the specific language governing permissions and
00020 // limitations under the License.
00021 //
00023 
00024 #ifndef TESSERACT_CCUTIL_KDPAIR_H_
00025 #define TESSERACT_CCUTIL_KDPAIR_H_
00026 
00027 #include "genericvector.h"
00028 
00029 namespace tesseract {
00030 
00031 // A useful base struct to facilitate the common operation of sorting a vector
00032 // of simple or smart-pointer data using a separate key. Similar to STL pair.
00033 template <typename Key, typename Data>
00034 struct KDPair {
00035   KDPair() {}
00036   KDPair(Key k, Data d) : data(d), key(k) {}
00037 
00038   int operator==(const KDPair<Key, Data>& other) const {
00039     return key == other.key;
00040   }
00041 
00042   // WARNING! Keep data as the first element! KDPairInc and KDPairDec depend
00043   // on the order of these elements so they can downcast pointers appropriately
00044   // for use by GenericHeap::Reshuffle.
00045   Data data;
00046   Key key;
00047 };
00048 // Specialization of KDPair to provide operator< for sorting in increasing order
00049 // and recasting of data pointers for use with DoublePtr.
00050 template <typename Key, typename Data>
00051 struct KDPairInc : public KDPair<Key, Data> {
00052   KDPairInc() {}
00053   KDPairInc(Key k, Data d) : KDPair<Key, Data>(k, d) {}
00054   // Operator< facilitates sorting in increasing order.
00055   int operator<(const KDPairInc<Key, Data>& other) const {
00056     return this->key < other.key;
00057   }
00058   // Returns the input Data pointer recast to a KDPairInc pointer.
00059   // Just casts a pointer to the first element to a pointer to the whole struct.
00060   static KDPairInc* RecastDataPointer(Data* data_ptr) {
00061     return reinterpret_cast<KDPairInc*>(data_ptr);
00062   }
00063 };
00064 // Specialization of KDPair to provide operator< for sorting in decreasing order
00065 // and recasting of data pointers for use with DoublePtr.
00066 template <typename Key, typename Data>
00067 struct KDPairDec : public KDPair<Key, Data> {
00068   KDPairDec() {}
00069   KDPairDec(Key k, Data d) : KDPair<Key, Data>(k, d) {}
00070   // Operator< facilitates sorting in decreasing order by using operator> on
00071   // the key values.
00072   int operator<(const KDPairDec<Key, Data>& other) const {
00073     return this->key > other.key;
00074   }
00075   // Returns the input Data pointer recast to a KDPairDec pointer.
00076   // Just casts a pointer to the first element to a pointer to the whole struct.
00077   static KDPairDec* RecastDataPointer(Data* data_ptr) {
00078     return reinterpret_cast<KDPairDec*>(data_ptr);
00079   }
00080 };
00081 
00082 // A useful base class to facilitate the common operation of sorting a vector
00083 // of owned pointer data using a separate key. This class owns its data pointer,
00084 // deleting it when it has finished with it, and providing copy constructor and
00085 // operator= that have move semantics so that the data does not get copied and
00086 // only a single instance of KDPtrPair holds a specific data pointer.
00087 template <typename Key, typename Data>
00088 class KDPtrPair {
00089  public:
00090   KDPtrPair() : data_(NULL) {}
00091   KDPtrPair(Key k, Data* d) : data_(d), key_(k) {}
00092   // Copy constructor steals the pointer from src and NULLs it in src, thereby
00093   // moving the (single) ownership of the data.
00094   KDPtrPair(KDPtrPair& src) : data_(src.data_), key_(src.key_) {
00095     src.data_ = NULL;
00096   }
00097   // Destructor deletes data, assuming it is the sole owner.
00098   ~KDPtrPair() {
00099     delete this->data_;
00100     this->data_ = NULL;
00101   }
00102   // Operator= steals the pointer from src and NULLs it in src, thereby
00103   // moving the (single) ownership of the data.
00104   void operator=(KDPtrPair& src) {
00105     delete this->data_;
00106     this->data_ = src.data_;
00107     src.data_ = NULL;
00108     this->key_ = src.key_;
00109   }
00110 
00111   int operator==(const KDPtrPair<Key, Data>& other) const {
00112     return key_ == other.key_;
00113   }
00114 
00115   // Accessors.
00116   const Key& key() const {
00117     return key_;
00118   }
00119   void set_key(const Key& new_key) {
00120     key_ = new_key;
00121   }
00122   const Data* data() const {
00123     return data_;
00124   }
00125   // Sets the data pointer, taking ownership of the data.
00126   void set_data(Data* new_data) {
00127     delete data_;
00128     data_ = new_data;
00129   }
00130   // Relinquishes ownership of the data pointer (setting it to NULL).
00131   Data* extract_data() {
00132     Data* result = data_;
00133     data_ = NULL;
00134     return result;
00135   }
00136 
00137  private:
00138   // Data members are private to keep deletion of data_ encapsulated.
00139   Data* data_;
00140   Key key_;
00141 };
00142 // Specialization of KDPtrPair to provide operator< for sorting in increasing
00143 // order.
00144 template <typename Key, typename Data>
00145 struct KDPtrPairInc : public KDPtrPair<Key, Data> {
00146   // Since we are doing non-standard stuff we have to duplicate *all* the
00147   // constructors and operator=.
00148   KDPtrPairInc() : KDPtrPair<Key, Data>() {}
00149   KDPtrPairInc(Key k, Data* d) : KDPtrPair<Key, Data>(k, d) {}
00150   KDPtrPairInc(KDPtrPairInc& src) : KDPtrPair<Key, Data>(src) {}
00151   void operator=(KDPtrPairInc& src) {
00152     KDPtrPair<Key, Data>::operator=(src);
00153   }
00154   // Operator< facilitates sorting in increasing order.
00155   int operator<(const KDPtrPairInc<Key, Data>& other) const {
00156     return this->key() < other.key();
00157   }
00158 };
00159 // Specialization of KDPtrPair to provide operator< for sorting in decreasing
00160 // order.
00161 template <typename Key, typename Data>
00162 struct KDPtrPairDec : public KDPtrPair<Key, Data> {
00163   // Since we are doing non-standard stuff we have to duplicate *all* the
00164   // constructors and operator=.
00165   KDPtrPairDec() : KDPtrPair<Key, Data>() {}
00166   KDPtrPairDec(Key k, Data* d) : KDPtrPair<Key, Data>(k, d) {}
00167   KDPtrPairDec(KDPtrPairDec& src) : KDPtrPair<Key, Data>(src) {}
00168   void operator=(KDPtrPairDec& src) {
00169     KDPtrPair<Key, Data>::operator=(src);
00170   }
00171   // Operator< facilitates sorting in decreasing order by using operator> on
00172   // the key values.
00173   int operator<(const KDPtrPairDec<Key, Data>& other) const {
00174     return this->key() > other.key();
00175   }
00176 };
00177 
00178 // Specialization for a pair of ints in increasing order.
00179 typedef KDPairInc<int, int> IntKDPair;
00180 
00181 // Vector of IntKDPair.
00182 class KDVector : public GenericVector<IntKDPair> {
00183   // TODO(rays) Add some code to manipulate a KDVector. For now there
00184   // is nothing and this class is effectively a specialization typedef.
00185 };
00186 
00187 }  // namespace tesseract
00188 
00189 #endif  // TESSERACT_CCUTIL_KDPAIR_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines