use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class ArrayMemory method toBooleanMap.
public Map<String, Boolean> toBooleanMap() {
Map<String, Boolean> r = new LinkedHashMap<String, Boolean>();
ForeachIterator iterator = foreachIterator(false, false);
while (iterator.next()) {
r.put(iterator.getKey().toString(), iterator.getValue().toBoolean());
}
return r;
}
use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class ArrayMemory method toLongMap.
public Map<String, Long> toLongMap() {
Map<String, Long> r = new LinkedHashMap<String, Long>();
ForeachIterator iterator = foreachIterator(false, false);
while (iterator.next()) {
r.put(iterator.getKey().toString(), iterator.getValue().toLong());
}
return r;
}
use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class ArrayMemory method unshift.
public void unshift(Memory... values) {
checkCopied();
if (values == null) {
throw new NullPointerException();
}
if (size == 0) {
for (Memory value : values) add(value);
} else {
if (list != null) {
if (values.length > 1) {
List<ReferenceMemory> tmp = new ArrayList<ReferenceMemory>();
for (Memory value : values) tmp.add(new ReferenceMemory(value));
list.addAll(0, tmp);
size = list.size();
} else if (values.length == 1) {
list.add(0, new ReferenceMemory(values[0]));
size = list.size();
}
} else {
ArrayMemory tmp = new ArrayMemory();
tmp.convertToMap();
for (Memory el : values) tmp.add(el);
ForeachIterator iterator = getNewIterator(null, false, false);
while (iterator.next()) {
Object key = iterator.getKey();
if (key instanceof String)
tmp.put(key, iterator.getValue());
else
add(iterator.getValue());
}
lastLongIndex = tmp.lastLongIndex;
map = tmp.map;
size = tmp.size;
}
}
}
use of php.runtime.lang.ForeachIterator 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 && list != null)
ArrayMemory.this.convertToMap();
if (list == 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.size();
}
}
@Override
protected boolean init() {
if (getKeyReferences && list != null)
ArrayMemory.this.convertToMap();
if (list == null) {
if (withPrevious || getKeyReferences) {
keys = new ArrayList<Object>(map.keySet()).listIterator();
} else {
keys = new ArrayList<Object>(map.keySet()).iterator();
}
} else {
listMax = list.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.value;
if (getKeyReferences) {
currentKeyMemory = new ArrayKeyMemory(ArrayMemory.this, getMemoryKey());
}
}
@Override
public boolean end() {
if (ArrayMemory.this.size == 0)
return false;
if (ArrayMemory.this.list != null) {
cursor = ArrayMemory.this.size - 1;
currentKey = (long) cursor;
setCurrentValue(list.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.list != null) {
if (cursor <= 0) {
currentKey = null;
currentValue = null;
cursor--;
keys = null;
return false;
} else {
cursor--;
currentKey = LongMemory.valueOf((long) cursor);
setCurrentValue(list.get(cursor));
return true;
}
} else {
ListIterator<Object> keyIterator = (ListIterator) keys;
if (keyIterator.hasPrevious()) {
currentKey = keyIterator.previous();
setCurrentValue(map.get(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.list != null) {
if (((cursor >= listMax && freeze) || (cursor >= size && !freeze)) || size < listMax) {
currentKey = null;
currentValue = null;
return false;
}
currentKey = LongMemory.valueOf((long) cursor);
setCurrentValue(list.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.get(currentKey));
return true;
} else {
currentKey = null;
currentValue = null;
return false;
}
}
}
};
}
use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class ArrayMemory method plus.
@Override
public Memory plus(Memory memory) {
switch(memory.type) {
case ARRAY:
ArrayMemory left = (ArrayMemory) toImmutable();
ArrayMemory other = (ArrayMemory) memory;
ForeachIterator iterator = other.foreachIterator(false, false);
while (iterator.next()) {
Object key = iterator.getKey();
Memory origin = getByScalar(key);
if (origin == null) {
left.checkCopied();
left.put(key, iterator.getValue().toImmutable());
}
}
return left;
case REFERENCE:
return plus(memory.toValue());
default:
return toNumeric().plus(memory);
}
}
Aggregations