コード例 #1
0
ファイル: RegNode.java プロジェクト: fgravil/Java
  /** Adds mapping from key to obj and returns the node containing that mapping. */
  @Override
  public Node<K, O> addTraverse(K newKey, O newObj) {

    if (newKey.compareTo(this.key) < 0) {
      leftNode = leftNode.addTraverse(newKey, newObj);
    } else if (newKey.compareTo(this.key) > 0) {
      rightNode = rightNode.addTraverse(newKey, newObj);
    } else {
      obj = newObj;
    }

    return this;
  }
コード例 #2
0
 protected V get(NODE node, K key) {
   if (node == null) return null;
   int cmp = key.compareTo(node.key);
   if (cmp < 0) return get(node.left, key);
   else if (cmp > 0) return get(node.right, key);
   else return node.value;
 }
コード例 #3
0
 private V get(K key, Node<K, V> node) {
   if (node == null) return null;
   int cmp = key.compareTo(node.key);
   if (cmp == 0) return node.value;
   else if (cmp < 0) return get(key, node.left);
   else return get(key, node.right);
 }
コード例 #4
0
 protected int rank(NODE node, K key) {
   if (node == null) return 0;
   int cmp = key.compareTo(node.key);
   if (cmp < 0) {
     return rank(node.left, key);
   } else if (cmp > 0) {
     return rank(node.right, key) + size(node.left) + 1;
   } else {
     return size(node.left);
   }
 }
コード例 #5
0
ファイル: DLClassReference.java プロジェクト: pwub/lillytab
 @SuppressWarnings("unchecked")
 @Override
 public int compareTo(final IDLTerm<I, L, K, R> o) {
   final int compare = getDLTermOrder().compareTo(o);
   if (compare == 0) {
     assert o instanceof IDLClassReference;
     return _klass.compareTo((K) (((IDLClassReference) o).getElement()));
   } else {
     return compare;
   }
 }
コード例 #6
0
 protected NODE ceiling(NODE node, K key) {
   if (node == null) return null;
   int cmp = key.compareTo(node.key);
   if (cmp < 0) {
     NODE x = ceiling(node.left, key);
     return x != null ? x : node;
   } else if (cmp > 0) {
     return ceiling(node.right, key);
   } else {
     return node;
   }
 }
コード例 #7
0
  private Node<K, V> put(K key, V val, Node<K, V> node) {
    if (node == null) return new Node<K, V>(key, val, 1);

    int cmp = key.compareTo(node.key);
    if (cmp == 0) {
      node.value = val;
    } else if (cmp < 0) {
      node.left = put(key, val, node.left);
    } else if (cmp > 0) {
      node.right = put(key, val, node.right);
    }
    node.size = size(node.left) + size(node.right) + 1;
    return node;
  }
コード例 #8
0
    public void put(K k1, K k2, V value) {
      if (k1.compareTo(k2) > 0) {
        K k3 = k1;
        k1 = k2;
        k2 = k3;
      }

      Map<K, V> l2Map = l1Map.get(k1);
      if (l2Map == null) {
        l2Map = new HashMap<K, V>();
        l1Map.put(k1, l2Map);
      }

      l2Map.put(k2, value);
    }
コード例 #9
0
    V get(K k1, K k2) {
      if (k1.compareTo(k2) > 0) {
        K k3 = k1;
        k1 = k2;
        k2 = k3;
      }

      Map<K, V> l2Map = l1Map.get(k1);
      if (l2Map == null) {
        l2Map = new HashMap<K, V>(sizePerDimension);
        l1Map.put(k1, l2Map);
      }

      return l2Map.get(k2);
    }
コード例 #10
0
 protected NODE delete(NODE node, K key) {
   if (node == null) {
     // We've reached a leaf. Nothing to delete.
     return null;
   }
   int cmp = key.compareTo(node.key);
   if (cmp < 0) {
     // Key is less than the node. Delete in the left subtree.
     node.left = delete(node.left, key);
     node.size = size(node.left) + size(node.right) + 1;
     return node;
   } else if (cmp > 0) {
     // Key is greater than the node. Delete in the right subtree.
     node.right = delete(node.right, key);
     node.size = size(node.left) + size(node.right) + 1;
     return node;
   } else {
     // Delete the node.
     if (node.left == null) {
       // Replace node with its right subtree.
       return node.right;
     } else if (node.right == null) {
       // Replace node with its left subtree.
       return node.left;
     } else if (node.right.left == null) {
       // Replace node with its right subtree.
       // Move the left subtree to the right node.
       node.right.left = node.left;
       node.right.size += size(node.left);
       return node.right;
     } else {
       // Replace node with its successor (Hibbard).
       NODE s = min(node.right);
       NODE x = delete(node, s.key);
       node.key = s.key;
       node.value = s.value;
       return x;
     }
   }
 }
