public MultiDimArrayNode(ObjectNode parent, Array array, string name) : base(parent, array, 0, null)
 {
     if (name == null)
     {
         this.Name = array.GetType().GetElementType().FormatTypeName() + "[,]";
     }
     else
     {
         this.Name = name;
     }
     this.ElementType = array.GetType().GetElementType();
     this.Data = array;
 }
Example #2
0
 public static string CodeRepresentation(Array a)
 {
     StringBuilder ret = new StringBuilder();
     ret.Append(a.GetType().FullName);
     ret.Append("(");
     switch (a.Rank) {
         case 1: {
                 for (int i = 0; i < a.Length; i++) {
                     if (i > 0) ret.Append(", ");
                     ret.Append(Ops.StringRepr(a.GetValue(i + a.GetLowerBound(0))));
                 }
             }
             break;
         case 2: {
                 int imax = a.GetLength(0);
                 int jmax = a.GetLength(1);
                 for (int i = 0; i < imax; i++) {
                     ret.Append("\n");
                     for (int j = 0; j < jmax; j++) {
                         if (j > 0) ret.Append(", ");
                         ret.Append(Ops.StringRepr(a.GetValue(i + a.GetLowerBound(0), j + a.GetLowerBound(1))));
                     }
                 }
             }
             break;
         default:
             ret.Append(" Multi-dimensional array ");
             break;
     }
     ret.Append(")");
     return ret.ToString();
 }
Example #3
0
 /// <summary>
 /// 重新定义一个数组列表
 /// </summary>
 /// <param name="OriginalArray">需要被重定义的数组</param>
 /// <param name="NewSize">这个数组的新大小</param>
 public static Array ReDim(Array OriginalArray, Int32 NewSize)
 {
     Type ArrayElementsType = OriginalArray.GetType().GetElementType();
     Array newArray = Array.CreateInstance(ArrayElementsType, NewSize);
     Array.Copy(OriginalArray, 0, newArray, 0, Math.Min(OriginalArray.Length, NewSize));
     return newArray;
 }
 void ICollection.CopyTo(Array array, int index)
 {
     if (array is ThemeInfo[])
         ((ICollection)this._collection).CopyTo(array, index);
     else
         throw new InvalidCastException(String.Format("Can not cast {0} to ThemeInfo[]", array.GetType()));
 }
Example #5
0
        public static string ArrayToDescriptor(Array array, 
            TypeRegistry treg,
            Type type = null,
            string th = null)
        {
            if (type==null)
                 type = array.GetType();

              if (array.LongLength>MAX_ELM_COUNT)
                throw new SlimSerializationException(StringConsts.SLIM_ARRAYS_OVER_MAX_ELM_ERROR.Args(array.LongLength, MAX_ELM_COUNT));

              if (type==typeof(object[]))//special case for object[], because this type is very often used in Glue and other places
               return "$1|"+array.Length.ToString();

              if (th==null)
                 th = treg.GetTypeHandle(type);

               var ar = array.Rank;
               if (ar>MAX_DIM_COUNT)
                throw new SlimSerializationException(StringConsts.SLIM_ARRAYS_OVER_MAX_DIMS_ERROR.Args(ar, MAX_DIM_COUNT));

               var descr = new StringBuilder(th);
               descr.Append('|');//separator char

               for(int i=0; i<ar; i++)
               {
                  descr.Append(array.GetLowerBound(i));
                  descr.Append('~');
                  descr.Append(array.GetUpperBound(i));
                  if (i<ar-1)
                   descr.Append(',');
               }

              return descr.ToString();
        }
Example #6
0
		public ArrayExtension (Array elements)
		{
			if (elements == null)
				throw new ArgumentNullException ("elements");
			Type = elements.GetType ().GetElementType ();
			items = new ArrayList (elements);
		}
