Skip to content

Commit

Permalink
Merge pull request #242 from reckfullol/dev/binary_heap
Browse files Browse the repository at this point in the history
标准化BinaryHeap接口
  • Loading branch information
lailongwei authored Mar 9, 2024
2 parents 33d8837 + 1629b89 commit 34f1a45
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 70 deletions.
34 changes: 14 additions & 20 deletions llbc/include/llbc/core/timer/BinaryHeap.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,76 +47,70 @@ class LLBC_BinaryHeap
* Check heap empty or not.
* @return bool - return true is empty, otherwise return false.
*/
bool IsEmpty() const;
bool empty() const;

/**
* Get top element, if is empty, wlll raise abort signal.
* @return const T & - the top element reference.
*/
const T &Top() const;
const T &top() const;

/**
* Get top element.
* @param[out] elem - top element.
* @return int - return 0 not empty, otherwise return -1.
*/
int Top(T &elem) const;
int top(T &elem) const;

/**
* Insert element.
*/
void Insert(const T &elem);
void insert(const T &elem);

/**
* Delete top element.
* @return int - return 0 if delete success, otherwise return -1.
* @return void
*/
int DeleteTop();
void pop();

/**
* Delete top element, and return deleted element copy.
* @param[out] elem - deleted element.
* @return int - return 0 if delete success, otherwise return -1.
* @return void
*/
int DeleteTop(T &elem);
void pop(T &elem);

/**
* Delete specific index's heap element.
* @param[in] index - heap index.
* @return int - return 0 if success, otherwise return -1.
*/
int DeleteElem(size_t index);
int erase(size_t index);

/**
* Delete specific elem's heap element.
* Delete specific element in heap.
* @param[in] elem - element.
* @return int - return 0 if success, otherwise return -1.
*/
int DeleteElem(const T &elem);
int erase(const T &elem);

/**
* Delete specific index's heap element, and return deleted element copy.
* @param[in] index - heap index.
* @return int - return 0 if success, otherwise return -1.
*/
int DeleteElem(size_t index, T &elem);
int erase(size_t index, T &elem);

/**
* Clear heap.
*/
void Clear();
void clear();

/**
* Get heap size.
* @return size_t - heap size.
*/
size_t GetSize() const;

/**
* Get heap data.
* @return const Container & - heap data.
*/
const Container &GetData() const;
size_t size() const;

