获取键及其值,然后有效地将其从Dictionary <string,value>中删除

时间:2018-08-26 08:28:49

标签: c# dictionary min

我需要获取键和最小值的值,并同时删除这对键值,如果字典不是最好的数据结构,我必须以一种高效的方式进行操作我可以改变这是我的字典:

Dictionary<string,int> myDict = new Dictionary<string,int>();

1 个答案:

答案 0 :(得分:0)

我创建了一个DictionaryList,其中包含一个阴影排序的值列表。它与字典保持同步。您可以添加键值,删除键并删除最小值。这比较快,可以进行O(log n)比较,其中n是数组中元素的数量。现在,您可以根据自己的喜好将其扩展。

using System;
using System.Collections.Generic;

namespace MainClass
{
    class Program
    {

        static void Main(string[] args)
        {
            DictionaryList<string, int> dict = new DictionaryList<string, int>();

            dict.Add("assd", 45);
            dict.Add("gh", 78);
            dict.Add("uih", 235);
            dict.Add("sdfg", 345);
            dict.Add("jkl", 456);
            dict.Add("wer", 345);
            dict.Add("dfg", 454);
            dict.Add("we", 490895);
            dict.Add("dfgq", 78);
            dict.Add("jkel", 34);
            dict.Add("wepr", 1);
            dict.Add("zer", 345);
            dict.Add("kt", 345);
            dict.Add("qrk", 345);


            dict.RemoveMinimum();
            dict.Remove("wer");

        }

    }

    public class DictionaryList<Key, Value> : Dictionary<Key, Value> where Value : IComparable where Key : IComparable
    {
        private class MinComparer<Key1, Value1> : IComparer<KeyValuePair<Key1, Value1>> where Value1 : IComparable where Key1:IComparable
        {
            public int Compare(KeyValuePair<Key1, Value1> x, KeyValuePair<Key1, Value1> y)
            {
                int comp = x.Value.CompareTo(y.Value);
                if (comp == 0)
                {
                    return x.Key.CompareTo(y.Key);
                }
                return comp;
            }
        }

        private List<KeyValuePair<Key, Value>> shadowList = new List<KeyValuePair<Key, Value>>();
        private MinComparer<Key, Value> mc = new MinComparer<Key, Value>();

        public new void Add(Key key, Value value)
        {
            base.Add(key, value);

            KeyValuePair<Key, Value> shadow = new KeyValuePair<Key, Value>(key, value);
            int index = shadowList.BinarySearch(shadow, mc);
            if (index < 0) index = ~index;
            shadowList.Insert(index, shadow);
        }

        public void RemoveMinimum()
        {
            if (shadowList.Count > 0)
            {
                this.Remove(shadowList[0].Key);
            }
        }

        public new void Remove(Key key)
        {
            Value shadow = this[key];
            base.Remove(key);

            KeyValuePair<Key, Value> dummy = new KeyValuePair<Key, Value>(key, shadow);

            int index = shadowList.BinarySearch(dummy, mc);

            shadowList.RemoveAt(index);

        }
    }
 }