Search in sources :

Example 1 with RBTree

use of org.drools.core.util.RBTree in project drools by kiegroup.

the class LeftTupleIndexRangeRBTree method toArray.

public Entry[] toArray() {
    FastIterator it = tree.fastIterator();
    if (it == null) {
        return new Entry[0];
    }
    List<Comparable> toBeRemoved = new ArrayList<Comparable>();
    List<Comparable> nestedToBeRemoved = new ArrayList<Comparable>();
    List<Tuple> result = new ArrayList<Tuple>();
    RBTree.Node<Comparable<Comparable>, RBTree<Comparable<Comparable>, TupleList>> node = null;
    RBTree.Node<Comparable<Comparable>, TupleList> nestedNode = null;
    while ((node = (RBTree.Node<Comparable<Comparable>, RBTree<Comparable<Comparable>, TupleList>>) it.next(node)) != null) {
        nestedToBeRemoved.clear();
        RBTree<Comparable<Comparable>, TupleList> nestedTree = node.value;
        FastIterator nestedIt = nestedTree.fastIterator();
        while ((nestedNode = (RBTree.Node<Comparable<Comparable>, TupleList>) nestedIt.next(nestedNode)) != null) {
            TupleList list = nestedNode.value;
            int listSize = list.size();
            if (listSize == 0) {
                nestedToBeRemoved.add(nestedNode.key);
            } else {
                Tuple entry = list.getFirst();
                while (entry != null) {
                    result.add(entry);
                    entry = (Tuple) entry.getNext();
                }
            }
        }
        for (Comparable key : nestedToBeRemoved) {
            nestedTree.delete(key);
        }
        if (nestedTree.isEmpty()) {
            toBeRemoved.add(node.key);
        }
    }
    for (Comparable key : toBeRemoved) {
        tree.delete(key);
    }
    return result.toArray(new Tuple[result.size()]);
}
Also used : ArrayList(java.util.ArrayList) Entry(org.drools.core.util.Entry) RBTree(org.drools.core.util.RBTree) FastIterator(org.drools.core.util.FastIterator) Tuple(org.drools.core.spi.Tuple)

Example 2 with RBTree

use of org.drools.core.util.RBTree in project drools by kiegroup.

the class LeftTupleIndexRangeRBTree method getNext.

private Entry getNext(Tuple tuple) {
    Tuple next = (Tuple) tuple.getNext();
    if (next != null) {
        return next;
    }
    Comparable ascendingKey = getLeftAscendingIndexedValue(tuple);
    Comparable descendingKey = getLeftDescendingIndexedValue(tuple);
    RBTree<Comparable<Comparable>, TupleList> nestedTree = tree.lookup(ascendingKey);
    while (nestedTree != null) {
        while (true) {
            RBTree.Node<Comparable<Comparable>, TupleList> nextNode = nestedTree.findNearestNode(descendingKey, false, RBTree.Boundary.LOWER);
            if (nextNode == null) {
                break;
            }
            if (nextNode.value.size() == 0) {
                nestedTree.delete(nextNode.key);
            } else {
                return nextNode.value.getFirst();
            }
        }
        RBTree.Node<Comparable<Comparable>, RBTree<Comparable<Comparable>, TupleList>> nextNode = tree.findNearestNode(ascendingKey, false, RBTree.Boundary.UPPER);
        nestedTree = nextNode == null ? null : nextNode.value;
    }
    return null;
}
Also used : RBTree(org.drools.core.util.RBTree) Tuple(org.drools.core.spi.Tuple)

Aggregations

Tuple (org.drools.core.spi.Tuple)2 RBTree (org.drools.core.util.RBTree)2 ArrayList (java.util.ArrayList)1 Entry (org.drools.core.util.Entry)1 FastIterator (org.drools.core.util.FastIterator)1