Search in sources :

Example 1 with KeyValueMemory

use of php.runtime.memory.KeyValueMemory in project jphp by jphp-compiler.

the class Generator method setCurrent.

protected Bucket setCurrent(Memory value) {
    boolean returnRef = (((GeneratorEntity) getReflection()).isReturnReference());
    Bucket current = iterator.getCurrentValue();
    if (value instanceof KeyValueMemory) {
        if (current != null) {
            current.setKey(((KeyValueMemory) value).key);
            current.setValue(returnRef ? new ReferenceMemory(value) : value.toValue());
        } else {
            current = new Bucket(((KeyValueMemory) value).key, returnRef ? new ReferenceMemory(value) : value.toValue());
        }
    } else {
        if (returnRef) {
            value = new ReferenceMemory(value);
        }
        if (current != null) {
            current.pushValue(value);
        } else {
            current = new Bucket(Memory.CONST_INT_0, value);
        }
    }
    return current;
}
Also used : ReferenceMemory(php.runtime.memory.ReferenceMemory) KeyValueMemory(php.runtime.memory.KeyValueMemory)

Example 2 with KeyValueMemory

use of php.runtime.memory.KeyValueMemory in project jphp by jphp-compiler.

the class ArrayFunctions method _asort_impl.

protected static boolean _asort_impl(Environment env, TraceInfo trace, @Reference Memory array, Comparator comparator) {
    if (expecting(env, trace, 1, array, ARRAY)) {
        ArrayMemory arrayMemory = array.toValue(ArrayMemory.class);
        Memory[] values = new Memory[arrayMemory.size()];
        ForeachIterator iterator = arrayMemory.getNewIterator(env);
        int i = 0;
        while (iterator.next()) {
            values[i++] = new KeyValueMemory(iterator.getMemoryKey(), iterator.getValue());
        }
        arrayMemory.clear();
        try {
            Arrays.sort(values, comparator);
        } catch (IllegalArgumentException e) {
            return false;
        }
        for (Memory value : values) {
            arrayMemory.add(value);
        }
        return true;
    } else {
        return false;
    }
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ForeachIterator(php.runtime.lang.ForeachIterator) KeyValueMemory(php.runtime.memory.KeyValueMemory) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) ReferenceMemory(php.runtime.memory.ReferenceMemory) LongMemory(php.runtime.memory.LongMemory) KeyValueMemory(php.runtime.memory.KeyValueMemory)

Example 3 with KeyValueMemory

use of php.runtime.memory.KeyValueMemory in project jphp by jphp-compiler.

the class ItemsUtils method sortByKeys.

@Signature({ @Arg(value = "collection", type = HintType.TRAVERSABLE), @Arg(value = "comparator", type = HintType.CALLABLE, optional = @Optional("null")), @Arg(value = "saveKeys", optional = @Optional("false")) })
public static Memory sortByKeys(Environment env, Memory... args) {
    boolean saveKeys = args[2].toBoolean();
    List<KeyValueMemory> tmp = new ArrayList<KeyValueMemory>();
    ForeachIterator iterator = args[0].toImmutable().getNewIterator(env);
    while (iterator.next()) {
        tmp.add(new KeyValueMemory(iterator.getMemoryKey(), iterator.getValue().toImmutable()));
    }
    final Invoker invoker = args[0].isNull() ? null : Invoker.valueOf(env, null, args[1]);
    Collections.sort(tmp, new Comparator<KeyValueMemory>() {

        @Override
        public int compare(KeyValueMemory o1, KeyValueMemory o2) {
            if (invoker == null)
                return o1.key.compareTo(o2.key);
            else
                return invoker.callNoThrow(o1.key, o2.key).toInteger();
        }
    });
    ArrayMemory r = new ArrayMemory();
    Iterator<KeyValueMemory> iterator1 = tmp.iterator();
    while (iterator1.hasNext()) {
        if (saveKeys)
            r.add(iterator1.next());
        else
            r.add(iterator1.next().value);
        iterator1.remove();
    }
    return r.toConstant();
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ForeachIterator(php.runtime.lang.ForeachIterator) KeyValueMemory(php.runtime.memory.KeyValueMemory) Invoker(php.runtime.invoke.Invoker)

Example 4 with KeyValueMemory

use of php.runtime.memory.KeyValueMemory in project jphp by jphp-compiler.

the class ItemsUtils method sort.

@Signature({ @Arg(value = "collection", type = HintType.TRAVERSABLE), @Arg(value = "comparator", type = HintType.CALLABLE, optional = @Optional("null")), @Arg(value = "saveKeys", optional = @Optional("false")) })
public static Memory sort(Environment env, Memory... args) {
    boolean saveKeys = args[2].toBoolean();
    Memory[] sortTmp;
    if (!saveKeys && args[0].isArray()) {
        Memory[] original = args[0].toValue(ArrayMemory.class).values(true);
        sortTmp = Arrays.copyOf(original, original.length);
    } else {
        ForeachIterator iterator = args[0].toImmutable().getNewIterator(env);
        List<Memory> tmp = new ArrayList<Memory>();
        while (iterator.next()) {
            if (saveKeys)
                tmp.add(new KeyValueMemory(iterator.getMemoryKey(), iterator.getValue().toImmutable()));
            else
                tmp.add(iterator.getValue().toImmutable());
        }
        sortTmp = tmp.toArray(new Memory[tmp.size()]);
        tmp.clear();
    }
    if (args[1].isNull()) {
        Arrays.sort(sortTmp, new Comparator<Memory>() {

            @Override
            public int compare(Memory o1, Memory o2) {
                return o1.compareTo(o2);
            }
        });
    } else {
        final Invoker invoker = Invoker.valueOf(env, null, args[1]);
        Arrays.sort(sortTmp, new Comparator<Memory>() {

            @Override
            public int compare(Memory o1, Memory o2) {
                return invoker.callNoThrow(o1, o2).toInteger();
            }
        });
    }
    ArrayMemory r = new ArrayMemory();
    for (Memory el : sortTmp) {
        r.add(el);
    }
    return r.toConstant();
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ForeachIterator(php.runtime.lang.ForeachIterator) KeyValueMemory(php.runtime.memory.KeyValueMemory) Invoker(php.runtime.invoke.Invoker) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) LongMemory(php.runtime.memory.LongMemory) KeyValueMemory(php.runtime.memory.KeyValueMemory) ObjectMemory(php.runtime.memory.ObjectMemory)

Aggregations

KeyValueMemory (php.runtime.memory.KeyValueMemory)4 ForeachIterator (php.runtime.lang.ForeachIterator)3 ArrayMemory (php.runtime.memory.ArrayMemory)3 Memory (php.runtime.Memory)2 Invoker (php.runtime.invoke.Invoker)2 LongMemory (php.runtime.memory.LongMemory)2 ReferenceMemory (php.runtime.memory.ReferenceMemory)2 ObjectMemory (php.runtime.memory.ObjectMemory)1