diff --git a/libraries/OclIteratorOptimised.java b/libraries/OclIteratorOptimised.java index 0440ece9..b40cc871 100644 --- a/libraries/OclIteratorOptimised.java +++ b/libraries/OclIteratorOptimised.java @@ -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; } @@ -236,15 +224,20 @@ public static OclIterator newOclIterator_Function(Function 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() { @@ -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 f) - { if (position + 1 <= elements.size()) + { if (position + 1 <= elements.length) { Object x = this.next(); f.apply(x); return true; @@ -278,12 +270,14 @@ public boolean tryAdvance(Function f) } public void forEachRemaining(Function 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(); @@ -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); @@ -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 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); + } } diff --git a/libraries/ocl.py b/libraries/ocl.py index 1a395130..5a10ccc9 100644 --- a/libraries/ocl.py +++ b/libraries/ocl.py @@ -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] @@ -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 : @@ -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)) + diff --git a/libraries/oclfile.py b/libraries/oclfile.py index 21c0759f..6ea1f1b3 100644 --- a/libraries/oclfile.py +++ b/libraries/oclfile.py @@ -4,6 +4,7 @@ import pickle import socket import tempfile +import gc from enum import Enum @@ -11,6 +12,14 @@ def free(x): del x + # Garbage Collection + try: + gc.collect() + except Exception as e: + pass + else: + pass + class OclFile: oclfile_instances = []