public virtual void TestMapFactory() { TwoDimensionalMap <string, string, string> map = new TwoDimensionalMap <string, string, string>(MapFactory.IdentityHashMapFactory <string, IDictionary <string, string> >(), MapFactory.IdentityHashMapFactory <string, string>()); map.Put(new string("A"), "B", "C"); map.Put(new string("A"), "B", "C"); NUnit.Framework.Assert.AreEqual(2, map.Size()); }
public virtual TwoDimensionalMap <K2, K3, V> GetTwoDimensionalMap(K1 key1) { TwoDimensionalMap <K2, K3, V> m = map[key1]; if (m == null) { m = new TwoDimensionalMap <K2, K3, V>(); map[key1] = m; } return(m); }
/// <summary>Adds all the keys in the given TwoDimensionalMap.</summary> /// <remarks>Adds all the keys in the given TwoDimensionalMap. Returns true iff at least one key is added.</remarks> public virtual bool AddAllKeys <_T0>(TwoDimensionalMap <_T0> map) where _T0 : K1 { bool result = false; foreach (TwoDimensionalMap.Entry <K1, K2, object> entry in map) { if (Add(entry.GetFirstKey(), entry.GetSecondKey())) { result = true; } } return(result); }
public virtual ICollection <K3> ThirdKeySet() { ICollection <K3> keys = Generics.NewHashSet(); foreach (K1 k1 in map.Keys) { TwoDimensionalMap <K2, K3, V> m = map[k1]; foreach (K2 k2 in m.FirstKeySet()) { Sharpen.Collections.AddAll(keys, m.Get(k2).Keys); } } return(keys); }
public virtual ICollection <K4> FourthKeySet() { ICollection <K4> keys = Generics.NewHashSet(); foreach (K1 k1 in map.Keys) { ThreeDimensionalMap <K2, K3, K4, V> m3 = map[k1]; foreach (K2 k2 in m3.FirstKeySet()) { TwoDimensionalMap <K3, K4, V> m2 = m3.Get(k2); foreach (K3 k3 in m2.FirstKeySet()) { Sharpen.Collections.AddAll(keys, m2.Get(k3).Keys); } } } return(keys); }
public virtual void TestBasicIterator() { TwoDimensionalMap <string, string, string> map = new TwoDimensionalMap <string, string, string>(); IEnumerator <TwoDimensionalMap.Entry <string, string, string> > mapIterator = map.GetEnumerator(); NUnit.Framework.Assert.IsFalse(mapIterator.MoveNext()); map.Put("A", "B", "C"); mapIterator = map.GetEnumerator(); NUnit.Framework.Assert.IsTrue(mapIterator.MoveNext()); TwoDimensionalMap.Entry <string, string, string> entry = mapIterator.Current; NUnit.Framework.Assert.AreEqual("A", entry.GetFirstKey()); NUnit.Framework.Assert.AreEqual("B", entry.GetSecondKey()); NUnit.Framework.Assert.AreEqual("C", entry.GetValue()); NUnit.Framework.Assert.IsFalse(mapIterator.MoveNext()); map.Put("A", "E", "F"); map.Put("D", "E", "F"); map.Put("G", "H", "I"); map.Put("J", "K", "L"); NUnit.Framework.Assert.AreEqual(5, map.Size()); int count = 0; ICollection <string> firstKeys = new HashSet <string>(); ICollection <string> values = new HashSet <string>(); foreach (TwoDimensionalMap.Entry <string, string, string> e in map) { ++count; firstKeys.Add(e.GetFirstKey()); values.Add(e.GetValue()); } NUnit.Framework.Assert.IsTrue(firstKeys.Contains("A")); NUnit.Framework.Assert.IsTrue(firstKeys.Contains("D")); NUnit.Framework.Assert.IsTrue(firstKeys.Contains("G")); NUnit.Framework.Assert.IsTrue(firstKeys.Contains("J")); NUnit.Framework.Assert.IsTrue(values.Contains("C")); NUnit.Framework.Assert.IsTrue(values.Contains("F")); NUnit.Framework.Assert.IsTrue(values.Contains("I")); NUnit.Framework.Assert.IsTrue(values.Contains("L")); NUnit.Framework.Assert.AreEqual(5, count); NUnit.Framework.Assert.AreEqual(4, firstKeys.Count); NUnit.Framework.Assert.AreEqual(4, values.Count); }
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()); }
public virtual void TestAddAll() { TwoDimensionalMap <string, string, string> m1 = TwoDimensionalMap.HashMap(); m1.Put("A", "B", "1"); m1.Put("Z", "Y", "2"); m1.Put("Z", "B", "3"); m1.Put("A", "Y", "4"); m1.Put("D", "D", "5"); m1.Put("D", "F", "6"); m1.Put("K", "G", "7"); m1.Put("G", "F", "8"); TwoDimensionalMap <string, string, string> m2 = TwoDimensionalMap.TreeMap(); m2.AddAll(m1, Functions.IdentityFunction <string>()); NUnit.Framework.Assert.AreEqual(m1, m2); IFunction <string, int> valueOf = null; TwoDimensionalMap <string, string, int> m3 = TwoDimensionalMap.HashMap(); m3.AddAll(m1, valueOf); NUnit.Framework.Assert.AreEqual(m1.Size(), m3.Size()); NUnit.Framework.Assert.AreEqual(3, m3.Get("Z", "B")); }
public virtual void TestTreeMapIterator() { TwoDimensionalMap <string, string, string> map = new TwoDimensionalMap <string, string, string>(MapFactory.TreeMapFactory <string, IDictionary <string, string> >(), MapFactory.TreeMapFactory <string, string>()); map.Put("A", "B", "C"); map.Put("Z", "Y", "X"); map.Put("Z", "B", "C"); map.Put("A", "Y", "X"); map.Put("D", "D", "D"); map.Put("D", "F", "E"); map.Put("K", "G", "B"); map.Put("G", "F", "E"); map.Put("D", "D", "E"); // sneaky overwritten entry NUnit.Framework.Assert.AreEqual(8, map.Size()); IEnumerator <TwoDimensionalMap.Entry <string, string, string> > mapIterator = map.GetEnumerator(); TwoDimensionalMap.Entry <string, string, string> entry = mapIterator.Current; NUnit.Framework.Assert.AreEqual("A", entry.GetFirstKey()); NUnit.Framework.Assert.AreEqual("B", entry.GetSecondKey()); NUnit.Framework.Assert.AreEqual("C", entry.GetValue()); entry = mapIterator.Current; NUnit.Framework.Assert.AreEqual("A", entry.GetFirstKey()); NUnit.Framework.Assert.AreEqual("Y", entry.GetSecondKey()); NUnit.Framework.Assert.AreEqual("X", entry.GetValue()); entry = mapIterator.Current; NUnit.Framework.Assert.AreEqual("D", entry.GetFirstKey()); NUnit.Framework.Assert.AreEqual("D", entry.GetSecondKey()); NUnit.Framework.Assert.AreEqual("E", entry.GetValue()); entry = mapIterator.Current; NUnit.Framework.Assert.AreEqual("D", entry.GetFirstKey()); NUnit.Framework.Assert.AreEqual("F", entry.GetSecondKey()); NUnit.Framework.Assert.AreEqual("E", entry.GetValue()); entry = mapIterator.Current; NUnit.Framework.Assert.AreEqual("G", entry.GetFirstKey()); NUnit.Framework.Assert.AreEqual("F", entry.GetSecondKey()); NUnit.Framework.Assert.AreEqual("E", entry.GetValue()); entry = mapIterator.Current; NUnit.Framework.Assert.AreEqual("K", entry.GetFirstKey()); NUnit.Framework.Assert.AreEqual("G", entry.GetSecondKey()); NUnit.Framework.Assert.AreEqual("B", entry.GetValue()); entry = mapIterator.Current; NUnit.Framework.Assert.AreEqual("Z", entry.GetFirstKey()); NUnit.Framework.Assert.AreEqual("B", entry.GetSecondKey()); NUnit.Framework.Assert.AreEqual("C", entry.GetValue()); entry = mapIterator.Current; NUnit.Framework.Assert.AreEqual("Z", entry.GetFirstKey()); NUnit.Framework.Assert.AreEqual("Y", entry.GetSecondKey()); NUnit.Framework.Assert.AreEqual("X", entry.GetValue()); NUnit.Framework.Assert.IsFalse(mapIterator.MoveNext()); IEnumerator <string> valueIterator = map.ValueIterator(); NUnit.Framework.Assert.IsTrue(valueIterator.MoveNext()); NUnit.Framework.Assert.AreEqual("C", valueIterator.Current); NUnit.Framework.Assert.AreEqual("X", valueIterator.Current); NUnit.Framework.Assert.AreEqual("E", valueIterator.Current); NUnit.Framework.Assert.AreEqual("E", valueIterator.Current); NUnit.Framework.Assert.AreEqual("E", valueIterator.Current); NUnit.Framework.Assert.AreEqual("B", valueIterator.Current); NUnit.Framework.Assert.AreEqual("C", valueIterator.Current); NUnit.Framework.Assert.AreEqual("X", valueIterator.Current); NUnit.Framework.Assert.IsFalse(valueIterator.MoveNext()); }
public virtual V Put(K1 key1, K2 key2, K3 key3, V value) { TwoDimensionalMap <K2, K3, V> m = GetTwoDimensionalMap(key1); return(m.Put(key2, key3, value)); }
public static Edu.Stanford.Nlp.Util.TwoDimensionalSet <K1, K2> HashSet <K1, K2>() { return(new Edu.Stanford.Nlp.Util.TwoDimensionalSet <K1, K2>(TwoDimensionalMap.HashMap <K1, K2, bool>())); }
public TwoDimensionalSet(TwoDimensionalMap <K1, K2, bool> backingMap) { this.backingMap = backingMap; }
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); }
internal TwoDimensionalMapIterator(TwoDimensionalMap <K1, K2, V> map) { outerIterator = map.map.GetEnumerator(); PrimeNext(); }
internal TwoDimensionalMapValueIterator(TwoDimensionalMap <K1, K2, V> map) { entryIterator = map.GetEnumerator(); }