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 | { | |
e45132ac BA |
23 | BufferTop* bufferTopCopy = _buffertop_new( |
24 | bufferTop->heap->dataSize, bufferTop->capacity, | |
25 | bufferTop->heap->hType, bufferTop->heap->arity); | |
26 | heap_destroy(bufferTopCopy->heap); //TODO: bad style... | |
27 | bufferTopCopy->heap = heap_copy(bufferTop->heap); | |
28 | return bufferTopCopy; | |
a7868768 BA |
29 | } |
30 | ||
31 | List* buffertop_2list(BufferTop* bufferTop) | |
32 | { | |
e45132ac BA |
33 | // Copy the buffer, and then use the copy to build the list |
34 | BufferTop* bufferTopCopy = buffertop_copy(bufferTop); | |
35 | List* bufferInList = _list_new(bufferTop->heap->array->dataSize); | |
36 | while (!buffertop_empty(bufferTopCopy)) | |
37 | { | |
38 | void* topItem = buffertop_first_raw(bufferTopCopy)->item; | |
39 | // NOTE: list_insert_front(), to reverse (wrong) items order | |
40 | // ==> in the returned list, top element is at head. | |
41 | _list_insert_front(bufferInList, topItem); | |
42 | buffertop_pop(bufferTopCopy); | |
43 | } | |
44 | buffertop_destroy(bufferTopCopy); | |
45 | return bufferInList; | |
a7868768 BA |
46 | } |
47 | ||
1ff641f9 | 48 | bool buffertop_empty(BufferTop* bufferTop) |
a7868768 | 49 | { |
e45132ac | 50 | return (heap_size(bufferTop->heap) == 0); |
a7868768 BA |
51 | } |
52 | ||
53 | UInt buffertop_size(BufferTop* bufferTop) | |
54 | { | |
e45132ac | 55 | return heap_size(bufferTop->heap); |
a7868768 BA |
56 | } |
57 | ||
58 | void _buffertop_tryadd(BufferTop* bufferTop, void* item, Real value) | |
59 | { | |
e45132ac BA |
60 | if (heap_size(bufferTop->heap) >= bufferTop->capacity && |
61 | ((bufferTop->bType == MIN_T && | |
62 | value >= ((ItemValue*) (bufferTop->heap->array->datas[0]))->value) | |
63 | || | |
64 | (bufferTop->bType == MAX_T && | |
65 | value <= ((ItemValue*) (bufferTop->heap->array->datas[0]))->value))) | |
66 | { | |
eed1b5d2 | 67 | // Shortcut : if value "worse" than top->value and buffer is full, skip |
e45132ac BA |
68 | return; |
69 | } | |
a7868768 | 70 | |
eed1b5d2 | 71 | // Insertion somewhere in the item-values heap |
e45132ac | 72 | _heap_insert(bufferTop->heap, item, value); |
a7868768 | 73 | |
e45132ac | 74 | if (heap_size(bufferTop->heap) > bufferTop->capacity) |
eed1b5d2 | 75 | // We must remove current root |
e45132ac | 76 | heap_pop(bufferTop->heap); |
a7868768 BA |
77 | } |
78 | ||
79 | ItemValue* buffertop_first_raw(BufferTop* bufferTop) | |
80 | { | |
e45132ac | 81 | return heap_top_raw(bufferTop->heap); |
a7868768 BA |
82 | } |
83 | ||
84 | void buffertop_pop(BufferTop* bufferTop) | |
85 | { | |
e45132ac | 86 | heap_pop(bufferTop->heap); |
a7868768 BA |
87 | } |
88 | ||
89 | void buffertop_clear(BufferTop* bufferTop) | |
90 | { | |
e45132ac | 91 | heap_clear(bufferTop->heap); |
a7868768 BA |
92 | } |
93 | ||
94 | void buffertop_destroy(BufferTop* bufferTop) | |
95 | { | |
e45132ac BA |
96 | heap_destroy(bufferTop->heap); |
97 | safe_free(bufferTop); | |
a7868768 | 98 | } |