/// <summary> /// Add the specified key to the tri. /// Assumes this is the node reached by traversing key[0]..key[ich-1]. /// Add nodes as necessary so that key can be matched and will return value. /// </summary> /// <param name="key"></param> /// <param name="ich"></param> /// <param name="markerSpec"></param> public void Add(string key, int ich, MarkerSpec markerSpec) { if (ich >= key.Length) TargetMarkerSpec = markerSpec; else { TriNode tn = Match(key[ich]); if (tn == null) { tn = new TriNode(); Add(key[ich], tn); } tn.Add(key, ich + 1, markerSpec); } }
/// <summary> /// Associate the specified TriNode with the specified char. /// </summary> /// <param name="key"></param> /// <param name="triNode"></param> public void Add(char key, TriNode triNode) { if (m_values == null) m_values = new TriNode[256]; m_values[Convert.ToInt32(key)] = triNode; }
public void TestTriNode() { TriNode tnAbc = new TriNode(); TriNode tnAbd = new TriNode(); MarkerSpec fmsAbc = MarkerSpec.CreateMarkerSpec("abc", "dummy1", "dummy2", false, null, null, null); tnAbc.TargetMarkerSpec = fmsAbc; MarkerSpec fmsAbd = MarkerSpec.CreateMarkerSpec("acd", "dummy1", "dummy2", false, null, null, null); tnAbd.TargetMarkerSpec = fmsAbd; TriNode tnAb = new TriNode(); TriNode tnA = new TriNode(); TriNode tnRoot = new TriNode(); tnRoot.Add(fmsAbc.Marker[0], tnA); tnA.Add(fmsAbc.Marker[1], tnAb); tnAb.Add(fmsAbc.Marker[2], tnAbc); tnAb.Add(fmsAbd.Marker[2], tnAbd); Assert.IsNull(tnRoot.TargetMarkerSpec); Assert.AreEqual(fmsAbc, tnAbc.TargetMarkerSpec); Assert.AreEqual(fmsAbd, tnAbd.TargetMarkerSpec); Assert.AreEqual(tnA, tnRoot.Match(fmsAbc.Marker[0])); Assert.AreEqual(tnAb, tnA.Match(fmsAbc.Marker[1])); Assert.AreEqual(tnAbc, tnAb.Match(fmsAbc.Marker[2])); Assert.AreEqual(tnAbd, tnAb.Match(fmsAbd.Marker[2])); string s1 = "Ax"; Assert.IsNull(tnRoot.Match(s1[0])); Assert.IsNull(tnRoot.Match(s1[1])); Assert.IsNull(tnAb.Match(s1[0])); Assert.IsNull(tnAb.Match(s1[1])); Assert.IsNull(tnAbc.Match(s1[0])); Assert.IsNull(tnAbc.Match(s1[1])); tnRoot = new TriNode(); // start over using char[] Add. tnRoot.Add(fmsAbc.Marker, 0, fmsAbc); tnRoot.Add(fmsAbd.Marker, 0, fmsAbd); Assert.AreEqual(fmsAbc, tnRoot.Match(fmsAbc.Marker[0]). Match(fmsAbc.Marker[1]).Match(fmsAbc.Marker[2]).TargetMarkerSpec); Assert.AreEqual(fmsAbd, tnRoot.Match(fmsAbd.Marker[0]). Match(fmsAbd.Marker[1]).Match(fmsAbd.Marker[2]).TargetMarkerSpec); }