Example #1
0
 public static void ForEach(ForEachDelegate callback)
 {
     foreach (BaseObject type in registar)
     {
         callback(type);
     }
 }
Example #2
0
        internal void Run(int threadCount, ForEachDelegate forEachDelegate)
        {
            Console.WriteLine();
            Console.WriteLine("Testing dictionaries with locks that don't allow duplicate values to be created");
            int count = _dictionariesNoDuplicates.Length;

            for (int i = 0; i < count; i++)
            {
                Program._Measure(threadCount, forEachDelegate, _dictionariesNoDuplicates[i]);
                if (_singleUse)
                {
                    _dictionariesNoDuplicates[i] = null;
                }
            }

            Console.WriteLine();
            Console.WriteLine("Testing dictionaries with locks that allow duplicate values to be created");
            count = _dictionariesDuplicates.Length;
            for (int i = 0; i < count; i++)
            {
                Program._Measure(threadCount, forEachDelegate, _dictionariesDuplicates[i]);
                if (_singleUse)
                {
                    _dictionariesDuplicates[i] = null;
                }
            }
        }
 public static void ReversedForEach <T>(this List <T> list, ForEachDelegate <T> callback)
 {
     for (var i = list.Count - 1; i >= 0; i--)
     {
         callback(list[i], i);
     }
 }
Example #4
0
        public static void ForEach <T>(IEnumerable <T> enumerable, ForEachDelegate <T> action)
        {
            IEnumerator <T> enumerator = enumerable.GetEnumerator();
            object          oLock      = new object();

            ProcessDelegate process = delegate() {
                while (true)
                {
                    T current;

                    lock (oLock) {
                        if (!enumerator.MoveNext())
                        {
                            return;
                        }
                        current = enumerator.Current;
                    }

                    action(current);
                }
            };

            int threads = Environment.ProcessorCount;

            WaitHandle[] handles = new WaitHandle[threads];
            for (int i = 0; i < threads; i++)
            {
                handles[i] = process.BeginInvoke(ProcessCallback, process).AsyncWaitHandle;
            }
            WaitHandle.WaitAll(handles);
        }
 public static void ForEach <T>(this List <T> list, ForEachDelegate <T> callback)
 {
     for (var i = 0; i < list.Count; i++)
     {
         callback(list[i], i);
     }
 }
Example #6
0
 public static void ForEach(ForEachDelegate callback)
 {
     foreach (BaseType type in registar.Values)
     {
         callback(type);
     }
 }
Example #7
0
 /// <summary>
 /// Performs the specified action on each element of the
 /// <see cref="elements"/> collection.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="elements">
 /// A collection of elements to perform the <see cref="ForEachDelegate{T}"/>
 /// action over each element.
 /// </param>
 /// <param name="action">
 /// The <see cref="ForEachDelegate{T}"/> delegate to perform on each
 /// element of <paramref name="elements"/> collection.
 /// </param>
 /// <remarks>
 /// The <see cref="ForEachDelegate{T}"/> is a delegate to a method that
 /// performs an action on the object passed to it. The elements of the
 /// <paramref name="elements"/> are individually passed to the
 /// <see cref="ForEachDelegate{T}"/> delegate.
 /// <para>
 /// This method is a O(n) operation, where n is the number of elements
 /// of the <paramref name="elements"/> collection.
 /// </para>
 /// </remarks>
 public JsonStringBuilder ForEach <T>(IEnumerable <T> elements,
                                      ForEachDelegate <T> action)
 {
     foreach (T element in elements)
     {
         action(element, this);
     }
     return(this);
 }
Example #8
0
        static void MyForEach <T>(IEnumerable <T> collection, ForEachDelegate <T> body)
        {
            var it = collection.GetEnumerator();

            while (it.MoveNext())
            {
                body(it.Current);
            }
        }
 /// <summary>
 /// Calls the delegate once for each key/value pair in the dictionary.
 /// </summary>
 public void ForEach(ForEachDelegate code)
 {
     foreach (TKey key in Keys)
     {
         foreach (TValue value in this[key])
         {
             code(key, value);
         }
     }
 }
 public static void ForEach(IEnumerable list, ForEachDelegate <object> forEachProc)
 {
     if (!CollectionUtils.IsNullOrEmpty(list))
     {
         foreach (object obj in list)
         {
             forEachProc(obj);
         }
     }
 }
