1 | /**************************************************************************
|
---|
2 | *
|
---|
3 | * mgheap.c -- Heap routines
|
---|
4 | * Copyright (C) 1994 Neil Sharman
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This program is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU General Public License
|
---|
17 | * along with this program; if not, write to the Free Software
|
---|
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
19 | *
|
---|
20 | * $Id: mgheap.cpp 855 2000-01-14 02:17:52Z sjboddie $
|
---|
21 | *
|
---|
22 | **************************************************************************/
|
---|
23 |
|
---|
24 | /*
|
---|
25 | $Log$
|
---|
26 | Revision 1.1 2000/01/14 02:17:11 sjboddie
|
---|
27 | Rodgers new C++ mg
|
---|
28 |
|
---|
29 | Revision 1.1 1999/10/11 02:55:18 cs025
|
---|
30 | Base install of MG-PP
|
---|
31 |
|
---|
32 | Revision 1.1 1999/08/10 21:16:55 sjboddie
|
---|
33 | renamed mg-1.3d directory mg
|
---|
34 |
|
---|
35 | Revision 1.1 1999/01/15 03:05:50 rjmcnab
|
---|
36 |
|
---|
37 | Renamed lib/heap to be lib/mgheap (it was causing some problems with
|
---|
38 | some versions of the STL libraries which contained a heap.h)
|
---|
39 |
|
---|
40 | Revision 1.1 1998/11/17 09:32:01 rjmcnab
|
---|
41 | *** empty log message ***
|
---|
42 |
|
---|
43 | * Revision 1.1 1994/08/22 00:24:43 tes
|
---|
44 | * Initial placement under CVS.
|
---|
45 | *
|
---|
46 | */
|
---|
47 |
|
---|
48 | static char *RCSID = "$Id: mgheap.cpp 855 2000-01-14 02:17:52Z sjboddie $";
|
---|
49 |
|
---|
50 | #include "mgheap.h"
|
---|
51 |
|
---|
52 | #define SWAP(s, a, b) \
|
---|
53 | do { \
|
---|
54 | register int __i; \
|
---|
55 | register char *__a = (a), *__b = (b); \
|
---|
56 | for (__i = (s); __i; __i--) \
|
---|
57 | { \
|
---|
58 | register char __t = *__a; \
|
---|
59 | *__a++ = *__b; *__b++ = __t; \
|
---|
60 | } \
|
---|
61 | } while(0)
|
---|
62 |
|
---|
63 |
|
---|
64 |
|
---|
65 | #define COPY(s, dst, src) \
|
---|
66 | do { \
|
---|
67 | register int __i; \
|
---|
68 | register char *__dst = (dst), *__src = (src); \
|
---|
69 | for (__i = (s); __i; __i--) \
|
---|
70 | *__dst++ = *__src++; \
|
---|
71 | } while(0)
|
---|
72 |
|
---|
73 |
|
---|
74 |
|
---|
75 | static void
|
---|
76 | heap_heapify (void *heap, int size, int num, int curr, heap_comp hc)
|
---|
77 | {
|
---|
78 | register int child = curr * 2;
|
---|
79 | while (child <= num)
|
---|
80 | {
|
---|
81 | if (child < num && hc ((char *) heap + child * size,
|
---|
82 | (char *) heap + child * size - size) < 0)
|
---|
83 | child++;
|
---|
84 | if (hc ((char *) heap + (curr - 1) * size, (char *) heap + (child - 1) * size) > 0)
|
---|
85 | {
|
---|
86 | SWAP (size, (char *) heap + (curr - 1) * size,
|
---|
87 | (char *) heap + (child - 1) * size);
|
---|
88 | curr = child;
|
---|
89 | child = child * 2;
|
---|
90 | }
|
---|
91 | else
|
---|
92 | break;
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | static void
|
---|
97 | heap_pullup (void *heap, int size, int num, heap_comp hc)
|
---|
98 | {
|
---|
99 | register int curr = num;
|
---|
100 | register int parent = curr >> 1;
|
---|
101 | while (parent &&
|
---|
102 | hc ((char *) heap + (curr - 1) * size, (char *) heap + (parent - 1) * size) < 0)
|
---|
103 | {
|
---|
104 | SWAP (size, (char *) heap + (curr - 1) * size,
|
---|
105 | (char *) heap + (parent - 1) * size);
|
---|
106 | curr = parent;
|
---|
107 | parent = curr >> 1;
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | /************************************************************************
|
---|
112 | *
|
---|
113 | * NOTE: If you choose to change the comparison function the first thing
|
---|
114 | * you do after changing the function is call heap_build.
|
---|
115 | *
|
---|
116 | */
|
---|
117 | void
|
---|
118 | heap_build (void *heap, int size, int num, heap_comp hc)
|
---|
119 | {
|
---|
120 | register int i;
|
---|
121 | for (i = num / 2; i > 0; i--)
|
---|
122 | heap_heapify (heap, size, num, i, hc);
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 |
|
---|
127 | /****************************************************
|
---|
128 | *
|
---|
129 | * NOTE : The heap must be built before heap_sort is called.
|
---|
130 | * This has the effect of reversing the order of the array.
|
---|
131 | * e.g. if your comparison function is designed to pull the
|
---|
132 | * biggest thing off the heap first then the result of
|
---|
133 | * sorting with this function will be to put the bigest
|
---|
134 | * thing at the end of the array.
|
---|
135 | *
|
---|
136 | */
|
---|
137 | void
|
---|
138 | heap_sort (void *heap, int size, int num, heap_comp hc)
|
---|
139 | {
|
---|
140 | register int i;
|
---|
141 | for (i = num; i > 1; i--)
|
---|
142 | {
|
---|
143 | SWAP (size, (char *) heap, (char *) heap + (i - 1) * size);
|
---|
144 | heap_heapify (heap, size, i - 1, 1, hc);
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | void
|
---|
150 | heap_deletehead (void *heap, int size, int *num, heap_comp hc)
|
---|
151 | {
|
---|
152 | (*num)--;
|
---|
153 | SWAP (size, (char *) heap, (char *) heap + *num * size);
|
---|
154 | heap_heapify (heap, size, *num, 1, hc);
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | void
|
---|
159 | heap_changedhead (void *heap, int size, int num, heap_comp hc)
|
---|
160 | {
|
---|
161 | heap_heapify (heap, size, num, 1, hc);
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | /***********************************************************************
|
---|
166 | *
|
---|
167 | * This assumes that the item has been added to the end of the
|
---|
168 | * array that is the heap. But that num has not been changed.
|
---|
169 | *
|
---|
170 | */
|
---|
171 | void
|
---|
172 | heap_additem (void *heap, int size, int *num, heap_comp hc)
|
---|
173 | {
|
---|
174 | (*num)++;
|
---|
175 | heap_pullup (heap, size, *num, hc);
|
---|
176 | }
|
---|