cdcontainers  0.1.1
Library of data containers and collections for C programming language.
binomial-heap.h
Go to the documentation of this file.
1 // The MIT License (MIT)
2 // Copyright (c) 2018 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.
27 #ifndef CDCONTAINERS_INCLUDE_CDCONTAINERS_BINOMIAL_HEAP_H
28 #define CDCONTAINERS_INCLUDE_CDCONTAINERS_BINOMIAL_HEAP_H
29 
30 #include <cdcontainers/common.h>
31 #include <cdcontainers/status.h>
32 
33 #include <assert.h>
34 #include <stdarg.h>
35 #include <stdbool.h>
36 #include <stdlib.h>
37 
47  size_t degree;
48  void *key;
49 };
50 
59  size_t size;
61 };
62 
71 };
72 
81  struct cdc_data_info *info);
82 
92  struct cdc_data_info *info, ...);
93 
103  struct cdc_data_info *info, va_list args);
104 
110 
111 // Element access
118 static inline void *cdc_binomial_heap_top(struct cdc_binomial_heap *h)
119 {
120  assert(h != NULL);
121 
122  return h->top->key;
123 }
124 
125 // Capacity
131 static inline size_t cdc_binomial_heap_size(struct cdc_binomial_heap *h)
132 {
133  assert(h != NULL);
134 
135  return h->size;
136 }
137 
143 static inline bool cdc_binomial_heap_empty(struct cdc_binomial_heap *h)
144 {
145  assert(h != NULL);
146 
147  return h->size == 0;
148 }
149 
150 // Modifiers
159 
171  struct cdc_binomial_heap_iter *ret);
172 
181  struct cdc_binomial_heap *h, void *key)
182 {
183  assert(h != NULL);
184 
185  return cdc_binomial_heap_riinsert(h, key, NULL);
186 }
187 
196  struct cdc_binomial_heap_iter *pos,
197  void *key);
198 
204 
212  struct cdc_binomial_heap *b);
213 
214 // Operations
222  struct cdc_binomial_heap *other);
223 
230 
231 // Iterators
235 static inline void *cdc_binomial_heap_iter_data(
236  struct cdc_binomial_heap_iter *it)
237 {
238  assert(it != NULL);
239 
240  return it->current->key;
241 }
242 
247 static inline bool cdc_binomial_heap_iter_is_eq(
248  struct cdc_binomial_heap_iter *it1, struct cdc_binomial_heap_iter *it2)
249 {
250  assert(it1 != NULL);
251  assert(it2 != NULL);
252 
253  return it1->container == it2->container && it1->current == it2->current;
254 }
255 
256 // Short names
257 #ifdef CDC_USE_SHORT_NAMES
258 typedef struct cdc_binomial_heap binomial_heap_t;
259 typedef struct cdc_binomial_heap_iter binomial_heap_iter;
260 
261 #define binomial_heap_ctor(...) cdc_binomial_heap_ctor(__VA_ARGS__)
262 #define binomial_heap_ctorl(...) cdc_binomial_heap_ctorl(__VA_ARGS__)
263 #define binomial_heap_ctorv(...) cdc_binomial_heap_ctorv(__VA_ARGS__)
264 #define binomial_heap_dtor(...) cdc_binomial_heap_dtor(__VA_ARGS__)
265 
266 // Element access
267 #define binomial_heap_top(...) cdc_binomial_heap_top(__VA_ARGS__)
268 
269 // Capacity
270 #define binomial_heap_empty(...) cdc_binomial_heap_empty(__VA_ARGS__)
271 #define binomial_heap_size(...) cdc_binomial_heap_size(__VA_ARGS__)
272 
273 // Modifiers
274 #define binomial_heap_extract_top(...) \
275  cdc_binomial_heap_extract_top(__VA_ARGS__)
276 #define binomial_heap_riinsert(...) cdc_binomial_heap_riinsert(__VA_ARGS__)
277 #define binomial_heap_insert(...) cdc_binomial_heap_insert(__VA_ARGS__)
278 #define binomial_heap_change_key(...) cdc_binomial_heap_change_key(__VA_ARGS__)
279 #define binomial_heap_clear(...) cdc_binomial_heap_clear(__VA_ARGS__)
280 #define binomial_heap_swap(...) cdc_binomial_heap_swap(__VA_ARGS__)
281 
282 // Operations
283 #define binomial_heap_merge(...) cdc_binomial_heap_merge(__VA_ARGS__)
284 
285 #define binomial_heap_is_heap(...) cdc_binomial_heap_is_heap(__VA_ARGS__)
286 
287 // Iterators
288 #define binomial_heap_iter_data(...) cdc_binomial_heap_iter_data(__VA_ARGS__)
289 #define binomial_heap_iter_is_eq(...) cdc_binomial_heap_iter_is_eq(__VA_ARGS__)
290 #endif
291 
292 #endif // CDCONTAINERS_INCLUDE_CDCONTAINERS_BINOMIAL_HEAP_H
enum cdc_stat cdc_binomial_heap_extract_top(struct cdc_binomial_heap *h)
Extracts the top item from the binomial heap. This function assumes that the binomial heap isn&#39;t empt...
enum cdc_stat cdc_binomial_heap_ctor(struct cdc_binomial_heap **h, struct cdc_data_info *info)
Constructs an empty binomial heap.
enum cdc_stat cdc_binomial_heap_riinsert(struct cdc_binomial_heap *h, void *key, struct cdc_binomial_heap_iter *ret)
Inserts element key to the binomial heap. Write an iterator pointing to a new element in the ret...
void cdc_binomial_heap_clear(struct cdc_binomial_heap *h)
Removes all the elements from the binomial heap.
static void * cdc_binomial_heap_iter_data(struct cdc_binomial_heap_iter *it)
Returns a pointer to the key of current item.
Definition: binomial-heap.h:235
static size_t cdc_binomial_heap_size(struct cdc_binomial_heap *h)
Returns the number of items in the binomial heap.
Definition: binomial-heap.h:131
struct cdc_binomial_heap_node * parent
Definition: binomial-heap.h:44
static bool cdc_binomial_heap_iter_is_eq(struct cdc_binomial_heap_iter *it1, struct cdc_binomial_heap_iter *it2)
Returns false if the iterator it1 equal to the iterator it2, otherwise returns false.
Definition: binomial-heap.h:247
struct cdc_binomial_heap_node * current
Definition: binomial-heap.h:70
struct cdc_binomial_heap * container
Definition: binomial-heap.h:69
struct cdc_binomial_heap_node * child
Definition: binomial-heap.h:45
void cdc_binomial_heap_merge(struct cdc_binomial_heap *h, struct cdc_binomial_heap *other)
Merges two heaps. In the heap h will be the result of the merger, and the heap other will remain empt...
static enum cdc_stat cdc_binomial_heap_insert(struct cdc_binomial_heap *h, void *key)
Inserts element key to the binomial heap.
Definition: binomial-heap.h:180
struct cdc_binomial_heap_node * top
Definition: binomial-heap.h:58
void cdc_binomial_heap_dtor(struct cdc_binomial_heap *h)
Destroys the binomial heap.
enum cdc_stat cdc_binomial_heap_ctorl(struct cdc_binomial_heap **h, struct cdc_data_info *info,...)
Constructs a binomial heap, initialized by an arbitrary number of pointers. The last item must be NUL...
static bool cdc_binomial_heap_empty(struct cdc_binomial_heap *h)
Returns true if the binomial heap has size 0; otherwise returns false.
Definition: binomial-heap.h:143
struct cdc_binomial_heap_node * sibling
Definition: binomial-heap.h:46
struct cdc_binomial_heap_node * root
Definition: binomial-heap.h:57
size_t degree
Definition: binomial-heap.h:47
void * key
Definition: binomial-heap.h:48
cdc_stat
Definition: status.h:24
The cdc_binomial_heap_node struct.
Definition: binomial-heap.h:43
The cdc_binomial_heap_iter struct.
Definition: binomial-heap.h:68
struct cdc_data_info * dinfo
Definition: binomial-heap.h:60
void cdc_binomial_heap_swap(struct cdc_binomial_heap *a, struct cdc_binomial_heap *b)
Swaps binomial heaps a and b. This operation is very fast and never fails.
size_t size
Definition: binomial-heap.h:59
The cdc_data_info struct used to initialize contaners.
Definition: common.h:71
enum cdc_stat cdc_binomial_heap_ctorv(struct cdc_binomial_heap **h, struct cdc_data_info *info, va_list args)
Constructs a binomial heap, initialized by args. The last item must be NULL.
The cdc_binomial_heap struct.
Definition: binomial-heap.h:56
void cdc_binomial_heap_change_key(struct cdc_binomial_heap *h, struct cdc_binomial_heap_iter *pos, void *key)
Changes the item key on the pos position in the binomial heap.
bool cdc_binomial_heap_is_heap(struct cdc_binomial_heap *h)
Checks the heap property.
static void * cdc_binomial_heap_top(struct cdc_binomial_heap *h)
Returns a pointer to the binomial heap&#39;s top item. This function assumes that the binomial heap isn&#39;t...
Definition: binomial-heap.h:118