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

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

บทความนี้สาธิตวิธีตรวจสอบว่า SortedSet<T> เป็น proper superset (ซูเปอร์เซตที่เหมือนกันไม่ได้) ของคอลเลกชันที่ระบุ โดยใช้เมธอด IsProperSupersetOf() ใน C#

คืออะไร Proper Superset?

เซต A เป็น proper superset ของเซต B หาก:

  • สมาชิกทุกตัวของ B อยู่ใน A (A ⊇ B)
  • A มีสมาชิกเพิ่มเติมอย่างน้อยหนึ่งตัวที่ไม่อยู่ใน B (A ≠ B)

ตัวอย่างที่ 1: SortedSet2 เป็น proper superset ของ SortedSet1

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("Elements in 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("Elements in SortedSet2...");
        foreach (int res in set2)
        {
            Console.WriteLine(res);
        }

        Console.WriteLine("SortedSet2 is a proper superset of SortedSet1? = " + set2.IsProperSupersetOf(set1));
    }
}

ผลลัพธ์

Elements in SortedSet1...
10
20
30
40
50
60
Elements in SortedSet2...
10
20
30
40
50
60
70
80
90
100
SortedSet2 is a proper superset of SortedSet1? = True

ตัวอย่างที่ 2: SortedSet1 มีสมาชิกซ้ำ (ถูกตัดทอน) และ Set2 ยังคงเป็น proper superset

using System;
using System.Collections.Generic;

public class Demo
{
    public static void Main()
    {
        SortedSet<int> set1 = new SortedSet<int>();
        set1.Add(10);
        set1.Add(10); // ซ้ำ - จะมีแค่ค่าเดียว
        set1.Add(10);
        set1.Add(10);

        Console.WriteLine("Elements in 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("Elements in SortedSet2...");
        foreach (int res in set2)
        {
            Console.WriteLine(res);
        }

        Console.WriteLine("SortedSet2 is a proper superset of SortedSet1? = " + set2.IsProperSupersetOf(set1));
    }
}

ผลลัพธ์

Elements in SortedSet1...
10
Elements in SortedSet2...
10
20
30
40
50
60
70
80
90
100
SortedSet2 is a proper superset of SortedSet1? = True

สรุป

  • ใช้ IsProperSupersetOf(IEnumerable<T> other) เพื่อตรวจสอบ proper superset
  • คืนค่า true ก็ต่อเมื่อเซตปัจจุบันมีสมาชิกครบทุกตัวของ other และ มีสมาชิกเพิ่มเติมอย่างน้อยหนึ่งตัว
  • หากสองเซตมีสมาชิกเหมือนกันทุกตัว (equal sets) จะคืนค่า false เพราะไม่ใช่ "proper"
  • SortedSet รับพารามิเตอร์เป็น IEnumerable<T> ดังนั้นจึงใช้กับ array, List, HashSet อื่นๆ ได้
  • Complexity: O(n log m) โดย n = จำนวนสมาชิกใน other, m = จำนวนสมาชิกในเซตปัจจุบัน

ตารางเปรียบเทียบเมธอดเซต

เมธอดความหมายคืนค่า true เมื่อ
IsSubsetOfเป็น subset (รวม equal)สมาชิกทุกตัวอยู่ใน other
IsProperSubsetOfเป็น proper subsetเป็น subset และไม่เท่ากับ other
IsSupersetOfเป็น superset (รวม equal)มีสมาชิกครบทุกตัวของ other
IsProperSupersetOfเป็น proper supersetเป็น superset และไม่เท่ากับ other
SetEqualsเท่ากัน (สมาชิกเหมือนกันทุกตัว)สองเซตมีสมาชิกเหมือนกันทุกตัว
Overlapsมีสมาชิกร่วมกันอย่างน้อย 1 ตัวIntersection ≠ ∅