CopyTo() public méthode

Copies the elements of the given sparse array to this sparse array, starting at a particular index. Existing values are overwritten.
public CopyTo ( SparseArray source, uint start ) : void
source SparseArray The sparse array to copy.
start uint The zero-based index at which copying begins.
Résultat void
 /// <summary>
 /// Creates a sparse array from the given dense array.
 /// </summary>
 /// <param name="array"> The array to copy items from. </param>
 /// <param name="length"> The number of items to copy. </param>
 /// <returns> A new sparse array containing the items from the given array. </returns>
 public static SparseArray FromDenseArray(object[] array, int length)
 {
     if (array == null)
         throw new ArgumentNullException("array");
     if (length > array.Length)
         throw new ArgumentOutOfRangeException("length");
     var result = new SparseArray();
     result.CopyTo(array, 0, length);
     return result;
 }
Exemple #2
0
        /// <summary>
        /// Creates a sparse array from the given dense array.
        /// </summary>
        /// <param name="array"> The array to copy items from. </param>
        /// <param name="length"> The number of items to copy. </param>
        /// <returns> A new sparse array containing the items from the given array. </returns>
        public static SparseArray FromDenseArray(object[] array, int length)
        {
            if (array == null)
            {
                throw new ArgumentNullException("array");
            }
            if (length > array.Length)
            {
                throw new ArgumentOutOfRangeException("length");
            }
            var result = new SparseArray();

            result.CopyTo(array, 0, length);
            return(result);
        }
Exemple #3
0
        public static ArrayInstance Concat(ObjectInstance thisObj, params object[] items)
        {
            // Create a new items array with the thisObject at the beginning.
            var temp = new object[items.Length + 1];
            temp[0] = thisObj;
            Array.Copy(items, 0, temp, 1, items.Length);
            items = temp;

            // Determine if the resulting array should be dense or sparse and calculate the length
            // at the same time.
            bool dense = true;
            uint length = (uint)items.Length;
            foreach (object item in items)
                if (item is ArrayInstance)
                {
                    length += ((ArrayInstance)item).Length - 1;
                    if (((ArrayInstance)item).dense == null)
                        dense = false;
                }

            // This method only supports arrays of length up to 2^31-1, rather than 2^32-1.
            if (length > int.MaxValue)
                throw new JavaScriptException(thisObj.Engine, "RangeError", "The resulting array is too long");

            if (dense == true)
            {
                // Create a dense array.
                var result = new object[length];

                int index = 0;
                foreach (object item in items)
                {
                    if (item is ArrayInstance)
                    {
                        // Add the items in the array to the end of the resulting array.
                        var array = (ArrayInstance)item;
                        Array.Copy(array.dense, 0, result, index, (int)array.Length);
                        if (array.denseMayContainHoles == true && array.Prototype != null)
                        {
                            // Populate holes from the prototype.
                            for (uint i = 0; i < array.length; i++)
                                if (array.dense[i] == null)
                                    result[index + i] = array.Prototype.GetPropertyValue(i);
                        }
                        index += (int)array.Length;
                    }
                    else
                    {
                        // Add the item to the end of the resulting array.
                        result[index ++] = item;
                    }
                }

                // Return the new dense array.
                return new ArrayInstance(thisObj.Engine.Array.InstancePrototype, result);
            }
            else
            {
                // Create a new sparse array.
                var result = new SparseArray();

                int index = 0;
                foreach (object item in items)
                {
                    if (item is ArrayInstance)
                    {
                        // Add the items in the array to the end of the resulting array.
                        var array = (ArrayInstance)item;
                        if (array.dense != null)
                        {
                            result.CopyTo(array.dense, (uint)index, (int)array.Length);
                            if (array.Prototype != null)
                            {
                                // Populate holes from the prototype.
                                for (uint i = 0; i < array.length; i++)
                                    if (array.dense[i] == null)
                                        result[(uint)index + i] = array.Prototype.GetPropertyValue(i);
                            }
                        }
                        else
                        {
                            result.CopyTo(array.sparse, (uint)index);
                            if (array.Prototype != null)
                            {
                                // Populate holes from the prototype.
                                for (uint i = 0; i < array.Length; i++)
                                    if (array.sparse[i] == null)
                                        result[(uint)index + i] = array.Prototype.GetPropertyValue(i);
                            }
                        }
                        index += (int)array.Length;
                    }
                    else
                    {
                        // Add the item to the end of the resulting array.
                        result[(uint)index] = item;
                        index++;
                    }
                }

                // Return the new sparse array.
                return new ArrayInstance(thisObj.Engine.Array.InstancePrototype, result, length);
            }
        }