xo-interpreter2 stack: refactor: string clases -> xo-stringtable2/
This commit is contained in:
parent
5fc59b0df4
commit
f60f90d8f3
20 changed files with 966 additions and 6 deletions
140
include/xo/stringtable2/DUniqueString.hpp
Normal file
140
include/xo/stringtable2/DUniqueString.hpp
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
/** @file DUniqueString.hpp
|
||||
*
|
||||
* @author Roland Conybeare, Jan 2026
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <xo/stringtable2/DString.hpp>
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
/** @class DUniqueString
|
||||
* @brief unique immutable string
|
||||
*
|
||||
* A DUniqueString is an immutable string stored in a shared StringTable.
|
||||
* Follows that DUniqueStrings at different memory locations
|
||||
* have different contents.
|
||||
*
|
||||
* DUniqueString instances will be created by StringTable (see also).
|
||||
* Application code will not allocate them directly.
|
||||
*
|
||||
* Needs to be gc-aware so that collector knows what to do when it encounters
|
||||
* a obj<AGCObject> with a DUnqiueString data pointer; such instances
|
||||
* will not be allocated from GC memory
|
||||
**/
|
||||
class DUniqueString {
|
||||
public:
|
||||
using AAllocator = xo::mm::AAllocator;
|
||||
using ACollector = xo::mm::ACollector;
|
||||
using size_type = DString::size_type;
|
||||
using ppindentinfo = xo::print::ppindentinfo;
|
||||
|
||||
/* Memory model for a DUniqueString allocated via xo allocator
|
||||
*
|
||||
* 0 8 16 20 24 24+z
|
||||
* v v v v v v
|
||||
* +---------------+-+-------------+-------+-------+-----------+
|
||||
* | header |u| padding | cap | size | text... \0|
|
||||
* +---------------+-+-------------+-------+-------+-----------+
|
||||
*
|
||||
* Legend
|
||||
* header 8 byte allocation header
|
||||
* u 1 byte DUniqueString placeholder (c++ insists)
|
||||
* padding 7 bytes allocator-imposed padding to 8-byte alignment
|
||||
* cap 4 bytes DString.capacity
|
||||
* size 4 bytes DString.size
|
||||
* text z bytes DString.size bytes of text (including null)
|
||||
* In practice followed by padding to 8 byte
|
||||
* alignment
|
||||
*/
|
||||
|
||||
/** @defgroup duniquestring-ctors constructors **/
|
||||
///@{
|
||||
|
||||
/** not copyable **/
|
||||
DUniqueString(const DUniqueString &) = delete;
|
||||
|
||||
///@}
|
||||
/** @defgroup duniquestring-methods methods **/
|
||||
///@{
|
||||
|
||||
/** Available storage for this instance.
|
||||
* For completeness' sake since uniquestring not modifiable
|
||||
**/
|
||||
size_type capacity() const noexcept { return _text()->capacity(); }
|
||||
size_type size() const noexcept { return _text()->size(); }
|
||||
const char * chars() const noexcept { return _text()->chars(); }
|
||||
|
||||
/** compare unique strings: return n with {n<0, n=0, n>0}
|
||||
* when @p lhs lexicographically {before, at, after} @p rhs
|
||||
**/
|
||||
static int compare(const DUniqueString & lhs, const DUniqueString & rhs);
|
||||
|
||||
std::size_t hash() const noexcept { return _text()->hash(); }
|
||||
operator std::string_view() const noexcept { return std::string_view(*_text()); }
|
||||
/** not assignable **/
|
||||
DUniqueString & operator=(const DUniqueString &) = delete;
|
||||
|
||||
///@}
|
||||
/** @defgroup duniquestring-printable-methods printable facet methods **/
|
||||
///@{
|
||||
|
||||
bool pretty(const ppindentinfo & ppii) const;
|
||||
|
||||
///@}
|
||||
/** @defgroup duniquestring-gcobject-methods gcobject facet methods **/
|
||||
///@{
|
||||
|
||||
std::size_t shallow_size() const noexcept;
|
||||
|
||||
/** clone unique string, using memory from allocator @p mm. **/
|
||||
DUniqueString * shallow_copy(obj<AAllocator> mm) const noexcept;
|
||||
|
||||
/** fixup child pointers (trivial for DUniqueString, no gc-owned children **/
|
||||
std::size_t forward_children(obj<ACollector> gc) noexcept;
|
||||
|
||||
///@}
|
||||
|
||||
private:
|
||||
/** @defgroup duniquestring-impl-methods implementation methods **/
|
||||
///@{
|
||||
|
||||
/** default ctor **/
|
||||
DUniqueString() = default;
|
||||
|
||||
/** DString containing actual string content immediately follows DUniqueString
|
||||
* in memory; part of same alloc
|
||||
**/
|
||||
DString * _text() const noexcept;
|
||||
|
||||
//explicit DUniqueString(const DString * text) : text_{text} {}
|
||||
|
||||
/** create instance using memory from @p mm,
|
||||
* with string contents copied from @p sv
|
||||
**/
|
||||
static DUniqueString * from_view(obj<AAllocator> mm,
|
||||
std::string_view sv);
|
||||
|
||||
///@}
|
||||
|
||||
friend class StringTable;
|
||||
};
|
||||
|
||||
/* since unique: just compare addresses */
|
||||
inline bool operator==(const DUniqueString & lhs, const DUniqueString & rhs) {
|
||||
return (&lhs == &rhs);
|
||||
}
|
||||
|
||||
/* since unique: just compare addresses **/
|
||||
inline bool operator!=(const DUniqueString & lhs, const DUniqueString & rhs) {
|
||||
return (&lhs != &rhs);
|
||||
}
|
||||
|
||||
inline bool operator<=(const DUniqueString & lhs, const DUniqueString & rhs) {
|
||||
return (DUniqueString::compare(lhs, rhs) <= 0);
|
||||
}
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end UniqueString.hpp */
|
||||
65
include/xo/stringtable2/StringTable.hpp
Normal file
65
include/xo/stringtable2/StringTable.hpp
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
/** @file StringTable.hpp
|
||||
*
|
||||
* @author Roland Conybeare, Jan 2026
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "DUniqueString.hpp"
|
||||
#include <xo/arena/DArenaHashMap.hpp>
|
||||
#include <xo/arena/DArena.hpp>
|
||||
#include <xo/arena/hashmap/verify_policy.hpp>
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
|
||||
/** @class StringTable
|
||||
* @brief table containing a set of interned strings
|
||||
*
|
||||
* A table of strings referenced in schematika expressions
|
||||
**/
|
||||
class StringTable {
|
||||
public:
|
||||
using DArena = xo::mm::DArena;
|
||||
using MemorySizeVisitor = xo::mm::MemorySizeVisitor;
|
||||
using StringMap = xo::map::DArenaHashMap<std::string_view,
|
||||
DUniqueString*>;
|
||||
using size_type = StringMap::size_type;
|
||||
|
||||
public:
|
||||
StringTable(size_type hint_max_capacity,
|
||||
bool debug_flag = false);
|
||||
|
||||
/** lookup interned string; nullptr if not present **/
|
||||
const DUniqueString * lookup(std::string_view key) const;
|
||||
|
||||
/** return unique string with contents @p key. Idempotent! **/
|
||||
const DUniqueString * intern(std::string_view key);
|
||||
|
||||
/** generate unique symbol -- guaranteed not to collide
|
||||
* with existing symbol in this table.
|
||||
**/
|
||||
const DUniqueString * gensym(std::string_view prefix);
|
||||
|
||||
/** verify StringTable invariants.
|
||||
* Act on failure according to policy @p p
|
||||
**/
|
||||
bool verify_ok(verify_policy p = verify_policy::throw_only()) const;
|
||||
|
||||
/** visit string-table memory pools, call visitor(info) for each **/
|
||||
void visit_pools(const MemorySizeVisitor & visitor) const;
|
||||
|
||||
private:
|
||||
/** allocate string storage in this arena; use DString to represent each string.
|
||||
* Can't use DArenaVector b/c DString has variable size
|
||||
**/
|
||||
DArena strings_;
|
||||
/** map_[s] points to arena strings, i.e. members of @ref strings_ **/
|
||||
StringMap map_;
|
||||
};
|
||||
|
||||
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end StringTable.hpp */
|
||||
12
include/xo/stringtable2/UniqueString.hpp
Normal file
12
include/xo/stringtable2/UniqueString.hpp
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
/** @file UniqueString.hpp
|
||||
*
|
||||
* @author Roland Conybeare, Feb 2026
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "DUniqueString.hpp"
|
||||
#include "uniquestring/IGCObject_DUniqueString.hpp"
|
||||
#include "uniquestring/IPrintable_DUniqueString.hpp"
|
||||
|
||||
/* end UniqueString.hpp */
|
||||
|
|
@ -64,4 +64,4 @@ namespace xo {
|
|||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end */
|
||||
/* end */
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
/** @file IGCObject_DUniqueString.hpp
|
||||
*
|
||||
* Generated automagically from ingredients:
|
||||
* 1. code generator:
|
||||
* [xo-facet/codegen/genfacet]
|
||||
* arguments:
|
||||
* --input [idl/IGCObject_DUniqueString.json5]
|
||||
* 2. jinja2 template for abstract facet .hpp file:
|
||||
* [iface_facet_repr.hpp.j2]
|
||||
* 3. idl for facet methods
|
||||
* [idl/IGCObject_DUniqueString.json5]
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "GCObject.hpp"
|
||||
#include <xo/alloc2/GCObject.hpp>
|
||||
#include <xo/alloc2/Allocator.hpp>
|
||||
#include "DUniqueString.hpp"
|
||||
|
||||
namespace xo { namespace scm { class IGCObject_DUniqueString; } }
|
||||
|
||||
namespace xo {
|
||||
namespace facet {
|
||||
template <>
|
||||
struct FacetImplementation<xo::mm::AGCObject,
|
||||
xo::scm::DUniqueString>
|
||||
{
|
||||
using ImplType = xo::mm::IGCObject_Xfer
|
||||
<xo::scm::DUniqueString,
|
||||
xo::scm::IGCObject_DUniqueString>;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
/** @class IGCObject_DUniqueString
|
||||
**/
|
||||
class IGCObject_DUniqueString {
|
||||
public:
|
||||
/** @defgroup scm-gcobject-duniquestring-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-duniquestring-methods **/
|
||||
///@{
|
||||
// const methods
|
||||
/** memory consumption for this instance **/
|
||||
static size_type shallow_size(const DUniqueString & self) noexcept;
|
||||
/** copy instance using allocator **/
|
||||
static Opaque shallow_copy(const DUniqueString & self, obj<AAllocator> mm) noexcept;
|
||||
|
||||
// non-const methods
|
||||
/** during GC: forward immdiate children **/
|
||||
static size_type forward_children(DUniqueString & self, obj<ACollector> gc) noexcept;
|
||||
///@}
|
||||
};
|
||||
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end */
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
/** @file IPrintable_DUniqueString.hpp
|
||||
*
|
||||
* Generated automagically from ingredients:
|
||||
* 1. code generator:
|
||||
* [xo-facet/codegen/genfacet]
|
||||
* arguments:
|
||||
* --input [idl/IPrintable_DUniqueString.json5]
|
||||
* 2. jinja2 template for abstract facet .hpp file:
|
||||
* [iface_facet_repr.hpp.j2]
|
||||
* 3. idl for facet methods
|
||||
* [idl/IPrintable_DUniqueString.json5]
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Printable.hpp"
|
||||
#include <xo/printable2/Printable.hpp>
|
||||
#include <xo/printable2/detail/IPrintable_Xfer.hpp>
|
||||
#include "DUniqueString.hpp"
|
||||
|
||||
namespace xo { namespace scm { class IPrintable_DUniqueString; } }
|
||||
|
||||
namespace xo {
|
||||
namespace facet {
|
||||
template <>
|
||||
struct FacetImplementation<xo::print::APrintable,
|
||||
xo::scm::DUniqueString>
|
||||
{
|
||||
using ImplType = xo::print::IPrintable_Xfer
|
||||
<xo::scm::DUniqueString,
|
||||
xo::scm::IPrintable_DUniqueString>;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
/** @class IPrintable_DUniqueString
|
||||
**/
|
||||
class IPrintable_DUniqueString {
|
||||
public:
|
||||
/** @defgroup scm-printable-duniquestring-type-traits **/
|
||||
///@{
|
||||
using ppindentinfo = xo::print::APrintable::ppindentinfo;
|
||||
using Copaque = xo::print::APrintable::Copaque;
|
||||
using Opaque = xo::print::APrintable::Opaque;
|
||||
///@}
|
||||
/** @defgroup scm-printable-duniquestring-methods **/
|
||||
///@{
|
||||
// const methods
|
||||
/** Pretty-printing support for this object.
|
||||
See [xo-indentlog/xo/indentlog/pretty.hpp] **/
|
||||
static bool pretty(const DUniqueString & self, const ppindentinfo & ppii);
|
||||
|
||||
// non-const methods
|
||||
///@}
|
||||
};
|
||||
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end */
|
||||
Loading…
Add table
Add a link
Reference in a new issue