Commit | Line | Data |
---|---|---|
a7868768 BA |
1 | /** |
2 | * @file BufferTop.c | |
3 | */ | |
4 | ||
5 | #include "cgds/BufferTop.h" | |
6 | ||
eed1b5d2 | 7 | // NOTE: no init() method here, since BufferTop has no specific initialization |
a7868768 | 8 | |
eed1b5d2 BA |
9 | BufferTop* _buffertop_new( |
10 | size_t dataSize, UInt capacity, OrderType bType, UInt arity) | |
a7868768 | 11 | { |
e45132ac BA |
12 | BufferTop* bufferTop = (BufferTop*) safe_malloc(sizeof (BufferTop)); |
13 | bufferTop->capacity = capacity; | |
14 | bufferTop->bType = bType; //redondant, but facilitate understanding | |
15 | // WARNING: heap must have opposite type: "smallest" element first | |
eed1b5d2 BA |
16 | bufferTop->heap = |
17 | _heap_new(dataSize, (bType == MAX_T ? MIN_T : MAX_T), arity); | |
e45132ac | 18 | return bufferTop; |
a7868768 BA |
19 | } |
20 | ||
21 | BufferTop* buffertop_copy(BufferTop* bufferTop) | |
22 | { | |
1d60b10a BA |
23 | BufferTop* bufferTopCopy = (BufferTop*) safe_malloc(sizeof (BufferTop)); |
24 | bufferTopCopy->capacity = bufferTop->capacity; | |
25 | bufferTopCopy->bType = bufferTop->bType; | |
e45132ac BA |
26 | bufferTopCopy->heap = heap_copy(bufferTop->heap); |
27 | return bufferTopCopy; | |
a7868768 BA |
28 | } |
29 | ||
30 | List* buffertop_2list(BufferTop* bufferTop) | |
31 | { | |
e45132ac BA |
32 | // Copy the buffer, and then use the copy to build the list |
33 | BufferTop* bufferTopCopy = buffertop_copy(bufferTop); | |
1d60b10a | 34 | List* bufferInList = _list_new(bufferTop->heap->items->dataSize); |
e45132ac BA |
35 | while (!buffertop_empty(bufferTopCopy)) |
36 | { | |
1d60b10a | 37 | void* topItem = _heap_top(bufferTopCopy->heap).item; |
e45132ac BA |
38 | // NOTE: list_insert_front(), to reverse (wrong) items order |
39 | // ==> in the returned list, top element is at head. | |
40 | _list_insert_front(bufferInList, topItem); | |
41 | buffertop_pop(bufferTopCopy); | |
42 | } | |
43 | buffertop_destroy(bufferTopCopy); | |
44 | return bufferInList; | |
a7868768 BA |
45 | } |
46 | ||
1ff641f9 | 47 | bool buffertop_empty(BufferTop* bufferTop) |
a7868768 | 48 | { |
e45132ac | 49 | return (heap_size(bufferTop->heap) == 0); |
a7868768 BA |
50 | } |
51 | ||
52 | UInt buffertop_size(BufferTop* bufferTop) | |
53 | { | |
e45132ac | 54 | return heap_size(bufferTop->heap); |
a7868768 BA |
55 | } |
56 | ||
57 | void _buffertop_tryadd(BufferTop* bufferTop, void* item, Real value) | |
58 | { | |
1d60b10a BA |
59 | if (heap_size(bufferTop->heap) >= bufferTop->capacity) { |
60 | Real topValue = *((Real*) (bufferTop->heap->values->datas)); | |
61 | if ( | |
62 | (bufferTop->bType == MIN_T && value >= topValue) || | |
63 | (bufferTop->bType == MAX_T && value <= topValue) | |
64 | ) { | |
65 | // Shortcut : if value "worse" than top->value and buffer is full, skip | |
66 | return; | |
67 | } | |
e45132ac | 68 | } |
a7868768 | 69 | |
eed1b5d2 | 70 | // Insertion somewhere in the item-values heap |
e45132ac | 71 | _heap_insert(bufferTop->heap, item, value); |
a7868768 | 72 | |
e45132ac | 73 | if (heap_size(bufferTop->heap) > bufferTop->capacity) |
eed1b5d2 | 74 | // We must remove current root |
e45132ac | 75 | heap_pop(bufferTop->heap); |
a7868768 BA |
76 | } |
77 | ||
1d60b10a | 78 | ItemValue _buffertop_first(BufferTop* bufferTop) |
a7868768 | 79 | { |
1d60b10a | 80 | return _heap_top(bufferTop->heap); |
a7868768 BA |
81 | } |
82 | ||
83 | void buffertop_pop(BufferTop* bufferTop) | |
84 | { | |
e45132ac | 85 | heap_pop(bufferTop->heap); |
a7868768 BA |
86 | } |
87 | ||
88 | void buffertop_clear(BufferTop* bufferTop) | |
89 | { | |
e45132ac | 90 | heap_clear(bufferTop->heap); |
a7868768 BA |
91 | } |
92 | ||
93 | void buffertop_destroy(BufferTop* bufferTop) | |
94 | { | |
e45132ac BA |
95 | heap_destroy(bufferTop->heap); |
96 | safe_free(bufferTop); | |
a7868768 | 97 | } |