Esempio n. 1
0
 /// <summary>
 /// 索引器根据键TKey获取对应的组
 /// </summary>
 /// <param name="key"></param>
 /// <returns></returns>
 internal IEnumerable <TElement> this[TKey key]
 {
     get {
         MyGrouping <TKey, TElement> grouping = GetGrouping(key, false);
         if (grouping != null)
         {
             return(grouping);
         }
         return(null);
     }
 }
Esempio n. 2
0
        public 数据结构.IEnumerable <系统内置接口.IGrouping <TKey, TElement> > GetPerGroup()
        {
            MyList <系统内置接口.IGrouping <TKey, TElement> > list = new MyList <系统内置接口.IGrouping <TKey, TElement> >();
            MyGrouping <TKey, TElement> g = lastGroup;

            if (g != null)
            {
                do
                {
                    g = g.next;
                    list.Add(g);
                } while (g != lastGroup);
            }
            return(list);
        }
Esempio n. 3
0
        /// <summary>
        /// 根据键值来获取对应的组或者创建对应的组
        /// </summary>
        /// <param name="key">键值</param>
        /// <param name="create">是否创建新的组</param>
        /// <returns></returns>
        internal 数据结构.MyGrouping <TKey, TElement> GetGrouping(TKey key, bool create)
        {
            //1.根据每个对象的分组字段的值计算hashcode
            //2.根据hashcode获取这个对象在grouping[]中的索引
            //3.判断这个索引的位置是否存在元素(①:存在元素判断hashcode的值并根据比较器比较Key和当前的Key是否相等②:不存在元素,那么就创建一个新的组)
            //int hashCode = InitialHashCode(key);
            int hashCode = MyInitialHashCode(key);
            int index    = hashCode % groupings.Length;

            for (MyGrouping <TKey, TElement> g = groupings[index]; g != null; g = g.hashNext)
            {
                if (g != null)
                {
                    if (g.hashcode == hashCode && comparer.Equal(g.Key, key))
                    {
                        return(g);
                    }
                }
            }
            //在已有的存在的grouping的数组中都没有找到就创建对应的Grouping
            if (create)
            {
                //所有具有相同的HashCode、不同键的值得数据形成一条单链表
                //调用MyGrouping类的默认的容量来初始化Telement[] 数组的大小
                MyGrouping <TKey, TElement> newGrouping = new MyGrouping <TKey, TElement>();
                newGrouping.key      = key;
                newGrouping.hashcode = hashCode;
                newGrouping.hashNext = groupings[index];
                groupings[index]     = newGrouping;
                //使所有的元素形成一条循环链表
                if (lastGroup == null)
                {
                    newGrouping.next = newGrouping;
                    lastGroup        = newGrouping;
                }
                else
                {
                    newGrouping.next = lastGroup.next;
                    lastGroup.next   = newGrouping;
                    lastGroup        = newGrouping;
                }
                this.count++;
                return(newGrouping);
            }
            return(null);
        }
Esempio n. 4
0
        /// <summary>
        /// 对已经分完组的每一个组进行统计操作
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="resultselector"></param>
        /// <returns></returns>


#warning 这个地方还可以改进(换成自己的集合的返回的类型)
        public System.Collections.Generic.IEnumerable <TResult> StaticResult <TResult>(Func <TKey, System.Collections.Generic.IEnumerable <TElement>, TResult> resultselector)
        {
            MyGrouping <TKey, TElement> g = lastGroup;

            if (g != null)
            {
                do
                {
                    g = g.next;
                    if (g.count != g.temp.Length)
                    {
                        Array.Resize <TElement>(ref g.temp, g.count);
                    }
                    yield return(resultselector(g.key, g.temp));

#warning 这个地方为什么是不等于?
                } while (g != lastGroup);
                //while (g == lastGroup);
            }
        }