Example #11
0
        internal static void _Measure(int threadCount, ForEachDelegate forEachDelegate, IGetOrCreateValueSample sample)
        {
            Console.Write(sample.Message);

            using (var startCountdown = new CountdownEvent(threadCount))
            {
                using (var canStartProcessingEvent = new ManualResetEvent(false))
                {
                    using (var finishCountdown = new CountdownEvent(threadCount))
                    {
                        for (int threadIndex = 0; threadIndex < threadCount; threadIndex++)
                        {
                            var thread =
                                new Thread
                                (
                                    (p) =>
                            {
                                int i = (int)p;
                                startCountdown.Signal();
                                canStartProcessingEvent.WaitOne();
                                // tell that the thread is ready and wait for all threads to be ready
                                // before starting. We should not start before all threads are created
                                // and we should not consider the time to create the thread on our results.

                                forEachDelegate(i, sample);

                                finishCountdown.Signal();
                            }
                                );

                            thread.Start(threadIndex);
                        }

                        // wait until all threads are created to start the stopwatch.
                        startCountdown.Wait();
                        var stopwatch = new Stopwatch();
                        stopwatch.Start();
                        canStartProcessingEvent.Set();
                        // now we let all threads run. We should wait until all of them
                        // finish to know the real result.
                        finishCountdown.Wait();
                        stopwatch.Stop();

                        Console.WriteLine(stopwatch.Elapsed);
                    }
                }
            }
        }
Example #12
0
        internal void Run(int threadCount, ForEachDelegate forEachDelegate)
        {
			Console.WriteLine();
			Console.WriteLine("Testing dictionaries with locks that don't allow duplicate values to be created");
			int count = _dictionariesNoDuplicates.Length;
			for(int i=0; i<count; i++)
			{
				Program._Measure(threadCount, forEachDelegate, _dictionariesNoDuplicates[i]);
	            if (_singleUse)
					_dictionariesNoDuplicates[i] = null;
			}

			Console.WriteLine();
			Console.WriteLine("Testing dictionaries with locks that allow duplicate values to be created");
			count = _dictionariesDuplicates.Length;
			for(int i=0; i<count; i++)
			{
				Program._Measure(threadCount, forEachDelegate, _dictionariesDuplicates[i]);
	            if (_singleUse)
					_dictionariesDuplicates[i] = null;
			}
        }
        public static void AllocationFreeForEach(TEnumerable instance, ref TState state, ForEachDelegate itemCallback)
        {
            Debug.Assert(instance != null && itemCallback != null);

            var type = instance.GetType();

            var allocationFreeForEachDelegate = AllocationFreeForEachDelegates.GetOrAdd(
                type,
                BuildAllocationFreeForEachDelegateRef);

            allocationFreeForEachDelegate(instance, ref state, itemCallback);
        }
Example #14
0
		internal static void _Measure(int threadCount, ForEachDelegate forEachDelegate, IGetOrCreateValueSample sample)
		{
			Console.Write(sample.Message);

			using(var startCountdown = new CountdownEvent(threadCount))
			{
				using(var canStartProcessingEvent = new ManualResetEvent(false))
				{
					using(var finishCountdown = new CountdownEvent(threadCount))
					{
						for(int threadIndex=0; threadIndex<threadCount; threadIndex++)
						{
							var thread = 
								new Thread
								(
									(p) =>
									{
                                        int i = (int)p;
										startCountdown.Signal();
										canStartProcessingEvent.WaitOne();
                                        // tell that the thread is ready and wait for all threads to be ready
                                        // before starting. We should not start before all threads are created
                                        // and we should not consider the time to create the thread on our results.

                                        forEachDelegate(i, sample);

										finishCountdown.Signal();
									}	
								);

							thread.Start(threadIndex);
						}

                        // wait until all threads are created to start the stopwatch.
						startCountdown.Wait();
						var stopwatch = new Stopwatch();
						stopwatch.Start();
						canStartProcessingEvent.Set();
                        // now we let all threads run. We should wait until all of them
                        // finish to know the real result.
						finishCountdown.Wait();
						stopwatch.Stop();

						Console.WriteLine(stopwatch.Elapsed);
					}
				}
			}
		}
