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

            char[] chars = key.toCharArray();
            for (char aChar : chars)
            {
                if (branch == null)
                {
                    return(null);
                }
                branch = branch.getChild(aChar);
            }

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

            for (int i = begin; i < path.Length; ++i)
            {
                cur = cur.getChild(path[i]);
                if (cur == null || cur.status == Status.UNDEFINED_0)
                {
                    return(null);
                }
            }
            return(cur);
        }
Exemplo n.º 3
0
        public void put(char[] key, V value)
        {
            BaseNode branch = this;

            for (int i = 0; i < key.length - 1; ++i)
            {
                // 除了最后一个字外,都是继续
                branch.addChild(new Node(key[i], Status.NOT_WORD_1, null));
                branch = branch.getChild(key[i]);
            }
            // 最后一个字加入时属性为end
            if (branch.addChild(new Node <V>(key[key.length - 1], Status.WORD_END_3, value)))
            {
                ++size; // 维护size
            }
        }
Exemplo n.º 4
0
        public boolean containsKey(String key)
        {
            BaseNode branch = this;

            char[] chars = key.toCharArray();
            for (char aChar : chars)
            {
                if (branch == null)
                {
                    return(false);
                }
                branch = branch.getChild(aChar);
            }

            return(branch != null && (branch.status == Status.WORD_END_3 || branch.status == Status.WORD_MIDDLE_2));
        }
Exemplo n.º 5
0
        /**
         * 删除一个词
         *
         * @param key
         */
        public void remove(String key)
        {
            BaseNode branch = this;

            char[] chars = key.toCharArray();
            for (int i = 0; i < chars.length - 1; ++i)
            {
                if (branch == null)
                {
                    return;
                }
                branch = branch.getChild(chars[i]);
            }
            // 最后一个字设为undefined
            if (branch.addChild(new Node(chars[chars.length - 1], Status.UNDEFINED_0, value)))
            {
                --size;
            }
        }
Exemplo n.º 6
0
        /**
         * 插入一个词
         *
         * @param key
         * @param value
         */
        public void put(String key, V value)
        {
            if (key.length() == 0)
            {
                return;                 // 安全起见
            }
            BaseNode branch = this;

            char[] chars = key.toCharArray();
            for (int i = 0; i < chars.length - 1; ++i)
            {
                // 除了最后一个字外,都是继续
                branch.addChild(new Node(chars[i], Status.NOT_WORD_1, null));
                branch = branch.getChild(chars[i]);
            }
            // 最后一个字加入时属性为end
            if (branch.addChild(new Node <V>(chars[chars.length - 1], Status.WORD_END_3, value)))
            {
                ++size; // 维护size
            }
        }
Exemplo n.º 7
0
        /**
         * 前缀查询,通过字符数组来表示字符串可以优化运行速度
         *
         * @param chars 字符串的字符数组
         * @param begin 开始的下标
         * @return
         */
        public LinkedList <Map.Entry <String, V> > commonPrefixSearchWithValue(char[] chars, int begin)
        {
            LinkedList <Map.Entry <String, V> > result = new LinkedList <Map.Entry <String, V> >();
            StringBuilder sb     = new StringBuilder();
            BaseNode      branch = this;

            for (int i = begin; i < chars.length; ++i)
            {
                char aChar = chars[i];
                branch = branch.getChild(aChar);
                if (branch == null || branch.status == Status.UNDEFINED_0)
                {
                    return(result);
                }
                sb.append(aChar);
                if (branch.status == Status.WORD_MIDDLE_2 || branch.status == Status.WORD_END_3)
                {
                    result.add(new AbstractMap.SimpleEntry <String, V>(sb.toString(), (V)branch.value));
                }
            }

            return(result);
        }
Exemplo n.º 8
0
        /**
         * 前缀查询
         *
         * @param key 查询串
         * @return 键值对
         */
        public Set <Map.Entry <String, V> > prefixSearch(String key)
        {
            Set <Map.Entry <String, V> > entrySet = new TreeSet <Map.Entry <String, V> >();
            StringBuilder sb     = new StringBuilder(key.substring(0, key.length() - 1));
            BaseNode      branch = this;

            char[] chars = key.toCharArray();
            for (char aChar : chars)
            {
                if (branch == null)
                {
                    return(entrySet);
                }
                branch = branch.getChild(aChar);
            }

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