1、数据结构不太好 - 很浪费空间;
2、也不好使用 - 存一个对象还要麻烦去记住它对应的id。
建议你直接用Dictionary或者Hashtable类。

解决方案 »

  1.   


    额。  我这个就是个hash表啊。  只不过是特定类型的hash 我不知道C# 的dictionay 和hashtable 内的hash函数怎么写的。 但要广泛适用所有数据所以我对效率不太相信。 但是我这个hash表只用作存取的特定算法。   或许是鸡肋骨,但肯定不是你说的 1,2 问题。
     对于频繁存取的数据不是很高效吗。 我想知道C#的容器是不是有更高明的实现方法。
      

  2.   

    解决了什么问题?无非就是Guid代替了Hashcode
    而且,List<T>内部就是使用数组的,和你的算法几乎一样。
      

  3.   


    解决了大批量数据随机存取的速度问题。  根据list<T>提供的方法我推断不出来他内部怎么个存取发。也找不到相关文档。   你要说的应该是dictionary吧。  不过dictionary  key值要自己分配的。    所以我认为没有一个提供key value  存储的容器,而又常用所以写了个。
      

  4.   


    解决了大批量数据随机存取的速度问题。  根据list<T>提供的方法我推断不出来他内部怎么个存取发。也找不到相关文档。   你要说的应该是dictionary吧。  不过dictionary  key值要自己分配的。    所以我认为没有一个提供key value  存储的容器,而又常用所以写了个。写出你的调用代码和测试数据,很简单,我写一个看看能不能比你的快。
      

  5.   


    解决了大批量数据随机存取的速度问题。  根据list<T>提供的方法我推断不出来他内部怎么个存取发。也找不到相关文档。   你要说的应该是dictionary吧。  不过dictionary  key值要自己分配的。    所以我认为没有一个提供key value  存储的容器,而又常用所以写了个。写出你的调用代码和测试数据,很简单,我写一个看看能不能比你的快。测试的写好了 long start= System.DateTime.Now.Ticks;
                int addtimes=0;
                int removetimes = 0;
                Queue<int> q = new Queue<int>();
                NetServer.KeyedContainer<int> a = new NetServer.KeyedContainer<int>(10000);
                for (int i = 0; i < 1000000; i++)
                {
                    Random r = new Random();
                    int rv =r.Next(10000);
                    if(a.count()<9000)
                    {
                        if(rv%2==1)
                        {
                            int key=a.add(rv);
                            q.Enqueue(key);
                            addtimes++;
                        }
                        else
                        {
                          
                            if (q.Count > 0)
                            {
                                int key = q.Dequeue();
                                a.remove(key);
                                removetimes++;
                            }
                        }
                    }                                
                }
                Console.WriteLine("add:" + addtimes);
                Console.WriteLine("remove:" + removetimes);
                Console.WriteLine("end:"+(System.DateTime.Now.Ticks-start) );
                Console.Read();
      

  6.   

    这个简单点  long start= System.DateTime.Now.Ticks;
                Queue<int> q = new Queue<int>();
                NetServer.KeyedContainer<int> a = new NetServer.KeyedContainer<int>(10000);
                for (int i = 0; i < 1000000; i++)
                {
                    Random r = new Random();
                    int rv =r.Next(10000);
                    int key = a.add(rv);
                    q.Enqueue(key);                   
                }
                for (int i = 0; i < 1000000; i++)
                {
                    int key=q.Dequeue();
                    a.remove(key);
                }
                long t= (System.DateTime.Now.Ticks - start);
                Console.WriteLine("end:" + t);            
                Console.Read();
      

  7.   


     public class MYGUID
        {
            public int curid;
            public Queue<int> freeList = new Queue<int>();
            private MYGUID(){ }
            public MYGUID(int start = 0)
            {
                curid = start;
            }
            public int getGUID()
            {
                int id;
                if(freeList.Count>0)
                {
                    id=freeList.Dequeue();
                }
                else
                {
                    id = curid++;
                    Trace.Assert(id < int.MaxValue);
                }
                return id;
            }
            public void freeGUID(int id)
            {
                freeList.Enqueue(id);
            }        public int useCount()
            {
                return curid - freeList.Count;
            }
        }
      

  8.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;namespace ConsoleApplication2
    {
        public class MYGUID
        {
            public int curid;
            public Queue<int> freeList = new Queue<int>();
            private MYGUID() { }
            public MYGUID(int start = 0)
            {
                curid = start;
            }
            public int getGUID()
            {
                int id;
                if (freeList.Count > 0)
                {
                    id = freeList.Dequeue();
                }
                else
                {
                    id = curid++;
                }
                return id;
            }
            public void freeGUID(int id)
            {
                freeList.Enqueue(id);
            }        public int useCount()
            {
                return curid - freeList.Count;
            }
        }    public class KeyedContainer<T>
        {
            MYGUID guid = new MYGUID();
            public T[] values;
            private KeyedContainer() { }
            public KeyedContainer(int capture)
            {
                values = new T[capture];
            }
            public int add(T t)
            {
                int id = guid.getGUID();
                if (id >= values.Length)
                {
                    T[] temp = new T[values.Length + values.Length / 2];
                    values.CopyTo(temp, 0);
                    values = temp;
                }
                values[id] = t;
                return id;
            }
            public void remove(int id)
            {
                values[id] = default(T);
                guid.freeGUID(id);
            }
            public T get(int id)
            {
                return values[id];
            }
        }    class Program
        {
            static void Main(string[] args)
            {
                long start = System.DateTime.Now.Ticks;
                Queue<int> q = new Queue<int>();
                KeyedContainer<int> a = new KeyedContainer<int>(10000);
                for (int i = 0; i < 100000; i++)
                {
                    Random r = new Random();
                    int rv = r.Next(10000);
                    int key = a.add(rv);
                    q.Enqueue(key);
                }
                for (int i = 0; i < 100000; i++)
                {
                    int key = q.Dequeue();
                    a.remove(key);
                }
                long t = (System.DateTime.Now.Ticks - start);
                Console.WriteLine("end:" + t);
                start = System.DateTime.Now.Ticks;
                q = new Queue<int>();
                Dictionary<int, int> a1 = new Dictionary<int, int>();
                for (int i = 0; i < 100000; i++)
                {
                    Random r = new Random();
                    int rv = r.Next(10000);
                    a1.Add(i, rv);
                    int key = i;
                    q.Enqueue(key);
                }
                for (int i = 0; i < 100000; i++)
                {
                    int key = q.Dequeue();
                    a1.Remove(i);
                }
                t = (System.DateTime.Now.Ticks - start);
                Console.WriteLine("end:" + t);
                Console.Read();
            }
        }
    }和直接用字典有区别么?
      

  9.   

    我试了下。  字典的key也是hash表效率也高。 不过 用随机 key值直接就崩了。   关键就是key值吧我最初也是用guid加字典的。 嫌不简洁才想起搞个自带key的容器。    这样看来都不简洁。
      

  10.   

    Random r = new Random();
                    int rv = r.Next(10000);
                    a1.Add(i, rv);
                    int key = i;
                    q.Enqueue(key);    就是把这块的key值改成随机值dictionary效率急剧下降。   dictionary效率我大概了解倒不用再测了。
      

  11.   

    所以我估计在未知 key下dictionay的效率也是未知的。 因为它的hash函数什么样子不知道的。  
    msdn也这样说了 “dictionay检索速度取决于为 TKey 指定的类型的哈希算法的质量。”
    所以我更相信自己写的一些。