첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ Program.cs

using System;

namespace TestProject
{
    /// <summary>
    /// 프로그램
    /// </summary>
    class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 프로그램 시작하기 - Main()

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static void Main()
        {
            PriorityQueue<TestItem> queue = new PriorityQueue<TestItem>(5);

            TestItem item;

            queue.EnqueueItem(item = new TestItem { Priority = 0, ID = "01" });
            
            Console.WriteLine("ENQUEUE ITEM : {0}, {1}", item.Priority, item.ID);

            queue.EnqueueItem(item = new TestItem { Priority = 1, ID = "02" });
            
            Console.WriteLine("ENQUEUE ITEM : {0}, {1}", item.Priority, item.ID);

            queue.EnqueueItem(item = new TestItem { Priority = 2, ID = "03" });
            
            Console.WriteLine("ENQUEUE ITEM : {0}, {1}", item.Priority, item.ID);

            queue.EnqueueItem(item = new TestItem { Priority = 2, ID = "04" });
            
            Console.WriteLine("ENQUEUE ITEM : {0}, {1}", item.Priority, item.ID);

            queue.EnqueueItem(item = new TestItem { Priority = 3, ID = "05" });
            
            Console.WriteLine("ENQUEUE ITEM : {0}, {1}", item.Priority, item.ID);

            queue.EnqueueItem(item = new TestItem { Priority = 4, ID = "06" });
            
            Console.WriteLine("ENQUEUE ITEM : {0}, {1}", item.Priority, item.ID);

            queue.EnqueueItem(item = new TestItem { Priority = 4, ID = "07" });
            
            Console.WriteLine("ENQUEUE ITEM : {0}, {1}", item.Priority, item.ID);

            item = queue.DequeueItem();
            
            Console.WriteLine("DEQUEUE ITEM : {0}, {1}", item.Priority, item.ID);

            item = queue.DequeueItem();
            
            Console.WriteLine("DEQUEUE ITEM : {0}, {1}", item.Priority, item.ID);

            item = queue.DequeueItem();
            
            Console.WriteLine("DEQUEUE ITEM : {0}, {1}", item.Priority, item.ID);

            item = queue.DequeueItem();
            
            Console.WriteLine("DEQUEUE ITEM : {0}, {1}", item.Priority, item.ID);

            item = queue.DequeueItem();
            
            Console.WriteLine("DEQUEUE ITEM : {0}, {1}", item.Priority, item.ID);

            item = queue.DequeueItem();
            
            Console.WriteLine("DEQUEUE ITEM : {0}, {1}", item.Priority, item.ID);

            item = queue.DequeueItem();
            
            Console.WriteLine("DEQUEUE ITEM : {0}, {1}", item.Priority, item.ID);
        }

        #endregion
    }
}

 

▶ IPriorityItem.cs

namespace TestProject
{
    /// <summary>
    /// 우선 순위 항목 인터페이스
    /// </summary>
    public interface IPriorityItem
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

        #region 우선 순위 - Priority

        /// <summary>
        /// 우선 순위
        /// </summary>
        int Priority { get; set; }

        #endregion
    }
}

 

▶ PriorityQueue.cs

using System.Collections.Generic;

namespace TestProject
{
    /// <summary>
    /// 우선 순위 큐
    /// </summary>
    /// <typeparam name="T">항목 타입</typeparam>
    public class PriorityQueue<T> : Dictionary<int, ListQueue<T>> where T : IPriorityItem
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Protected

        #region Field

        /// <summary>
        /// 동기 객체
        /// </summary>
        protected object syncObject;

        /// <summary>
        /// 우선 순위 카운트
        /// </summary>
        protected int priorityCount;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 우선 순위 카운트 - PriorityCount

        /// <summary>
        /// 우선 순위 카운트
        /// </summary>
        public int PriorityCount
        {
            get
            {
                return this.priorityCount;
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - PriorityQueue(priorityCount)

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="priorityCount">우선 순위 카운트</param>
        public PriorityQueue(int priorityCount)
        {
            this.syncObject    = new object();
            this.priorityCount = priorityCount;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 항목 넣기 - EnqueueItem(item)

        /// <summary>
        /// 항목 넣기
        /// </summary>
        /// <param name="item">항목</param>
        public void EnqueueItem(T item)
        {
            lock(this.syncObject)
            {
                int priority = item.Priority;

                if(priority < 0)
                {
                    priority = 0;
                }

                if(priority >= this.priorityCount)
                {
                    priority = this.priorityCount - 1;
                }

                ListQueue<T> queue;

                if(ContainsKey(priority))
                {
                    queue = this[priority];
                }
                else
                {
                    queue = new ListQueue<T>();

                    Add(priority, queue);
                }

                queue.EnqueueItem(item);
            }
        }

        #endregion
        #region 항목 꺼내기 - DequeueItem()

        /// <summary>
        /// 항목 꺼내기
        /// </summary>
        /// <returns>항목</returns>
        public T DequeueItem()
        {
            lock(this.syncObject)
            {
                for(int i = this.priorityCount - 1; i >= 0; i--)
                {
                    if(ContainsKey(i))
                    {
                        ListQueue<T> queue = this[i];

                        if(queue.Count > 0)
                        {
                            T item = queue.DequeueItem();

                            return item;
                        }
                    }
                }

                return default(T);
            }
        }

        #endregion
        #region 항목 카운트 구하기 - GetItemCount()

        /// <summary>
        /// 항목 카운트 구하기
        /// </summary>
        /// <returns>항목 카운트</returns>
        public int GetItemCount()
        {
            lock(this.syncObject)
            {
                int count = 0;

                foreach(KeyValuePair<int, ListQueue<T>> keyValuePair in this)
                {
                    count += keyValuePair.Value.Count;
                }

                return count;
            }
        }

        #endregion
        #region 데이터 지우기 - ClearData()

        /// <summary>
        /// 데이터 지우기
        /// </summary>
        public void ClearData()
        {
            lock(this.syncObject)
            {
                foreach(KeyValuePair<int, ListQueue<T>> keyValuePair in this)
                {
                    ListQueue<T> queue = keyValuePair.Value;

                    queue.ClearData();
                }

                Clear();
            }
        }

        #endregion
    }
}
728x90
반응형
그리드형
Posted by 사용자 icodebroker
TAG , ,

댓글을 달아 주세요