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

namespace Loyc.Essentials.Collections.Implementations
{
	class ArrayOf4<T>
	{
		T t0, t1, t2, t3;
		
		T A { get { return t0; } set { t0 = value; } }
		T B { get { return t1; } set { t1 = value; } }
		T C { get { return t2; } set { t2 = value; } }
		T D { get { return t3; } set { t3 = value; } }

		public T this[int index]
		{
			get {
				Debug.Assert((uint)index < (uint)4);
				if (index < 2)
					return index > 0 ? t1 : t0;
				else
					return index == 2 ? t2 : t3;
			}
			set {
				Debug.Assert((uint)index < (uint)4);
				if (index < 2) {
					if (index == 0)
						t0 = value;
					else
						t1 = value;
				} else {
					if (index == 2)
						t2 = value;
					else
						t3 = value;
				}
			}
		}
		public T Insert(int index, T item)
		{
			T popped = t3;
			t3 = t2;
			if (index < 2) {
				t2 = t1;
				if (index == 0)
					t0 = item;
				else
					t1 = item;
				return popped;
			} else {
				if (index == 2)
					t2 = item;
				else
					t3 = item;
				return popped;
			}
		}
		public void RemoveAt(int index, T newFourth)
		{
			if (index < 2)
			{
				if (index == 0)
					t0 = t1;
				t1 = t2;
			}
			if (index == 2)
				t2 = t3;
			t3 = newFourth;
		}
		public T First
		{
			get { return t0; }
			set { t0 = value; }
		}
	}
}

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