public HyphenationTree() { stoplist = new HashMap<string, IList<object>>(23); // usually a small table classmap = new TernaryTree(); vspace = new ByteVector(); vspace.Alloc(1); // this reserves index 0, which we don't use }
public void TestAddingNullKeysInTree() { TernaryTree<string> tree = new TernaryTree<string>(); try { tree.Add(null, null); Assert.Fail("null key allowed."); } catch (InvalidOperationException e) { Assert.AreEqual("Keys cannot be null or empty.", e.Message); } try { tree.Add("", null); Assert.Fail("empty key allowed."); } catch (InvalidOperationException e) { Assert.AreEqual("Keys cannot be null or empty.", e.Message); } Assert.AreEqual(0, tree.Length); tree.Add(" ", "white space"); Assert.AreEqual(1, tree.Length); tree.Add(" a ", "another key with space"); Assert.AreEqual("white space", tree[" "]); }
public HyphenationTree() { stoplist = new Dictionary<>(23); // usually a small table classmap = new TernaryTree(); vspace = new ByteVector(); vspace.alloc(1); // this reserves index 0, which we don't use }
public void Copy_To_Returns_Correct_Result() { TernaryTree <int> subject = TernaryTree <int> .Create(_keyValueCollection); KeyValuePair <string, int>[] actualResult = new KeyValuePair <string, int> [subject.Count]; subject.CopyTo(actualResult, 0); Assert.That(actualResult, Is.EqualTo(_sortedKVPairs)); }
public void TestAddingNullValuesInTree() { TernaryTree<string> tree = new TernaryTree<string>(); tree.Add("Key1", null); Assert.AreEqual(1, tree.Length); Assert.IsNull(tree["Key1"]); Assert.IsTrue(tree.Contains("Key1")); }
public void Keys_Returns_A_Sorted_Collection_Of_All_Keys() { TernaryTree <int> subject = TernaryTree <int> .Create(_keyValueDictionary); ICollection <string> actualResult = subject.Keys(); Assert.That(actualResult, Is.EqualTo(_sortedKeys)); }
public void This_Int_Throws_IndexOutOfRangeException(int index) { TernaryTree <int> subject = TernaryTree <int> .Create(_keyValueCollection); Assert.Throws <IndexOutOfRangeException>(() => { string actualResult = subject[index]; }); }
public void TestAddingNullValuesInTree() { TernaryTree <string> tree = new TernaryTree <string>(); tree.Add("Key1", null); Assert.AreEqual(1, tree.Length); Assert.IsNull(tree["Key1"]); Assert.IsTrue(tree.Contains("Key1")); }
public void Count_After_Consecutive_Calls_To_Remove() { TernaryTree <string> subject = TernaryTree <string> .Create(_keys); foreach (string key in _keys) { subject.Remove(key); } Assert.That(subject.Count, Is.EqualTo(0)); }
static void TernaryTreeTest() { TernaryTree <int> testTree = new TernaryTree <int>(); ArrayList <int> list = new ArrayList <int>(); list.appendAll(new int[] { 5, 4, 9, 5, 7, 2, 2 }); testTree.addToTree(list); testTree.print(); }
public static void TargetSumUsingSubsetOfElements() { int[] a = { 1, 3, 5 }; TernaryTree tree = new TernaryTree(); tree.Root = new Node(6); int x = TargetSumUsingSubsetOfElementsUtils(6, 0, tree.Root); Console.WriteLine(x); }
public void Add_Key_Value_Throws_ArgumentException_For_Duplicate_Keys() { TernaryTree <int> subject = new TernaryTree <int>(); Assert.Throws <ArgumentException>(() => { subject.Add(_keys[0], 0); subject.Add(_keys[0], 0); }); }
public void Count_After_Consecutive_Add_KeyValuePair_Calls() { TernaryTree <int> subject = new TernaryTree <int>(); foreach (KeyValuePair <string, int> kvPair in _keyValueCollection) { subject.Add(kvPair); } Assert.That(subject.Count, Is.EqualTo(_keyValueCollection.Count)); }
public void Add_Key_Throws_ArgumentException_When_Adding_Duplicate_Keys() { TernaryTree <int> subject = new TernaryTree <int>(); Assert.Throws <ArgumentException>(() => { subject.Add(_keys[0]); subject.Add(_keys[0]); }); }
public void Count_After_Consecutive_Add_Key_Calls() { TernaryTree <string> subject = new TernaryTree <string>(); foreach (string key in _keys) { subject.Add(key); } Assert.That(subject.Count, Is.EqualTo(_keys.Length)); }
public void This_Int_Returns_Correct_Key() { TernaryTree <int> subject = TernaryTree <int> .Create(_keyValueDictionary); Assert.Multiple(() => { for (int i = 0; i < _sortedKeys.Length; i++) { Assert.That(subject[i], Is.EqualTo(_sortedKeys[i])); } }); }
public void Add_KeyValuePair_Throws_ArgumentException_For_Duplicate_Keys() { TernaryTree <int> subject = new TernaryTree <int>(); KeyValuePair <string, int> kvPair = new KeyValuePair <string, int>(_keys[0], 0); Assert.Throws <ArgumentException>(() => { subject.Add(kvPair); subject.Add(kvPair); }); }
public void RegularSearch() { // Arrange var tree = new TernaryTree(Language.Swedish); tree.Add("abcd", "aecd"); // Act var matches = tree.Matches("aecd"); // Assert Assert.AreEqual(1, matches.Count); }
public void Tree_ContainsKey_All_Added_Keys() { TernaryTree <string> subject = TernaryTree <string> .Create(_keys); Assert.Multiple(() => { foreach (string key in _keys) { Assert.That(subject.ContainsKey(key)); } }); }
public void VowelsSearch() { // Arrange var tree = new TernaryTree(Language.Swedish); tree.Add("abcd", "ebcd", "tbcd", "ubcd"); // Act var matches = tree.Matches("@bcd"); // Assert Assert.AreEqual(3, matches.Count); }
public void TestAddingDuplicateKeysInTree() { TernaryTree <string> tree = new TernaryTree <string>(); tree.Add("Key1", "value1"); Assert.AreEqual(1, tree.Length); Assert.AreEqual("value1", tree["Key1"]); tree.Add("Key1", "value2"); Assert.AreEqual(1, tree.Length); Assert.AreEqual("value2", tree["Key1"]); }
public void Enumerator_Returns_All_Elements_In_Order() { TernaryTree <int> subject = TernaryTree <int> .Create(_keyValueCollection); List <KeyValuePair <string, int> > actualResult = new List <KeyValuePair <string, int> >(); foreach (KeyValuePair <string, int> kvPair in subject) { actualResult.Add(kvPair); } Assert.That(actualResult, Is.EqualTo(_sortedKeyValueCollection)); }
public void ConsonantsSearch() { // Arrange var tree = new TernaryTree(Language.Swedish); tree.Add("abeg", "abhg", "abfg", "abug", "abtg"); // Act var matches = tree.Matches("ab#g"); // Assert Assert.AreEqual(3, matches.Count); }
public void TestAddingDuplicateKeysInTree() { TernaryTree<string> tree = new TernaryTree<string>(); tree.Add("Key1", "value1"); Assert.AreEqual(1, tree.Length); Assert.AreEqual("value1", tree["Key1"]); tree.Add("Key1", "value2"); Assert.AreEqual(1, tree.Length); Assert.AreEqual("value2", tree["Key1"]); }
public void NearSearch() { // Arrange var tree = new TernaryTree(Language.Swedish); tree.Add("abcde", "abce", "abcf"); // Act var matches = tree.NearSearch("abc"); // Assert Assert.AreEqual(2, matches.Count); }
public void TestTreeNearSearchForValues() { TernaryTree <string> tree = new TernaryTree <string>(); var InputKeys = "aback abacus abalone abandon abase abash abate abbas abbe abbey abbot Abbott".ToLower().Split(' '); foreach (var key in InputKeys) { tree.Add(key, "value of " + key); } var near = tree.NearSearch("abacus", 5); int count = getCount(near); Assert.AreEqual(10, count); }
public void Traverse() { // Arrange var tree = new TernaryTree(Language.Swedish); tree.Add("abe", "abc", "abd"); var visited = new List<string>(); // Act tree.Traverse(visited.Add); // Assert var expected = new List<string> { "abc", "abd", "abe" }; Assert.AreEqual(expected.Count, visited.Count); Assert.AreEqual(expected[0], visited[0]); Assert.AreEqual(expected[1], visited[1]); Assert.AreEqual(expected[2], visited[2]); }
public void TestCreateTerenaryTree() { TernaryTree<string> tree = new TernaryTree<string>(); var InputKeys = "aback abacus abalone abandon abase abash abate abbas abbe abbey abbot Abbott".ToLower().Split(' '); foreach (var key in InputKeys) { tree.Add(key, "value of " + key); } Assert.IsNotNull(tree); Assert.AreEqual(InputKeys.Length, tree.Length); IEnumerable<string> Keys = tree.Keys; Assert.IsNotNull(Keys); int count = getCount(Keys); Assert.AreEqual(InputKeys.Length, count); }
public void Regex_Match_Test(string pattern, string[] matchingKeys, string[] nonMatchingKeys) { TernaryTree <int> subject = TernaryTree <int> .Create(matchingKeys); foreach (string nonMatchingKey in nonMatchingKeys) { subject.Add(nonMatchingKey); } HashSet <string> actualResult = new HashSet <string>(subject.Match(pattern)); Assert.Multiple(() => { Assert.That(subject.Count, Is.EqualTo(matchingKeys.Length + nonMatchingKeys.Length)); Assert.That(actualResult.Count, Is.EqualTo(matchingKeys.Length)); foreach (string key in matchingKeys) { Assert.That(actualResult.Contains(key)); } }); }
private static int TargetSumUsingSubsetOfElementsUtils(int n, int v, Node root) { n = n - v; if (n < 0) { return(0); } else if (n == 0) { return(1); } //if(dp[n] != 0 ) //{ // return dp[n]; //} return(dp[n] = TargetSumUsingSubsetOfElementsUtils(n, 1, TernaryTree.Construct(root, "left", n, 1)) + TargetSumUsingSubsetOfElementsUtils(n, 3, TernaryTree.Construct(root, "mid", n, 3)) + TargetSumUsingSubsetOfElementsUtils(n, 5, TernaryTree.Construct(root, "right", n, 5))); }
public void TestTreePrefixSearchForValues() { TernaryTree <string> tree = new TernaryTree <string>(); var InputKeys = "aback abacus abalone abandon abase abash abate abbas abbe abbey abbot Abbott".ToLower().Split(' '); foreach (var key in InputKeys) { tree.Add(key, "value of " + key); } { var matches = tree.Search("ab"); int count = getCount(matches); Assert.AreEqual(12, count); int i = 0; foreach (var val in matches) { Assert.AreEqual("value of " + InputKeys[i++], val); } } }
public void TestCreateTerenaryTree() { TernaryTree <string> tree = new TernaryTree <string>(); var InputKeys = "aback abacus abalone abandon abase abash abate abbas abbe abbey abbot Abbott".ToLower().Split(' '); foreach (var key in InputKeys) { tree.Add(key, "value of " + key); } Assert.IsNotNull(tree); Assert.AreEqual(InputKeys.Length, tree.Length); IEnumerable <string> Keys = tree.Keys; Assert.IsNotNull(Keys); int count = getCount(Keys); Assert.AreEqual(InputKeys.Length, count); }
public void TestTreeWildCardSearch() { TernaryTree <string> tree = new TernaryTree <string>(); var InputKeys = "aback abacus abalone abandon abase abash abate abbas abbe abbey abbot Abbott".ToLower().Split(' '); foreach (var key in InputKeys) { tree.Add(key, "value of " + key); } { var matches = tree.WildcardMatch("...cus"); int count = getCount(matches); Assert.AreEqual(1, count); } { var matches = tree.WildcardMatch("....y"); int count = getCount(matches); Assert.AreEqual(1, count); } { var matches = tree.WildcardMatch("a.b.t."); int count = getCount(matches); Assert.AreEqual(1, count); } { var matches = tree.WildcardMatch("aba...."); int count = getCount(matches); Assert.AreEqual(2, count); } { var matches = tree.WildcardMatch("..a.."); int count = getCount(matches); Assert.AreEqual(4, count); } }
public void Setup(int numKeys) { foreach (string tenant in tenants) { Console.Write($"Generating {numKeys / 5} keys for tenant {tenant}. "); Random random = new Random(); startTime = DateTime.Now.Ticks; for (int i = 0; i < numKeys / 5; i++) { int docType = random.Next(0, docTypes.Length); string key = $"{tenant}-{docTypes[docType]}-{Guid.NewGuid()}"; keys.Add(key); } endTime = DateTime.Now.Ticks; Console.Write($"Took {(endTime - startTime) / 10000} ms." + Environment.NewLine); } Console.Write(Environment.NewLine + $"Adding {numKeys} keys to TernaryTree. "); startTime = DateTime.Now.Ticks; subject = TernaryTree <int> .Create(keys); endTime = DateTime.Now.Ticks; Console.WriteLine($"Took {(endTime - startTime) / 10000} ms." + Environment.NewLine + Environment.NewLine); }
public void Contains_Returns_False_For_Invalid_Key() { TernaryTree <string> subject = TernaryTree <string> .Create(_keys); Assert.IsFalse(subject.Contains("invalid key")); }
/// <summary> /// Read hyphenation patterns from an XML file. /// </summary> /// <param name="source"> the InputSource for the file </param> /// <exception cref="IOException"> In case the parsing fails </exception> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public void loadPatterns(org.xml.sax.InputSource source) throws java.io.IOException public virtual void loadPatterns(InputSource source) { PatternParser pp = new PatternParser(this); ivalues = new TernaryTree(); pp.parse(source); // patterns/values should be now in the tree // let's optimize a bit trimToSize(); vspace.trimToSize(); classmap.trimToSize(); // get rid of the auxiliary map ivalues = null; }
public void TestTreeNearSearchForValues() { TernaryTree<string> tree = new TernaryTree<string>(); var InputKeys = "aback abacus abalone abandon abase abash abate abbas abbe abbey abbot Abbott".ToLower().Split(' '); foreach (var key in InputKeys) { tree.Add(key, "value of " + key); } var near = tree.NearSearch("abacus", 5); int count = getCount(near); Assert.AreEqual(10, count); }
public void Create_From_ICollection_String() { TernaryTree <int> subject = TernaryTree <int> .Create(_keys); Assert.That(subject.Count, Is.EqualTo(_keys.Length)); }
public void Create_From_ICollection_KVPair() { TernaryTree <int> subject = TernaryTree <int> .Create(_keyValueCollection); Assert.That(subject.Count, Is.EqualTo(_keyValueCollection.Count)); }
public void Create_From_IDictionary_String_Int() { TernaryTree <int> subject = TernaryTree <int> .Create(_keyValueDictionary); Assert.That(subject.Count, Is.EqualTo(_keyValueDictionary.Count)); }
public void TestTreeWildCardSearch() { TernaryTree<string> tree = new TernaryTree<string>(); var InputKeys = "aback abacus abalone abandon abase abash abate abbas abbe abbey abbot Abbott".ToLower().Split(' '); foreach (var key in InputKeys) { tree.Add(key, "value of " + key); } { var matches = tree.WildcardMatch("...cus"); int count = getCount(matches); Assert.AreEqual(1, count); } { var matches = tree.WildcardMatch("....y"); int count = getCount(matches); Assert.AreEqual(1, count); } { var matches = tree.WildcardMatch("a.b.t."); int count = getCount(matches); Assert.AreEqual(1, count); } { var matches = tree.WildcardMatch("aba...."); int count = getCount(matches); Assert.AreEqual(2, count); } { var matches = tree.WildcardMatch("..a.."); int count = getCount(matches); Assert.AreEqual(4, count); } }
public void Throws_ArgumentException_For_Bad_Pattern(string pattern) { TernaryTree <int> subject = new TernaryTree <int>(); Assert.Throws <ArgumentException>(() => { subject.Match(pattern); }); }
public void TestTreePrefixSearchForValues() { TernaryTree<string> tree = new TernaryTree<string>(); var InputKeys = "aback abacus abalone abandon abase abash abate abbas abbe abbey abbot Abbott".ToLower().Split(' '); foreach (var key in InputKeys) { tree.Add(key, "value of " + key); } { var matches = tree.Search("ab"); int count = getCount(matches); Assert.AreEqual(12, count); int i = 0; foreach (var val in matches) { Assert.AreEqual("value of " + InputKeys[i++], val); } } }
public void TestTreePrefixSearch() { TernaryTree<string> tree = new TernaryTree<string>(); var InputKeys = "aback abacus abalone abandon abase abash abate abbas abbe abbey abbot Abbott".ToLower().Split(' '); foreach (var key in InputKeys) { tree.Add(key, "value of " + key); } { var matches = tree.PrefixMatch("ab"); int count = getCount(matches); Assert.AreEqual(InputKeys.Length, count); } { var matches = tree.PrefixMatch("aba"); int count = getCount(matches); Assert.AreEqual(7, count); } { var matches = tree.PrefixMatch("abb"); int count = getCount(matches); Assert.AreEqual(5, count); } { var matches = tree.PrefixMatch("XXX"); int count = getCount(matches); Assert.AreEqual(0, count); } }