Example #1
0
 public ArcCollection(bool isFsa, State <TData, TOffset> state)
 {
     _state       = state;
     _isFsa       = isFsa;
     _arcs        = new List <Arc <TData, TOffset> >();
     _arcComparer = ProjectionComparer <Arc <TData, TOffset> > .Create(arc => arc.PriorityType).Reverse();
 }
Example #2
0
        static void Main()
        {
            // This uses lambda expressions and extension methods...
            IComparer <Product> comparer = ProjectionComparer <Product>
                                           .Create(p => p.Popularity).Reverse()
                                           .ThenBy(p => p.Price)
                                           .ThenBy(p => p.Name);

            // Products are sorted by popularity (highest first), price (lowest first) and then name
            Product remote     = new Product(100, 25.00m, "Remote control");
            Product mouse      = new Product(50, 10.00m, "Wireless mouse");
            Product headphones = new Product(100, 20.00m, "USB drive");
            Product wipes      = new Product(50, 10.00m, "Screen wipes");
            Product game       = new Product(150, 40.00m, "Shiny new game");

            Product[] products = { remote, mouse, headphones, wipes, game };

            Array.Sort(products, comparer);
            foreach (Product product in products)
            {
                Console.WriteLine(product);
            }

            Product otherGame = new Product(150, 40.00m, "Shiny new game");

            Console.WriteLine("Two similar products compare as 0? {0}", comparer.Compare(game, otherGame) == 0);
        }
Example #3
0
        public void ThenByWithProjection()
        {
            var data = SampleType.SampleData;
            IComparer <SampleType> primary = ProjectionComparer <SampleType> .Create(t => t.First);

            data.Sort(primary.ThenBy(t => t.Second));

            Assert.IsTrue(new[] { 2, 10, 5 }.SequenceEqual(data.Select(x => x.Second)));
        }
 public static IOrderedEnumerable <TSource> OrderBy <TSource, TKey>
 (
     this IEnumerable <TSource> source,
     Func <TSource, TKey> keySelector
 )
 {
     return(new OrderedEnumerable <TSource>
            (
                source,
                ProjectionComparer.Create(keySelector)
            ));
 }
 public static IOrderedEnumerable <TSource> OrderByDescending <TSource, TKey>
 (
     this IEnumerable <TSource> source,
     Func <TSource, TKey> keySelector
 )
 {
     return(new OrderedEnumerable <TSource>
            (
                source,
                new ReverseComparer <TSource>(ProjectionComparer.Create(keySelector))
            ));
 }
Example #6
0
        public void Equals_DifferentValues_False()
        {
            var comparer = ProjectionComparer <Foo> .Create(f => new { f.Bar, f.Baz });

            Assert.IsFalse(comparer.Equals(
                               new Foo {
                Bar = "foo", Baz = 2, Qux = DateTime.Now
            },
                               new Foo {
                Bar = "foo", Baz = 3, Qux = DateTime.Now.AddHours(-1)
            })
                           );
        }
Example #7
0
        private void LoadCollectionView()
        {
            var vm = (WordListsViewModel)DataContext;

            if (vm == null)
            {
                return;
            }

            WordListsGrid.CurrentColumn = null;
            WordListsGrid.CurrentItem   = null;
            WordListsGrid.Columns.Clear();
            var view = new DataGridCollectionView(vm.Varieties, typeof(WordListsVarietyViewModel), false, false);

            view.ItemProperties.Add(new DataGridItemProperty("Variety", ".", typeof(WordListsVarietyViewModel)));
            IComparer sortComparer = ProjectionComparer <WordListsVarietyMeaningViewModel> .Create(meaning => meaning.StrRep);

            for (int i = 0; i < vm.Meanings.Count; i++)
            {
                view.ItemProperties.Add(new DataGridItemProperty("Meaning" + i, $"Meanings[{i}]", typeof(WordListsVarietyMeaningViewModel))
                {
                    SortComparer = sortComparer
                });
            }
            vm.VarietiesView = view;
            WordListsGrid.Items.SortDescriptions.Clear();

            var headerColumn = new Column {
                FieldName = "Variety"
            };

            DataGridControlBehaviors.SetIsRowHeader(headerColumn, true);
            DataGridControlBehaviors.SetAutoSize(headerColumn, true);
            DataGridControlBehaviors.SetAutoSizePadding(headerColumn, 18);
            WordListsGrid.Columns.Add(headerColumn);
            for (int i = 0; i < vm.Meanings.Count; i++)
            {
                var column = new Column {
                    FieldName = "Meaning" + i, Width = 100, CellEditor = WordListsGrid.DefaultCellEditors[typeof(WordListsVarietyMeaningViewModel)]
                };
                var titleBinding = new Binding($"DataGridControl.DataContext.Meanings[{i}].Gloss")
                {
                    RelativeSource = RelativeSource.Self
                };
                BindingOperations.SetBinding(column, ColumnBase.TitleProperty, titleBinding);
                WordListsGrid.Columns.Add(column);
            }
        }
Example #8
0
        public void ProjectToNumberWithGenericType()
        {
            IComparer <NameAndNumber> comparer = ProjectionComparer <NameAndNumber> .Create(x => x.Number);

            TestComparisons(comparer);
        }
Example #9
0
        public void ProjectToNumberWithExplicitType()
        {
            IComparer <NameAndNumber> comparer = ProjectionComparer.Create((NameAndNumber x) => x.Number);

            TestComparisons(comparer);
        }
Example #10
0
        public void ProjectToNumberWithIgnoredParameter()
        {
            IComparer <NameAndNumber> comparer = ProjectionComparer.Create(A10, x => x.Number);

            TestComparisons(comparer);
        }
Example #11
0
        public static void Sort <TSource, TKey>(IList <TSource> arr, Func <TSource, TKey> keySelector)
        {
            var comparer = ProjectionComparer <TSource> .Create(keySelector);

            Sort(arr, comparer);
        }
Example #12
0
        private void LoadCollectionView()
        {
            var vm = (MultipleWordAlignmentViewModel)DataContext;

            AlignmentGrid.Columns.Clear();

            var view = new DataGridCollectionView(vm.Words, typeof(MultipleWordAlignmentWordViewModel), false, false);

            view.ItemProperties.Add(new DataGridItemProperty("Variety", "Variety", typeof(VarietyViewModel))
            {
                SortComparer = ProjectionComparer <VarietyViewModel> .Create(v => v.Name)
            });
            view.ItemProperties.Add(new DataGridItemProperty("StrRep", "StrRep", typeof(string)));
            view.ItemProperties.Add(new DataGridItemProperty("CognateSetIndex", "CognateSetIndex", typeof(int)));
            view.ItemProperties.Add(new DataGridItemProperty("Prefix", "Prefix", typeof(string)));
            for (int i = 0; i < vm.ColumnCount; i++)
            {
                view.ItemProperties.Add(new DataGridItemProperty("Column" + i, string.Format("Columns[{0}]", i), typeof(string)));
            }
            view.ItemProperties.Add(new DataGridItemProperty("Suffix", "Suffix", typeof(string)));
            if (vm.GroupByCognateSet)
            {
                Debug.Assert(view.GroupDescriptions != null);
                view.GroupDescriptions.Add(new DataGridGroupDescription("CognateSetIndex"));
            }
            vm.WordsView = view;

            var headerColumn = new Column {
                FieldName = "Variety"
            };

            DataGridControlBehaviors.SetIsRowHeader(headerColumn, true);
            DataGridControlBehaviors.SetAutoSize(headerColumn, true);
            DataGridControlBehaviors.SetAutoSizePadding(headerColumn, 18);
            AlignmentGrid.Columns.Add(headerColumn);

            object fontSizeObj = System.Windows.Application.Current.FindResource("PhoneticFontSize");

            Debug.Assert(fontSizeObj != null);
            var fontSize = (double)fontSizeObj;

            var prefixColumn = new Column {
                FieldName = "Prefix", ReadOnly = true, CanBeCurrentWhenReadOnly = false
            };

            DataGridControlBehaviors.SetAutoSize(prefixColumn, true);
            DataGridControlBehaviors.SetAutoSizePadding(prefixColumn, 9);
            DataGridControlBehaviors.SetFontSizeHint(prefixColumn, fontSize);
            AlignmentGrid.Columns.Add(prefixColumn);
            for (int i = 0; i < vm.ColumnCount; i++)
            {
                var column = new Column {
                    FieldName = "Column" + i
                };
                DataGridControlBehaviors.SetAutoSize(column, true);
                DataGridControlBehaviors.SetAutoSizePadding(column, 9);
                DataGridControlBehaviors.SetFontSizeHint(column, fontSize);
                AlignmentGrid.Columns.Add(column);
            }
            var suffixColumn = new Column {
                FieldName = "Suffix", ReadOnly = true, CanBeCurrentWhenReadOnly = false
            };

            DataGridControlBehaviors.SetAutoSize(suffixColumn, true);
            DataGridControlBehaviors.SetAutoSizePadding(suffixColumn, 9);
            DataGridControlBehaviors.SetFontSizeHint(suffixColumn, fontSize);
            AlignmentGrid.Columns.Add(suffixColumn);

            AlignmentGrid.CurrentItem = null;
        }
        public void Sort <TKey>(Func <T, TKey> keySelector)
        {
            var comparer = ProjectionComparer <T> .Create(keySelector);

            Sort(comparer);
        }