Example #15
0
        public static void ForEachMethod()
        {
            DynamicMethod foreachMethod = new DynamicMethod("ForEachRun", typeof(Int32), new Type[] { typeof(Int32[]) });
            ILGenerator   methodIL      = foreachMethod.GetILGenerator();

            //用来保存求和结果的局部变量
            LocalBuilder sum = methodIL.DeclareLocal(typeof(Int32));
            //foreach 中的 int i
            LocalBuilder i = methodIL.DeclareLocal(typeof(Int32));
            //用来保存传入的数组
            LocalBuilder ints = methodIL.DeclareLocal(typeof(Int32[]));
            //数组循环用临时变量
            LocalBuilder index = methodIL.DeclareLocal(typeof(Int32));

            Label compareLabel   = methodIL.DefineLabel();
            Label enterLoopLabel = methodIL.DefineLabel();

            //首先,它用一个局部变量保存了整个数组,并用它替换了所有原先直接使用数组的地方;
            //最后,它把sum += ints[i];的操作分解成为i = ints[index]和sum += i两个步骤

            //int sum = 0;
            methodIL.Emit(OpCodes.Ldc_I4_0);
            methodIL.Emit(OpCodes.Stloc_0);
            //ints = ints
            methodIL.Emit(OpCodes.Ldarg_0);
            methodIL.Emit(OpCodes.Stloc_2);
            //int index = 0
            methodIL.Emit(OpCodes.Ldc_I4_0);
            methodIL.Emit(OpCodes.Stloc_3);
            methodIL.Emit(OpCodes.Br, compareLabel);

            //定义一个标签,表示从下面开始进入循环体
            methodIL.MarkLabel(enterLoopLabel);
            //其中Ldelem_I4用来加载一个数组中的Int32类型的元素
            //加载 i = ints[index]
            methodIL.Emit(OpCodes.Ldloc_2);
            methodIL.Emit(OpCodes.Ldloc_3);
            methodIL.Emit(OpCodes.Ldelem_I4);
            methodIL.Emit(OpCodes.Stloc_1);
            //sum += i;
            methodIL.Emit(OpCodes.Ldloc_0);
            methodIL.Emit(OpCodes.Ldloc_1);
            methodIL.Emit(OpCodes.Add);
            methodIL.Emit(OpCodes.Stloc_0);

            //index++
            methodIL.Emit(OpCodes.Ldloc_3);
            methodIL.Emit(OpCodes.Ldc_I4_1);
            methodIL.Emit(OpCodes.Add);
            methodIL.Emit(OpCodes.Stloc_3);

            //定义一个标签,表示从下面开始进入循环的比较
            methodIL.MarkLabel(compareLabel);
            //index < ints.Length
            methodIL.Emit(OpCodes.Ldloc_3);
            methodIL.Emit(OpCodes.Ldloc_2);
            methodIL.Emit(OpCodes.Ldlen);
            methodIL.Emit(OpCodes.Conv_I4);
            methodIL.Emit(OpCodes.Clt);
            methodIL.Emit(OpCodes.Brtrue_S, enterLoopLabel);

            //return sum;
            methodIL.Emit(OpCodes.Ldloc_0);
            methodIL.Emit(OpCodes.Ret);

            //完成动态方法的创建,并且获取一个可以执行该动态方法的委托
            ForEachDelegate forRun = (ForEachDelegate)foreachMethod.CreateDelegate(typeof(ForEachDelegate));

            // 执行动态方法,将在屏幕上打印Hello World!
            int result = forRun(new int[] { 1, 2, 3, 4, 5, 5 });

            Console.WriteLine(result.ToString());
        }