示例#1
0
        protected override void SetKeywords2(List <string> keywords)
        {
            List <TrieNode> allNode = BuildFirstLayerTrieNode(keywords);
            TrieNode        root    = allNode[0];

            StringBuilder stringBuilder = new StringBuilder();

            for (int i = 1; i < allNode.Count; i++)
            {
                stringBuilder.Append(allNode[i].Char);
            }
            var length = CreateDict(stringBuilder.ToString());

            stringBuilder = null;


            var allNode2 = new List <TrieNode3Ex>();

            for (int i = 0; i < allNode.Count; i++)
            {
                allNode2.Add(new TrieNode3Ex()
                {
                    Index = i
                });;
            }

            for (int i = 0; i < allNode2.Count; i++)
            {
                var oldNode = allNode[i];
                var newNode = allNode2[i];

                foreach (var item in oldNode.m_values)
                {
                    var key   = _dict[item.Key];
                    var index = item.Value.Index;
                    if (key == 0)
                    {
                        newNode.HasWildcard  = true;
                        newNode.WildcardNode = allNode2[index];
                        continue;
                    }
                    newNode.Add((char)key, allNode2[index]);
                }
                foreach (var item in oldNode.Results)
                {
                    if (oldNode.IsWildcard)
                    {
                        if (keywords[item].Length > oldNode.WildcardLayer)
                        {
                            newNode.SetResults(item);
                        }
                    }
                    else
                    {
                        newNode.SetResults(item);
                    }
                    //newNode.SetResults(item);
                }

                var failure = oldNode.Failure;
                while (failure != root)
                {
                    if (oldNode.IsWildcard && failure.Layer <= oldNode.WildcardLayer)
                    {
                        break;
                    }
                    foreach (var item in failure.m_values)
                    {
                        var key   = _dict[item.Key];
                        var index = item.Value.Index;
                        if (key == 0)
                        {
                            newNode.HasWildcard = true;
                            if (newNode.WildcardNode == null)
                            {
                                newNode.WildcardNode = allNode2[index];
                            }
                            continue;
                        }
                        if (newNode.HasKey((char)key) == false)
                        {
                            newNode.Add((char)key, allNode2[index]);
                        }
                    }
                    foreach (var item in failure.Results)
                    {
                        if (oldNode.IsWildcard)
                        {
                            if (keywords[item].Length > oldNode.WildcardLayer)
                            {
                                newNode.SetResults(item);
                            }
                        }
                        else
                        {
                            newNode.SetResults(item);
                        }
                    }
                    failure = failure.Failure;
                }
            }
            allNode.Clear();
            allNode = null;
            root    = null;


            //var min = new List<ushort>();
            //var max = new List<ushort>();
            var wildcard   = new List <int>();
            var nextIndexs = new List <Dictionary <ushort, int> >();
            var end        = new List <int>()
            {
                0
            };
            var resultIndex = new List <int>();

            for (int i = 0; i < allNode2.Count; i++)
            {
                var dict = new Dictionary <ushort, int>();
                var node = allNode2[i];
                //min.Add(node.minflag);
                //max.Add(node.maxflag);

                if (node.HasWildcard)
                {
                    wildcard.Add(node.WildcardNode.Index);
                }
                else
                {
                    wildcard.Add(0);
                }

                if (i > 0)
                {
                    foreach (var item in node.m_values)
                    {
                        dict[item.Key] = item.Value.Index;
                    }
                }
                foreach (var item in node.Results)
                {
                    resultIndex.Add(item);
                }
                end.Add(resultIndex.Count);
                nextIndexs.Add(dict);
            }
            var first = new int[Char.MaxValue + 1];

            foreach (var item in allNode2[0].m_values)
            {
                first[item.Key] = item.Value.Index;
            }

            _firstIndex = first;
            //_min = min.ToArray();
            //_max = max.ToArray();
            _nextIndex = new IntDictionary[nextIndexs.Count];
            for (int i = 0; i < nextIndexs.Count; i++)
            {
                IntDictionary dictionary = new IntDictionary(nextIndexs[i]);
                //dictionary.SetDictionary(nextIndexs[i]);
                _nextIndex[i] = dictionary;
            }
            _wildcard    = wildcard.ToArray();
            _end         = end.ToArray();
            _resultIndex = resultIndex.ToArray();

            allNode2.Clear();
            allNode2 = null;
        }