cdcontainers  0.1.1
Library of data containers and collections for C programming language.
treap.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.
26 #ifndef CDCONTAINERS_INCLUDE_CDCONTAINERS_TREAP_H
27 #define CDCONTAINERS_INCLUDE_CDCONTAINERS_TREAP_H
28 
29 #include <cdcontainers/common.h>
30 #include <cdcontainers/status.h>
31 
32 #include <assert.h>
33 #include <stdarg.h>
34 #include <stdbool.h>
35 
42 typedef int (*cdc_priority_fn_t)(void *);
43 
53  void *key;
54  void *value;
55  int priority;
56 };
57 
63 struct cdc_treap {
65  size_t size;
68 };
69 
79 };
80 
82  struct cdc_treap_iter first;
83  bool second;
84 };
85 
86 // Base
98 enum cdc_stat cdc_treap_ctor(struct cdc_treap **t, struct cdc_data_info *info);
99 
120 enum cdc_stat cdc_treap_ctorl(struct cdc_treap **t, struct cdc_data_info *info,
121  ...);
122 
131 enum cdc_stat cdc_treap_ctorv(struct cdc_treap **t, struct cdc_data_info *info,
132  va_list args);
133 
142 enum cdc_stat cdc_treap_ctor1(struct cdc_treap **t, struct cdc_data_info *info,
143  cdc_priority_fn_t prior);
144 
155 enum cdc_stat cdc_treap_ctorl1(struct cdc_treap **t, struct cdc_data_info *info,
156  cdc_priority_fn_t prior, ...);
157 
167 enum cdc_stat cdc_treap_ctorv1(struct cdc_treap **t, struct cdc_data_info *info,
168  cdc_priority_fn_t prior, va_list args);
169 
174 void cdc_treap_dtor(struct cdc_treap *t);
177 // Lookup
190 enum cdc_stat cdc_treap_get(struct cdc_treap *t, void *key, void **value);
191 
200 size_t cdc_treap_count(struct cdc_treap *t, void *key);
201 
210 void cdc_treap_find(struct cdc_treap *t, void *key, struct cdc_treap_iter *it);
213 // Capacity
223 static inline size_t cdc_treap_size(struct cdc_treap *t)
224 {
225  assert(t != NULL);
226 
227  return t->size;
228 }
229 
235 static inline bool cdc_treap_empty(struct cdc_treap *t)
236 {
237  assert(t != NULL);
238 
239  return t->size == 0;
240 }
243 // Modifiers
252 void cdc_treap_clear(struct cdc_treap *t);
253 
266 enum cdc_stat cdc_treap_insert(struct cdc_treap *t, void *key, void *value,
267  struct cdc_pair_treap_iter_bool *ret);
268 
282 enum cdc_stat cdc_treap_insert1(struct cdc_treap *t, void *key, void *value,
283  struct cdc_treap_iter *it, bool *inserted);
284 
297 enum cdc_stat cdc_treap_insert_or_assign(struct cdc_treap *t, void *key,
298  void *value,
299  struct cdc_pair_treap_iter_bool *ret);
300 
314 enum cdc_stat cdc_treap_insert_or_assign1(struct cdc_treap *t, void *key,
315  void *value,
316  struct cdc_treap_iter *it,
317  bool *inserted);
318 
325 size_t cdc_treap_erase(struct cdc_treap *t, void *key);
326 
332 void cdc_treap_swap(struct cdc_treap *a, struct cdc_treap *b);
335 // Iterators
345 void cdc_treap_begin(struct cdc_treap *t, struct cdc_treap_iter *it);
346 
352 void cdc_treap_end(struct cdc_treap *t, struct cdc_treap_iter *it);
355 // Iterators
365 void cdc_treap_iter_next(struct cdc_treap_iter *it);
366 
371 void cdc_treap_iter_prev(struct cdc_treap_iter *it);
372 
380 static inline bool cdc_treap_iter_has_next(struct cdc_treap_iter *it)
381 {
382  assert(it != NULL);
383 
384  return it->current != NULL;
385 }
386 
394 static inline bool cdc_treap_iter_has_prev(struct cdc_treap_iter *it)
395 {
396  assert(it != NULL);
397 
398  return it->prev != NULL;
399 }
400 
406 static inline void *cdc_treap_iter_key(struct cdc_treap_iter *it)
407 {
408  assert(it != NULL);
409 
410  return it->current->key;
411 }
412 
418 static inline void *cdc_treap_iter_value(struct cdc_treap_iter *it)
419 {
420  assert(it != NULL);
421 
422  return it->current->value;
423 }
424 
430 static inline struct cdc_pair cdc_treap_iter_key_value(
431  struct cdc_treap_iter *it)
432 {
433  assert(it != NULL);
434 
435  struct cdc_pair pair = {it->prev->key, it->prev->value};
436  return pair;
437 }
438 
447 static inline bool cdc_treap_iter_is_eq(struct cdc_treap_iter *it1,
448  struct cdc_treap_iter *it2)
449 {
450  assert(it1 != NULL);
451  assert(it2 != NULL);
452 
453  return it1->container == it2->container && it1->prev == it2->prev &&
454  it1->current == it2->current;
455 }
458 // Short names
459 #ifdef CDC_USE_SHORT_NAMES
460 typedef struct cdc_treap treap_t;
461 typedef struct cdc_treap_iter treap_iter_t;
462 typedef struct cdc_pair_treap_iter pair_treap_iter_t;
463 typedef struct cdc_pair_treap_iter_bool pair_treap_iter_bool_t;
464 
465 // Base
466 #define treap_ctor(...) cdc_treap_ctor(__VA_ARGS__)
467 #define treap_ctorv(...) cdc_treap_ctorv(__VA_ARGS__)
468 #define treap_ctorl(...) cdc_treap_ctorl(__VA_ARGS__)
469 #define treap_ctor1(...) cdc_treap_ctor1(__VA_ARGS__)
470 #define treap_ctorv1(...) cdc_treap_ctorv1(__VA_ARGS__)
471 #define treap_ctorl1(...) cdc_treap_ctorl1(__VA_ARGS__)
472 #define treap_dtor(...) cdc_treap_dtor(__VA_ARGS__)
473 
474 // Lookup
475 #define treap_get(...) cdc_treap_get(__VA_ARGS__)
476 #define treap_count(...) cdc_treap_count(__VA_ARGS__)
477 #define treap_find(...) cdc_treap_find(__VA_ARGS__)
478 
479 // Capacity
480 #define treap_size(...) cdc_treap_size(__VA_ARGS__)
481 #define treap_empty(...) cdc_treap_empty(__VA_ARGS__)
482 
483 // Modifiers
484 #define treap_clear(...) cdc_treap_clear(__VA_ARGS__)
485 #define treap_insert(...) cdc_treap_insert(__VA_ARGS__)
486 #define treap_insert1(...) cdc_treap_insert1(__VA_ARGS__)
487 #define treap_insert_or_assign(...) cdc_treap_insert_or_assign(__VA_ARGS__)
488 #define treap_insert_or_assign1(...) cdc_treap_insert_or_assign1(__VA_ARGS__)
489 #define treap_erase(...) cdc_treap_erase(__VA_ARGS__)
490 #define treap_swap(...) cdc_treap_swap(__VA_ARGS__)
491 
492 // Iterators
493 #define treap_begin(...) cdc_treap_begin(__VA_ARGS__)
494 #define treap_end(...) cdc_treap_end(__VA_ARGS__)
495 
496 // Iterators
497 #define treap_iter_next(...) cdc_treap_iter_next(__VA_ARGS__)
498 #define treap_iter_has_next(...) cdc_treap_iter_has_next(__VA_ARGS__)
499 #define treap_iter_key(...) cdc_treap_iter_key(__VA_ARGS__)
500 #define treap_iter_value(...) cdc_treap_iter_value(__VA_ARGS__)
501 #define treap_iter_key_value(...) cdc_treap_iter_key_value(__VA_ARGS__)
502 #define treap_iter_is_eq(...) cdc_treap_iter_is_eq(__VA_ARGS__)
503 #endif
504 
505 #endif // CDSTRUCTURES_INCLUDE_CDCONTAINERS_VECTOR_H
void cdc_treap_end(struct cdc_treap *t, struct cdc_treap_iter *it)
Initializes the iterator to the end.
enum cdc_stat cdc_treap_insert(struct cdc_treap *t, void *key, void *value, struct cdc_pair_treap_iter_bool *ret)
Inserts an element into the container, if the container doesn&#39;t already contain an element with an eq...
void * key
Definition: treap.h:53
cdc_priority_fn_t prior
Definition: treap.h:66
bool second
Definition: treap.h:83
struct cdc_data_info * dinfo
Definition: treap.h:67
enum cdc_stat cdc_treap_get(struct cdc_treap *t, void *key, void **value)
Returns a value that is mapped to a key. If the key does not exist, then NULL will return...
void cdc_treap_dtor(struct cdc_treap *t)
Destroys the treap.
enum cdc_stat cdc_treap_ctorl(struct cdc_treap **t, struct cdc_data_info *info,...)
Constructs a treap, initialized by an variable number of pointers on cdc_pair&#39;s(first - key...
static struct cdc_pair cdc_treap_iter_key_value(struct cdc_treap_iter *it)
Returns a pair, where first - key, second - value.
Definition: treap.h:430
static void * cdc_treap_iter_key(struct cdc_treap_iter *it)
Returns an item&#39;s key.
Definition: treap.h:406
void cdc_treap_swap(struct cdc_treap *a, struct cdc_treap *b)
Swaps treaps a and b. This operation is very fast and never fails.
struct cdc_treap_node * parent
Definition: treap.h:50
enum cdc_stat cdc_treap_ctorv(struct cdc_treap **t, struct cdc_data_info *info, va_list args)
Constructs a treap, initialized by args. The last item must be CDC_END.
static bool cdc_treap_iter_has_next(struct cdc_treap_iter *it)
Returns true if there is at least one element ahead of the iterator, i.e. the iterator is not at the ...
Definition: treap.h:380
Definition: treap.h:81
The cdc_treap is service struct.
Definition: treap.h:63
static bool cdc_treap_iter_has_prev(struct cdc_treap_iter *it)
Returns true if there is at least one element behind the iterator, i.e. the iterator is not at the fr...
Definition: treap.h:394
Definition: common.h:60
struct cdc_treap_node * current
Definition: treap.h:78
size_t cdc_treap_erase(struct cdc_treap *t, void *key)
Removes the element (if one exists) with the key equivalent to key.
void cdc_treap_begin(struct cdc_treap *t, struct cdc_treap_iter *it)
Initializes the iterator to the beginning.
The cdc_treap_iter is service struct.
Definition: treap.h:75
enum cdc_stat cdc_treap_insert_or_assign1(struct cdc_treap *t, void *key, void *value, struct cdc_treap_iter *it, bool *inserted)
Inserts an element or assigns to the current element if the key already exists.
struct cdc_treap_node * root
Definition: treap.h:64
size_t size
Definition: treap.h:65
struct cdc_treap_node * left
Definition: treap.h:51
cdc_stat
Definition: status.h:24
void cdc_treap_iter_prev(struct cdc_treap_iter *it)
Advances the iterator to the previous element in the treap.
void cdc_treap_clear(struct cdc_treap *t)
Removes all the elements from the treap.
void cdc_treap_iter_next(struct cdc_treap_iter *it)
Advances the iterator to the next element in the treap.
void cdc_treap_find(struct cdc_treap *t, void *key, struct cdc_treap_iter *it)
Finds an element with key equivalent to key.
enum cdc_stat cdc_treap_ctorl1(struct cdc_treap **t, struct cdc_data_info *info, cdc_priority_fn_t prior,...)
Constructs a treap, initialized by an variable number of pointers on cdc_pair&#39;s(first - key...
int priority
Definition: treap.h:55
static bool cdc_treap_empty(struct cdc_treap *t)
Checks if the treap has no elements.
Definition: treap.h:235
static size_t cdc_treap_size(struct cdc_treap *t)
Returns the number of items in the treap.
Definition: treap.h:223
The cdc_treap_node is service struct.
Definition: treap.h:49
static void * cdc_treap_iter_value(struct cdc_treap_iter *it)
Returns an item&#39;s value.
Definition: treap.h:418
struct cdc_treap_node * prev
Definition: treap.h:77
enum cdc_stat cdc_treap_insert_or_assign(struct cdc_treap *t, void *key, void *value, struct cdc_pair_treap_iter_bool *ret)
Inserts an element or assigns to the current element if the key already exists.
enum cdc_stat cdc_treap_ctor1(struct cdc_treap **t, struct cdc_data_info *info, cdc_priority_fn_t prior)
Constructs an empty treap.
void * value
Definition: treap.h:54
static bool cdc_treap_iter_is_eq(struct cdc_treap_iter *it1, struct cdc_treap_iter *it2)
Returns false if the iterator |it1| equal to the iterator |it2|, otherwise returns false...
Definition: treap.h:447
struct cdc_treap * container
Definition: treap.h:76
enum cdc_stat cdc_treap_ctorv1(struct cdc_treap **t, struct cdc_data_info *info, cdc_priority_fn_t prior, va_list args)
Constructs a treap, initialized by args. The last item must be CDC_END.
The cdc_data_info struct used to initialize contaners.
Definition: common.h:71
enum cdc_stat cdc_treap_ctor(struct cdc_treap **t, struct cdc_data_info *info)
Constructs an empty treap.
struct cdc_treap_node * right
Definition: treap.h:52
enum cdc_stat cdc_treap_insert1(struct cdc_treap *t, void *key, void *value, struct cdc_treap_iter *it, bool *inserted)
Inserts an element into the container, if the container doesn&#39;t already contain an element with an eq...
size_t cdc_treap_count(struct cdc_treap *t, void *key)
Returns the number of elements with key that compares equal to the specified argument key...
int(* cdc_priority_fn_t)(void *)
Definition: treap.h:42