Exemplo n.º 1
0
        public T get(String key)
        {
            BaseNode <T> branch = this;

            char[] chars = key.ToCharArray();
            foreach (char aChar in chars)
            {
                if (branch == null)
                {
                    return(default(T));
                }
                branch = branch.GetChild(aChar);
            }

            if (branch == null)
            {
                return(default(T));
            }
            // 下面这句可以保证只有成词的节点被返回
            if (!(branch.NodeStatus == Status.WORD_END_3 || branch.NodeStatus == Status.WORD_MIDDLE_2))
            {
                return(default(T));
            }
            return((T)branch.Value);
        }
Exemplo n.º 2
0
        public BaseNode <T> Transition(char[] path, int begin)
        {
            BaseNode <T> cur = this;

            for (int i = begin; i < path.Length; ++i)
            {
                cur = cur.GetChild(path[i]);
                if (cur == null || cur.NodeStatus == Status.UNDEFINED_0)
                {
                    return(null);
                }
            }
            return(cur);
        }
Exemplo n.º 3
0
        /**
         * 前缀查询,通过字符数组来表示字符串可以优化运行速度
         *
         * @param chars 字符串的字符数组
         * @param begin 开始的下标
         * @return
         */
        public LinkedList <KeyValuePair <String, T> > commonPrefixSearchWithValue(char[] chars, int begin)
        {
            var           result = new LinkedList <KeyValuePair <String, T> >();
            StringBuilder sb     = new StringBuilder();
            BaseNode <T>  branch = this;

            for (int i = begin; i < chars.Length; ++i)
            {
                char aChar = chars[i];
                branch = branch.GetChild(aChar);
                if (branch == null || branch.NodeStatus == Status.UNDEFINED_0)
                {
                    return(result);
                }
                sb.Append(aChar);
                if (branch.NodeStatus == Status.WORD_MIDDLE_2 || branch.NodeStatus == Status.WORD_END_3)
                {
                    result.AddLast(new KeyValuePair <String, T>(sb.ToString(), (T)branch.Value));
                }
            }

            return(result);
        }
Exemplo n.º 4
0
        /**
         * 前缀查询
         *
         * @param key 查询串
         * @return 键值对
         */
        public Dictionary <string, T> prefixSearch(String key)
        {
            var           entrySet = new Dictionary <String, T>();
            StringBuilder sb       = new StringBuilder(key.Substring(0, key.Length - 1));
            BaseNode <T>  branch   = this;

            char[] chars = key.ToCharArray();
            foreach (char aChar in chars)
            {
                if (branch == null)
                {
                    return(entrySet);
                }
                branch = branch.GetChild(aChar);
            }

            if (branch == null)
            {
                return(entrySet);
            }
            branch.Walk(sb, entrySet);
            return(entrySet);
        }