mirror of
https://github.com/coop-deluxe/sm64coopdx.git
synced 2025-10-30 08:01:01 +00:00
add unordered map to cmap
This commit is contained in:
parent
2bfd9bc5f6
commit
c0691d4b3e
5 changed files with 166 additions and 70 deletions
|
|
@ -1,84 +1,187 @@
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <unordered_map>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
typedef std::map<int64_t, void*> Map;
|
enum class MapType {
|
||||||
|
Ordered,
|
||||||
|
Unordered
|
||||||
|
};
|
||||||
|
|
||||||
struct MapIter {
|
// Ordered maps can be iterated by key order
|
||||||
Map* map;
|
// Unordered maps have the fastest lookup times (also called a hash map)
|
||||||
Map::iterator itr;
|
|
||||||
|
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" {
|
extern "C" {
|
||||||
|
void* hmap_create(MapType type) {
|
||||||
void* hmap_create() {
|
return new HMap<int64_t, void*>(type);
|
||||||
return reinterpret_cast<void*> (new Map());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void* hmap_get(void* map, int64_t k) {
|
void* hmap_get(void* map, int64_t key) {
|
||||||
if (!map) { return NULL; }
|
if (!map) { return NULL; }
|
||||||
Map* m = reinterpret_cast<Map*> (map);
|
HMap<int64_t, void*>* hmap = reinterpret_cast<HMap<int64_t, void*>*>(map);
|
||||||
Map::iterator pos = m->find(k);
|
return hmap->get(key);
|
||||||
if (pos == m->end()) {
|
|
||||||
return NULL;
|
|
||||||
} else {
|
|
||||||
return pos->second;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void hmap_put(void* map, int64_t k, void* v) {
|
void hmap_put(void* map, int64_t key, void* value) {
|
||||||
if (!map) { return; }
|
if (!map) { return; }
|
||||||
Map* m = reinterpret_cast<Map*> (map);
|
HMap<int64_t, void*>* hmap = reinterpret_cast<HMap<int64_t, void*>*>(map);
|
||||||
if (m->count(k) > 0) {
|
hmap->put(key, value);
|
||||||
m->erase(k);
|
|
||||||
}
|
|
||||||
m->insert(std::pair<int64_t, void*>(k, v));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void hmap_del(void* map, int64_t k) {
|
void hmap_del(void* map, int64_t key) {
|
||||||
if (!map) { return; }
|
if (!map) { return; }
|
||||||
Map* m = reinterpret_cast<Map*> (map);
|
HMap<int64_t, void*>* hmap = reinterpret_cast<HMap<int64_t, void*>*>(map);
|
||||||
m->erase(k);
|
hmap->erase(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
void hmap_clear(void* map) {
|
void hmap_clear(void* map) {
|
||||||
if (!map) { return; }
|
if (!map) { return; }
|
||||||
Map* m = reinterpret_cast<Map*> (map);
|
HMap<int64_t, void*>* hmap = reinterpret_cast<HMap<int64_t, void*>*>(map);
|
||||||
m->clear();
|
hmap->clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void hmap_destroy(void* map) {
|
||||||
|
if (!map) { return; }
|
||||||
|
delete reinterpret_cast<HMap<int64_t, void*>*>(map);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t hmap_len(void* map) {
|
size_t hmap_len(void* map) {
|
||||||
if (!map) { return 0; }
|
if (!map) { return 0; }
|
||||||
Map* m = reinterpret_cast<Map*> (map);
|
HMap<int64_t, void*>* hmap = reinterpret_cast<HMap<int64_t, void*>*>(map);
|
||||||
return m->size();
|
return hmap->size();
|
||||||
}
|
}
|
||||||
|
|
||||||
void* hmap_iter(void* map) {
|
void* hmap_begin(void* map) {
|
||||||
if (!map) { return nullptr; }
|
if (!map) { return NULL; }
|
||||||
auto iter = new MapIter();
|
HMap<int64_t, void*>* hmap = reinterpret_cast<HMap<int64_t, void*>*>(map);
|
||||||
Map* m = reinterpret_cast<Map*> (map);
|
return hmap->begin();
|
||||||
iter->map = m;
|
|
||||||
return reinterpret_cast<void*> (iter);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void* hmap_begin(void* iter) {
|
void* hmap_next(void* map) {
|
||||||
if (!iter) { return nullptr; }
|
if (!map) { return NULL; }
|
||||||
MapIter* i = reinterpret_cast<MapIter*> (iter);
|
HMap<int64_t, void*>* hmap = reinterpret_cast<HMap<int64_t, void*>*>(map);
|
||||||
i->itr = i->map->begin();
|
return hmap->next();
|
||||||
if (i->itr == i->map->end()) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return i->itr->second;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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"
|
|
||||||
|
|
@ -2,16 +2,16 @@
|
||||||
#define DYNOS_CMAP_CPP_H
|
#define DYNOS_CMAP_CPP_H
|
||||||
#ifndef __cplusplus
|
#ifndef __cplusplus
|
||||||
|
|
||||||
void* hmap_create();
|
void* hmap_create(bool useUnordered);
|
||||||
void* hmap_get(void* map, int64_t k);
|
void* hmap_get(void* map, int64_t k);
|
||||||
void hmap_put(void* map, int64_t k, void* v);
|
void hmap_put(void* map, int64_t k, void* v);
|
||||||
void hmap_del(void* map, int64_t k);
|
void hmap_del(void* map, int64_t k);
|
||||||
void hmap_clear(void* map);
|
void hmap_clear(void* map);
|
||||||
|
void hmap_destroy(void* map);
|
||||||
size_t hmap_len(void* map);
|
size_t hmap_len(void* map);
|
||||||
|
|
||||||
void* hmap_iter(void* map);
|
void* hmap_begin(void* map);
|
||||||
void* hmap_begin(void* iter);
|
void* hmap_next(void* map);
|
||||||
void* hmap_next(void* iter);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -199,7 +199,6 @@ struct SmCodeGlyph sSmCodeDuplicateGlyphs[] = {
|
||||||
};
|
};
|
||||||
|
|
||||||
static void* sCharMap = NULL;
|
static void* sCharMap = NULL;
|
||||||
static void* sCharIter = NULL;
|
|
||||||
|
|
||||||
static s32 count_bytes_for_char(char* text) {
|
static s32 count_bytes_for_char(char* text) {
|
||||||
s32 bytes = 0;
|
s32 bytes = 0;
|
||||||
|
|
@ -229,8 +228,7 @@ static u64 convert_unicode_char_to_u64(char* text) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void djui_unicode_init(void) {
|
void djui_unicode_init(void) {
|
||||||
sCharMap = hmap_create();
|
sCharMap = hmap_create(true);
|
||||||
sCharIter = hmap_iter(sCharMap);
|
|
||||||
|
|
||||||
size_t glyphCount = sizeof(sSmCodeGlyphs) / sizeof(sSmCodeGlyphs[0]);
|
size_t glyphCount = sizeof(sSmCodeGlyphs) / sizeof(sSmCodeGlyphs[0]);
|
||||||
for (size_t i = 0; i < glyphCount; i++) {
|
for (size_t i = 0; i < glyphCount; i++) {
|
||||||
|
|
|
||||||
|
|
@ -16,12 +16,9 @@ void smlua_pointer_user_data_add(uintptr_t pointer, CObject *obj) {
|
||||||
if (pointer == 0) { return; }
|
if (pointer == 0) { return; }
|
||||||
|
|
||||||
if (!sPointers) {
|
if (!sPointers) {
|
||||||
sPointers = hmap_create();
|
sPointers = hmap_create(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!hmap_get(sPointers, pointer)) {
|
|
||||||
hmap_put(sPointers, pointer, obj);
|
hmap_put(sPointers, pointer, obj);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void smlua_pointer_user_data_delete(uintptr_t pointer) {
|
void smlua_pointer_user_data_delete(uintptr_t pointer) {
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,6 @@
|
||||||
#include "pc/debug_context.h"
|
#include "pc/debug_context.h"
|
||||||
|
|
||||||
static void* sSoMap = NULL;
|
static void* sSoMap = NULL;
|
||||||
static void* sSoIter = NULL;
|
|
||||||
|
|
||||||
#define FORGET_TIMEOUT 10
|
#define FORGET_TIMEOUT 10
|
||||||
|
|
||||||
|
|
@ -33,8 +32,7 @@ static bool sFreeingAll = false;
|
||||||
////////////
|
////////////
|
||||||
|
|
||||||
void sync_objects_init_system(void) {
|
void sync_objects_init_system(void) {
|
||||||
sSoMap = hmap_create();
|
sSoMap = hmap_create(true);
|
||||||
sSoIter = hmap_iter(sSoMap);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool sync_objects_forget_list_contains(struct SyncObject* so) {
|
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) {
|
struct SyncObject* sync_object_get_first(void) {
|
||||||
return hmap_begin(sSoIter);
|
return hmap_begin(sSoMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct SyncObject* sync_object_get_next(void) {
|
struct SyncObject* sync_object_get_next(void) {
|
||||||
return hmap_next(sSoIter);
|
return hmap_next(sSoMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Object* sync_object_get_object(u32 syncId) {
|
struct Object* sync_object_get_object(u32 syncId) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue