From a2fadd2da50b8b5c3a19609209e24295d2f384d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Mon, 16 Jan 2017 23:28:47 -0800 Subject: [PATCH] Cleanup. --- include/bx/handlealloc.h | 713 ++++++++----------------------------- include/bx/handlealloc.inl | 712 ++++++++++++++++++++++++++++++++++++ 2 files changed, 861 insertions(+), 564 deletions(-) create mode 100644 include/bx/handlealloc.inl diff --git a/include/bx/handlealloc.h b/include/bx/handlealloc.h index 04303f4..cf8efc5 100644 --- a/include/bx/handlealloc.h +++ b/include/bx/handlealloc.h @@ -18,130 +18,65 @@ namespace bx public: static const uint16_t invalid = UINT16_MAX; - HandleAlloc(uint16_t _maxHandles) - : m_numHandles(0) - , m_maxHandles(_maxHandles) - { - reset(); - } + /// + HandleAlloc(uint16_t _maxHandles); - ~HandleAlloc() - { - } + /// + ~HandleAlloc(); - const uint16_t* getHandles() const - { - return getDensePtr(); - } + /// + const uint16_t* getHandles() const; - uint16_t getHandleAt(uint16_t _at) const - { - return getDensePtr()[_at]; - } + /// + uint16_t getHandleAt(uint16_t _at) const; - uint16_t getNumHandles() const - { - return m_numHandles; - } + /// + uint16_t getNumHandles() const; - uint16_t getMaxHandles() const - { - return m_maxHandles; - } + /// + uint16_t getMaxHandles() const; - uint16_t alloc() - { - if (m_numHandles < m_maxHandles) - { - uint16_t index = m_numHandles; - ++m_numHandles; + /// + uint16_t alloc(); - uint16_t* dense = getDensePtr(); - uint16_t handle = dense[index]; - uint16_t* sparse = getSparsePtr(); - sparse[handle] = index; - return handle; - } + /// + bool isValid(uint16_t _handle) const; - return invalid; - } + /// + void free(uint16_t _handle); - bool isValid(uint16_t _handle) const - { - uint16_t* dense = getDensePtr(); - uint16_t* sparse = getSparsePtr(); - uint16_t index = sparse[_handle]; - - return index < m_numHandles - && dense[index] == _handle - ; - } - - void free(uint16_t _handle) - { - uint16_t* dense = getDensePtr(); - uint16_t* sparse = getSparsePtr(); - uint16_t index = sparse[_handle]; - --m_numHandles; - uint16_t temp = dense[m_numHandles]; - dense[m_numHandles] = _handle; - sparse[temp] = index; - dense[index] = temp; - } - - void reset() - { - m_numHandles = 0; - uint16_t* dense = getDensePtr(); - for (uint16_t ii = 0, num = m_maxHandles; ii < num; ++ii) - { - dense[ii] = ii; - } - } + /// + void reset(); private: HandleAlloc(); - uint16_t* getDensePtr() const - { - uint8_t* ptr = (uint8_t*)reinterpret_cast(this); - return (uint16_t*)&ptr[sizeof(HandleAlloc)]; - } + /// + uint16_t* getDensePtr() const; - uint16_t* getSparsePtr() const - { - return &getDensePtr()[m_maxHandles]; - } + /// + uint16_t* getSparsePtr() const; uint16_t m_numHandles; uint16_t m_maxHandles; }; - inline HandleAlloc* createHandleAlloc(AllocatorI* _allocator, uint16_t _maxHandles) - { - uint8_t* ptr = (uint8_t*)BX_ALLOC(_allocator, sizeof(HandleAlloc) + 2*_maxHandles*sizeof(uint16_t) ); - return ::new (ptr) HandleAlloc(_maxHandles); - } + /// + HandleAlloc* createHandleAlloc(AllocatorI* _allocator, uint16_t _maxHandles); - inline void destroyHandleAlloc(AllocatorI* _allocator, HandleAlloc* _handleAlloc) - { - _handleAlloc->~HandleAlloc(); - BX_FREE(_allocator, _handleAlloc); - } + /// + void destroyHandleAlloc(AllocatorI* _allocator, HandleAlloc* _handleAlloc); /// template class HandleAllocT : public HandleAlloc { public: - HandleAllocT() - : HandleAlloc(MaxHandlesT) - { - } + /// + HandleAllocT(); - ~HandleAllocT() - { - } + /// + ~HandleAllocT(); private: uint16_t m_padding[2*MaxHandlesT]; @@ -154,170 +89,51 @@ namespace bx public: static const uint16_t invalid = UINT16_MAX; - HandleListT() - { - reset(); - } + /// + HandleListT(); - void pushBack(uint16_t _handle) - { - insertAfter(m_back, _handle); - } + /// + void pushBack(uint16_t _handle); - uint16_t popBack() - { - uint16_t last = invalid != m_back - ? m_back - : m_front - ; + /// + uint16_t popBack(); - if (invalid != last) - { - remove(last); - } + /// + void pushFront(uint16_t _handle); - return last; - } + /// + uint16_t popFront(); - void pushFront(uint16_t _handle) - { - insertBefore(m_front, _handle); - } + /// + uint16_t getFront() const; - uint16_t popFront() - { - uint16_t front = m_front; + /// + uint16_t getBack() const; - if (invalid != front) - { - remove(front); - } + /// + uint16_t getNext(uint16_t _handle) const; - return front; - } + /// + uint16_t getPrev(uint16_t _handle) const; - uint16_t getFront() const - { - return m_front; - } + /// + void remove(uint16_t _handle); - uint16_t getBack() const - { - return m_back; - } - - uint16_t getNext(uint16_t _handle) const - { - BX_CHECK(isValid(_handle), "Invalid handle %d!", _handle); - const Link& curr = m_links[_handle]; - return curr.m_next; - } - - uint16_t getPrev(uint16_t _handle) const - { - BX_CHECK(isValid(_handle), "Invalid handle %d!", _handle); - const Link& curr = m_links[_handle]; - return curr.m_prev; - } - - void remove(uint16_t _handle) - { - BX_CHECK(isValid(_handle), "Invalid handle %d!", _handle); - Link& curr = m_links[_handle]; - - if (invalid != curr.m_prev) - { - Link& prev = m_links[curr.m_prev]; - prev.m_next = curr.m_next; - } - else - { - m_front = curr.m_next; - } - - if (invalid != curr.m_next) - { - Link& next = m_links[curr.m_next]; - next.m_prev = curr.m_prev; - } - else - { - m_back = curr.m_prev; - } - - curr.m_prev = invalid; - curr.m_next = invalid; - } - - void reset() - { - memset(m_links, 0xff, sizeof(m_links) ); - m_front = invalid; - m_back = invalid; - } + /// + void reset(); private: - void insertBefore(uint16_t _before, uint16_t _handle) - { - Link& curr = m_links[_handle]; - curr.m_next = _before; + /// + void insertBefore(uint16_t _before, uint16_t _handle); - if (invalid != _before) - { - Link& link = m_links[_before]; - if (invalid != link.m_prev) - { - Link& prev = m_links[link.m_prev]; - prev.m_next = _handle; - } + /// + void insertAfter(uint16_t _after, uint16_t _handle); - curr.m_prev = link.m_prev; - link.m_prev = _handle; - } + /// + bool isValid(uint16_t _handle) const; - updateFrontBack(_handle); - } - - void insertAfter(uint16_t _after, uint16_t _handle) - { - Link& curr = m_links[_handle]; - curr.m_prev = _after; - - if (invalid != _after) - { - Link& link = m_links[_after]; - if (invalid != link.m_next) - { - Link& next = m_links[link.m_next]; - next.m_prev = _handle; - } - - curr.m_next = link.m_next; - link.m_next = _handle; - } - - updateFrontBack(_handle); - } - - bool isValid(uint16_t _handle) const - { - return _handle < MaxHandlesT; - } - - void updateFrontBack(uint16_t _handle) - { - Link& curr = m_links[_handle]; - - if (invalid == curr.m_prev) - { - m_front = _handle; - } - - if (invalid == curr.m_next) - { - m_back = _handle; - } - } + /// + void updateFrontBack(uint16_t _handle); uint16_t m_front; uint16_t m_back; @@ -338,89 +154,50 @@ namespace bx public: static const uint16_t invalid = UINT16_MAX; - HandleAllocLruT() - { - reset(); - } + /// + HandleAllocLruT(); - ~HandleAllocLruT() - { - } + /// + ~HandleAllocLruT(); - const uint16_t* getHandles() const - { - return m_alloc.getHandles(); - } + /// + const uint16_t* getHandles() const; - uint16_t getHandleAt(uint16_t _at) const - { - return m_alloc.getHandleAt(_at); - } + /// + uint16_t getHandleAt(uint16_t _at) const; - uint16_t getNumHandles() const - { - return m_alloc.getNumHandles(); - } + /// + uint16_t getNumHandles() const; - uint16_t getMaxHandles() const - { - return m_alloc.getMaxHandles(); - } + /// + uint16_t getMaxHandles() const; - uint16_t alloc() - { - uint16_t handle = m_alloc.alloc(); - if (invalid != handle) - { - m_list.pushFront(handle); - } - return handle; - } + /// + uint16_t alloc(); - bool isValid(uint16_t _handle) const - { - return m_alloc.isValid(_handle); - } + /// + bool isValid(uint16_t _handle) const; - void free(uint16_t _handle) - { - BX_CHECK(isValid(_handle), "Invalid handle %d!", _handle); - m_list.remove(_handle); - m_alloc.free(_handle); - } + /// + void free(uint16_t _handle); - void touch(uint16_t _handle) - { - BX_CHECK(isValid(_handle), "Invalid handle %d!", _handle); - m_list.remove(_handle); - m_list.pushFront(_handle); - } + /// + void touch(uint16_t _handle); - uint16_t getFront() const - { - return m_list.getFront(); - } + /// + uint16_t getFront() const; - uint16_t getBack() const - { - return m_list.getBack(); - } + /// + uint16_t getBack() const; - uint16_t getNext(uint16_t _handle) const - { - return m_list.getNext(_handle); - } + /// + uint16_t getNext(uint16_t _handle) const; - uint16_t getPrev(uint16_t _handle) const - { - return m_list.getPrev(_handle); - } + /// + uint16_t getPrev(uint16_t _handle) const; - void reset() - { - m_list.reset(); - m_alloc.reset(); - } + /// + void reset(); private: HandleListT m_list; @@ -434,103 +211,34 @@ namespace bx public: static const uint16_t invalid = UINT16_MAX; - HandleHashMapT() - : m_maxCapacity(MaxCapacityT) - { - reset(); - } + /// + HandleHashMapT(); - ~HandleHashMapT() - { - } + /// + ~HandleHashMapT(); - bool insert(KeyT _key, uint16_t _handle) - { - if (invalid == _handle) - { - return false; - } + /// + bool insert(KeyT _key, uint16_t _handle); - const KeyT hash = mix(_key); - const uint32_t firstIdx = hash % MaxCapacityT; - uint32_t idx = firstIdx; - do - { - if (m_handle[idx] == invalid) - { - m_key[idx] = _key; - m_handle[idx] = _handle; - ++m_numElements; - return true; - } + /// + bool removeByKey(KeyT _key); - if (m_key[idx] == _key) - { - return false; - } + /// + bool removeByHandle(uint16_t _handle); - idx = (idx + 1) % MaxCapacityT; + /// + uint16_t find(KeyT _key) const; - } while (idx != firstIdx); + /// + void reset(); - return false; - } + /// + uint32_t getNumElements() const; - bool removeByKey(KeyT _key) - { - uint32_t idx = findIndex(_key); - if (UINT32_MAX != idx) - { - removeIndex(idx); - return true; - } - - return false; - } - - bool removeByHandle(uint16_t _handle) - { - if (invalid != _handle) - { - for (uint32_t idx = 0; idx < MaxCapacityT; ++idx) - { - if (m_handle[idx] == _handle) - { - removeIndex(idx); - } - } - } - - return false; - } - - uint16_t find(KeyT _key) const - { - uint32_t idx = findIndex(_key); - if (UINT32_MAX != idx) - { - return m_handle[idx]; - } - - return invalid; - } - - void reset() - { - memset(m_handle, 0xff, sizeof(m_handle) ); - m_numElements = 0; - } - - uint32_t getNumElements() const - { - return m_numElements; - } - - uint32_t getMaxCapacity() const - { - return m_maxCapacity; - } + /// + uint32_t getMaxCapacity() const; + /// struct Iterator { uint16_t handle; @@ -541,104 +249,24 @@ namespace bx uint32_t num; }; - Iterator first() const - { - Iterator it; - it.handle = invalid; - it.pos = 0; - it.num = m_numElements; + /// + Iterator first() const; - if (0 == it.num) - { - return it; - } - - ++it.num; - next(it); - return it; - } - - bool next(Iterator& _it) const - { - if (0 == _it.num) - { - return false; - } - - for ( - ;_it.pos < MaxCapacityT && invalid == m_handle[_it.pos] - ; ++_it.pos - ); - _it.handle = m_handle[_it.pos]; - ++_it.pos; - --_it.num; - return true; - } + /// + bool next(Iterator& _it) const; private: - uint32_t findIndex(KeyT _key) const - { - const KeyT hash = mix(_key); + /// + uint32_t findIndex(KeyT _key) const; - const uint32_t firstIdx = hash % MaxCapacityT; - uint32_t idx = firstIdx; - do - { - if (m_handle[idx] == invalid) - { - return UINT32_MAX; - } + /// + void removeIndex(uint32_t _idx); - if (m_key[idx] == _key) - { - return idx; - } + /// + uint32_t mix(uint32_t _x) const; - idx = (idx + 1) % MaxCapacityT; - - } while (idx != firstIdx); - - return UINT32_MAX; - } - - void removeIndex(uint32_t _idx) - { - m_handle[_idx] = invalid; - --m_numElements; - - for (uint32_t idx = (_idx + 1) % MaxCapacityT - ; m_handle[idx] != invalid - ; idx = (idx + 1) % MaxCapacityT) - { - if (m_handle[idx] != invalid) - { - const KeyT key = m_key[idx]; - if (idx != findIndex(key) ) - { - const uint16_t handle = m_handle[idx]; - m_handle[idx] = invalid; - --m_numElements; - insert(key, handle); - } - } - } - } - - uint32_t mix(uint32_t _x) const - { - const uint32_t tmp0 = uint32_mul(_x, UINT32_C(2246822519) ); - const uint32_t tmp1 = uint32_rol(tmp0, 13); - const uint32_t result = uint32_mul(tmp1, UINT32_C(2654435761) ); - return result; - } - - uint64_t mix(uint64_t _x) const - { - const uint64_t tmp0 = uint64_mul(_x, UINT64_C(14029467366897019727) ); - const uint64_t tmp1 = uint64_rol(tmp0, 31); - const uint64_t result = uint64_mul(tmp1, UINT64_C(11400714785074694791) ); - return result; - } + /// + uint64_t mix(uint64_t _x) const; uint32_t m_maxCapacity; uint32_t m_numElements; @@ -654,86 +282,41 @@ namespace bx public: static const uint16_t invalid = UINT16_MAX; - HandleHashMapAllocT() - { - reset(); - } + /// + HandleHashMapAllocT(); - ~HandleHashMapAllocT() - { - } + /// + ~HandleHashMapAllocT(); - uint16_t alloc(KeyT _key) - { - uint16_t handle = m_alloc.alloc(); - if (invalid == handle) - { - return invalid; - } + /// + uint16_t alloc(KeyT _key); - bool ok = m_table.insert(_key, handle); - if (!ok) - { - m_alloc.free(handle); - return invalid; - } + /// + void free(KeyT _key); - return handle; - } + /// + void free(uint16_t _handle); - void free(KeyT _key) - { - uint16_t handle = m_table.find(_key); - if (invalid == handle) - { - return; - } + /// + uint16_t find(KeyT _key) const; - m_table.removeByKey(_key); - m_alloc.free(handle); - } + /// + const uint16_t* getHandles() const; - void free(uint16_t _handle) - { - m_table.removeByHandle(_handle); - m_alloc.free(_handle); - } + /// + uint16_t getHandleAt(uint16_t _at) const; - uint16_t find(KeyT _key) const - { - return m_table.find(_key); - } + /// + uint16_t getNumHandles() const; - const uint16_t* getHandles() const - { - return m_alloc.getHandles(); - } + /// + uint16_t getMaxHandles() const; - uint16_t getHandleAt(uint16_t _at) const - { - return m_alloc.getHandleAt(_at); - } + /// + bool isValid(uint16_t _handle) const; - uint16_t getNumHandles() const - { - return m_alloc.getNumHandles(); - } - - uint16_t getMaxHandles() const - { - return m_alloc.getMaxHandles(); - } - - bool isValid(uint16_t _handle) const - { - return m_alloc.isValid(_handle); - } - - void reset() - { - m_table.reset(); - m_alloc.reset(); - } + /// + void reset(); private: HandleHashMapT m_table; @@ -742,4 +325,6 @@ namespace bx } // namespace bx +#include "handlealloc.inl" + #endif // BX_HANDLE_ALLOC_H_HEADER_GUARD diff --git a/include/bx/handlealloc.inl b/include/bx/handlealloc.inl new file mode 100644 index 0000000..04369b5 --- /dev/null +++ b/include/bx/handlealloc.inl @@ -0,0 +1,712 @@ +/* + * Copyright 2010-2017 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bx#license-bsd-2-clause + */ + +#ifndef BX_HANDLE_ALLOC_H_HEADER_GUARD +# error "Must be included from bx/handlealloc.h!" +#endif // BX_HANDLE_ALLOC_H_HEADER_GUARD + +#include "bx.h" +#include "allocator.h" +#include "uint32_t.h" + +namespace bx +{ + inline HandleAlloc::HandleAlloc(uint16_t _maxHandles) + : m_numHandles(0) + , m_maxHandles(_maxHandles) + { + reset(); + } + + inline HandleAlloc::~HandleAlloc() + { + } + + inline const uint16_t* HandleAlloc::getHandles() const + { + return getDensePtr(); + } + + inline uint16_t HandleAlloc::getHandleAt(uint16_t _at) const + { + return getDensePtr()[_at]; + } + + inline uint16_t HandleAlloc::getNumHandles() const + { + return m_numHandles; + } + + inline uint16_t HandleAlloc::getMaxHandles() const + { + return m_maxHandles; + } + + inline uint16_t HandleAlloc::alloc() + { + if (m_numHandles < m_maxHandles) + { + uint16_t index = m_numHandles; + ++m_numHandles; + + uint16_t* dense = getDensePtr(); + uint16_t handle = dense[index]; + uint16_t* sparse = getSparsePtr(); + sparse[handle] = index; + return handle; + } + + return invalid; + } + + inline bool HandleAlloc::isValid(uint16_t _handle) const + { + uint16_t* dense = getDensePtr(); + uint16_t* sparse = getSparsePtr(); + uint16_t index = sparse[_handle]; + + return index < m_numHandles + && dense[index] == _handle + ; + } + + inline void HandleAlloc::free(uint16_t _handle) + { + uint16_t* dense = getDensePtr(); + uint16_t* sparse = getSparsePtr(); + uint16_t index = sparse[_handle]; + --m_numHandles; + uint16_t temp = dense[m_numHandles]; + dense[m_numHandles] = _handle; + sparse[temp] = index; + dense[index] = temp; + } + + inline void HandleAlloc::reset() + { + m_numHandles = 0; + uint16_t* dense = getDensePtr(); + for (uint16_t ii = 0, num = m_maxHandles; ii < num; ++ii) + { + dense[ii] = ii; + } + } + + inline uint16_t* HandleAlloc::getDensePtr() const + { + uint8_t* ptr = (uint8_t*)reinterpret_cast(this); + return (uint16_t*)&ptr[sizeof(HandleAlloc)]; + } + + inline uint16_t* HandleAlloc::getSparsePtr() const + { + return &getDensePtr()[m_maxHandles]; + } + + inline HandleAlloc* createHandleAlloc(AllocatorI* _allocator, uint16_t _maxHandles) + { + uint8_t* ptr = (uint8_t*)BX_ALLOC(_allocator, sizeof(HandleAlloc) + 2*_maxHandles*sizeof(uint16_t) ); + return ::new (ptr) HandleAlloc(_maxHandles); + } + + inline void destroyHandleAlloc(AllocatorI* _allocator, HandleAlloc* _handleAlloc) + { + _handleAlloc->~HandleAlloc(); + BX_FREE(_allocator, _handleAlloc); + } + + template + inline HandleAllocT::HandleAllocT() + : HandleAlloc(MaxHandlesT) + { + } + + template + inline HandleAllocT::~HandleAllocT() + { + } + + template + inline HandleListT::HandleListT() + { + reset(); + } + + template + inline void HandleListT::pushBack(uint16_t _handle) + { + insertAfter(m_back, _handle); + } + + template + inline uint16_t HandleListT::popBack() + { + uint16_t last = invalid != m_back + ? m_back + : m_front + ; + + if (invalid != last) + { + remove(last); + } + + return last; + } + + template + inline void HandleListT::pushFront(uint16_t _handle) + { + insertBefore(m_front, _handle); + } + + template + inline uint16_t HandleListT::popFront() + { + uint16_t front = m_front; + + if (invalid != front) + { + remove(front); + } + + return front; + } + + template + inline uint16_t HandleListT::getFront() const + { + return m_front; + } + + template + inline uint16_t HandleListT::getBack() const + { + return m_back; + } + + template + inline uint16_t HandleListT::getNext(uint16_t _handle) const + { + BX_CHECK(isValid(_handle), "Invalid handle %d!", _handle); + const Link& curr = m_links[_handle]; + return curr.m_next; + } + + template + inline uint16_t HandleListT::getPrev(uint16_t _handle) const + { + BX_CHECK(isValid(_handle), "Invalid handle %d!", _handle); + const Link& curr = m_links[_handle]; + return curr.m_prev; + } + + template + inline void HandleListT::remove(uint16_t _handle) + { + BX_CHECK(isValid(_handle), "Invalid handle %d!", _handle); + Link& curr = m_links[_handle]; + + if (invalid != curr.m_prev) + { + Link& prev = m_links[curr.m_prev]; + prev.m_next = curr.m_next; + } + else + { + m_front = curr.m_next; + } + + if (invalid != curr.m_next) + { + Link& next = m_links[curr.m_next]; + next.m_prev = curr.m_prev; + } + else + { + m_back = curr.m_prev; + } + + curr.m_prev = invalid; + curr.m_next = invalid; + } + + template + inline void HandleListT::reset() + { + memset(m_links, 0xff, sizeof(m_links) ); + m_front = invalid; + m_back = invalid; + } + + template + inline void HandleListT::insertBefore(uint16_t _before, uint16_t _handle) + { + Link& curr = m_links[_handle]; + curr.m_next = _before; + + if (invalid != _before) + { + Link& link = m_links[_before]; + if (invalid != link.m_prev) + { + Link& prev = m_links[link.m_prev]; + prev.m_next = _handle; + } + + curr.m_prev = link.m_prev; + link.m_prev = _handle; + } + + updateFrontBack(_handle); + } + + template + inline void HandleListT::insertAfter(uint16_t _after, uint16_t _handle) + { + Link& curr = m_links[_handle]; + curr.m_prev = _after; + + if (invalid != _after) + { + Link& link = m_links[_after]; + if (invalid != link.m_next) + { + Link& next = m_links[link.m_next]; + next.m_prev = _handle; + } + + curr.m_next = link.m_next; + link.m_next = _handle; + } + + updateFrontBack(_handle); + } + + template + inline bool HandleListT::isValid(uint16_t _handle) const + { + return _handle < MaxHandlesT; + } + + template + inline void HandleListT::updateFrontBack(uint16_t _handle) + { + Link& curr = m_links[_handle]; + + if (invalid == curr.m_prev) + { + m_front = _handle; + } + + if (invalid == curr.m_next) + { + m_back = _handle; + } + } + + template + inline HandleAllocLruT::HandleAllocLruT() + { + reset(); + } + + template + inline HandleAllocLruT::~HandleAllocLruT() + { + } + + template + inline const uint16_t* HandleAllocLruT::getHandles() const + { + return m_alloc.getHandles(); + } + + template + inline uint16_t HandleAllocLruT::getHandleAt(uint16_t _at) const + { + return m_alloc.getHandleAt(_at); + } + + template + inline uint16_t HandleAllocLruT::getNumHandles() const + { + return m_alloc.getNumHandles(); + } + + template + inline uint16_t HandleAllocLruT::getMaxHandles() const + { + return m_alloc.getMaxHandles(); + } + + template + inline uint16_t HandleAllocLruT::alloc() + { + uint16_t handle = m_alloc.alloc(); + if (invalid != handle) + { + m_list.pushFront(handle); + } + return handle; + } + + template + inline bool HandleAllocLruT::isValid(uint16_t _handle) const + { + return m_alloc.isValid(_handle); + } + + template + inline void HandleAllocLruT::free(uint16_t _handle) + { + BX_CHECK(isValid(_handle), "Invalid handle %d!", _handle); + m_list.remove(_handle); + m_alloc.free(_handle); + } + + template + inline void HandleAllocLruT::touch(uint16_t _handle) + { + BX_CHECK(isValid(_handle), "Invalid handle %d!", _handle); + m_list.remove(_handle); + m_list.pushFront(_handle); + } + + template + inline uint16_t HandleAllocLruT::getFront() const + { + return m_list.getFront(); + } + + template + inline uint16_t HandleAllocLruT::getBack() const + { + return m_list.getBack(); + } + + template + inline uint16_t HandleAllocLruT::getNext(uint16_t _handle) const + { + return m_list.getNext(_handle); + } + + template + inline uint16_t HandleAllocLruT::getPrev(uint16_t _handle) const + { + return m_list.getPrev(_handle); + } + + template + inline void HandleAllocLruT::reset() + { + m_list.reset(); + m_alloc.reset(); + } + + template + inline HandleHashMapT::HandleHashMapT() + : m_maxCapacity(MaxCapacityT) + { + reset(); + } + + template + inline HandleHashMapT::~HandleHashMapT() + { + } + + template + inline bool HandleHashMapT::insert(KeyT _key, uint16_t _handle) + { + if (invalid == _handle) + { + return false; + } + + const KeyT hash = mix(_key); + const uint32_t firstIdx = hash % MaxCapacityT; + uint32_t idx = firstIdx; + do + { + if (m_handle[idx] == invalid) + { + m_key[idx] = _key; + m_handle[idx] = _handle; + ++m_numElements; + return true; + } + + if (m_key[idx] == _key) + { + return false; + } + + idx = (idx + 1) % MaxCapacityT; + + } while (idx != firstIdx); + + return false; + } + + template + inline bool HandleHashMapT::removeByKey(KeyT _key) + { + uint32_t idx = findIndex(_key); + if (UINT32_MAX != idx) + { + removeIndex(idx); + return true; + } + + return false; + } + + template + inline bool HandleHashMapT::removeByHandle(uint16_t _handle) + { + if (invalid != _handle) + { + for (uint32_t idx = 0; idx < MaxCapacityT; ++idx) + { + if (m_handle[idx] == _handle) + { + removeIndex(idx); + } + } + } + + return false; + } + + template + inline uint16_t HandleHashMapT::find(KeyT _key) const + { + uint32_t idx = findIndex(_key); + if (UINT32_MAX != idx) + { + return m_handle[idx]; + } + + return invalid; + } + + template + inline void HandleHashMapT::reset() + { + memset(m_handle, 0xff, sizeof(m_handle) ); + m_numElements = 0; + } + + template + inline uint32_t HandleHashMapT::getNumElements() const + { + return m_numElements; + } + + template + inline uint32_t HandleHashMapT::getMaxCapacity() const + { + return m_maxCapacity; + } + + template + inline typename HandleHashMapT::Iterator HandleHashMapT::first() const + { + Iterator it; + it.handle = invalid; + it.pos = 0; + it.num = m_numElements; + + if (0 == it.num) + { + return it; + } + + ++it.num; + next(it); + return it; + } + + template + inline bool HandleHashMapT::next(Iterator& _it) const + { + if (0 == _it.num) + { + return false; + } + + for ( + ;_it.pos < MaxCapacityT && invalid == m_handle[_it.pos] + ; ++_it.pos + ); + _it.handle = m_handle[_it.pos]; + ++_it.pos; + --_it.num; + return true; + } + + template + inline uint32_t HandleHashMapT::findIndex(KeyT _key) const + { + const KeyT hash = mix(_key); + + const uint32_t firstIdx = hash % MaxCapacityT; + uint32_t idx = firstIdx; + do + { + if (m_handle[idx] == invalid) + { + return UINT32_MAX; + } + + if (m_key[idx] == _key) + { + return idx; + } + + idx = (idx + 1) % MaxCapacityT; + + } while (idx != firstIdx); + + return UINT32_MAX; + } + + template + inline void HandleHashMapT::removeIndex(uint32_t _idx) + { + m_handle[_idx] = invalid; + --m_numElements; + + for (uint32_t idx = (_idx + 1) % MaxCapacityT + ; m_handle[idx] != invalid + ; idx = (idx + 1) % MaxCapacityT) + { + if (m_handle[idx] != invalid) + { + const KeyT key = m_key[idx]; + if (idx != findIndex(key) ) + { + const uint16_t handle = m_handle[idx]; + m_handle[idx] = invalid; + --m_numElements; + insert(key, handle); + } + } + } + } + + template + inline uint32_t HandleHashMapT::mix(uint32_t _x) const + { + const uint32_t tmp0 = uint32_mul(_x, UINT32_C(2246822519) ); + const uint32_t tmp1 = uint32_rol(tmp0, 13); + const uint32_t result = uint32_mul(tmp1, UINT32_C(2654435761) ); + return result; + } + + template + inline uint64_t HandleHashMapT::mix(uint64_t _x) const + { + const uint64_t tmp0 = uint64_mul(_x, UINT64_C(14029467366897019727) ); + const uint64_t tmp1 = uint64_rol(tmp0, 31); + const uint64_t result = uint64_mul(tmp1, UINT64_C(11400714785074694791) ); + return result; + } + + template + inline HandleHashMapAllocT::HandleHashMapAllocT() + { + reset(); + } + + template + inline HandleHashMapAllocT::~HandleHashMapAllocT() + { + } + + template + inline uint16_t HandleHashMapAllocT::alloc(KeyT _key) + { + uint16_t handle = m_alloc.alloc(); + if (invalid == handle) + { + return invalid; + } + + bool ok = m_table.insert(_key, handle); + if (!ok) + { + m_alloc.free(handle); + return invalid; + } + + return handle; + } + + template + inline void HandleHashMapAllocT::free(KeyT _key) + { + uint16_t handle = m_table.find(_key); + if (invalid == handle) + { + return; + } + + m_table.removeByKey(_key); + m_alloc.free(handle); + } + + template + inline void HandleHashMapAllocT::free(uint16_t _handle) + { + m_table.removeByHandle(_handle); + m_alloc.free(_handle); + } + + template + inline uint16_t HandleHashMapAllocT::find(KeyT _key) const + { + return m_table.find(_key); + } + + template + inline const uint16_t* HandleHashMapAllocT::getHandles() const + { + return m_alloc.getHandles(); + } + + template + inline uint16_t HandleHashMapAllocT::getHandleAt(uint16_t _at) const + { + return m_alloc.getHandleAt(_at); + } + + template + inline uint16_t HandleHashMapAllocT::getNumHandles() const + { + return m_alloc.getNumHandles(); + } + + template + inline uint16_t HandleHashMapAllocT::getMaxHandles() const + { + return m_alloc.getMaxHandles(); + } + + template + inline bool HandleHashMapAllocT::isValid(uint16_t _handle) const + { + return m_alloc.isValid(_handle); + } + + template + inline void HandleHashMapAllocT::reset() + { + m_table.reset(); + m_alloc.reset(); + } + +} // namespace bx