Search in sources :

Example 1 with ArrayKeyMemory

use of php.runtime.memory.helper.ArrayKeyMemory 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)

Aggregations

ForeachIterator (php.runtime.lang.ForeachIterator)1 IObject (php.runtime.lang.IObject)1 ArrayKeyMemory (php.runtime.memory.helper.ArrayKeyMemory)1 ArrayValueMemory (php.runtime.memory.helper.ArrayValueMemory)1