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

📄 undoredolist.cs

📁 .net程序中实现重做_撤销功能(Undo_Redo)
💻 CS
📖 第 1 页 / 共 2 页
字号:
// Siarhei Arkhipenka (c) 2006-2007. email: sbs-arhipenko@yandex.ru
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Diagnostics;

namespace UndoRedoFramework.Collections.Generic
{
    public class UndoRedoList<T> : IUndoRedoMember, IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
    {
        List<T> list;

        #region IUndoRedoMember Members

        void IUndoRedoMember.OnCommit(object change)
        {
            Debug.Assert(change != null);
            ((Change<List<T>>)change).NewState = list;
        }

        void IUndoRedoMember.OnUndo(object change)
        {
            Debug.Assert(change != null);
            list = ((Change<List<T>>)change).OldState;
        }

        void IUndoRedoMember.OnRedo(object change)
        {
            Debug.Assert(change != null);
            list = ((Change<List<T>>)change).NewState;
        }
        #endregion

        void Enlist()
        {
            Enlist(true);
        }
        void Enlist(bool copyItems)
        {
            UndoRedoManager.AssertCommand();
            if (!UndoRedoManager.CurrentCommand.ContainsKey(this))
            {
                Change<List<T>> change = new Change<List<T>>();
                change.OldState = list;
                UndoRedoManager.CurrentCommand[this] = change;
                if (copyItems)
                    list = new List<T>(list);
                else
                    list = new List<T>();
            }
        }        

        ///<summary>
        /// Initializes a new instance of the System.Collections.Generic.List<T> class
        /// that is empty and has the default initial capacity.
        /// </summary>
        public UndoRedoList()
        {
            list = new List<T>();
        }
        //
        ///<summary>
        // Initializes a new instance of the System.Collections.Generic.List<T> class
        // that contains elements copied from the specified collection and has sufficient
        // capacity to accommodate the number of elements copied.
        //
        // Parameters:
        //   collection:
        // The collection whose elements are copied to the new list.
        //
        // Exceptions:
        //   System.ArgumentNullException:
        // collection is null.
        public UndoRedoList(IEnumerable<T> collection)
        {
            list = new List<T>(collection);
        }
        ///<summary>
        // Gets or sets the total number of elements the internal data structure can
        // hold without resizing.
        //
        // Returns:
        // The number of elements that the System.Collections.Generic.List<T> can contain
        // before resizing is required.
        //
        // Exceptions:
        //   System.ArgumentOutOfRangeException:
        // System.Collections.Generic.List<T>.Capacity is set to a value that is less
        // than System.Collections.Generic.List<T>.Count.
        public int Capacity 
        {
            get { return list.Capacity; }
            set { list.Capacity = value; }
        }
        //
        ///<summary>
        // Gets the number of elements actually contained in the System.Collections.Generic.List<T>.
        //
        // Returns:
        // The number of elements actually contained in the System.Collections.Generic.List<T>.
        public int Count 
        {
            get { return list.Count; }
        }

        ///<summary>
        // Gets or sets the element at the specified index.
        //
        // Parameters:
        //   index:
        // The zero-based index of the element to get or set.
        //
        // Returns:
        // The element at the specified index.
        //
        // Exceptions:
        //   System.ArgumentOutOfRangeException:
        // index is less than 0.-or-index is equal to or greater than System.Collections.Generic.List<T>.Count.
        public T this[int index] 
        {
            get { return list[index]; }
            set
            {
                Enlist();
                list[index] = value;
            }
        }

        ///<summary>
        // Adds an object to the end of the System.Collections.Generic.List<T>.
        //
        // Parameters:
        //   item:
        // The object to be added to the end of the System.Collections.Generic.List<T>.
        // The value can be null for reference types.
        public void Add(T item)
        {
            Enlist();
            list.Add(item);
        }
        //
        ///<summary>
        // Adds the elements of the specified collection to the end of the System.Collections.Generic.List<T>.
        //
        // Parameters:
        //   collection:
        // The collection whose elements should be added to the end of the System.Collections.Generic.List<T>.
        // The collection itself cannot be null, but it can contain elements that are
        // null, if type T is a reference type.
        //
        // Exceptions:
        //   System.ArgumentNullException:
        // collection is null.
        public void AddRange(IEnumerable<T> collection)
        {
            Enlist();
            list.AddRange(collection);
        }
        //
        ///<summary>
        // Returns a read-only System.Collections.Generic.IList<T> wrapper for the current
        // collection.
        //
        // Returns:
        // A System.Collections.Generic.ReadOnlyCollection`1 that acts as a read-only
        // wrapper around the current System.Collections.Generic.List<T>.
        public ReadOnlyCollection<T> AsReadOnly()
        {
            return list.AsReadOnly();
        }
        //
        ///<summary>
        // Searches the entire sorted System.Collections.Generic.List<T> for an element
        // using the default comparer and returns the zero-based index of the element.
        //
        // Parameters:
        //   item:
        // The object to locate. The value can be null for reference types.
        //
        // Returns:
        // The zero-based index of item in the sorted System.Collections.Generic.List<T>,
        // if item is found; otherwise, a negative number that is the bitwise complement
        // of the index of the next element that is larger than item or, if there is
        // no larger element, the bitwise complement of System.Collections.Generic.List<T>.Count.
        //
        // Exceptions:
        //   System.InvalidOperationException:
        // The default comparer System.Collections.Generic.Comparer<T>.Default cannot
        // find an implementation of the System.IComparable<T> generic interface or
        // the System.IComparable interface for type T.
        public int BinarySearch(T item)
        {
            return list.BinarySearch(item);
        }
        //
        ///<summary>
        // Searches the entire sorted System.Collections.Generic.List<T> for an element
        // using the specified comparer and returns the zero-based index of the element.
        //
        // Parameters:
        //   item:
        // The object to locate. The value can be null for reference types.
        //
        //   comparer:
        // The System.Collections.Generic.IComparer<T> implementation to use when comparing
        // elements.-or-null to use the default comparer System.Collections.Generic.Comparer<T>.Default.
        //
        // Returns:
        // The zero-based index of item in the sorted System.Collections.Generic.List<T>,
        // if item is found; otherwise, a negative number that is the bitwise complement
        // of the index of the next element that is larger than item or, if there is
        // no larger element, the bitwise complement of System.Collections.Generic.List<T>.Count.
        //
        // Exceptions:
        //   System.InvalidOperationException:
        // comparer is null, and the default comparer System.Collections.Generic.Comparer<T>.Default
        // cannot find an implementation of the System.IComparable<T> generic interface
        // or the System.IComparable interface for type T.
        public int BinarySearch(T item, IComparer<T> comparer)
        {
            return list.BinarySearch(item, comparer);
        }
        //
        ///<summary>
        // Searches a range of elements in the sorted System.Collections.Generic.List<T>
        // for an element using the specified comparer and returns the zero-based index
        // of the element.
        //
        // Parameters:
        //   count:
        // The length of the range to search.
        //
        //   item:
        // The object to locate. The value can be null for reference types.
        //
        //   index:
        // The zero-based starting index of the range to search.
        //

⌨️ 快捷键说明

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