Click here to Skip to main content
15,880,469 members
Articles / Programming Languages / C#

The List Trifecta, Part 1

Rate me:
Please Sign up or sign in to vote.
4.97/5 (21 votes)
20 May 2016LGPL321 min read 37K   161   40  
The A-list is an all-purpose list, a data structure that can support most standard list operation in O(log n) time and does lots of other stuff, too
//
// Defines enhanced enumerators:
// - IBinumerator<T>: bidirectional enumerator
// - IMEnumerator<T>: unidirectional, mutable enumerator
// - IMBinumerator<T>: bidirectional, mutable enumerator
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Loyc.Collections
{
	/// <summary>Extends the "enumerator" concept to allow backward enumeration.</summary>
	/// <remarks>
	/// When MoveNext() returns false, indicating that there are no more elements,
	/// you can still call MovePrev() to go back to the previous element.
	/// </remarks>
	public interface IBinumerator<T> : IEnumerator<T>
	{
		bool MovePrev();

		// Implementing IEnumerator is such a pain in the butt. 
		// Copy and paste to get started:
		/*
		public bool MovePrev()
		{
			TODO;
		}
		public bool MoveNext()
		{
			TODO;
		}
		public T Current
		{
			get { return TODO; }
		}

		void IDisposable.Dispose() { }
		object System.Collections.IEnumerator.Current { get { return Current; } }
		public void Reset() { throw new NotSupportedException(); }
		*/
	}

	/// <summary>A mutable enumerator interface. Provides a "Remove" method like
	/// Java iterators have, and allows you to modify the current item.</summary>
	/// <remarks>Please note, not all collections will support "Remove".</remarks>
	interface IMEnumerator<T> : IEnumerator<T>
	{
		/// <summary>Gets or sets the value of the current item.</summary>
		new T Current { get; set; }
		/// <summary>Removes the current item and moves to the next one. Remember
		/// NOT to call MoveNext() immediately after Remove().</summary>
		/// <returns>True if there is a next item after this one, 
		/// false if the removed item was the last one.</returns>
		/// <exception cref="NotSupportedException">The collection does not permit
		/// this operation.</exception>
		bool Remove();
	}

	/// <summary>A mutable bidirectional enumerator interface. Please note that
	/// the "Remove" method always moves to the next item, even though the 
	/// Binumerator is capable of moving backward.</summary>
	interface IMBinumerator<T> : IBinumerator<T>, IMEnumerator<T>
	{
	}

	interface IBinumerable<T>
	{
		IBinumerator<T> Begin();
		IBinumerator<T> End();
	}
}

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)


Written By
Software Developer None
Canada Canada
Since I started programming when I was 11, I wrote the SNES emulator "SNEqr", the FastNav mapping component, the Enhanced C# programming language (in progress), the parser generator LLLPG, and LES, a syntax to help you start building programming languages, DSLs or build systems.

My overall focus is on the Language of your choice (Loyc) initiative, which is about investigating ways to improve interoperability between programming languages and putting more power in the hands of developers. I'm also seeking employment.

Comments and Discussions