x-procedure2: + dict_lookup() primitive

This commit is contained in:
Roland Conybeare 2026-03-16 00:57:17 -05:00
commit bd811111a1
2 changed files with 19 additions and 0 deletions

View file

@ -110,6 +110,9 @@ namespace xo {
/** current dictionary size (number of key-value pairs) **/ /** current dictionary size (number of key-value pairs) **/
size_type size() const noexcept { return keys_->size(); } size_type size() const noexcept { return keys_->size(); }
/** return value associated with @p key, if key is present **/
std::optional<obj<AGCObject>> lookup(const DString * key) const noexcept;
/** return element @p key-value pair at position @p index (0-based) **/ /** return element @p key-value pair at position @p index (0-based) **/
std::pair<const DString *, obj<AGCObject>> at_index(size_type index) const; std::pair<const DString *, obj<AGCObject>> at_index(size_type index) const;
/** return @p i'th key. O(1) **/ /** return @p i'th key. O(1) **/
@ -117,6 +120,7 @@ namespace xo {
/** return @p i'th value. O(1) **/ /** return @p i'th value. O(1) **/
obj<AGCObject> value_at_index(size_type i) const; obj<AGCObject> value_at_index(size_type i) const;
auto operator[](const DString * key) const noexcept { return LValue<decltype(this)>(this, key); } auto operator[](const DString * key) const noexcept { return LValue<decltype(this)>(this, key); }
auto operator[](const DString * key) noexcept { return LValue<decltype(this)>(this, key); } auto operator[](const DString * key) noexcept { return LValue<decltype(this)>(this, key); }

View file

@ -35,6 +35,21 @@ namespace xo {
return new (mem) DDictionary(keys, values); return new (mem) DDictionary(keys, values);
} }
std::optional<obj<AGCObject>>
DDictionary::lookup(const DString * key) const noexcept
{
for (DArray::size_type i = 0, z = keys_->size(); i < z; ++i) {
auto i_key = obj<AGCObject,DString>::from(keys_->at(i));
assert(i_key);
if (DString::compare(*key, *i_key.data()) == 0)
return values_->at(i);
}
return {};
}
std::pair<const DString *, obj<AGCObject>> std::pair<const DString *, obj<AGCObject>>
DDictionary::at_index(size_type ix) const DDictionary::at_index(size_type ix) const
{ {