Click here to Skip to main content
15,886,519 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
/*
 * Created by SharpDevelop.
 * User: Pook
 * Date: 4/10/2011
 * Time: 8:47 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace Loyc.Collections
{
	/// <summary>A sequence that simply repeats the same value a specified number 
	/// of times, returned from <see cref="LCExt.Repeat{T}"/>.</summary>
	[Serializable]
	public struct Repeated<T> : IList<T>, IListSource<T>, IRange<T>
	{
		int _count;
		T _value;

		public Repeated(T value, int count)
		{
			_count = count;
			_value = value;
		}

		#region IListSource<T> Members

		public T TryGet(int index, ref bool fail)
		{
			if ((uint)index < (uint)_count)
				return _value;
			fail = true;
			return default(T);
		}

		public int Count
		{
			get { return _count; }
		}

		public T this[int index]
		{ 
			get {
				bool fail = false;
				T value = TryGet(index, ref fail);
				if (fail)
					CheckParam.ThrowIndexOutOfRange(index, _count);
				return value;
			}
		}
		
		public int IndexOf(T item)
		{
			return LCInterfaces.IndexOf(this, item);
		}

		IRange<T> IListSource<T>.Slice(int start, int count)
		{
			return Slice(start, count); 
		}
		public Slice_<T> Slice(int start, int count)
		{
			return new Slice_<T>(this, start, count); 
		}

		#endregion

		#region IList<T> Members

		T IList<T>.this[int index]
		{
			get { return this[index]; }
			set { throw new ReadOnlyException(); }
		}
		void IList<T>.Insert(int index, T item)
		{
			throw new ReadOnlyException();
		}
		void IList<T>.RemoveAt(int index)
		{
			throw new ReadOnlyException();
		}
		
		#endregion

		#region ICollection<T> Members

		void ICollection<T>.Add(T item)
		{
			throw new ReadOnlyException();
		}
		void ICollection<T>.Clear()
		{
			throw new ReadOnlyException();
		}
		void ICollection<T>.CopyTo(T[] array, int arrayIndex)
		{
			LCInterfaces.CopyTo(this, array, arrayIndex);
		}
		bool ICollection<T>.IsReadOnly
		{
			get { return true; }
		}
		bool ICollection<T>.Remove(T item)
		{
			throw new ReadOnlyException();
		}
		public bool Contains(T item)
		{
			return Enumerable.Contains(this, item);
		}

		#endregion

		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); }
		public IEnumerator<T> GetEnumerator()
		{
			bool fail = false;
			T value;
			int count = Count;
			int i = 0;
			for (;; ++i) {
				value = TryGet(i, ref fail);
				if (count != Count)
					throw new EnumerationException();
				if (fail)
					break;
				yield return value;
			}
			Debug.Assert(i >= Count);
		}

		#region IRange<T>

		public bool IsEmpty
		{
			get { return _count <= 0; }
		}

		public T Back
		{
			get { return Front; }
		}
		public T PopBack(out bool fail)
		{
			return PopBack(out fail);
		}

		public T Front
		{
			get { 
				if (_count <= 0)
					throw new EmptySequenceException(); 
				return _value; 
			}
		}
		public T PopFront(out bool fail)
		{
			if (!(fail = _count <= 0)) {
				_count--;
				return _value;
			} else
				return default(T);
		}

		IFRange<T> ICloneable<IFRange<T>>.Clone() { return Clone(); }
		IBRange<T> ICloneable<IBRange<T>>.Clone() { return Clone(); }
		IRange<T> ICloneable<IRange<T>>.Clone() { return Clone(); }
		public Repeated<T> Clone()
		{
			return new Repeated<T>(_value, _count);
		}

		#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