[3745] | 1 | /**************************************************************************
|
---|
| 2 | *
|
---|
| 3 | * mgheap.h -- 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 | **************************************************************************/
|
---|
| 21 |
|
---|
| 22 | /* Care should be taken when using these routines as NO error checking is
|
---|
| 23 | * done.
|
---|
| 24 | */
|
---|
| 25 |
|
---|
| 26 |
|
---|
| 27 |
|
---|
| 28 | /* The heap comparison routine should return
|
---|
| 29 | * <0 : If a should be before b
|
---|
| 30 | * 0 : If a and b are equal
|
---|
| 31 | * >0 : If a should be after b
|
---|
| 32 | */
|
---|
| 33 | typedef int (*heap_comp) (void *a, void *b);
|
---|
| 34 |
|
---|
| 35 |
|
---|
| 36 | /************************************************************************
|
---|
| 37 | *
|
---|
| 38 | * NOTE: If you choose to change the comparison function the first thing
|
---|
| 39 | * you do after changing the function is call heap_build.
|
---|
| 40 | *
|
---|
| 41 | */
|
---|
| 42 | void heap_build (void *heap, int size, int num, heap_comp hc);
|
---|
| 43 |
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 | /************************************************************************
|
---|
| 47 | *
|
---|
| 48 | * NOTE : The heap must be built before heap_sort is called.
|
---|
| 49 | * This has the effect of reversing the order of the array.
|
---|
| 50 | * e.g. if your comparison function is designed to pull the
|
---|
| 51 | * biggest thing off the heap first then the result of
|
---|
| 52 | * sorting with this function will be to put the bigest
|
---|
| 53 | * thing at the end of the array.
|
---|
| 54 | *
|
---|
| 55 | */
|
---|
| 56 | void heap_sort (void *heap, int size, int num, heap_comp hc);
|
---|
| 57 |
|
---|
| 58 | /***********************************************************************
|
---|
| 59 | *
|
---|
| 60 | * NOTE: After deleting the head the heap will be one item shorter.
|
---|
| 61 | * The deleted item will be placed at the end of the heap.
|
---|
| 62 | *
|
---|
| 63 | */
|
---|
| 64 | void heap_deletehead (void *heap, int size, int *num, heap_comp hc);
|
---|
| 65 |
|
---|
| 66 |
|
---|
| 67 | /***********************************************************************
|
---|
| 68 | *
|
---|
| 69 | * If you change the "value" of the root node of the heap then you
|
---|
| 70 | * should call this to re-pritorize the heap.
|
---|
| 71 | *
|
---|
| 72 | */
|
---|
| 73 | void heap_changedhead (void *heap, int size, int num, heap_comp hc);
|
---|
| 74 |
|
---|
| 75 |
|
---|
| 76 | /***********************************************************************
|
---|
| 77 | *
|
---|
| 78 | * This assumes that the item has been added to the end of the
|
---|
| 79 | * array that is the heap. But that num has not been changed.
|
---|
| 80 | *
|
---|
| 81 | */
|
---|
| 82 | void heap_additem (void *heap, int size, int *num, heap_comp hc);
|
---|