스토리지

[3.24] SortedList를 통한 IComparable<T> 복습 본문

Unity/수업내용(C#)

[3.24] SortedList를 통한 IComparable<T> 복습

ljw4104 2021. 3. 24. 15:22

SortedList<T1, T2>

  • 키 값에 의해 정렬된 컬렉션
  • System.Collections.Generic 네임스페이스 안에 존재한다. (using문 선언 필수)
  • 일반리스트는 UnsortedList이다.
  • 기본형식은 SortedList<T1, T2> 이다. 내부에는 KeyValuePair 형식으로 저장이 된다.
  • 검색 시 순차접근이지만 정렬된 순서로 순차접근한다. 이 말은 값을 검색할 때 이분검색, Binary Search가 가능하다. => O(log n)

IComparable<T> (interface)

  • 이 인터페이스를 상속시킴으로서 컬렉션간의 비교 연산이 가능해진다.
  • 인터페이스이므로 public int CompareTo(T other)를 반드시 구현하여야 한다.
  • 보통 구현된 함수안에 Object.CompareTo 함수를 많이 사용한다.
값 (CompareTo가 return한 값) 의미
0보다 작음 this 인스턴스가 other 인스턴스보다 작다. (우선순위가 낮다)
0 같다.
0보다 큼 this 인스턴스가 other 인스턴스보다 크다. (우선순위가 높다)

 

using System;

namespace Study10
{
    public class Temperature : IComparable<Temperature>
    {
        private double kelvin;
        public double Kelvin
        {
            get
            {
                return this.kelvin;
            }
            set
            {
                if(value < 0.0)
                {
                    throw new ArgumentException("Temperature cannot be less than absolute zero.");
                }
                else
                {
                    this.kelvin = value;
                }
            }
        }

        public Temperature(double kelvins)
        {
            this.Kelvin = kelvins;
        }

        #region IComparable 구현
        public int CompareTo(Temperature other)
        {
            if (other == null) return 1;

            return kelvin.CompareTo(other.Kelvin);
        }
        #endregion
    }
}
using System;
using System.Collections.Generic;

namespace Study10
{
    public class App
    {
        public App()
        {
            SortedList<Temperature, string> temps = new SortedList<Temperature, string>();

            temps.Add(new Temperature(2017.15), "Boiling point of Lead");
            temps.Add(new Temperature(0), "Absolute zero");
            temps.Add(new Temperature(273.15), "Freezing point of water");
            temps.Add(new Temperature(5100.15), "Boiling point of Carbon");
            temps.Add(new Temperature(373.15), "Boiling point of water");
            temps.Add(new Temperature(600.65), "Melting point of Lead");

            foreach (var pair in temps)
            {
                Console.WriteLine("kelvin: {0}, desc: {1}", pair.Key.Kelvin, pair.Value);
            }


            var t1 = new Temperature(600.65);
            var t2 = new Temperature(5100.15);

            Console.WriteLine("======================");
            Console.WriteLine("t1 : {0}, t2 : {1}", t1.Kelvin, t2.Kelvin);
            var result = t1.CompareTo(t2);
            if(result < 0)
            {
                Console.WriteLine("t1 < t2");
            }
            else if(result > 0)
            {
                Console.WriteLine("t1 > t2");
            }
            else
            {
                Console.WriteLine("t1 == t2");
            }
        }
    }
}

Comments