Skip to content

Commit bf3a2d1

Browse files
authored
Added Spliterator methods to OclIterator
1 parent 22f904f commit bf3a2d1

File tree

10 files changed

+148
-18
lines changed

10 files changed

+148
-18
lines changed

libraries/MathLib.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,19 @@ MathLib::setix(( MathLib::getix() * 171 ) % 30269);
4343
return result;
4444
}
4545

46+
bool MathLib::nextBoolean()
47+
{ double r = MathLib::random();
48+
if (r > 0.5)
49+
{ return true; }
50+
return false;
51+
}
4652

4753
long MathLib::combinatorial(int n,int m)
4854
{ long result = 0;
49-
if (n < m || m < 0) { return result; }
55+
56+
if (n < m || m < 0)
57+
{ return result; }
58+
5059
if (n - m < m)
5160
{ result = UmlRsdsLib<int>::prd(UmlRsdsLib<int>::integerSubrange(m + 1,n)) / UmlRsdsLib<int>::prd(UmlRsdsLib<int>::integerSubrange(1,n - m));
5261
}

libraries/MathLib.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ public static double random()
184184
return result;
185185
}
186186

187+
public static boolean nextBoolean()
188+
{ double r = MathLib.random();
189+
if (r > 0.5)
190+
{ return true; }
191+
return false;
192+
}
187193

188194
public static long combinatorial(int n, int m)
189195
{

libraries/MathLib.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class MathLib
4747

4848
static double random();
4949

50+
static bool nextBoolean();
51+
5052
static long combinatorial(int n,int m);
5153

5254
static long factorial(int x);

libraries/Ocl.class

48 Bytes
Binary file not shown.

libraries/Ocl.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,8 +1010,13 @@ public static String subrange(String s, int i, int j)
10101010

10111011
public static <T> ArrayList<T> subrange(List<T> l, int i, int j)
10121012
{ ArrayList<T> tmp = new ArrayList<T>();
1013-
if (i < 0) { i = l.size() + i; }
1014-
if (j < 0) { j = l.size() + j; }
1013+
if (i < 0)
1014+
{ i = l.size() + i; }
1015+
else if (i == 0)
1016+
{ i = 1; }
1017+
if (j < 0)
1018+
{ j = l.size() + j; }
1019+
10151020

10161021
for (int k = i-1; k < j; k++)
10171022
{ tmp.add(l.get(k)); }
@@ -1029,7 +1034,11 @@ public static String subrange(String s, int i)
10291034

10301035
public static <T> ArrayList<T> subrange(List<T> l, int i)
10311036
{ ArrayList<T> tmp = new ArrayList<T>();
1032-
if (i < 0) { i = l.size() + i; }
1037+
if (i < 0)
1038+
{ i = l.size() + i; }
1039+
else if (i == 0)
1040+
{ i = 1; }
1041+
10331042
for (int k = i-1; k < l.size(); k++)
10341043
{ tmp.add(l.get(k)); }
10351044
return tmp;

libraries/OclIterator.class

923 Bytes
Binary file not shown.

libraries/OclIterator.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,15 @@ public static OclIterator newOclIterator_Function(Func<int,object> f)
194194
return ot;
195195
}
196196

197+
public OclIterator trySplit()
198+
{
199+
ArrayList firstpart = SystemTypes.subrange(elements, 1, position - 1);
200+
elements = SystemTypes.subrange(elements, position, elements.Count);
201+
position = 0;
202+
markedPosition = 0;
203+
return OclIterator.newOclIterator_Sequence(firstpart);
204+
}
205+
197206
public object getCurrent()
198207
{
199208
object result = null;
@@ -246,6 +255,24 @@ public OclIteratorResult nextResult()
246255
return OclIteratorResult.newOclIteratorResult(res);
247256
}
248257

258+
public bool tryAdvance(Func<Object, Object> f)
259+
{
260+
if (position + 1 <= elements.Count)
261+
{
262+
Object x = this.next();
263+
f(x);
264+
return true;
265+
}
266+
return false;
267+
}
268+
269+
public void forEachRemaining(Func<Object, Object> f)
270+
{
271+
ArrayList remainingElements = SystemTypes.subrange(elements, position, elements.Count);
272+
foreach (Object x in remainingElements)
273+
{ f(x); }
274+
}
275+
249276

250277
public object next()
251278
{

libraries/OclIterator.java

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public boolean isBeforeFirst()
9696
return result;
9797
}
9898

99-
public boolean hasPrevious()
99+
public boolean hasPrevious()
100100
{
101101
boolean result = false;
102102
if (position > 1 && position <= elements.size() + 1)
@@ -110,35 +110,35 @@ public boolean hasPrevious()
110110
}
111111

112112

113-
public int nextIndex()
113+
public int nextIndex()
114114
{
115115
int result = 0;
116116
result = position + 1;
117117
return result;
118118
}
119119

120120

121-
public int previousIndex()
121+
public int previousIndex()
122122
{
123123
int result = 0;
124124
result = position - 1;
125125
return result;
126126
}
127127

128128

129-
public void moveForward()
129+
public void moveForward()
130130
{
131131
position = position + 1;
132132
}
133133

134134

135-
public void moveBackward()
135+
public void moveBackward()
136136
{
137137
position = position - 1;
138138
}
139139

140140

141-
public void moveTo( int i)
141+
public void moveTo( int i)
142142
{
143143
position = i;
144144
}
@@ -158,29 +158,29 @@ public void movePosition(int i)
158158
position = i + position;
159159
}
160160

161-
public void moveToMarkedPosition()
161+
public void moveToMarkedPosition()
162162
{
163163
position = markedPosition;
164164
}
165165

166-
public void moveToFirst()
166+
public void moveToFirst()
167167
{
168168
position = 1;
169169
}
170170

171-
public void moveToLast()
171+
public void moveToLast()
172172
{
173173
position = elements.size();
174174
}
175175

176176

177-
public void moveToStart()
177+
public void moveToStart()
178178
{
179179
position = 0;
180180
}
181181

182182

183-
public void moveToEnd()
183+
public void moveToEnd()
184184
{
185185
position = elements.size() + 1;
186186
}
@@ -230,6 +230,13 @@ public static OclIterator newOclIterator_Function(Function<Integer,Object> f)
230230
return ot;
231231
}
232232

