Beispiel #1
0
        public void TestElementAndResultSelector()
        {
            var originalInputs = new Tuple <int, float>[]
            {
                Tuple.Create(1, 1.1f),
                Tuple.Create(2, 2.2f),
                Tuple.Create(1, 1.2f),
                Tuple.Create(2, 2.3f),
                Tuple.Create(1, 1.8f)
            };

            Tuple <int, float>[] results = originalInputs.GroupBy(
                t => t.Item1,
                t => t.Item2 * 2,
                (key, elements) => Tuple.Create(key, elements.Sum()))
                                           .OrderBy(t => t.Item1)
                                           .ToArray();

            Assert.AreEqual(2, results.Length);

            Assert.AreEqual(1, results[0].Item1);
            Assert.AreEqual(8.2f, results[0].Item2, 0.001);

            Assert.AreEqual(2, results[1].Item1);
            Assert.AreEqual(9.0f, results[1].Item2, 0.001);
        }
        private IList <List <int> > GetGroups(TState[] states)
        {
            var positions = new Tuple <TState, int> [states.Length];

            for (int i = 0; i < states.Length; i++)
            {
                positions[i] = new Tuple <TState, int>(states[i], i);
            }

            return(positions.GroupBy(x => x.Item1, x => x.Item2).Select(x => x.ToList()).ToList());
        }
Beispiel #3
0
 // subItems: tuple<XML, inheritsFrom>[]
 protected void WriteTypedSubItemTable(Tuple<XElement, string>[] subItems, string tableName, TextWriter writer, DocItemWriterContext context)
 {
     var describedSubItems = subItems
         .GroupBy(x => x.Item1.Attribute("name").Value)
         .ToDictionary(
             g => g.Key,
             g => g.Select(x =>
                 Tuple.Create(
                     x.Item1.Element("document").Element("member"),
                     x.Item1.Element("type").Value,
                     x.Item2
                     )
                 ).ToArray()
             );
     WriteTypedSubItemTable(describedSubItems, tableName, writer, context, true);
 }
        private void VerifySchedule(Tuple<DayOfWeek, TimeSpan>[] scheduleData, DateTime now)
        {
            WeeklySchedule schedule = new WeeklySchedule();
            foreach (var occurrence in scheduleData)
            {
                schedule.Add(occurrence.Item1, occurrence.Item2);
            }

            var expectedSchedule = scheduleData.GroupBy(p => p.Item1);

            // loop through the full schedule a few times, ensuring we cross over
            // a month boundary ensuring day handling is correct
            for (int i = 0; i < 10; i++)
            {
                // run through the entire schedule once, ordering the expected times per day
                foreach (var expectedScheduleDay in expectedSchedule)
                {
                    foreach (TimeSpan time in expectedScheduleDay.OrderBy(p => p.Item2).Select(p => p.Item2))
                    {
                        DateTime nextOccurrence = schedule.GetNextOccurrence(now);
                        Assert.Equal(expectedScheduleDay.Key, nextOccurrence.DayOfWeek);
                        Assert.Equal(time, nextOccurrence.TimeOfDay);
                        now = nextOccurrence + TimeSpan.FromSeconds(1);
                    }
                }
            }
        }
            private static Tuple<int, int>[] IOLinks_PostAdjust(Tuple<int, int>[] initial, Set2D[] brainSets, Item2D[] io)
            {
                // Get the links and distances between brain sets
                Tuple<int, double>[][] brainLinks = IOLinks_PostAdjust_BrainBrainLinks(brainSets);

                // Turn the initial links into something more usable (list of links by brain)
                BrainBurden[] ioLinks = initial.
                    GroupBy(o => o.Item1).
                    Select(o => new BrainBurden(o.Key, o.Select(p => p.Item2))).
                    ToArray();

                // Make sure there is an element in ioLinks for each brainset (the groupby logic above only grabs brainsets that have links)
                int[] missing = Enumerable.Range(0, brainLinks.Length).Where(o => !ioLinks.Any(p => p.Index == o)).ToArray();

                ioLinks = UtilityCore.Iterate(ioLinks, missing.Select(o => new BrainBurden(o, Enumerable.Empty<int>()))).ToArray();

                // Move one link at a time
                while (true)
                {
                    // Figure out how burdened each brain set is
                    foreach (BrainBurden brain in ioLinks)
                    {
                        brain.Recalc(brainSets, io);
                    }

                    // Find the best link to move
                    if (!IOLinks_PostAdjust_MoveNext(ioLinks, brainLinks, brainSets, io))
                    {
                        break;
                    }
                }


                // Just build it manually
                //return ioLinks.Select(o => new { BrainIndex = o.Index, UtilityCore.Iterate(o.LinksOrig, o.LinksMoved)

                //var test = ioLinks.
                //    Select(o => new { BrainIndex = o.Index, Links = UtilityCore.Iterate(o.LinksOrig, o.LinksMoved).ToArray() }).
                //    ToArray();


                List<Tuple<int, int>> retVal = new List<Tuple<int, int>>();

                foreach (BrainBurden brain in ioLinks)
                {
                    foreach (int ioIndex in UtilityCore.Iterate(brain.LinksOrig, brain.LinksMoved))
                    {
                        retVal.Add(Tuple.Create(brain.Index, ioIndex));
                    }
                }

                return retVal.ToArray();


                //return initial;
            }