Click here to Skip to main content
15,892,072 members
Articles / Desktop Programming / Windows Forms

Storm - the world's best IDE framework for .NET

Rate me:
Please Sign up or sign in to vote.
4.96/5 (82 votes)
4 Feb 2010LGPL311 min read 276.7K   6.5K   340  
Create fast, flexible, and extensible IDE applications easily with Storm - it takes nearly no code at all!
using System;
using System.Collections;

namespace Storm.TextEditor.Editor.Undo
{
	// This code was generated by a tool.

	using T = UndoBlockCollection;

	/// <summary>
	/// 
	/// </summary>
	public sealed class UndoBuffer : ICollection, IList, IEnumerable, ICloneable
	{
		private int _MaxSize = 1000;
		private const int DefaultMinimumCapacity = 16;

		private T[] m_array = new T[DefaultMinimumCapacity];
		private int m_count = 0;
		private int m_version = 0;

		public int MaxSize
		{
			get { return _MaxSize; }
			set { _MaxSize = value; }
		}

		// Construction
		public UndoBuffer()
		{
		}

		public UndoBuffer(UndoBuffer collection)
		{
			AddRange(collection);
		}

		public UndoBuffer(T[] array)
		{
			AddRange(array);
		}

		// Operations (type-safe ICollection)
		public int Count
		{
			get { return m_count; }
		}

		public void RemoveRange(int index, int count)
		{
			for (int i = 0; i < count; i++)
			{
				if (i < Count)
					RemoveAt(index);
				else
					break;
			}
		}

		public void ClearFrom(int index)
		{
			while (index <= Count - 1)
				RemoveAt(index);
		}

		public void CopyTo(T[] array)
		{
			CopyTo(array, 0);
		}

		public void CopyTo(T[] array, int start)
		{
			if (m_count > array.GetUpperBound(0) + 1 - start)
				throw new ArgumentException("Destination array was not long enough.");

			Array.Copy(m_array, 0, array, start, m_count);
		}

		// Operations (type-safe IList)
		public T this[int index]
		{
			get
			{
				ValidateIndex(index);
				return m_array[index];
			}
			set
			{
				ValidateIndex(index);

				++m_version;
				m_array[index] = value;
			}
		}

		public int Add(T item)
		{
			if (NeedsGrowth())
				Grow();

			++m_version;
			m_array[m_count] = item;

			m_count++;

			while (Count > MaxSize)
			{
				RemoveAt(0);
			}

			return m_count;
		}

		public void Clear()
		{
			++m_version;
			m_array = new T[DefaultMinimumCapacity];
			m_count = 0;
		}

		public bool Contains(T item)
		{
			return ((IndexOf(item) == -1) ? false : true);
		}

		public int IndexOf(T item)
		{
			for (int i = 0; i < m_count; ++i)
				if (m_array[i] == (item))
					return i;
			return -1;
		}

		public void Insert(int position, T item)
		{
			ValidateIndex(position, true);

			if (NeedsGrowth())
				Grow();

			++m_version;
			Array.Copy(m_array, position, m_array, position + 1, m_count - position);

			m_array[position] = item;
			m_count++;
		}

		public void Remove(T item)
		{
			int index = IndexOf(item);
			if (index < 0)
				throw new ArgumentException("Cannot remove the specified item because it was not found in the specified Collection.");

			RemoveAt(index);
		}

		public void RemoveAt(int index)
		{
			ValidateIndex(index);

			++m_version;
			m_count--;
			Array.Copy(m_array, index + 1, m_array, index, m_count - index);

			if (NeedsTrimming())
				Trim();
		}

		// Operations (type-safe IEnumerable)
		public Enumerator GetEnumerator()
		{
			return new Enumerator(this);
		}

		// Operations (type-safe ICloneable)
		public UndoBuffer Clone()
		{
			UndoBuffer tc = new UndoBuffer();
			tc.AddRange(this);
			tc.Capacity = m_array.Length;
			tc.m_version = m_version;
			return tc;
		}

