reformat all code
This commit is contained in:
@@ -11,7 +11,9 @@ import java.util.Comparator;
|
||||
/* We implement an ArbitraryOrderCollection using a red-black tree. We order the nodes arbitrarily.
|
||||
*/
|
||||
public class ArbitraryOrderCollection {
|
||||
/** The Comparator for ordering ArbitraryOrderNodes. */
|
||||
/**
|
||||
* The Comparator for ordering ArbitraryOrderNodes.
|
||||
*/
|
||||
private static final Comparator<ArbitraryOrderNode> NODE_COMPARATOR = new Comparator<ArbitraryOrderNode>() {
|
||||
@Override
|
||||
public int compare(ArbitraryOrderNode node1, ArbitraryOrderNode node2) {
|
||||
@@ -19,10 +21,14 @@ public class ArbitraryOrderCollection {
|
||||
}
|
||||
};
|
||||
|
||||
/** The root node of the tree. */
|
||||
/**
|
||||
* The root node of the tree.
|
||||
*/
|
||||
private ArbitraryOrderNode root = new ArbitraryOrderNode();
|
||||
|
||||
/** Adds and returns a new value for ordering. */
|
||||
/**
|
||||
* Adds and returns a new value for ordering.
|
||||
*/
|
||||
public ArbitraryOrderValue createValue() {
|
||||
ArbitraryOrderNode node = new ArbitraryOrderNode();
|
||||
root = root.insert(node, true, NODE_COMPARATOR);
|
||||
|
||||
@@ -2,7 +2,9 @@ package com.github.btrekkie.arbitrary_order_collection;
|
||||
|
||||
import com.github.btrekkie.red_black_node.RedBlackNode;
|
||||
|
||||
/** A node in an ArbitraryOrderCollection tree. See ArbitraryOrderCollection. */
|
||||
/**
|
||||
* A node in an ArbitraryOrderCollection tree. See ArbitraryOrderCollection.
|
||||
*/
|
||||
class ArbitraryOrderNode extends RedBlackNode<ArbitraryOrderNode> {
|
||||
|
||||
}
|
||||
|
||||
@@ -5,7 +5,9 @@ package com.github.btrekkie.arbitrary_order_collection;
|
||||
* compareTo.
|
||||
*/
|
||||
public class ArbitraryOrderValue implements Comparable<ArbitraryOrderValue> {
|
||||
/** The node that establishes this value's relative position. */
|
||||
/**
|
||||
* The node that establishes this value's relative position.
|
||||
*/
|
||||
final ArbitraryOrderNode node;
|
||||
|
||||
ArbitraryOrderValue(ArbitraryOrderNode node) {
|
||||
|
||||
@@ -1,19 +1,20 @@
|
||||
package com.github.btrekkie.arbitrary_order_collection.test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import com.github.btrekkie.arbitrary_order_collection.ArbitraryOrderCollection;
|
||||
import com.github.btrekkie.arbitrary_order_collection.ArbitraryOrderValue;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.github.btrekkie.arbitrary_order_collection.ArbitraryOrderCollection;
|
||||
import com.github.btrekkie.arbitrary_order_collection.ArbitraryOrderValue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ArbitraryOrderCollectionTest {
|
||||
/** Tests ArbitraryOrderCollection. */
|
||||
/**
|
||||
* Tests ArbitraryOrderCollection.
|
||||
*/
|
||||
@Test
|
||||
public void test() {
|
||||
ArbitraryOrderCollection collection = new ArbitraryOrderCollection();
|
||||
|
||||
@@ -2,14 +2,18 @@ package com.github.btrekkie.connectivity.test;
|
||||
|
||||
import com.github.btrekkie.connectivity.Augmentation;
|
||||
|
||||
/** Stores two values: a sum and a maximum. Used for testing augmentation in ConnGraph. */
|
||||
/**
|
||||
* Stores two values: a sum and a maximum. Used for testing augmentation in ConnGraph.
|
||||
*/
|
||||
class SumAndMax {
|
||||
/** An Augmentation that combines two SumAndMaxes into one. */
|
||||
/**
|
||||
* An Augmentation that combines two SumAndMaxes into one.
|
||||
*/
|
||||
public static final Augmentation AUGMENTATION = new Augmentation() {
|
||||
@Override
|
||||
public Object combine(Object value1, Object value2) {
|
||||
SumAndMax sumAndMax1 = (SumAndMax)value1;
|
||||
SumAndMax sumAndMax2 = (SumAndMax)value2;
|
||||
SumAndMax sumAndMax1 = (SumAndMax) value1;
|
||||
SumAndMax sumAndMax2 = (SumAndMax) value2;
|
||||
return new SumAndMax(sumAndMax1.sum + sumAndMax2.sum, Math.max(sumAndMax1.max, sumAndMax2.max));
|
||||
}
|
||||
};
|
||||
@@ -28,7 +32,7 @@ class SumAndMax {
|
||||
if (!(obj instanceof SumAndMax)) {
|
||||
return false;
|
||||
}
|
||||
SumAndMax sumAndMax = (SumAndMax)obj;
|
||||
SumAndMax sumAndMax = (SumAndMax) obj;
|
||||
return sum == sumAndMax.sum && max == sumAndMax.max;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,16 +8,21 @@ package com.github.btrekkie.interval_tree;
|
||||
* interval. Each node is augmented with the maximum ending value of an interval in the subtree rooted at the node.
|
||||
*/
|
||||
public class IntervalTree {
|
||||
/** The root node of the tree. */
|
||||
/**
|
||||
* The root node of the tree.
|
||||
*/
|
||||
private IntervalTreeNode root = IntervalTreeNode.LEAF;
|
||||
|
||||
/** Adds the specified interval to this. */
|
||||
/**
|
||||
* Adds the specified interval to this.
|
||||
*/
|
||||
public void addInterval(IntervalTreeInterval interval) {
|
||||
root = root.insert(new IntervalTreeNode(interval), true, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the specified interval from this, if it is present.
|
||||
*
|
||||
* @param interval The interval.
|
||||
* @return Whether the interval was present.
|
||||
*/
|
||||
|
||||
@@ -4,10 +4,14 @@ package com.github.btrekkie.interval_tree;
|
||||
* An inclusive range of values [start, end]. Two intervals are equal if they have the same starting and ending values.
|
||||
*/
|
||||
public class IntervalTreeInterval {
|
||||
/** The smallest value in the range. */
|
||||
/**
|
||||
* The smallest value in the range.
|
||||
*/
|
||||
public final double start;
|
||||
|
||||
/** The largest value in the range. */
|
||||
/**
|
||||
* The largest value in the range.
|
||||
*/
|
||||
public final double end;
|
||||
|
||||
public IntervalTreeInterval(double start, double end) {
|
||||
@@ -22,7 +26,7 @@ public class IntervalTreeInterval {
|
||||
if (!(obj instanceof IntervalTreeInterval)) {
|
||||
return false;
|
||||
}
|
||||
IntervalTreeInterval interval = (IntervalTreeInterval)obj;
|
||||
IntervalTreeInterval interval = (IntervalTreeInterval) obj;
|
||||
return start == interval.start && end == interval.end;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,13 +7,19 @@ import com.github.btrekkie.red_black_node.RedBlackNode;
|
||||
* nodes as suggested in the comments for the implementation of IntervalTree.
|
||||
*/
|
||||
class IntervalTreeNode extends RedBlackNode<IntervalTreeNode> {
|
||||
/** The dummy leaf node. */
|
||||
/**
|
||||
* The dummy leaf node.
|
||||
*/
|
||||
public static final IntervalTreeNode LEAF = new IntervalTreeNode();
|
||||
|
||||
/** The interval stored in this node. */
|
||||
/**
|
||||
* The interval stored in this node.
|
||||
*/
|
||||
public IntervalTreeInterval interval;
|
||||
|
||||
/** The maximum ending value of an interval in the subtree rooted at this node. */
|
||||
/**
|
||||
* The maximum ending value of an interval in the subtree rooted at this node.
|
||||
*/
|
||||
public double maxEnd;
|
||||
|
||||
public IntervalTreeNode(IntervalTreeInterval interval) {
|
||||
@@ -21,7 +27,9 @@ class IntervalTreeNode extends RedBlackNode<IntervalTreeNode> {
|
||||
maxEnd = interval.end;
|
||||
}
|
||||
|
||||
/** Constructs a new dummy leaf node. */
|
||||
/**
|
||||
* Constructs a new dummy leaf node.
|
||||
*/
|
||||
private IntervalTreeNode() {
|
||||
interval = null;
|
||||
maxEnd = Double.NEGATIVE_INFINITY;
|
||||
|
||||
@@ -1,17 +1,15 @@
|
||||
package com.github.btrekkie.interval_tree.test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.github.btrekkie.interval_tree.IntervalTree;
|
||||
import com.github.btrekkie.interval_tree.IntervalTreeInterval;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class IntervalTreeTest {
|
||||
/** Tests IntervalTree. */
|
||||
/**
|
||||
* Tests IntervalTree.
|
||||
*/
|
||||
@Test
|
||||
public void test() {
|
||||
IntervalTree tree = new IntervalTree();
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package com.github.btrekkie.red_black_node.test;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Tests RedBlackNode. Most of the testing for RedBlackNode takes place in TreeListTest, IntervalTreeTest,
|
||||
@@ -30,8 +30,9 @@ public class RedBlackNodeTest {
|
||||
/**
|
||||
* Returns whether the nodes in the subtree rooted at the specified node are ordered correctly, as in
|
||||
* TestRedBlackNode.assertOrderIsValid.
|
||||
*
|
||||
* @param comparator A comparator indicating how the nodes should be ordered. If this is null, we use the nodes'
|
||||
* natural ordering, as in TestRedBlackNode.compare.
|
||||
* natural ordering, as in TestRedBlackNode.compare.
|
||||
*/
|
||||
private boolean isOrderValid(TestRedBlackNode node, Comparator<TestRedBlackNode> comparator) {
|
||||
try {
|
||||
@@ -42,7 +43,9 @@ public class RedBlackNodeTest {
|
||||
}
|
||||
}
|
||||
|
||||
/** Tests RedBlackNode.assertSubtreeIsValid() and RedBlackNode.assertOrderIsValid. */
|
||||
/**
|
||||
* Tests RedBlackNode.assertSubtreeIsValid() and RedBlackNode.assertOrderIsValid.
|
||||
*/
|
||||
@Test
|
||||
public void testAssertIsValid() {
|
||||
// Create a perfectly balanced tree of height 3
|
||||
@@ -129,12 +132,12 @@ public class RedBlackNodeTest {
|
||||
node2.value = 3;
|
||||
assertFalse(isOrderValid(node3, null));
|
||||
assertFalse(
|
||||
isOrderValid(node3, new Comparator<TestRedBlackNode>() {
|
||||
@Override
|
||||
public int compare(TestRedBlackNode node1, TestRedBlackNode node2) {
|
||||
return node1.value - node2.value;
|
||||
}
|
||||
}));
|
||||
isOrderValid(node3, new Comparator<TestRedBlackNode>() {
|
||||
@Override
|
||||
public int compare(TestRedBlackNode node1, TestRedBlackNode node2) {
|
||||
return node1.value - node2.value;
|
||||
}
|
||||
}));
|
||||
node3.value = 3;
|
||||
node2.value = 2;
|
||||
|
||||
@@ -161,11 +164,11 @@ public class RedBlackNodeTest {
|
||||
assertTrue(isOrderValid(node0, null));
|
||||
assertTrue(isOrderValid(TestRedBlackNode.LEAF, null));
|
||||
assertTrue(
|
||||
isOrderValid(node3, new Comparator<TestRedBlackNode>() {
|
||||
@Override
|
||||
public int compare(TestRedBlackNode node1, TestRedBlackNode node2) {
|
||||
return node1.value - node2.value;
|
||||
}
|
||||
}));
|
||||
isOrderValid(node3, new Comparator<TestRedBlackNode>() {
|
||||
@Override
|
||||
public int compare(TestRedBlackNode node1, TestRedBlackNode node2) {
|
||||
return node1.value - node2.value;
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,22 +2,32 @@ package com.github.btrekkie.red_black_node.test;
|
||||
|
||||
import com.github.btrekkie.red_black_node.RedBlackNode;
|
||||
|
||||
/** A RedBlackNode for RedBlackNodeTest. */
|
||||
/**
|
||||
* A RedBlackNode for RedBlackNodeTest.
|
||||
*/
|
||||
class TestRedBlackNode extends RedBlackNode<TestRedBlackNode> {
|
||||
/** The dummy leaf node. */
|
||||
/**
|
||||
* The dummy leaf node.
|
||||
*/
|
||||
public static final TestRedBlackNode LEAF = new TestRedBlackNode();
|
||||
|
||||
/** The value stored in this node. "value" is unspecified if this is a leaf node. */
|
||||
/**
|
||||
* The value stored in this node. "value" is unspecified if this is a leaf node.
|
||||
*/
|
||||
public int value;
|
||||
|
||||
/** Whether this node is considered valid, as in assertNodeIsValid(). */
|
||||
/**
|
||||
* Whether this node is considered valid, as in assertNodeIsValid().
|
||||
*/
|
||||
public boolean isValid = true;
|
||||
|
||||
public TestRedBlackNode(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/** Constructs a new dummy leaf node. */
|
||||
/**
|
||||
* Constructs a new dummy leaf node.
|
||||
*/
|
||||
private TestRedBlackNode() {
|
||||
|
||||
}
|
||||
|
||||
@@ -1,14 +1,20 @@
|
||||
package com.github.btrekkie.sub_array_min;
|
||||
|
||||
/** A list of integers. SubArrayMin provides the ability to quickly determine the minimum value in a given sublist. */
|
||||
/**
|
||||
* A list of integers. SubArrayMin provides the ability to quickly determine the minimum value in a given sublist.
|
||||
*/
|
||||
/* We implement SubArrayMin using a red-black tree augmented by subtree size and minimum value. Using the subtree size
|
||||
* augmentation, we can find the node at a given index.
|
||||
*/
|
||||
public class SubArrayMin {
|
||||
/** The root node. */
|
||||
/**
|
||||
* The root node.
|
||||
*/
|
||||
private SubArrayMinNode root = SubArrayMinNode.LEAF;
|
||||
|
||||
/** Appends the specified value to the end of the list. */
|
||||
/**
|
||||
* Appends the specified value to the end of the list.
|
||||
*/
|
||||
public void add(int value) {
|
||||
SubArrayMinNode newNode = new SubArrayMinNode(value);
|
||||
newNode.left = SubArrayMinNode.LEAF;
|
||||
@@ -25,7 +31,9 @@ public class SubArrayMin {
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns the node for the element with the specified index. Assumes "index" is in the range [0, root.size). */
|
||||
/**
|
||||
* Returns the node for the element with the specified index. Assumes "index" is in the range [0, root.size).
|
||||
*/
|
||||
private SubArrayMinNode getNode(int index) {
|
||||
if (index < 0 || index >= root.size) {
|
||||
throw new IndexOutOfBoundsException("Index " + index + " is not in the range [0, " + root.size + ")");
|
||||
|
||||
@@ -2,18 +2,28 @@ package com.github.btrekkie.sub_array_min;
|
||||
|
||||
import com.github.btrekkie.red_black_node.RedBlackNode;
|
||||
|
||||
/** A node in a SubArrayMin object. See the comments for the implementation of that class. */
|
||||
/**
|
||||
* A node in a SubArrayMin object. See the comments for the implementation of that class.
|
||||
*/
|
||||
class SubArrayMinNode extends RedBlackNode<SubArrayMinNode> {
|
||||
/** The dummy leaf node. */
|
||||
/**
|
||||
* The dummy leaf node.
|
||||
*/
|
||||
public static final SubArrayMinNode LEAF = new SubArrayMinNode();
|
||||
|
||||
/** The element stored in the node. The value is unspecified if this is a leaf node. */
|
||||
/**
|
||||
* The element stored in the node. The value is unspecified if this is a leaf node.
|
||||
*/
|
||||
public final int value;
|
||||
|
||||
/** The number of elements in the subtree rooted at this node. */
|
||||
/**
|
||||
* The number of elements in the subtree rooted at this node.
|
||||
*/
|
||||
public int size;
|
||||
|
||||
/** The minimum element in the subtree rooted at this node. This is Integer.MAX_VALUE if this is a leaf node. */
|
||||
/**
|
||||
* The minimum element in the subtree rooted at this node. This is Integer.MAX_VALUE if this is a leaf node.
|
||||
*/
|
||||
public int min;
|
||||
|
||||
public SubArrayMinNode(int value) {
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
package com.github.btrekkie.sub_array_min.test;
|
||||
|
||||
import com.github.btrekkie.sub_array_min.SubArrayMin;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.github.btrekkie.sub_array_min.SubArrayMin;
|
||||
|
||||
public class SubArrayMinTest {
|
||||
/** Tests SubArrayMin. */
|
||||
/**
|
||||
* Tests SubArrayMin.
|
||||
*/
|
||||
@Test
|
||||
public void test() {
|
||||
SubArrayMin sam = new SubArrayMin();
|
||||
|
||||
Reference in New Issue
Block a user