예제 #1
0
        /// <summary>
        ///     Add 将一个元素添加到数组中。如果数组中已存在目标元素,则忽略。无论哪种情况,posIndex都会被赋予正确的值。
        /// </summary>
        public void Add(T t, out int posIndex)
        {
            if (t == null)
            {
                throw new Exception("Target can't be null !");
            }

            using (SmartRwLocker.Lock(AccessMode.Write))
            {
                var index = Array.BinarySearch(_array, 0, Count, t, _comparer4Key);
                if (index >= 0)
                {
                    posIndex = index;
                    return;
                }

                AdjustCapacity(1);
                posIndex = ~index;
                Array.Copy(_array, posIndex, _array, posIndex + 1, Count - posIndex);
                _array[posIndex] = t;

                ++Count;
            }

            _lazyCopy = null;
        }
예제 #2
0
        public void Clear()
        {
            using (SmartRwLocker.Lock(AccessMode.Write))
            {
                _array = new T[_minCapacityForShrink];
                Count  = 0;
            }

            _lazyCopy = null;
        }
예제 #3
0
        public T GetMin()
        {
            using (SmartRwLocker.Lock(AccessMode.Read))
            {
                if (Count == 0)
                {
                    throw new Exception("SortedArray is Empty !");
                }

                return(_array[0]);
            }
        }
예제 #4
0
        public int IndexOf(T t)
        {
            using (SmartRwLocker.Lock(AccessMode.Read))
            {
                if (Count == 0)
                {
                    return(-1);
                }

                var index = Array.BinarySearch(_array, 0, Count, t, _comparer4Key);

                return(index < 0 ? -1 : index);
            }
        }
예제 #5
0
        public List <T> GetAll()
        {
            var list = new List <T>();

            using (SmartRwLocker.Lock(AccessMode.Read))
            {
                for (var i = 0; i < Count; i++)
                {
                    list.Add(_array[i]);
                }
            }

            return(list);
        }
예제 #6
0
        /// <summary>
        ///     Add 如果能保证collection中的元素不会与现有的元素重复,则checkRepeat可以传入false。
        /// </summary>
        public void Add(ICollection <T> collection, bool checkRepeat)
        {
            if (collection == null || collection.Count == 0)
            {
                return;
            }

            using (SmartRwLocker.Lock(AccessMode.Write))
            {
                var resultCollection = collection;

                #region checkRepeat

                if (checkRepeat)
                {
                    var dic = new Dictionary <T, T>();
                    foreach (var t in collection)
                    {
                        if (dic.ContainsKey(t) || Contains(t))
                        {
                            continue;
                        }

                        dic.Add(t, t);
                    }

                    resultCollection = dic.Keys;
                }

                #endregion

                if (resultCollection.Count == 0)
                {
                    return;
                }

                AdjustCapacity(resultCollection.Count);

                foreach (var t in resultCollection)
                {
                    _array[Count] = t;
                    ++Count;
                }

                Array.Sort(_array, 0, Count, _comparer4Key);
            }

            _lazyCopy = null;
        }
예제 #7
0
        public T this[int index]
        {
            get
            {
                using (SmartRwLocker.Lock(AccessMode.Read))
                {
                    if (index < 0 || index >= Count)
                    {
                        throw new Exception("Index out of the range !");
                    }

                    return(_array[index]);
                }
            }
        }
예제 #8
0
        /// <summary>
        ///     Shrink 将内部数组收缩到最小,释放内存。
        /// </summary>
        public void Shrink()
        {
            using (SmartRwLocker.Lock(AccessMode.Write))
            {
                if (_array.Length == Count)
                {
                    return;
                }


                var len = Count >= _minCapacityForShrink ? Count : _minCapacityForShrink;

                var newAry = new T[len];

                Array.Copy(_array, 0, newAry, 0, Count);
                _array = newAry;
            }
        }
예제 #9
0
        public T[] GetBetween(int minIndex, int maxIndex)
        {
            using (SmartRwLocker.Lock(AccessMode.Read))
            {
                minIndex = minIndex < 0 ? 0 : minIndex;
                maxIndex = maxIndex >= Count ? Count - 1 : maxIndex;

                if (maxIndex < minIndex)
                {
                    return(new T[0]);
                }

                var count  = maxIndex - minIndex - 1;
                var result = new T[count];

                Array.Copy(_array, minIndex, result, 0, count);
                return(result);
            }
        }
예제 #10
0
        public void RemoveBetween(int minIndex, int maxIndex)
        {
            using (SmartRwLocker.Lock(AccessMode.Write))
            {
                minIndex = minIndex < 0 ? 0 : minIndex;
                maxIndex = maxIndex >= Count ? Count - 1 : maxIndex;

                if (maxIndex < minIndex)
                {
                    return;
                }

                Array.Copy(_array, maxIndex + 1, _array, minIndex, Count - maxIndex - 1);

                Count -= maxIndex - minIndex + 1;
            }

            _lazyCopy = null;
        }
예제 #11
0
        /// <summary>
        ///     注意,内部使用了Lazy缓存,返回的集合不可被修改。
        /// </summary>
        public List <T> GetAllReadonly()
        {
            if (_lazyCopy != null)
            {
                return(_lazyCopy);
            }

            using (SmartRwLocker.Lock(AccessMode.Read))
            {
                var list = new List <T>();
                for (var i = 0; i < Count; i++)
                {
                    list.Add(_array[i]);
                }

                _lazyCopy = list;
                return(_lazyCopy);
            }
        }
예제 #12
0
        private void AdjustCapacity(int newCount)
        {
            using (SmartRwLocker.Lock(AccessMode.Write))
            {
                var totalCount = Count + newCount;
                if (_array.Length >= totalCount)
                {
                    return;
                }

                var newCapacity = _array.Length;
                while (newCapacity < totalCount)
                {
                    newCapacity *= 2;
                }

                var newAry = new T[newCapacity];
                Array.Copy(_array, 0, newAry, 0, Count);
                _array = newAry;
            }
        }
예제 #13
0
        public void RemoveAt(int index)
        {
            using (SmartRwLocker.Lock(AccessMode.Write))
            {
                if (index < 0 || index >= Count)
                {
                    return;
                }

                if (index == Count - 1)
                {
                    _array[index] = default(T);
                }
                else
                {
                    Array.Copy(_array, index + 1, _array, index, Count - index - 1);
                }
                --Count;
            }

            _lazyCopy = null;
        }