public:
/**
Expand Down
56 changes: 22 additions & 34 deletions llbc/include/llbc/core/timer/BinaryHeapInl.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,22 @@ LLBC_BinaryHeap<T, Comp>::LLBC_BinaryHeap(const LLBC_BinaryHeap<T, Comp> &other)
}

template <typename T, typename Comp>
inline bool LLBC_BinaryHeap<T, Comp>::IsEmpty() const
inline bool LLBC_BinaryHeap<T, Comp>::empty() const
{
return _size == 0;
}

template <typename T, typename Comp>
const T &LLBC_BinaryHeap<T, Comp>::Top() const
const T &LLBC_BinaryHeap<T, Comp>::top() const
{
ASSERT(LIKELY(_size != 0) && "BinaryHeap is empty");
return _elems[1];
}

template <typename T, typename Comp>
int LLBC_BinaryHeap<T, Comp>::Top(T &elem) const
int LLBC_BinaryHeap<T, Comp>::top(T &elem) const
{
if (this->IsEmpty())
if (this->empty())
{
LLBC_SetLastError(LLBC_ERROR_NOT_FOUND);
return LLBC_FAILED;
Expand All @@ -76,7 +76,7 @@ int LLBC_BinaryHeap<T, Comp>::Top(T &elem) const
}

template <typename T, typename Comp>
void LLBC_BinaryHeap<T, Comp>::Insert(const T &elem)
void LLBC_BinaryHeap<T, Comp>::insert(const T &elem)
{
size_t size = _elems.size();
if (_size == size - 1)
Expand All @@ -91,12 +91,11 @@ void LLBC_BinaryHeap<T, Comp>::Insert(const T &elem)
}

template <typename T, typename Comp>
int LLBC_BinaryHeap<T, Comp>::DeleteTop()
void LLBC_BinaryHeap<T, Comp>::pop()
{
if (this->IsEmpty())
if (this->empty())
{
LLBC_SetLastError(LLBC_ERROR_NOT_FOUND);
return LLBC_FAILED;
return;
}

if (_size != 1)
Expand All @@ -105,17 +104,14 @@ int LLBC_BinaryHeap<T, Comp>::DeleteTop()
_elems[_size--] = T();

this->PercolateDown(1);

return LLBC_OK;
}

template <typename T, typename Comp>
int LLBC_BinaryHeap<T, Comp>::DeleteTop(T &elem)
void LLBC_BinaryHeap<T, Comp>::pop(T &elem)
{
if (this->IsEmpty())
if (this->empty())
{
LLBC_SetLastError(LLBC_ERROR_NOT_FOUND);
return LLBC_FAILED;
return;
}

elem = _elems[1];
Expand All @@ -126,14 +122,12 @@ int LLBC_BinaryHeap<T, Comp>::DeleteTop(T &elem)
_elems[_size--] = T();

this->PercolateDown(1);

return LLBC_OK;
}

template <typename T, typename Comp>
int LLBC_BinaryHeap<T, Comp>::DeleteElem(size_t index)
int LLBC_BinaryHeap<T, Comp>::erase(size_t index)
{
if (this->IsEmpty())
if (this->empty())
{
LLBC_SetLastError(LLBC_ERROR_NOT_FOUND);
return LLBC_FAILED;
Expand All @@ -156,22 +150,22 @@ int LLBC_BinaryHeap<T, Comp>::DeleteElem(size_t index)
}

template<typename T, typename Comp>
int LLBC_BinaryHeap<T, Comp>::DeleteElem(const T &elem)
int LLBC_BinaryHeap<T, Comp>::erase(const T &elem)
{
for (size_t i = 1; i <= _size; ++i)
{
if (_elems[i] == elem)
return this->DeleteElem(i);
return this->erase(i);
}

LLBC_SetLastError(LLBC_ERROR_NOT_FOUND);
return LLBC_FAILED;
}

template <typename T, typename Comp>
int LLBC_BinaryHeap<T, Comp>::DeleteElem(size_t index, T &elem)
int LLBC_BinaryHeap<T, Comp>::erase(size_t index, T &elem)
{
if (this->IsEmpty())
if (this->empty())
{
LLBC_SetLastError(LLBC_ERROR_NOT_FOUND);
return LLBC_FAILED;
Expand All @@ -195,29 +189,23 @@ int LLBC_BinaryHeap<T, Comp>::DeleteElem(size_t index, T &elem)
}

template <typename T, typename Comp>
void LLBC_BinaryHeap<T, Comp>::Clear()
void LLBC_BinaryHeap<T, Comp>::clear()
{
_size = 0;
_elems.resize(1);
}

template <typename T, typename Comp>
size_t LLBC_BinaryHeap<T, Comp>::GetSize() const
size_t LLBC_BinaryHeap<T, Comp>::size() const
{
return _size;
}

template <typename T, typename Comp>
const typename LLBC_BinaryHeap<T, Comp>::Container &LLBC_BinaryHeap<T, Comp>::GetData() const
{
return _elems;
}

template <typename T, typename Comp>
typename LLBC_BinaryHeap<T, Comp>::_This &LLBC_BinaryHeap<
T, Comp>::operator=(const typename LLBC_BinaryHeap<T, Comp>::_This &right)
{
this->Clear();
this->clear();

this->_size = right._size;
this->_elems.assign(right._elems.begin(), right._elems.end());
Expand All @@ -228,13 +216,13 @@ typename LLBC_BinaryHeap<T, Comp>::_This &LLBC_BinaryHeap<
template <typename T, typename Comp>
inline LLBC_BinaryHeap<T, Comp>::operator bool() const
{
return !this->IsEmpty();
return !this->empty();
}

template <typename T, typename Comp>
inline bool LLBC_BinaryHeap<T, Comp>::operator!() const
{
return this->IsEmpty();
return this->empty();
}

template <typename T, typename Comp>
Expand Down
28 changes: 13 additions & 15 deletions llbc/src/core/timer/TimerScheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ LLBC_TimerScheduler::~LLBC_TimerScheduler()
{
_destroyed = true;

const size_t size = _heap.GetSize();
const _Heap::Container &elems = _heap.GetData();
for (size_t i = 1; i <= size; ++i)
_Heap::Container elems(_heap);
for(auto &elem : elems)
{
LLBC_TimerData *data = const_cast<LLBC_TimerData *>(elems[i]);
LLBC_TimerData *data = elem;
if (data->validate)
{
data->validate = false;
Expand All @@ -70,17 +69,17 @@ void LLBC_TimerScheduler::Update()
{
if (UNLIKELY(!_enabled))
return;
else if (_heap.IsEmpty())
else if (_heap.empty())
return;

LLBC_TimerData *data;
sint64 now = LLBC_GetMilliseconds();
while (_heap.Top(data) == LLBC_OK)
while (_heap.top(data) == LLBC_OK)
{
if (now < data->handle)
break;

_heap.DeleteTop();
_heap.pop();
if (!data->validate)
{
if (--data->refCount == 0)
Expand Down Expand Up @@ -127,7 +126,7 @@ void LLBC_TimerScheduler::Update()
sint64 delay = (data->period != 0) ? (now - data->handle) % data->period : 0;
data->handle = now + data->period - delay;

_heap.Insert(data);
_heap.insert(data);
}
else
{
Expand All @@ -149,7 +148,7 @@ void LLBC_TimerScheduler::SetEnabled(bool enabled)

size_t LLBC_TimerScheduler::GetTimerCount() const
{
return _heap.GetSize();
return _heap.size();
}

bool LLBC_TimerScheduler::IsDestroyed() const
Expand Down Expand Up @@ -182,7 +181,7 @@ int LLBC_TimerScheduler::Schedule(LLBC_Timer *timer, sint64 dueTime, sint64 peri
}

timer->_timerData = data;
_heap.Insert(data);
_heap.insert(data);

return LLBC_OK;
}
Expand All @@ -206,7 +205,7 @@ int LLBC_TimerScheduler::Cancel(LLBC_Timer *timer)

if (data->handle - LLBC_GetMilliseconds() >= LLBC_CFG_CORE_TIMER_LONG_TIMEOUT_TIME)
{
int delElemRet = _heap.DeleteElem(data);
int delElemRet = _heap.erase(data);
ASSERT(delElemRet == LLBC_OK &&
"Timer scheduler internal error, Could not found timer data when Cancel long timeout timer!");
if (--data->refCount == 0)
Expand All @@ -224,11 +223,10 @@ void LLBC_TimerScheduler::CancelAll()
if (UNLIKELY(_destroyed))
return;

const size_t size = _heap.GetSize();
_Heap::Container copyElems(_heap.GetData());
for (size_t i = 1; i <= size; ++i)
_Heap::Container copyElems(_heap);
for(auto &elem : copyElems)
{
LLBC_TimerData *data = copyElems[i];
LLBC_TimerData *data = elem;
if (UNLIKELY(!data->validate))
return;

Expand Down
2 changes: 1 addition & 1 deletion testsuite/comm/TestCase_Comm_SvcBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ struct TestData : public LLBC_Coder
return true;
}

virtual void Reuse()
void Reuse()
{
iVal = 0;
strVal.clear();
Expand Down

0 comments on commit 34f1a45

Please sign in to comment.