Click here to Skip to main content
15,891,905 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 37.1K   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
// This file is part of the Loyc project. Licence: LGPL
using System;
using System.Collections.Generic;
using System.Text;

namespace Loyc.Collections
{
	[Serializable]
	public class EmptyList<T> : IList<T>, IRange<T>
	{
		public static readonly EmptyList<T> Value = new EmptyList<T>();

		public int IndexOf(T item)
		{
			return -1;
		}
		public void Insert(int index, T item)
		{
			ReadOnly();
		}
		private void ReadOnly()
		{
			throw new InvalidOperationException("Collection is read-only");
		}
		public void RemoveAt(int index)
		{
			ReadOnly();
		}
		public T this[int index]
		{
			get {
				throw new IndexOutOfRangeException();
			}
			set {
				throw new IndexOutOfRangeException();
			}
		}
		public T TryGet(int index, ref bool fail)
		{
			fail = true;
			return default(T);
		}
		public void Add(T item)
		{
			ReadOnly();
		}
		public void Clear()
		{
		}
		public bool Contains(T item)
		{
			return false;
		}
		public void CopyTo(T[] array, int arrayIndex)
		{
		}
		public int Count
		{
			get { return 0; }
		}
		public bool IsReadOnly
		{
			get { return true; }
		}
		public bool Remove(T item)
		{
			return false;
		}
		public IEnumerator<T> GetEnumerator()
		{
			return EmptyEnumerator<T>.Value;
		}
		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			return EmptyEnumerator<T>.Value;
		}
		public IRange<T> Slice(int start, int count)
		{
			return this;
		}

		#region IRange<T> members

		public bool IsEmpty
		{
			get { return true; }
		}
		public T Front
		{
			get { throw new EmptySequenceException(); }
		}
		public T Back
		{
			get { throw new EmptySequenceException(); }
		}

		public T PopBack(out bool fail)
		{
			fail = true; return default(T);
		}
		public T PopFront(out bool fail)
		{
			fail = true; return default(T);
		}

		IFRange<T> ICloneable<IFRange<T>>.Clone() { return this; }
		IBRange<T> ICloneable<IBRange<T>>.Clone() { return this; }
		IRange<T> ICloneable<IRange<T>>.Clone() { return this; }
		
		#endregion
	}
}

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