233+
public OclIterator trySplit()
234+
{ ArrayList firstpart = Ocl.subrange(elements,1,position-1);
235+
elements = Ocl.subrange(elements, position);
236+
position = 0;
237+
markedPosition = 0;
238+
return OclIterator.newOclIterator_Sequence(firstpart);
239+
}
233240

234241
public Object getCurrent()
235242
{
@@ -240,19 +247,35 @@ public Object getCurrent()
240247
}
241248

242249

243-
public void set(Object x)
250+
public void set(Object x)
244251
{
245252
elements.set(position - 1,x);
246253
}
247254

248255

249-
public void insert(Object x)
256+
public void insert(Object x)
250257
{ elements.add(position-1,x); }
251258

252259

253-
public void remove()
260+
public void remove()
254261
{ elements.remove(position - 1); }
255262

263+
public boolean tryAdvance(Function<Object,Object> f)
264+
{ if (position + 1 <= elements.size())
265+
{ Object x = this.next();
266+
f.apply(x);
267+
return true;
268+
}
269+
return false;
270+
}
271+
272+
public void forEachRemaining(Function<Object,Object> f)
273+
{ ArrayList remainingElements = Ocl.subrange(elements, position);
274+
for (Object x : remainingElements)
275+
{ f.apply(x); }
276+
}
277+
278+
256279
public OclIteratorResult nextResult()
257280
{ if (generatorFunction == null)
258281
{ Object v = next();
@@ -331,6 +354,21 @@ public void close()
331354
elements = (new ArrayList());
332355
columnNames = new ArrayList<String>();
333356
}
357+
358+
/*
359+
public static void main(String[] args)
360+
{ ArrayList lst = new ArrayList();
361+
lst.add(1); lst.add(2); lst.add(3); lst.add(4); lst.add(5);
362+
OclIterator iter1 = OclIterator.newOclIterator_Sequence(lst);
363+
System.out.println(iter1.elements);
364+
iter1.setPosition(3);
365+
OclIterator iter2 = iter1.trySplit();
366+
System.out.println(iter1.elements);
367+
System.out.println(iter2.elements);
368+
369+
Function<Object,Object> ff = (y)->{ System.out.println(y); return y; };
370+
iter1.forEachRemaining(ff);
371+
} */
334372

335373
}
336374

