priorityq.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
  3. * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice including the dates of first publication and
  13. * either this permission notice or a reference to
  14. * http://oss.sgi.com/projects/FreeB/
  15. * shall be included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
  22. * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. *
  25. * Except as contained in this notice, the name of Silicon Graphics, Inc.
  26. * shall not be used in advertising or otherwise to promote the sale, use or
  27. * other dealings in this Software without prior written authorization from
  28. * Silicon Graphics, Inc.
  29. */
  30. /*
  31. ** Author: Eric Veach, July 1994.
  32. **
  33. */
  34. #ifndef __priorityq_sort_h_
  35. #define __priorityq_sort_h_
  36. #include "priorityq-heap.h"
  37. #undef PQkey
  38. #undef PQhandle
  39. #undef PriorityQ
  40. #undef pqNewPriorityQ
  41. #undef pqDeletePriorityQ
  42. #undef pqInit
  43. #undef pqInsert
  44. #undef pqMinimum
  45. #undef pqExtractMin
  46. #undef pqDelete
  47. #undef pqIsEmpty
  48. /* Use #define's so that another heap implementation can use this one */
  49. #define PQkey PQSortKey
  50. #define PQhandle PQSortHandle
  51. #define PriorityQ PriorityQSort
  52. #define pqNewPriorityQ(leq) __gl_pqSortNewPriorityQ(leq)
  53. #define pqDeletePriorityQ(pq) __gl_pqSortDeletePriorityQ(pq)
  54. /* The basic operations are insertion of a new key (pqInsert),
  55. * and examination/extraction of a key whose value is minimum
  56. * (pqMinimum/pqExtractMin). Deletion is also allowed (pqDelete);
  57. * for this purpose pqInsert returns a "handle" which is supplied
  58. * as the argument.
  59. *
  60. * An initial heap may be created efficiently by calling pqInsert
  61. * repeatedly, then calling pqInit. In any case pqInit must be called
  62. * before any operations other than pqInsert are used.
  63. *
  64. * If the heap is empty, pqMinimum/pqExtractMin will return a NULL key.
  65. * This may also be tested with pqIsEmpty.
  66. */
  67. #define pqInit(pq) __gl_pqSortInit(pq)
  68. #define pqInsert(pq,key) __gl_pqSortInsert(pq,key)
  69. #define pqMinimum(pq) __gl_pqSortMinimum(pq)
  70. #define pqExtractMin(pq) __gl_pqSortExtractMin(pq)
  71. #define pqDelete(pq,handle) __gl_pqSortDelete(pq,handle)
  72. #define pqIsEmpty(pq) __gl_pqSortIsEmpty(pq)
  73. /* Since we support deletion the data structure is a little more
  74. * complicated than an ordinary heap. "nodes" is the heap itself;
  75. * active nodes are stored in the range 1..pq->size. When the
  76. * heap exceeds its allocated size (pq->max), its size doubles.
  77. * The children of node i are nodes 2i and 2i+1.
  78. *
  79. * Each node stores an index into an array "handles". Each handle
  80. * stores a key, plus a pointer back to the node which currently
  81. * represents that key (ie. nodes[handles[i].node].handle == i).
  82. */
  83. typedef PQHeapKey PQkey;
  84. typedef PQHeapHandle PQhandle;
  85. typedef struct PriorityQ PriorityQ;
  86. struct PriorityQ {
  87. PriorityQHeap *heap;
  88. PQkey *keys;
  89. PQkey **order;
  90. PQhandle size, max;
  91. int initialized;
  92. int (*leq)(PQkey key1, PQkey key2);
  93. };
  94. PriorityQ *pqNewPriorityQ( int (*leq)(PQkey key1, PQkey key2) );
  95. void pqDeletePriorityQ( PriorityQ *pq );
  96. int pqInit( PriorityQ *pq );
  97. PQhandle pqInsert( PriorityQ *pq, PQkey key );
  98. PQkey pqExtractMin( PriorityQ *pq );
  99. void pqDelete( PriorityQ *pq, PQhandle handle );
  100. PQkey pqMinimum( PriorityQ *pq );
  101. int pqIsEmpty( PriorityQ *pq );
  102. #endif