Skip to content

Commit

Permalink
add support for SLICE expression
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@25429 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
rfranke committed Apr 7, 2015
1 parent 3abb26c commit 504a45c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
4 changes: 3 additions & 1 deletion Compiler/Template/CodegenCpp.tpl
Expand Up @@ -12086,7 +12086,9 @@ template daeExpCrefIndexSpec(list<Subscript> subs, Context context,
let &preExp += '<%tmp_slice%>.push_back(Slice());<%\n%>'
''
case SLICE(__) then
error(sourceInfo(), "Unknown slice expression " + printExpStr(exp))
let expPart = daeExp(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
let &preExp += '<%tmp_slice%>.push_back(Slice(<%expPart%>));<%\n%>'
''
;separator="\n ")
<<<%tmp_slice%>>>
end daeExpCrefIndexSpec;
Expand Down
38 changes: 25 additions & 13 deletions SimulationRuntime/cpp/Include/Core/Math/ArraySlice.h
Expand Up @@ -40,43 +40,55 @@ class Slice {
public:
// all indices
Slice() {
this->start = 1;
this->step = 1;
this->stop = 0;
start = 1;
step = 1;
stop = 0;
indices = NULL;
nindices = 0;
}

// one index
Slice(int index) {
this->start = index;
this->step = 1;
this->stop = index;
start = index;
step = 1;
stop = index;
indices = NULL;
nindices = 0;
}

Slice(int start, int stop) {
this->start = start;
this->step = 1;
step = 1;
this->stop = stop;
indices = NULL;
nindices = 0;
}

Slice(int start, int step, int stop) {
this->start = start;
this->step = step;
this->stop = stop;
indices = NULL;
nindices = 0;
}

Slice(vector<int> &ivec) {
Slice(BaseArray<int> &ivec) {
start = 0;
step = 0;
stop = 0;
vector<int>::const_iterator it;
for (it = ivec.begin(); it != ivec.end(); it++)
indices.push_back(*it);
if (ivec.getNumDims() != 1)
throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION,
"Slice requires an index vector");
// make shallow copy as ivec should live long enough in a Modelica model
indices = ivec.getData();
nindices = ivec.getNumElems();
}

size_t start;
size_t step;
size_t stop;
vector<size_t> indices;
const int *indices;
int nindices;
};

// Multi-dimensional array slice holding a reference to a BaseArray.
Expand All @@ -98,7 +110,7 @@ class ArraySlice : public BaseArray<T> {
size_t dim;
for (dim = 1, sit = slice.begin(); sit != slice.end(); dim++, sit++) {
if (sit->step == 0)
_idxs[dim - 1] = sit->indices;
_idxs[dim - 1].assign(sit->indices, sit->indices + sit->nindices);
else {
size_t maxIndex = baseArray.getDims()[dim - 1];
size_t start = sit->start > 0? sit->start: maxIndex;
Expand Down

0 comments on commit 504a45c

Please sign in to comment.