Skip to content

Commit

Permalink
Cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
neesjanvaneck committed Aug 19, 2023
1 parent f83fe56 commit eeb42ac
Show file tree
Hide file tree
Showing 11 changed files with 228 additions and 273 deletions.
10 changes: 2 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
33 changes: 23 additions & 10 deletions src/main/java/nl/cwts/networkanalysis/Network.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.
*
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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)
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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>
{
long from;
Expand All @@ -1554,6 +1564,9 @@ public java.util.Iterator<Long> iterator()
}
}

/**
* Iterator capable of iterating over a specified range.
*/
public class RangeIterator implements PrimitiveIterator.OfLong
{
private final long to;
Expand Down
55 changes: 26 additions & 29 deletions src/main/java/nl/cwts/util/LargeBooleanArray.java
Original file line number Diff line number Diff line change
@@ -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;

/**
* <p>
* This class enables arrays of booleans up to 64-bits in size (for the exact
Expand All @@ -28,15 +27,18 @@
* (indicated by {@link #MINIMUM_INITIAL_CAPACITY}). This dynamic array then
* also supports {@link #push} and {@link #pop} operations.
* </p>
*
* @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;
/**
Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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);
}

/***************************************************************************
Expand Down Expand Up @@ -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++)
{
Expand All @@ -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++;

Expand All @@ -431,6 +430,7 @@ public void ensureCapacity(long minCapacity)

/**
* Resizes array.
*
* @param size New size
*/
public void resize(long size)
Expand Down Expand Up @@ -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;
Expand All @@ -484,6 +483,7 @@ public void shrink()

/**
* Gets size of array.
*
* <p>This is always less than or equal to the capacity.</p>
*
* @return Size
Expand All @@ -495,6 +495,7 @@ public long size()

/**
* Gets capacity of array.
*
* <p>This is always greater than or equal to the size.</p>
*
* @return Capacity
Expand Down Expand Up @@ -555,19 +556,15 @@ 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);
}

/**
* Sorts elements in ascending order using quick sort.
*/
public void quickSort()
{
BigArrays.quickSort(0, capacity,
this::compare,
this::swap);
BigArrays.quickSort(0, capacity, this::compare, this::swap);
}

/**
Expand Down Expand Up @@ -600,10 +597,12 @@ public void updateFrom(LargeBooleanArray array)

/**
* Updates this array from the provided array.
*
* <p>
* Values from other array starting at {@code from} until
* {@code to} (exclusive) will be copied to this array, starting
* from the {@code insertionPoint} onwards.
* </p>
*
* @param array Array to update from
* @param from Index in {@code array} from where to update,
Expand Down Expand Up @@ -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++;
Expand All @@ -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)
Expand Down Expand Up @@ -771,6 +770,4 @@ public LargeBooleanArray clone()
return null;
}
}

}

Loading

0 comments on commit eeb42ac

Please sign in to comment.