add unordered map to cmap

This commit is contained in:
Isaac0-dev 2024-11-30 15:33:11 +10:00
parent 2bfd9bc5f6
commit c0691d4b3e
5 changed files with 166 additions and 70 deletions

View file

@ -1,84 +1,187 @@
#include <map>
#include <unordered_map>
#include <cstdint>
#include <cstddef>
#include <memory>
typedef std::map<int64_t, void*> Map;
enum class MapType {
Ordered,
Unordered
};
struct MapIter {
Map* map;
Map::iterator itr;
// Ordered maps can be iterated by key order
// Unordered maps have the fastest lookup times (also called a hash map)
template <typename Key, typename Value>
class HMap {
public:
HMap(MapType type = MapType::Ordered) : mMapType(type) {
switch (mMapType) {
case MapType::Ordered:
mOrderedMap = std::make_unique<std::map<Key, Value>>();
break;
case MapType::Unordered:
mUnorderedMap = std::make_unique<std::unordered_map<Key, Value>>();
break;
}
}
Value get(Key key) {
switch (mMapType) {
case MapType::Ordered:
if (mOrderedMap->count(key)) {
return mOrderedMap->at(key);
}
break;
case MapType::Unordered:
if (mUnorderedMap->count(key)) {
return mUnorderedMap->at(key);
}
break;
}
return nullptr;
}
void put(Key key, Value value) {
switch (mMapType) {
case MapType::Ordered:
if (mOrderedMap->count(key)) { mOrderedMap->erase(key); }
mOrderedMap->insert({key, value});
break;
case MapType::Unordered:
if (mUnorderedMap->count(key)) { mUnorderedMap->erase(key); }
mUnorderedMap->insert({key, value});
break;
}
}
void erase(Key key) {
switch (mMapType) {
case MapType::Ordered:
mOrderedMap->erase(key);
break;
case MapType::Unordered:
mUnorderedMap->erase(key);
break;
}
}
void clear() {
switch (mMapType) {
case MapType::Ordered:
mOrderedMap->clear();
break;
case MapType::Unordered:
mUnorderedMap->clear();
break;
}
}
size_t size() const {
switch (mMapType) {
case MapType::Ordered:
return mOrderedMap->size();
case MapType::Unordered:
return mUnorderedMap->size();
}
return 0;
}
void* begin() {
switch (mMapType) {
case MapType::Ordered: {
auto& orderedMap = *mOrderedMap;
if (orderedMap.empty()) { return nullptr; }
mOrderedIterator = mOrderedMap->begin();
return mOrderedIterator->second;
}
case MapType::Unordered: {
auto& unorderedMap = *mUnorderedMap;
if (unorderedMap.empty()) { return nullptr; }
mUnorderedIterator = mUnorderedMap->begin();
return mUnorderedIterator->second;
}
}
return nullptr;
}
void* next() {
switch (mMapType) {
case MapType::Ordered: {
if (++mOrderedIterator != mOrderedMap->end()) {
return mOrderedIterator->second;
}
break;
}
case MapType::Unordered: {
if (++mUnorderedIterator != mUnorderedMap->end()) {
return mUnorderedIterator->second;
}
break;
}
}
return nullptr;
}
private:
MapType mMapType;
std::unique_ptr<std::map<Key, Value>> mOrderedMap;
typename std::map<Key, Value>::iterator mOrderedIterator;
std::unique_ptr<std::unordered_map<Key, Value>> mUnorderedMap;
typename std::unordered_map<Key, Value>::iterator mUnorderedIterator;
};
extern "C" {
void* hmap_create() {
return reinterpret_cast<void*> (new Map());
void* hmap_create(MapType type) {
return new HMap<int64_t, void*>(type);
}
void* hmap_get(void* map, int64_t k) {
void* hmap_get(void* map, int64_t key) {
if (!map) { return NULL; }
Map* m = reinterpret_cast<Map*> (map);
Map::iterator pos = m->find(k);
if (pos == m->end()) {
return NULL;
} else {
return pos->second;
}
HMap<int64_t, void*>* hmap = reinterpret_cast<HMap<int64_t, void*>*>(map);
return hmap->get(key);
}
void hmap_put(void* map, int64_t k, void* v) {
void hmap_put(void* map, int64_t key, void* value) {
if (!map) { return; }
Map* m = reinterpret_cast<Map*> (map);
if (m->count(k) > 0) {
m->erase(k);
}
m->insert(std::pair<int64_t, void*>(k, v));
HMap<int64_t, void*>* hmap = reinterpret_cast<HMap<int64_t, void*>*>(map);
hmap->put(key, value);
}
void hmap_del(void* map, int64_t k) {
void hmap_del(void* map, int64_t key) {
if (!map) { return; }
Map* m = reinterpret_cast<Map*> (map);
m->erase(k);
HMap<int64_t, void*>* hmap = reinterpret_cast<HMap<int64_t, void*>*>(map);
hmap->erase(key);
}
void hmap_clear(void* map) {
if (!map) { return; }
Map* m = reinterpret_cast<Map*> (map);
m->clear();
HMap<int64_t, void*>* hmap = reinterpret_cast<HMap<int64_t, void*>*>(map);
hmap->clear();
}
void hmap_destroy(void* map) {
if (!map) { return; }
delete reinterpret_cast<HMap<int64_t, void*>*>(map);
}
size_t hmap_len(void* map) {
if (!map) { return 0; }
Map* m = reinterpret_cast<Map*> (map);
return m->size();
HMap<int64_t, void*>* hmap = reinterpret_cast<HMap<int64_t, void*>*>(map);
return hmap->size();
}
void* hmap_iter(void* map) {
if (!map) { return nullptr; }
auto iter = new MapIter();
Map* m = reinterpret_cast<Map*> (map);
iter->map = m;
return reinterpret_cast<void*> (iter);
void* hmap_begin(void* map) {
if (!map) { return NULL; }
HMap<int64_t, void*>* hmap = reinterpret_cast<HMap<int64_t, void*>*>(map);
return hmap->begin();
}
void* hmap_begin(void* iter) {
if (!iter) { return nullptr; }
MapIter* i = reinterpret_cast<MapIter*> (iter);
i->itr = i->map->begin();
if (i->itr == i->map->end()) {
return NULL;
}
return i->itr->second;
void* hmap_next(void* map) {
if (!map) { return NULL; }
HMap<int64_t, void*>* hmap = reinterpret_cast<HMap<int64_t, void*>*>(map);
return hmap->next();
}
void* hmap_next(void* iter) {
if (!iter) { return nullptr; }
MapIter* i = reinterpret_cast<MapIter*> (iter);
i->itr++;
if (i->itr == i->map->end()) {
return NULL;
}
return i->itr->second;
}
} // extern "C"

View file

@ -2,16 +2,16 @@
#define DYNOS_CMAP_CPP_H
#ifndef __cplusplus
void* hmap_create();
void* hmap_create(bool useUnordered);
void* hmap_get(void* map, int64_t k);
void hmap_put(void* map, int64_t k, void* v);
void hmap_del(void* map, int64_t k);
void hmap_clear(void* map);
void hmap_destroy(void* map);
size_t hmap_len(void* map);
void* hmap_iter(void* map);
void* hmap_begin(void* iter);
void* hmap_next(void* iter);
void* hmap_begin(void* map);
void* hmap_next(void* map);
#endif
#endif

View file

@ -199,7 +199,6 @@ struct SmCodeGlyph sSmCodeDuplicateGlyphs[] = {
};
static void* sCharMap = NULL;
static void* sCharIter = NULL;
static s32 count_bytes_for_char(char* text) {
s32 bytes = 0;
@ -229,8 +228,7 @@ static u64 convert_unicode_char_to_u64(char* text) {
}
void djui_unicode_init(void) {
sCharMap = hmap_create();
sCharIter = hmap_iter(sCharMap);
sCharMap = hmap_create(true);
size_t glyphCount = sizeof(sSmCodeGlyphs) / sizeof(sSmCodeGlyphs[0]);
for (size_t i = 0; i < glyphCount; i++) {

View file

@ -16,12 +16,9 @@ void smlua_pointer_user_data_add(uintptr_t pointer, CObject *obj) {
if (pointer == 0) { return; }
if (!sPointers) {
sPointers = hmap_create();
}
if (!hmap_get(sPointers, pointer)) {
hmap_put(sPointers, pointer, obj);
sPointers = hmap_create(true);
}
hmap_put(sPointers, pointer, obj);
}
void smlua_pointer_user_data_delete(uintptr_t pointer) {

View file

@ -14,7 +14,6 @@
#include "pc/debug_context.h"
static void* sSoMap = NULL;
static void* sSoIter = NULL;
#define FORGET_TIMEOUT 10
@ -33,8 +32,7 @@ static bool sFreeingAll = false;
////////////
void sync_objects_init_system(void) {
sSoMap = hmap_create();
sSoIter = hmap_iter(sSoMap);
sSoMap = hmap_create(true);
}
static bool sync_objects_forget_list_contains(struct SyncObject* so) {
@ -254,11 +252,11 @@ struct SyncObject* sync_object_get(u32 syncId) {
}
struct SyncObject* sync_object_get_first(void) {
return hmap_begin(sSoIter);
return hmap_begin(sSoMap);
}
struct SyncObject* sync_object_get_next(void) {
return hmap_next(sSoIter);
return hmap_next(sSoMap);
}
struct Object* sync_object_get_object(u32 syncId) {