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

推荐订阅源

量子位
S
Securelist
MyScale Blog
MyScale Blog
Jina AI
Jina AI
罗磊的独立博客
The Cloudflare Blog
美团技术团队
博客园 - 叶小钗
阮一峰的网络日志
阮一峰的网络日志
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
雷峰网
雷峰网
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
大猫的无限游戏
大猫的无限游戏
博客园 - Franky
博客园 - 聂微东
Y
Y Combinator Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
T
Tailwind CSS Blog
Attack and Defense Labs
Attack and Defense Labs
博客园_首页
Latest news
Latest news
Apple Machine Learning Research
Apple Machine Learning Research
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Hacker News
The Hacker News
G
GRAHAM CLULEY
Simon Willison's Weblog
Simon Willison's Weblog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
U
Unit 42
D
Docker
Webroot Blog
Webroot Blog
N
Netflix TechBlog - Medium
T
Tor Project blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Last Watchdog
The Last Watchdog
B
Blog
Recent Announcements
Recent Announcements
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
Security Latest
Security Latest
V2EX - 技术
V2EX - 技术
N
News | PayPal Newsroom
Microsoft Security Blog
Microsoft 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;
}