Add basic Set implementation - TODO: add iterators for Set and HashTable
[cgds.git] / src / BufferTop.h
CommitLineData
a7868768
BA
1/**
2 * @file BufferTop.h
3 */
4
5#ifndef CGDS_BUFFER_TOP_H
6#define CGDS_BUFFER_TOP_H
7
8#include <stdlib.h>
9#include <string.h>
10#include "cgds/types.h"
11#include "cgds/Heap.h"
12#include "cgds/List.h"
13#include "cgds/safe_alloc.h"
14
15/**
16 * @brief Data structure to store top (MAX or MIN) elements in a buffer.
17 */
18typedef struct BufferTop {
e45132ac
BA
19 UInt capacity; ///< Buffer capacity (in items count).
20 OrderType bType; ///< Type of buffer: keep max or min items (MAX_T or MIN_T).
21 Heap* heap; ///< Item-ValueS are internally organized into a heap.
a7868768
BA
22} BufferTop;
23
24/**
25 * @brief Return an allocated and initialized buffer.
26 */
27BufferTop* _buffertop_new(
e45132ac
BA
28 size_t dataSize, ///< Size in bytes of a buffer element.
29 UInt capacity, ///< Maximum number of elements that the buffer can contain.
30 OrderType bType, ///< Type of buffer: keep max or min items (MAX_T or MIN_T).
31 UInt arity ///< Arity of the wrapped heap: any integer >=2.
a7868768
BA
32);
33
34/**
35 * @brief Return an allocated and initialized buffer.
36 * @param type Type of a buffer item (int, char*, ...).
37 * @param capacity Maximum number of elements that the buffer can contain.
1ff641f9 38 * @param bType type of buffer: keep max or min items (MAX_T or MIN_T).
a7868768 39 * @param arity Arity of the wrapped heap: any integer >=2.
1ff641f9 40 *
e45132ac 41 * Usage: BufferTop* buffertop_new(<Type> type, UInt capacity, OrderTypebType, UInt arity)
a7868768
BA
42 */
43#define buffertop_new(type, capacity, bType, arity) \
eed1b5d2 44 _buffertop_new(sizeof(type), capacity, bType, arity)
a7868768
BA
45
46/**
1ff641f9 47 * @brief Copy constructor (shallow copy, ok for basic types).
a7868768
BA
48 */
49BufferTop* buffertop_copy(
e45132ac 50 BufferTop* bufferTop ///< "this" pointer.
a7868768
BA
51);
52
53/**
54 * @brief Turn the buffer into a list to scan its content linearly.
55 */
56List* buffertop_2list(
e45132ac 57 BufferTop* bufferTop ///< "this" pointer.
a7868768
BA
58);
59
60/**
61 * @brief Check if the buffer is empty.
62 */
1ff641f9 63bool buffertop_empty(
e45132ac 64 BufferTop* bufferTop ///< "this" pointer.
a7868768
BA
65);
66
67/**
68 * @brief Return the size of current buffer (<= capacity).
69 */
70UInt buffertop_size(
e45132ac 71 BufferTop* bufferTop ///< "this" pointer.
a7868768
BA
72);
73
74/**
75 * @brief (Try to) add an item-value in the buffer.
76 */
77void _buffertop_tryadd(
e45132ac
BA
78 BufferTop* bufferTop, ///< "this" pointer.
79 void* item, ///< Pointer to an item of type as defined in the constructor.
80 Real value ///< Value associated with the item.
a7868768
BA
81);
82
83/**
84 * @brief (Try to) add an item-value in the buffer.
85 * @param bufferTop "this" pointer.
86 * @param item Item of type as defined in the constructor.
87 * @param value Value associated with the item.
1ff641f9 88 *
a7868768
BA
89 * Usage: void buffertop_tryadd(BufferTop* bufferTop, void item, Real value)
90 */
91#define buffertop_tryadd(bufferTop, item, value) \
92{ \
eed1b5d2 93 typeof(item) tmp = item; \
e45132ac 94 _buffertop_tryadd(bufferTop, &tmp, value); \
a7868768
BA
95}
96
97/**
98 * @brief Return the top ("worst among best") ItemValue inside current buffer.
99 */
100ItemValue* buffertop_first_raw(
e45132ac 101 BufferTop* bufferTop ///< "this" pointer.
a7868768
BA
102);
103
104/**
105 * @brief Set item_ to the top ("worst among best") item inside current buffer.
106 * @param bufferTop "this" pointer.
107 * @param item_ Variable to be assigned.
1ff641f9 108 *
a7868768
BA
109 * Usage: void buffertop_first(BufferTop* bufferTop, void item)
110 */
111#define buffertop_first(bufferTop, item_) \
112{ \
e45132ac
BA
113 void* pItem = buffertop_first_raw(bufferTop)->item; \
114 item_ = *((typeof(&item_))pItem); \
a7868768
BA
115}
116
117/**
118 * @brief Remove the top ("worst among best") item-value inside the buffer.
119 */
120void buffertop_pop(
e45132ac 121 BufferTop* bufferTop ///< "this" pointer.
a7868768
BA
122);
123
124/**
125 * @brief Clear the entire buffer.
126 */
127void buffertop_clear(
e45132ac 128 BufferTop* bufferTop ///< "this" pointer.
a7868768
BA
129);
130
131/**
132 * @brief Destroy the buffer: clear it, and free 'bufferTop' pointer.
133 */
134void buffertop_destroy(
e45132ac 135 BufferTop* bufferTop ///< "this" pointer.
a7868768
BA
136);
137
138#endif