| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- /*
- * Copyright 2010-2020 Branimir Karadzic. All rights reserved.
- * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
- */
- #ifndef BX_HANDLE_ALLOC_H_HEADER_GUARD
- #define BX_HANDLE_ALLOC_H_HEADER_GUARD
- #include "bx.h"
- #include "allocator.h"
- #include "uint32_t.h"
- namespace bx
- {
- constexpr uint16_t kInvalidHandle = UINT16_MAX;
- ///
- class HandleAlloc
- {
- public:
- ///
- HandleAlloc(uint16_t _maxHandles);
- ///
- ~HandleAlloc();
- ///
- const uint16_t* getHandles() const;
- ///
- uint16_t getHandleAt(uint16_t _at) const;
- ///
- uint16_t getNumHandles() const;
- ///
- uint16_t getMaxHandles() const;
- ///
- uint16_t alloc();
- ///
- bool isValid(uint16_t _handle) const;
- ///
- void free(uint16_t _handle);
- ///
- void reset();
- private:
- HandleAlloc();
- ///
- uint16_t* getDensePtr() const;
- ///
- uint16_t* getSparsePtr() const;
- uint16_t m_numHandles;
- uint16_t m_maxHandles;
- };
- ///
- HandleAlloc* createHandleAlloc(AllocatorI* _allocator, uint16_t _maxHandles);
- ///
- void destroyHandleAlloc(AllocatorI* _allocator, HandleAlloc* _handleAlloc);
- ///
- template <uint16_t MaxHandlesT>
- class HandleAllocT : public HandleAlloc
- {
- public:
- ///
- HandleAllocT();
- ///
- ~HandleAllocT();
- private:
- uint16_t m_padding[2*MaxHandlesT];
- };
- ///
- template <uint16_t MaxHandlesT>
- class HandleListT
- {
- public:
- ///
- HandleListT();
- ///
- void pushBack(uint16_t _handle);
- ///
- uint16_t popBack();
- ///
- void pushFront(uint16_t _handle);
- ///
- uint16_t popFront();
- ///
- uint16_t getFront() const;
- ///
- uint16_t getBack() const;
- ///
- uint16_t getNext(uint16_t _handle) const;
- ///
- uint16_t getPrev(uint16_t _handle) const;
- ///
- void remove(uint16_t _handle);
- ///
- void reset();
- private:
- ///
- void insertBefore(uint16_t _before, uint16_t _handle);
- ///
- void insertAfter(uint16_t _after, uint16_t _handle);
- ///
- bool isValid(uint16_t _handle) const;
- ///
- void updateFrontBack(uint16_t _handle);
- uint16_t m_front;
- uint16_t m_back;
- struct Link
- {
- uint16_t m_prev;
- uint16_t m_next;
- };
- Link m_links[MaxHandlesT];
- };
- ///
- template <uint16_t MaxHandlesT>
- class HandleAllocLruT
- {
- public:
- ///
- HandleAllocLruT();
- ///
- ~HandleAllocLruT();
- ///
- const uint16_t* getHandles() const;
- ///
- uint16_t getHandleAt(uint16_t _at) const;
- ///
- uint16_t getNumHandles() const;
- ///
- uint16_t getMaxHandles() const;
- ///
- uint16_t alloc();
- ///
- bool isValid(uint16_t _handle) const;
- ///
- void free(uint16_t _handle);
- ///
- void touch(uint16_t _handle);
- ///
- uint16_t getFront() const;
- ///
- uint16_t getBack() const;
- ///
- uint16_t getNext(uint16_t _handle) const;
- ///
- uint16_t getPrev(uint16_t _handle) const;
- ///
- void reset();
- private:
- HandleListT<MaxHandlesT> m_list;
- HandleAllocT<MaxHandlesT> m_alloc;
- };
- ///
- template <uint32_t MaxCapacityT, typename KeyT = uint32_t>
- class HandleHashMapT
- {
- public:
- ///
- HandleHashMapT();
- ///
- ~HandleHashMapT();
- ///
- bool insert(KeyT _key, uint16_t _handle);
- ///
- bool removeByKey(KeyT _key);
- ///
- bool removeByHandle(uint16_t _handle);
- ///
- uint16_t find(KeyT _key) const;
- ///
- void reset();
- ///
- uint32_t getNumElements() const;
- ///
- uint32_t getMaxCapacity() const;
- ///
- struct Iterator
- {
- uint16_t handle;
- private:
- friend class HandleHashMapT<MaxCapacityT, KeyT>;
- uint32_t pos;
- uint32_t num;
- };
- ///
- Iterator first() const;
- ///
- bool next(Iterator& _it) const;
- private:
- ///
- uint32_t findIndex(KeyT _key) const;
- ///
- void removeIndex(uint32_t _idx);
- ///
- uint32_t mix(uint32_t _x) const;
- ///
- uint64_t mix(uint64_t _x) const;
- uint32_t m_maxCapacity;
- uint32_t m_numElements;
- KeyT m_key[MaxCapacityT];
- uint16_t m_handle[MaxCapacityT];
- };
- ///
- template <uint16_t MaxHandlesT, typename KeyT = uint32_t>
- class HandleHashMapAllocT
- {
- public:
- ///
- HandleHashMapAllocT();
- ///
- ~HandleHashMapAllocT();
- ///
- uint16_t alloc(KeyT _key);
- ///
- void free(KeyT _key);
- ///
- void free(uint16_t _handle);
- ///
- uint16_t find(KeyT _key) const;
- ///
- const uint16_t* getHandles() const;
- ///
- uint16_t getHandleAt(uint16_t _at) const;
- ///
- uint16_t getNumHandles() const;
- ///
- uint16_t getMaxHandles() const;
- ///
- bool isValid(uint16_t _handle) const;
- ///
- void reset();
- private:
- HandleHashMapT<MaxHandlesT+MaxHandlesT/2, KeyT> m_table;
- HandleAllocT<MaxHandlesT> m_alloc;
- };
- } // namespace bx
- #include "inline/handlealloc.inl"
- #endif // BX_HANDLE_ALLOC_H_HEADER_GUARD
|