Example #1
0
        /// <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(StackValue.Null);
            }

            if (zippedIndices.Length == 1)
            {
                StackValue coercedData = TypeSystem.Coerce(value, t, core);
                GCUtils.GCRetain(coercedData, core);
                return(ArrayUtils.SetValueForIndices(array, zippedIndices[0], coercedData, core));
            }

            if (t.rank > 0)
            {
                t.rank = t.rank - 1;
            }

            if (value.optype == AddressType.ArrayPointer)
            {
                // Replication happens on both side.
                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));
            }
        }
Example #2
0
        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(StackUtils.IsArray(array));
            if (!StackUtils.IsArray(array))
            {
                return(StackUtils.BuildNull());
            }

            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(HeapUtils.StoreArray(elements, dict, core));
        }
Example #4
0
        /// <summary>
        /// Try to get value for key from nested dictionaries. This function is
        /// used in the case that indexing into dictionaries that returned from
        /// a replicated function whose return type is dictionary.
        /// </summary>
        /// <param name="array"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="core"></param>
        /// <returns></returns>
        public static bool TryGetValueFromNestedDictionaries(StackValue array, StackValue key, out StackValue value, Core core)
        {
            if (!array.IsArray)
            {
                value = StackValue.Null;
                return(false);
            }

            HeapElement he = GetHeapElement(array, core);

            if (he.Dict != null && he.Dict.TryGetValue(key, out value))
            {
                return(true);
            }

            var  values   = new List <StackValue>();
            bool hasValue = false;

            foreach (var element in he.VisibleItems)
            {
                StackValue valueInElement;
                if (TryGetValueFromNestedDictionaries(element, key, out valueInElement, core))
                {
                    hasValue = true;
                    GCUtils.GCRetain(valueInElement, core);
                    values.Add(valueInElement);
                }
            }

            if (hasValue)
            {
                value = HeapUtils.StoreArray(values.ToArray(), null, core);
                return(true);
            }
            else
            {
                value = StackValue.Null;
                return(false);
            }
        }
        /// <summary>
        /// = array[index1][index2][...][indexN], and
        /// indices = {index1, index2, ..., indexN}
        /// </summary>
        /// <param name="array"></param>
        /// <param name="indices"></param>
        /// <param name="core"></param>
        /// <returns></returns>
        public static StackValue GetValueFromIndices(StackValue array, List <StackValue> indices, Core core)
        {
            if (indices.Count == 0)
            {
                return(array);
            }
            else if (!StackUtils.IsArray(array) && !StackUtils.IsString(array))
            {
                core.RuntimeStatus.LogWarning(WarningID.kOverIndexing, WarningMessage.kArrayOverIndexed);
                return(StackUtils.BuildNull());
            }

            StackValue[][] zippedIndices = ArrayUtils.GetZippedIndices(indices, core);
            if (zippedIndices == null || zippedIndices.Length == 0)
            {
                return(StackUtils.BuildNull());
            }

            StackValue[] values = new StackValue[zippedIndices.Length];
            for (int i = 0; i < zippedIndices.Length; ++i)
            {
                values[i] = GetValueFromIndices(array, zippedIndices[i], core);
            }

            if (zippedIndices.Length > 1)
            {
                for (int i = 0; i < values.Length; ++i)
                {
                    GCUtils.GCRelease(values[i], core);
                }

                return(HeapUtils.StoreArray(values, null, core));
            }
            else
            {
                return(values[0]);
            }
        }
Example #6
0
        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));
        }