public IReadOnlyList <TValue> this[TKey key]
        {
            get
            {
                FoundationContract.Ensures(Contract.Result <IReadOnlyList <TValue> >() != null);

                IReadOnlyList <TValue> readOnlyList;

                var index = IndexOf(key);
                if (index >= 0)
                {
                    var currentGroupIndex = _groups[index];
                    var nextGroupIndex    = index < _groups.Count - 1 ? _groups[index + 1] : _values.Count;
                    var count             = nextGroupIndex - currentGroupIndex;

                    readOnlyList = new ReadOnlyListSegment <TValue>(_values, currentGroupIndex, count);
                }
                else
                {
                    readOnlyList = EmptyReadOnlyList <TValue> .Value;
                }

                return(readOnlyList);
            }
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <param name="source"></param>
        /// <param name="count"></param>
        /// <param name="partitionCount"></param>
        /// <returns></returns>
        public static IEnumerable <List <TSource> > GetPartitions <TSource>(
            this IEnumerable <TSource> source,
            int count,
            int partitionCount)
        {
            Assert.IsNotNull(source);
            FoundationContract.Requires <ArgumentOutOfRangeException>(count >= 0);
            FoundationContract.Requires <ArgumentOutOfRangeException>(partitionCount > 0);

            FoundationContract.Ensures(Contract.Result <IEnumerable <List <TSource> > >().Count() <= partitionCount);
            FoundationContract.Ensures(Contract.ForAll(Contract.Result <IEnumerable <List <TSource> > >().ToList(), partition => partition.Count > 0));

            var partitionSize = count / partitionCount;
            var remainder     = count % partitionCount;

            using (var enumerator = source.GetEnumerator())
            {
                for (var partitionIndex = 0; partitionIndex < partitionCount; partitionIndex++)
                {
                    var currentPartitionSize = partitionSize;
                    if (remainder > 0)
                    {
                        currentPartitionSize++;
                        remainder--;
                    }

                    if (currentPartitionSize > 0)
                    {
                        var partition = enumerator.Take(currentPartitionSize);
                        if (partition.Count > 0)
                        {
                            yield return(partition);
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
Пример #3
0
 /// <summary>
 ///
 /// </summary>
 public void Clear()
 {
     FoundationContract.Ensures(Count == 0);
     _list.Clear();
 }
Пример #4
0
        void ICollection <T> .Clear()
        {
            FoundationContract.Ensures(_dictionary.Count == 0);

            _dictionary.Clear();
        }
Пример #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Contains(T item)
        {
            FoundationContract.Ensures(!Contract.Result <bool>() || Count > 0);

            return(_defaultIndex.Contains(item));
        }
Пример #6
0
        /// <summary>
        ///
        /// </summary>
        public void Clear()
        {
            FoundationContract.Ensures(Count == 0);

            _dictionary.Clear();
        }