บทความนี้สาธิตวิธีตรวจสอบว่า 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 ≠ ∅ |