cdcontainers  0.1.1
Library of data containers and collections for C programming language.
pairing-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_PAIRING_HEAP_H
28 #define CDCONTAINERS_INCLUDE_CDCONTAINERS_PAIRING_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  void *key;
48 };
49 
57  size_t size;
59 };
60 
69 };
70 
79  struct cdc_data_info *info);
80 
90  struct cdc_data_info *info, ...);
91 
101  struct cdc_data_info *info, va_list args);
102 
108 
109 // Element access
116 static inline void *cdc_pairing_heap_top(struct cdc_pairing_heap *h)
117 {
118  assert(h != NULL);
119 
120  return h->root->key;
121 }
122 
123 // Capacity
129 static inline size_t cdc_pairing_heap_size(struct cdc_pairing_heap *h)
130 {
131  assert(h != NULL);
132 
133  return h->size;
134 }
135 
141 static inline bool cdc_pairing_heap_empty(struct cdc_pairing_heap *h)
142 {
143  assert(h != NULL);
144 
145  return h->size == 0;
146 }
147 
148 // Modifiers
157 
169  struct cdc_pairing_heap_iter *ret);
170 
178 static inline enum cdc_stat cdc_pairing_heap_insert(struct cdc_pairing_heap *h,
179  void *key)
180 {
181  assert(h != NULL);
182 
183  return cdc_pairing_heap_riinsert(h, key, NULL);
184 }
185 
194  struct cdc_pairing_heap_iter *pos, void *key);
195 
201 
209  struct cdc_pairing_heap *b);
210 
211 // Operations
219  struct cdc_pairing_heap *other);
220 
227 
228 // Iterators
232 static inline void *cdc_pairing_heap_iter_data(struct cdc_pairing_heap_iter *it)
233 {
234  assert(it != NULL);
235 
236  return it->current->key;
237 }
238 
243 static inline bool cdc_pairing_heap_iter_is_eq(
244  struct cdc_pairing_heap_iter *it1, struct cdc_pairing_heap_iter *it2)
245 {
246  assert(it1 != NULL);
247  assert(it2 != NULL);
248 
249  return it1->container == it2->container && it1->current == it2->current;
250 }
251 
252 // Short names
253 #ifdef CDC_USE_SHORT_NAMES
254 typedef struct cdc_pairing_heap pairing_heap_t;
255 typedef struct cdc_pairing_heap_iter pairing_heap_iter;
256 
257 #define pairing_heap_ctor(...) cdc_pairing_heap_ctor(__VA_ARGS__)
258 #define pairing_heap_ctorl(...) cdc_pairing_heap_ctorl(__VA_ARGS__)
259 #define pairing_heap_ctorv(...) cdc_pairing_heap_ctorv(__VA_ARGS__)
260 #define pairing_heap_dtor(...) cdc_pairing_heap_dtor(__VA_ARGS__)
261 
262 // Element access
263 #define pairing_heap_top(...) cdc_pairing_heap_top(__VA_ARGS__)
264 
265 // Capacity
266 #define pairing_heap_empty(...) cdc_pairing_heap_empty(__VA_ARGS__)
267 #define pairing_heap_size(...) cdc_pairing_heap_size(__VA_ARGS__)
268 
269 // Modifiers
270 #define pairing_heap_extract_top(...) cdc_pairing_heap_extract_top(__VA_ARGS__)
271 #define pairing_heap_riinsert(...) cdc_pairing_heap_riinsert(__VA_ARGS__)
272 #define pairing_heap_insert(...) cdc_pairing_heap_insert(__VA_ARGS__)
273 #define pairing_heap_change_key(...) cdc_pairing_heap_change_key(__VA_ARGS__)
274 #define pairing_heap_clear(...) cdc_pairing_heap_clear(__VA_ARGS__)
275 #define pairing_heap_swap(...) cdc_pairing_heap_swap(__VA_ARGS__)
276 
277 // Operations
278 #define pairing_heap_merge(...) cdc_pairing_heap_merge(__VA_ARGS__)
279 
280 #define pairing_heap_is_heap(...) cdc_pairing_heap_is_heap(__VA_ARGS__)
281 
282 // Iterators
283 #define pairing_heap_iter_data(...) cdc_pairing_heap_iter_data(__VA_ARGS__)
284 #define pairing_heap_iter_is_eq(...) cdc_pairing_heap_iter_is_eq(__VA_ARGS__)
285 #endif
286 
287 #endif // CDCONTAINERS_INCLUDE_CDCONTAINERS_PAIRING_HEAP_H
enum cdc_stat cdc_pairing_heap_ctorl(struct cdc_pairing_heap **h, struct cdc_data_info *info,...)
Constructs a pairing heap, initialized by an arbitrary number of pointers. The last item must be NULL...
bool cdc_pairing_heap_is_heap(struct cdc_pairing_heap *h)
Checks the heap property.
The cdc_pairing_heap_iter struct.
Definition: pairing-heap.h:66
enum cdc_stat cdc_pairing_heap_ctor(struct cdc_pairing_heap **h, struct cdc_data_info *info)
Constructs an empty pairing heap.
void cdc_pairing_heap_clear(struct cdc_pairing_heap *h)
Removes all the elements from the pairing heap.
static bool cdc_pairing_heap_iter_is_eq(struct cdc_pairing_heap_iter *it1, struct cdc_pairing_heap_iter *it2)
Returns false if the iterator it1 equal to the iterator it2, otherwise returns false.
Definition: pairing-heap.h:243
static size_t cdc_pairing_heap_size(struct cdc_pairing_heap *h)
Returns the number of items in the pairing heap.
Definition: pairing-heap.h:129
void cdc_pairing_heap_merge(struct cdc_pairing_heap *h, struct cdc_pairing_heap *other)
Merges two heaps. In the heap h will be the result of the merger, and the heap other will remain empt...
struct cdc_pairing_heap_node * current
Definition: pairing-heap.h:68
The cdc_pairing_heap_node struct.
Definition: pairing-heap.h:43
enum cdc_stat cdc_pairing_heap_extract_top(struct cdc_pairing_heap *h)
Extracts the top item from the pairing heap. This function assumes that the pairing heap isn&#39;t empty...
enum cdc_stat cdc_pairing_heap_ctorv(struct cdc_pairing_heap **h, struct cdc_data_info *info, va_list args)
Constructs a pairing heap, initialized by args. The last item must be NULL.
size_t size
Definition: pairing-heap.h:57
static bool cdc_pairing_heap_empty(struct cdc_pairing_heap *h)
Returns true if the pairing heap has size 0; otherwise returns false.
Definition: pairing-heap.h:141
void cdc_pairing_heap_swap(struct cdc_pairing_heap *a, struct cdc_pairing_heap *b)
Swaps pairing heaps a and b. This operation is very fast and never fails.
enum cdc_stat cdc_pairing_heap_riinsert(struct cdc_pairing_heap *h, void *key, struct cdc_pairing_heap_iter *ret)
Inserts element key to the pairing heap. Write an iterator pointing to a new element in the ret...
struct cdc_pairing_heap_node * root
Definition: pairing-heap.h:56
struct cdc_data_info * dinfo
Definition: pairing-heap.h:58
static void * cdc_pairing_heap_iter_data(struct cdc_pairing_heap_iter *it)
Returns a pointer to the key of current item.
Definition: pairing-heap.h:232
The cdc_pairing_heap struct.
Definition: pairing-heap.h:55
cdc_stat
Definition: status.h:24
static enum cdc_stat cdc_pairing_heap_insert(struct cdc_pairing_heap *h, void *key)
Inserts element key to the pairing heap.
Definition: pairing-heap.h:178
struct cdc_pairing_heap_node * child
Definition: pairing-heap.h:45
struct cdc_pairing_heap * container
Definition: pairing-heap.h:67
void cdc_pairing_heap_change_key(struct cdc_pairing_heap *h, struct cdc_pairing_heap_iter *pos, void *key)
Changes the item key on the pos position in the pairing heap.
struct cdc_pairing_heap_node * sibling
Definition: pairing-heap.h:46
static void * cdc_pairing_heap_top(struct cdc_pairing_heap *h)
Returns a pointer to the pairing heap&#39;s top item. This function assumes that the pairing heap isn&#39;t e...
Definition: pairing-heap.h:116
struct cdc_pairing_heap_node * parent
Definition: pairing-heap.h:44
void * key
Definition: pairing-heap.h:47
void cdc_pairing_heap_dtor(struct cdc_pairing_heap *h)
Destroys the pairing heap.
The cdc_data_info struct used to initialize contaners.
Definition: common.h:71