All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ca.odell.glazedlists.jfreechart.TreePair Maven / Gradle / Ivy

/* Glazed Lists                                                 (c) 2003-2006 */
/* http://publicobject.com/glazedlists/                      publicobject.com,*/
/*                                                     O'Dell Engineering Ltd.*/
package ca.odell.glazedlists.jfreechart;

import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.impl.adt.barcode2.SimpleTree;

/**
 * A TreePair contains two tree structures which store the start and end
 * {@link Comparable} values of {@link ValueSegment} objects in their natural
 * orders. This allows for efficient answers to questions like
 *
 * 
    *
  • How many {@link ValueSegment}s exist after X? *
  • How many {@link ValueSegment}s exist before Y? *
  • How many {@link ValueSegment}s exist between X and Y? *
* * @author James Lemieux */ final class TreePair { /** The tree which orders the start indices of all ValueSegments. */ private SimpleTree start = new SimpleTree(GlazedLists.comparableComparator()); /** The tree which orders the start indices of all ValueSegments. */ private SimpleTree end = new SimpleTree(GlazedLists.comparableComparator()); /** * Inserts the given segment into the trees. */ public void insert(ValueSegment segment) { start.addInSortedOrder((byte)1, segment.getStart(), 1); end.addInSortedOrder((byte)1, segment.getEnd(), 1); } /** * Removes the previousSegment and inserts the * newSegment into the trees. */ public void update(ValueSegment previousSegment, ValueSegment newSegment) { delete(previousSegment); insert(newSegment); } /** * Removes the segment from the trees. */ public void delete(ValueSegment 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); } /** * Clears the data from the trees efficiently. */ public void clear() { start = new SimpleTree(GlazedLists.comparableComparator()); end = new SimpleTree(GlazedLists.comparableComparator()); } /** * Returns true if the trees contain any data; false * otherwise. */ public boolean isEmpty() { return size() == 0; } /** * Returns the number of {@link ValueSegment}s which appear between the * given start and end 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; } /** * Returns the number of {@link ValueSegment} objects contained within. */ public int size() { return start.size(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy