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