C#排序列表 - 快速,具有可移动,重复的密钥

时间:2017-10-13 16:15:56

标签: c# list sorting

我使用压缩机制创建应用程序并需要我自己的Dictionary。我的应用程序中的每个cicle,它都会将新元素添加到myDictionary中并更新(在myDictionary中的某些先前元素中添加一个char)。我正在使用普通列表和Quicksort函数,但它真的很慢。我正在寻找一些新方法如何做到这一点,但SortedList,Dictionary或LookUp似乎不像我在寻找。制作我自己的SortedList或者管理太难/太复杂是不是更好?

部分代码:

public class MyDictionary
{
    private List<string> Contexts;
    private List<string> Contents;
    private int Count;  //words count

    //Konstruktor
    public MyDictionary()
    {
        Count = 0;
        Contexts = new List<string>();
        Contents = new List<string>();
    }

区域公共职能

    public void AddChar(char ch, int contentSize)
    {
        for (int i = 0; i < Count; i++)
        {
            if (Contents[i].Length < contentSize)
            {
                Contents[i] = Contents[i] + ch;
            }
        }
    }

    public void Add(string context, string content)
    {
        Contexts.Add(Reverse(context)); //otočený kontext
        Contents.Add(content);
        Count++;
    }

    public void update()
    {
        quicksort(Contexts, Contents, 0, Count-1);
    }

    private void quicksort(List<String> context, List<String> content, int left, int right)
    {
        int i = left, j = right;
        string pivot = context[(left + right) / 2];

        while (i <= j)
        {
            while (context[i].CompareTo(pivot) < 0)
            {
                i++;
            }

            while (context[j].CompareTo(pivot) > 0)
            {
                j--;
            }

            if (i <= j)
            {
                swap(i,j);                                  
                i++;
                j--;
            }
        }

        // Recursive calls
        if (left < j)
        {
            quicksort(context, content, left, j);
        }

        if (i < right)
        {
            quicksort(context, content, i, right);
        }
    }

    private static string Reverse(string s)
    {
        char[] charArray = s.ToCharArray();
        Array.Reverse(charArray);
        return new string(charArray);
    }

1 个答案:

答案 0 :(得分:0)

这是一个类似SortedDictionary的类,但可以使用相同的键保存多个值。您可能需要使用Remove等方法对其进行充实,并在需要时添加对您自己的IComparer<TKey>的支持。 LINQPad file

public class SortedMultiValue<TKey, TValue> : IEnumerable<TValue>
{
    private SortedDictionary<TKey, List<TValue>> _data;

    public SortedMultiValue()
    {
        _data = new SortedDictionary<TKey, System.Collections.Generic.List<TValue>>();
    }

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

    public void Add(TKey key, TValue value)
    {
        if (!_data.TryGetValue(key, out List<TValue> items))
        {
            items = new List<TValue>();
            _data.Add(key, items);
        }
        items.Add(value);
    }

    public IEnumerable<TValue> Get(TKey key)
    {
        if (_data.TryGetValue(key, out List<TValue> items))
        {
            return items;
        }
        throw new KeyNotFoundException();
    }

    public IEnumerator<TValue> GetEnumerator()
    {
        return CreateEnumerable().GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return CreateEnumerable().GetEnumerator();
    }

    IEnumerable<TValue> CreateEnumerable()
    {
        foreach (IEnumerable<TValue> values in _data.Values)
        {
            foreach (TValue value in values)
            {
                yield return value;
            }
        }
    }
}

你可以像这样使用它:

var data = new SortedMultiValue<string, string>();

data.Add("Dog", "Buddy");
data.Add("Dog", "Mr. Peanutbutter");
data.Add("cat", "Charlie");
data.Add("cat", "Sam");
data.Add("cat", "Leo");

foreach (string item in data)
{
    Console.WriteLine(item);
}

Console.WriteLine();
foreach (string item in data.Get("cat"))
{
    Console.WriteLine(item);
}

Console.WriteLine();
foreach (string item in data.Get("Dog"))
{
    Console.WriteLine(item);
}

它生成它作为输出(注意第一组名称按插入的键排序):

  

查理
  山姆
  狮子座
  巴迪
  Peanutbutter先生

     

查理
  山姆
  狮子座

     

好友
  Peanutbutter先生