| 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 | |
| 9 | BufferTop* _buffertop_new( |
| 10 | size_t dataSize, UInt capacity, OrderType bType, UInt arity) |
| 11 | { |
| 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 |
| 16 | bufferTop->heap = |
| 17 | _heap_new(dataSize, (bType == MAX_T ? MIN_T : MAX_T), arity); |
| 18 | return bufferTop; |
| 19 | } |
| 20 | |
| 21 | BufferTop* buffertop_copy(BufferTop* bufferTop) |
| 22 | { |
| 23 | BufferTop* bufferTopCopy = (BufferTop*) safe_malloc(sizeof (BufferTop)); |
| 24 | bufferTopCopy->capacity = bufferTop->capacity; |
| 25 | bufferTopCopy->bType = bufferTop->bType; |
| 26 | bufferTopCopy->heap = heap_copy(bufferTop->heap); |
| 27 | return bufferTopCopy; |
| 28 | } |
| 29 | |
| 30 | List* buffertop_2list(BufferTop* bufferTop) |
| 31 | { |
| 32 | // Copy the buffer, and then use the copy to build the list |
| 33 | BufferTop* bufferTopCopy = buffertop_copy(bufferTop); |
| 34 | List* bufferInList = _list_new(bufferTop->heap->items->dataSize); |
| 35 | while (!buffertop_empty(bufferTopCopy)) |
| 36 | { |
| 37 | void* topItem = _heap_top(bufferTopCopy->heap).item; |
| 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; |
| 45 | } |
| 46 | |
| 47 | bool buffertop_empty(BufferTop* bufferTop) |
| 48 | { |
| 49 | return (heap_size(bufferTop->heap) == 0); |
| 50 | } |
| 51 | |
| 52 | UInt buffertop_size(BufferTop* bufferTop) |
| 53 | { |
| 54 | return heap_size(bufferTop->heap); |
| 55 | } |
| 56 | |
| 57 | void _buffertop_tryadd(BufferTop* bufferTop, void* item, Real value) |
| 58 | { |
| 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 | } |
| 68 | } |
| 69 | |
| 70 | // Insertion somewhere in the item-values heap |
| 71 | _heap_insert(bufferTop->heap, item, value); |
| 72 | |
| 73 | if (heap_size(bufferTop->heap) > bufferTop->capacity) |
| 74 | // We must remove current root |
| 75 | heap_pop(bufferTop->heap); |
| 76 | } |
| 77 | |
| 78 | ItemValue _buffertop_first(BufferTop* bufferTop) |
| 79 | { |
| 80 | return _heap_top(bufferTop->heap); |
| 81 | } |
| 82 | |
| 83 | void buffertop_pop(BufferTop* bufferTop) |
| 84 | { |
| 85 | heap_pop(bufferTop->heap); |
| 86 | } |
| 87 | |
| 88 | void buffertop_clear(BufferTop* bufferTop) |
| 89 | { |
| 90 | heap_clear(bufferTop->heap); |
| 91 | } |
| 92 | |
| 93 | void buffertop_destroy(BufferTop* bufferTop) |
| 94 | { |
| 95 | heap_destroy(bufferTop->heap); |
| 96 | safe_free(bufferTop); |
| 97 | } |