示例#1
0
        public SegmentedList(IEnumerable <T> items, int segmentLengthLog2 = 7)
        {
            this.segmentLengthLog2 = -1;
            if (segmentLengthLog2 < 0)
            {
                throw new ArgumentOutOfRangeException($"{"segmentLengthLog2"}={segmentLengthLog2.ToString()} but must be non-negative");
            }
            this.segmentLengthLog2 = segmentLengthLog2;
            this.Initialize();
            int segmentLength      = this.SegmentLength;
            SegmentedList <T> list = items as SegmentedList <T>;

            if ((list != null) && (list.SegmentLengthLog2 == this.SegmentLengthLog2))
            {
                this.count = list.count;
                int count = list.segments.Count;
                this.segments.Capacity = count;
                for (int i = 0; i < count; i++)
                {
                    T[] destinationArray = this.segmentAllocator.Get();
                    Array.Copy(list.segments[i], destinationArray, segmentLength);
                    this.segments.Add(destinationArray);
                }
            }
            else
            {
                this.AddRange(items);
            }
        }
示例#2
0
        public static void Resize <T>(IList <T> list, int newCount)
        {
            Validate.Begin().IsNotNull <IList <T> >(list, "list").IsNotNegative(newCount, "newCount").Check();
            SegmentedList <T> list2 = list as SegmentedList <T>;

            if (list2 != null)
            {
                list2.Resize(newCount, false, true);
            }
            else
            {
                int count = list.Count;
                if (newCount != count)
                {
                    if (newCount == 0)
                    {
                        list.Clear();
                    }
                    else if (newCount < count)
                    {
                        int num2 = list.Count - newCount;
                        list.RemoveRange <T>(list.Count - num2, num2);
                    }
                    else if (newCount > count)
                    {
                        int num3 = newCount - list.Count;
                        list.AddDefaults <T>(num3);
                    }
                }
            }
        }
示例#3
0
 private SparseList(SerializationInfo info, StreamingContext context)
 {
     this.segmentLengthLog2 = info.GetInt32("segmentLengthLog2");
     if (this.segmentLengthLog2 < 0)
     {
         throw new FormatException($"segmentLengthLog2({this.segmentLengthLog2}) must not be negative");
     }
     this.count = info.GetInt32("count");
     if (this.count < 0)
     {
         throw new FormatException($"count({this.count}) must not be negative");
     }
     if (this.count != 0)
     {
         IList <T[]> items = info.GetValue <IList <T[]> >("segments");
         this.segments = (items as SegmentedList <T[]>) ?? new SegmentedList <T[]>(items, 7);
     }
     this.Initialize();
     if (this.count == 0)
     {
         this.ClearReset();
     }
     else
     {
         this.EnsureCapacity(this.count);
         int segmentLength = this.SegmentLength;
         for (int i = 0; i < this.segments.Count; i++)
         {
             T[] array = this.segments[i];
             if (array != null)
             {
                 if (array.Length != segmentLength)
                 {
                     Array.Resize <T>(ref array, segmentLength);
                 }
                 int segmentSize = this.GetSegmentSize(i);
                 if (array.Length > segmentSize)
                 {
                     Array.Clear(array, segmentSize, segmentSize - array.Length);
                 }
                 for (int j = 0; j < segmentSize; j++)
                 {
                     if (!SparseList <T> .defaultValueComparer.IsDefaultValue(ref array[j]))
                     {
                         int num5 = i;
                         int num6 = this.segmentNonNullCount[num5] + 1;
                         this.segmentNonNullCount[num5] = num6;
                     }
                 }
                 if (this.segmentNonNullCount[i] == 0)
                 {
                     this.segments[i] = null;
                 }
             }
         }
     }
 }
示例#4
0
 internal Enumerator(SegmentedList <T> list)
 {
     if (list == null)
     {
         throw new InternalErrorException(new ArgumentNullException("list"));
     }
     this.list            = list;
     this.currentIndex    = -1;
     this.requiredVersion = this.list.Version;
 }
示例#5
0
        public static void Resize <T>(this IList <T> list, int newCount)
        {
            Validate.IsNotNull <IList <T> >(list, "list");
            int count = list.Count;

            if (count != newCount)
            {
                List <T> list2 = list as List <T>;
                if (list2 != null)
                {
                    if (newCount > count)
                    {
                        list2.AddRange(Enumerable.Repeat <T>(default(T), newCount - count));
                    }
                    else
                    {
                        list2.RemoveRange(newCount, count - newCount);
                    }
                }
                else
                {
                    SegmentedList <T> list3 = list as SegmentedList <T>;
                    if (list3 != null)
                    {
                        list3.Resize(newCount, true, true);
                    }
                    else
                    {
                        SegmentedListStruct <T>?nullable = list as SegmentedListStruct <T>?;
                        if (nullable.HasValue)
                        {
                            nullable.Value.Source.Resize(newCount, true, true);
                        }
                        else
                        {
                            UncheckedSegmentedListStruct <T>?nullable2 = list as UncheckedSegmentedListStruct <T>?;
                            if (nullable2.HasValue)
                            {
                                nullable2.Value.Source.Resize(newCount, true, true);
                            }
                            else if (newCount > count)
                            {
                                list.AddDefaults <T>(newCount - count);
                            }
                            else
                            {
                                list.RemoveRange <T>(newCount, count - newCount);
                            }
                        }
                    }
                }
            }
        }
