$search
00001 /* 00002 * Copyright (c) 2008, Maxim Likhachev 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of the University of Pennsylvania nor the names of its 00014 * contributors may be used to endorse or promote products derived from 00015 * this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00021 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00022 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00023 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00024 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00025 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00026 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00027 * POSSIBILITY OF SUCH DAMAGE. 00028 */ 00029 #ifndef __HEAP_H_ 00030 #define __HEAP_H_ 00031 00032 //the maximum size of the heap 00033 #define HEAPSIZE 20000000 00034 #define HEAPSIZE_INIT 5000 00035 00036 struct HEAPELEMENT 00037 { 00038 AbstractSearchState *heapstate; 00039 CKey key; 00040 }; 00041 typedef struct HEAPELEMENT heapelement; 00042 00043 class CHeap 00044 { 00045 00046 //data 00047 public: 00048 int percolates; //for counting purposes 00049 heapelement* heap; 00050 int currentsize; 00051 int allocated; 00052 00053 //constructors 00054 public: 00055 CHeap(); 00056 ~CHeap(); 00057 00058 //functions 00059 public: 00060 00061 bool emptyheap(); 00062 bool fullheap(); 00063 bool inheap(AbstractSearchState *AbstractSearchState); 00064 CKey getkeyheap(AbstractSearchState *AbstractSearchState); 00065 void makeemptyheap(); 00066 void insertheap(AbstractSearchState *AbstractSearchState, CKey key); 00067 void deleteheap(AbstractSearchState *AbstractSearchState); 00068 void updateheap(AbstractSearchState *AbstractSearchState, CKey NewKey); 00069 AbstractSearchState *getminheap(); 00070 AbstractSearchState *getminheap(CKey& ReturnKey); 00071 CKey getminkeyheap(); 00072 AbstractSearchState *deleteminheap(); 00073 void makeheap(); 00074 00075 private: 00076 void percolatedown(int hole, heapelement tmp); 00077 void percolateup(int hole, heapelement tmp); 00078 void percolateupordown(int hole, heapelement tmp); 00079 00080 void growheap(); 00081 void sizecheck(); 00082 00083 00084 //operators 00085 public: 00086 00087 }; 00088 00089 00090 struct HEAPINTELEMENT 00091 { 00092 AbstractSearchState *heapstate; 00093 int key; 00094 }; 00095 typedef struct HEAPINTELEMENT heapintelement; 00096 00097 00098 class CIntHeap 00099 { 00100 00101 //data 00102 public: 00103 int percolates; //for counting purposes 00104 heapintelement* heap; 00105 int currentsize; 00106 int allocated; 00107 00108 //constructors 00109 public: 00110 CIntHeap(); 00111 CIntHeap(int initial_size); 00112 ~CIntHeap(); 00113 00114 //functions 00115 public: 00116 00117 bool emptyheap(); 00118 bool fullheap(); 00119 bool inheap(AbstractSearchState *AbstractSearchState); 00120 int getkeyheap(AbstractSearchState *AbstractSearchState); 00121 void makeemptyheap(); 00122 void insertheap(AbstractSearchState *AbstractSearchState, int key); 00123 void deleteheap(AbstractSearchState *AbstractSearchState); 00124 void updateheap(AbstractSearchState *AbstractSearchState, int NewKey); 00125 AbstractSearchState *getminheap(); 00126 AbstractSearchState *getminheap(int& ReturnKey); 00127 int getminkeyheap(); 00128 AbstractSearchState *deleteminheap(); 00129 void makeheap(); 00130 00131 private: 00132 void percolatedown(int hole, heapintelement tmp); 00133 void percolateup(int hole, heapintelement tmp); 00134 void percolateupordown(int hole, heapintelement tmp); 00135 00136 void growheap(); 00137 void sizecheck(); 00138 00139 00140 //operators 00141 public: 00142 00143 }; 00144 00145 00146 #endif 00147 00148 00149