Click here to Skip to main content
15,890,897 members
Home / Discussions / C#
   

C#

 
GeneralRe: Threads Pin
Luc Pattyn21-Jan-10 0:47
sitebuilderLuc Pattyn21-Jan-10 0:47 
GeneralRe: Threads Pin
Harvey Saayman21-Jan-10 0:51
Harvey Saayman21-Jan-10 0:51 
GeneralRe: Threads Pin
Matty2221-Jan-10 11:35
Matty2221-Jan-10 11:35 
AnswerRe: Threads Pin
Harvey Saayman20-Jan-10 19:40
Harvey Saayman20-Jan-10 19:40 
QuestionHow can I copy a multidimensional List Pin
leekangjae20-Jan-10 15:23
leekangjae20-Jan-10 15:23 
AnswerRe: How can I copy a multidimensional List Pin
Not Active20-Jan-10 15:39
mentorNot Active20-Jan-10 15:39 
GeneralRe: How can I copy a multidimensional List Pin
leekangjae20-Jan-10 17:41
leekangjae20-Jan-10 17:41 
AnswerRe: How can I copy a multidimensional List Pin
AspDotNetDev20-Jan-10 16:25
protectorAspDotNetDev20-Jan-10 16:25 
Here you go:
C#
public class SlimList<T> : IList<T>
{

	#region Constants

	private const int OVERHEAD = 31;
	private const int MAX_CAPACITY = 1 << 30;
	private const string NEGATIVE_CAPACITY = "Capacity cannot be less than 0.";

	#endregion


	#region Variables

	private T[][] items;
	private int count;
	private int capacity;
	private int capacityIndex;
	private int lastSize;

	#endregion


	#region Properties

	/// <summary>
	/// Gets or sets an item at the specified zero-based index.
	/// </summary>
	/// <param name="index">The zero-based index of the item to get or set.</param>
	/// <returns>The item at the specified zero-based index.</returns>
	public T this[int index]
	{
		get
		{

			// Validate index.
			ValidateIndex(index);


			// Index of first array.
			int firstIndex = (index == 0 ? 0 : (int)Math.Truncate(Math.Log(index, 2)));


			// Calculate index into the sub-array.
			int baseIndex = (firstIndex == 0 ? 0 : (1 << firstIndex));
			int offset = index - baseIndex;


			// Return item.
			return items[firstIndex][offset];

		}
		set
		{

			// Validate index.
			ValidateIndex(index);


			// Index of first array.
			int firstIndex = (index == 0 ? 0 : (int)Math.Truncate(Math.Log(index, 2)));


			// Calculate index into the sub-array.
			int baseIndex = (firstIndex == 0 ? 0 : (1 << firstIndex));
			int offset = index - baseIndex;


			// Assign item.
			items[firstIndex][offset] = value;

		}
	}


	/// <summary>
	/// The number of items currently in the list.
	/// </summary>
	public int Count
	{
		get
		{
			return this.count;
		}
	}


	/// <summary>
	/// The number of items the list can currently hold before increasing size.
	/// </summary>
	public int Capacity
	{
		get
		{
			return this.capacity;
		}
	}


	/// <summary>
	/// Always returns false.
	/// </summary>
	public bool IsReadOnly
	{
		get
		{
			return false;
		}
	}


	#endregion


	#region Constructors

	/// <summary>
	/// Creates an empty list.
	/// </summary>
	public SlimList()
	{
		this.Clear();
	}


	/// <summary>
	/// Creates a list with a capacity at least as great as the specified capacity.
	/// </summary>
	/// <param name="initialCapacity">The initial minimum capacity.</param>
	/// <remarks>
	/// As capacities increase exponentially, the specified capacity is used as an minimum.
	/// The actual initial capacity may actually be nearly twice as much as the specified
	/// initial capacity.
	/// </remarks>
	public SlimList(int initialCapacity)
	{

		// Initialize empty list.
		this.Clear();
		if (initialCapacity == 0)
		{
			return;
		}
		else if (initialCapacity < 0)
		{
			throw new InvalidOperationException(NEGATIVE_CAPACITY);
		}


		// Make capacity 2.
		this.items[0] = new T[2];
		this.capacity = 2;
		this.capacityIndex = 0;
		this.lastSize = 1;


		// Increase capacity until it is equal to or more than the requested capacity.
		while (this.capacity < initialCapacity)
		{
			this.capacityIndex++;
			this.lastSize <<= 1;
			this.capacity <<= 1;
			this.items[this.capacityIndex] = new T[this.lastSize];
		}
		
	}


