/// <summary> /// array[index1][index2][...][indexN] = value, and /// indices = {index1, index2, ..., indexN} /// /// Note this function doesn't support the replication of array indexing. /// </summary> /// <param name="indices"></param> /// <param name="value"></param> /// <param name="core"></param> /// <returns></returns> public StackValue SetValueForIndices(StackValue[] indices, StackValue value, RuntimeCore runtimeCore) { DSArray array = this; for (int i = 0; i < indices.Length - 1; ++i) { StackValue index = indices[i]; StackValue svSubArray; if (index.IsNumeric) { index = index.ToInteger(); int absIndex = array.ExpandByAcessingAt((int)index.opdata); svSubArray = array.GetValueAt(absIndex); } else { svSubArray = array.GetValueFromIndex(index, runtimeCore); } // auto-promotion if (!svSubArray.IsArray) { svSubArray = heap.AllocateArray(new StackValue[] { svSubArray }); array.SetValueForIndex(index, svSubArray, runtimeCore); } array = heap.ToHeapObject <DSArray>(svSubArray); } return(array.SetValueForIndex(indices[indices.Length - 1], value, runtimeCore)); }
public static bool CompareFromDifferentCore(DSArray array1, DSArray array2, RuntimeCore rtCore1, RuntimeCore rtCore2, Context context = null) { if (array1.Count != array2.Count) { return(false); } for (int i = 0; i < array1.Count; i++) { if (!StackUtils.CompareStackValues(array1.GetValueAt(i), array2.GetValueAt(i), rtCore1, rtCore2, context)) { return(false); } } foreach (var key in array1.Dict.Keys) { StackValue value1 = array1.Dict[key]; StackValue value2 = StackValue.Null; if (!array2.Dict.TryGetValue(key, out value2)) { return(false); } if (!StackUtils.CompareStackValues(value1, value2, rtCore1, rtCore2)) { return(false); } } return(true); }
/// <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)); } }
/// <summary> /// array[index1][index2][...][indexN] = value, and /// indices = {index1, index2, ..., indexN} /// /// Note this function doesn't support the replication of array indexing. /// </summary> /// <param name="indices"></param> /// <param name="value"></param> /// <param name="core"></param> /// <returns></returns> public StackValue SetValueForIndices(StackValue[] indices, StackValue value, RuntimeCore runtimeCore) { DSArray array = this; for (int i = 0; i < indices.Length - 1; ++i) { StackValue index = indices[i]; StackValue svSubArray; if (index.IsNumeric) { index = index.ToInteger(); try { int absIndex = array.ExpandByAcessingAt((int)index.IntegerValue); svSubArray = array.GetValueAt(absIndex); } catch (RunOutOfMemoryException) { runtimeCore.RuntimeStatus.LogWarning(WarningID.RunOutOfMemory, Resources.RunOutOfMemory); return(StackValue.Null); } } else { svSubArray = array.GetValueFromIndex(index, runtimeCore); } // auto-promotion if (!svSubArray.IsArray) { try { svSubArray = heap.AllocateArray(new StackValue[] { svSubArray }); } catch (RunOutOfMemoryException) { svSubArray = StackValue.Null; runtimeCore.RuntimeStatus.LogWarning(WarningID.RunOutOfMemory, Resources.RunOutOfMemory); } array.SetValueForIndex(index, svSubArray, runtimeCore); } array = heap.ToHeapObject <DSArray>(svSubArray); } return(array.SetValueForIndex(indices[indices.Length - 1], value, runtimeCore)); }
public static bool CompareFromDifferentCore(DSArray array1, DSArray array2, RuntimeCore rtCore1, RuntimeCore rtCore2, Context context = null) { if (array1.Count != array2.Count) { return(false); } for (int i = 0; i < array1.Count; i++) { if (!StackUtils.CompareStackValues(array1.GetValueAt(i), array2.GetValueAt(i), rtCore1, rtCore2, context)) { return(false); } } return(true); }
/// <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); } } }
public static bool CompareFromDifferentCore(DSArray array1, DSArray array2, RuntimeCore rtCore1, RuntimeCore rtCore2, Context context = null) { if (array1.Count != array2.Count) { return false; } for (int i = 0; i < array1.Count; i++) { if (!StackUtils.CompareStackValues(array1.GetValueAt(i), array2.GetValueAt(i), rtCore1, rtCore2, context)) { return false; } } foreach (var key in array1.Dict.Keys) { StackValue value1 = array1.Dict[key]; StackValue value2 = StackValue.Null; if (!array2.Dict.TryGetValue(key, out value2)) { return false; } if (!StackUtils.CompareStackValues(value1, value2, rtCore1, rtCore2)) { return false; } } return true; }