From bd811111a1194696f9ebee69f4afb1acd13272eb Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Mon, 16 Mar 2026 00:57:17 -0500 Subject: [PATCH] x-procedure2: + dict_lookup() primitive --- include/xo/object2/DDictionary.hpp | 4 ++++ src/object2/DDictionary.cpp | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/include/xo/object2/DDictionary.hpp b/include/xo/object2/DDictionary.hpp index f584408..28f9651 100644 --- a/include/xo/object2/DDictionary.hpp +++ b/include/xo/object2/DDictionary.hpp @@ -110,6 +110,9 @@ namespace xo { /** current dictionary size (number of key-value pairs) **/ size_type size() const noexcept { return keys_->size(); } + /** return value associated with @p key, if key is present **/ + std::optional> lookup(const DString * key) const noexcept; + /** return element @p key-value pair at position @p index (0-based) **/ std::pair> at_index(size_type index) const; /** return @p i'th key. O(1) **/ @@ -117,6 +120,7 @@ namespace xo { /** return @p i'th value. O(1) **/ obj value_at_index(size_type i) const; + auto operator[](const DString * key) const noexcept { return LValue(this, key); } auto operator[](const DString * key) noexcept { return LValue(this, key); } diff --git a/src/object2/DDictionary.cpp b/src/object2/DDictionary.cpp index d39e669..40c85c0 100644 --- a/src/object2/DDictionary.cpp +++ b/src/object2/DDictionary.cpp @@ -35,6 +35,21 @@ namespace xo { return new (mem) DDictionary(keys, values); } + std::optional> + DDictionary::lookup(const DString * key) const noexcept + { + for (DArray::size_type i = 0, z = keys_->size(); i < z; ++i) { + auto i_key = obj::from(keys_->at(i)); + + assert(i_key); + + if (DString::compare(*key, *i_key.data()) == 0) + return values_->at(i); + } + + return {}; + } + std::pair> DDictionary::at_index(size_type ix) const {