Example #1
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;
        }
Example #2
0
 //�ΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡ��������Ӻ���
 public static Array Join(Array list1, Array list2, Type arrayType)
 {
     Array list = null;
     if (list1 != null && list2 != null)
     {
         list = Activator.CreateInstance(arrayType, list1.Length + list2.Length) as Array;
         if (list != null)
         {
             list1.CopyTo(list, 0);
             list2.CopyTo(list, list1.Length);
         }
     }
     return list;
 }
Example #3
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;
        }
Example #4
0
		void ICollection.CopyTo (Array array, int index)
		{
			// assuming that Nodes is a correct collection.
			array.CopyTo (Nodes.ToArray (typeof(XmlAttribute)), index);
		}
 public void CopyTo(Array array, int index)
 {
     array.CopyTo(array, index);
 }
Example #6
0
 /// <summary>
 /// Fills dest with a rotated version of source.
 /// </summary>
 /// <param name="source">The source array.</param>
 /// <param name="dest">The dest array, which must have the same length and underlying type
 /// as source.</param>
 /// <param name="rotation">The number of elements to rotate to the left by.</param>
 private static void RotateArrayLeft(Array source, Array dest, int rotation)
 {
     if (source.Length != dest.Length)
     {
         throw new ArgumentException("source and dest lengths differ.");
     }
     if (rotation == 0)
     {
         source.CopyTo(dest, 0);
     }
     else
     {
         for (int i = 0; i < source.Length; ++i)
         {
             dest.SetValue(source.GetValue((rotation + i) % source.Length), i);
         }
     }
 }
Example #7
0
 public static object[] AppendToArray(Array currentListeners, object listener) {
     var elemType = currentListeners.GetType().GetElementType();
     var newListeners = Array.CreateInstance(elemType, currentListeners.Length + 1);
     currentListeners.CopyTo(newListeners, 0);
     newListeners.SetValue(listener, currentListeners.Length);
     return (object[]) newListeners;
 }
Example #8
0
        public void AcquireWriteLock_ReadWrite_LocksCorrectly()
        {
            var name = Guid.NewGuid().ToString();
            Random r = new Random();
            int bufSize = 1024;
            byte[] data = new byte[bufSize];
            byte[] readBuf = new byte[bufSize];

            bool readIsFirst = false;
            bool readBlocked = false;
            int syncValue = 0;

            // Fill with random data
            r.NextBytes(data);

            using (var sma = new Array<byte>(name, bufSize))
            {
                // Acquire write lock early
                sma.AcquireWriteLock();
                using (var smr = new Array<byte>(name))
                {
                    var t1 = Task.Factory.StartNew(() =>
                        {
                            if (System.Threading.Interlocked.Exchange(ref syncValue, 1) == 0)
                                readIsFirst = true;
                            // Should block until write lock is released
                            smr.AcquireReadLock();
                            if (System.Threading.Interlocked.Exchange(ref syncValue, 3) == 4)
                                readBlocked = true;
                            smr.CopyTo(readBuf);
                            smr.ReleaseReadLock();
                        });

                    System.Threading.Thread.Sleep(10);

                    var t2 = Task.Factory.StartNew(() =>
                        {
                            var val = System.Threading.Interlocked.Exchange(ref syncValue, 2);
                            if (val == 0)
                                readIsFirst = false;
                            else if (val == 3)
                                readBlocked = false;
                            System.Threading.Thread.Sleep(10);
                            sma.Write(data);
                            System.Threading.Interlocked.Exchange(ref syncValue, 4);
                            sma.ReleaseWriteLock();
                        });

                    Task.WaitAll(t1, t2);

                    Assert.IsTrue(readIsFirst, "The read thread did not enter first.");
                    Assert.IsTrue(readBlocked, "The read thread did not block.");

                    // Check data was written before read
                    for (var i = 0; i < readBuf.Length; i++)
                    {
                        Assert.AreEqual(data[i], readBuf[i]);
                    }
                }
            }
        }
Example #9
0
 public void CopyTo_NullArray_ThrowsException()
 {
     var name = Guid.NewGuid().ToString();
     using (var sma = new Array<int>(name, 10))
     {
         bool exceptionThrown = false;
         try
         {
             sma.CopyTo(null);
         }
         catch (ArgumentNullException)
         {
             exceptionThrown = true;
         }
         Assert.IsTrue(exceptionThrown, "null buffer should result in ArgumentNullException");
     }
 }
Example #10
0
 /// <summary>
 /// Creates an <see cref="InPredicate"/>.
 /// </summary>
 /// <param name="field">Database field.</param>
 /// <param name="values">List of values.</param>
 /// <param name="negate">Specifies whether the predicate is negated (NOT IN).</param>
 /// <returns>InPredicate.</returns>
 /// <remarks>Creates an IN predicate that determines if a field matches any value in a list.</remarks>
 public static InPredicate In(IDbColumn field, Array values, bool negate)
 {
     object[] objValues = new object[values.Length];
     values.CopyTo(objValues, 0);
     return In(field, objValues, negate);
 }
Example #11
0
 public void setRow(int row, Array vals)
 {
     vals.CopyTo (boxes, row * 4);
 }
Example #12
0
        public void TestWriteToAndReadFrom()
        {
            using (var map = new MemoryMapStream())
            {
                using (var memoryStream = new MemoryStream())
                {
                    using (var array = new Array<int>(map, 10))
                    {
                        for (var i = 0; i < array.Length; i++)
                        {
                            array[i] = i + 100;
                        }

                        array.CopyTo(memoryStream);
                        memoryStream.Seek(0, SeekOrigin.Begin);

                        using (var array1 = new Array<int>(map, array.Length))
                        {
                            array1.CopyFrom(memoryStream);
                            for (var i = 0; i < array.Length; i++)
                            {
                                Assert.AreEqual(array[i], array1[i]);
                            }
                        }
                    }
                }
            }

            using (var map = new MemoryMapStream())
            {
                using (var memoryStream = new MemoryStream())
                {
                    using (var array = new Array<int>(map, 10000, 32, 32, 2))
                    {
                        for (var i = 0; i < array.Length; i++)
                        {
                            array[i] = i + 100;
                        }

                        array.CopyFrom(memoryStream);
                        memoryStream.Seek(0, SeekOrigin.Begin);

                        using (var array1 = new Array<int>(map, array.Length))
                        {
                            array.CopyFrom(memoryStream);
                            for (var i = 0; i < array.Length; i++)
                            {
                                Assert.AreEqual(array[i], array1[i]);
                            }
                        }
                    }
                }
            }
        }