Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Namespace Members | Data Fields | Globals

KDPQueue.h

Go to the documentation of this file.
00001 /* Copyright (C) 2004
00002    Priority Queue for Nearest Neighbor Search in K-Dimension Tree Node Data
00003    Structure Implementation
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with this library; if not, write to the Free Software
00017    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 
00020 #ifndef _KDPQUEUE_H_
00021 #define _KDPQUEUE_H_
00022 
00023 #include "Object.h"
00024 #include <stdio.h>
00025 #include <iostream>
00026 #include <math.h>
00027 #include <float.h>
00028 #include "KDPoint.h"
00029 
00030 
00031 // Template class for node values
00032 // IMPORTANT: class KDPQKey requires =, > operators
00033 template <class KDPQKey, class KDPQElement>
00034 class KDPQueue : public FD::Object
00035 {
00036 public:
00037         /*
00038                 KDPQueue
00039 
00040                 Default constructor
00041         */
00042         KDPQueue()
00043         : m_maxKeys(0), m_numKeys(0), m_keys(NULL), m_elements(NULL)
00044         {
00045 
00046         }
00047 
00048         /*
00049                 KDPQueue
00050 
00051                 Other constructor
00052 
00053                 PARAMETERS
00054                 i_maxKeys : maximum number of elements to keep in queue
00055         */
00056         KDPQueue(int i_maxKeys)
00057         : m_maxKeys(i_maxKeys), m_numKeys(0)
00058         {
00059                 m_keys = new KDPQKey[m_maxKeys+1];
00060                 m_elements = new KDPQElement[m_maxKeys+1];
00061         }
00062 
00063         /*
00064                 ~KDPQueue
00065 
00066                 Destructor
00067         */
00068         ~KDPQueue()
00069         {
00070                 delete [] m_keys;
00071                 delete [] m_elements;
00072                 fflush(stdout);
00073         }
00074 
00075         /*
00076                 printOn
00077 
00078                 Object routine required to print its content
00079 
00080                 PARAMETERS
00081                 out : output stream to print the object content
00082         */
00083         void printOn(std::ostream &out) const
00084         {
00085                 throw new FD::GeneralException ("KDPQueue::printOn : routine not implemented",__FILE__,__LINE__);
00086         }
00087 
00088         /*
00089                 readFrom
00090 
00091                 Object routine required to read an Object content
00092 
00093                 PARAMETERS
00094                 in : input stream to read the object content
00095         */
00096         void readFrom(std::istream &in)
00097         {
00098                 throw new FD::GeneralException ("KDPQueue::readFrom : routine not implemented",__FILE__,__LINE__);
00099         }
00100 
00101         /*
00102                 Insert
00103 
00104                 Insert an element i_element with key i_key in queue.
00105 
00106                 PARAMETERS
00107                 i_key : key value of the element to insert in queue
00108                 i_element : element to insert in queue
00109         */
00110         inline void Insert(KDPQKey i_key, KDPQElement *i_element)
00111         {
00112                 int i;
00113 
00114                 // Shift all larger value than key
00115                 for (i=m_numKeys; i>0; --i) {
00116                         if (m_keys[i-1] > i_key) {
00117                                 m_keys[i] = m_keys[i-1];
00118                                 m_elements[i] = m_elements[i-1];
00119                         }
00120                         else {
00121                                 break;
00122                         }
00123                 }
00124 
00125                 // Store element
00126                 m_keys[i] = i_key;
00127                 m_elements[i] = *i_element;
00128 
00129                 if (m_numKeys < m_maxKeys) {
00130                         m_numKeys++;
00131                 }
00132 
00133         }
00134 
00135         /*
00136                 GetMaxKey
00137 
00138                 Return the current queue maximum key value.
00139 
00140                 RETURNS
00141                 The current queue maximum key value
00142         */
00143         inline KDPQKey GetMaxKey() const
00144         {
00145                 if (m_numKeys == m_maxKeys) {
00146                         return m_keys[m_numKeys-1];
00147                 }
00148                 else {
00149                         return (KDPQKey)DBL_MAX;
00150                 }
00151         }
00152 
00153         /*
00154                 GetMinKey
00155 
00156                 Return the current queue minimum key value.
00157 
00158                 RETURNS
00159                 The current queue minimum key value
00160         */
00161         inline KDPQKey GetMinKey() const
00162         {
00163                 if (m_numKeys > 0) {
00164                         return m_keys[0];
00165                 }
00166                 else {
00167                         return (KDPQKey)DBL_MAX;
00168                 }
00169         }
00170 
00171         /*
00172                 GetIthKey
00173 
00174                 Return the current queue ith key value.
00175 
00176                 PARAMETER:
00177                 i_idx : index to key in queue
00178 
00179                 RETURNS
00180                 The current queue ith key value
00181         */
00182         inline KDPQKey GetIthKey(int i_idx) const
00183         {
00184                 if (i_idx < m_numKeys) {
00185                         return m_keys[i_idx];
00186                 }
00187                 else {
00188                         return (KDPQKey)DBL_MAX;
00189                 }
00190         }
00191 
00192         /*
00193                 GetIthKey
00194 
00195                 Return the current queue ith element value.
00196 
00197                 PARAMETER:
00198                 i_idx : index to element in queue
00199 
00200                 RETURNS
00201                 The current queue ith element value
00202         */
00203         inline KDPQElement *GetIthElement(int i_idx) const
00204         {
00205                 if (i_idx < m_numKeys) {
00206                         return &(m_elements[i_idx]);
00207                 }
00208                 else {
00209                         return NULL;
00210                 }
00211         }
00212 
00213 private:
00214         // Maximum number of keys/elements in queue
00215         int m_maxKeys;
00216         // Current number of keys/elements in queue
00217         int m_numKeys;
00218         // Keys array
00219         KDPQKey *m_keys;
00220         // Elements array
00221         KDPQElement *m_elements;
00222 };
00223 
00224 #endif

Generated on Wed Oct 5 14:36:12 2005 for RobotFlow by  doxygen 1.4.4