Exemplo n.º 1
0
  /**
   * 唯一的构建方法
   *
   * @param _key 值set,必须字典序
   * @param _length 对应每个key的长度,留空动态获取
   * @param _value 每个key对应的值,留空使用key的下标作为值
   * @param _keySize key的长度,应该设为_key.size
   * @return 是否出错
   */
  public int build(List<String> _key, int _length[], int _value[], int _keySize) {
    if (_keySize > _key.size() || _key == null) return 0;

    // progress_func_ = progress_func;
    key = _key;
    length = _length;
    keySize = _keySize;
    value = _value;
    progress = 0;

    resize(65536 * 32); // 32个双字节

    base[0] = 1;
    nextCheckPos = 0;

    Node root_node = new Node();
    root_node.left = 0;
    root_node.right = keySize;
    root_node.depth = 0;

    List<Node> siblings = new ArrayList<Node>();
    fetch(root_node, siblings);
    insert(siblings);

    // size += (1 << 8 * 2) + 1; // ???
    // if (size >= allocSize) resize (size);

    used = null;
    key = null;
    length = null;

    return error_;
  }
Exemplo n.º 2
0
  /**
   * 获取直接相连的子节点
   *
   * @param parent 父节点
   * @param siblings (子)兄弟节点
   * @return 兄弟节点个数
   */
  private int fetch(Node parent, List<Node> siblings) {
    if (error_ < 0) return 0;

    int prev = 0;

    for (int i = parent.left; i < parent.right; i++) {
      if ((length != null ? length[i] : key.get(i).length()) < parent.depth) continue;

      String tmp = key.get(i);

      int cur = 0;
      if ((length != null ? length[i] : tmp.length()) != parent.depth)
        cur = (int) tmp.charAt(parent.depth) + 1;

      if (prev > cur) {
        error_ = -3;
        return 0;
      }

      if (cur != prev || siblings.size() == 0) {
        Node tmp_node = new Node();
        tmp_node.depth = parent.depth + 1;
        tmp_node.code = cur;
        tmp_node.left = i;
        if (siblings.size() != 0) siblings.get(siblings.size() - 1).right = i;

        siblings.add(tmp_node);
      }

      prev = cur;
    }

    if (siblings.size() != 0) siblings.get(siblings.size() - 1).right = parent.right;

    return siblings.size();
  }