xo-interpreter2 stack: + dict.make() + Dictionary impl

This commit is contained in:
Roland Conybeare 2026-03-03 16:45:54 +11:00
commit 7871c7dbea
16 changed files with 904 additions and 23 deletions

View file

@ -90,6 +90,7 @@ namespace xo {
obj<AGCObject> at(size_type index) const;
const obj<AGCObject> & operator[](size_type index) const noexcept { return elts_[index]; }
// TODO: nuke this or provide LValue shim. need write barrier!
obj<AGCObject> & operator[](size_type index) noexcept { return elts_[index]; }
///@}
@ -99,10 +100,17 @@ namespace xo {
///@}
/** @defgroup darray-assign assignment **/
///@{
/** store @p elt at position @p index.
* true on success, false otherwise
**/
bool assign_at(size_type index, obj<AGCObject> elt) noexcept;
/** append @p elt at the end of array.
* true on success, false otherwise
**/
bool push_back(obj<AGCObject> elt) noexcept;
///@}
/** @defgroup darray-general general methods **/
///@{

View file

@ -0,0 +1,217 @@
/** @file DDictionary.hpp
*
* @author Roland Conybeare, Jan 2026
**/
#pragma once
#include "DArray.hpp"
#include "DString.hpp"
#include <xo/gc/GCObject.hpp>
#include <xo/gc/Collector.hpp>
#include <xo/alloc2/Allocator.hpp>
#include <xo/facet/obj.hpp>
#include <xo/indentlog/print/ppindentinfo.hpp>
#include <concepts>
#include <cstdint>
namespace xo {
namespace scm {
/** @class DStruct
* @brief Polymorphic in-memory key-value store with gc hooks
*
* Small dictionary implementation for Schematika.
* O(n) lookup, at least for now. Not typed. Keys are strings,
* so functionally equivalent to python dictionaries.
**/
class DDictionary {
public:
/** @defgroup ddictionary-types type traits **/
///@{
/** type for array size **/
using size_type = std::uint32_t;
/** xo allocator facet **/
using AAllocator = xo::mm::AAllocator;
/** garbage collector facet **/
using ACollector = xo::mm::ACollector;
/** gc-aware object facet **/
using AGCObject = xo::mm::AGCObject;
/** pretty-printer state for APrintable **/
using ppindentinfo = xo::print::ppindentinfo;
/** canonical type representing a key-value pair **/
using pair_type = std::pair<const DString *, obj<AGCObject>>;
/** shim to represent result of expression like @c dict[key]
**/
template <typename DictPtr>
class LValue {
public:
LValue(DictPtr d, const DString * key) : dict_{d}, key_{key} {}
bool is_sentinel() const noexcept { return dict_ == nullptr; }
bool is_valid() const noexcept { return dict_ != nullptr; }
obj<AGCObject> & operator=(obj<AGCObject> & rvalue) {
return dict_->upsert(key_, rvalue);
}
operator std::optional<obj<AGCObject>>() const noexcept {
return dict_->lookup(key_);
}
private:
/** sentinel LValue represented by null ptr here **/
DictPtr dict_ = nullptr;
/** sentinel LValue has null ptr here **/
const DString * key_ = nullptr;
};
///@}
/** @defgroup ddictionary-ctors constructors **/
///@{
/** default ctor. zero capacity sentinel **/
DDictionary(DArray * keys, DArray * values);
/** create empty array with space for @p cap elements
* using memory from allocator @p mm.
* Nullptr if space exhausted
**/
static DDictionary * empty(obj<AAllocator> mm,
size_type cap);
/** create copy of @p src using memory from @p mm
* with capacity for @p new_cap elements
**/
static DDictionary * copy(obj<AAllocator> mm,
DDictionary * src,
size_type new_cap);
/** create dictionary containing elements @p kv_pairs, using memory from @p mm.
* Nullptr if space exhausted.
*
* Use:
* auto kv1 = std::make_pair<const DString *, obj<AGCObject>>(...);
* auto kv2 = std::make_pair<const DString *, obj<AGCObject>>(...);
* Ddictionary * v = DDictionary::make(mm, kv1, kv2, ..);
**/
template <typename... Args>
requires (std::same_as<Args, std::pair<const DString*, obj<AGCObject>>> && ...)
static DDictionary * make(obj<AAllocator> mm, Args... args);
///@}
/** @defgroup ddictionary-access acecss methods **/
///@{
/** true iff array is empty **/
bool is_empty() const noexcept { return keys_->size() == 0; }
/** array capacity **/
size_type capacity() const noexcept { return keys_->capacity(); }
/** current dictionary size (number of key-value pairs) **/
size_type size() const noexcept { return keys_->size(); }
/** return element @p key-value pair at position @p index (0-based) **/
std::pair<const DString *, obj<AGCObject>> at_index(size_type index) const;
/** return @p i'th key. O(1) **/
const DString * key_at_index(size_type i) const;
/** return @p i'th value. O(1) **/
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) noexcept { return LValue<decltype(this)>(this, key); }
///@}
/** @defgroup ddictionary-iterators iterators **/
///@{
///@}
/** @defgroup ddictionary-assign assignment **/
///@{
/** update key-value pair @p kvpair in-place,
* provide key is already present.
*
* @return true if key-value pair updated; false if key not found
**/
bool try_update(const pair_type & kvpair);
/** upsert key-value pair @p kvpair into dictionary.
* If key kvpair.first not already present, add it.
* In either case replace/establish associated value with kvpair.second.
*
* False if dictionary already at capacity
**/
bool try_upsert(const pair_type & kvpair);
/** upsert key-value pair @p kvpair into dictionary.
* If at capacity, expand capacity, getting new memory from @p mm.
*
* False iff upsert failed because allocator memory exhausted
**/
bool upsert(obj<AAllocator> mm, const pair_type & kvpair);
///@}
/** @defgroup ddictionary-general general methods **/
///@{
/** reduce array capacity to current array size
*
* note: with X1Collector, capacity is reduced but memory not recycled
* until next collection
**/
void shrink_to_fit() noexcept;
///@}
/** @defgroup ddictionary-conversion-operators conversion operators **/
///@{
///@}
/** @defgroup ddictionary-sequence-methods **/
///@{
///@}
/** @defgroup ddictionary-printable-methods **/
///@{
/** pretty-printing support **/
bool pretty(const ppindentinfo & ppii) const;
///@}
/** @defgroup ddictionary-gcobject-methods **/
///@{
/** shallow memory consumption. Excludes child objects **/
AAllocator::size_type shallow_size() const noexcept;
/** return shallow copy of this array, using memory from @p mm **/
DDictionary * shallow_copy(obj<AAllocator> mm) const noexcept;
/** forward elements to @p gc to-space; replace originals with forarding pointers **/
AAllocator::size_type forward_children(obj<ACollector> gc) noexcept;
///@}
private:
/** @defgroup ddictionary-instance-variables instance variables **/
///@{
/** dictionary keys. These will be strings **/
DArray * keys_;
/** dictionary values. values_[i] associates with keys_[i] **/
DArray * values_;
///@}
};
template <typename... Args>
requires (std::same_as<Args, std::pair<const DString *, obj<DDictionary::AGCObject>>> && ...)
DDictionary *
DDictionary::make(obj<AAllocator> mm, Args... args)
{
DDictionary * result = empty(mm, sizeof...(args));
if (result) {
(result->upsert(args), ...);
}
return result;
}
} /*namespace scm*/
} /*namespace xo*/
/* end DDictionary.hpp */

View file

@ -0,0 +1,183 @@
/** @file DStruct.hpp
*
* @author Roland Conybeare, Jan 2026
**/
#pragma once
#include "DArray.hpp"
#include <xo/gc/GCObject.hpp>
#include <xo/gc/Collector.hpp>
#include <xo/alloc2/Allocator.hpp>
#include <xo/facet/obj.hpp>
#include <xo/indentlog/print/ppindentinfo.hpp>
#include <concepts>
#include <cstdint>
namespace xo {
namespace scm {
/** @class DStruct
* @brief Polymorphic in-memory key-value store with gc hooks
*
* Small dictionary implementation for Schematika.
* O(n) lookup, at least for now. Keys are unique strings.
* Intended to have key-space fixed at comptime.
* Relevant since keys are immortal DUniqueStrings.
**/
class DStruct {
public:
/** @defgroup dstruct-types type traits **/
///@{
/** type for array size **/
using size_type = std::uint32_t;
/** xo allocator facet **/
using AAllocator = xo::mm::AAllocator;
/** garbage collector facet **/
using ACollector = xo::mm::ACollector;
/** gc-aware object facet **/
using AGCObject = xo::mm::AGCObject;
/** pretty-printer state for APrintable **/
using ppindentinfo = xo::print::ppindentinfo;
///@}
/** @defgroup dstruct-ctors constructors **/
///@{
/** default ctor. zero capacity sentinel **/
DStruct() = default;
/** not simply copyable because of flexible array.
* Need allocator. See @ref clone
**/
DStruct(const DStruct &) = delete;
/** create empty array with space for @p cap elements
* using memory from allocator @p mm.
* Nullptr if space exhausted
**/
static DStruct * empty(obj<AAllocator> mm,
size_type cap);
/** create copy of @p src using memory from @p mm
* with capacity for @p new_cap elements
**/
static DStruct * copy(obj<AAllocator> mm,
DStruct * src,
size_type new_cap);
/** create array containing elements @p args, using memory from @p mm.
* Nullptr if space exhausted.
*
* Use:
* Dstruct * v = DStruct::array(mm, e1, e2, e3);
**/
template <typename... Args>
requires (std::same_as<Args, obj<AGCObject>> && ...)
static DStruct * array(obj<AAllocator> mm, Args... args);
///@}
/** @defgroup dstruct-access acecss methods **/
///@{
/** true iff array is empty **/
bool is_empty() const noexcept { return size_ == 0; }
/** only support finite arrays :-) **/
bool is_finite() const noexcept { return true; }
/** array capacity **/
size_type capacity() const noexcept { return capacity_; }
/** current array size (number of elements) **/
size_type size() const noexcept { return size_; }
/** return element @p index of this array (0-based) **/
obj<AGCObject> at(size_type index) const;
const obj<AGCObject> & operator[](size_type index) const noexcept { return elts_[index]; }
obj<AGCObject> & operator[](size_type index) noexcept { return elts_[index]; }
///@}
/** @defgroup dstruct-iterators iterators **/
///@{
///@}
/** @defgroup dstruct-assign assignment **/
///@{
/** append @p elt at the end of array.
* true on success, false otherwise
**/
bool push_back(obj<AGCObject> elt) noexcept;
///@}
/** @defgroup dstruct-general general methods **/
///@{
/** resize to @p new_size. @p new_size may not be larger than capacity
* Return true if resize was accomplished; false otherwise.
**/
bool resize(size_type new_size) noexcept;
/** reduce array capacity to current array size
*
* note: with X1Collector, capacity is reduced but memory not recycled
* until next collection
**/
void shrink_to_fit() noexcept;
///@}
/** @defgroup dstruct-conversion-operators conversion operators **/
///@{
///@}
/** @defgroup dstruct-sequence-methods **/
///@{
///@}
/** @defgroup dstruct-printable-methods **/
///@{
/** pretty-printing support **/
bool pretty(const ppindentinfo & ppii) const;
///@}
/** @defgroup dstruct-gcobject-methods **/
///@{
/** shallow memory consumption. Excludes child objects **/
AAllocator::size_type shallow_size() const noexcept;
/** return shallow copy of this array, using memory from @p mm **/
DStruct * shallow_copy(obj<AAllocator> mm) const noexcept;
/** forward elements to @p gc to-space; replace originals with forarding pointers **/
AAllocator::size_type forward_children(obj<ACollector> gc) noexcept;
///@}
private:
/** @defgroup dstruct-instance-variables instance variables **/
///@{
/** extent of @ref elts_ array **/
size_type capacity_ = 0;
/** array size
* Invariant: size_ <= capacity_
**/
size_type size_ = 0;
/** struct keys. These will be unique strings **/
DArray * keys_ = nullptr;
/** struct member values **/
DArray * values_ = nullptr;
///@}
};
template <typename... Args>
requires (std::same_as<Args, obj<DStruct::AGCObject>> && ...)
DStruct *
DStruct::array(obj<AAllocator> mm, Args... args)
{
DStruct * result = empty(mm, sizeof...(args));
if (result) {
(result->push_back(args), ...);
}
return result;
}
} /*namespace scm*/
} /*namespace xo*/
/* end DStruct.hpp */

View file

@ -0,0 +1,12 @@
/** @file Dictionary.hpp
*
* @author Roland Conybeare, Mar 2026
**/
#pragma once
#include "DDictionary.hpp"
#include "dictionary/IGCObject_DDictionary.hpp"
#include "dictionary/IPrintable_DDictionary.hpp"
/* end Dictionary.hpp */

View file

@ -0,0 +1,67 @@
/** @file IGCObject_DDictionary.hpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [xo-facet/codegen/genfacet]
* arguments:
* --input [idl/IGCObject_DDictionary.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_repr.hpp.j2]
* 3. idl for facet methods
* [idl/IGCObject_DDictionary.json5]
**/
#pragma once
#include "GCObject.hpp"
#include <xo/gc/GCObject.hpp>
#include <xo/alloc2/Allocator.hpp>
#include "DDictionary.hpp"
namespace xo { namespace scm { class IGCObject_DDictionary; } }
namespace xo {
namespace facet {
template <>
struct FacetImplementation<xo::mm::AGCObject,
xo::scm::DDictionary>
{
using ImplType = xo::mm::IGCObject_Xfer
<xo::scm::DDictionary,
xo::scm::IGCObject_DDictionary>;
};
}
}
namespace xo {
namespace scm {
/** @class IGCObject_DDictionary
**/
class IGCObject_DDictionary {
public:
/** @defgroup scm-gcobject-ddictionary-type-traits **/
///@{
using size_type = xo::mm::AGCObject::size_type;
using AAllocator = xo::mm::AGCObject::AAllocator;
using ACollector = xo::mm::AGCObject::ACollector;
using Copaque = xo::mm::AGCObject::Copaque;
using Opaque = xo::mm::AGCObject::Opaque;
///@}
/** @defgroup scm-gcobject-ddictionary-methods **/
///@{
// const methods
/** memory consumption for this instance **/
static size_type shallow_size(const DDictionary & self) noexcept;
/** copy instance using allocator **/
static Opaque shallow_copy(const DDictionary & self, obj<AAllocator> mm) noexcept;
// non-const methods
/** during GC: forward immdiate children **/
static size_type forward_children(DDictionary & self, obj<ACollector> gc) noexcept;
///@}
};
} /*namespace scm*/
} /*namespace xo*/
/* end */

View file

@ -0,0 +1,62 @@
/** @file IPrintable_DDictionary.hpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [xo-facet/codegen/genfacet]
* arguments:
* --input [idl/IPrintable_DDictionary.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_repr.hpp.j2]
* 3. idl for facet methods
* [idl/IPrintable_DDictionary.json5]
**/
#pragma once
#include "Printable.hpp"
#include <xo/printable2/Printable.hpp>
#include <xo/printable2/detail/IPrintable_Xfer.hpp>
#include "DDictionary.hpp"
namespace xo { namespace scm { class IPrintable_DDictionary; } }
namespace xo {
namespace facet {
template <>
struct FacetImplementation<xo::print::APrintable,
xo::scm::DDictionary>
{
using ImplType = xo::print::IPrintable_Xfer
<xo::scm::DDictionary,
xo::scm::IPrintable_DDictionary>;
};
}
}
namespace xo {
namespace scm {
/** @class IPrintable_DDictionary
**/
class IPrintable_DDictionary {
public:
/** @defgroup scm-printable-ddictionary-type-traits **/
///@{
using ppindentinfo = xo::print::APrintable::ppindentinfo;
using Copaque = xo::print::APrintable::Copaque;
using Opaque = xo::print::APrintable::Opaque;
///@}
/** @defgroup scm-printable-ddictionary-methods **/
///@{
// const methods
/** Pretty-printing support for this object.
See [xo-indentlog/xo/indentlog/pretty.hpp] **/
static bool pretty(const DDictionary & self, const ppindentinfo & ppii);
// non-const methods
///@}
};
} /*namespace scm*/
} /*namespace xo*/
/* end */