Skip to content

Commit

Permalink
Extended optimised iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinlano authored Jan 12, 2025
1 parent 5349e50 commit dbc95c4
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 29 deletions.
68 changes: 40 additions & 28 deletions libraries/OclIteratorOptimised.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,31 +90,19 @@ public boolean isBeforeFirst()

public boolean hasPrevious()
{
boolean result = false;
if (position > 1 && position <= elements.length + 1)
{
result = true;
}
else {
result = false;
}
return result;
return (position > 1 && position <= elements.length + 1);
}


public int nextIndex()
{
int result = 0;
result = position + 1;
return result;
return position + 1;
}


public int previousIndex()
{
int result = 0;
result = position - 1;
return result;
return position - 1;
}


Expand Down Expand Up @@ -236,15 +224,20 @@ public static OclIterator newOclIterator_Function(Function<Integer,Object> f)
ot.generatorFunction = f;
ot.position = 0;
return ot;
}
} */

public OclIterator trySplit()
{ ArrayList firstpart = Ocl.subrange(elements,1,position-1);
elements = Ocl.subrange(elements, position);
{ ArrayList firstpart = new ArrayList();
for (int i = 0; i < position; i++)
{ firstpart.add(elements[i]); }
Object[] newelements = new Object[elements.length - position];
for (int i = 0; i + position < elements.length; i++)
{ newelements[i] = elements[i + position]; }
elements = newelements;
position = 0;
markedPosition = 0;
return OclIterator.newOclIterator_Sequence(firstpart);
} */
}

public Object getCurrent()
{
Expand All @@ -264,12 +257,11 @@ public void set(Object x)
/* public void insert(Object x)
{ elements.add(position-1,x); }
public void remove()
{ elements.remove(position - 1); }
{ elements.remove(position - 1); } */

public boolean tryAdvance(Function<Object,Object> f)
{ if (position + 1 <= elements.size())
{ if (position + 1 <= elements.length)
{ Object x = this.next();
f.apply(x);
return true;
Expand All @@ -278,12 +270,14 @@ public boolean tryAdvance(Function<Object,Object> f)
}

public void forEachRemaining(Function<Object,Object> f)
{ ArrayList remainingElements = Ocl.subrange(elements, position);
for (Object x : remainingElements)
{ f.apply(x); }
{ // ArrayList remainingElements = Ocl.subrange(elements, position);
for (int i = position; i < elements.length; i++)
{ Object x = elements[i];
f.apply(x);
}
}

/*
public OclIteratorResult nextResult()
{ if (generatorFunction == null)
{ Object v = next();
Expand Down Expand Up @@ -363,7 +357,7 @@ public void close()


public static void main(String[] args)
{ ArrayList lst = new ArrayList();
{ /* ArrayList lst = new ArrayList();
for (int i = 0; i < 100000; i++)
{ lst.add(i); }
OclIterator iter1 = OclIterator.newOclIterator_Sequence(lst);
Expand All @@ -377,7 +371,25 @@ public static void main(String[] args)
java.util.Date d2 = new java.util.Date();
long t2 = d2.getTime();
System.out.println(t2-t1);
System.out.println(t2-t1); */

Function<Object,Object> ff = (y)->{ System.out.println(y); return y; };

ArrayList lst = new ArrayList();
lst.add(1); lst.add(2); lst.add(3); lst.add(4); lst.add(5);
OclIterator iter1 = OclIterator.newOclIterator_Sequence(lst);
iter1.forEachRemaining(ff);
System.out.println();
// System.out.println(iter1.elements);
iter1.setPosition(3);
OclIterator iter2 = iter1.trySplit();
// System.out.println(iter1.elements);
// System.out.println(iter2.elements);

iter1.forEachRemaining(ff);
System.out.println();
iter2.forEachRemaining(ff);

}

}
Expand Down
20 changes: 19 additions & 1 deletion libraries/ocl.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def insertAtString(x,i,s) :
return x1 + s + x2

def setAtString(x,i,s) :
# i must be > 0, s is length 1
# i must be > 0, s has length 1
if i <= 0 :
return x
x1 = x[0:i-1]
Expand Down Expand Up @@ -527,6 +527,21 @@ def setSubrange(x,i,j,v) :
x1 = x[0:i-1] + str(v) + x[j:]
return x1

def listSubrange(l, i, j) :
# For lists. OCL indexing used for i, j
result = []
if j < 0 :
j = len(l) + 1 + j
# eg: -1 is the last element of l
if i < 1:
i = 1
i = i - 1
j = j - 1
for k in range(i, j+1):
result.append(l[k])
return result


def insertAt(x,i,s) :
# i must be > 0
if i <= 0 :
Expand Down Expand Up @@ -1124,5 +1139,8 @@ def values(m) :
# pp = setSubrange(ss, 3, 5, "and")
# print(pp)

# ss = [1, 4, 6, 7, 2]
# print(listSubrange(ss, 2, -1))



9 changes: 9 additions & 0 deletions libraries/oclfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@
import pickle
import socket
import tempfile
import gc

from enum import Enum


def free(x):
del x

# Garbage Collection
try:
gc.collect()
except Exception as e:
pass
else:
pass


class OclFile:
oclfile_instances = []
Expand Down

0 comments on commit dbc95c4

Please sign in to comment.