/// <summary> /// Copy an array and coerce its elements/values to target type /// </summary> /// <param name="array"></param> /// <param name="type"></param> /// <param name="core"></param> /// <returns></returns> public StackValue CopyArray(Type type, RuntimeCore runtimeCore) { StackValue[] elements = new StackValue[Count]; for (int i = 0; i < Count; i++) { StackValue coercedValue = TypeSystem.Coerce(GetValueAt(i), type, runtimeCore); elements[i] = coercedValue; } Dictionary <StackValue, StackValue> dict = null; if (Dict != null) { dict = new Dictionary <StackValue, StackValue>(new StackValueComparer(runtimeCore)); foreach (var pair in Dict) { StackValue key = pair.Key; StackValue value = pair.Value; StackValue coercedValue = TypeSystem.Coerce(value, type, runtimeCore); dict[key] = coercedValue; } } var svArray = heap.AllocateArray(elements); var array = heap.ToHeapObject <DSArray>(svArray); array.Dict = dict; return(svArray); }
/// <summary> /// array[index1][index2][...][indexN] = value, and /// indices = {index1, index2, ..., indexN} /// </summary> /// <param name="array"></param> /// <param name="indices"></param> /// <param name="value"></param> /// <param name="t"></param> /// <param name="core"></param> /// <returns></returns> public static StackValue SetValueForIndices(StackValue array, List <StackValue> indices, StackValue value, Type t, Core core) { StackValue[][] zippedIndices = ArrayUtils.GetZippedIndices(indices, core); if (zippedIndices == null || zippedIndices.Length == 0) { return(StackUtils.BuildNull()); } if (zippedIndices.Length == 1) { StackValue coercedData = TypeSystem.Coerce(value, t, core); GCUtils.GCRetain(coercedData, core); return(ArrayUtils.SetValueForIndices(array, zippedIndices[0], coercedData, core)); } else if (value.optype == AddressType.ArrayPointer) { // Replication happens on both side. if (t.rank > 0) { t.rank = t.rank - 1; if (t.rank == 0) { t.IsIndexable = false; } } HeapElement dataHeapElement = GetHeapElement(value, core); int length = Math.Min(zippedIndices.Length, dataHeapElement.VisibleSize); StackValue[] oldValues = new StackValue[length]; for (int i = 0; i < length; ++i) { StackValue coercedData = TypeSystem.Coerce(dataHeapElement.Stack[i], t, core); GCUtils.GCRetain(coercedData, core); oldValues[i] = SetValueForIndices(array, zippedIndices[i], coercedData, core); } // The returned old values shouldn't have any key-value pairs return(HeapUtils.StoreArray(oldValues, null, core)); } else { // Replication is only on the LHS, so collect all old values // and return them in an array. StackValue coercedData = TypeSystem.Coerce(value, t, core); GCUtils.GCRetain(coercedData, core); StackValue[] oldValues = new StackValue[zippedIndices.Length]; for (int i = 0; i < zippedIndices.Length; ++i) { oldValues[i] = SetValueForIndices(array, zippedIndices[i], coercedData, core); } // The returned old values shouldn't have any key-value pairs return(HeapUtils.StoreArray(oldValues, null, core)); } }
/// <summary> /// array[index1][index2][...][indexN] = value, and /// indices = {index1, index2, ..., indexN} /// </summary> /// <param name="array"></param> /// <param name="indices"></param> /// <param name="value"></param> /// <param name="t"></param> /// <param name="core"></param> /// <returns></returns> public static StackValue SetValueForIndices(StackValue array, List <StackValue> indices, StackValue value, Type t, RuntimeCore runtimeCore) { RuntimeMemory rmem = runtimeCore.RuntimeMemory; StackValue[][] zippedIndices = ArrayUtils.GetZippedIndices(indices, runtimeCore); if (zippedIndices == null || zippedIndices.Length == 0) { return(StackValue.Null); } if (zippedIndices.Length == 1) { StackValue coercedData = TypeSystem.Coerce(value, t, runtimeCore); return(ArrayUtils.SetValueForIndices(array, zippedIndices[0], coercedData, runtimeCore)); } if (t.rank > 0) { t.rank = t.rank - 1; } if (value.IsArray) { // Replication happens on both side. HeapElement dataHeapElement = GetHeapElement(value, runtimeCore); int length = Math.Min(zippedIndices.Length, dataHeapElement.VisibleSize); StackValue[] oldValues = new StackValue[length]; for (int i = 0; i < length; ++i) { StackValue coercedData = TypeSystem.Coerce(dataHeapElement.Stack[i], t, runtimeCore); oldValues[i] = SetValueForIndices(array, zippedIndices[i], coercedData, runtimeCore); } // The returned old values shouldn't have any key-value pairs return(rmem.Heap.AllocateArray(oldValues, null)); } else { // Replication is only on the LHS, so collect all old values // and return them in an array. StackValue coercedData = TypeSystem.Coerce(value, t, runtimeCore); StackValue[] oldValues = new StackValue[zippedIndices.Length]; for (int i = 0; i < zippedIndices.Length; ++i) { oldValues[i] = SetValueForIndices(array, zippedIndices[i], coercedData, runtimeCore); } // The returned old values shouldn't have any key-value pairs return(rmem.Heap.AllocateArray(oldValues, null)); } }
/// <summary> /// array[index1][index2][...][indexN] = value, and /// indices = {index1, index2, ..., indexN} /// </summary> /// <param name="indices"></param> /// <param name="value"></param> /// <param name="t"></param> /// <param name="core"></param> /// <returns></returns> public StackValue SetValueForIndices(List <StackValue> indices, StackValue value, Type t, RuntimeCore runtimeCore) { StackValue[][] zippedIndices = ArrayUtils.GetZippedIndices(indices, runtimeCore); if (zippedIndices == null || zippedIndices.Length == 0) { return(StackValue.Null); } if (zippedIndices.Length == 1) { StackValue coercedData = TypeSystem.Coerce(value, t, runtimeCore); return(SetValueForIndices(zippedIndices[0], coercedData, runtimeCore)); } if (t.rank > 0) { t.rank = t.rank - 1; } if (value.IsArray) { // Replication happens on both side. DSArray dataElements = heap.ToHeapObject <DSArray>(value); int length = Math.Min(zippedIndices.Length, dataElements.Count); StackValue[] oldValues = new StackValue[length]; for (int i = 0; i < length; ++i) { StackValue coercedData = TypeSystem.Coerce(dataElements.GetValueAt(i), t, runtimeCore); oldValues[i] = SetValueForIndices(zippedIndices[i], coercedData, runtimeCore); } // The returned old values shouldn't have any key-value pairs return(heap.AllocateArray(oldValues)); } else { // Replication is only on the LHS, so collect all old values // and return them in an array. StackValue coercedData = TypeSystem.Coerce(value, t, runtimeCore); StackValue[] oldValues = new StackValue[zippedIndices.Length]; for (int i = 0; i < zippedIndices.Length; ++i) { oldValues[i] = SetValueForIndices(zippedIndices[i], coercedData, runtimeCore); } // The returned old values shouldn't have any key-value pairs return(heap.AllocateArray(oldValues)); } }
public static StackValue SetDataAtIndices(StackValue array, List <StackValue> indices, StackValue data, Type t, Core core) { int[][] zippedIndices = ArrayUtils.GetZippedIndices(indices, core); if (zippedIndices == null || zippedIndices.Length == 0) { return(StackUtils.BuildNull()); } if (zippedIndices.Length == 1) { StackValue coercedData = TypeSystem.Coerce(data, t, core); GCUtils.GCRetain(coercedData, core); return(ArrayUtils.SetDataAtIndices(array, zippedIndices[0], coercedData, core)); } else if (data.optype == AddressType.ArrayPointer) { if (t.rank > 0) { t.rank = t.rank - 1; if (t.rank == 0) { t.IsIndexable = false; } } HeapElement dataHeapElement = core.Heap.Heaplist[(int)data.opdata]; int length = Math.Min(zippedIndices.Length, dataHeapElement.VisibleSize); StackValue[] oldValues = new StackValue[length]; for (int i = 0; i < length; ++i) { StackValue coercedData = TypeSystem.Coerce(dataHeapElement.Stack[i], t, core); GCUtils.GCRetain(coercedData, core); oldValues[i] = ArrayUtils.SetDataAtIndices(array, zippedIndices[i], coercedData, core); } return(HeapUtils.StoreArray(oldValues, core)); } else { StackValue coercedData = TypeSystem.Coerce(data, t, core); GCUtils.GCRetain(coercedData, core); StackValue[] oldValues = new StackValue[zippedIndices.Length]; for (int i = 0; i < zippedIndices.Length; ++i) { oldValues[i] = ArrayUtils.SetDataAtIndices(array, zippedIndices[i], coercedData, core); } return(HeapUtils.StoreArray(oldValues, core)); } }
/// <summary> /// Copy an array and coerce its elements/values to target type /// </summary> /// <param name="array"></param> /// <param name="type"></param> /// <param name="core"></param> /// <returns></returns> public static StackValue CopyArray(StackValue array, Type type, Core core) { Validity.Assert(array.IsArray); if (!array.IsArray) { return(StackValue.Null); } HeapElement he = GetHeapElement(array, core); Validity.Assert(he != null); int elementSize = GetElementSize(array, core); StackValue[] elements = new StackValue[elementSize]; for (int i = 0; i < elementSize; i++) { StackValue coercedValue = TypeSystem.Coerce(he.Stack[i], type, core); GCUtils.GCRetain(coercedValue, core); elements[i] = coercedValue; } Dictionary <StackValue, StackValue> dict = null; if (he.Dict != null) { dict = new Dictionary <StackValue, StackValue>(new StackValueComparer(core)); foreach (var pair in he.Dict) { StackValue key = pair.Key; StackValue value = pair.Value; StackValue coercedValue = TypeSystem.Coerce(value, type, core); GCUtils.GCRetain(key, core); GCUtils.GCRetain(coercedValue, core); dict[key] = coercedValue; } } return(core.Heap.AllocateArray(elements, dict)); }
/// <summary> /// Copy an array and coerce its elements/values to target type /// </summary> public StackValue CopyArray(Type type, RuntimeCore runtimeCore) { var elements = new StackValue[Count]; for (int i = 0; i < Count; i++) { var coercedValue = TypeSystem.Coerce(GetValueAt(i), type, runtimeCore); elements[i] = coercedValue; } try { var svArray = heap.AllocateArray(elements); var array = heap.ToHeapObject <DSArray>(svArray); return(svArray); } catch (RunOutOfMemoryException) { runtimeCore.RuntimeStatus.LogWarning(WarningID.RunOutOfMemory, Resources.RunOutOfMemory); return(StackValue.Null); } }
/// <summary> /// Copy an array and coerce its elements/values to target type /// </summary> /// <param name="array"></param> /// <param name="type"></param> /// <param name="core"></param> /// <returns></returns> public StackValue CopyArray(Type type, RuntimeCore runtimeCore) { StackValue[] elements = new StackValue[Count]; for (int i = 0; i < Count; i++) { StackValue coercedValue = TypeSystem.Coerce(GetValueAt(i), type, runtimeCore); elements[i] = coercedValue; } Dictionary <StackValue, StackValue> dict = null; if (Dict != null) { dict = new Dictionary <StackValue, StackValue>(new StackValueComparer(runtimeCore)); foreach (var pair in Dict) { StackValue key = pair.Key; StackValue value = pair.Value; StackValue coercedValue = TypeSystem.Coerce(value, type, runtimeCore); dict[key] = coercedValue; } } try { var svArray = heap.AllocateArray(elements); var array = heap.ToHeapObject <DSArray>(svArray); array.Dict = dict; return(svArray); } catch (RunOutOfMemoryException) { runtimeCore.RuntimeStatus.LogWarning(WarningID.RunOutOfMemory, Resources.RunOutOfMemory); return(StackValue.Null); } }
public static StackValue CoerceArray(StackValue array, Type typ, Core core) { //@TODO(Luke) handle array rank coersions Validity.Assert(IsArray(array), "Argument needs to be an array {99FB71A6-72AD-4C93-8F1E-0B1F419C1A6D}"); //This is the element on the heap that manages the data structure HeapElement heapElement = core.Heap.Heaplist[(int)array.opdata]; StackValue[] newSVs = new StackValue[heapElement.VisibleSize]; for (int i = 0; i < heapElement.VisibleSize; ++i) { StackValue sv = heapElement.Stack[i]; StackValue coercedValue; if (IsArray(sv)) { Type typ2 = new Type(); typ2.UID = typ.UID; typ2.rank = typ.rank - 1; typ2.IsIndexable = (typ2.rank == -1 || typ2.rank > 0); coercedValue = CoerceArray(sv, typ2, core); } else { coercedValue = TypeSystem.Coerce(sv, typ, core); } GCUtils.GCRetain(coercedValue, core); newSVs[i] = coercedValue; } return(HeapUtils.StoreArray(newSVs, core)); }
/// <summary> /// array[index1][index2][...][indexN] = value, and /// indices = {index1, index2, ..., indexN} /// </summary> /// <param name="indices"></param> /// <param name="value"></param> /// <param name="t"></param> /// <param name="core"></param> /// <returns></returns> public StackValue SetValueForIndices(List <StackValue> indices, StackValue value, RuntimeCore runtimeCore) { StackValue[][] zippedIndices = ArrayUtils.GetZippedIndices(indices, runtimeCore); if (zippedIndices == null || zippedIndices.Length == 0) { return(StackValue.Null); } var t = TypeSystem.BuildPrimitiveTypeObject(PrimitiveType.Var); if (zippedIndices.Length == 1) { StackValue coercedData = TypeSystem.Coerce(value, t, runtimeCore); return(SetValueForIndices(zippedIndices[0], coercedData, runtimeCore)); } if (value.IsArray) { // Replication happens on both side. DSArray dataElements = heap.ToHeapObject <DSArray>(value); int length = Math.Min(zippedIndices.Length, dataElements.Count); StackValue[] oldValues = new StackValue[length]; for (int i = 0; i < length; ++i) { StackValue coercedData = TypeSystem.Coerce(dataElements.GetValueAt(i), t, runtimeCore); oldValues[i] = SetValueForIndices(zippedIndices[i], coercedData, runtimeCore); } // The returned old values shouldn't have any key-value pairs try { return(heap.AllocateArray(oldValues)); } catch (RunOutOfMemoryException) { runtimeCore.RuntimeStatus.LogWarning(WarningID.RunOutOfMemory, Resources.RunOutOfMemory); return(StackValue.Null); } } else { // Replication is only on the LHS, so collect all old values // and return them in an array. StackValue coercedData = TypeSystem.Coerce(value, t, runtimeCore); StackValue[] oldValues = new StackValue[zippedIndices.Length]; for (int i = 0; i < zippedIndices.Length; ++i) { oldValues[i] = SetValueForIndices(zippedIndices[i], coercedData, runtimeCore); } // The returned old values shouldn't have any key-value pairs try { return(heap.AllocateArray(oldValues)); } catch (RunOutOfMemoryException) { runtimeCore.RuntimeStatus.LogWarning(WarningID.RunOutOfMemory, Resources.RunOutOfMemory); return(StackValue.Null); } } }