Skip to content

Instantly share code, notes, and snippets.

@dislogical
Created April 19, 2012 19:04
Show Gist options
  • Select an option

  • Save dislogical/2423152 to your computer and use it in GitHub Desktop.

Select an option

Save dislogical/2423152 to your computer and use it in GitHub Desktop.
Dictionary data structure *REQUIRES LIST*
using System;
using System.Collections;
using System.Collections.Generic;
namespace DictionaryTest
{
/// <summary>
/// Dictionary of objects, stored by key</summary>
/// <typeparam name="TKey">Type of objects used</typeparam>
/// <typeparam name="TValue">Type of objects stored</typeparam>
/// <contributor>Colden Cullen</contributor>
class Dictionary<TKey, TValue> : IEnumerable<DictionaryPair<TKey, TValue>>
{
#region Fields, Properties
/// <summary>
/// Keys to values
/// </summary>
private List<DictionaryPair<TKey, TValue>> objects;
/// <summary>
/// Number of items in dictionary
/// </summary>
private int count;
/// <summary>
/// Keys to values
/// </summary>
public List<TKey> Keys
{
get
{
List<TKey> returnList = new List<TKey>();
foreach( DictionaryPair<TKey, TValue> p in objects )
returnList.Add( p.Key );
return returnList;
}
}
/// <summary>
/// Values stored
/// </summary>
public List<TValue> Values
{
get
{
List<TValue> returnList = new List<TValue>();
foreach( DictionaryPair<TKey, TValue> p in objects )
returnList.Add( p.Value );
return returnList;
}
}
/// <summary>
/// Number of items in dictionary
/// </summary>
public int Count
{
get { return count; }
}
#endregion
#region Indexer, Enumerator
/// <summary>
/// Finds the element with given key, return value
/// </summary>
/// <param name="key">Key associated with value to return</param>
/// <returns>Value associated with given key</returns>
public TValue this[ TKey key ]
{
get
{
for( int ii = 0; ii < objects.Count; ii++ )
if( objects[ ii ].Key.Equals( key ) )
return objects[ ii ].Value;
throw new NullReferenceException( "Key is not in dictionary" );
}
set
{
for( int ii = 0; ii < objects.Count; ii++ )
if( objects[ ii ].Key.Equals( key ) )
{
objects[ ii ].Value = value;
return;
}
throw new NullReferenceException( "Key is not in dictionary" );
}
}
/// <summary>
/// Enumerator for for loops</summary>
/// <returns></returns>
public IEnumerator<DictionaryPair<TKey, TValue>> GetEnumerator()
{
foreach( DictionaryPair<TKey, TValue> temp in objects )
yield return temp;
}
/// <summary>
/// Required for enumerating</summary>
/// <returns>
/// GetEnumerator()</returns>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
#region Constructor
public Dictionary()
{
count = 0;
objects = new List<DictionaryPair<TKey, TValue>>();
}
#endregion
#region Add
/// <summary>
/// Adds item to dictionary
/// </summary>
/// <param name="key">How to access value</param>
/// <param name="value">Value to access</param>
public void Add( TKey key, TValue value )
{
// If key is not already taken
if( !AddContains( key ) )
{
objects.Add( new DictionaryPair<TKey, TValue>( key, value ) );
count++;
}
// Else, throw exception
else throw new Exception( "Key already exists in dictionary." );
}
#endregion
#region Remove
/// <summary>
/// Removes element from lists
/// </summary>
/// <param name="key">Key at which to remove index from</param>
/// <returns>Success</returns>
public bool Remove( TKey key )
{
// For each index in dictionary
for( int ii = 0; ii < count; ii++ )
// If the key is equal to given key
if( objects[ ii ].Key.Equals( key ) )
{
// Remove elements from both lists, decreases count, returns true
objects.RemoveAt( ii );
count--;
return true;
}
// Returns false if element is not found
return false;
}
/// <summary>
/// Removes first element found from lists
/// </summary>
/// <param name="value">Value to check for</param>
/// <returns>Success</returns>
public bool Remove( TValue value )
{
// For each index in dictionary
for( int ii = 0; ii < count; ii++ )
// If the value is equal to given value
if( objects[ ii ].Value.Equals( value ) )
{
// Remove elements from both lists, decreases count, returns true
objects.RemoveAt( ii );
count--;
return true;
}
// Returns false if element is not found
return false;
}
/// <summary>
/// Resets everything
/// </summary>
public void Clear()
{
objects = new List<DictionaryPair<TKey, TValue>>();
count = 0;
}
#endregion
#region Contains
/// <summary>
/// Whether or not key is taken
/// </summary>
/// <param name="key">Key to check for</param>
/// <returns>Whether or not key exists</returns>
public bool Contains( TKey key )
{
return Keys.Contains( key );
}
/// <summary>
/// Whether or not key is taken
/// </summary>
/// <param name="key">Key to check for</param>
/// <returns>Whether or not key exists</returns>
public bool Contains( TValue value )
{
return Values.Contains( value );
}
/// <summary>
/// Whether or not key is taken, meant to be used by add method
/// </summary>
/// <param name="key">Key to check for</param>
/// <returns>Whether or not key exists</returns>
private bool AddContains( TKey key )
{
try
{
if( !this[ key ].Equals( default( TValue ) ) )
return true;
else return false;
}
catch
{
return false;
}
}
#endregion
#region Other Methods
/// <summary>
/// Whether or not dictionary is empty
/// </summary>
/// <returns>Whether or not dictionary is empty</returns>
public bool IsEmpty()
{
return count == 0;
}
/// <summary>
/// Puts values in printable form
/// </summary>
/// <returns>Dictionary_2List information</returns>
public override string ToString()
{
// String to return
string returnString = "";
// For each item, add to string
for( int ii = 0; ii < count; ii++ )
{
returnString += objects[ ii ] + "\n";
}
// Return the string
return returnString;
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment