Click here to Skip to main content
15,895,084 members
Articles / Programming Languages / C# 4.0

The List Trifecta, Part 2

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
7 Sep 2013LGPL310 min read 28.7K   317   12  
The BDictionary is like a Dictionary mashed up with List<T>. BList and BMultiMap also say hello.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Loyc.Collections
{
	public static class Range
	{
		/// <summary>Advances by the specified number of elements.</summary>
		/// <param name="count">Number of items to remove from the beginning of the
		/// range. If count is higher than the number of items in the range, no
		/// exception is thrown but the return value will be less than this value.</param>
		/// <returns>Returns the number of items skipped.</returns>
		public static int Skip<R, T>(ref R range, int count) where R : IFRange<T>
		{
			bool fail;
			for (int i = 0; i < count; i++) {
				range.PopFront(out fail);
				if (fail) return i;
			}
			return count;
		}
		public static int DropLast<R, T>(ref R range, int count) where R : IBRange<T>
		{
			bool fail;
			for (int i = 0; i < count; i++)
			{
				range.PopBack(out fail);
				if (fail) return i;
			}
			return count;
		}
		public static T PopFirst<R, T>(ref R range, T defaultValue) where R : IFRange<T>
		{
			bool fail;
			T next = range.PopFront(out fail);
			if (!fail)
				return next;
			else
				return defaultValue;
		}
		public static T PopFirst<R,T>(ref R range) where R:IFRange<T>
		{
			bool fail;
			T next = range.PopFront(out fail);
			if (fail) throw new EmptySequenceException();
			return next;
		}
		public static T PopLast<R, T>(ref R range, T defaultValue) where R : IBRange<T>
		{
			bool fail;
			T next = range.PopBack(out fail);
			if (!fail)
				return next;
			else
				return defaultValue;
		}
		public static T PopLast<R, T>(ref R range) where R : IBRange<T>
		{
			bool fail;
			T next = range.PopBack(out fail);
			if (fail) throw new EmptySequenceException();
			return next;
		}

		public static bool Contains<R,T>(this R range, IRangeEx<R,T> other) where R : IRangeEx<R,T>, ICloneable<R>
		{
			int r0 = range.SliceStart, o0 = other.SliceStart;
			return r0 <= o0 && r0 + range.Count >= o0 + other.Count
				&& object.Equals(range.InnerList, other.InnerList);
		}
		public static bool Overlaps<R, T>(this R range, IRangeEx<R, T> other) where R : IRangeEx<R,T>, ICloneable<R>
		{
			int r0 = range.SliceStart, r1 = r0 + range.Count;
			int o0 = other.SliceStart, o1 = o0 + other.Count;
			return r1 > o0 && o1 > r0
				&& object.Equals(range.InnerList, other.InnerList);
		}

		public static Repeated<T> Repeat<T>(T value, int count)
		{
			return new Repeated<T>(value, count);
		}
		public static Repeated<T> Single<T>(T value)
		{
			return new Repeated<T>(value, 1);
		}
		/// <summary>Returns <c>new IntRange(start, count)</c>.</summary>
		public static IntRange IntRange(int start, int count)
		{
			return new IntRange(start, count);
		}
	}


}

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