public virtual void TestBasicOperations() { TwoDimensionalMap <string, string, string> map = new TwoDimensionalMap <string, string, string>(); NUnit.Framework.Assert.AreEqual(0, map.Size()); NUnit.Framework.Assert.IsTrue(map.IsEmpty()); map.Put("A", "B", "C"); NUnit.Framework.Assert.AreEqual("C", map.Get("A", "B")); NUnit.Framework.Assert.AreEqual(1, map.Size()); NUnit.Framework.Assert.IsFalse(map.IsEmpty()); NUnit.Framework.Assert.IsTrue(map.Contains("A", "B")); NUnit.Framework.Assert.IsFalse(map.Contains("A", "C")); NUnit.Framework.Assert.IsFalse(map.Contains("B", "F")); map.Put("A", "B", "D"); NUnit.Framework.Assert.AreEqual("D", map.Get("A", "B")); NUnit.Framework.Assert.AreEqual(1, map.Size()); NUnit.Framework.Assert.IsFalse(map.IsEmpty()); NUnit.Framework.Assert.IsTrue(map.Contains("A", "B")); NUnit.Framework.Assert.IsFalse(map.Contains("A", "C")); NUnit.Framework.Assert.IsFalse(map.Contains("B", "F")); map.Put("A", "C", "E"); NUnit.Framework.Assert.AreEqual("D", map.Get("A", "B")); NUnit.Framework.Assert.AreEqual("E", map.Get("A", "C")); NUnit.Framework.Assert.AreEqual(2, map.Size()); NUnit.Framework.Assert.IsFalse(map.IsEmpty()); NUnit.Framework.Assert.IsTrue(map.Contains("A", "B")); NUnit.Framework.Assert.IsTrue(map.Contains("A", "C")); NUnit.Framework.Assert.IsFalse(map.Contains("B", "F")); map.Put("B", "F", "G"); NUnit.Framework.Assert.AreEqual("D", map.Get("A", "B")); NUnit.Framework.Assert.AreEqual("E", map.Get("A", "C")); NUnit.Framework.Assert.AreEqual("G", map.Get("B", "F")); NUnit.Framework.Assert.AreEqual(3, map.Size()); NUnit.Framework.Assert.IsFalse(map.IsEmpty()); NUnit.Framework.Assert.IsTrue(map.Contains("A", "B")); NUnit.Framework.Assert.IsTrue(map.Contains("A", "C")); NUnit.Framework.Assert.IsTrue(map.Contains("B", "F")); map.Clear(); NUnit.Framework.Assert.AreEqual(0, map.Size()); NUnit.Framework.Assert.IsTrue(map.IsEmpty()); }
private bool Equals(Edu.Stanford.Nlp.Util.ArrayCoreMap other) { TwoDimensionalMap <ICoreMap, ICoreMap, bool> calledMap = equalsCalled.Get(); bool createdCalledMap = (calledMap == null); if (createdCalledMap) { calledMap = TwoDimensionalMap.IdentityHashMap(); equalsCalled.Set(calledMap); } // Note that for the purposes of recursion, we assume the two maps // are equals. The two maps will therefore be equal if they // encounter each other again during the recursion unless there is // some other key that causes the equality to fail. // We do not need to later put false, as the entire call to equals // will unwind with false if any one equality check returns false. // TODO: since we only ever keep "true", we would rather use a // TwoDimensionalSet, but no such thing exists if (calledMap.Contains(this, other)) { return(true); } bool result = true; calledMap.Put(this, other, true); calledMap.Put(other, this, true); if (this.size != other.size) { result = false; } else { for (int i = 0; i < this.size; i++) { // test if other contains this key,value pair bool matched = false; for (int j = 0; j < other.size; j++) { if (this.keys[i] == other.keys[j]) { if ((this.values[i] == null && other.values[j] != null) || (this.values[i] != null && other.values[j] == null)) { matched = false; break; } if ((this.values[i] == null && other.values[j] == null) || (this.values[i].Equals(other.values[j]))) { matched = true; break; } } } if (!matched) { result = false; break; } } } if (createdCalledMap) { equalsCalled.Set(null); } return(result); }