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
 }
Example #2
0
        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[" "]);
        }
Example #3
0
 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));
        }
Example #5
0
 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];
            });
        }
Example #8
0
        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);
        }
Example #12
0
        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);
            });
        }
Example #13
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));
        }
Example #14
0
        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]);
            });
        }
Example #15
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));
        }
Example #16
0
        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]));
                }
            });
        }
Example #17
0
        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);
            });
        }
Example #18
0
        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);
        }
Example #19
0
        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));
                }
            });
        }
Example #20
0
        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);
        }
Example #21
0
        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"]);
        }
Example #22
0
        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));
        }
Example #23
0
        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);
        }
Example #24
0
        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"]);
        }
Example #25
0
        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);
        }
Example #26
0
        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);
        }
Example #27
0
        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]);
        }
Example #28
0
        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);
        }
Example #29
0
        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)));
        }
Example #31
0
        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);
                }
            }
        }
Example #32
0
        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);
        }
Example #33
0
        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);
            }
        }
Example #34
0
        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);
        }
Example #35
0
        public void Contains_Returns_False_For_Invalid_Key()
        {
            TernaryTree <string> subject = TernaryTree <string> .Create(_keys);

            Assert.IsFalse(subject.Contains("invalid key"));
        }
Example #36
0
        /// <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;
        }
Example #37
0
 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);
 }
Example #38
0
        public void Create_From_ICollection_String()
        {
            TernaryTree <int> subject = TernaryTree <int> .Create(_keys);

            Assert.That(subject.Count, Is.EqualTo(_keys.Length));
        }
Example #39
0
        public void Create_From_ICollection_KVPair()
        {
            TernaryTree <int> subject = TernaryTree <int> .Create(_keyValueCollection);

            Assert.That(subject.Count, Is.EqualTo(_keyValueCollection.Count));
        }
Example #40
0
        public void Create_From_IDictionary_String_Int()
        {
            TernaryTree <int> subject = TernaryTree <int> .Create(_keyValueDictionary);

            Assert.That(subject.Count, Is.EqualTo(_keyValueDictionary.Count));
        }
Example #41
0
        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);
            }
        }
Example #42
0
        public void Throws_ArgumentException_For_Bad_Pattern(string pattern)
        {
            TernaryTree <int> subject = new TernaryTree <int>();

            Assert.Throws <ArgumentException>(() => { subject.Match(pattern); });
        }
Example #43
0
        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);
                }
            }
        }
Example #44
0
        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);
            }
        }