	/// <summary>
	/// Creates a list from the elements in another collection.
	/// </summary>
	/// <param name="collection">
	/// The collection from which elements will be copied to the new list.
	/// </param>
	public SlimList(IEnumerable<T> collection)
	{

		// Initialize empty list.
		this.Clear();
		
		
		// Add each item in the collection to this list.
		foreach (T item in collection)
		{
			this.Add(item);
		}
		
	}

	#endregion

	
	#region Methods

	/// <summary>
	/// Get the index of an item.
	/// </summary>
	/// <param name="item">The item to get the index of.</param>
	/// <returns>The index, or -1 if it could not be found.</returns>
	public int IndexOf(T item)
	{
		EqualityComparer<T> comparer = EqualityComparer<T>.Default;
		for (int i = 0; i < count; i++)
		{
			if(comparer.Equals(item, this[i]))
			{
				return i;
			}
		}
		return -1;
	}


	/// <summary>
	/// Confirms the specified index is valid.
	/// </summary>
	/// <param name="index">The index to check.</param>
	/// <remarks>
	/// Will throw an exception if the index is out of the valid range.
	/// </remarks>
	private void ValidateIndex(int index)
	{
		if (index < 0 || index >= count)
		{
			throw new IndexOutOfRangeException();
		}
	}


	/// <summary>
	/// Inserts an item at the specified index.
	/// </summary>
	/// <param name="index">The index to insert the item at.</param>
	/// <param name="item">The item to insert.</param>
	public void Insert(int index, T item)
	{

		// Validate index.
		this.ValidateIndex(index);


		// Increase the list size.
		this.Add(this[this.count - 1]);


		// Make room for the new item.
		for (int i = this.count - 1; i > index; i--)
		{
			this[i] = this[i - 1];
		}


		// Assign the item to the specified index.
		this[index] = item;

	}


	/// <summary>
	/// Remove the item at the specified index from the list.
	/// </summary>
	/// <param name="index">
	/// The index of the item to remove.
	/// </param>
	public void RemoveAt(int index)
	{

		// Ensure index is valid.
		this.ValidateIndex(index);


		// Shift elements.
		for (int i = index; i + 1 < this.count; i++)
		{
			this[i] = this[i + 1];
		}


		// Decrement item count.
		this.count--;

	}

	
	/// <summary>
	/// Add the specified item to the end of the list.
	/// </summary>
	/// <param name="item">
	/// The item to add.
	/// </param>
	public void Add(T item)
	{

		// Increment count.
		count++;


		// Need to add more space to the list?
		if (count > capacity)
		{

			// Handle initial states.
			if (capacity == 0)
			{
				capacity = 1;
				this.lastSize = 1;
			}
			else if (capacity == 2)
			{
				this.lastSize = 1;
			}
			else if (capacity == MAX_CAPACITY)
			{
				throw new InvalidOperationException("The list is full.");
			}


			// Increase capacity.
			capacity <<= 1;
			this.capacityIndex++;
			this.lastSize <<= 1;


			// Create new nested array of items.
			this.items[this.capacityIndex] = new T[this.lastSize];

		}


		// Assign item to last index.
		this[count - 1] = item;

	}


	/// <summary>
	/// Empties the list.
	/// </summary>
	public void Clear()
	{

		// Reset array of arrays.
		items = new T[OVERHEAD][];


		// Reset all size-related values.
		this.count = 0;
		this.capacity = 0;
		this.capacityIndex = -1;
		this.lastSize = 0;

	}


	/// <summary>
	/// Checks if the specified item exists in this list.
	/// </summary>
	/// <param name="item">
	/// The item to search for.
	/// </param>
	/// <returns>
	/// True, if the item was found; otherwise, false.
	/// </returns>
	public bool Contains(T item)
	{
		return this.IndexOf(item) >= 0;
	}


	/// <summary>
	/// Copies the values in this list to an array.
	/// </summary>
	/// <param name="array">The array to copy the values to.</param>
	/// <param name="arrayIndex">
	/// The index into the destination array to start placing values.
	/// </param>
	public void CopyTo(T[] array, int arrayIndex)
	{
		for (int i = 0; i < this.count; i++)
		{
			array[i + arrayIndex] = this[i];
		}
	}

	
	/// <summary>
	/// Removes the specified item from this list.
	/// </summary>
	/// <param name="item">
	/// The item to remove.
	/// </param>
	/// <returns>
	/// True, if the item was found and removed; otherwise, false.
	/// </returns>
	public bool Remove(T item)
	{

		// Variables.
		int index = this.IndexOf(item);
		bool removed;


		// If found, remove item.
		if (index >= 0)
		{
			this.RemoveAt(index);
			removed = true;
		}
		else
		{
			removed = false;
		}


		// Return success or failure.
		return removed;

	}
	

