using System;
using System.Collections.Generic;
using System.Collections;
namespace TextEditorTest
{
///
/// From: https://www.codeproject.com/Articles/20910/Generic-Gap-Buffer
///
///
public partial class GapBuffer
{
///
/// Enumerates the elements of a .
///
[Serializable]
public struct Enumerator : IEnumerator, IEnumerator
{
#region Fields
private T _current;
private int _index;
private GapBuffer _gapBuffer;
private int _version;
#endregion Fields
#region Constructors
internal Enumerator(GapBuffer buffer)
{
_gapBuffer = buffer;
_index = 0;
_version = _gapBuffer._version;
_current = default(T);
}
#endregion Constructors
#region Properties
///
/// Gets the element at the current position of the enumerator.
///
/// The element in the at the current
/// position of the enumerator.
public T Current
{
get { return _current; }
}
// Explicit IEnumerator implementation
object IEnumerator.Current
{
get
{
// Is it possible to have a current item?
if (_index == 0 || _index == (_gapBuffer.Count + 1))
throw new InvalidOperationException("Enumeration has either not started or has already finished.");
return Current;
}
}
#endregion Properties
#region Methods
///
/// Advances the enumerator to the next element of the .
///
/// true if the enumerator was successfully advanced to the next element;
/// false if the enumerator has passed the end of the collection.
///
/// The collection was modified after the enumerator was created.
///
public bool MoveNext()
{
// Check version numbers
if (this._version != this._gapBuffer._version)
throw new InvalidOperationException("Collection was modified; enumeration operation may not execute.");
// Advance the index
if (this._index < this._gapBuffer.Count)
{
this._current = this._gapBuffer[this._index];
this._index++;
return true;
}
// The pointer is at the end of the collection
this._index = this._gapBuffer.Count + 1;
this._current = default(T);
return false;
}
///
/// Releases all resources used by the .
///
public void Dispose()
{
// Nothing to release here
}
// Explicit IEnumerator implementation
void IEnumerator.Reset()
{
// Check the version
if (_version != _gapBuffer._version)
throw new InvalidOperationException("Collection was modified; enumeration operation may not execute.");
// Reset the pointer
this._index = 0;
this._current = default(T);
}
#endregion Methods
}
}
}