Search in sources :

Example 16 with ForeachIterator

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;
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator)

Example 17 with ForeachIterator

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;
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator)

Example 18 with ForeachIterator

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;
        }
    }
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) ShortcutMemory(php.runtime.memory.helper.ShortcutMemory) Memory(php.runtime.Memory) ArrayKeyMemory(php.runtime.memory.helper.ArrayKeyMemory) ArrayValueMemory(php.runtime.memory.helper.ArrayValueMemory) IObject(php.runtime.lang.IObject)

Example 19 with ForeachIterator

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;
                }
            }
        }
    };
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) ArrayValueMemory(php.runtime.memory.helper.ArrayValueMemory) ForeachIterator(php.runtime.lang.ForeachIterator) ArrayKeyMemory(php.runtime.memory.helper.ArrayKeyMemory) IObject(php.runtime.lang.IObject)

Example 20 with ForeachIterator

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);
    }
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) ShortcutMemory(php.runtime.memory.helper.ShortcutMemory) Memory(php.runtime.Memory) ArrayKeyMemory(php.runtime.memory.helper.ArrayKeyMemory) ArrayValueMemory(php.runtime.memory.helper.ArrayValueMemory) IObject(php.runtime.lang.IObject)

Aggregations

ForeachIterator (php.runtime.lang.ForeachIterator)100 ArrayMemory (php.runtime.memory.ArrayMemory)49 Memory (php.runtime.Memory)47 LongMemory (php.runtime.memory.LongMemory)22 KeyValueMemory (php.runtime.memory.KeyValueMemory)18 ReferenceMemory (php.runtime.memory.ReferenceMemory)16 Invoker (php.runtime.invoke.Invoker)12 ObjectMemory (php.runtime.memory.ObjectMemory)12 IObject (php.runtime.lang.IObject)9 ArrayKeyMemory (php.runtime.memory.helper.ArrayKeyMemory)6 ArrayValueMemory (php.runtime.memory.helper.ArrayValueMemory)6 ShortcutMemory (php.runtime.memory.helper.ShortcutMemory)5 Signature (php.runtime.annotation.Reflection.Signature)4 StringMemory (php.runtime.memory.StringMemory)4 File (java.io.File)3 HashSet (java.util.HashSet)3 Element (org.w3c.dom.Element)3 FastMethod (php.runtime.annotation.Runtime.FastMethod)3 ClassEntity (php.runtime.reflection.ClassEntity)3 PropertyEntity (php.runtime.reflection.PropertyEntity)3