⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lightcollection.cs

📁 Fireball.CodeEditor is an source code editor control derived from the best compona SyntaxBox Control
💻 CS
📖 第 1 页 / 共 3 页
字号:

//    Copyright (C) 2004  Riccardo Marzi <riccardo@dotnetfireball.net>
//
//    Copyright (C) 2004  Sebastian Faltoni <sebastian@dotnetfireball.net>
//
//    This library is free software; you can redistribute it and/or
//    modify it under the terms of the GNU Lesser General Public
//    License as published by the Free Software Foundation; either
//    version 2.1 of the License, or (at your option) any later version.
//
//    This library is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//    Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public
//    License along with this library; if not, write to the Free Software
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA



#region Using directives

using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;

#endregion

namespace Fireball.Collections.Generic
{

    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class LightCollection<T> : ILightCollection<T>, ICloneable, IList
    {
        #region Static Members

        internal static string InternalGetResourceError(string key)
        {
            return string.Empty;
        }

        #endregion

        #region Nested Types

        public struct Enumerator : IEnumerator<T>, IEnumerator
        {
            private LightCollection<T> _list;
            private int _index;
            //private int _version;
            private T _current;

            internal Enumerator(LightCollection<T> list)
            {
                _list = list;
                _index = 0;
                _current = default(T);
            }

            public T Current
            {
                get { return _current; }
            }
            public void Dispose()
            {
            }
            object IEnumerator.Current
            {
                get
                {
                    if ((_index == 0) || (_index == (_list.Count + 1)))
                    {
                        throw new InvalidOperationException();
                    }
                    return this.Current;
                }
            }
            public bool MoveNext()
            {
                if (_index < _list.Count)
                {
                    _current = _list[_index];
                    _index++;
                    return true;
                }
                _index = _list.Count + 1;
                _current = default(T);
                return false;
            }
            public void Reset()
            {
                _index = 0;
                _current = default(T);
            }
        }

        public class LightCollectionAddEventArgs : EventArgs
        {
            private T _Item;
            private int _Index;

            public T Item
            {
                get
                {
                    return _Item;
                }
            }

            public int Index
            {
                get
                {
                    return _Index;
                }
            }

            public LightCollectionAddEventArgs(T item, int index)
            {
                _Item = item;
                _Index = index;
            }
        }
        public class LightCollectionRemoveEventArgs : EventArgs
        {
            private T _RemovedItem;
            private int _Index;

            public T RemovedItem
            {
                get
                {
                    return _RemovedItem;
                }
            }

            public int Index
            {
                get
                {
                    return _Index;
                }
            }

            public LightCollectionRemoveEventArgs(T removedItem, int index)
            {
                _RemovedItem = removedItem;
                _Index = index;
            }
        }
        public class LightCollectionMoveEventArgs : EventArgs
        {
            private T _Item;
            private int _NewIndex;
            private int _OldIndex;

            public T Item
            {
                get
                {
                    return _Item;
                }
            }

            public int NewIndex
            {
                get
                {
                    return _NewIndex;
                }
            }

            public int OldIndex
            {
                get
                {
                    return _OldIndex;
                }
            }


            public LightCollectionMoveEventArgs(T item, int newIndex, int oldIndex)
            {
                _Item = item;
                _NewIndex = newIndex;
                _OldIndex = oldIndex;
            }
        }
        public class LightCollectionAddRangeEventArgs : EventArgs
        {
            private T[] _Items;
            private int _StartIndex;

            public T[] Items
            {
                get
                {
                    return _Items;
                }
            }

            public int StartIndex
            {
                get
                {
                    return _StartIndex;
                }
            }

            public LightCollectionAddRangeEventArgs(T[] items, int startIndex)
            {
                _Items = items;
                _StartIndex = startIndex;
            }
        }

        #endregion

        #region Delegates

        public delegate void LightCollectionAddHandler(object sender, LightCollectionAddEventArgs e);
        public delegate void LightCollectionRemoveHandler(object sender, LightCollectionRemoveEventArgs e);
        public delegate void LightCollectionMoveHandler(object sender, LightCollectionMoveEventArgs e);
        public delegate void LightCollectionAddRangeHandler(object sender, LightCollectionAddRangeEventArgs e);

        #endregion

        #region Events

        public event LightCollectionAddHandler ItemAdd;
        public event LightCollectionRemoveHandler ItemRemove;
        public event LightCollectionMoveHandler ItemMove;
        public event LightCollectionAddRangeHandler ItemAddRange;
        public event EventHandler CollectionClear;

        #endregion

        #region Fields

        protected T[] _items;
        private int _count;
        private ReadOnlyCollection<T> _readonlyColl;

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor of LightCollection
        /// </summary>
        public LightCollection()
        {
            Init(4);
        }
        /// <summary>
        /// Constructor of LightCollection with the initial capacity specified of the collection
        /// </summary>
        /// <param name="StartCapacity">The initial capacity of the collection</param>
        public LightCollection(int StartCapacity)
        {
            Init(StartCapacity);
        }
        /// <summary>
        /// Constructor of LightCollection
        /// </summary>
        /// <param name="coll">The collection from where copy items</param>
        public LightCollection(ILightCollection<T> coll)
        {
            Init(coll.Count);

            this.AddRange(coll.GetItems());
        }
        /// <summary>
        /// Constructor of LightCollection with array of T[] as argument
        /// </summary>
        /// <param name="array">Array of T[] as initial values</param>
        public LightCollection(T[] array)
        {

            if (array == null)
            {
                throw new ArgumentNullException("array");
            }
            _items = array;
            _count = array.Length;
        }

        #endregion

        #region Private Members

        private void Init(int capacity)
        {
            _items = new T[capacity];
            _count = 0;
            _readonlyColl = null;
        }
        private void EnsureCapacity(int min)
        {
            if (this._items.Length < min)
            {
                int newCapacity = (this._items.Length == 0) ? 4 : (this._items.Length * 2);

                if (newCapacity < min)
                {
                    newCapacity = min;
                }
                this.SetCollCapacity(newCapacity);
            }
        }
        private void SetCollCapacity(int value)
        {
            if (value != this._items.Length)
            {
                if (value < this._count)
                {
                    throw new ArgumentOutOfRangeException("value", "ArgumentOutOfRange SmallCapacity");
                }
                if (value > 0)
                {
                    T[] newArray = new T[value];

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -