C # için sıralanmış kümeler özel nesnelerle nasıl çalışır? [çiftleme]

Dec 14 2020

Özel nesneler için c # 'da sıralanmış kümelerle oynamaya çalışıyorum ve bazı nedenlerden dolayı, sıralanmış kümeler verileri depolamak için nesnelerin referanslarını kullanmıyor gibi görünüyor ..

Aşağıdaki kod parçacığında, özel sınıfın Counts özelliğine güvenmek için özel bir IComparer kullanıyorum. Ancak bazı nedenlerden dolayı bu, ekleme işlevini etkiliyor gibi görünüyor. ve counter.Add (iki) satırı, farklı bir referans olmasına ve iki özellik için farklı bir değere sahip olmasına rağmen kümeye herhangi bir ekleme yapmaz.

Bir şey mi kaçırıyorum? SortedSet'lerin C # ile nasıl çalışması gerektiği konusunda yanlış bir şey mi var?

Kod Parçacığı

    public class SortedStructureTesting
    {
        public void TestingSortedSets()
        {
            SortedSet<CounterSetup> counter = new SortedSet<CounterSetup>(new CompareCounts());

            CounterSetup one = new CounterSetup(1);
            CounterSetup two = new CounterSetup(2);
            CounterSetup three = new CounterSetup(3, 2);

            counter.Add(one);
            counter.Add(two); // Does not work. This value does not get added to the set.
            counter.Add(three);

            var max = counter.Max;
            counter.Remove(max);
            var sec = counter.Max;
            counter.Remove(sec);
        }

        public class CounterSetup
        {
            public static Random random = new Random();
            public CounterSetup(int no, int cnt = 1)
            {
                Number = no;
                Count = cnt;
                Blah = new string(Guid.NewGuid().ToString());
            }

            public int Number { get; private set; }

            public int Count { get; set; }

            public string Blah { get; private set; }
        }

        public class CompareCounts : IComparer<CounterSetup>
        {
            public int Compare(CounterSetup one, CounterSetup two)
            {
                return one.Count.CompareTo(two.Count);
            }
        }
    }

Göz atıp yardım ettiğiniz için teşekkürler!

Yanıtlar

1 DmitryBychenko Dec 14 2020 at 13:18

Peki [Sorted]Set, yalnızca farklı öğeler içerebilir ; yani Setiki eşit öğe daha olamaz. Öğeyi aşağıdakiler açısından karşılaştırırsınız (eşit olarak ele alın) Count: iki öğe aynıysa Counteşit kabul edilir. Kodunuzda

  CounterSetup one = new CounterSetup(1);         // one.Count == 1
  CounterSetup two = new CounterSetup(2);         // two.Count == 1
  CounterSetup three = new CounterSetup(3, 2);    // three.Count == 2

Sahip one.Count == two.Count == 1yüzden oneve twoolan eşit için countersıralı kümesi. Öğe eklediğinizde, ikincisi (olan two) yok sayılır:

  counter.Add(one);
  counter.Add(two); // Ignored: there's already an item (one) with the same Count
  counter.Add(three);

Ayrı kriterler istiyorsanız (biri için Equalsdiğeri sipariş içindir) HashSet, Linq'in yardımıyla sipariş edildiği gibi temsil edebileceğiniz eski güzelleri deneyebilirsiniz :

  using System.Linq;

  ...

  // Items in counter are unique (based on MyEqualityComparer)
  HashSet<CounterSetup> counter = new HashSet<CounterSetup>(
    new MyEqualityComparer()
  );

  // Now we order counter items by different criterium (here we sort by `Count`)
  var ordered = counter
    .OrderBy(item => item.Count);