LTI-Lib latest version v1.9 - last update 10 Apr 2010

ltiPriorityQueue.h

00001 /*
00002  * Copyright (C) 2003, 2004, 2005, 2006
00003  * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany
00004  *
00005  * This file is part of the LTI-Computer Vision Library (LTI-Lib)
00006  *
00007  * The LTI-Lib is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License (LGPL)
00009  * as published by the Free Software Foundation; either version 2.1 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * The LTI-Lib is distributed in the hope that it will be
00013  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00014  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with the LTI-Lib; see the file LICENSE.  If
00019  * not, write to the Free Software Foundation, Inc., 59 Temple Place -
00020  * Suite 330, Boston, MA 02111-1307, USA.
00021  */
00022 
00023 
00024 /*--------------------------------------------------------------------
00025  * project ....: LTI-Lib: Image Processing and Computer Vision Library
00026  * file .......: ltiPriorityQueue.h
00027  * authors ....: Pablo Alvarado
00028  * organization: LTI, RWTH Aachen
00029  * creation ...: 24.08.2003
00030  * revisions ..: $Id: ltiPriorityQueue.h,v 1.6 2006/02/08 12:39:43 ltilib Exp $
00031  */
00032 
00033 #ifndef _LTI_PRIORITY_QUEUE_H_
00034 #define _LTI_PRIORITY_QUEUE_H_
00035 
00036 #include "ltiMathObject.h"
00037 #include "ltiSTLIoInterface.h"
00038 
00039 #undef _LTI_DEBUG
00040 // #define _LTI_DEBUG 2
00041 #include "ltiDebug.h"
00042 
00043 namespace lti {
00044 
00045 
00046   /**
00047    * Simple priority queue data class.
00048    *
00049    * The main difference with the std::priority_queue is that when you insert
00050    * an element in the queue, you get an identification token (positive
00051    * integer value), that will uniquely identify the element in the queue as
00052    * long as the element exists.  This is used to access the specific element
00053    * independently of its position in the queue, allowing to change its key or
00054    * data, changing its positions, but still keeping a valid reference to it.
00055    *
00056    * The queue is always sorted in increasing order of the key.  This means
00057    * that the smallest key is always on the top of the queue.
00058    *
00059    * Removing elements of the queue (pop() or erase()) are O(1) operations.
00060    * Inserting an elements is a O(n) operation.  Therefore, a specialized
00061    * method to create the queue at once (create()) is provided, which will
00062    * build the queue in O(n*log(n)), instead of the O(n^2) required inserting
00063    * the elements one by one.
00064    *
00065    * The type T is the type for the key.  The queue is sorted with
00066    * respect to this key.
00067    *
00068    * The type U is the type of the data contained in each element.  
00069    */
00070   template<class T,class U>
00071   class priorityQueue : public mathObject {
00072   public:
00073     
00074     /**
00075      * index type used to reference elements in the queue
00076      */
00077     typedef int index_type;
00078     
00079     /**
00080      * constructor
00081      * @param inv key used to indicate an invalid queue entry.  Entries
00082      *            are for example invalid, if they were removed or "poped"
00083      *            from the queue.
00084      */
00085     priorityQueue(const T& inv);
00086     
00087     /**
00088      * copy constructor
00089      */
00090     priorityQueue(const priorityQueue<T,U>& other);
00091 
00092     /**
00093      * destructor
00094      */
00095     virtual ~priorityQueue();
00096     
00097     /**
00098      * insert a new node with the given key and data and return
00099      * a pointer to the created node
00100      * @param key key of the element
00101      * @param data data of the element
00102      */
00103     index_type insert(const T& key,const U& data);
00104     
00105     /**
00106      * remove the node with the given id
00107      */
00108     void erase(const index_type id);
00109     
00110     /**
00111      * update the key of an element without changing its id
00112      */
00113     void update(const index_type id,
00114                 const T& newKey);
00115     
00116     /**
00117      * update the key of an element without changing its id
00118      */
00119     void update(const index_type id,
00120                 const T& newKey,
00121                 const U& data);
00122     
00123     /**
00124      * Check if the given id is valid
00125      */
00126     bool valid(const index_type id) const;
00127     
00128     /**
00129      * Get the key for the given index id.  You must ensure first that the
00130      * id is valid
00131      */
00132     const T& getKey(const index_type id) const;
00133 
00134     /**
00135      * Get the data for the given index id.  You must ensure first that the
00136      * id is valid
00137      */
00138     const U& getData(const index_type id) const;
00139 
00140     /**
00141      * Change the data content of an element indexed by the given id.
00142      * You must first ensure that the id is valid.
00143      * @param id identification key for the element to be modified.
00144      * @param newData new data to be written in the element
00145      * @return true if successful, false otherwise.
00146      */
00147     bool setData(const index_type id,const U& newData);
00148 
00149     /**
00150      * return true if the queue is empty
00151      */
00152     bool empty() const;
00153     
00154     /**
00155      * create the queue using the given data.  The ids will be the original
00156      * element index
00157      */
00158     void create(const std::vector<T>& key,
00159                 const std::vector<U>& data);
00160     
00161     /**
00162      * create the queue using the given data.  The ids will be the original
00163      * element index
00164      */
00165     void create(const std::vector< std::pair<T,U> >& data);
00166 
00167     /**
00168      * return reference to element at the front
00169      */
00170     const std::pair<T,U>& front() const;
00171     
00172     /**
00173      * copy all data in the priority queue into the given std::vector.
00174      */
00175     void getData(std::vector< std::pair<T,U> >& d) const;
00176     
00177     /**
00178      * remove the first element of the queue
00179      */
00180     void pop();
00181     
00182     /**
00183      * remove all elements from the priority queue
00184      */
00185     void clear();
00186     
00187     /**
00188      * copy the priority queue
00189      * @param other the priority queue to be copied.
00190      * @return a reference to the current object
00191      */
00192     priorityQueue<T,U>& copy(const priorityQueue<T,U>& other);
00193 
00194     /**
00195      * copy the priority queue
00196      * @param other the priority queue to be copied.
00197      * @return a reference to the current object
00198      */
00199     priorityQueue<T,U>& operator=(const priorityQueue<T,U>& other);
00200 
00201     /**
00202      * returns a copy of this object
00203      */
00204     virtual mathObject* clone() const;
00205 
00206     /**
00207      * returns name of this type
00208      */
00209     const char* getTypeName() const;
00210 
00211     /**
00212      * write the priority queue
00213      */
00214     virtual bool write(ioHandler& handler,const bool complete=true) const;
00215     
00216     /**
00217      * read the priority queue
00218      */
00219     virtual bool read(ioHandler& handler,const bool complete=true);
00220     
00221     /**
00222      * write all attributes of the class
00223      *
00224      * To serialize a priority queue, not all data is necessary, but
00225      * for debugging purposes you might want to have all internals written.
00226      * This method writes everything.
00227      */
00228     virtual bool writeAll(ioHandler& handler,const bool complete=true) const;
00229     
00230     /**
00231      * read the output of the writeAll method.
00232      */
00233     virtual bool readAll(ioHandler& handler,const bool complete=true);
00234 
00235   protected:
00236     
00237     /**
00238      * debugging method to check the consistency of the priority queue
00239      */
00240     bool checkConsistency() const;
00241     
00242     /**
00243      * code used to indicate a removed element'
00244      */
00245     T invalid;
00246     
00247     /**
00248      * the real data.  Some of its elements can be removed, so the real
00249      * number of elements is NOT data.size() but numElements.  The data
00250      * here is sorted.  Because the indices of the elements will change
00251      * each time an element is inserted/removed, an equivalences vector
00252      * will keep track of the element movements.
00253      */
00254     std::vector< std::pair<T,U> > data;
00255     
00256     /**
00257      * number of elements in data
00258      */
00259     int numElements;
00260     
00261     /**
00262      * top of the stack.  This pointer MUST always point to the first valid 
00263      * element!
00264      */
00265     int stackPtr;
00266     
00267     /**
00268      * number of elements removed until now
00269      */
00270     int numRemoved;
00271     
00272     /**
00273      * get the real element position in the data vector given the
00274      * original index.
00275      */
00276     std::vector<int> idToIndex;
00277     
00278     /**
00279      * get the real element position in the data vector given the
00280      * original index.
00281      */
00282     std::vector<int> indexToId;
00283   };
00284   
00285 
00286 }
00287 
00288 #include "ltiPriorityQueue_template.h"
00289 #include "ltiUndebug.h"
00290 #endif

Generated on Sat Apr 10 15:25:59 2010 for LTI-Lib by Doxygen 1.6.1