前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Sweet Snippet 系列之 有序列表

Sweet Snippet 系列之 有序列表

作者头像
用户2615200
发布2018-08-02 17:42:29
3090
发布2018-08-02 17:42:29
举报
文章被收录于专栏:tkokof 的技术,小趣及杂念

工作中常常遇到需要使用有序列表的情况,这篇文章简单讨论一下相关实现(以 C# 中的 List<T> 为例)

使用 List<T>.Sort

很朴素的一种想法,为了维持 List 有序,我们可以在 Add 操作之后进行 Sort 操作(Remove 操作后不需要重新 Sort):

代码语言:javascript
复制
list.Add(value);
list.Sort();

该方案的缺点是时间消耗比较大,每次 Add 操作之后都要执行费时的 Sort 操作

借助平台库中的 SortedList<Tkey, TValue> etc.

使用平台库内建的 SortedList<Tkey, TValue>,我们可以立即实现有序列表功能,这也应该是我们大部分情况下的选择,稍有缺陷的是,平台库中的 SortedList 需要指定 TKey 和 TValue,这在存储非映射类数据时(譬如存储单一的 int 数值)显得有些内存浪费~ (类似的还有 SortedDictionary<TKey, TValue>)

那 SortedSet<T> 怎么样?

比起内部使用数组实现的 List 而言,目前默认使用红黑树实现的 SortedSet 会有更多的内存消耗,而且也不提供索引形式的访问,不过在插入和删除操作上,他更有时间优势~

其实我们可以自己封装

基于 List 有序这个前提,每次进行 Add 时,我们可以使用插入排序来添加元素,这样我们便可以省去之后的 Sort 操作,而 List 本身提供的 BinarySearch(二分查找)功能正好可以帮助我们实现插入排序~

代码语言:javascript
复制
// simple sorted list implementation using insert sort
// maintainer hugoyu

using System.Collections.Generic;

namespace Util
{
    public class SortedList<T>
    {
        public SortedList(IComparer<T> comparer = null)
        {
            m_comparer = comparer;
        }

        public int Count
        {
            get
            {
                return m_elementList.Count;
            }
        }

        public T this[int index]
        {
            get
            {
                return m_elementList[index];
            }
        }

        public bool Contains(T item)
        {
            return m_elementList.BinarySearch(item, m_comparer) >= 0;
        }

        public void Add(T item)
        {
            var index = m_elementList.BinarySearch(item, m_comparer);
            if (index < 0)
            {
                m_elementList.Insert(~index, item);
            }
            else
            {
                m_elementList.Insert(index, item);
            }
        }

        public bool Remove(T item)
        {
            var index = m_elementList.BinarySearch(item, m_comparer);
            if (index >= 0)
            {
                m_elementList.RemoveAt(index);
                return true;
            }

            return false;
        }

        public void Clear()
        {
            m_elementList.Clear();
        }

        IComparer<T> m_comparer;
        List<T> m_elementList = new List<T>();
    }
}

完整的代码在这里(gist)


软件开发的核心就是权衡,下次如果你需要使用有序列表,会选择怎么实现呢?

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018年07月26日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 使用 List<T>.Sort
  • 借助平台库中的 SortedList<Tkey, TValue> etc.
  • 那 SortedSet<T> 怎么样?
  • 其实我们可以自己封装
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档