I have implemented a very basic hash data structure with basic set/retrieve methods. A good example of use of this implementation would be a phone-book. I have used as underneath storage structure an array consisting of linked lists.
Please a code review within these requirements.
public class MyHash
{
private LinkedList<string>[] storage;
public MyHash()
{
storage = new LinkedList<string>[26]; //english alphabet
}
private int HashFunction(string key)
{
//maps first character of name to a 0:25 index
return key.ToLower().ToCharArray()[0] - 97;
}
public string this[string key]
{
get {
var list = storage[HashFunction(key)];
if (list != null)
{
foreach (var item in list)
{
if (item.Split(':')[0] == key)
return item.Split(':')[1];
}
}
return "Not found!";
}
set {
if (storage[HashFunction(key)] == null)
storage[HashFunction(key)] = new LinkedList<string>();
storage[HashFunction(key)].AddLast(key + ":" + value);
}
}
}
key + ":" + valueif you split it later and get the second item. What if a value contains a:too? Why don't you simply addvalue? Very weird. \$\endgroup\$:characters. Another problem is that your class fails to handle keys that don't start witha-z: keys like"1"or"中"cause anIndexOutOfRangeException. Yet another problem is that"Not found!"could actually be a valid input value, and besides that it's an undocumented 'magic' value. Besides that, you may want to be more specific in what kind of feedback you're looking for - is this for educational purposes, or for actual use? \$\endgroup\$