惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
CXSECURITY Database RSS Feed - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
月光博客
月光博客
T
Threat Research - Cisco Blogs
小众软件
小众软件
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
Apple Machine Learning Research
Apple Machine Learning Research
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tailwind CSS Blog
Cisco Talos Blog
Cisco Talos Blog
V
V2EX
博客园 - 【当耐特】
C
Cybersecurity and Infrastructure Security Agency CISA
Hugging Face - Blog
Hugging Face - Blog
The Cloudflare Blog
The Last Watchdog
The Last Watchdog
Simon Willison's Weblog
Simon Willison's Weblog
T
Threatpost
S
Secure Thoughts
O
OpenAI News
P
Proofpoint News Feed
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Application and Cybersecurity Blog
Application and Cybersecurity Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
宝玉的分享
宝玉的分享
Scott Helme
Scott Helme
T
Tenable Blog
A
Arctic Wolf
L
LINUX DO - 热门话题
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V
Visual Studio Blog
Hacker News: Ask HN
Hacker News: Ask HN
Hacker News - Newest:
Hacker News - Newest: "LLM"
腾讯CDC
博客园 - Franky
WordPress大学
WordPress大学
Know Your Adversary
Know Your Adversary
博客园_首页
雷峰网
雷峰网
IT之家
IT之家
PCI Perspectives
PCI Perspectives
L
LINUX DO - 最新话题
H
Heimdal Security Blog

博客园 - 赶路人之刚出发

集成WebSecurity的Authorize进行身份验证时,数据库连接报错问题 Html.ActionLink传递参数 Automapper结合EF实现insert,update方法 MVC中使用RemoteAttribute异步远程验证 Html.RenderPartial WebMatrix.WebSecurity创建自定义用户属性 强类型view中List<Model〉问题 ViewBag任意属性的实现方法 params关键字 配置LINQ中的datacontext的log路径,以记录datacontext执行了的查询sql LINQ join/left join/cross join/group by/group join/sortedlist/cast Linq to objects示例 yield return 和 Func Lamda表达式 IDisposable 匿名类型与扩展方法 对象初始化器和集合初始化器 C#自动属性 .net random伪随机数
SortedList
赶路人之刚出发 · 2013-04-27 · via 博客园 - 赶路人之刚出发

SortedList 表示键/值对的集合,这些键/值对基于关联的 System.Collections.Generic.IComparer<T> 实现按照键进行升序排序

public class SortedList<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable

其内部维护了两个动态数组keys[],values[],每增加一个新的元素时,调用排序方法找到新增key的顺序位置,然后插入,如有重复key则抛出exception,其排序方法基于二分法排序:

public virtual void Add(object key, object value)
{
    if (key == null)
    {
        throw new ArgumentNullException("key", Environment.GetResourceString("ArgumentNull_Key"));
    }
    int index = Array.BinarySearch(this.keys, 0, this._size, key, this.comparer);
    if (index >= 0)
    {
        throw new ArgumentException(Environment.GetResourceString("Argument_AddingDuplicate__", new object[] { this.GetKey(index), key }));
    }
    this.Insert(~index, key, value);
}

Array.BinarySearch为二分查找法,源码实现如下,:

public static int BinarySearch(Array array, int index, int length, object value, IComparer comparer)
{
    int num2;
    if (array == null)
    {
        throw new ArgumentNullException("array");
    }
    int lowerBound = array.GetLowerBound(0);
    if ((index < lowerBound) || (length < 0))
    {
        throw new ArgumentOutOfRangeException((index < lowerBound) ? "index" : "length", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
    }
    if ((array.Length - (index - lowerBound)) < length)
    {
        throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
    }
    if (array.Rank != 1)
    {
        throw new RankException(Environment.GetResourceString("Rank_MultiDimNotSupported"));
    }
    if (comparer == null)
    {
        comparer = Comparer.Default;
    }
    if ((comparer == Comparer.Default) && TrySZBinarySearch(array, index, length, value, out num2))
    {
        return num2;
    }
    int low = index;
    int hi = (index + length) - 1;
    object[] objArray = array as object[];
    if (objArray == null)
    {
        while (low <= hi)
        {
            int num8;
            int median = GetMedian(low, hi);
            try
            {
                num8 = comparer.Compare(array.GetValue(median), value);
            }
            catch (Exception exception2)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), exception2);
            }
            if (num8 == 0)
            {
                return median;
            }
            if (num8 < 0)
            {
                low = median + 1;
            }
            else
            {
                hi = median - 1;
            }
        }
    }
    else
    {
        while (low <= hi)
        {
            int num6;
            int num5 = GetMedian(low, hi);
            try
            {
                num6 = comparer.Compare(objArray[num5], value);
            }
            catch (Exception exception)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), exception);
            }
            if (num6 == 0)
            {
                return num5;
            }
            if (num6 < 0)
            {
                low = num5 + 1;
            }
            else
            {
                hi = num5 - 1;
            }
        }
    }
    return ~low;
}