x-procedure2: + dict_lookup() primitive
This commit is contained in:
parent
87f08a4e4d
commit
bd811111a1
2 changed files with 19 additions and 0 deletions
|
|
@ -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); }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue