diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-06-04 14:31:53 +0200 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-06-04 14:31:53 +0200 |
commit | 20e551590b4491dade12191caf94411e7ec67dd9 (patch) | |
tree | 7168db69a4972f0d2ac6697d619fe870f7bca4f5 /src | |
parent | 011593650caa66457694fc46352208b8607d8994 (diff) | |
download | lanes-20e551590b4491dade12191caf94411e7ec67dd9.tar.gz lanes-20e551590b4491dade12191caf94411e7ec67dd9.tar.bz2 lanes-20e551590b4491dade12191caf94411e7ec67dd9.zip |
Refactored keeper implementation of linda:get()
Diffstat (limited to 'src')
-rw-r--r-- | src/keeper.cpp | 40 |
1 files changed, 23 insertions, 17 deletions
diff --git a/src/keeper.cpp b/src/keeper.cpp index eba184f..f1079e4 100644 --- a/src/keeper.cpp +++ b/src/keeper.cpp | |||
@@ -125,15 +125,23 @@ void KeyUD::prepareAccess(KeeperState const K_) | |||
125 | 125 | ||
126 | // in: fifo | 126 | // in: fifo |
127 | // out: ...|nothing | 127 | // out: ...|nothing |
128 | // expects exactly 1 value on the stack! | 128 | // pops the fifo, push as much data as is available (up to the specified count) without consuming it |
129 | // function assumes that there is enough data in the fifo to satisfy the request | ||
130 | void KeyUD::peek(KeeperState const K_, int const count_) | 129 | void KeyUD::peek(KeeperState const K_, int const count_) |
131 | { | 130 | { |
132 | //LUA_ASSERT(K_, KeyUD::GetPtr(K_, 1) == this); | 131 | LUA_ASSERT(K_, KeyUD::GetPtr(K_, -1) == this); |
133 | STACK_GROW(K_, count_); | 132 | if (count <= 0) { // no data is available |
134 | for (int const _i : std::ranges::iota_view{ 0, count_ }) { | 133 | lua_pop(K_, 1); // K_: |
135 | lua_rawgeti(K_, 1, first + _i); | 134 | return; |
135 | } | ||
136 | |||
137 | // read <count_> value off the fifo | ||
138 | prepareAccess(K_); // K_: fifo | ||
139 | int const _at{ lua_gettop(K_) }; | ||
140 | for (int const _i : std::ranges::iota_view{ 1, std::min(count_, count) }) { // push val2 to valN | ||
141 | lua_rawgeti(K_, 1, first + _i); // K_: fifo val2..N | ||
136 | } | 142 | } |
143 | lua_rawgeti(K_, 1, first); // push val1 // K_: fifo val2..N val1 | ||
144 | lua_replace(K_, _at); // replace fifo by val1 to get the output properly ordered // K_: val1..N | ||
137 | } | 145 | } |
138 | 146 | ||
139 | // ################################################################################################# | 147 | // ################################################################################################# |
@@ -518,23 +526,21 @@ int keepercall_get(lua_State* const L_) | |||
518 | KeeperState const _K{ L_ }; | 526 | KeeperState const _K{ L_ }; |
519 | int _count{ 1 }; | 527 | int _count{ 1 }; |
520 | if (lua_gettop(_K) == 3) { // _K: linda key count | 528 | if (lua_gettop(_K) == 3) { // _K: linda key count |
521 | _count = static_cast<int>(lua_tointeger(_K, 3)); | 529 | _count = static_cast<int>(lua_tointeger(_K, 3)); // linda:get() made sure _count >= 1 |
522 | lua_pop(_K, 1); // _K: linda key | 530 | lua_pop(_K, 1); // _K: linda key |
523 | } | 531 | } |
524 | PushKeysDB(_K, 1); // _K: linda key KeysDB | 532 | PushKeysDB(_K, 1); // _K: linda key KeysDB |
525 | lua_replace(_K, 1); // _K: KeysDB key | 533 | lua_replace(_K, 1); // _K: KeysDB key |
526 | lua_rawget(_K, 1); // _K: KeysDB KeyUD | 534 | lua_rawget(_K, 1); // _K: KeysDB KeyUD |
535 | lua_remove(_K, 1); // _K: KeyUD | ||
527 | KeyUD* const _key{ KeyUD::GetPtr(_K, -1) }; | 536 | KeyUD* const _key{ KeyUD::GetPtr(_K, -1) }; |
528 | if (_key != nullptr && _key->count > 0) { | 537 | if (_key != nullptr) { |
529 | _key->prepareAccess(_K); // _K: KeysDB fifo | 538 | _key->peek(_K, _count); // _K: val... |
530 | lua_remove(_K, 1); // _K: fifo | 539 | } else { |
531 | _count = std::min(_count, _key->count); | 540 | // no fifo was ever registered for this key, or it is empty |
532 | // read <count> value off the fifo | 541 | lua_pop(_K, 1); // _K: |
533 | _key->peek(_K, _count); // _K: fifo ... | 542 | } |
534 | return _count; | 543 | return lua_gettop(_K); |
535 | } | ||
536 | // no fifo was ever registered for this key, or it is empty | ||
537 | return 0; | ||
538 | } | 544 | } |
539 | 545 | ||
540 | // ################################################################################################# | 546 | // ################################################################################################# |