use of php.runtime.memory.helper.ArrayValueMemory in project jphp by jphp-compiler.
the class ArrayMemory method foreachIterator.
public ForeachIterator foreachIterator(boolean getReferences, boolean getKeyReferences, boolean withPrevious, final boolean freeze) {
return new ForeachIterator(getReferences, getKeyReferences, withPrevious) {
protected int cursor = 0;
protected int listMax;
protected Iterator<Object> keys;
@Override
public void reset() {
if (getKeyReferences && map == null)
ArrayMemory.this.convertToMap();
if (map != null) {
if (withPrevious || getKeyReferences)
keys = new ArrayList<Object>(map.keySet()).listIterator();
else {
if (freeze) {
keys = new ArrayList<Object>(map.keySet()).iterator();
} else {
keys = map.keySet().iterator();
}
}
} else {
listMax = _list == null ? 0 : getList().size();
}
}
@Override
protected boolean init() {
if (getKeyReferences && map == null)
ArrayMemory.this.convertToMap();
if (map != null) {
if (withPrevious || getKeyReferences) {
keys = new ArrayList<Object>(map.keySet()).listIterator();
} else {
keys = new ArrayList<Object>(map.keySet()).iterator();
}
} else {
listMax = _list == null ? 0 : getList().size();
}
return true;
}
private void setCurrentValue(ReferenceMemory value) {
if (getReferences) {
if (plainReferences)
currentValue = value;
else
currentValue = new ArrayValueMemory(getMemoryKey(), ArrayMemory.this, value);
} else
currentValue = value.getValue();
if (getKeyReferences) {
currentKeyMemory = new ArrayKeyMemory(ArrayMemory.this, getMemoryKey());
}
}
@Override
public boolean end() {
if (ArrayMemory.this.size == 0)
return false;
if (ArrayMemory.this.map == null) {
cursor = ArrayMemory.this.size - 1;
currentKey = (long) cursor;
setCurrentValue(getList().get(cursor));
return true;
} else {
init = true;
ArrayList<Object> tmp = new ArrayList<Object>(map.keySet());
keys = tmp.listIterator(tmp.size() - 1);
if (keys.hasNext() && next()) {
return true;
} else {
return false;
}
}
}
@Override
protected boolean prevValue() {
if (ArrayMemory.this.map == null) {
if (cursor <= 0) {
currentKey = null;
currentValue = null;
cursor--;
keys = null;
return false;
} else {
cursor--;
currentKey = LongMemory.valueOf((long) cursor);
setCurrentValue(getList().get(cursor));
return true;
}
} else {
ListIterator<Object> keyIterator = (ListIterator) keys;
if (keyIterator.hasPrevious()) {
currentKey = keyIterator.previous();
setCurrentValue(map.getEntry(currentKey));
return true;
} else {
currentKey = null;
currentValue = null;
keys = null;
cursor = -1;
return false;
}
}
}
@Override
protected boolean nextValue() {
if (withPrevious && (keys == null && cursor < 0))
return false;
if (ArrayMemory.this.map == null) {
if (((cursor >= listMax && freeze) || (cursor >= size && !freeze)) || size < listMax) {
currentKey = null;
currentValue = null;
return false;
}
currentKey = LongMemory.valueOf((long) cursor);
setCurrentValue(getList().get(cursor));
cursor++;
return true;
} else {
if (keys == null) {
ArrayList<Object> tmp = new ArrayList<Object>(map.keySet());
keys = tmp.listIterator(cursor - 1);
}
if (keys.hasNext()) {
currentKey = keys.next();
setCurrentValue(map.getEntry(currentKey));
return true;
} else {
currentKey = null;
currentValue = null;
return false;
}
}
}
};
}
Aggregations