X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=src%2FBufferTop.c;h=a7d5f95a7411eba8e149a951c55db45eff895fd6;hb=1d60b10a9482afc28f4ac027b21edf930d48440c;hp=4f1ac497671af62c443dd3200b4305b4f28284b5;hpb=e45132acdb58c076d5e06849fa51c26de9a7486d;p=cgds.git diff --git a/src/BufferTop.c b/src/BufferTop.c index 4f1ac49..a7d5f95 100644 --- a/src/BufferTop.c +++ b/src/BufferTop.c @@ -4,24 +4,25 @@ #include "cgds/BufferTop.h" -// NOTE: no _init() method here, since BufferTop has no specific initialization +// NOTE: no init() method here, since BufferTop has no specific initialization -BufferTop* _buffertop_new(size_t dataSize, UInt capacity, OrderType bType, UInt arity) +BufferTop* _buffertop_new( + size_t dataSize, UInt capacity, OrderType bType, UInt arity) { BufferTop* bufferTop = (BufferTop*) safe_malloc(sizeof (BufferTop)); bufferTop->capacity = capacity; bufferTop->bType = bType; //redondant, but facilitate understanding // WARNING: heap must have opposite type: "smallest" element first - bufferTop->heap = _heap_new(dataSize, (bType == MAX_T ? MIN_T : MAX_T), arity); + bufferTop->heap = + _heap_new(dataSize, (bType == MAX_T ? MIN_T : MAX_T), arity); return bufferTop; } BufferTop* buffertop_copy(BufferTop* bufferTop) { - BufferTop* bufferTopCopy = _buffertop_new( - bufferTop->heap->dataSize, bufferTop->capacity, - bufferTop->heap->hType, bufferTop->heap->arity); - heap_destroy(bufferTopCopy->heap); //TODO: bad style... + BufferTop* bufferTopCopy = (BufferTop*) safe_malloc(sizeof (BufferTop)); + bufferTopCopy->capacity = bufferTop->capacity; + bufferTopCopy->bType = bufferTop->bType; bufferTopCopy->heap = heap_copy(bufferTop->heap); return bufferTopCopy; } @@ -30,10 +31,10 @@ List* buffertop_2list(BufferTop* bufferTop) { // Copy the buffer, and then use the copy to build the list BufferTop* bufferTopCopy = buffertop_copy(bufferTop); - List* bufferInList = _list_new(bufferTop->heap->array->dataSize); + List* bufferInList = _list_new(bufferTop->heap->items->dataSize); while (!buffertop_empty(bufferTopCopy)) { - void* topItem = buffertop_first_raw(bufferTopCopy)->item; + void* topItem = _heap_top(bufferTopCopy->heap).item; // NOTE: list_insert_front(), to reverse (wrong) items order // ==> in the returned list, top element is at head. _list_insert_front(bufferInList, topItem); @@ -55,28 +56,28 @@ UInt buffertop_size(BufferTop* bufferTop) void _buffertop_tryadd(BufferTop* bufferTop, void* item, Real value) { - if (heap_size(bufferTop->heap) >= bufferTop->capacity && - ((bufferTop->bType == MIN_T && - value >= ((ItemValue*) (bufferTop->heap->array->datas[0]))->value) - || - (bufferTop->bType == MAX_T && - value <= ((ItemValue*) (bufferTop->heap->array->datas[0]))->value))) - { - // shortcut : if value "worse" than top->value and buffer is full, skip - return; + if (heap_size(bufferTop->heap) >= bufferTop->capacity) { + Real topValue = *((Real*) (bufferTop->heap->values->datas)); + if ( + (bufferTop->bType == MIN_T && value >= topValue) || + (bufferTop->bType == MAX_T && value <= topValue) + ) { + // Shortcut : if value "worse" than top->value and buffer is full, skip + return; + } } - // insertion somewhere in the item-values heap + // Insertion somewhere in the item-values heap _heap_insert(bufferTop->heap, item, value); if (heap_size(bufferTop->heap) > bufferTop->capacity) - // we must remove current root + // We must remove current root heap_pop(bufferTop->heap); } -ItemValue* buffertop_first_raw(BufferTop* bufferTop) +ItemValue _buffertop_first(BufferTop* bufferTop) { - return heap_top_raw(bufferTop->heap); + return _heap_top(bufferTop->heap); } void buffertop_pop(BufferTop* bufferTop)