From 1138898989fa6b0153b7a00a1af3b37e07947219 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Sat, 30 Sep 2017 02:34:56 +0200 Subject: [PATCH] UI-Coordinates: implement indexed access ...under the assumption that the content is normalised, which means - leading NULL is changed to Symbol::EMPTY - missing elements in the middle are marked as "*" - trailing NULL in extension storage is handled by adjusting nominal extension size --- src/lib/path-array.hpp | 24 ++++++++++++++++++++++-- src/lib/symbol-impl.cpp | 1 + src/lib/symbol.hpp | 1 + tests/gui/interact/ui-coord-test.cpp | 5 +++-- tests/library/path-array-test.cpp | 11 +++++++---- 5 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/lib/path-array.hpp b/src/lib/path-array.hpp index 506c5fa18..ffe36cc9a 100644 --- a/src/lib/path-array.hpp +++ b/src/lib/path-array.hpp @@ -50,6 +50,7 @@ #include "lib/symbol.hpp" #include "lib/iter-adapter.hpp" #include "lib/meta/variadic-helper.hpp" +#include "lib/format-obj.hpp" #include "lib/util.hpp" //#include @@ -61,6 +62,7 @@ namespace lib { + namespace error = lumiera::error; // using std::unique_ptr; using std::forward; @@ -164,6 +166,13 @@ namespace lib { return storage_? size(unConst(this)->storage_) : 0; } + + const char* + operator[] (size_t idx) const + { + REQUIRE (storage_ and idx < size()); + return storage_[1+idx]; + } }; }//(End)Implementation helper @@ -249,7 +258,7 @@ namespace lib { bool empty() const { - UNIMPLEMENTED ("path implementation storage"); + return not elms_[0]; // normalise() ensures this holds only for empty paths } operator string() const; @@ -258,7 +267,18 @@ namespace lib { Literal operator[] (size_t idx) { - UNIMPLEMENTED ("path implementation storage"); + Lit elm{0}; + if (idx < chunk_size) + elm = elms_[idx]; + else + if (idx-chunk_size < tail_.size()) + elm = tail_[idx-chunk_size]; + + if (not elm) + throw error::Invalid ("Accessing index "+util::toString(idx) + +" on PathArray of size "+ util::toString(size()) + ,error::LUMIERA_ERROR_INDEX_BOUNDS); + return elm; } diff --git a/src/lib/symbol-impl.cpp b/src/lib/symbol-impl.cpp index c616ce7c2..47c410634 100644 --- a/src/lib/symbol-impl.cpp +++ b/src/lib/symbol-impl.cpp @@ -82,6 +82,7 @@ namespace lib { /* == predefined marker Symbols == */ + Symbol Symbol::EMPTY = ""; Symbol Symbol::BOTTOM = "⟂"; Symbol Symbol::FAILURE = "↯"; diff --git a/src/lib/symbol.hpp b/src/lib/symbol.hpp index c6abfd856..1627a3af9 100644 --- a/src/lib/symbol.hpp +++ b/src/lib/symbol.hpp @@ -93,6 +93,7 @@ namespace lib { : public Literal { public: + static Symbol EMPTY; static Symbol BOTTOM; static Symbol FAILURE; diff --git a/tests/gui/interact/ui-coord-test.cpp b/tests/gui/interact/ui-coord-test.cpp index 4f2b77345..9d0786c3a 100644 --- a/tests/gui/interact/ui-coord-test.cpp +++ b/tests/gui/interact/ui-coord-test.cpp @@ -41,6 +41,7 @@ using std::string; //using lib::idi::EntryID; //using lib::diff::GenNode; //using util::isSameObject; +using lib::Symbol; using util::isnil; using util::join; @@ -126,8 +127,8 @@ namespace test { // representation is trimmed and filled CHECK ("UI:Θ.*.*/*/Φ" == string(uic)); - CHECK (NULL == uic[UIC_WINDOW]); - CHECK (NULL == uic[UIC_PERSP]); + CHECK (Symbol::EMPTY == uic[UIC_WINDOW]); + CHECK (Symbol::EMPTY == uic[UIC_PERSP]); CHECK ("Θ" == uic[UIC_PANEL]); CHECK ("*" == uic[UIC_VIEW]); CHECK ("*" == uic[UIC_TAB]); diff --git a/tests/library/path-array-test.cpp b/tests/library/path-array-test.cpp index e2cbb3ad2..0fa3456ce 100644 --- a/tests/library/path-array-test.cpp +++ b/tests/library/path-array-test.cpp @@ -41,6 +41,7 @@ using std::string; //using lib::idi::EntryID; //using lib::diff::GenNode; //using util::isSameObject; +using lib::Symbol; using util::isnil; using util::join; @@ -111,8 +112,8 @@ namespace test { // representation is trimmed and filled CHECK ("Θ/*/*/*/Φ" == string(parr)); - CHECK (NULL == parr[0]); - CHECK (NULL == parr[1]); + CHECK (Symbol::EMPTY == parr[0]); + CHECK (Symbol::EMPTY == parr[1]); CHECK ("Θ" == parr[2]); CHECK ("*" == parr[3]); CHECK ("*" == parr[4]); @@ -158,8 +159,10 @@ namespace test { // index numbering starts at absolute root CHECK ("Ω" == *parr.begin()); - CHECK (nullptr == parr[0]); - CHECK ("Ω" == parr[15]); + CHECK (Symbol::EMPTY == parr[0]); + CHECK (Symbol::EMPTY == parr[1]); + CHECK (Symbol::EMPTY == parr[2]); + CHECK ("Ω" == parr[15]); VERIFY_ERROR (INDEX_BOUNDS, parr[16]); }