diff --git a/llbc/include/llbc/core/timer/BinaryHeap.h b/llbc/include/llbc/core/timer/BinaryHeap.h index 502601f5..b3ceac66 100644 --- a/llbc/include/llbc/core/timer/BinaryHeap.h +++ b/llbc/include/llbc/core/timer/BinaryHeap.h @@ -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: /** diff --git a/llbc/include/llbc/core/timer/BinaryHeapInl.h b/llbc/include/llbc/core/timer/BinaryHeapInl.h index 8601a44a..47767d6f 100644 --- a/llbc/include/llbc/core/timer/BinaryHeapInl.h +++ b/llbc/include/llbc/core/timer/BinaryHeapInl.h @@ -50,22 +50,22 @@ LLBC_BinaryHeap::LLBC_BinaryHeap(const LLBC_BinaryHeap &other) } template -inline bool LLBC_BinaryHeap::IsEmpty() const +inline bool LLBC_BinaryHeap::empty() const { return _size == 0; } template -const T &LLBC_BinaryHeap::Top() const +const T &LLBC_BinaryHeap::top() const { ASSERT(LIKELY(_size != 0) && "BinaryHeap is empty"); return _elems[1]; } template -int LLBC_BinaryHeap::Top(T &elem) const +int LLBC_BinaryHeap::top(T &elem) const { - if (this->IsEmpty()) + if (this->empty()) { LLBC_SetLastError(LLBC_ERROR_NOT_FOUND); return LLBC_FAILED; @@ -76,7 +76,7 @@ int LLBC_BinaryHeap::Top(T &elem) const } template -void LLBC_BinaryHeap::Insert(const T &elem) +void LLBC_BinaryHeap::insert(const T &elem) { size_t size = _elems.size(); if (_size == size - 1) @@ -91,12 +91,11 @@ void LLBC_BinaryHeap::Insert(const T &elem) } template -int LLBC_BinaryHeap::DeleteTop() +void LLBC_BinaryHeap::pop() { - if (this->IsEmpty()) + if (this->empty()) { - LLBC_SetLastError(LLBC_ERROR_NOT_FOUND); - return LLBC_FAILED; + return; } if (_size != 1) @@ -105,17 +104,14 @@ int LLBC_BinaryHeap::DeleteTop() _elems[_size--] = T(); this->PercolateDown(1); - - return LLBC_OK; } template -int LLBC_BinaryHeap::DeleteTop(T &elem) +void LLBC_BinaryHeap::pop(T &elem) { - if (this->IsEmpty()) + if (this->empty()) { - LLBC_SetLastError(LLBC_ERROR_NOT_FOUND); - return LLBC_FAILED; + return; } elem = _elems[1]; @@ -126,14 +122,12 @@ int LLBC_BinaryHeap::DeleteTop(T &elem) _elems[_size--] = T(); this->PercolateDown(1); - - return LLBC_OK; } template -int LLBC_BinaryHeap::DeleteElem(size_t index) +int LLBC_BinaryHeap::erase(size_t index) { - if (this->IsEmpty()) + if (this->empty()) { LLBC_SetLastError(LLBC_ERROR_NOT_FOUND); return LLBC_FAILED; @@ -156,12 +150,12 @@ int LLBC_BinaryHeap::DeleteElem(size_t index) } template -int LLBC_BinaryHeap::DeleteElem(const T &elem) +int LLBC_BinaryHeap::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); @@ -169,9 +163,9 @@ int LLBC_BinaryHeap::DeleteElem(const T &elem) } template -int LLBC_BinaryHeap::DeleteElem(size_t index, T &elem) +int LLBC_BinaryHeap::erase(size_t index, T &elem) { - if (this->IsEmpty()) + if (this->empty()) { LLBC_SetLastError(LLBC_ERROR_NOT_FOUND); return LLBC_FAILED; @@ -195,29 +189,23 @@ int LLBC_BinaryHeap::DeleteElem(size_t index, T &elem) } template -void LLBC_BinaryHeap::Clear() +void LLBC_BinaryHeap::clear() { _size = 0; _elems.resize(1); } template -size_t LLBC_BinaryHeap::GetSize() const +size_t LLBC_BinaryHeap::size() const { return _size; } -template -const typename LLBC_BinaryHeap::Container &LLBC_BinaryHeap::GetData() const -{ - return _elems; -} - template typename LLBC_BinaryHeap::_This &LLBC_BinaryHeap< T, Comp>::operator=(const typename LLBC_BinaryHeap::_This &right) { - this->Clear(); + this->clear(); this->_size = right._size; this->_elems.assign(right._elems.begin(), right._elems.end()); @@ -228,13 +216,13 @@ typename LLBC_BinaryHeap::_This &LLBC_BinaryHeap< template inline LLBC_BinaryHeap::operator bool() const { - return !this->IsEmpty(); + return !this->empty(); } template inline bool LLBC_BinaryHeap::operator!() const { - return this->IsEmpty(); + return this->empty(); } template diff --git a/llbc/src/core/timer/TimerScheduler.cpp b/llbc/src/core/timer/TimerScheduler.cpp index c470e11d..5456917d 100644 --- a/llbc/src/core/timer/TimerScheduler.cpp +++ b/llbc/src/core/timer/TimerScheduler.cpp @@ -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(elems[i]); + LLBC_TimerData *data = elem; if (data->validate) { data->validate = false; @@ -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) @@ -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 { @@ -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 @@ -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; } @@ -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) @@ -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; diff --git a/testsuite/comm/TestCase_Comm_SvcBase.cpp b/testsuite/comm/TestCase_Comm_SvcBase.cpp index 4cd1e1b8..45050077 100644 --- a/testsuite/comm/TestCase_Comm_SvcBase.cpp +++ b/testsuite/comm/TestCase_Comm_SvcBase.cpp @@ -52,7 +52,7 @@ struct TestData : public LLBC_Coder return true; } - virtual void Reuse() + void Reuse() { iVal = 0; strVal.clear();