From eeb42aca42a105ca34c6506d18622832b62e4ab2 Mon Sep 17 00:00:00 2001 From: Nees Jan van Eck <36573334+neesjanvaneck@users.noreply.github.com> Date: Sat, 19 Aug 2023 22:18:10 +0200 Subject: [PATCH] Cleanup code --- .gitignore | 10 +-- .../java/nl/cwts/networkanalysis/Network.java | 33 +++++--- .../java/nl/cwts/util/LargeBooleanArray.java | 55 ++++++------- .../java/nl/cwts/util/LargeDoubleArray.java | 79 ++++++++----------- src/main/java/nl/cwts/util/LargeIntArray.java | 72 ++++++++--------- .../java/nl/cwts/util/LargeLongArray.java | 72 ++++++++--------- .../TestNetworkClustering.java | 47 +++++------ .../nl/cwts/util/LargeBooleanArrayTest.java | 32 ++++---- .../nl/cwts/util/LargeDoubleArrayTest.java | 33 ++++---- .../java/nl/cwts/util/LargeIntArrayTest.java | 34 ++++---- .../java/nl/cwts/util/LargeLongArrayTest.java | 34 ++++---- 11 files changed, 228 insertions(+), 273 deletions(-) diff --git a/.gitignore b/.gitignore index 6c4f40c..83eb170 100644 --- a/.gitignore +++ b/.gitignore @@ -6,16 +6,10 @@ build.xml nbbuild.xml manifest.mf nbproject/ -/test/ -src/PublicationClustering.java +/bin/ /dist/ -test_network.ser -*.txt -bin/ +/test/ .classpath .project .settings/ .gradle/ -fastutil-8.4.2.jar -.idea/ -*.iml diff --git a/src/main/java/nl/cwts/networkanalysis/Network.java b/src/main/java/nl/cwts/networkanalysis/Network.java index 1b31900..2f5af2a 100644 --- a/src/main/java/nl/cwts/networkanalysis/Network.java +++ b/src/main/java/nl/cwts/networkanalysis/Network.java @@ -1,10 +1,5 @@ package nl.cwts.networkanalysis; -import nl.cwts.util.LargeBooleanArray; -import nl.cwts.util.LargeDoubleArray; -import nl.cwts.util.LargeIntArray; -import nl.cwts.util.LargeLongArray; - import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; @@ -15,6 +10,11 @@ import java.util.PrimitiveIterator; import java.util.Random; +import nl.cwts.util.LargeBooleanArray; +import nl.cwts.util.LargeDoubleArray; +import nl.cwts.util.LargeIntArray; +import nl.cwts.util.LargeLongArray; + /** * Network. * @@ -39,8 +39,6 @@ public class Network implements Serializable { private static final long serialVersionUID = 1; - public static final long MAX_N_EDGES = LargeDoubleArray.MAX_SIZE / 2; - /** * Number of nodes. */ @@ -522,6 +520,7 @@ public int[] getNeighbors(int node) * Returns an iterable over all the neighbors of a node. * * @param node Node + * * @return Iterable over neighbors */ public LargeIntArray.FromToIterable neighbors(int node) @@ -533,7 +532,8 @@ public LargeIntArray.FromToIterable neighbors(int node) * Returns an iterable over all the incident edges of a node. * * @param node Node - * @return Iterable over incident edges. + * + * @return Iterable over incident edges */ public RangeIterable incidentEdges(int node) { @@ -638,7 +638,8 @@ public double[] getEdgeWeights(int node) * node. * * @param node Node - * @return Iterable over edge weights of a node. + * + * @return Iterable over edge weights of a node */ public LargeDoubleArray.FromToIterable edgeWeights(int node) { @@ -1307,6 +1308,12 @@ else if (k == l) } } + /** + * Sorts a list of edges and the corresponding edge weights. + * + * @param edges Edge list + * @param edgeWeights Edge weights + */ public static void sortEdges(LargeIntArray[] edges, LargeDoubleArray edgeWeights) { class EdgeComparator @@ -1485,7 +1492,7 @@ private double getRandomNumber(int node1, int node2, LargeDoubleArray randomNumb return randomNumbers.get(i * nNodes + j); } - protected Network createSubnetwork(Clustering clustering, int cluster, int[] nodes, int[] subnetworkNodes, LargeIntArray subnetworkNeighbors, LargeDoubleArray subnetworkEdgeWeights) + private Network createSubnetwork(Clustering clustering, int cluster, int[] nodes, int[] subnetworkNodes, LargeIntArray subnetworkNeighbors, LargeDoubleArray subnetworkEdgeWeights) { int i, j; long k; @@ -1536,6 +1543,9 @@ protected Network createSubnetwork(Clustering clustering, int cluster, int[] nod return subnetwork; } + /** + * Iterable starting from a certain element to a certain element. + */ public class RangeIterable implements Iterable { long from; @@ -1554,6 +1564,9 @@ public java.util.Iterator iterator() } } + /** + * Iterator capable of iterating over a specified range. + */ public class RangeIterator implements PrimitiveIterator.OfLong { private final long to; diff --git a/src/main/java/nl/cwts/util/LargeBooleanArray.java b/src/main/java/nl/cwts/util/LargeBooleanArray.java index b1caf16..3d02cb7 100644 --- a/src/main/java/nl/cwts/util/LargeBooleanArray.java +++ b/src/main/java/nl/cwts/util/LargeBooleanArray.java @@ -1,12 +1,11 @@ package nl.cwts.util; -/* We need the fastutil package for sorting purposes */ +import java.util.Arrays; +/* We need the fastutil package for sorting purposes. */ import it.unimi.dsi.fastutil.BigArrays; import it.unimi.dsi.fastutil.longs.LongComparator; -import java.util.Arrays; - /** *

* This class enables arrays of booleans up to 64-bits in size (for the exact @@ -28,15 +27,18 @@ * (indicated by {@link #MINIMUM_INITIAL_CAPACITY}). This dynamic array then * also supports {@link #push} and {@link #pop} operations. *

+ * + * @author Vincent Traag + * @author Nees Jan van Eck */ public final class LargeBooleanArray implements Cloneable { /** - * The number of bits to use for each individual array + * The number of bits to use for each individual array. */ public static final byte ARRAY_BIT_SIZE = 30; /** - * The maximum size of each individual array + * The maximum size of each individual array. */ public static final int MAX_SIZE_ARRAY = 1 << ARRAY_BIT_SIZE; /** @@ -108,8 +110,8 @@ public LargeBooleanArray(long size) segment = 0; while (remainingLength > MAX_SIZE_ARRAY) { - // As long as longer than a single array, we allocate - // up until MAX_SIZE_ARRAY + // As long as longer than a single array, we allocate up until + // MAX_SIZE_ARRAY this.values[segment] = new boolean[MAX_SIZE_ARRAY]; remainingLength -= MAX_SIZE_ARRAY; segment++; @@ -236,7 +238,7 @@ public boolean get(int segment, int offset) } /** - * Sets element to value for indicated index + * Sets element to value for indicated index. * * @param index Index of element * @param value Value @@ -286,7 +288,7 @@ public void fill(long from, long to, boolean constant) // Fill first segment segment = getSegment(from); Arrays.fill(this.values[segment], getOffset(from), - segment == segmentTo ? getOffset(to) : this.values[segment].length, constant); + segment == segmentTo ? getOffset(to) : this.values[segment].length, constant); segment++; // Fill subsequent segments @@ -295,8 +297,7 @@ public void fill(long from, long to, boolean constant) // Fill last segment if (segment == segmentTo && offsetTo > 0) - Arrays.fill(this.values[segment], 0, - offsetTo, constant); + Arrays.fill(this.values[segment], 0, offsetTo, constant); } /*************************************************************************** @@ -389,8 +390,8 @@ public void ensureCapacity(long minCapacity) if (getOffset(oldCapacity) > 0) nOldSegments += 1; // Add one if there was a remainder - // Simply refer to the previously existing segments for all - // segments except for the last one. + // Simply refer to the previously existing segments for all segments + // except for the last one. remainingLength = newCapacity; for (segment = 0; segment < nOldSegments - 1; segment++) { @@ -400,11 +401,9 @@ public void ensureCapacity(long minCapacity) // We now need to copy only the last old segment if (remainingLength > MAX_SIZE_ARRAY) - newValues[segment] = Arrays.copyOf(values[segment], - MAX_SIZE_ARRAY); + newValues[segment] = Arrays.copyOf(values[segment], MAX_SIZE_ARRAY); else - newValues[segment] = Arrays.copyOf(values[segment], - (int)remainingLength); + newValues[segment] = Arrays.copyOf(values[segment], (int)remainingLength); remainingLength -= newValues[segment].length; segment++; @@ -431,6 +430,7 @@ public void ensureCapacity(long minCapacity) /** * Resizes array. + * * @param size New size */ public void resize(long size) @@ -473,8 +473,7 @@ public void shrink() // Truncate the last segment to the new capacity if (remainingLength > 0) - newValues[segment] = Arrays.copyOf(values[segment], - (int)remainingLength); + newValues[segment] = Arrays.copyOf(values[segment], (int)remainingLength); // Assign to actual values this.values = newValues; @@ -484,6 +483,7 @@ public void shrink() /** * Gets size of array. + * *

This is always less than or equal to the capacity.

* * @return Size @@ -495,6 +495,7 @@ public long size() /** * Gets capacity of array. + * *

This is always greater than or equal to the size.

* * @return Capacity @@ -555,9 +556,7 @@ private int compare(long indexA, long indexB) */ public void mergeSort() { - BigArrays.mergeSort(0, capacity, - this::compare, - this::swap); + BigArrays.mergeSort(0, capacity, this::compare, this::swap); } /** @@ -565,9 +564,7 @@ public void mergeSort() */ public void quickSort() { - BigArrays.quickSort(0, capacity, - this::compare, - this::swap); + BigArrays.quickSort(0, capacity, this::compare, this::swap); } /** @@ -600,10 +597,12 @@ public void updateFrom(LargeBooleanArray array) /** * Updates this array from the provided array. + * *

* Values from other array starting at {@code from} until * {@code to} (exclusive) will be copied to this array, starting * from the {@code insertionPoint} onwards. + *

* * @param array Array to update from * @param from Index in {@code array} from where to update, @@ -640,8 +639,7 @@ public void updateFrom(LargeBooleanArray array, long from, long to, long inserti } // Copy actual value - this.values[segment][offset] = - array.values[segmentFrom][offsetFrom]; + this.values[segment][offset] = array.values[segmentFrom][offsetFrom]; offsetFrom++; offset++; @@ -653,6 +651,7 @@ public void updateFrom(LargeBooleanArray array, long from, long to, long inserti * * @param from From index, inclusive * @param to To index, exclusive + * * @return New array */ public LargeBooleanArray copyOfRange(long from, long to) @@ -771,6 +770,4 @@ public LargeBooleanArray clone() return null; } } - } - diff --git a/src/main/java/nl/cwts/util/LargeDoubleArray.java b/src/main/java/nl/cwts/util/LargeDoubleArray.java index 062ad30..2f95cfc 100644 --- a/src/main/java/nl/cwts/util/LargeDoubleArray.java +++ b/src/main/java/nl/cwts/util/LargeDoubleArray.java @@ -1,13 +1,12 @@ package nl.cwts.util; -/* We need the fastutil package for sorting purposes */ +import java.util.Arrays; +import java.util.PrimitiveIterator; +/* We need the fastutil package for sorting purposes. */ import it.unimi.dsi.fastutil.BigArrays; import it.unimi.dsi.fastutil.longs.LongComparator; -import java.util.Arrays; -import java.util.PrimitiveIterator; - /** *

* This class enables arrays of doubles up to 64-bits in size (for the exact @@ -43,15 +42,18 @@ * {@code for (double x : array)} and * {@code for (double x : array.fromto(10, 50)} *

+ * + * @author Vincent Traag + * @author Nees Jan van Eck */ public final class LargeDoubleArray implements Cloneable, Iterable { /** - * The number of bits to use for each individual array + * The number of bits to use for each individual array. */ public static final byte ARRAY_BIT_SIZE = 30; /** - * The maximum size of each individual array + * The maximum size of each individual array. */ public static final int MAX_SIZE_ARRAY = 1 << ARRAY_BIT_SIZE; /** @@ -123,8 +125,8 @@ public LargeDoubleArray(long size) segment = 0; while (remainingLength > MAX_SIZE_ARRAY) { - // As long as longer than a single array, we allocate - // up until MAX_SIZE_ARRAY + // As long as longer than a single array, we allocate up until + // MAX_SIZE_ARRAY this.values[segment] = new double[MAX_SIZE_ARRAY]; remainingLength -= MAX_SIZE_ARRAY; segment++; @@ -251,7 +253,7 @@ public double get(int segment, int offset) } /** - * Sets element to value for indicated index + * Sets element to value for indicated index. * * @param index Index of element * @param value Value @@ -301,7 +303,7 @@ public void fill(long from, long to, double constant) // Fill first segment segment = getSegment(from); Arrays.fill(this.values[segment], getOffset(from), - segment == segmentTo ? getOffset(to) : this.values[segment].length, constant); + segment == segmentTo ? getOffset(to) : this.values[segment].length, constant); segment++; // Fill subsequent segments @@ -310,8 +312,7 @@ public void fill(long from, long to, double constant) // Fill last segment if (segment == segmentTo && offsetTo > 0) - Arrays.fill(this.values[segment], 0, - offsetTo, constant); + Arrays.fill(this.values[segment], 0, offsetTo, constant); } /*************************************************************************** @@ -388,8 +389,7 @@ public void ensureCapacity(long minCapacity) oldCapacity = capacity; if (minCapacity > oldCapacity) { - newCapacity = - (long)((1 + RELATIVE_CAPACITY_INCREASE) * oldCapacity); + newCapacity = (long)((1 + RELATIVE_CAPACITY_INCREASE) * oldCapacity); if (newCapacity < minCapacity) newCapacity = minCapacity; @@ -405,8 +405,8 @@ public void ensureCapacity(long minCapacity) if (getOffset(oldCapacity) > 0) nOldSegments += 1; // Add one if there was a remainder - // Simply refer to the previously existing segments for all - // segments except for the last one. + // Simply refer to the previously existing segments for all segments + // except for the last one. remainingLength = newCapacity; for (segment = 0; segment < nOldSegments - 1; segment++) { @@ -416,11 +416,9 @@ public void ensureCapacity(long minCapacity) // We now need to copy only the last old segment if (remainingLength > MAX_SIZE_ARRAY) - newValues[segment] = Arrays.copyOf(values[segment], - MAX_SIZE_ARRAY); + newValues[segment] = Arrays.copyOf(values[segment], MAX_SIZE_ARRAY); else - newValues[segment] = Arrays.copyOf(values[segment], - (int)remainingLength); + newValues[segment] = Arrays.copyOf(values[segment], (int)remainingLength); remainingLength -= newValues[segment].length; segment++; @@ -447,6 +445,7 @@ public void ensureCapacity(long minCapacity) /** * Resizes array. + * * @param size New size */ public void resize(long size) @@ -489,8 +488,7 @@ public void shrink() // Truncate the last segment to the new capacity if (remainingLength > 0) - newValues[segment] = Arrays.copyOf(values[segment], - (int)remainingLength); + newValues[segment] = Arrays.copyOf(values[segment], (int)remainingLength); // Assign to actual values this.values = newValues; @@ -500,6 +498,7 @@ public void shrink() /** * Gets size of array. + * *

This is always less than or equal to the capacity.

* * @return Size @@ -511,6 +510,7 @@ public long size() /** * Gets capacity of array. + * *

This is always greater than or equal to the size.

* * @return Capacity @@ -568,7 +568,6 @@ public void divide(long index, double divisor) this.values[getSegment(index)][getOffset(index)] /= divisor; } - /** * Calculates the sum of all elements. * @@ -726,9 +725,7 @@ private int compare(long indexA, long indexB) */ public void mergeSort() { - BigArrays.mergeSort(0, capacity, - this::compare, - this::swap); + BigArrays.mergeSort(0, capacity, this::compare, this::swap); } /** @@ -736,9 +733,7 @@ public void mergeSort() */ public void quickSort() { - BigArrays.quickSort(0, capacity, - this::compare, - this::swap); + BigArrays.quickSort(0, capacity, this::compare, this::swap); } /** @@ -767,14 +762,11 @@ public void sort(LongComparator comparator) * it is not sorted, the results are undefined. If the array contains * multiple elements with the specified value, there is no guarantee which * one will be found. - *

- * Parameters: a - the array to be searched key - the value to be searched - * for Returns: *

* * @param value The value to search for * - * @return index of the search key, if it is contained in the array; + * @return Index of the search key, if it is contained in the array; * otherwise, (-(insertion point) - 1). The insertion point is defined as * the point at which the key would be inserted into the array: the index of * the first element greater than the key, or the total size if all elements @@ -796,16 +788,13 @@ public long binarySearch(double value) * making this call. If it is not sorted, the results are undefined. If the * array contains multiple elements with the specified value, there is no * guarantee which one will be found. - *

- * Parameters: a - the array to be searched key - the value to be searched - * for Returns: *

* * @param from From index, inclusive * @param to To index, exclusive * @param value The value to search for * - * @return index of the search key, if it is contained in the array; + * @return Index of the search key, if it is contained in the array; * otherwise, (-(insertion point) - 1). The insertion point is defined as * the point at which the key would be inserted into the array: the index of * the first element greater than the key, or the total size if all elements @@ -896,10 +885,12 @@ public void updateFrom(LargeDoubleArray array) /** * Updates this array from the provided array. + * *

* Values from other array starting at {@code from} until * {@code to} (exclusive) will be copied to this array, starting * from the {@code insertionPoint} onwards. + *

* * @param array Array to update from * @param from Index in {@code array} from where to update, @@ -936,8 +927,7 @@ public void updateFrom(LargeDoubleArray array, long from, long to, long insertio } // Copy actual value - this.values[segment][offset] = - array.values[segmentFrom][offsetFrom]; + this.values[segment][offset] = array.values[segmentFrom][offsetFrom]; offsetFrom++; offset++; @@ -949,6 +939,7 @@ public void updateFrom(LargeDoubleArray array, long from, long to, long insertio * * @param from From index, inclusive * @param to To index, exclusive + * * @return New array */ public LargeDoubleArray copyOfRange(long from, long to) @@ -1069,7 +1060,7 @@ public LargeDoubleArray clone() } /** - * Iterable starting from a certain element + * Iterable starting from a certain element. */ public class FromIterable implements Iterable { @@ -1088,7 +1079,7 @@ public java.util.Iterator iterator() } /** - * Iterable starting from a certain element to a certain element + * Iterable starting from a certain element to a certain element. */ public class FromToIterable implements Iterable { @@ -1109,7 +1100,7 @@ public java.util.Iterator iterator() } /** - * Iterator capable of iterating over specified range. + * Iterator capable of iterating over a specified range. */ public class Iterator implements PrimitiveIterator.OfDouble { @@ -1186,7 +1177,7 @@ public Iterator(long from, long to) } /** - * Provides next value in array + * Provides next value in array. * * @return Next value */ @@ -1224,6 +1215,4 @@ public boolean hasNext() return hasNext; } } - } - diff --git a/src/main/java/nl/cwts/util/LargeIntArray.java b/src/main/java/nl/cwts/util/LargeIntArray.java index 22ab3dc..9c50e86 100644 --- a/src/main/java/nl/cwts/util/LargeIntArray.java +++ b/src/main/java/nl/cwts/util/LargeIntArray.java @@ -1,13 +1,12 @@ package nl.cwts.util; -/* We need the fastutil package for sorting purposes */ +import java.util.Arrays; +import java.util.PrimitiveIterator; +/* We need the fastutil package for sorting purposes. */ import it.unimi.dsi.fastutil.BigArrays; import it.unimi.dsi.fastutil.longs.LongComparator; -import java.util.Arrays; -import java.util.PrimitiveIterator; - /** *

* This class enables arrays of ints up to 64-bits in size (for the exact @@ -43,15 +42,18 @@ * {@code for (int x : array)} and * {@code for (int x : array.fromto(10, 50)} *

+ * + * @author Vincent Traag + * @author Nees Jan van Eck */ public final class LargeIntArray implements Cloneable, Iterable { /** - * The number of bits to use for each individual array + * The number of bits to use for each individual array. */ public static final byte ARRAY_BIT_SIZE = 30; /** - * The maximum size of each individual array + * The maximum size of each individual array. */ public static final int MAX_SIZE_ARRAY = 1 << ARRAY_BIT_SIZE; /** @@ -251,7 +253,7 @@ public int get(int segment, int offset) } /** - * Sets element to value for indicated index + * Sets element to value for indicated index. * * @param index Index of element * @param value Value @@ -301,7 +303,7 @@ public void fill(long from, long to, int constant) // Fill first segment segment = getSegment(from); Arrays.fill(this.values[segment], getOffset(from), - segment == segmentTo ? getOffset(to) : this.values[segment].length, constant); + segment == segmentTo ? getOffset(to) : this.values[segment].length, constant); segment++; // Fill subsequent segments @@ -310,8 +312,7 @@ public void fill(long from, long to, int constant) // Fill last segment if (segment == segmentTo && offsetTo > 0) - Arrays.fill(this.values[segment], 0, - offsetTo, constant); + Arrays.fill(this.values[segment], 0, offsetTo, constant); } /*************************************************************************** @@ -404,8 +405,8 @@ public void ensureCapacity(long minCapacity) if (getOffset(oldCapacity) > 0) nOldSegments += 1; // Add one if there was a remainder - // Simply refer to the previously existing segments for all - // segments except for the last one. + // Simply refer to the previously existing segments for all segments + // except for the last one. remainingLength = newCapacity; for (segment = 0; segment < nOldSegments - 1; segment++) { @@ -415,11 +416,9 @@ public void ensureCapacity(long minCapacity) // We now need to copy only the last old segment if (remainingLength > MAX_SIZE_ARRAY) - newValues[segment] = Arrays.copyOf(values[segment], - MAX_SIZE_ARRAY); + newValues[segment] = Arrays.copyOf(values[segment], MAX_SIZE_ARRAY); else - newValues[segment] = Arrays.copyOf(values[segment], - (int)remainingLength); + newValues[segment] = Arrays.copyOf(values[segment], (int)remainingLength); remainingLength -= newValues[segment].length; segment++; @@ -446,6 +445,7 @@ public void ensureCapacity(long minCapacity) /** * Resizes array. + * * @param size New size */ public void resize(long size) @@ -488,8 +488,7 @@ public void shrink() // Truncate the last segment to the new capacity if (remainingLength > 0) - newValues[segment] = Arrays.copyOf(values[segment], - (int)remainingLength); + newValues[segment] = Arrays.copyOf(values[segment], (int)remainingLength); // Assign to actual values this.values = newValues; @@ -499,6 +498,7 @@ public void shrink() /** * Gets size of array. + * *

This is always less than or equal to the capacity.

* * @return Size @@ -510,6 +510,7 @@ public long size() /** * Gets capacity of array. + * *

This is always greater than or equal to the size.

* * @return Capacity @@ -567,7 +568,6 @@ public void divide(long index, int divisor) this.values[getSegment(index)][getOffset(index)] /= divisor; } - /** * Calculates the sum of all elements. * @@ -724,9 +724,7 @@ private int compare(long indexA, long indexB) */ public void mergeSort() { - BigArrays.mergeSort(0, capacity, - this::compare, - this::swap); + BigArrays.mergeSort(0, capacity, this::compare, this::swap); } /** @@ -734,9 +732,7 @@ public void mergeSort() */ public void quickSort() { - BigArrays.quickSort(0, capacity, - this::compare, - this::swap); + BigArrays.quickSort(0, capacity, this::compare, this::swap); } /** @@ -765,14 +761,11 @@ public void sort(LongComparator comparator) * it is not sorted, the results are undefined. If the array contains * multiple elements with the specified value, there is no guarantee which * one will be found. - *

- * Parameters: a - the array to be searched key - the value to be searched - * for Returns: *

* * @param value The value to search for * - * @return index of the search key, if it is contained in the array; + * @return Index of the search key, if it is contained in the array; * otherwise, (-(insertion point) - 1). The insertion point is defined as * the point at which the key would be inserted into the array: the index of * the first element greater than the key, or the total size if all elements @@ -794,16 +787,13 @@ public long binarySearch(int value) * making this call. If it is not sorted, the results are undefined. If the * array contains multiple elements with the specified value, there is no * guarantee which one will be found. - *

- * Parameters: a - the array to be searched key - the value to be searched - * for Returns: *

* * @param from From index, inclusive * @param to To index, exclusive * @param value The value to search for * - * @return index of the search key, if it is contained in the array; + * @return Index of the search key, if it is contained in the array; * otherwise, (-(insertion point) - 1). The insertion point is defined as * the point at which the key would be inserted into the array: the index of * the first element greater than the key, or the total size if all elements @@ -894,10 +884,12 @@ public void updateFrom(LargeIntArray array) /** * Updates this array from the provided array. + * *

* Values from other array starting at {@code from} until * {@code to} (exclusive) will be copied to this array, starting * from the {@code insertionPoint} onwards. + *

* * @param array Array to update from * @param from Index in {@code array} from where to update, @@ -934,8 +926,7 @@ public void updateFrom(LargeIntArray array, long from, long to, long insertionPo } // Copy actual value - this.values[segment][offset] = - array.values[segmentFrom][offsetFrom]; + this.values[segment][offset] = array.values[segmentFrom][offsetFrom]; offsetFrom++; offset++; @@ -947,6 +938,7 @@ public void updateFrom(LargeIntArray array, long from, long to, long insertionPo * * @param from From index, inclusive * @param to To index, exclusive + * * @return New array */ public LargeIntArray copyOfRange(long from, long to) @@ -1067,7 +1059,7 @@ public LargeIntArray clone() } /** - * Iterable starting from a certain element + * Iterable starting from a certain element. */ public class FromIterable implements Iterable { @@ -1086,7 +1078,7 @@ public java.util.Iterator iterator() } /** - * Iterable starting from a certain element to a certain element + * Iterable starting from a certain element to a certain element. */ public class FromToIterable implements Iterable { @@ -1107,7 +1099,7 @@ public java.util.Iterator iterator() } /** - * Iterator capable of iterating over specified range. + * Iterator capable of iterating over a specified range. */ public class Iterator implements PrimitiveIterator.OfInt { @@ -1184,7 +1176,7 @@ public Iterator(long from, long to) } /** - * Provides next value in array + * Provides next value in array. * * @return Next value */ @@ -1222,6 +1214,4 @@ public boolean hasNext() return hasNext; } } - } - diff --git a/src/main/java/nl/cwts/util/LargeLongArray.java b/src/main/java/nl/cwts/util/LargeLongArray.java index 92c807f..8c5a01d 100644 --- a/src/main/java/nl/cwts/util/LargeLongArray.java +++ b/src/main/java/nl/cwts/util/LargeLongArray.java @@ -1,13 +1,12 @@ package nl.cwts.util; -/* We need the fastutil package for sorting purposes */ +import java.util.Arrays; +import java.util.PrimitiveIterator; +/* We need the fastutil package for sorting purposes. */ import it.unimi.dsi.fastutil.BigArrays; import it.unimi.dsi.fastutil.longs.LongComparator; -import java.util.Arrays; -import java.util.PrimitiveIterator; - /** *

* This class enables arrays of longs up to 64-bits in size (for the exact @@ -43,15 +42,18 @@ * {@code for (long x : array)} and * {@code for (long x : array.fromto(10, 50)} *

+ * + * @author Vincent Traag + * @author Nees Jan van Eck */ public final class LargeLongArray implements Cloneable, Iterable { /** - * The number of bits to use for each individual array + * The number of bits to use for each individual array. */ public static final byte ARRAY_BIT_SIZE = 30; /** - * The maximum size of each individual array + * The maximum size of each individual array. */ public static final int MAX_SIZE_ARRAY = 1 << ARRAY_BIT_SIZE; /** @@ -251,7 +253,7 @@ public long get(int segment, int offset) } /** - * Sets element to value for indicated index + * Sets element to value for indicated index. * * @param index Index of element * @param value Value @@ -301,7 +303,7 @@ public void fill(long from, long to, long constant) // Fill first segment segment = getSegment(from); Arrays.fill(this.values[segment], getOffset(from), - segment == segmentTo ? getOffset(to) : this.values[segment].length, constant); + segment == segmentTo ? getOffset(to) : this.values[segment].length, constant); segment++; // Fill subsequent segments @@ -310,8 +312,7 @@ public void fill(long from, long to, long constant) // Fill last segment if (segment == segmentTo && offsetTo > 0) - Arrays.fill(this.values[segment], 0, - offsetTo, constant); + Arrays.fill(this.values[segment], 0, offsetTo, constant); } /*************************************************************************** @@ -404,8 +405,8 @@ public void ensureCapacity(long minCapacity) if (getOffset(oldCapacity) > 0) nOldSegments += 1; // Add one if there was a remainder - // Simply refer to the previously existing segments for all - // segments except for the last one. + // Simply refer to the previously existing segments for all segments + // except for the last one. remainingLength = newCapacity; for (segment = 0; segment < nOldSegments - 1; segment++) { @@ -415,11 +416,9 @@ public void ensureCapacity(long minCapacity) // We now need to copy only the last old segment if (remainingLength > MAX_SIZE_ARRAY) - newValues[segment] = Arrays.copyOf(values[segment], - MAX_SIZE_ARRAY); + newValues[segment] = Arrays.copyOf(values[segment], MAX_SIZE_ARRAY); else - newValues[segment] = Arrays.copyOf(values[segment], - (int)remainingLength); + newValues[segment] = Arrays.copyOf(values[segment], (int)remainingLength); remainingLength -= newValues[segment].length; segment++; @@ -446,6 +445,7 @@ public void ensureCapacity(long minCapacity) /** * Resizes array. + * * @param size New size */ public void resize(long size) @@ -488,8 +488,7 @@ public void shrink() // Truncate the last segment to the new capacity if (remainingLength > 0) - newValues[segment] = Arrays.copyOf(values[segment], - (int)remainingLength); + newValues[segment] = Arrays.copyOf(values[segment], (int)remainingLength); // Assign to actual values this.values = newValues; @@ -499,6 +498,7 @@ public void shrink() /** * Gets size of array. + * *

This is always less than or equal to the capacity.

* * @return Size @@ -510,6 +510,7 @@ public long size() /** * Gets capacity of array. + * *

This is always greater than or equal to the size.

* * @return Capacity @@ -567,7 +568,6 @@ public void divide(long index, long divisor) this.values[getSegment(index)][getOffset(index)] /= divisor; } - /** * Calculates the sum of all elements. * @@ -725,9 +725,7 @@ private int compare(long indexA, long indexB) */ public void mergeSort() { - BigArrays.mergeSort(0, capacity, - this::compare, - this::swap); + BigArrays.mergeSort(0, capacity, this::compare, this::swap); } /** @@ -735,9 +733,7 @@ public void mergeSort() */ public void quickSort() { - BigArrays.quickSort(0, capacity, - this::compare, - this::swap); + BigArrays.quickSort(0, capacity, this::compare, this::swap); } /** @@ -766,14 +762,11 @@ public void sort(LongComparator comparator) * it is not sorted, the results are undefined. If the array contains * multiple elements with the specified value, there is no guarantee which * one will be found. - *

- * Parameters: a - the array to be searched key - the value to be searched - * for Returns: *

* * @param value The value to search for * - * @return index of the search key, if it is contained in the array; + * @return Index of the search key, if it is contained in the array; * otherwise, (-(insertion point) - 1). The insertion point is defined as * the point at which the key would be inserted into the array: the index of * the first element greater than the key, or the total size if all elements @@ -795,16 +788,13 @@ public long binarySearch(long value) * making this call. If it is not sorted, the results are undefined. If the * array contains multiple elements with the specified value, there is no * guarantee which one will be found. - *

- * Parameters: a - the array to be searched key - the value to be searched - * for Returns: *

* * @param from From index, inclusive * @param to To index, exclusive * @param value The value to search for * - * @return index of the search key, if it is contained in the array; + * @return Index of the search key, if it is contained in the array; * otherwise, (-(insertion point) - 1). The insertion point is defined as * the point at which the key would be inserted into the array: the index of * the first element greater than the key, or the total size if all elements @@ -895,10 +885,12 @@ public void updateFrom(LargeLongArray array) /** * Updates this array from the provided array. + * *

* Values from other array starting at {@code from} until * {@code to} (exclusive) will be copied to this array, starting * from the {@code insertionPoint} onwards. + *

* * @param array Array to update from * @param from Index in {@code array} from where to update, @@ -935,8 +927,7 @@ public void updateFrom(LargeLongArray array, long from, long to, long insertionP } // Copy actual value - this.values[segment][offset] = - array.values[segmentFrom][offsetFrom]; + this.values[segment][offset] = array.values[segmentFrom][offsetFrom]; offsetFrom++; offset++; @@ -948,6 +939,7 @@ public void updateFrom(LargeLongArray array, long from, long to, long insertionP * * @param from From index, inclusive * @param to To index, exclusive + * * @return New array */ public LargeLongArray copyOfRange(long from, long to) @@ -1068,7 +1060,7 @@ public LargeLongArray clone() } /** - * Iterable starting from a certain element + * Iterable starting from a certain element. */ public class FromIterable implements Iterable { @@ -1087,7 +1079,7 @@ public java.util.Iterator iterator() } /** - * Iterable starting from a certain element to a certain element + * Iterable starting from a certain element to a certain element. */ public class FromToIterable implements Iterable { @@ -1108,7 +1100,7 @@ public java.util.Iterator iterator() } /** - * Iterator capable of iterating over specified range. + * Iterator capable of iterating over a specified range. */ public class Iterator implements PrimitiveIterator.OfLong { @@ -1185,7 +1177,7 @@ public Iterator(long from, long to) } /** - * Provides next value in array + * Provides next value in array. * * @return Next value */ @@ -1223,6 +1215,4 @@ public boolean hasNext() return hasNext; } } - } - diff --git a/src/test/java/nl/cwts/networkanalysis/TestNetworkClustering.java b/src/test/java/nl/cwts/networkanalysis/TestNetworkClustering.java index 4b236b9..c492260 100644 --- a/src/test/java/nl/cwts/networkanalysis/TestNetworkClustering.java +++ b/src/test/java/nl/cwts/networkanalysis/TestNetworkClustering.java @@ -1,13 +1,14 @@ package nl.cwts.networkanalysis; -import nl.cwts.util.LargeIntArray; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.Random; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import nl.cwts.util.LargeIntArray; public class TestNetworkClustering { @@ -16,8 +17,7 @@ public class TestNetworkClustering @BeforeEach public void setUp() { - int[][] e = {{0, 0, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 5, 6, 6, 7}, - {1, 2, 0, 2, 3, 0, 1, 1, 4, 5, 3, 5, 3, 4, 6, 5, 7, 6}}; + int[][] e = { { 0, 0, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 5, 6, 6, 7 }, { 1, 2, 0, 2, 3, 0, 1, 1, 4, 5, 3, 5, 3, 4, 6, 5, 7, 6 } }; LargeIntArray[] edges = new LargeIntArray[2]; edges[0] = new LargeIntArray(e[0]); edges[1] = new LargeIntArray(e[1]); @@ -27,14 +27,12 @@ public void setUp() @Test public void testReducedNetworkQualityValue() { - Clustering clustering = new Clustering(new int[]{1, 1, 1, 0, 0, 0, 0, 0}); + Clustering clustering = new Clustering(new int[] { 1, 1, 1, 0, 0, 0, 0, 0 }); LeidenAlgorithm algorithm = new LeidenAlgorithm(); - double expectedQuality = algorithm.calcQuality(testNetwork, - clustering); + double expectedQuality = algorithm.calcQuality(testNetwork, clustering); Network reducedNetwork = testNetwork.createReducedNetwork(clustering); Clustering reducedClustering = new Clustering(reducedNetwork.nNodes); - double reducedQuality = algorithm.calcQuality(reducedNetwork, - reducedClustering); + double reducedQuality = algorithm.calcQuality(reducedNetwork, reducedClustering); assertEquals(expectedQuality, reducedQuality, 1e-10); } @@ -42,31 +40,24 @@ public void testReducedNetworkQualityValue() public void testLeidenAlgorithm() { double resolution = 0.1; - LeidenAlgorithm algorithm = - new LeidenAlgorithm(resolution, - LeidenAlgorithm.DEFAULT_N_ITERATIONS, - LeidenAlgorithm.DEFAULT_RANDOMNESS, - new Random(0)); + LeidenAlgorithm algorithm = new LeidenAlgorithm(resolution, LeidenAlgorithm.DEFAULT_N_ITERATIONS, + LeidenAlgorithm.DEFAULT_RANDOMNESS, new Random(0)); Clustering clustering = algorithm.findClustering(testNetwork); clustering.orderClustersByNNodes(); - Clustering expectedClustering = new Clustering(new int[]{1, 1, 1, 0, 0, 0, 0, 0}); + Clustering expectedClustering = new Clustering(new int[] { 1, 1, 1, 0, 0, 0, 0, 0 }); assertArrayEquals(expectedClustering.getClusters(), clustering.getClusters()); - assertEquals(algorithm.calcQuality(testNetwork, clustering), - (16 - 0.1 * (3 * 3 + 5 * 5)) / 18, 1e-10); + assertEquals((16 - 0.1 * (3 * 3 + 5 * 5)) / 18, algorithm.calcQuality(testNetwork, clustering), 1e-10); resolution = 0.5; - algorithm = new LeidenAlgorithm(resolution, - LeidenAlgorithm.DEFAULT_N_ITERATIONS, - LeidenAlgorithm.DEFAULT_RANDOMNESS, - new Random(0)); + algorithm = new LeidenAlgorithm(resolution, LeidenAlgorithm.DEFAULT_N_ITERATIONS, + LeidenAlgorithm.DEFAULT_RANDOMNESS, new Random(0)); clustering = algorithm.findClustering(testNetwork); clustering.orderClustersByNNodes(); - expectedClustering = new Clustering(new int[]{0, 0, 0, 1, 1, 1, 2, 2}); + expectedClustering = new Clustering(new int[] { 0, 0, 0, 1, 1, 1, 2, 2 }); assertArrayEquals(expectedClustering.getClusters(), clustering.getClusters()); - assertEquals(algorithm.calcQuality(testNetwork, clustering), - (14 - resolution * (3 * 3 + 3 * 3 + 2 * 2)) / 18, 1e-10); + assertEquals((14 - resolution * (3 * 3 + 3 * 3 + 2 * 2)) / 18, algorithm.calcQuality(testNetwork, clustering), + 1e-10); } } - diff --git a/src/test/java/nl/cwts/util/LargeBooleanArrayTest.java b/src/test/java/nl/cwts/util/LargeBooleanArrayTest.java index db5343f..e9e97f3 100644 --- a/src/test/java/nl/cwts/util/LargeBooleanArrayTest.java +++ b/src/test/java/nl/cwts/util/LargeBooleanArrayTest.java @@ -1,12 +1,12 @@ package nl.cwts.util; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Random; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; class LargeBooleanArrayTest { @@ -79,8 +79,7 @@ void ensureCapacity() { long c = a.capacity(); a.ensureCapacity(a.capacity() + 1); - assertEquals(a.capacity(), - c * (1 + LargeBooleanArray.RELATIVE_CAPACITY_INCREASE)); + assertEquals(a.capacity(), c * (1 + LargeBooleanArray.RELATIVE_CAPACITY_INCREASE)); } @Test @@ -123,8 +122,7 @@ void mergeSort() a.mergeSort(); for (long i = 1; i < a.size(); i++) - assertTrue((a.get(i) && !a.get(i - 1)) || - (a.get(i) == a.get(i - 1))); + assertTrue((a.get(i) && !a.get(i - 1)) || (a.get(i) == a.get(i - 1))); } @Test @@ -139,8 +137,7 @@ void quickSort() a.quickSort(); for (long i = 1; i < a.size(); i++) - assertTrue((a.get(i) && !a.get(i - 1)) || - (a.get(i) == a.get(i - 1))); + assertTrue((a.get(i) && !a.get(i - 1)) || (a.get(i) == a.get(i - 1))); } @Test @@ -155,19 +152,18 @@ void sort() a.sort(); for (long i = 1; i < a.size(); i++) - assertTrue((a.get(i) && !a.get(i - 1)) || - (a.get(i) == a.get(i - 1))); + assertTrue((a.get(i) && !a.get(i - 1)) || (a.get(i) == a.get(i - 1))); } @Test void updateFrom() { Random r = new Random(0); - LargeBooleanArray a2 = new LargeBooleanArray(a.size()/2); + LargeBooleanArray a2 = new LargeBooleanArray(a.size() / 2); for (long i = 0; i < a2.size(); i++) a2.set(i, r.nextBoolean()); - long insertionPoint = a.size()/4; + long insertionPoint = a.size() / 4; a.updateFrom(a2, 0, a2.size(), insertionPoint); for (long i = 0; i < a2.size(); i++) @@ -181,8 +177,8 @@ void copyOfRange() for (long i = 0; i < a.size(); i++) a.set(i, r.nextBoolean()); - long from = (long)(0.25*a.size()); - long to = (long)(0.75*a.size()); + long from = (long)(0.25 * a.size()); + long to = (long)(0.75 * a.size()); LargeBooleanArray a2 = a.copyOfRange(from, to); for (long i = 0; i < a2.size(); i++) @@ -196,8 +192,8 @@ void toArray() for (long i = 0; i < a.size(); i++) a.set(i, r.nextBoolean()); - long from = (long)(0.2*a.size()); - long to = (long)(0.4*a.size()); + long from = (long)(0.2 * a.size()); + long to = (long)(0.4 * a.size()); boolean[] a2 = a.toArray(from, to); for (int i = 0; i < a2.length; i++) diff --git a/src/test/java/nl/cwts/util/LargeDoubleArrayTest.java b/src/test/java/nl/cwts/util/LargeDoubleArrayTest.java index daf04db..2085c3e 100644 --- a/src/test/java/nl/cwts/util/LargeDoubleArrayTest.java +++ b/src/test/java/nl/cwts/util/LargeDoubleArrayTest.java @@ -1,12 +1,13 @@ package nl.cwts.util; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + import java.util.Random; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; - class LargeDoubleArrayTest { LargeDoubleArray a; @@ -78,8 +79,7 @@ void ensureCapacity() { long c = a.capacity(); a.ensureCapacity(a.capacity() + 1); - assertEquals(a.capacity(), - c * (1 + LargeDoubleArray.RELATIVE_CAPACITY_INCREASE)); + assertEquals(a.capacity(), c * (1 + LargeDoubleArray.RELATIVE_CAPACITY_INCREASE)); } @Test @@ -138,7 +138,7 @@ void divide() { a.add(i, 1); a.divide(i, i); - assertEquals(a.get(i), 1.0/i); + assertEquals(a.get(i), 1.0 / i); } } @@ -169,7 +169,7 @@ void calcMinimum() @Test void swap() { - a.set(0,42); + a.set(0, 42); a.set(1, 59); a.swap(0, 1); assertEquals(a.get(0), 59); @@ -251,7 +251,7 @@ void binarySearch() // Search all other items for (long i = 1; i < n - 1; i++) { - x = (a.get(i)/2 + a.get(i - 1)/2); + x = (a.get(i) / 2 + a.get(i - 1) / 2); j = a.binarySearch(x); if (j < 0) @@ -284,7 +284,6 @@ void binarySearch() } } - @Test void iterator() { @@ -296,7 +295,7 @@ void iterator() @Test void from() { - long i = a.size()/2; + long i = a.size() / 2; for (double x : a.from(i)) assertEquals(x, a.get(i++)); } @@ -304,8 +303,8 @@ void from() @Test void fromTo() { - long i = a.size()/3; - for (double x : a.fromTo(i, i + a.size()/3)) + long i = a.size() / 3; + for (double x : a.fromTo(i, i + a.size() / 3)) assertEquals(x, a.get(i++)); } @@ -313,11 +312,11 @@ void fromTo() void updateFrom() { Random r = new Random(0); - LargeDoubleArray a2 = new LargeDoubleArray(a.size()/2); + LargeDoubleArray a2 = new LargeDoubleArray(a.size() / 2); for (long i = 0; i < a2.size(); i++) a2.set(i, r.nextDouble()); - long insertionPoint = a.size()/4; + long insertionPoint = a.size() / 4; a.updateFrom(a2, 0, a2.size(), insertionPoint); for (long i = 0; i < a2.size(); i++) @@ -331,8 +330,8 @@ void copyOfRange() for (long i = 0; i < a.size(); i++) a.set(i, r.nextDouble()); - long from = (long)(0.25*a.size()); - long to = (long)(0.75*a.size()); + long from = (long)(0.25 * a.size()); + long to = (long)(0.75 * a.size()); LargeDoubleArray a2 = a.copyOfRange(from, to); for (long i = 0; i < a2.size(); i++) @@ -346,8 +345,8 @@ void toArray() for (long i = 0; i < a.size(); i++) a.set(i, r.nextDouble()); - long from = (long)(0.2*a.size()); - long to = (long)(0.4*a.size()); + long from = (long)(0.2 * a.size()); + long to = (long)(0.4 * a.size()); double[] a2 = a.toArray(from, to); for (int i = 0; i < a2.length; i++) diff --git a/src/test/java/nl/cwts/util/LargeIntArrayTest.java b/src/test/java/nl/cwts/util/LargeIntArrayTest.java index 0166ea8..e966e7d 100644 --- a/src/test/java/nl/cwts/util/LargeIntArrayTest.java +++ b/src/test/java/nl/cwts/util/LargeIntArrayTest.java @@ -1,12 +1,12 @@ package nl.cwts.util; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Random; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; class LargeIntArrayTest { @@ -79,8 +79,7 @@ void ensureCapacity() { long c = a.capacity(); a.ensureCapacity(a.capacity() + 1); - assertEquals(a.capacity(), - c * (1 + LargeIntArray.RELATIVE_CAPACITY_INCREASE)); + assertEquals(a.capacity(), c * (1 + LargeIntArray.RELATIVE_CAPACITY_INCREASE)); } @Test @@ -170,7 +169,7 @@ void calcMinimum() @Test void swap() { - a.set(0,42); + a.set(0, 42); a.set(1, 59); a.swap(0, 1); assertEquals(a.get(0), 59); @@ -252,7 +251,7 @@ void binarySearch() // Search all other items for (long i = 1; i < n - 1; i++) { - x = (int)(a.get(i)/2.0 + a.get(i - 1)/2.0); + x = (int)(a.get(i) / 2.0 + a.get(i - 1) / 2.0); j = a.binarySearch(x); if (j < 0) @@ -285,7 +284,6 @@ void binarySearch() } } - @Test void iterator() { @@ -297,7 +295,7 @@ void iterator() @Test void from() { - long i = a.size()/2; + long i = a.size() / 2; for (int x : a.from(i)) assertEquals(x, a.get(i++)); } @@ -305,8 +303,8 @@ void from() @Test void fromTo() { - long i = a.size()/3; - for (int x : a.fromTo(i, i + a.size()/3)) + long i = a.size() / 3; + for (int x : a.fromTo(i, i + a.size() / 3)) assertEquals(x, a.get(i++)); } @@ -314,11 +312,11 @@ void fromTo() void updateFrom() { Random r = new Random(0); - LargeIntArray a2 = new LargeIntArray(a.size()/2); + LargeIntArray a2 = new LargeIntArray(a.size() / 2); for (long i = 0; i < a2.size(); i++) a2.set(i, r.nextInt()); - long insertionPoint = a.size()/4; + long insertionPoint = a.size() / 4; a.updateFrom(a2, 0, a2.size(), insertionPoint); for (long i = 0; i < a2.size(); i++) @@ -332,8 +330,8 @@ void copyOfRange() for (long i = 0; i < a.size(); i++) a.set(i, r.nextInt()); - long from = (long)(0.25*a.size()); - long to = (long)(0.75*a.size()); + long from = (long)(0.25 * a.size()); + long to = (long)(0.75 * a.size()); LargeIntArray a2 = a.copyOfRange(from, to); for (long i = 0; i < a2.size(); i++) @@ -347,8 +345,8 @@ void toArray() for (long i = 0; i < a.size(); i++) a.set(i, r.nextInt()); - long from = (long)(0.2*a.size()); - long to = (long)(0.4*a.size()); + long from = (long)(0.2 * a.size()); + long to = (long)(0.4 * a.size()); int[] a2 = a.toArray(from, to); for (int i = 0; i < a2.length; i++) diff --git a/src/test/java/nl/cwts/util/LargeLongArrayTest.java b/src/test/java/nl/cwts/util/LargeLongArrayTest.java index 449df39..9cc9e9e 100644 --- a/src/test/java/nl/cwts/util/LargeLongArrayTest.java +++ b/src/test/java/nl/cwts/util/LargeLongArrayTest.java @@ -1,12 +1,12 @@ package nl.cwts.util; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Random; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; class LargeLongArrayTest { @@ -79,8 +79,7 @@ void ensureCapacity() { long c = a.capacity(); a.ensureCapacity(a.capacity() + 1); - assertEquals(a.capacity(), - c * (1 + LargeLongArray.RELATIVE_CAPACITY_INCREASE)); + assertEquals(a.capacity(), c * (1 + LargeLongArray.RELATIVE_CAPACITY_INCREASE)); } @Test @@ -170,7 +169,7 @@ void calcMinimum() @Test void swap() { - a.set(0,42); + a.set(0, 42); a.set(1, 59); a.swap(0, 1); assertEquals(a.get(0), 59); @@ -252,7 +251,7 @@ void binarySearch() // Search all other items for (long i = 1; i < n - 1; i++) { - x = (long)(a.get(i)/2.0 + a.get(i - 1)/2.0); + x = (long)(a.get(i) / 2.0 + a.get(i - 1) / 2.0); j = a.binarySearch(x); if (j < 0) @@ -285,7 +284,6 @@ void binarySearch() } } - @Test void iterator() { @@ -297,7 +295,7 @@ void iterator() @Test void from() { - long i = a.size()/2; + long i = a.size() / 2; for (long x : a.from(i)) assertEquals(x, a.get(i++)); } @@ -305,8 +303,8 @@ void from() @Test void fromTo() { - long i = a.size()/3; - for (long x : a.fromTo(i, i + a.size()/3)) + long i = a.size() / 3; + for (long x : a.fromTo(i, i + a.size() / 3)) assertEquals(x, a.get(i++)); } @@ -314,11 +312,11 @@ void fromTo() void updateFrom() { Random r = new Random(0); - LargeLongArray a2 = new LargeLongArray(a.size()/2); + LargeLongArray a2 = new LargeLongArray(a.size() / 2); for (long i = 0; i < a2.size(); i++) a2.set(i, r.nextLong()); - long insertionPoint = a.size()/4; + long insertionPoint = a.size() / 4; a.updateFrom(a2, 0, a2.size(), insertionPoint); for (long i = 0; i < a2.size(); i++) @@ -332,8 +330,8 @@ void copyOfRange() for (long i = 0; i < a.size(); i++) a.set(i, r.nextLong()); - long from = (long)(0.25*a.size()); - long to = (long)(0.75*a.size()); + long from = (long)(0.25 * a.size()); + long to = (long)(0.75 * a.size()); LargeLongArray a2 = a.copyOfRange(from, to); for (long i = 0; i < a2.size(); i++) @@ -347,8 +345,8 @@ void toArray() for (long i = 0; i < a.size(); i++) a.set(i, r.nextLong()); - long from = (long)(0.2*a.size()); - long to = (long)(0.4*a.size()); + long from = (long)(0.2 * a.size()); + long to = (long)(0.4 * a.size()); long[] a2 = a.toArray(from, to); for (int i = 0; i < a2.length; i++)