Example #7
0
 private static Array aRedimension(Array orgArray, Int32 tamaño)
 {
     Type t = orgArray.GetType().GetElementType();
     Array nArray = Array.CreateInstance(t, tamaño);
     Array.Copy(orgArray, 0, nArray, 0, Math.Min(orgArray.Length, tamaño));
     return nArray;
 }
      /// <summary>
      /// This factory method wraps a SafePinnedObject around the specified array.
      /// </summary>
      /// <param name="array">The array that you want to pin.</param>
      /// <param name="startOffset">The first element in the array whose address you want to pass to native code.</param>
      /// <param name="numberOfElements">The number of elements in the array you wish to pass to native code.</param>
      /// <returns>The SafePinnedObject wrapping the desired array elements.</returns>
      public static SafePinnedObject FromArray(Array array, Int32 startOffset, Int32 numberOfElements) {
         // If obj is null, we create this object but it pins nothing (size will be 0)
         if (array == null) return new SafePinnedObject(null, 0, 0);

         // Validate the structure of the object before pinning it
         if (array.Rank != 1)
            throw new ArgumentException("array Rank must be 1");

         if (startOffset < 0)
            throw new ArgumentOutOfRangeException("startOffset", "Must be >= 0");

         // Validate the structure of the array's element type
         Type elementType = array.GetType().GetElementType();
         if (!elementType.IsValueType && !elementType.IsEnum)
            throw new ArgumentException("array's elements must be value types or enum types");

         if (elementType.IsAutoLayout)
            throw new ArgumentException("array's elements must not be auto layout");

         // If numElements not specied (-1), assume the remainder of the array length
         if (numberOfElements == -1) numberOfElements = array.Length - startOffset;

         if (numberOfElements > array.Length)
            throw new ArgumentOutOfRangeException("numberOfElements", "Array has fewer elements than specified");

         // Convert startOffset from element offset to byte offset
         startOffset *= Marshal.SizeOf(elementType);

         return new SafePinnedObject(array, startOffset, 
            numberOfElements * Marshal.SizeOf(elementType));  // Convert numElements to number of bytes
      }
Example #9
0
		public static bool ArrayDeepEquals(Array arr1, Array arr2) {
			if (arr1.Length!=arr2.Length || arr1.GetType()!=arr2.GetType())
				return false;

			for (int i=0; i<arr1.Length; i++) {
				var v1 = arr1.GetValue(i);
				var v2 = arr2.GetValue(i);
				if (v1 is Array && v2 is Array)
					if (!ArrayDeepEquals((Array)v1, (Array)v2)) {
						return false;
					} else
						continue;

				if (v1==null && v2==null)
					continue;

				if (v1!=null)
					if (!v1.Equals(v2))
						return false;
				if (v2 != null)
					if (!v2.Equals(v1))
						return false;
			}
			return true;
		}
Example #10
0
        private static void WriteSimpleArray(this ObjectWriter writer, Array value)
        {
            writer.WriteTag(FormatterTag.Array);

            var elementType = value.GetType().GetElementType();
            writer.InnerWrite(elementType);
            writer.InnerWrite(value);
        }
Example #11
0
		public ArrayExtension (Array elements)
		{
			if (elements == null)
				throw new ArgumentNullException ("elements");
			Type = elements.GetType ().GetElementType ();
			Items = new List<object> (elements.Length);
			foreach (var o in elements)
				Items.Add (o);
		}
 public ArrayExtension(Array elements)
 {
     this.arrayElementList = new ArrayList();
     if (elements == null)
     {
         throw new ArgumentNullException("elements");
     }
     this.arrayElementList.AddRange(elements);
     this.arrayType = elements.GetType().GetElementType();
 }
Example #13
0
        /// <summary>
        ///     Compares to arrays for values equality.
        /// </summary>
        /// <returns>true if both arrays contain the same values, false otherwise</returns>
        internal static bool ArrayEquals(Array l, Array r)
        {
            if (l == null && r == null)
            {
                return true;
            }

            if (l == null || r == null)
            {
                return false;
            }

            if (l.GetType() != r.GetType())
            {
                return false;
            }

            if (l.Length != r.Length)
            {
                return false;
            }

            for (var i = 0; i < l.Length; i++)
            {
                var lv = l.GetValue(i);
                var rv = r.GetValue(i);

                if (lv == rv || lv == null && rv == null)
                {
                    continue;
                }

                if (lv == null || rv == null)
                {
                    return false;
                }

                if (lv.GetType().IsArray)
                {
                    if (!ArrayEquals(lv as Array, rv as Array))
                    {
                        return false;
                    }
                }
                else
                {
                    if (!lv.Equals(rv))
                    {
                        return false;
                    }
                }
            }

            return true;
        }
        private static void AssertImport(Array expected, string s)
        {
            JsonReader reader = CreateReader(s);
            
            object o = reader.Get(expected.GetType());

            if (expected == null)
                Assert.IsNull(o);
            
            Assert.AreEqual(expected, o);
        }
 /// <summary>
 /// 重定义数组
 /// </summary>
 /// <param name="origArray">原来的数组长度</param>
 /// <param name="desiredsize">新的宽度</param>
 /// <returns></returns>
 public static Array Redim(Array origArray, Int32 desiredsize)
 {
     //确定每个元素类型           
     Type t = origArray.GetType().GetElementType();
     //创建一个含有期望元素个数的新数组       
     //新数组的类型必须匹配原数组的类型       
     Array newArray = Array.CreateInstance(t, desiredsize);
     //将原数组中的元素拷贝到新数组中       
     Array.Copy(origArray, 0, newArray, 0, Math.Min(origArray.Length, desiredsize));
     return newArray;
 }
