00001 /* 00002 * Copyright (C) 2004-2007, 2009, 2012 Internet Systems Consortium, Inc. ("ISC") 00003 * Copyright (C) 1997-2001 Internet Software Consortium. 00004 * 00005 * Permission to use, copy, modify, and/or distribute this software for any 00006 * purpose with or without fee is hereby granted, provided that the above 00007 * copyright notice and this permission notice appear in all copies. 00008 * 00009 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 00010 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 00011 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 00012 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 00013 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 00014 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 00015 * PERFORMANCE OF THIS SOFTWARE. 00016 */ 00017 00018 /* $Id: heap.h,v 1.26 2009/01/17 23:47:43 tbox Exp $ */ 00019 00020 #ifndef ISC_HEAP_H 00021 #define ISC_HEAP_H 1 00022 00023 /*! \file isc/heap.h */ 00024 00025 #include <isc/lang.h> 00026 #include <isc/types.h> 00027 00028 ISC_LANG_BEGINDECLS 00029 00030 /*% 00031 * The comparison function returns ISC_TRUE if the first argument has 00032 * higher priority than the second argument, and ISC_FALSE otherwise. 00033 */ 00034 typedef isc_boolean_t (*isc_heapcompare_t)(void *, void *); 00035 00036 /*% 00037 * The index function allows the client of the heap to receive a callback 00038 * when an item's index number changes. This allows it to maintain 00039 * sync with its external state, but still delete itself, since deletions 00040 * from the heap require the index be provided. 00041 */ 00042 typedef void (*isc_heapindex_t)(void *, unsigned int); 00043 00044 /*% 00045 * The heapaction function is used when iterating over the heap. 00046 * 00047 * NOTE: The heap structure CANNOT BE MODIFIED during the call to 00048 * isc_heap_foreach(). 00049 */ 00050 typedef void (*isc_heapaction_t)(void *, void *); 00051 00052 typedef struct isc_heap isc_heap_t; 00053 00054 isc_result_t 00055 isc_heap_create(isc_mem_t *mctx, isc_heapcompare_t compare, 00056 isc_heapindex_t index, unsigned int size_increment, 00057 isc_heap_t **heapp); 00058 /*!< 00059 * \brief Create a new heap. The heap is implemented using a space-efficient 00060 * storage method. When the heap elements are deleted space is not freed 00061 * but will be reused when new elements are inserted. 00062 * 00063 * Heap elements are indexed from 1. 00064 * 00065 * Requires: 00066 *\li "mctx" is valid. 00067 *\li "compare" is a function which takes two void * arguments and 00068 * returns ISC_TRUE if the first argument has a higher priority than 00069 * the second, and ISC_FALSE otherwise. 00070 *\li "index" is a function which takes a void *, and an unsigned int 00071 * argument. This function will be called whenever an element's 00072 * index value changes, so it may continue to delete itself from the 00073 * heap. This option may be NULL if this functionality is unneeded. 00074 *\li "size_increment" is a hint about how large the heap should grow 00075 * when resizing is needed. If this is 0, a default size will be 00076 * used, which is currently 1024, allowing space for an additional 1024 00077 * heap elements to be inserted before adding more space. 00078 *\li "heapp" is not NULL, and "*heap" is NULL. 00079 * 00080 * Returns: 00081 *\li ISC_R_SUCCESS - success 00082 *\li ISC_R_NOMEMORY - insufficient memory 00083 */ 00084 00085 void 00086 isc_heap_destroy(isc_heap_t **heapp); 00087 /*!< 00088 * \brief Destroys a heap. 00089 * 00090 * Requires: 00091 *\li "heapp" is not NULL and "*heap" points to a valid isc_heap_t. 00092 */ 00093 00094 isc_result_t 00095 isc_heap_insert(isc_heap_t *heap, void *elt); 00096 /*!< 00097 * \brief Inserts a new element into a heap. 00098 * 00099 * Requires: 00100 *\li "heapp" is not NULL and "*heap" points to a valid isc_heap_t. 00101 */ 00102 00103 void 00104 isc_heap_delete(isc_heap_t *heap, unsigned int index); 00105 /*!< 00106 * \brief Deletes an element from a heap, by element index. 00107 * 00108 * Requires: 00109 *\li "heapp" is not NULL and "*heap" points to a valid isc_heap_t. 00110 *\li "index" is a valid element index, as provided by the "index" callback 00111 * provided during heap creation. 00112 */ 00113 00114 void 00115 isc_heap_increased(isc_heap_t *heap, unsigned int index); 00116 /*!< 00117 * \brief Indicates to the heap that an element's priority has increased. 00118 * This function MUST be called whenever an element has increased in priority. 00119 * 00120 * Requires: 00121 *\li "heapp" is not NULL and "*heap" points to a valid isc_heap_t. 00122 *\li "index" is a valid element index, as provided by the "index" callback 00123 * provided during heap creation. 00124 */ 00125 00126 void 00127 isc_heap_decreased(isc_heap_t *heap, unsigned int index); 00128 /*!< 00129 * \brief Indicates to the heap that an element's priority has decreased. 00130 * This function MUST be called whenever an element has decreased in priority. 00131 * 00132 * Requires: 00133 *\li "heapp" is not NULL and "*heap" points to a valid isc_heap_t. 00134 *\li "index" is a valid element index, as provided by the "index" callback 00135 * provided during heap creation. 00136 */ 00137 00138 void * 00139 isc_heap_element(isc_heap_t *heap, unsigned int index); 00140 /*!< 00141 * \brief Returns the element for a specific element index. 00142 * 00143 * Requires: 00144 *\li "heapp" is not NULL and "*heap" points to a valid isc_heap_t. 00145 *\li "index" is a valid element index, as provided by the "index" callback 00146 * provided during heap creation. 00147 * 00148 * Returns: 00149 *\li A pointer to the element for the element index. 00150 */ 00151 00152 void 00153 isc_heap_foreach(isc_heap_t *heap, isc_heapaction_t action, void *uap); 00154 /*!< 00155 * \brief Iterate over the heap, calling an action for each element. The 00156 * order of iteration is not sorted. 00157 * 00158 * Requires: 00159 *\li "heapp" is not NULL and "*heap" points to a valid isc_heap_t. 00160 *\li "action" is not NULL, and is a function which takes two arguments. 00161 * The first is a void *, representing the element, and the second is 00162 * "uap" as provided to isc_heap_foreach. 00163 *\li "uap" is a caller-provided argument, and may be NULL. 00164 * 00165 * Note: 00166 *\li The heap structure CANNOT be modified during this iteration. The only 00167 * safe function to call while iterating the heap is isc_heap_element(). 00168 */ 00169 00170 ISC_LANG_ENDDECLS 00171 00172 #endif /* ISC_HEAP_H */