Computer >> 컴퓨터 >  >> 프로그래밍 >> C#

C# SortedSet에서 하위 집합 구하기 – GetViewBetween() 메서드 활용법

C#의 SortedSet<T> 컬렉션에서 특정 값 범위에 해당하는 요소들만 추출해 하위 집합을 만들고 싶을 때가 있습니다. 이럴 때 사용하는 것이 바로 GetViewBetween() 메서드입니다.

GetViewBetween() 메서드란?

GetViewBetween(lowerValue, upperValue)는 SortedSet 안에서 lowerValue 이상, upperValue 이하의 값을 가진 요소들만 담고 있는 뷰(View)를 반환합니다. 반환된 결과는 단순한 복사본이 아니라 원본 집합과 연결된 뷰이므로, 원본 집합이 변경되면 뷰에도 그 변경 사항이 그대로 반영됩니다.

  • lowerValue가 upperValue보다 크면 ArgumentException이 발생합니다.
  • 뷰를 통해 요소를 추가하거나 제거하면 원본 집합에도 동일하게 적용됩니다.

예제 1: 문자열 SortedSet에서 하위 집합 얻기

using System;
using System.Collections.Generic;
public class Demo {
   public static void Main(){
      SortedSet<string> set1 = new SortedSet<string>();
      set1.Add("AB");
      set1.Add("BC");
      set1.Add("CD");
      set1.Add("EF");
      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의 요소 (열거자 사용)...");
      SortedSet<string>.Enumerator demoEnum = set2.GetEnumerator();
      while (demoEnum.MoveNext()) {
         string res = demoEnum.Current;
         Console.WriteLine(res);
      }
      // "CD"부터 "EF"까지 범위의 하위 집합(뷰) 생성
      SortedSet<string> set3 = set2.GetViewBetween("CD", "EF");
      Console.WriteLine("SortedSet3의 요소...");
      foreach (string res in set3){
         Console.WriteLine(res);
      }
   }
}

위 코드를 실행하면 다음과 같은 결과가 출력됩니다.

실행 결과

SortedSet1의 요소...
AB
BC
CD
EF
SortedSet2의 요소 (열거자 사용)...
AB
BC
CD
DE
EF
HI
JK
SortedSet3의 요소...
CD
DE
EF

출력을 보면 SortedSet2에는 7개의 요소가 있지만, GetViewBetween("CD", "EF")를 호출한 결과인 SortedSet3에는 정렬 순서상 "CD"와 "EF" 사이(경계값 포함)에 있는 "CD", "DE", "EF" 세 개의 요소만 포함되어 있는 것을 확인할 수 있습니다.

예제 2: 정수 SortedSet에서 하위 집합 얻기

이번에는 정수형 SortedSet에서 150 이상 400 이하의 요소들만 추출해 보겠습니다.

using System;
using System.Collections.Generic;
public class Demo {
   public static void Main(){
      SortedSet<int> set1 = new SortedSet<int>();
      set1.Add(50);
      set1.Add(100);
      set1.Add(150);
      set1.Add(200);
      set1.Add(250);
      set1.Add(300);
      set1.Add(350);
      set1.Add(400);
      set1.Add(450);
      set1.Add(500);
      Console.WriteLine("SortedSet1의 요소...");
      foreach (int res in set1){
         Console.WriteLine(res);
      }
      // 150 이상 400 이하 범위의 뷰 생성
      SortedSet<int> set2 = set1.GetViewBetween(150, 400);
      Console.WriteLine("SortedSet2의 요소...");
      foreach (int res in set2){
         Console.WriteLine(res);
      }
   }
}

실행 결과

SortedSet1의 요소...
50
100
150
200
250
300
350
400
450
500
SortedSet2의 요소...
150
200
250
300
350
400

원본 집합(set1)에는 10개의 요소가 있지만, 150~400 범위로 한정한 set2에는 150, 200, 250, 300, 350, 400 총 6개의 요소만 포함됩니다. 시작 값과 끝 값도 각각 '이상', '이하' 조건에 포함된다는 점에 유의하세요.

정리

C#의 SortedSet에서 하위 집합이 필요할 때는 GetViewBetween() 메서드 하나면 충분합니다. 별도의 반복문으로 일일이 필터링할 필요 없이 시작 값과 끝 값만 지정하면 해당 범위의 요소들로 구성된 뷰를 간단히 얻을 수 있으며, 내부적으로 정렬된 트리 구조를 활용하므로 성능 면에서도 효율적입니다.