コード例 #11
0
 protected NODE put(NODE node, K key, V value) {
   if (node == null) {
     // We've reached a leaf. Create a new node to store the new value.
     return createNode(key, value);
   }
   int cmp = key.compareTo(node.key);
   if (cmp < 0) {
     // Key is less than the node. Update the left subtree.
     node.left = put(node.left, key, value);
     node.size = size(node.left) + size(node.right) + 1;
     return node;
   } else if (cmp > 0) {
     // Key is greater than the node. Update the right subtree.
     node.right = put(node.right, key, value);
     node.size = size(node.left) + size(node.right) + 1;
     return node;
   } else {
     // Update the value on the node.
     node.value = value;
     return node;
   }
 }
コード例 #12
0
ファイル: Sorter.java プロジェクト: gifford-lab/GEM
 public int compareTo(Sortable s) {
   return key.compareTo(s.key);
 }
コード例 #13
0
 public static <K extends Comparable<K>> K max(K x, K y) {
   return x.compareTo(y) > 0 ? x : y;
 }
コード例 #14
0
 @Override
 public int compareTo(CountPair<K, V> o) {
   int vc = o.val.compareTo(val);
   return (0 != vc ? vc : key.compareTo(o.key));
 }
コード例 #15
0
ファイル: RegNode.java プロジェクト: fgravil/Java
 /*@ ensures /result == obj;*/
 public O Search(K key) {
   if (key.compareTo(this.key) < 0) return leftNode.Search(key);
   else if (key.compareTo(this.key) > 0) return rightNode.Search(key);
   else return obj;
 }
コード例 #16
0
ファイル: Btree.java プロジェクト: sambenz/URingPaxos
  public TreeNode<K, V> lookupNode(K k) {
    TreeNode<K, V> node = null;

    // cache
    // if(usecache && !cache.isEmpty() && k.compareTo(cache.firstKey().getMinKey()) <= 0){
    //	  node = cache.get(cache.firstKey()); //TODO: most left cache only possible with double
    // linked leafs
    // }else{
    if (usecache) {
      for (Entry<Range<K>, TreeNode<K, V>> e : cache.entrySet()) {
        if (k.compareTo(e.getKey().getMinKey()) >= 0
            && (e.getKey().getMaxKey() == null || k.compareTo(e.getKey().getMaxKey()) < 0)) {
          node = e.getValue();
          break;
        }
      }
      if (node != null) {
        cachehit++;
        return node;
      }
    }

    // tree walk
    treewalk++;
    node = root;
    while (true) {
      if (node.getChildren().isEmpty()) {
        // fill cache
        if (usecache && node != root) {
          Range<K> range;
          if (node.getNextNode() != null) {
            range = new Range<K>(node.getMinKey(), node.getNextNode().getMinKey());
          } else {
            range = new Range<K>(node.getMinKey(), null);
          }
          cache.put(range, node);
        }
        break; // is leaf
      } else {
        Iterator<TreeNode<K, V>> i = node.getChildren().iterator();
        TreeNode<K, V> n0 = i.next();
        if (k.compareTo(n0.getMinKey()) <= 0) {
          node = n0; // go left
          continue;
        }
        while (i.hasNext()) {
          TreeNode<K, V> n1 =
              i
                  .next(); // FIXME: can throw concurrent mod exception (even with fully
                           // synchronized TreeNode)
          if (k.compareTo(n0.getMinKey()) >= 0 && k.compareTo(n1.getMinKey()) < 0) {
            node = n0; // inner node
            break;
          }
          n0 = n1;
        }
        node = n0; // go right
      }
    }
    return node;
  }
コード例 #17
0
ファイル: OPair.java プロジェクト: nkibler7/orientdb
 public int compareTo(final OPair<K, V> o) {
   return key.compareTo(o.key);
 }