Skip to content

Commit 276ff9b

Browse files
authored
Improved iterators
1 parent f5438bd commit 276ff9b

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

libraries/OclIterator.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,17 @@ OclIterator<T>* newOclIterator_Function(function<T (int)> f)
191191
return res;
192192
}
193193

194+
template<class T>
195+
OclIterator<T> OclIterator<T>::trySplit()
196+
{
197+
vector<T>* firstpart = UmlRsdsLib<T>::subrange(elements, 1, position - 1);
198+
elements = UmlRsdsLib<T>::subrange(elements, position, elements->size());
199+
position = 0;
200+
markedPosition = 0;
201+
return OclIterator.newOclIterator_Sequence(firstpart);
202+
}
203+
204+
194205
template<class T>
195206
T OclIterator<T>::getCurrent()
196207
{
@@ -251,6 +262,29 @@ T OclIterator<T>::next()
251262
return ocliteratorx->getCurrent();
252263
}
253264

265+
template<class T>
266+
bool OclIterator<T>::tryAdvance(function<void*(void*)> f)
267+
{
268+
if (this->position + 1 <= this->elements->size())
269+
{
270+
void* x = this->next();
271+
f(x);
272+
return true;
273+
}
274+
return false;
275+
}
276+
277+
278+
template<class T>
279+
void OclIterator<T>::forEachRemaining(function<void*(void*)> f)
280+
{
281+
vector<T>* remainingElements = UmlRsdsLib<T>::subrange(elements, position, elements->size());
282+
for (int i = 0; i < remainingElements->size(); i++)
283+
{
284+
T x = remainingElements->at(i);
285+
f(x);
286+
}
287+
}
254288

255289
template<class T>
256290
T OclIterator<T>::previous()

libraries/OclIterator.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ class OclIterator
116116

117117
static OclIterator* newOclIterator_Function(function<T(int)> f);
118118

119+
OclIterator<T> trySplit();
120+
119121
T getCurrent();
120122

121123
void set(T x);
@@ -128,6 +130,10 @@ class OclIterator
128130

129131
OclIteratorResult<T> nextResult();
130132

133+
bool tryAdvance(function<void* (void*)> f);
134+
135+
void forEachRemaining(function<void* (void*)> f);
136+
131137
T previous();
132138

133139
T at(int i);

libraries/matrixlib.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import numpy as np
2+
3+
4+
class MatrixLib :
5+
6+
def rowMult(s, m) :
7+
result = []
8+
for i in range(0, len(s)) :
9+
sum = 0
10+
for k in range(0, len(m)) :
11+
sum = sum + s[k]*(m[k][i])
12+
result.append(sum)
13+
return result
14+
15+
def matrixMultiplication(m1, m2) :
16+
m1array = np.array(m1)
17+
m2array = np.array(m2)
18+
res = np.matmul(m1array, m2array)
19+
return res.tolist()
20+
21+
22+
print(MatrixLib.matrixMultiplication([[1,2], [3,4]], [[5,6], [7,8]]))

0 commit comments

Comments
 (0)