00001 /****************************************************************************** 00002 ** Filename: heap.h 00003 ** Purpose: Definition of heap access routines. 00004 ** Author: Dan Johnson 00005 ** History: 3/13/89, DSJ, Created. 00006 ** 00007 ** (c) Copyright Hewlett-Packard Company, 1988. 00008 ** Licensed under the Apache License, Version 2.0 (the "License"); 00009 ** you may not use this file except in compliance with the License. 00010 ** You may obtain a copy of the License at 00011 ** http://www.apache.org/licenses/LICENSE-2.0 00012 ** Unless required by applicable law or agreed to in writing, software 00013 ** distributed under the License is distributed on an "AS IS" BASIS, 00014 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 ** See the License for the specific language governing permissions and 00016 ** limitations under the License. 00017 ******************************************************************************/ 00018 #ifndef HEAP_H 00019 #define HEAP_H 00020 00024 #include "general.h" 00025 #include "cutil.h" 00026 00027 #define HEAPFULL 3000 00028 00029 #define OK 0 00030 #define EMPTY -1 00031 00032 typedef struct 00033 { 00034 FLOAT32 Key; 00035 void *Data; 00036 } 00037 00038 00039 HEAPENTRY; 00040 00041 typedef struct 00042 { 00043 inT32 Size; 00044 inT32 FirstFree; 00045 HEAPENTRY Entry[1]; 00046 } 00047 00048 00049 HEAP; 00050 00054 #define FreeHeap(H) memfree(H) 00055 #define MaxSizeOfHeap(H) (H->Size) 00056 #define SizeOfHeap(H) (H->FirstFree - 1) 00057 #define InitHeap(H) (H->FirstFree = 1) 00058 #define HeapFull(H) ((H)->FirstFree > (H)->Size) 00059 #define HeapEmpty(H) ((H)->FirstFree <= 1) 00060 00061 /* macros for accessing elements in heap by index. The indicies vary from 00062 0 to SizeOfHeap-1. No bounds checking is done. Elements accessed in 00063 this manner are in random order relative to the Key values. These 00064 macros should never be used as the LHS of an assignment statement as this 00065 will corrupt the heap.*/ 00066 #define HeapKeyFor(H,E) ((H)->Entry[(E)+1].Key) 00067 #define HeapDataFor(H,E) ((H)->Entry[(E)+1].Data) 00068 00072 HEAP *MakeHeap(int Size); 00073 00074 int HeapPop(HEAP *Heap, FLOAT32 *Key, void *out_ptr); 00075 00076 int HeapPopWorst(HEAP *Heap, FLOAT32 *Key, void *out_ptr); 00077 00078 void HeapPush(HEAP *Heap, FLOAT32 Key, void *Data); 00079 00080 void HeapStore(HEAP *Heap, HEAPENTRY *Entry); 00081 00082 int GetTopOfHeap(HEAP *Heap, HEAPENTRY *Entry); 00083 00084 void FreeHeapData(HEAP *Heap, void_dest destructor); 00085 00086 /* 00087 #if defined(__STDC__) || defined(__cplusplus) 00088 # define _ARGS(s) s 00089 #else 00090 # define _ARGS(s) () 00091 #endif*/ 00092 00093 /* heap.c 00094 HEAP *MakeHeap 00095 _ARGS((int Size)); 00096 00097 int HeapPop 00098 _ARGS((HEAP *Heap, 00099 FLOAT32 *Key, 00100 char **Data)); 00101 00102 int HeapPopWorst 00103 _ARGS((HEAP *Heap, 00104 FLOAT32 *Key, 00105 char **Data)); 00106 00107 void HeapPush 00108 _ARGS((HEAP *Heap, 00109 FLOAT32 Key, 00110 char *Data)); 00111 00112 void HeapStore 00113 _ARGS((HEAP *Heap, 00114 HEAPENTRY *Entry)); 00115 00116 int GetTopOfHeap 00117 _ARGS((HEAP *Heap, 00118 HEAPENTRY *Entry)); 00119 00120 void FreeHeapData 00121 _ARGS((HEAP *Heap, 00122 void (*Deallocator )())); 00123 00124 #undef _ARGS 00125 */ 00126 #endif