priorityqueue.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 * MeshLab                                                           o o     *
00003 * A versatile mesh processing toolbox                             o     o   *
00004 *                                                                _   O  _   *
00005 * Copyright(C) 2005                                                \/)\/    *
00006 * Visual Computing Lab                                            /\/|      *
00007 * ISTI - Italian National Research Council                           |      *
00008 *                                                                    \      *
00009 * All rights reserved.                                                      *
00010 *                                                                           *
00011 * This program is free software; you can redistribute it and/or modify      *
00012 * it under the terms of the GNU General Public License as published by      *
00013 * the Free Software Foundation; either version 2 of the License, or         *
00014 * (at your option) any later version.                                       *
00015 *                                                                           *
00016 * This program is distributed in the hope that it will be useful,           *
00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
00019 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
00020 * for more details.                                                         *
00021 *                                                                           *
00022 ****************************************************************************/
00023 
00024 #ifndef _PRIORITYQUEUE_H_
00025 #define _PRIORITYQUEUE_H_
00026 
00027 #include <algorithm>
00028 
00029 namespace vcg {
00030 
00033   template <typename Index, typename Weight>
00034   class HeapMaxPriorityQueue
00035   {
00036     struct Element
00037     {
00038       Weight weight;
00039       Index index;
00040     };
00041 
00042 
00043     struct
00044     {
00045       bool operator()(const Element& a, const Element& b) const
00046       {
00047         return a.weight < b.weight;
00048       }
00049     } lessElement;
00050 
00051 
00052     struct
00053     {
00054       bool operator()(const Element& a, const Element& b) const
00055       {
00056         return a.weight > b.weight;
00057       }
00058     } greaterElement;
00059 
00060 
00061 
00062   public:
00063 
00064     HeapMaxPriorityQueue(void)
00065     {
00066       mElements = 0;
00067       mMaxSize = 0;
00068     }
00069 
00070     ~HeapMaxPriorityQueue()
00071     {
00072       if (mElements)
00073         delete[] mElements;
00074     }
00075 
00076 
00077     inline void setMaxSize(int maxSize)
00078     {
00079       if (mMaxSize!=maxSize)
00080       {
00081         mMaxSize = maxSize;
00082         delete[] mElements;
00083         mElements = new Element[mMaxSize];
00084         mpOffsetedElements = (mElements-1);
00085       }
00086       init();
00087     }
00088 
00089     inline void init() { mCount = 0; }
00090 
00091     inline bool isFull() const { return mCount == mMaxSize; }
00092 
00095     inline int getNofElements() const { return mCount; }
00096 
00097     inline Weight getWeight(int i) const { return mElements[i].weight; }
00098     inline Index  getIndex(int i) const { return mElements[i].index; }
00099 
00100     inline Weight getTopWeight() const { return mElements[0].weight; }
00101 
00102     inline void insert(Index index, Weight weight)
00103     {
00104       if (mCount==mMaxSize)
00105       {
00106         if (weight<mElements[0].weight)
00107         {
00108           int j, k;
00109           j = 1;
00110           k = 2;
00111           while (k <= mMaxSize)
00112           {
00113             Element* z = &(mpOffsetedElements[k]);
00114             if ((k < mMaxSize) && (z->weight < mpOffsetedElements[k+1].weight))
00115               z = &(mpOffsetedElements[++k]);
00116 
00117             if(weight >= z->weight)
00118               break;
00119             mpOffsetedElements[j] = *z;
00120             j = k;
00121             k = 2 * j;
00122           }
00123           mpOffsetedElements[j].weight = weight;
00124           mpOffsetedElements[j].index = index;
00125         }
00126       }
00127       else
00128       {
00129         int i, j;
00130         i = ++mCount;
00131         while (i >= 2)
00132         {
00133           j = i >> 1;
00134           Element& y = mpOffsetedElements[j];
00135           if(weight <= y.weight)
00136             break;
00137           mpOffsetedElements[i] = y;
00138           i = j;
00139         }
00140         mpOffsetedElements[i].index = index;
00141         mpOffsetedElements[i].weight = weight;
00142       }
00143     }
00144 
00145     inline void sort(bool ascending = true)
00146     {
00147       if (ascending)
00148         std::sort(mElements, mElements + mCount, lessElement);
00149       else
00150         std::sort(mElements, mElements + mCount, greaterElement);
00151     }
00152 
00153   protected:
00154 
00155     int mCount;
00156     int mMaxSize;
00157     Element* mElements;
00158     Element* mpOffsetedElements;
00159   };
00160 
00161 }
00162 #endif


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:34:40