package com.oracle.truffle.js.runtime.array;
import static com.oracle.truffle.js.runtime.builtins.JSAbstractArray.arrayGetArray;
import static com.oracle.truffle.js.runtime.builtins.JSAbstractArray.arrayGetLength;
import static com.oracle.truffle.js.runtime.builtins.JSAbstractArray.arraySetArray;
import static com.oracle.truffle.js.runtime.builtins.JSAbstractArray.arraySetLength;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.TreeMap;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.js.runtime.Boundaries;
import com.oracle.truffle.js.runtime.Errors;
import com.oracle.truffle.js.runtime.JSRuntime;
import com.oracle.truffle.js.runtime.objects.Undefined;
public final class SparseArray extends DynamicArray {
private static final SparseArray SPARSE_ARRAY = new SparseArray(INTEGRITY_LEVEL_NONE, createCache());
private SparseArray(int integrityLevel, DynamicArrayCache cache) {
super(integrityLevel, cache);
}
public static SparseArray createSparseArray() {
return SPARSE_ARRAY;
}
public static SparseArray makeSparseArray(DynamicObject object, ScriptArray fromArray) {
assert !(fromArray instanceof SparseArray);
TreeMap<Long, Object> arrayMap = createArrayMap();
copyArrayToMap(object, fromArray, arrayMap);
arraySetLength(object, fromArray.length(object));
arraySetArray(object, arrayMap);
return createSparseArray();
}
@TruffleBoundary
public static TreeMap<Long, Object> createArrayMap() {
return new TreeMap<>();
}
protected static void copyArrayToMap(DynamicObject object, ScriptArray fromArray, Map<Long, Object> toMap) {
for (long index = fromArray.firstElementIndex(object); index <= fromArray.lastElementIndex(object); index = fromArray.nextElementIndex(object, index)) {
assert fromArray.hasElement(object, index);
Boundaries.mapPut(toMap, index, fromArray.getElement(object, index));
}
}
@SuppressWarnings("unchecked")
private static TreeMap<Long, Object> arrayMap(DynamicObject object) {
return (TreeMap<Long, Object>) arrayGetArray(object);
}
@TruffleBoundary
@Override
public Object getElement(DynamicObject object, long index) {
Object value = arrayMap(object).get(index);
return value != null ? value : Undefined.instance;
}
@TruffleBoundary
@Override
public Object getElementInBounds(DynamicObject object, long index) {
Object value = arrayMap(object).get(index);
assert value != null;
return value;
}
@TruffleBoundary
@Override
public ScriptArray setElementImpl(DynamicObject object, long index, Object value, boolean strict) {
arrayMap(object).put(index, value);
if (index >= length(object)) {
arraySetLength(object, index + 1);
}
return this;
}
@Override
public long length(DynamicObject object) {
return arrayGetLength(object);
}
@Override
public int lengthInt(DynamicObject object) {
long len = arrayGetLength(object);
if (len > Integer.MAX_VALUE) {
throw Errors.unsupported("array length too large");
}
return (int) len;
}
@TruffleBoundary
@Override
public SparseArray setLengthImpl(DynamicObject object, long len, ProfileHolder profile) {
arraySetLength(object, len);
arrayMap(object).tailMap(len).clear();
return this;
}
@TruffleBoundary
@Override
public long firstElementIndex(DynamicObject object) {
try {
return arrayMap(object).firstKey();
} catch (NoSuchElementException ex) {
return 0;
}
}
@TruffleBoundary
@Override
public long lastElementIndex(DynamicObject object) {
try {
return arrayMap(object).lastKey();
} catch (NoSuchElementException ex) {
return -1;
}
}
@TruffleBoundary
@Override
public long nextElementIndex(DynamicObject object, long index) {
Long nextIndex = arrayMap(object).higherKey(index);
return nextIndex != null ? nextIndex.longValue() : JSRuntime.MAX_SAFE_INTEGER_LONG;
}
@TruffleBoundary
@Override
public long previousElementIndex(DynamicObject object, long index) {
Long nextIndex = arrayMap(object).lowerKey(index);
return nextIndex != null ? nextIndex.longValue() : -1;
}
@Override
public Object[] toArray(DynamicObject object) {
assert length(object) <= Integer.MAX_VALUE;
Object[] newArray = new Object[(int) length(object)];
Arrays.fill(newArray, Undefined.instance);
for (long i = firstElementIndex(object); i <= lastElementIndex(object); i = nextElementIndex(object, i)) {
if (i >= 0) {
newArray[(int) i] = this.getElement(object, i);
}
}
return newArray;
}
@Override
public Object cloneArray(DynamicObject object) {
return arrayMap(object).clone();
}
@TruffleBoundary
@Override
public ScriptArray deleteElementImpl(DynamicObject object, long index, boolean strict) {
arrayMap(object).remove(index);
return this;
}
@TruffleBoundary
@Override
public boolean hasElement(DynamicObject object, long index) {
return arrayMap(object).containsKey(index);
}
@Override
public boolean isHolesType() {
return true;
}
@Override
public boolean hasHoles(DynamicObject object) {
return true;
}
@Override
public ScriptArray removeRangeImpl(DynamicObject object, long start, long end) {
assert start <= end;
assert start >= 0;
assert end < length(object);
long delta = end - start + 1;
long pos = start;
if (!hasElement(object, pos)) {
pos = nextElementIndex(object, pos);
}
while (pos <= end) {
deleteElementImpl(object, pos, false);
pos = nextElementIndex(object, pos);
}
while (pos < length(object)) {
setElement(object, pos - delta, getElement(object, pos), false);
deleteElementImpl(object, pos, false);
pos = nextElementIndex(object, pos);
}
return this;
}
@Override
public ScriptArray addRangeImpl(DynamicObject object, long offset, int size) {
assert offset < length(object);
long pos = length(object);
if (!hasElement(object, pos)) {
pos = previousElementIndex(object, pos);
}
while (pos >= offset) {
setElement(object, pos + size, getElement(object, pos), false);
deleteElementImpl(object, pos, false);
pos = previousElementIndex(object, pos);
}
return this;
}
@TruffleBoundary
@Override
public List<Object> ownPropertyKeys(DynamicObject object) {
Set<Long> keySet = arrayMap(object).keySet();
List<Object> list = new ArrayList<>(keySet.size());
for (long index : keySet) {
list.add(Boundaries.stringValueOf(index));
}
return list;
}
@Override
protected DynamicArray withIntegrityLevel(int newIntegrityLevel) {
return new SparseArray(newIntegrityLevel, cache);
}
}