From 23a6b6726f2153beb293225657e096a4e950d609 Mon Sep 17 00:00:00 2001 From: Isaac0-dev <62234577+Isaac0-dev@users.noreply.github.com> Date: Sat, 30 Nov 2024 16:19:48 +1000 Subject: [PATCH] better typing for cmaps --- data/dynos_cmap.cpp | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/data/dynos_cmap.cpp b/data/dynos_cmap.cpp index a89f859f6..0cef09dc3 100644 --- a/data/dynos_cmap.cpp +++ b/data/dynos_cmap.cpp @@ -12,21 +12,20 @@ enum class MapType { // Ordered maps can be iterated by key order // Unordered maps have the fastest lookup times (also called a hash map) -template class HMap { public: HMap(MapType type = MapType::Ordered) : mMapType(type) { switch (mMapType) { case MapType::Ordered: - mOrderedMap = std::make_unique>(); + mOrderedMap = std::make_unique>(); break; case MapType::Unordered: - mUnorderedMap = std::make_unique>(); + mUnorderedMap = std::make_unique>(); break; } } - Value get(Key key) { + void* get(int64_t key) { switch (mMapType) { case MapType::Ordered: if (mOrderedMap->count(key)) { @@ -42,20 +41,18 @@ public: return nullptr; } - void put(Key key, Value value) { + void put(int64_t key, void* value) { switch (mMapType) { case MapType::Ordered: - if (mOrderedMap->count(key)) { mOrderedMap->erase(key); } - mOrderedMap->insert({key, value}); + mOrderedMap->insert_or_assign(key, value); break; case MapType::Unordered: - if (mUnorderedMap->count(key)) { mUnorderedMap->erase(key); } - mUnorderedMap->insert({key, value}); + mUnorderedMap->insert_or_assign(key, value); break; } } - void erase(Key key) { + void erase(int64_t key) { switch (mMapType) { case MapType::Ordered: mOrderedMap->erase(key); @@ -126,62 +123,62 @@ public: private: MapType mMapType; - std::unique_ptr> mOrderedMap; - typename std::map::iterator mOrderedIterator; + std::unique_ptr> mOrderedMap; + typename std::map::iterator mOrderedIterator; - std::unique_ptr> mUnorderedMap; - typename std::unordered_map::iterator mUnorderedIterator; + std::unique_ptr> mUnorderedMap; + typename std::unordered_map::iterator mUnorderedIterator; }; extern "C" { void* hmap_create(MapType type) { - return new HMap(type); + return new HMap(type); } void* hmap_get(void* map, int64_t key) { if (!map) { return NULL; } - HMap* hmap = reinterpret_cast*>(map); + HMap* hmap = reinterpret_cast(map); return hmap->get(key); } void hmap_put(void* map, int64_t key, void* value) { if (!map) { return; } - HMap* hmap = reinterpret_cast*>(map); + HMap* hmap = reinterpret_cast(map); hmap->put(key, value); } void hmap_del(void* map, int64_t key) { if (!map) { return; } - HMap* hmap = reinterpret_cast*>(map); + HMap* hmap = reinterpret_cast(map); hmap->erase(key); } void hmap_clear(void* map) { if (!map) { return; } - HMap* hmap = reinterpret_cast*>(map); + HMap* hmap = reinterpret_cast(map); hmap->clear(); } void hmap_destroy(void* map) { if (!map) { return; } - delete reinterpret_cast*>(map); + delete reinterpret_cast(map); } size_t hmap_len(void* map) { if (!map) { return 0; } - HMap* hmap = reinterpret_cast*>(map); + HMap* hmap = reinterpret_cast(map); return hmap->size(); } void* hmap_begin(void* map) { if (!map) { return NULL; } - HMap* hmap = reinterpret_cast*>(map); + HMap* hmap = reinterpret_cast(map); return hmap->begin(); } void* hmap_next(void* map) { if (!map) { return NULL; } - HMap* hmap = reinterpret_cast*>(map); + HMap* hmap = reinterpret_cast(map); return hmap->next(); } }