Introduce Trie Iterators (#413)

* TrieIter: Add possibility to obtain a new'd HashTable iterator

* TrieIter: Add CellTrieIter and storage

* TrieIter: Implement TrieIterCreate

* TrieIter: Implement TrieIterEnded

* TrieIter: Implement TrieIterMore

* TrieIter: Implement TrieIterGetKey

* TrieIter: Implement TrieIterGetSize

* TrieIter: Implement TrieIterGetCell

* TrieIter: Implement TrieIterGetString

* TrieIter: Implement TrieIterGetArray

* TrieIter: Implement TrieIterDestroy

* TrieIter: Invalidate any mutating change that is key addition or key removal

* TrieIter: Clean up the handles at map change

* TrieITer; Add iter tests to trietest.sma

* TrieIter: Fix linux compilation

* TrieIter: Rename TrieIterMore to TrieIterNext

* TrieIter: Adjust documentation

* TrieITer; Adjust trietest.sma

* TrieIter: Create a custom StringHashMap class instead

+ used a copy of |iterator| instead of dynamic allocation
+ initialized vars directly in constructor
+ added a nested iteration test
This commit is contained in:
Vincent Herbet
2017-08-01 15:05:27 +02:00
committed by GitHub
parent 304e992942
commit 1dc1f1b9c4
6 changed files with 743 additions and 5 deletions

View File

@ -417,6 +417,7 @@ int C_Spawn(edict_t *pent)
ArrayHandles.clear();
TrieHandles.clear();
TrieIterHandles.clear();
TrieSnapshotHandles.clear();
DataPackHandles.clear();
TextParsersHandles.clear();

View File

@ -45,6 +45,11 @@ class NativeHandle
m_handles.clear();
}
size_t size()
{
return m_handles.length();
}
T *lookup(int handle)
{
--handle;

View File

@ -10,6 +10,7 @@
#include "trie_natives.h"
NativeHandle<CellTrie> TrieHandles;
NativeHandle<CellTrieIter> TrieIterHandles;
NativeHandle<TrieSnapshot> TrieSnapshotHandles;
// native Trie:TrieCreate();
@ -344,6 +345,18 @@ static cell AMX_NATIVE_CALL TrieDestroy(AMX *amx, cell *params)
return 0;
}
CellTrieIter *iter;
for (size_t index = 1; index <= TrieIterHandles.size(); index++)
{
if ((iter = TrieIterHandles.lookup(index)))
{
if (iter->trie == t)
{
iter->trie = nullptr;
}
}
}
if (TrieHandles.destroy(*ptr))
{
*ptr = 0;
@ -469,6 +482,226 @@ static cell AMX_NATIVE_CALL TrieSnapshotDestroy(AMX *amx, cell *params)
return 0;
}
#define CHECK_ITER_HANDLE(handle) \
if (!handle) { \
LogError(amx, AMX_ERR_NATIVE, "Invalid map iterator handle provided (%d)", params[arg_handle]); \
return 0; \
} \
if (!handle->trie) { \
LogError(amx, AMX_ERR_NATIVE, "Closed map iterator handle provided (%d)", params[arg_handle]); \
return 0; \
} \
if (handle->mod_count != handle->trie->map.mod_count()) { \
LogError(amx, AMX_ERR_NATIVE, "Outdated map iterator handle provided (%d)", params[arg_handle]);\
return 0; \
}
// native TrieIter:TrieIterCreate(Trie:handle)
static cell AMX_NATIVE_CALL TrieIterCreate(AMX *amx, cell *params)
{
enum args { arg_count, arg_handle };
auto handle = TrieHandles.lookup(params[arg_handle]);
if (!handle)
{
LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[arg_handle]);
return 0;
}
auto index = TrieIterHandles.create(handle);
auto iter = TrieIterHandles.lookup(index);
return static_cast<cell>(index);
}
// native bool:TrieIterEnded(TrieIter:handle)
static cell AMX_NATIVE_CALL TrieIterEnded(AMX *amx, cell *params)
{
enum args { arg_count, arg_handle };
auto handle = TrieIterHandles.lookup(params[arg_handle]);
CHECK_ITER_HANDLE(handle)
return handle->iter.empty();
}
// native TrieIterNext(TrieIter:handle)
static cell AMX_NATIVE_CALL TrieIterNext(AMX *amx, cell *params)
{
enum args { arg_count, arg_handle };
auto handle = TrieIterHandles.lookup(params[arg_handle]);
CHECK_ITER_HANDLE(handle)
if (handle->iter.empty())
{
return 0;
}
handle->iter.next();
return 1;
}
// native TrieIterGetKey(TrieIter:handle, key[], outputsize)
static cell AMX_NATIVE_CALL TrieIterGetKey(AMX *amx, cell *params)
{
enum args { arg_count, arg_handle, arg_output, arg_outputsize };
auto handle = TrieIterHandles.lookup(params[arg_handle]);
CHECK_ITER_HANDLE(handle)
auto& iter = handle->iter;
if (iter.empty())
{
*get_amxaddr(amx, params[arg_output]) = '\0';
return 0;
}
return set_amxstring_utf8(amx, params[arg_output], iter->key.chars(), iter->key.length(), params[arg_outputsize]);
}
// native TrieIterGetSize(TrieIter:handle)
static cell AMX_NATIVE_CALL TrieIterGetSize(AMX *amx, cell *params)
{
enum args { arg_count, arg_handle };
auto handle = TrieIterHandles.lookup(params[arg_handle]);
CHECK_ITER_HANDLE(handle)
return handle->trie->map.elements();
}
// native bool:TrieIterGetCell(TrieIter:handle, &any:value)
static cell AMX_NATIVE_CALL TrieIterGetCell(AMX *amx, cell *params)
{
enum args { arg_count, arg_handle, arg_outputvalue };
auto handle = TrieIterHandles.lookup(params[arg_handle]);
CHECK_ITER_HANDLE(handle)
auto& iter = handle->iter;
if (iter.empty() || !iter->value.isCell())
{
return false;
}
*get_amxaddr(amx, params[arg_outputvalue]) = iter->value.cell_();
return true;
}
// native bool:TrieIterGetString(TrieIter:handle, buffer[], outputsize, &size = 0)
static cell AMX_NATIVE_CALL TrieIterGetString(AMX *amx, cell *params)
{
enum args { arg_count, arg_handle, arg_output, arg_outputsize, arg_refsize };
auto handle = TrieIterHandles.lookup(params[arg_handle]);
CHECK_ITER_HANDLE(handle)
if (params[arg_outputsize] < 0)
{
LogError(amx, AMX_ERR_NATIVE, "Invalid buffer size (%d)", params[arg_outputsize]);
return 0;
}
auto& iter = handle->iter;
if (iter.empty() || !iter->value.isString())
{
return false;
}
auto refsize = get_amxaddr(amx, params[arg_refsize]);
*refsize = set_amxstring_utf8(amx, params[arg_output], iter->value.chars(), strlen(iter->value.chars()), params[arg_outputsize]);
return true;
}
// native bool:TrieIterGetArray(TrieIter:handle, array[], outputsize, &size = 0)
static cell AMX_NATIVE_CALL TrieIterGetArray(AMX *amx, cell *params)
{
enum args { arg_count, arg_handle, arg_output, arg_outputsize, arg_refsize };
auto handle = TrieIterHandles.lookup(params[arg_handle]);
CHECK_ITER_HANDLE(handle)
auto outputSize = params[arg_outputsize];
if (outputSize < 0)
{
LogError(amx, AMX_ERR_NATIVE, "Invalid array size (%d)", params[arg_outputsize]);
return 0;
}
auto& iter = handle->iter;
if (iter.empty() || !iter->value.isArray())
{
return false;
}
auto pOutput = get_amxaddr(amx, params[arg_output]);
auto pSize = get_amxaddr(amx, params[arg_refsize]);
if (!iter->value.array() || !outputSize)
{
*pSize = 0;
return false;
}
auto length = iter->value.arrayLength();
auto base = iter->value.array();
if (length > size_t(outputSize))
{
length = outputSize;
}
*pSize = length;
memcpy(pOutput, base, sizeof(cell) * length);
return true;
}
// native TrieIterDestroy(&TrieIter:handle)
static cell AMX_NATIVE_CALL TrieIterDestroy(AMX *amx, cell *params)
{
enum args { arg_count, arg_handle };
auto refhandle = get_amxaddr(amx, params[arg_handle]);
auto handle = TrieIterHandles.lookup(*refhandle);
if (!handle)
{
return false;
}
handle->trie = nullptr;
if (TrieIterHandles.destroy(*refhandle))
{
*refhandle = 0;
return true;
}
return false;
}
AMX_NATIVE_INFO trie_Natives[] =
{
{ "TrieCreate" , TrieCreate },
@ -493,6 +726,16 @@ AMX_NATIVE_INFO trie_Natives[] =
{ "TrieSnapshotGetKey" , TrieSnapshotGetKey },
{ "TrieSnapshotDestroy" , TrieSnapshotDestroy },
{ "TrieIterCreate" , TrieIterCreate },
{ "TrieIterEnded" , TrieIterEnded },
{ "TrieIterNext" , TrieIterNext },
{ "TrieIterGetKey" , TrieIterGetKey },
{ "TrieIterGetSize" , TrieIterGetSize },
{ "TrieIterGetCell" , TrieIterGetCell },
{ "TrieIterGetString" , TrieIterGetString },
{ "TrieIterGetArray" , TrieIterGetArray },
{ "TrieIterDestroy" , TrieIterDestroy },
{ nullptr , nullptr}
};

