Click here to Skip to main content
15,888,116 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Loyc
{
	public class InvalidStateException : InvalidOperationException
	{
		public InvalidStateException() : base(Localize.From("This object is in an invalid state.")) { }
		public InvalidStateException(string msg) : base(msg) { }
		public InvalidStateException(string msg, Exception innerException) : base(msg, innerException) { }
	}

	public class ConcurrentModificationException : InvalidOperationException
	{
		public ConcurrentModificationException() : base(Localize.From("A concurrect access was detected during modification.")) { }
		public ConcurrentModificationException(string msg) : base(msg) { }
		public ConcurrentModificationException(string msg, Exception innerException) : base(msg, innerException) { }
	}

	public class ReadOnlyException : InvalidOperationException
	{
		public ReadOnlyException() : base(Localize.From("An attempt was made to modify a read-only object.")) { }
		public ReadOnlyException(string msg) : base(msg) { }
		public ReadOnlyException(string msg, Exception innerException) : base(msg, innerException) { }
	}

	public class EmptySequenceException : InvalidOperationException
	{
		public EmptySequenceException() : base(Localize.From("Failed to access the sequence because it is empty.")) { }
	}

	public static class CheckParam
	{
		public static void IsNotNull(string paramName, object arg)
		{
			if (arg == null)
				ThrowArgumentNull(paramName);
		}
		public static void IsNotNegative(string argName, int value)
		{
			if (value < 0)
				throw new ArgumentOutOfRangeException(argName, Localize.From(@"Argument ""{0}"" value '{1}' should not be negative.", argName, value));
		}
		public static void Range(string paramName, int value, int min, int max)
		{
			if (value < min || value > max)
				ThrowOutOfRange(paramName, value, min, max);
		}
		public static void ThrowOutOfRange(string argName)
		{
			throw new ArgumentOutOfRangeException(argName);
		}
		public static void ThrowOutOfRange(string argName, int value, int min, int max)
		{
			throw new ArgumentOutOfRangeException(argName, Localize.From(@"Argument ""{0}"" value '{1}' is not within the expected range ({2}..{3})", argName, value, min, max)); 
		}
		public static void ThrowIndexOutOfRange(int index, int count)
		{
			throw new IndexOutOfRangeException(Localize.From(@"Index '{1}' is not within the expected range ({2}..{3})", index, 0, count-1)); 
		}
		public static void ThrowIndexOutOfRange(int index, int min, int max)
		{
			throw new IndexOutOfRangeException(Localize.From(@"Index '{1}' is not within the expected range ({2}..{3})", index, min, max)); 
		}
		public static void ThrowArgumentNull(string argName)
		{
			throw new ArgumentNullException(argName);
		}
		public static void Arg(string argName, bool condition)
		{
			if (!condition)
				throw new ArgumentException(string.Format("Invalid value for '{0}'", argName));
		}
	}
}

namespace Loyc.Collections
{
	using Loyc.Essentials;

	public class EnumerationException : InvalidOperationException
	{
		public EnumerationException() : base(Localize.From("The collection was modified after enumeration started.")) { }
		public EnumerationException(string msg) : base(msg) { }
		public EnumerationException(string msg, Exception innerException) : base(msg, innerException) { }
	}

	public class KeyAlreadyExistsException : InvalidOperationException
	{
		public KeyAlreadyExistsException() : base(Localize.From("The item or key being added already exists in the collection.")) { }
		public KeyAlreadyExistsException(string msg) : base(msg) { }
		public KeyAlreadyExistsException(string msg, Exception innerException) : base(msg, innerException) { }
	}

	public class EmptySequenceException : InvalidOperationException
	{
		public EmptySequenceException() : base(Localize.From("The sequence is empty and cannot be accessed.")) { }
		public EmptySequenceException(string msg) : base(msg) { }
		public EmptySequenceException(string msg, Exception innerException) : base(msg, innerException) { }
	}
}

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