public void AddRemoveAdd() { // same object reference var dict = new AnalysisDictionary <string, string>(); var value = "1"; dict.Add(value, "One"); dict[value] = "Two"; Assert.AreEqual(1, dict.Count); Assert.AreEqual("Two", dict["1"]); // difference object reference, but equal. var dict2 = new AnalysisDictionary <Hashable, string>(); var one = new Hashable(1, (self, x) => x._hash == 1); var two = new Hashable(1, (self, x) => x._hash == 1); dict2.Add(one, "One"); dict2[two] = "Two"; Assert.AreEqual("Two", dict2[one]); Assert.AreEqual("Two", dict2[two]); Assert.AreEqual(1, dict2.Count); dict2 = new AnalysisDictionary <Hashable, string>(); dict2.Add(new Hashable(1), "One"); Assert.AreEqual(false, dict2.Remove(new Hashable(2))); }
public void Collision() { // difference object reference, but equal. var dict = new AnalysisDictionary <Hashable, string>(); List <Hashable> items = new List <Hashable>(); for (int i = 0; i < 25; i++) { var item = new Hashable(1); items.Add(item); dict[item] = i.ToString(); } for (int i = 0; i < items.Count; i++) { Assert.AreEqual(i.ToString(), dict[items[i]]); } for (int i = 0; i < items.Count; i++) { dict.Remove(items[i]); for (int j = i + 1; j < items.Count; j++) { Assert.AreEqual(j.ToString(), dict[items[j]]); } } }
public void RemoveExisting() { var dict = new AnalysisDictionary <Hashable, string>(); var one = new Hashable(1, (self, x) => x._hash == 1); var two = new Hashable(1, (self, x) => x._hash == 1); dict.Add(one, "One"); dict.Remove(two); Assert.AreEqual(0, dict.Count); }
/// <summary> Insert into the hash table. If the item is /// already present, then do nothing. /// </summary> /// <param name="x">the item to insert. /// </param> public virtual void insert(Hashable x) { LinkedList whichList = theLists[x.hash(theLists.Length)]; LinkedListItr itr = whichList.find(x); if (itr.PastEnd) { whichList.insert(x, whichList.zeroth()); } }
public void ThreadedReaderValueRemoved() { var comparer = new SynchronizedComparer(); var dict = new AnalysisDictionary <Hashable, string>(comparer); var key = new Hashable(1, HashEquals); dict[key] = "0"; var thread = new Thread(() => { comparer.EqualWaiting.WaitOne(); dict.Remove(key); dict[new Hashable(1)] = "1"; comparer.DictUpdated.Set(); }); thread.Start(); Assert.IsFalse(dict.ContainsKey(new Hashable(1, HashEquals))); }
public void TestThreadedReaderUpdatedValue() { var comparer = new SynchronizedComparer(); var dict = new AnalysisDictionary <Hashable, string>(comparer); var key = new Hashable(1, HashEquals); dict[key] = "0"; var thread = new Thread(() => { comparer.EqualWaiting.WaitOne(); dict.Remove(key); dict[new Hashable(1, HashEquals)] = "1"; comparer.DictUpdated.Set(); }); thread.Start(); Assert.AreEqual("1", dict[new Hashable(1, HashEquals)]); }
private static bool HashEquals(Hashable x, Hashable y) { return(x._hash == y._hash && x._equals == y._equals); }
/// <summary> Find an item in the hash table.</summary> /// <param name="x">the item to search for. /// </param> /// <returns> the matching item, or null if not found. /// </returns> public virtual Hashable find(Hashable x) { return((Hashable)theLists[x.hash(theLists.Length)].find(x).retrieve()); }
/// <summary> Remove from the hash table.</summary> /// <param name="x">the item to remove. /// </param> public virtual void remove(Hashable x) { theLists[x.hash(theLists.Length)].remove(x); }