]>
git.ipfire.org Git - thirdparty/squid.git/blob - include/heap.h
f9aee7f143a59b643b5259772b7f1e7c6beb3158
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
10 * AUTHOR: John Dilley, Hewlett Packard
13 /****************************************************************************
14 * Copyright (C) 1999 by Hewlett Packard
16 * Heap data structure. Used to store objects for cache replacement. The
17 * heap is implemented as a contiguous array in memory. Heap sort and heap
18 * update are done in-place. The heap is ordered with the smallest value at
19 * the top of the heap (as in the smallest object key value). Child nodes
20 * are larger than their parent.
21 ****************************************************************************/
26 * Function for generating heap keys. The first argument will typically be
27 * a dws_md_p passed in as a void *. Should find a way to get type safety
28 * without having heap know all about metadata objects... The second arg is
29 * the current aging factor for the heap.
31 typedef unsigned long heap_mutex_t
;
32 typedef void * heap_t
;
33 typedef double heap_key
;
34 typedef heap_key
heap_key_func(heap_t
, heap_key
);
37 * Heap node. Has a key value generated by a key_func, id (array index) so
38 * it can be quickly found in its heap, and a pointer to a data object that
39 * key_func can generate a key from.
41 typedef struct _heap_node
{
48 * Heap object. Holds an array of heap_node objects along with a heap size
49 * (array length), the index of the last heap element, and a key generation
50 * function. Also stores aging factor for this heap.
52 typedef struct _heap
{
56 heap_key_func
*gen_key
; /* key generator for heap */
57 heap_key age
; /* aging factor for heap */
61 /****************************************************************************
63 ****************************************************************************/
66 * Create and initialize a new heap.
68 SQUIDCEXTERN heap
*new_heap(int init_size
, heap_key_func gen_key
);
71 * Delete a heap and clean up its memory. Does not delete what the heap
72 * nodes are pointing to!
74 SQUIDCEXTERN
void delete_heap(heap
*);
77 * Insert a new node into a heap, returning a pointer to it. The heap_node
78 * object returned is used to update or delete a heap object. Nothing else
79 * should be done with this data structure (especially modifying it!) The
80 * heap does not assume ownership of the data passed to it.
82 SQUIDCEXTERN heap_node
*heap_insert(heap
*hp
, heap_t dat
);
85 * Delete a node out of a heap. Returns the heap data from the deleted
86 * node. The caller is responsible for freeing this data.
88 SQUIDCEXTERN heap_t
heap_delete(heap
*, heap_node
* elm
);
91 * The semantics of this routine is the same as the followings:
92 * heap_delete(hp, elm);
93 * heap_insert(hp, dat);
94 * Returns the old data object from elm (the one being replaced). The
95 * caller must free this as necessary.
97 SQUIDCEXTERN heap_t
heap_update(heap
*, heap_node
* elm
, heap_t dat
);
100 * Generate a heap key for a given data object. Alternative macro form:
103 SQUIDCEXTERN heap_key
heap_gen_key(heap
* hp
, heap_t dat
);
105 #define heap_gen_key(hp,md) ((hp)->gen_key((md),(hp)->age))
106 #endif /* MACRO_DEBUG */
109 * Extract the minimum (root) element and maintain the heap property.
110 * Returns the data pointed to by the root node, which the caller must
113 SQUIDCEXTERN heap_t
heap_extractmin(heap
*);
116 * Extract the last leaf node (does not change the heap property).
117 * Returns the data that had been in the heap which the caller must free if
118 * necessary. Note that the last node is guaranteed to be less than its
119 * parent, but may not be less than any of the other (leaf or parent) notes
120 * in the tree. This operation is fast but imprecise.
122 SQUIDCEXTERN heap_t
heap_extractlast(heap
* hp
);
125 * Get the root key, the nth key, the root (smallest) element, or the nth
126 * element. None of these operations modify the heap.
128 SQUIDCEXTERN heap_key
heap_peepminkey(heap
*);
129 SQUIDCEXTERN heap_key
heap_peepkey(heap
*, int n
);
131 SQUIDCEXTERN heap_t
heap_peepmin(heap
*);
132 SQUIDCEXTERN heap_t
heap_peep(heap
*, int n
);
135 * Is the heap empty? How many nodes (data objects) are in it?
138 SQUIDCEXTERN
int heap_empty(heap
*);
139 SQUIDCEXTERN
int heap_nodes(heap
*);
140 #else /* MACRO_DEBUG */
141 #define heap_nodes(heap) ((heap)->last)
142 #define heap_empty(heap) ((heap)->last <= 0 ? 1 : 0)
143 #endif /* MACRO_DEBUG */
146 * Print the heap or a node in the heap.
148 SQUIDCEXTERN
void heap_print(heap
*);
149 SQUIDCEXTERN
void heap_printnode(char *msg
, heap_node
* elm
);
151 SQUIDCEXTERN
int verify_heap_property(heap
*);
153 #endif /* SQUID_HEAP_H */