Wm4TMinHeap.h

Go to the documentation of this file.
00001 // Wild Magic Source Code
00002 // David Eberly
00003 // http://www.geometrictools.com
00004 // Copyright (c) 1998-2007
00005 //
00006 // This library is free software; you can redistribute it and/or modify it
00007 // under the terms of the GNU Lesser General Public License as published by
00008 // the Free Software Foundation; either version 2.1 of the License, or (at
00009 // your option) any later version.  The license is available for reading at
00010 // either of the locations:
00011 //     http://www.gnu.org/copyleft/lgpl.html
00012 //     http://www.geometrictools.com/License/WildMagicLicense.pdf
00013 // The license applies to versions 0 through 4 of Wild Magic.
00014 //
00015 // Version: 4.0.2 (2006/10/15)
00016 
00017 #ifndef WM4TMINHEAP_H
00018 #define WM4TMINHEAP_H
00019 
00020 #include "Wm4System.h"
00021 
00022 namespace Wm4
00023 {
00024 
00025 template <typename Generator, typename Real> class TMinHeap;
00026 
00027 template <typename Generator, typename Real>
00028 class TMinHeapRecord
00029 {
00030 public:
00031     TMinHeapRecord ();
00032     ~TMinHeapRecord ();
00033 
00034     Generator GetGenerator () const;
00035     Real GetValue () const;
00036 
00037 private:
00038     friend class TMinHeap<Generator,Real>;
00039 
00040     Generator m_tGenerator;
00041     Real m_fValue;
00042     int m_iIndex;
00043 };
00044 
00045 template <typename Generator, typename Real>
00046 class TMinHeap
00047 {
00048 public:
00049     TMinHeap (int iMaxQuantity, int iGrowBy);
00050     ~TMinHeap ();
00051 
00052     // Member access.
00053     int GetMaxQuantity () const;
00054     int GetGrowBy () const;
00055     int GetQuantity () const;
00056     const TMinHeapRecord<Generator,Real>* GetRecord (int i) const;
00057 
00058     // Insert into the heap the number fValue that corresponds to the object
00059     // identified by iGenerator.  The return value is a pointer to the heap
00060     // record storing the information.
00061     const TMinHeapRecord<Generator,Real>* Insert (Generator tGenerator,
00062         Real fValue);
00063 
00064     // Remove the root of the heap.  The root contains the minimum value of
00065     // all heap elements.  The root information is returned by the function's
00066     // output parameters.
00067     void Remove (Generator& rtGenerator, Real& rfValue);
00068 
00069     // The value of a heap record must be modified through this function call.
00070     // The side effect is that the heap must be updated accordingly to
00071     // accommodate the new value.
00072     void Update (const TMinHeapRecord<Generator,Real>* pkConstRecord,
00073         Real fValue);
00074 
00075     // Support for debugging.  The first two functions check if the array of
00076     // records really do form a heap.  The last function prints the heap
00077     // to a file.
00078     bool IsValid (int iStart, int iFinal);
00079     bool IsValid ();
00080     void Print (const char* acFilename);
00081 
00082 private:
00083     // The actual record storage, allocated in one large chunk.
00084     int m_iMaxQuantity, m_iGrowBy, m_iQuantity;
00085     TMinHeapRecord<Generator,Real>* m_akRecords;
00086 
00087     // Pointers to the records in storage.  The two-level system avoids the
00088     // large number of allocations and deallocations that would occur if each
00089     // element of m_apkRecord were to be allocated/deallocated individually.
00090     TMinHeapRecord<Generator,Real>** m_apkRecords;
00091 };
00092 
00093 }
00094 
00095 #include "Wm4TMinHeap.inl"
00096 
00097 #endif

Generated on Wed Nov 23 19:01:09 2011 for FreeCAD by  doxygen 1.6.1