Leonardus
Loading...
Searching...
No Matches
soa.h
Go to the documentation of this file.
1
17#pragma once
18
19// Inc Library
20#include <string>
21#include <string_view>
22#include <vector>
23#include <algorithm>
24#include "determinations.h"
25
26// Inc Pure
27#include "dbc.h"
28
29// Inc Rich
30#include "so.h"
31
32
33class Context;
34
35
41class SOA : public SOcomposite {
42
48 struct SOAArray {
49 std::vector<SOp> stlvec_{};
53 for( auto * ptr : stlvec_ )
54 delete ptr;
55 }
56 };
57
58
60 std::shared_ptr<SOAArray> vec_;
61
62protected:
63
64 void * shptr() const override { return static_cast<void *>( vec_.get() ); }
65
66
67#ifndef DBC_IS_VOID
71 bool invariant() const noexcept override { /* LCOV_EXCL_START */
73 return false;
74 return std::none_of( vec_->stlvec_.begin(), vec_->stlvec_.end(),
75 []( const SO * p ) {
76 return p == nullptr;
77 } );
78 } /* LCOV_EXCL_STOP */
79#endif
80
81public:
82
87 explicit SOA( bool p_exec, size_t p_len = 0 );
88
89
91 SOA( const SOA & p_other ) : SOcomposite(p_other), vec_(p_other.vec_) {
93 }
94
95
97 ~SOA() override {
99 }
100
101
102 SOA() = delete;
103 SOA( SOA&& ) = delete;
104 SOA & operator=( const SOA & ) = delete;
105 SOA & operator=( SOA && ) = delete;
106
107public: /* accessor */
108
116 void setSO( size_t p_index, SOp p_sop ) {
117 DBC_PRE( p_index < getSize() );
118 DBC_PRE( p_sop != nullptr );
119
120 delete vec_->stlvec_[p_index];
121 vec_->stlvec_[p_index] = p_sop;
122
123 if( SOcomposite::otcodeIsComposite( p_sop->ot() ) &&
124 static_cast<SOcomposite *>(p_sop)->dagheight( 0 ) >= DET_MAXDAGHEIGHT )
125 inErrExit( semantics, "composite object DAG height exceeds maximum" );
126
127 DBC_INV;
128 }
129
130
138 SOp ncat( size_t p_pos ) const {
139 DBC_PRE( p_pos < getSize() );
140
141 return vec_->stlvec_[ p_pos ];
142 }
143
144public: /* virtual */
145
146 [[nodiscard]] SOA * dup() const override { return new SOA( *this ); }
147
148
152 [[nodiscard]] SOA * clone() const override;
153
154
155 std::string opequalequal() const override;
156
157
158 OTCode ot() const override {
159 return getExec() ? OTCode::Ax : OTCode::A;
160 }
161
162
163 std::string_view type() const override { return "arraytype"; }
164
165
168 bool equal( const SO * p_other ) const override {
169 DBC_PRE( p_other != nullptr );
170
171 if( p_other->ot() != OTCode::A && p_other->ot() != OTCode::Ax )
172 return false;
173 const auto o = static_cast<const SOA *>( p_other );
174 if( getSize() == 0 && o->getSize() == 0 )
175 return true; // Exception: arrays of size zero are always equal.
176 return vec_ == o->vec_;
177 }
178
179
180 size_t getSize() const override { return vec_->stlvec_.size(); }
181
182
183 size_t dagheight( size_t p_myheight ) const override;
184
185public: /* other */
186
188 auto begin() const { return vec_->stlvec_.begin(); }
189
190
192 auto end() const { return vec_->stlvec_.end(); }
193
194
199 void reduce();
200
201
204 void unfold2exec( Context & k ) const;
205
206
213 DBC_PRE( getSize() > 0 );
215
216 SOp retval = ncat( 0 );
217 vec_->stlvec_.erase( begin() );
218 return retval;
219 }
220
221
224 void bind( Context & k );
225
226
236 int findSubArray( const SOA * p_sub );
237
238
251 void substSubArray( size_t p_idx, size_t p_oldSize, const SOA * p_sub );
252
253};
The context of execution.
Definition context.h:46
virtual bool invariant() const noexcept
Checks the invariants of the class in which it is defined.
Definition dbc.h:40
Semantic Object Array.
Definition soa.h:41
int findSubArray(const SOA *p_sub)
Searches for p_sub within this.
Definition soa.cpp:142
SOp front_pop()
Returns a copy of the SOp at position 0 and removes this first position from the array.
Definition soa.h:212
size_t getSize() const override
Returns the size of the SO.
Definition soa.h:180
void unfold2exec(Context &k) const
Unfolds duplicates of the array-content to the execution stack.
Definition soa.cpp:89
auto end() const
Returns an iterator to the marker at end of the vector of SOps.
Definition soa.h:192
SOA * clone() const override
Creates a new instance as copy with deep cloning.
Definition soa.cpp:77
void bind(Context &k)
Replaces executable names with operator objects recursively into elements that are SOA.
Definition soa.cpp:52
~SOA() override
Dtor.
Definition soa.h:97
std::shared_ptr< SOAArray > vec_
The shared array.
Definition soa.h:60
std::string_view type() const override
Returns a type name.
Definition soa.h:163
void setSO(size_t p_index, SOp p_sop)
Setter for an array object.
Definition soa.h:116
bool equal(const SO *p_other) const override
Checks if this SO is equal to another SO.
Definition soa.h:168
OTCode ot() const override
Returns an OTCode.
Definition soa.h:158
SOp ncat(size_t p_pos) const
Returns a copy of the SOp at the given position.
Definition soa.h:138
bool invariant() const noexcept override
Checks class invariants.
Definition soa.h:71
size_t dagheight(size_t p_myheight) const override
DAG Height.
Definition soa.cpp:116
void substSubArray(size_t p_idx, size_t p_oldSize, const SOA *p_sub)
Substitute at index p_idx p_oldsize elements by p_sub.
Definition soa.cpp:166
SOA(const SOA &p_other)
Copy ctor.
Definition soa.h:91
void reduce()
Reduces the array by one SO at the end.
Definition soa.cpp:131
SOA * dup() const override
Creates a new instance as copy following the red book definition.
Definition soa.h:146
void * shptr() const override
Pointer to the shared object.
Definition soa.h:64
auto begin() const
Returns an iterator to the first SOp.
Definition soa.h:188
std::string opequalequal() const override
String representation for operators '==' and 'pstack'.
Definition soa.cpp:95
Semantic Object.
Definition so.h:67
virtual OTCode ot() const =0
Returns an OTCode.
bool getExec() const
Getter for exec_.
Definition so.h:100
Composite Semantic Object.
Definition so.h:261
virtual size_t dagheight(size_t p_myheigth) const =0
DAG Height.
static bool otcodeIsComposite(OTCode p_code)
Checks if an OTCode represents a composite type (A, Ax, D, K).
Definition so.h:290
Helpers for design by contract idioms.
#define DBC_INV_CTOR(T)
Assert for invariant checks in ctors and dtors.
Definition dbc.h:122
#define DBC_INV
Assert for invariant checks in member functions.
Definition dbc.h:114
#define DBC_PRE(XXX)
Assert for preconditions.
Definition dbc.h:104
#define DBC_INV_RAII(TT)
Defines an instance of the class DbCRAIIassert<>, which calls the invariant()-function at the return ...
Definition dbc.h:94
Determinations of the compile time constants, which we can not calculate (so far).
constexpr size_t DET_MAXDAGHEIGHT
The maximum number of nesting levels of composite semantic objects.
Definition determinations.h:33
void inErrExit(InError p_err, const std::string &p_details, const std::source_location p_location)
Write interpreter error message to interpreter out file and exit( EC_INTERPRETER / EC_CMDLINE ).
Definition error.cpp:86
@ semantics
infinite nesting of arrays; infinite loop loading values from dictionary;
Definition error.h:51
The class SO - semantic object.
OTCode
OTCode - the Object Type Code.
Definition so.h:38
@ A
SOA literal.
@ Ax
SOA executable.
@ o
SOo.
Semantic Object Array - array member class.
Definition soa.h:48
~SOAArray()
Dtor.
Definition soa.h:52
std::vector< SOp > stlvec_
The inner array.
Definition soa.h:49