source: trunk/gsdl/packages/mg/lib/mgheap.c@ 10853

Last change on this file since 10853 was 439, checked in by sjboddie, 25 years ago

renamed mg-1.3d directory mg

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.4 KB
Line 
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.c 439 1999-08-10 21:23:37Z sjboddie $
21 *
22 **************************************************************************/
23
24/*
25 $Log$
26 Revision 1.1 1999/08/10 21:16:55 sjboddie
27 renamed mg-1.3d directory mg
28
29 Revision 1.1 1999/01/15 03:05:50 rjmcnab
30
31 Renamed lib/heap to be lib/mgheap (it was causing some problems with
32 some versions of the STL libraries which contained a heap.h)
33
34 Revision 1.1 1998/11/17 09:32:01 rjmcnab
35 *** empty log message ***
36
37 * Revision 1.1 1994/08/22 00:24:43 tes
38 * Initial placement under CVS.
39 *
40 */
41
42static char *RCSID = "$Id: mgheap.c 439 1999-08-10 21:23:37Z sjboddie $";
43
44#include "mgheap.h"
45
46#define SWAP(s, a, b) \
47do { \
48 register int __i; \
49 register char *__a = (a), *__b = (b); \
50 for (__i = (s); __i; __i--) \
51 { \
52 register char __t = *__a; \
53 *__a++ = *__b; *__b++ = __t; \
54 } \
55} while(0)
56
57
58
59#define COPY(s, dst, src) \
60do { \
61 register int __i; \
62 register char *__dst = (dst), *__src = (src); \
63 for (__i = (s); __i; __i--) \
64 *__dst++ = *__src++; \
65} while(0)
66
67
68
69static void
70heap_heapify (void *heap, int size, int num, int curr, heap_comp hc)
71{
72 register int child = curr * 2;
73 while (child <= num)
74 {
75 if (child < num && hc ((char *) heap + child * size,
76 (char *) heap + child * size - size) < 0)
77 child++;
78 if (hc ((char *) heap + (curr - 1) * size, (char *) heap + (child - 1) * size) > 0)
79 {
80 SWAP (size, (char *) heap + (curr - 1) * size,
81 (char *) heap + (child - 1) * size);
82 curr = child;
83 child = child * 2;
84 }
85 else
86 break;
87 }
88}
89
90static void
91heap_pullup (void *heap, int size, int num, heap_comp hc)
92{
93 register int curr = num;
94 register int parent = curr >> 1;
95 while (parent &&
96 hc ((char *) heap + (curr - 1) * size, (char *) heap + (parent - 1) * size) < 0)
97 {
98 SWAP (size, (char *) heap + (curr - 1) * size,
99 (char *) heap + (parent - 1) * size);
100 curr = parent;
101 parent = curr >> 1;
102 }
103}
104
105/************************************************************************
106 *
107 * NOTE: If you choose to change the comparison function the first thing
108 * you do after changing the function is call heap_build.
109 *
110 */
111void
112heap_build (void *heap, int size, int num, heap_comp hc)
113{
114 register int i;
115 for (i = num / 2; i > 0; i--)
116 heap_heapify (heap, size, num, i, hc);
117}
118
119
120
121/****************************************************
122 *
123 * NOTE : The heap must be built before heap_sort is called.
124 * This has the effect of reversing the order of the array.
125 * e.g. if your comparison function is designed to pull the
126 * biggest thing off the heap first then the result of
127 * sorting with this function will be to put the bigest
128 * thing at the end of the array.
129 *
130 */
131void
132heap_sort (void *heap, int size, int num, heap_comp hc)
133{
134 register int i;
135 for (i = num; i > 1; i--)
136 {
137 SWAP (size, heap, (char *) heap + (i - 1) * size);
138 heap_heapify (heap, size, i - 1, 1, hc);
139 }
140}
141
142
143void
144heap_deletehead (void *heap, int size, int *num, heap_comp hc)
145{
146 (*num)--;
147 SWAP (size, heap, (char *) heap + *num * size);
148 heap_heapify (heap, size, *num, 1, hc);
149}
150
151
152void
153heap_changedhead (void *heap, int size, int num, heap_comp hc)
154{
155 heap_heapify (heap, size, num, 1, hc);
156}
157
158
159/***********************************************************************
160 *
161 * This assumes that the item has been added to the end of the
162 * array that is the heap. But that num has not been changed.
163 *
164 */
165void
166heap_additem (void *heap, int size, int *num, heap_comp hc)
167{
168 (*num)++;
169 heap_pullup (heap, size, *num, hc);
170}
Note: See TracBrowser for help on using the repository browser.