	/// <summary>
	/// Returns an enumerator that can iterate over each item in the list.
	/// </summary>
	/// <returns>The enumerator.</returns>
	public IEnumerator<T> GetEnumerator()
	{
		for (int i = 0; i < this.count; i++)
		{
			yield return this[i];
		}
	}


	/// <summary>
	/// Returns an enumerator that can iterate over each item in the list.
	/// </summary>
	/// <returns>
	/// The enumerator.
	/// </returns>
	/// <remarks>
	/// This version just calls the generic version.
	/// </remarks>
	IEnumerator IEnumerable.GetEnumerator()
	{
		return this.GetEnumerator();
	}

	#endregion

}

Actually, none of that has anything to do with multidimensional lists. See, isn't it annoying when somebody posts a bunch of code that is completely unrelated to the topic at hand? Also, see how nice my code looks (with all the colors and tabs)? You can do that by surrounding your code with a PRE tag and specifying the lang attribute (in your case, it would be C#). For example, you could type this:
HTML
<pre lang="C#">int x = 5;</pre>



GeneralRe: How can I copy a multidimensional List Pin
Luc Pattyn20-Jan-10 16:51
sitebuilderLuc Pattyn20-Jan-10 16:51 
GeneralRe: How can I copy a multidimensional List Pin
AspDotNetDev20-Jan-10 17:08
protectorAspDotNetDev20-Jan-10 17:08 
GeneralRe: How can I copy a multidimensional List Pin
AspDotNetDev20-Jan-10 17:10
protectorAspDotNetDev20-Jan-10 17:10 
GeneralRe: How can I copy a multidimensional List Pin
AspDotNetDev20-Jan-10 17:15
protectorAspDotNetDev20-Jan-10 17:15 
GeneralRe: How can I copy a multidimensional List Pin
Luc Pattyn20-Jan-10 17:47
sitebuilderLuc Pattyn20-Jan-10 17:47 
QuestionSimulate keypress Pin
Matt Cavanagh20-Jan-10 14:12
Matt Cavanagh20-Jan-10 14:12 
AnswerRe: Simulate keypress Pin
Bekjong20-Jan-10 16:07
Bekjong20-Jan-10 16:07 
QuestionProblem with AutoResetEvent Array and WaitHandle.WaitAll() Pin
FJJCENTU20-Jan-10 13:27
FJJCENTU20-Jan-10 13:27 
AnswerRe: Problem with AutoResetEvent Array and WaitHandle.WaitAll() Pin
Bekjong20-Jan-10 15:50
Bekjong20-Jan-10 15:50 
AnswerRe: Problem with AutoResetEvent Array and WaitHandle.WaitAll() Pin
Giorgi Dalakishvili20-Jan-10 19:17
mentorGiorgi Dalakishvili20-Jan-10 19:17 
QuestionManaged and Unmanaged code Pin
3bood.ghzawi20-Jan-10 13:03
3bood.ghzawi20-Jan-10 13:03 
AnswerRe: Managed and Unmanaged code Pin
Bekjong20-Jan-10 15:53
Bekjong20-Jan-10 15:53 
AnswerRe: Managed and Unmanaged code Pin
Abhinav S20-Jan-10 17:33
Abhinav S20-Jan-10 17:33 
QuestionDisplay highlighted data from Listbox in Textboxes Pin
CarlMartin1020-Jan-10 13:02
CarlMartin1020-Jan-10 13:02 
AnswerRe: Display highlighted data from Listbox in Textboxes Pin
Luc Pattyn20-Jan-10 13:27
sitebuilderLuc Pattyn20-Jan-10 13:27 
GeneralRe: Display highlighted data from Listbox in Textboxes Pin
CarlMartin1020-Jan-10 13:31
CarlMartin1020-Jan-10 13:31 
GeneralRe: Display highlighted data from Listbox in Textboxes Pin
Luc Pattyn20-Jan-10 13:37
sitebuilderLuc Pattyn20-Jan-10 13:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.