libraries/ocliterator.km3

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ class OclIteratorResult
102102
pre: true
103103
post: OclIterator->exists( ot | ot.elements = sq & ot.position = 0 & ot.columns = Sequence{} & result = ot);
104104

105+
operation trySplit() : OclIterator
106+
pre: true
107+
post: OclIterator->exists( ot | ot.elements = elements.subrange(1,position) & ot.position = 0 & ot.columns = Sequence{} & markedPosition = 0 & elements = elements.subrange(position+1) & position = 0 & result = ot);
108+
105109
static operation newOclIterator_Set(st : Set) : OclIterator
106110
pre: true
107111
post: OclIterator->exists( ot | ot.elements = Sequence{}->union(st->sort()) & ot.position = 0 & ot.columns = Sequence{} & result = ot );
@@ -136,13 +140,31 @@ class OclIteratorResult
136140
pre: true
137141
post: elements[position] = null & elements[position]->isDeleted();
138142

143+
operation tryAdvance(f : Function) : boolean
144+
pre: true
145+
post: true
146+
activity:
147+
if position + 1 <= elements->size()
148+
then
149+
(var x : OclAny := next() ;
150+
execute f->apply(x);
151+
return true)
152+
else return false;
139153

140154
operation next() : OclAny
141155
pre: true
142156
post: true
143157
activity:
144158
( moveForward() ; return getCurrent() );
145159

160+
operation forEachRemaining(f : Function) : void
161+
pre: true
162+
post: true
163+
activity:
164+
for x : elements.subrange(position)
165+
do
166+
execute f->apply(x);
167+
146168
operation nextResult() : OclIteratorResult
147169
pre: true
148170
post: true

libraries/ocliterator.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,13 @@ def newOclIterator_Function(f) :
186186
result = ot
187187
return result
188188

189+
def trySplit(self) :
190+
firstpart = self.elements[0:self.position]
191+
self.elements = self.elements[self.position:]
192+
self.position = 0
193+
self.markedPosition = 0
194+
return OclIterator.newOclIterator_Sequence(firstpart)
195+
189196
def getCurrent(self) :
190197
result = None
191198
if self.position <= len(self.elements) and self.position >= 1 :
@@ -202,6 +209,13 @@ def remove(self) :
202209
self.elements[self.position -1] = None
203210
del self.elements[self.position -1]
204211

212+
def tryAdvance(self, f) :
213+
if self.position + 1 <= len(self.elements) :
214+
x = self.next()
215+
f(x)
216+
return True
217+
return False
218+
205219
def next(self) :
206220
result = None
207221
self.moveForward()
@@ -222,6 +236,9 @@ def nextResult(self) :
222236
self.elements.append(r)
223237
return OclIteratorResult.newOclIteratorResult(r)
224238

239+
def forEachRemaining(self, f) :
240+
for x in self.elements[self.position:] :
241+
f(x)
225242

226243
def previous(self) :
227244
result = None

0 commit comments

Comments
 (0)