示例#1
0
        public List <Suit> Find(PropertyValue[] filter_propertys, int out_amount)
        {
            object      suitSetLock = new object();
            List <Suit> suitSet     = new List <Suit>();


            var rangePartitioner = System.Collections.Concurrent.Partitioner.Create(0, _Total());

            object readLock = new object();
            int    reads    = 0;

            System.Threading.SpinWait sw = new System.Threading.SpinWait();
            Parallel.ForEach(rangePartitioner, (range, loopState) =>
            {
                Regulus.Utility.TimeCounter time = new Regulus.Utility.TimeCounter();
                List <Suit> suits = new List <Suit>();
                int count         = 0;
                //var range = new {Item1 = 0 , Item2 = _Total()};
                for (long i = range.Item2 - 1; i >= range.Item1; --i, ++count)
                {
                    int[] indexs = _GetIndexs(i);
                    var s        = new Suit(
                        _GetCard(0, indexs[0]),
                        _GetCard(1, indexs[1]),
                        _GetCard(2, indexs[2]),
                        _GetCard(3, indexs[3]),
                        _GetCard(4, indexs[4]),
                        _GetCard(5, indexs[5]),
                        _GetCard(6, indexs[6]),
                        _GetCard(7, indexs[7]));

                    bool pass = (from filter in filter_propertys
                                 where s.GetValue(filter) < filter.Value
                                 select false).Count() == 0;
                    sw.SpinOnce();
                    if (pass)
                    {
                        suits.Add(s);
                    }

                    if (time.Second > 1)
                    {
                        lock (readLock)
                        {
                            reads += count;
                            count  = 0;
                        }

                        _UpdateSet(ref suitSet, filter_propertys, out_amount, suitSetLock, reads, suits);
                        suits.Clear();
                        time.Reset();

                        sw.SpinOnce();
                    }
                }

                lock (readLock)
                {
                    reads += count;
                    count  = 0;
                }
                _UpdateSet(ref suitSet, filter_propertys, out_amount, suitSetLock, reads, suits);
            });



            var result = suitSet.OrderByDescending((suit) => suit.GetValue(filter_propertys[0]));

            foreach (var property in filter_propertys.Skip(1))
            {
                result = result.ThenByDescending((suit) => suit.GetValue(property));
            }

            return(result.ToList());
        }
        private static List <Suit> _SingThread(PropertyValue[] filterPropertys, int outAmount, Property[] propertys, Card[] cards1, Card[] cards2, Card[] cards3, Card[] cards4, Card[] cards5, Card[] cards6, Card[] cards7, Card[] cards8, System.Int64 total)
        {
            int count = 0;

            Regulus.Utility.TimeCounter timeCounter = new Regulus.Utility.TimeCounter();

            List <Suit> suits = new List <Suit>();


            foreach (var card1 in cards1)
            {
                foreach (var card2 in cards2)
                {
                    foreach (var card3 in cards3)
                    {
                        foreach (var card4 in cards4)
                        {
                            foreach (var card5 in cards5)
                            {
                                foreach (var card6 in cards6)
                                {
                                    foreach (var card7 in cards7)
                                    {
                                        foreach (var card8 in cards8)
                                        {
                                            if (timeCounter.Second > 1)
                                            {
                                                var orders = suits.OrderBy((suit) => suit.GetValue(propertys[0]));
                                                foreach (var property in propertys.Skip(1))
                                                {
                                                    orders = orders.ThenBy((suit) => suit.GetValue(property));
                                                }

                                                suits = orders.ToList();

                                                if (suits.Count > outAmount)
                                                {
                                                    suits.RemoveRange(0, suits.Count - outAmount);
                                                }

                                                System.Console.WriteLine(string.Format("{0}/{1}...{2}", count, total, suits.Count));

                                                timeCounter.Reset();
                                            }

                                            var s = new Suit(card1, card2, card3, card4, card5, card6, card7, card8);

                                            bool pass = (from filter in filterPropertys
                                                         where s.GetValue(filter) < filter.Value
                                                         select false).Count() == 0;

                                            if (pass)
                                            {
                                                suits.Add(s);
                                            }
                                            count++;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            var result = suits.OrderByDescending((suit) => suit.GetValue(propertys[0]));

            foreach (var property in propertys.Skip(1))
            {
                result = result.ThenByDescending((suit) => suit.GetValue(property));
            }

            suits = result.ToList();
            return(suits);
        }