Esempio n. 1
0
        internal static bool TryCount <T, TSize>(IEnumerable <T> items, out TSize count)
            where TSize : struct, IConvertible
        {
            IHive <T, TSize> hive = items as IHive <T, TSize>;

            if (hive != null)
            {
                return(hive.TryGetCount(out count));
            }

            ICollection <T> collection = items as ICollection <T>;
            int             itemCount  = collection != null
                ? collection.Count
                : -1;

            count = SizeOperations <TSize> .Default.From(itemCount);

            return(itemCount >= 0);
        }
Esempio n. 2
0
        protected override void OnInsertRange <TInputSize, TInput>
            (Iterator position, IHive <T, TInputSize, TInput> range, out Range <T, TSize, Iterator> insertedRange)
        {
            TSize beginIndex = position.Key;
            TSize endIndex;

            TInputSize itemCount;

            if (range.TryGetCount(out itemCount))
            {
                endIndex = Size.Add(beginIndex, Size.From <TInputSize>(itemCount));
                EnsureSpaceExists(beginIndex, endIndex);

                TSize targetIndex = beginIndex;
                for (TInput i = range.Begin; !i.Equals(range.End); i.Increment())
                {
                    Size.SetValueInArray(_innerArray, i.Read(), targetIndex);
                    Size.Increment(ref targetIndex);
                }
            }
            else
            {
                StreamBuffer <T, TSize, TSizeOperations> snapshot =
                    StreamBuffer <T, TSize, TSizeOperations> .Create(range);

                endIndex = Size.Add(beginIndex, snapshot.Count);

                if (Size.Compare(snapshot.Count, Size.Zero) > 0)
                {
                    EnsureSpaceExists(beginIndex, endIndex);
                    snapshot.CopyTo(_innerArray, beginIndex);
                }
            }

            insertedRange = new Range <T, TSize, Iterator>(position, new Iterator(this, endIndex));
        }