cdcontainers  0.1.1
Library of data containers and collections for C programming language.
heap.h
Go to the documentation of this file.
1 // The MIT License (MIT)
2 // Copyright (c) 2017 Maksim Andrianov
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to
6 // deal in the Software without restriction, including without limitation the
7 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 // sell copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 // IN THE SOFTWARE.
26 #ifndef CDCONTAINERS_INCLUDE_CDCONTAINERS_HEAP_H
27 #define CDCONTAINERS_INCLUDE_CDCONTAINERS_HEAP_H
28 
29 #include <cdcontainers/array.h>
30 #include <cdcontainers/common.h>
31 #include <cdcontainers/status.h>
32 
33 #include <stdarg.h>
34 #include <stdbool.h>
35 #include <stdlib.h>
36 
42 struct cdc_heap {
43  struct cdc_array *array;
44 };
45 
51 struct cdc_heap_iter {
53  size_t current;
54 };
55 
63 enum cdc_stat cdc_heap_ctor(struct cdc_heap **h, struct cdc_data_info *info);
64 
73 enum cdc_stat cdc_heap_ctorl(struct cdc_heap **h, struct cdc_data_info *info,
74  ...);
75 
83 enum cdc_stat cdc_heap_ctorv(struct cdc_heap **h, struct cdc_data_info *info,
84  va_list args);
85 
90 void cdc_heap_dtor(struct cdc_heap *h);
91 
92 // Element access
99 static inline void *cdc_heap_top(struct cdc_heap *h)
100 {
101  assert(h != NULL);
102 
103  return cdc_array_front(h->array);
104 }
105 
106 // Capacity
112 static inline size_t cdc_heap_size(struct cdc_heap *h)
113 {
114  assert(h != NULL);
115 
116  return cdc_array_size(h->array);
117 }
118 
124 static inline bool cdc_heap_empty(struct cdc_heap *h)
125 {
126  assert(h != NULL);
127 
128  return cdc_array_empty(h->array);
129 }
130 
131 // Modifiers
137 void cdc_heap_extract_top(struct cdc_heap *h);
138 
149 enum cdc_stat cdc_heap_riinsert(struct cdc_heap *h, void *key,
150  struct cdc_heap_iter *ret);
151 
159 static inline enum cdc_stat cdc_heap_insert(struct cdc_heap *h, void *key)
160 {
161  assert(h != NULL);
162 
163  return cdc_heap_riinsert(h, key, NULL);
164 }
165 
173 void cdc_heap_change_key(struct cdc_heap *h, struct cdc_heap_iter *pos,
174  void *key);
175 
180 void cdc_heap_clear(struct cdc_heap *h);
181 
187 void cdc_heap_swap(struct cdc_heap *a, struct cdc_heap *b);
188 
189 // Operations
196 enum cdc_stat cdc_heap_merge(struct cdc_heap *h, struct cdc_heap *other);
197 
203 bool cdc_heap_is_heap(struct cdc_heap *h);
204 
205 // Iterators
209 static inline void *cdc_heap_iter_data(struct cdc_heap_iter *it)
210 {
211  assert(it != NULL);
212 
213  return cdc_array_get(it->container, it->current);
214 }
215 
220 static inline bool cdc_heap_iter_is_eq(struct cdc_heap_iter *it1,
221  struct cdc_heap_iter *it2)
222 {
223  assert(it1 != NULL);
224  assert(it2 != NULL);
225 
226  return it1->container == it2->container && it1->current == it2->current;
227 }
228 
229 // Short names
230 #ifdef CDC_USE_SHORT_NAMES
231 typedef struct cdc_heap heap_t;
232 typedef struct cdc_heap_iter heap_iter_t;
233 
234 #define heap_ctor(...) cdc_heap_ctor(__VA_ARGS__)
235 #define heap_ctorl(...) cdc_heap_ctorl(__VA_ARGS__)
236 #define heap_ctorv(...) cdc_heap_ctorv(__VA_ARGS__)
237 #define heap_dtor(...) cdc_heap_dtor(__VA_ARGS__)
238 
239 // Element access
240 #define heap_top(...) cdc_heap_top(__VA_ARGS__)
241 
242 // Capacity
243 #define heap_empty(...) cdc_heap_empty(__VA_ARGS__)
244 #define heap_size(...) cdc_heap_size(__VA_ARGS__)
245 
246 // Modifiers
247 #define heap_extract_top(...) cdc_heap_extract_top(__VA_ARGS__)
248 #define heap_riinsert(...) cdc_heap_riinsert(__VA_ARGS__)
249 #define heap_insert(...) cdc_heap_insert(__VA_ARGS__)
250 #define heap_change_key(...) cdc_heap_change_key(__VA_ARGS__)
251 #define heap_clear(...) cdc_heap_clear(__VA_ARGS__)
252 #define heap_swap(...) cdc_heap_swap(__VA_ARGS__)
253 
254 // Operations
255 #define heap_merge(...) cdc_heap_merge(__VA_ARGS__)
256 
257 #define heap_is_heap(...) cdc_heap_is_heap(__VA_ARGS__)
258 
259 // Iterators
260 #define heap_iter_data(...) cdc_heap_iter_data(__VA_ARGS__)
261 #define heap_iter_is_eq(...) cdc_heap_iter_is_eq(__VA_ARGS__)
262 #endif
263 
264 #endif // CDCONTAINERS_INCLUDE_CDCONTAINERS_HEAP_H
enum cdc_stat cdc_heap_merge(struct cdc_heap *h, struct cdc_heap *other)
Merges two heaps. In the heap h will be the result of the merger, and the heap other will remain empt...
void cdc_heap_change_key(struct cdc_heap *h, struct cdc_heap_iter *pos, void *key)
Changes the item key on the pos position in the heap.
static enum cdc_stat cdc_heap_insert(struct cdc_heap *h, void *key)
Inserts element key to the heap.
Definition: heap.h:159
enum cdc_stat cdc_heap_ctorl(struct cdc_heap **h, struct cdc_data_info *info,...)
Constructs a heap, initialized by an arbitrary number of pointers. The last item must be NULL...
struct cdc_array * container
Definition: heap.h:52
enum cdc_stat cdc_heap_ctorv(struct cdc_heap **h, struct cdc_data_info *info, va_list args)
Constructs a heap, initialized by args. The last item must be NULL.
void cdc_heap_swap(struct cdc_heap *a, struct cdc_heap *b)
Swaps heaps a and b. This operation is very fast and never fails.
static bool cdc_heap_iter_is_eq(struct cdc_heap_iter *it1, struct cdc_heap_iter *it2)
Returns false if the iterator it1 equal to the iterator it2, otherwise returns false.
Definition: heap.h:220
void cdc_heap_clear(struct cdc_heap *h)
Removes all the elements from the heap.
The cdc_array is a struct and functions that provide a dynamic array.
enum cdc_stat cdc_heap_ctor(struct cdc_heap **h, struct cdc_data_info *info)
Constructs an empty heap.
The cdc_array is service struct.
Definition: array.h:50
The cdc_heap_iter struct.
Definition: heap.h:51
The cdc_heap struct.
Definition: heap.h:42
void cdc_heap_extract_top(struct cdc_heap *h)
Extracts the top item from the heap. This function assumes that the heap isn&#39;t empty.
static void * cdc_array_front(struct cdc_array *v)
Returns a first element in the array.
Definition: array.h:142
struct cdc_array * array
Definition: heap.h:43
cdc_stat
Definition: status.h:24
static size_t cdc_heap_size(struct cdc_heap *h)
Returns the number of items in the heap.
Definition: heap.h:112
enum cdc_stat cdc_heap_riinsert(struct cdc_heap *h, void *key, struct cdc_heap_iter *ret)
Inserts element key to the heap. Write an iterator pointing to a new element in the ret...
bool cdc_heap_is_heap(struct cdc_heap *h)
Checks the heap property.
The cdc_data_info struct used to initialize contaners.
Definition: common.h:71
static size_t cdc_array_size(struct cdc_array *v)
Returns the number of elements in the array.
Definition: array.h:209
static void * cdc_heap_iter_data(struct cdc_heap_iter *it)
Returns a pointer to the key of current item.
Definition: heap.h:209
size_t current
Definition: heap.h:53
static bool cdc_array_empty(struct cdc_array *v)
Checks if the array has no elements.
Definition: array.h:197
static bool cdc_heap_empty(struct cdc_heap *h)
Returns true if the heap has size 0; otherwise returns false.
Definition: heap.h:124
static void * cdc_array_get(struct cdc_array *v, size_t index)
Returns an element at index position in the array.
Definition: array.h:118
static void * cdc_heap_top(struct cdc_heap *h)
Returns a pointer to the heap&#39;s top item. This function assumes that the heap isn&#39;t empty...
Definition: heap.h:99
void cdc_heap_dtor(struct cdc_heap *h)
Destroys the heap.