View File

@ -14,6 +14,7 @@
#include <sm_stringhashmap.h>
#include <sm_memtable.h>
#include "natives_handles.h"
#include <amtl/am-uniqueptr.h>
enum EntryType
{
@ -138,9 +139,128 @@ private:
cell data_;
};
template <typename T>
class StringHashMapCustom
{
typedef StringHashMap<T> Internal;
public:
StringHashMapCustom() : mod_count_(0)
{}
public:
typedef typename Internal::Result Result;
typedef typename Internal::Insert Insert;
typedef typename Internal::iterator iterator;
public:
size_t elements() const
{
return internal_.elements();
}
iterator iter()
{
return internal_.iter();
}
bool contains(const char *aKey)
{
return internal_.contains(aKey);
}
bool replace(const char *aKey, const T &value)
{
return internal_.contains(aKey, value);
}
bool insert(const char *aKey, const T &value)
{
if (internal_.insert(aKey, value))
{
mod_count_++;
return true;
}
return false;
}
bool remove(const char *aKey)
{
if (internal_.remove(aKey))
{
mod_count_++;
return true;
}
return false;
}
void clear()
{
mod_count_++;
internal_.clear();
}
void remove(Result &r)
{
mod_count_++;
internal_.remove(r);
}
Result find(const char *aKey)
{
return internal_.find(aKey);
}
Insert findForAdd(const char *aKey)
{
return internal_.findForAdd(aKey);
}
bool add(Insert &i, const char *aKey)
{
if (internal_.add(i, aKey))
{
mod_count_++;
return true;
}
return false;
}
bool add(Insert &i)
{
if (internal_.add(i))
{
mod_count_++;
return true;
}
return false;
}
public:
size_t mod_count() const
{
return mod_count_;
}
private:
Internal internal_;
size_t mod_count_;
};
struct CellTrie
{
StringHashMap<Entry> map;
StringHashMapCustom<Entry> map;
};
struct CellTrieIter
{
CellTrieIter(CellTrie *_trie) : trie(_trie), iter(_trie->map.iter()), mod_count(_trie->map.mod_count())
{}
CellTrie *trie;
StringHashMapCustom<Entry>::iterator iter;
size_t mod_count;
};
struct TrieSnapshot
@ -160,6 +280,7 @@ struct TrieSnapshot
};
extern NativeHandle<CellTrie> TrieHandles;
extern NativeHandle<CellTrieIter> TrieIterHandles;
extern NativeHandle<TrieSnapshot> TrieSnapshotHandles;
extern AMX_NATIVE_INFO trie_Natives[];