示例#6
0
        public static SegmentedList <T[]> Clone2DSegmented <T>(this IList <T[]> list2D)
        {
            Validate.IsNotNull <IList <T[]> >(list2D, "list2D");
            int count = list2D.Count;
            SegmentedList <T[]> list = new SegmentedList <T[]>();

            list.EnsureCapacity(count);
            for (int i = 0; i < count; i++)
            {
                list.Add(list2D[i].CloneT <T>());
            }
            return(list);
        }
示例#7
0
        private void Initialize()
        {
            this.segmentSubIndexMask = (((int)1) << this.segmentLengthLog2) - 1;
            if (this.segments == null)
            {
                if (this.count != 0)
                {
                    throw new InternalErrorException("this.segments is null, but this.count != 0");
                }
                this.segments            = new SegmentedList <T[]>();
                this.segmentNonNullCount = new SegmentedList <int>();
            }
            if (this.segmentNonNullCount == null)
            {
                this.segmentNonNullCount = new SegmentedList <int>();
                this.segmentNonNullCount.Resize(this.segments.Count);
            }
            this.segmentAllocator = SegmentAllocator <T> .GetInstance(this.segmentLengthLog2);

            this.isInitialized = true;
        }
示例#8
0
        public SynchronizedEnumerator <T> GetEnumerator()
        {
            List <T> source = this.source as List <T>;

            if (source != null)
            {
                return(new SynchronizedEnumerator <T, List <T> .Enumerator>(this.sync, source.GetEnumerator()));
            }
            SegmentedList <T> list2 = this.source as SegmentedList <T>;

            if (list2 != null)
            {
                return(new SynchronizedEnumerator <T, SegmentedList <T> .Enumerator>(this.sync, list2.GetEnumerator()));
            }
            SparseList <T> list3 = this.source as SparseList <T>;

            if (list3 != null)
            {
                return(new SynchronizedEnumerator <T, SparseList <T> .Enumerator>(this.sync, list3.GetEnumerator()));
            }
            return(new SynchronizedEnumerator <T, IEnumerator <T> >(this.sync, this.source.GetEnumerator()));
        }
示例#9
0
        public void TrimExcess()
        {
            SegmentedList <KeyValuePair <TKey, SparseQueue <TValue> > > list = null;
            object sync = this.sync;

            lock (sync)
            {
                foreach (KeyValuePair <TKey, SparseQueue <TValue> > pair in this.pools)
                {
                    SparseQueue <TValue> collection = pair.Value;
                    if (collection.Any <TValue>())
                    {
                        collection.TrimExcess();
                    }
                    else
                    {
                        if (list == null)
                        {
                            list = new SegmentedList <KeyValuePair <TKey, SparseQueue <TValue> > >();
                        }
                        list.Add(pair);
                    }
                }
                if (list != null)
                {
                    foreach (KeyValuePair <TKey, SparseQueue <TValue> > pair2 in list)
                    {
                        this.pools.Remove(pair2.Key);
                    }
                }
            }
            foreach (KeyValuePair <TKey, SparseQueue <TValue> > pair3 in list)
            {
                pair3.Value.TrimExcess();
            }
        }
示例#10
0
 public static SegmentedListStruct <T> AsStruct <T>(this SegmentedList <T> source) =>
 new SegmentedListStruct <T>(source);
示例#11
0
 public SegmentedStack(int capacity, int segmentLengthLog2 = 7)
 {
     this.list = new SegmentedList <T>(capacity, segmentLengthLog2);
 }
示例#12
0
 public SegmentedStack(IEnumerable <T> items, int segmentLengthLog2 = 7)
 {
     this.list = new SegmentedList <T>(items, segmentLengthLog2);
 }
示例#13
0
 public Enumerator(SegmentedList <T> source)
 {
     this.enumerator = source.GetEnumerator();
 }
示例#14
0
 public UncheckedSegmentedListStruct(SegmentedList <T> source)
 {
     Validate.IsNotNull <SegmentedList <T> >(source, "source");
     this.source = source;
 }
示例#15
0
 public SegmentIterator(SegmentedList <T> list, int listIndex)
 {
     this.list      = list;
     this.listIndex = listIndex;
 }
示例#16
0
 public void Dispose()
 {
     this.list            = null;
     this.currentIndex    = -1;
     this.requiredVersion = -1;
 }