From 9dbcd6a5c8a9cb5c29867dd450b2af8934616f56 Mon Sep 17 00:00:00 2001 From: ailurux Date: Thu, 30 May 2024 13:49:56 +1000 Subject: [PATCH] Fix prev/next behaviour on database iterator Fixes issue with apparent duplicated tracks in the infinite list --- src/tangara/database/database.cpp | 9 ++++++--- src/tangara/lua/lua_database.cpp | 4 ++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/tangara/database/database.cpp b/src/tangara/database/database.cpp index 4064c3ed..f7cba610 100644 --- a/src/tangara/database/database.cpp +++ b/src/tangara/database/database.cpp @@ -704,9 +704,8 @@ Iterator::Iterator(std::shared_ptr db, const IndexKey::Header& header) key_ = { .prefix = {prefix.data(), prefix.size(), &memory::kSpiRamResource}, .key = {}, - .offset = 0, + .offset = -1, }; - iterate(key_); } auto Iterator::value() const -> const std::optional& { @@ -715,7 +714,11 @@ auto Iterator::value() const -> const std::optional& { auto Iterator::next() -> void { SearchKey new_key = key_; - new_key.offset = 1; + if (new_key.offset == -1) { + new_key.offset = 0; + } else { + new_key.offset = 1; + } iterate(new_key); } diff --git a/src/tangara/lua/lua_database.cpp b/src/tangara/lua/lua_database.cpp index 1afb01f0..bf84a399 100644 --- a/src/tangara/lua/lua_database.cpp +++ b/src/tangara/lua/lua_database.cpp @@ -162,7 +162,7 @@ static auto push_iterator(lua_State* state, static auto db_iterate_prev(lua_State* state) -> int { database::Iterator* it = db_check_iterator(state, 1); - std::optional res = (*it)--; + std::optional res = --(*it); if (res) { push_lua_record(state, *res); @@ -175,7 +175,7 @@ static auto db_iterate_prev(lua_State* state) -> int { static auto db_iterate(lua_State* state) -> int { database::Iterator* it = db_check_iterator(state, 1); - std::optional res = (*it)++; + std::optional res = ++(*it); if (res) { push_lua_record(state, *res);