示例#1
0
  @Override
  public boolean put(String s, V v) {
    TreeTrie<V> t = this;
    int limit = s.length();
    for (int k = 0; k < limit; k++) {
      char c = s.charAt(k);

      int index = c >= 0 && c < 0x7f ? __lookup[c] : -1;
      if (index >= 0) {
        if (t._nextIndex[index] == null) t._nextIndex[index] = new TreeTrie<V>(c);
        t = t._nextIndex[index];
      } else {
        TreeTrie<V> n = null;
        for (int i = t._nextOther.size(); i-- > 0; ) {
          n = t._nextOther.get(i);
          if (n._c == c) break;
          n = null;
        }
        if (n == null) {
          n = new TreeTrie<V>(c);
          t._nextOther.add(n);
        }
        t = n;
      }
    }
    t._key = v == null ? null : s;
    t._value = v;
    return true;
  }
示例#2
0
  private V getBestByteBuffer(ByteBuffer b, int offset, int len) {
    TreeTrie<V> t = this;
    int pos = b.position() + offset;
    for (int i = 0; i < len; i++) {
      byte c = b.get(pos++);
      int index = c >= 0 && c < 0x7f ? __lookup[c] : -1;
      if (index >= 0) {
        if (t._nextIndex[index] == null) break;
        t = t._nextIndex[index];
      } else {
        TreeTrie<V> n = null;
        for (int j = t._nextOther.size(); j-- > 0; ) {
          n = t._nextOther.get(j);
          if (n._c == c) break;
          n = null;
        }
        if (n == null) break;
        t = n;
      }

      // Is the next Trie is a match
      if (t._key != null) {
        // Recurse so we can remember this possibility
        V best = t.getBest(b, offset + i + 1, len - i - 1);
        if (best != null) return best;
        break;
      }
    }
    return t._value;
  }