From 3a56da3c09845676534f460f75c8adb6c65fbb79 Mon Sep 17 00:00:00 2001 From: gumi Date: Mon, 26 Mar 2018 17:11:34 -0400 Subject: add missing hashtable iterator buildins --- src/emap/hashtable.c | 42 ++++++++++++++++++++++++++++++++++++++++++ src/emap/hashtable.h | 3 +++ src/emap/init.c | 3 +++ src/emap/script_buildins.c | 39 +++++++++++++++++++++++++++++++++++++++ src/emap/script_buildins.h | 3 +++ 5 files changed, 90 insertions(+) diff --git a/src/emap/hashtable.c b/src/emap/hashtable.c index 6645a0d..1fead60 100644 --- a/src/emap/hashtable.c +++ b/src/emap/hashtable.c @@ -134,6 +134,32 @@ bool htreg_iterator_exists(int64 id) return i64db_exists(htreg->iterators, id); } +const char* htreg_iterator_firstkey(int64 id) +{ + struct DBIterator *it = i64db_get(htreg->iterators, id); + if (it) + { + union DBKey key; + it->first(it, &key); + if (dbi_exists(it)) + return key.str; + } + return NULL; +} + +const char* htreg_iterator_lastkey(int64 id) +{ + struct DBIterator *it = i64db_get(htreg->iterators, id); + if (it) + { + union DBKey key; + it->last(it, &key); + if (dbi_exists(it)) + return key.str; + } + return NULL; +} + const char* htreg_iterator_nextkey(int64 id) { struct DBIterator *it = i64db_get(htreg->iterators, id); @@ -147,6 +173,19 @@ const char* htreg_iterator_nextkey(int64 id) return NULL; } +const char* htreg_iterator_prevkey(int64 id) +{ + struct DBIterator *it = i64db_get(htreg->iterators, id); + if (it) + { + union DBKey key; + it->prev(it, &key); + if (dbi_exists(it)) + return key.str; + } + return NULL; +} + /** * Initializer. */ @@ -212,5 +251,8 @@ void htreg_defaults(void) htreg->destroy_iterator = htreg_destroy_iterator; htreg->iterator_check = htreg_iterator_check; htreg->iterator_exists = htreg_iterator_exists; + htreg->iterator_firstkey = htreg_iterator_firstkey; + htreg->iterator_lastkey = htreg_iterator_lastkey; htreg->iterator_nextkey = htreg_iterator_nextkey; + htreg->iterator_prevkey = htreg_iterator_prevkey; } diff --git a/src/emap/hashtable.h b/src/emap/hashtable.h index 44712cd..403304e 100644 --- a/src/emap/hashtable.h +++ b/src/emap/hashtable.h @@ -31,7 +31,10 @@ struct htreg_interface bool (*destroy_iterator) (int64 id); bool (*iterator_check) (int64 id); bool (*iterator_exists) (int64 id); + const char* (*iterator_firstkey) (int64 id); + const char* (*iterator_lastkey) (int64 id); const char* (*iterator_nextkey) (int64 id); + const char* (*iterator_prevkey) (int64 id); }; void htreg_defaults(void); diff --git a/src/emap/init.c b/src/emap/init.c index fefbc6c..9063735 100644 --- a/src/emap/init.c +++ b/src/emap/init.c @@ -183,7 +183,10 @@ HPExport void plugin_init (void) addScriptCommand("htsize", "i", htSize); addScriptCommand("htexists", "i", htExists); addScriptCommand("htiterator", "i", htIterator); + addScriptCommand("htifirstkey", "i", htiFirstKey); + addScriptCommand("htilastkey", "i", htiLastKey); addScriptCommand("htinextkey", "i", htiNextKey); + addScriptCommand("htiprevkey", "i", htiPrevKey); addScriptCommand("hticheck", "i", htiCheck); addScriptCommand("htidelete", "i", htiDelete); addScriptCommand("setfakecells", "iii??", setFakeCells); diff --git a/src/emap/script_buildins.c b/src/emap/script_buildins.c index 703307a..78fb940 100644 --- a/src/emap/script_buildins.c +++ b/src/emap/script_buildins.c @@ -1991,6 +1991,32 @@ BUILDIN(htIterator) return false; \ } +BUILDIN(htiFirstKey) +{ + int64 id = script_getnum(st, 2); + checkHtIteratorExists(id); + + const char * key = htreg->iterator_firstkey(id); + if (key) + script_pushstrcopy(st, key); + else + script_pushstrcopy(st, ""); + return true; +} + +BUILDIN(htiLastKey) +{ + int64 id = script_getnum(st, 2); + checkHtIteratorExists(id); + + const char * key = htreg->iterator_lastkey(id); + if (key) + script_pushstrcopy(st, key); + else + script_pushstrcopy(st, ""); + return true; +} + BUILDIN(htiNextKey) { int64 id = script_getnum(st, 2); @@ -2004,6 +2030,19 @@ BUILDIN(htiNextKey) return true; } +BUILDIN(htiPrevKey) +{ + int64 id = script_getnum(st, 2); + checkHtIteratorExists(id); + + const char * key = htreg->iterator_prevkey(id); + if (key) + script_pushstrcopy(st, key); + else + script_pushstrcopy(st, ""); + return true; +} + BUILDIN(htiCheck) { int64 id = script_getnum(st, 2); diff --git a/src/emap/script_buildins.h b/src/emap/script_buildins.h index 2066432..a72a035 100644 --- a/src/emap/script_buildins.h +++ b/src/emap/script_buildins.h @@ -82,7 +82,10 @@ BUILDIN(htDelete); BUILDIN(htSize); BUILDIN(htExists); BUILDIN(htIterator); +BUILDIN(htiFirstKey); +BUILDIN(htiLastKey); BUILDIN(htiNextKey); +BUILDIN(htiPrevKey); BUILDIN(htiCheck); BUILDIN(htiDelete); BUILDIN(setFakeCells); -- cgit v1.2.3-70-g09d2