Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

ตรวจสอบว่า SortedSet เป็น Superset ของคอลเลกชันที่ระบุใน C#

เมธอด IsSupersetOf(IEnumerable<T> other) ของ SortedSet<T> คืนค่า true หาก SortedSet ปัจจุบันมีองค์ประกอบ **ครบถ้วนของ other** (อาจมีเพิ่มเติมได้) - คือ Superset ในความหมายทางคณิตศาสตร์ (A ⊇ B)

ตัวอย่างที่ 1: SortedSet<string>

using System;
using System.Collections.Generic;

public class Demo
{
    public static void Main()
    {
        SortedSet<string> set1 = new SortedSet<string>();
        set1.Add("CD");
        set1.Add("CD"); // ซ้ำ - ถูกละเว้น
        set1.Add("CD");
        set1.Add("CD");

        Console.WriteLine("องค์ประกอบใน SortedSet1...");
        foreach (string res in set1)
        {
            Console.WriteLine(res);
        }

        SortedSet<string> set2 = new SortedSet<string>();
        set2.Add("BC");
        set2.Add("CD");
        set2.Add("DE");
        set2.Add("EF");
        set2.Add("AB");
        set2.Add("HI");
        set2.Add("JK");

        Console.WriteLine("องค์ประกอบใน SortedSet2...");
        foreach (string res in set2)
        {
            Console.WriteLine(res);
        }

        // set2 มี "CD" (สมาชิกของ set1) และมีเพิ่มเติม -> Superset
        Console.WriteLine("SortedSet2 เป็น Superset ของ SortedSet1? = " + set2.IsSupersetOf(set1));
    }
}

ผลลัพธ์ (เรียงลำดับอัตโนมัติ)

องค์ประกอบใน SortedSet1...
CD
องค์ประกอบใน SortedSet2...
AB
BC
CD
DE
EF
HI
JK
SortedSet2 เป็น Superset ของ SortedSet1? = True

ตัวอย่างที่ 2: SortedSet<int>

using System;
using System.Collections.Generic;

public class Demo
{
    public static void Main()
    {
        SortedSet<int> set1 = new SortedSet<int>();
        set1.Add(10);
        set1.Add(20);
        set1.Add(30);
        set1.Add(40);
        set1.Add(50);
        set1.Add(60);

        Console.WriteLine("องค์ประกอบใน SortedSet1...");
        foreach (int res in set1)
        {
            Console.WriteLine(res);
        }

        SortedSet<int> set2 = new SortedSet<int>();
        set2.Add(10);
        set2.Add(20);
        set2.Add(30);
        set2.Add(40);
        set2.Add(50);
        set2.Add(60);
        set2.Add(70);
        set2.Add(80);
        set2.Add(90);
        set2.Add(100);

        Console.WriteLine("องค์ประกอบใน SortedSet2...");
        foreach (int res in set2)
        {
            Console.WriteLine(res);
        }

        // set2 มีทุกตัวของ set1 และมีเพิ่ม -> Superset
        Console.WriteLine("SortedSet2 เป็น Superset ของ SortedSet1? = " + set2.IsSupersetOf(set1));
    }
}

ผลลัพธ์

องค์ประกอบใน SortedSet1...
10
20
30
40
50
60
องค์ประกอบใน SortedSet2...
10
20
30
40
50
60
70
80
90
100
SortedSet2 เป็น Superset ของ SortedSet1? = True

ตารางเปรียบเทียบ Set Operations

เมธอดสัญลักษณ์ความหมายตัวอย่าง A={1,2,3}, B={1,2}
IsSubsetOfA ⊆ BA เป็นสับเซตของ B (อาจเท่ากันได้)False
IsProperSubsetOfA ⊂ BA เป็นโพรเพอร์สับเซต (ต้องไม่เท่ากัน)False
IsSupersetOfA ⊇ BA เป็นซูเปอร์เซตของ B (อาจเท่ากันได้)True
IsProperSupersetOfA ⊃ BA เป็นโพรเพอร์ซูเปอร์เซต (ต้องไม่เท่ากัน)True
OverlapsA ∩ B ≠ ∅มีสมาชิกร่วมกันอย่างน้อย 1 ตัวTrue
SetEqualsA = BสมาชิกเหมือนกันทุกตัวFalse

ข้อสังเกต

  • Superset (IsSupersetOf) รวมถึงกรณีที่เท่ากัน (A = B → True)
  • Proper Superset (IsProperSupersetOf) ต้องมีสมาชิกมากกว่า (A = B → False)
  • SortedSet ใช้ Red-Black Tree ให้ O(log n) สำหรับ Contains, Add, Remove
  • รักษาลำดับเรียงอัตโนมัติ (In-Order Traversal ให้ลำดับสำเร็จรูป)
  • ต้องการ T implements IComparable<T> หรือระบุ IComparer<T>