		public int Capacity
		{
			get { return m_array.Length; }
			set
			{
				if (value < m_count) value = m_count;
				if (value < DefaultMinimumCapacity) value = DefaultMinimumCapacity;

				if (m_array.Length == value) return;

				++m_version;

				T[] temp = new T[value];
				Array.Copy(m_array, 0, temp, 0, m_count);
				m_array = temp;
			}
		}

		public void AddRange(UndoBuffer collection)
		{
			++m_version;

			Capacity += collection.Count;
			Array.Copy(collection.m_array, 0, m_array, m_count, collection.m_count);
			m_count += collection.Count;
		}

		public void AddRange(T[] array)
		{
			++m_version;

			Capacity += array.Length;
			Array.Copy(array, 0, m_array, m_count, array.Length);
			m_count += array.Length;
		}

		// Implementation (helpers)
		private void ValidateIndex(int index)
		{
			ValidateIndex(index, false);
		}

		private void ValidateIndex(int index, bool allowEqualEnd)
		{
			int max = (allowEqualEnd) ? (m_count) : (m_count - 1);
			if (index < 0 || index > max)
				throw new ArgumentOutOfRangeException("Index was out of range.  Must be non-negative and less than the size of the collection.", (object)index, "Specified argument was out of the range of valid values.");
		}

		private bool NeedsGrowth()
		{
			return (m_count >= Capacity);
		}

		private void Grow()
		{
			if (NeedsGrowth())
				Capacity = m_count * 2;
		}

		private bool NeedsTrimming()
		{
			return (m_count <= Capacity / 2);
		}

		private void Trim()
		{
			if (NeedsTrimming())
				Capacity = m_count;
		}

		// Implementation (ICollection)
		bool ICollection.IsSynchronized
		{
			get { return m_array.IsSynchronized; }
		}

		object ICollection.SyncRoot
		{
			get { return m_array.SyncRoot; }
		}

		void ICollection.CopyTo(Array array, int start)
		{
			CopyTo((T[])array, start);
		}

		// Implementation (IList)
		bool IList.IsFixedSize
		{
			get { return false; }
		}

		bool IList.IsReadOnly
		{
			get { return false; }
		}

		object IList.this[int index]
		{
			get { return (object)this[index]; }
			set { this[index] = (T)value; }
		}

		int IList.Add(object item)
		{
			return Add((T)item);
		}

		bool IList.Contains(object item)
		{
			return Contains((T)item);
		}

		int IList.IndexOf(object item)
		{
			return IndexOf((T)item);
		}

		void IList.Insert(int position, object item)
		{
			Insert(position, (T)item);
		}

		void IList.Remove(object item)
		{
			Remove((T)item);
		}

		// Implementation (IEnumerable)
		IEnumerator IEnumerable.GetEnumerator()
		{
			return (IEnumerator)(GetEnumerator());
		}

		// Implementation (ICloneable)
		object ICloneable.Clone()
		{
			return (object)(Clone());
		}

		// Nested enumerator class
		public class Enumerator : IEnumerator
		{
			private UndoBuffer m_collection;
			private int m_index;
			private int m_version;

			// Construction
			public Enumerator(UndoBuffer tc)
			{
				m_collection = tc;
				m_index = -1;
				m_version = tc.m_version;
			}

			// Operations (type-safe IEnumerator)
			public T Current
			{
				get { return m_collection[m_index]; }
			}

			public bool MoveNext()
			{
				if (m_version != m_collection.m_version)
					throw new InvalidOperationException("Collection was modified; enumeration operation may not execute.");

				++m_index;
				return (m_index < m_collection.Count) ? true : false;
			}

			public void Reset()
			{
				if (m_version != m_collection.m_version)
					throw new InvalidOperationException("Collection was modified; enumeration operation may not execute.");

				m_index = -1;
			}

			// Implementation (IEnumerator)

			object IEnumerator.Current
			{
				get { return (object)(Current); }
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)



Comments and Discussions