Example #16
0
        private static Array CombineArrays(Array arr1, Array arr2)
        {
            if (arr1 == null)
                return arr2;
            if (arr2 == null)
                return arr1;

            Array newArr = Array.CreateInstance(arr1.GetType().GetElementType(), arr1.Length + arr2.Length);
            arr1.CopyTo(newArr, 0);
            arr2.CopyTo(newArr, arr1.Length);
            return newArr;
        }
        private static PropertyDescriptorCollection CreateColumnPropertyDescriptorCollection(Array source)
        {
            var elementType = source.GetType().GetElementType();
            var n = source.GetLength(0);
            var descriptors = new Array2DIndexPropertyDescriptor[n];
            for (int i = 0; i < n; i++)
            {
                descriptors[i] = new Array2DIndexPropertyDescriptor(elementType, i);
            }

            return new PropertyDescriptorCollection(descriptors);
        }
Example #18
0
        protected Array resizeArray(Array oldArray, int newSize)
        {
            int oldSize = oldArray.Length;
            Type elementType = oldArray.GetType().GetElementType();
            System.Array newArray = System.Array.CreateInstance(elementType, newSize);
            int preserveLength = System.Math.Min(oldSize, newSize);
            if (preserveLength > 0)
                System.Array.Copy(oldArray, newArray, preserveLength);


            return newArray;
        }
Example #19
0
		public static int ByteLength (Array array)
		{
			if (array == null)throw new ArgumentNullException ("array");

            if (array.Length == 0) return 0;

		    var type = array.GetType().GetElementType();

            if (!type.IsPrimitive)throw new ArgumentException ("Object must be an array of primitives.");

            return array.Length * NumberOfBytesForPrimitiveType (type);
		}
Example #20
0
 internal void Iterate_array_elements_in_all_dimensions(Array objectToFlatten, List<int> indexes)
 {
     var t = objectToFlatten.GetType();
     if (t.GetArrayRank() > indexes.Count)
         for (var i = 0; i < objectToFlatten.GetLength(indexes.Count); i++)
         {
             indexes.Add(i);
             Iterate_array_elements_in_all_dimensions(objectToFlatten, indexes);
             indexes.RemoveAt(indexes.Count - 1);
         }
     else
         this.Element(objectToFlatten.GetValue(indexes.ToArray()));
 }
Example #21
0
	// Validate a primitive array and get its length in bytes.
	private static int ValidatePrimitive(Array array, String name)
			{
				if(array == null)
				{
					throw new ArgumentNullException(name);
				}
				else if(!array.GetType().GetElementType().IsPrimitive)
				{
					throw new ArgumentException
						(_("Arg_NonPrimitiveArray"), name);
				}
				return GetLength(array);
			}
        private static PropertyDescriptorCollection CreateRowPropertyDescriptorCollection(Array source)
        {
            var elementType = source.GetType().GetElementType();
            var n = source.GetLength(1);
            var descriptors = new Array2DIndexPropertyDescriptor[n];
            for (int i = 0; i < n; i++)
            {
                descriptors[i] = new Array2DIndexPropertyDescriptor(elementType, i);
            }

            // ReSharper disable once CoVariantArrayConversion
            return new PropertyDescriptorCollection(descriptors);
        }
Example #23
0
        public static Array Add(Array data1, Array data2)
        {
            if (data1.Rank > 1 || data2.Rank > 1) throw new NotImplementedException("can't add multidimensional arrays");

            Type type1 = data1.GetType();
            Type type2 = data2.GetType();
            Type type = (type1 == type2) ? type1.GetElementType() : typeof(object);

            Array ret = Array.CreateInstance(type, data1.Length + data2.Length);
            Array.Copy(data1, 0, ret, 0, data1.Length);
            Array.Copy(data2, 0, ret, data1.Length, data2.Length);
            return ret;
        }
Example #24
0
		public ArrayExtension (Array elements)
		{
			if (elements == null)
				throw new ArgumentNullException ("elements");
			Type = elements.GetType ().GetElementType ();
#if MOONLIGHT
			items = new List<object> ();
			foreach (var element in elements)
				items.Add (element);
#else
			items = new ArrayList (elements);
#endif
		}
Example #25
0
        public static Array Add(Array data1, Array data2) {
            if (data1 == null) throw PythonOps.TypeError("expected array for 1st argument, got None");
            if (data2 == null) throw PythonOps.TypeError("expected array for 2nd argument, got None");

            if (data1.Rank > 1 || data2.Rank > 1) throw new NotImplementedException("can't add multidimensional arrays");

            Type type1 = data1.GetType();
            Type type2 = data2.GetType();
            Type type = (type1 == type2) ? type1.GetElementType() : typeof(object);

            Array ret = Array.CreateInstance(type, data1.Length + data2.Length);
            Array.Copy(data1, 0, ret, 0, data1.Length);
            Array.Copy(data2, 0, ret, data1.Length, data2.Length);
            return ret;
        }
