Пример #1
0
        internal static GroupingSegment <TKey, TElement> Create(DirtyTracker tracker, byte[] bytes, int offset, out int byteSize)
        {
            var segment = new GroupingSegment <TKey, TElement>();

            tracker         = tracker.CreateChild();
            segment.tracker = tracker;

            var keyFormatter  = Formatter <TKey> .Default;
            var listFormatter = Formatter <IList <TElement> > .Default;

            int keySize;

            segment.key = keyFormatter.Deserialize(ref bytes, offset, tracker, out keySize);

            segment.hashCode = BinaryUtil.ReadInt32(ref bytes, offset + keySize);

            int listSize;

            segment.elements = listFormatter.Deserialize(ref bytes, offset + keySize + 4, tracker, out listSize);

            byteSize = keySize + 4 + listSize;
            segment.originalBytes = new ArraySegment <byte>(bytes, offset, byteSize);

            return(segment);
        }
Пример #2
0
        public IEnumerable <TElement> this[TKey key]
        {
            get
            {
                GroupingSegment <TKey, TElement> grouping = GetGrouping(key, create: false);
                if (grouping != null)
                {
                    return(grouping);
                }

                return(EmptyArray);
            }
        }
Пример #3
0
        GroupingSegment <TKey, TElement> GetGrouping(TKey key, bool create)
        {
            int hashCode = (key == null) ? 0 : comparer.GetHashCode(key) & 0x7FFFFFFF;

            IList <GroupingSegment <TKey, TElement> > groupList = groupings[hashCode % groupings.Count];

            if (groupList != null)
            {
                for (int i = 0; i < groupList.Count; i++)
                {
                    var g = groupList[i];
                    if (g.hashCode == hashCode && comparer.Equals(g.key, key))
                    {
                        return(g);
                    }
                }
            }

            if (create)
            {
                int index = hashCode % groupings.Count;
                var g     = new GroupingSegment <TKey, TElement>(key, hashCode);

                var list = groupings[index];
                if (list == null)
                {
                    list = groupings[index] = new List <GroupingSegment <TKey, TElement> >();
                }
                list.Add(g);

                count++;
                return(g);
            }

            return(null);
        }