this.node = ( SimpleNode < T0> )tree.get(currentIndex ); this.index = count1 - tree.indexOfNode(this.node, (byte)1);
/** * Remove size values at the specified index. Only values of the type * specified in indexColors will be removed. * * <p>Note that if the two nodes on either side of the removed node could * be merged, they probably will not be merged by this implementation. This * is to simplify the implementation, but it means that when iterating a * tree, sometimes multiple nodes of the same color and value will be * encountered in sequence. */ public void remove(int index, int size) { if(size == 0) return; assert(index >= 0); assert(index + size <= size( )); assert(root != null); // remove values from the tree removeFromSubtree(root, index, size); // clean up any nodes that got deleted drainZeroQueue(); assert(valid()); }
/** * Replace all values at the specified index with the specified new value. * * <p>Currently this uses a naive implementation of remove then add. If * it proves desirable, it may be worthwhile to optimize this implementation * with one that performs the remove and insert simultaneously, to save * on tree navigation. * * @return the element that was updated. This is non-null unless the size * parameter is 0, in which case the result is always <code>null</code>. */ public Element<T0> set(int index, T0 value, int size) { remove(index, size); return add(index, value, size); }
/** * Remove the specified element from the tree outright. */ public void remove(Element<T0> element) { SimpleNode < T0> node = ( SimpleNode < T0> )element; assert(root != null); // delete the node by adding to the zero queue fixCountsThruRoot(node, -1 ); zeroQueue.add(node); drainZeroQueue(); assert(valid()); }
/** * Statically build the separators data structures. */ private void rebuildSeparators() { // clear the initial state of these separators insertedSeparators = new Barcode(); separators = new SimpleTree<GroupSeparator>(); // prepare the separator list insertedSeparators.add(0, SOURCE_ELEMENT, source.size()); for(BarcodeIterator i = grouper.getBarcode().iterator(); i.hasNextColour(Grouper.UNIQUE); ) { i.nextColour(Grouper.UNIQUE); int groupIndex = i.getColourIndex(Grouper.UNIQUE); int sourceIndex = i.getIndex(); insertedSeparators.add(groupIndex + sourceIndex, SEPARATOR, 1); Element<GroupSeparator> node = separators.add(groupIndex, new GroupSeparator(), 1); node.get().setNode(node); node.get().applyLimit(defaultLimit, false); } // update the cached values in all separators for(int i = 0; i < separators.size(); i++) { separators.get(i).get().updateCachedValues(); } }
private void processInsert(final int changeIndex, E inserted, boolean notify) { // This is a bit clunky - to find the group for an element, I am // creating a new GroupList and looking for it in the tree. GroupLists // are considered equal if their matchers compare equal. GroupMatcher<E> matcher = factory.createGroupMatcher(inserted); GroupList newGroup = new GroupList(matcher); int groupIndex = groupLists.indexOfValue(newGroup, true, false, (byte) 1); if (groupIndex >= 0) { insertHelper(changeIndex, groupIndex); if (notify) { GroupList groupingList = groupLists.get(groupIndex).get(); // an insert on the existing list groupingList.getListEventAssembler().elementInserted( groupingList.barcode.getBlackIndex(changeIndex), inserted); // an update on the grouping list updates.elementUpdated(groupIndex, groupingList, groupingList); } } else { Element<GroupList> newList = groupLists.addInSortedOrder((byte) 1, newGroup, 1); groupIndex = groupLists.indexOfNode(newList, (byte) 1); insertHelper(changeIndex, groupIndex); if (notify) { // begin event so later inserts, updates, deletes will work newGroup.getListEventAssembler().beginEvent(); // an update on the grouping list updates.elementInserted(groupIndex, newGroup); } } }
AgedNode agedNode = (AgedNode)cacheNode.get(); value = agedNode.getValue(); cache.remove(cacheNode); SparseListNode indexNode = agedNode.getIndexNode(); indexNode.setValue(cache.addInSortedOrder((byte)1, agedNode, 1)); Element oldestInCache = cache.get(0); cache.remove(oldestInCache); AgedNode oldAgedNode = (AgedNode)oldestInCache.get(); SparseListNode oldIndexNode = oldAgedNode.getIndexNode(); SparseListNode indexNode = indexTree.getNode(index); AgedNode agedNode = new AgedNode(indexNode, value); indexNode.setValue(cache.addInSortedOrder((byte)1, agedNode, 1)); currentSize++;
if(comparator != null) treeComparator = new ElementComparator(comparator); else treeComparator = new ElementRawOrderComparator(); sorted = new SimpleTree<Element>(treeComparator); unsorted = new SimpleTree<Element>(); Element unsortedNode = unsorted.add(i, EMPTY_ELEMENT, 1); insertByUnsortedNode(unsortedNode); Element unsortedNode = (Element)oldSortedNode.get(); Element newSortedNode = (Element)unsortedNode.get(); int newSortedIndex = sorted.indexOfNode(newSortedNode, ALL_COLORS); reorderMap[newSortedIndex] = oldSortedIndex;
/** * Returns the number of {@link ValueSegment} objects contained within. */ public int size() { return start.size(); } }
/** {@inheritDoc} */ @Override public List<E> get(int index) { return groupLists.get(index).get(); }
/** * Helper for {@link #listChanged(ListEvent)} when deleting. */ private void handleDelete(int sourceIndex) { // find the index of the black node with that index final int parentIndex = getAbsoluteIndex(sourceIndex); final int nextParentIndex = getAbsoluteIndex(sourceIndex + 1); final int childCount = nextParentIndex - parentIndex - 1; // subtract one for the parent // record the delete events first while the deleted values still exist if(childCount > 0) { int firstDeletedChildIndex = parentIndex - sourceIndex; int firstNotDeletedChildIndex = firstDeletedChildIndex + childCount; for (int i = firstDeletedChildIndex; i < firstNotDeletedChildIndex; i++) { updates.elementDeleted(firstDeletedChildIndex, get(i)); } } // update the list of child lists Element<ChildElement<E>> removedChildElement = childElements.get(sourceIndex); childElements.remove(removedChildElement); removedChildElement.get().dispose(); // update the barcode barcode.remove(parentIndex, 1 + childCount); // delete the parent and all children }
/** * Removes the <code>segment</code> from the trees. */ public void delete(ValueSegment<V,?> segment) { int startIndex = start.indexOfValue(segment.getStart(), true, false, (byte)1); int endIndex = end.indexOfValue(segment.getEnd(), true, false, (byte)1); start.remove(startIndex, 1); end.remove(endIndex, 1); }
/** * Returns the number of {@link ValueSegment}s which appear between the * given <code>start</code> and <code>end</code> values. */ public int getCount(V start, V end) { final int numStartedBeforeSegmentEnd = this.start.indexOfValue(end, true, true, (byte)1); final int numEndedBeforeSegmentStart = this.end.indexOfValue(start, true, true, (byte)1); return numStartedBeforeSegmentEnd - numEndedBeforeSegmentStart; }
/** * Inserts the specified unsorted node as the value in the sorted tree * and returns the sorted order. * * @return the sortIndex of the inserted object. */ private int insertByUnsortedNode(Element unsortedNode) { // add the object to the sorted set Element<Element> sortedNode = sorted.addInSortedOrder(ALL_COLORS, unsortedNode, 1); // assign the unsorted node the value of the sorted node unsortedNode.set(sortedNode); // return the sorted index return sorted.indexOfNode(sortedNode, ALL_COLORS); } /**
/** * Deletes the node in the sorted tree based on the value of the specified * unsorted tree node. * * @return the sortIndex of the deleted object. */ private int deleteByUnsortedNode(Element unsortedNode) { // get the sorted node Element sortedNode = (Element)unsortedNode.get(); // look up the sorted index before removing the nodes int sortedIndex = sorted.indexOfNode(sortedNode, ALL_COLORS); // delete the sorted node from its tree sorted.remove(sortedIndex, 1); // return the sorted index return sortedIndex; }
/** {@inheritDoc} */ @Override public void add(int index, T0 element) { tree.add(index, element, 1); }
/** * Clears the data from the trees efficiently. */ public void clear() { start = new SimpleTree<V>(GlazedLists.comparableComparator()); end = new SimpleTree<V>(GlazedLists.comparableComparator()); }
/** * Inserts the given <code>segment</code> into the trees. */ public void insert(ValueSegment<V,?> segment) { start.addInSortedOrder((byte)1, segment.getStart(), 1); end.addInSortedOrder((byte)1, segment.getEnd(), 1); }
/** {@inheritDoc} */ @Override public T0 remove(int index) { T0 removed = get(index); tree.remove(index, 1); return removed; }
/** * After the barcode has been updated in response to a change in the * grouping {@link Comparator}, this method is used to rebuild the tree of * GroupLists which map those GroupLists to their overall indices. */ private void rebuildGroupListTreeFromBarcode() { // clear the contents of the GroupList tree groupLists.clear(); // fetch our GrouperClient final GrouperClient grouperClient = (GrouperClient) grouper.getClient(); // build the tree of GroupLists from the barcode for (int i = 0, n = grouper.getBarcode().colourSize(Grouper.UNIQUE); i < n; i++) { grouperClient.insertGroupList(i); } }