Example #26
0
 /// <summary>
 /// Fills the array with an specific value from an specific index to an specific index.
 /// </summary>
 /// <param _name="array">The array to be filled.</param>
 /// <param _name="fromindex">The first index to be filled.</param>
 /// <param _name="toindex">The last index to be filled.</param>
 /// <param _name="val">The value to fill the array with.</param>
 public static void Fill(Array array, Int32 fromindex, Int32 toindex, Object val)
 {
     Object temp = val;
     Type elementtype = array.GetType().GetElementType();
     if (elementtype != val.GetType())
         temp = Convert.ChangeType(val, elementtype);
     if (array.Length == 0)
         throw (new NullReferenceException());
     if (fromindex > toindex)
         throw (new ArgumentException());
     if ((fromindex < 0) || (array).Length < toindex)
         throw (new IndexOutOfRangeException());
     for (int index = (fromindex > 0) ? fromindex-- : fromindex; index < toindex; index++)
         array.SetValue(temp, index);
 }
        /// <summary>
        /// Build array item bindings for the particular array.
        /// </summary>
        public IArrayItemBinding[] CreateArrayBindings(Array array)
        {
            Argument.NotNull(() => array);

            var childPropertyBindings = new List<IArrayItemBinding>();
            var objType = array.GetType();

            int i = 0;

            foreach (var item in array)
            {
                childPropertyBindings.Add(CreateArrayBinding(item, i, array));

                ++i;
            }

            return childPropertyBindings.ToArray();
        }
		/// <summary>
		/// Concatenates two arrays.
		/// </summary>
		/// <param name="l">Left array</param>
		/// <param name="r">Right array</param>
		/// <returns>Array containing both left and right arrays.</returns>
		public static Array ConcatenateArrays(Array l, Array r) 
		{
			if (l == null)
			{
				throw new ArgumentNullException("l");
			}
			if (r == null)
			{
				throw new ArgumentNullException("r");
			}

			int len = l.Length + r.Length;
			Array a = Array.CreateInstance(l.GetType(), len);

			Array.Copy(l, 0, a, 0, l.Length);
			Array.Copy(r, 0, a, l.Length, r.Length);

			return a;
		}
Example #29
0
 public static object GetArraySlice( Array inputarray, int sliceindex )
 {
     int numoutputdimensions = inputarray.Rank - 1;
     int[] newdimensions = new int[ numoutputdimensions ];
     for( int i = 1; i < numoutputdimensions + 1; i++ )
     {
         newdimensions[ i - 1 ] = inputarray.GetUpperBound( i ) + 1;
     }
     Array newarray = Array.CreateInstance( inputarray.GetType().GetElementType(), newdimensions );
     
     int[]traverseinputindex = new int[ numoutputdimensions + 1 ];
     int[]traverseoutputindex = new int[ numoutputdimensions ];
     traverseinputindex[0] = sliceindex;
     bool bDone = false;
     while( !bDone )
     {
         newarray.SetValue( inputarray.GetValue( traverseinputindex ), traverseoutputindex );
         bool bUpdatedtraverseindex = false;
         for( int i = numoutputdimensions - 1; i >= 0 && !bUpdatedtraverseindex; i-- )
         {
             traverseinputindex[i + 1]++;
             traverseoutputindex[i]++;
             if( traverseoutputindex[i] >= newdimensions[i] )
             {
                 if( i == 0 )
                 {
                     bDone = true;
                 }
                 else
                 {
                     traverseinputindex[i + 1] = 0;
                     traverseoutputindex[i ] = 0; 
                 }
             }
             else
             {
                 bUpdatedtraverseindex = true;
             }
         }
     }
     
     return newarray;
 }
Example #30
0
        public static Array Add(Array aFirst, Array aSecond)
        {
            if (aFirst ==  null)
            {
                return aSecond.Clone() as Array;
            }

            if (aSecond == null)
            {
                return aFirst.Clone() as Array;
            }

            Type typeFirst = aFirst.GetType().GetElementType();
            Type typeSecond = aSecond.GetType().GetElementType();

            System.Diagnostics.Debug.Assert(typeFirst == typeSecond);

            Array aNewArray = Array.CreateInstance(typeFirst, aFirst.Length + aSecond.Length);
            aFirst.CopyTo(aNewArray, 0);
            aSecond.CopyTo(aNewArray, aFirst.Length);

            return aNewArray;
        }