@@ -29,7 +29,7 @@ void heap_swap(heap_t* heap, int a, int b)
29
29
/*
30
30
* Create a new fixed size heap.
31
31
*/
32
- heap_t * heap_create (int size , size_t type_size , comparator cmp )
32
+ heap_t * heap_create (int size , size_t type_size , comparator cmp , dtor destructor )
33
33
{
34
34
heap_t * h = NULL ;
35
35
@@ -43,6 +43,7 @@ heap_t* heap_create(int size, size_t type_size, comparator cmp)
43
43
h -> items = 0 ;
44
44
h -> size = size ;
45
45
h -> item_size = type_size ;
46
+ h -> destructor = destructor ;
46
47
h -> list = calloc (1 , type_size * size );
47
48
48
49
check_mem (h -> list );
@@ -61,37 +62,43 @@ heap_t* heap_create(int size, size_t type_size, comparator cmp)
61
62
/*
62
63
* Decallocate every item in the heap.
63
64
*/
64
- void heap_clear (heap_t * heap )
65
+ void heap_clear (heap_t * h )
65
66
{
66
67
int i = 0 ;
67
- if (heap -> items > 0 ) {
68
- for (i = 0 ; i < heap -> size ; i ++ ) {
69
- if (heap -> list [i ] != NULL ) {
70
- free (heap -> list [i ]);
68
+ if (h -> items <= 0 ) {
69
+ return ;
70
+ }
71
+ for (i = 0 ; i < h -> size ; i ++ ) {
72
+ if (h -> list [i ] != NULL ) {
73
+ if (h -> destructor != NULL ) {
74
+ h -> destructor (h -> list [i ]);
71
75
}
76
+ h -> list [i ] = NULL ;
72
77
}
73
78
}
74
79
}
75
80
76
81
/*
77
82
* Deallocate a heap
78
83
*/
79
- void heap_destroy (heap_t * heap )
84
+ void heap_destroy (heap_t * h )
80
85
{
81
- if (heap != NULL ) {
82
- if (heap -> list != NULL ) {
83
- free (heap -> list );
84
- }
85
- free (heap );
86
+ if (h == NULL ) {
87
+ return ;
88
+ }
89
+ if (h -> list != NULL ) {
90
+ heap_clear (h );
91
+ free (h -> list );
86
92
}
93
+ free (h );
87
94
}
88
95
89
96
/*
90
97
* Return the index of the items parent
91
98
*/
92
- int heap_parent (const heap_t * heap , const int pos ) {
99
+ int heap_parent (const heap_t * h , const int pos ) {
93
100
check (pos >= 0 , "Heap position must be greater than 0" );
94
- check (pos < heap -> items , "Heap position is out of range" );
101
+ check (pos < h -> items , "Heap position is out of range" );
95
102
return (pos - 1 ) / 2 ;
96
103
97
104
error :
@@ -101,15 +108,15 @@ int heap_parent(const heap_t* heap, const int pos) {
101
108
/*
102
109
* Determine if the element at pos is a leaf node
103
110
*/
104
- bool heap_leaf (heap_t * heap , int pos ) {
105
- return (pos >= heap -> items / 2 ) && (pos < heap -> items );
111
+ bool heap_leaf (heap_t * h , int pos ) {
112
+ return (pos >= h -> items / 2 ) && (pos < h -> items );
106
113
}
107
114
108
115
/*
109
116
* Get the index of the left child of element at pos
110
117
*/
111
- int heap_left (heap_t * heap , int pos ) {
112
- check (pos < heap -> items , "Heap position is out of range" );
118
+ int heap_left (heap_t * h , int pos ) {
119
+ check (pos < h -> items , "Heap position is out of range" );
113
120
return (2 * pos ) + 1 ;
114
121
115
122
error :
@@ -119,8 +126,8 @@ int heap_left(heap_t* heap, int pos) {
119
126
/*
120
127
* Get index of the right child of element at pos
121
128
*/
122
- int heap_right (heap_t * heap , int pos ) {
123
- check (pos < heap -> items , "Heap position is out of range" );
129
+ int heap_right (heap_t * h , int pos ) {
130
+ check (pos < h -> items , "Heap position is out of range" );
124
131
return (2 * pos ) + 2 ;
125
132
126
133
error :
@@ -130,21 +137,21 @@ int heap_right(heap_t* heap, int pos) {
130
137
/*
131
138
* Move element at pos to its correct position within the heap
132
139
*/
133
- void heap_sift (heap_t * heap , int pos ) {
140
+ void heap_sift (heap_t * h , int pos ) {
134
141
int j ;
135
- while (!heap_leaf (heap , pos )) {
136
- j = heap_left (heap , pos );
142
+ while (!heap_leaf (h , pos )) {
143
+ j = heap_left (h , pos );
137
144
138
- if ((j < heap -> items - 1 ) &&
139
- (heap -> cmp (heap -> list [j ], heap -> list [j + 1 ]) > 0 )) {
145
+ if ((j < h -> items - 1 ) &&
146
+ (h -> cmp (h -> list [j ], h -> list [j + 1 ]) > 0 )) {
140
147
j ++ ;
141
148
}
142
149
143
- if (heap -> cmp (heap -> list [pos ], heap -> list [j ]) <= 0 ) {
150
+ if (h -> cmp (h -> list [pos ], h -> list [j ]) <= 0 ) {
144
151
return ;
145
152
}
146
153
147
- heap_swap (heap , pos , j );
154
+ heap_swap (h , pos , j );
148
155
pos = j ;
149
156
}
150
157
}
0 commit comments