From 23341f1499925bfb78d70e5ce6e8d672f9826889 Mon Sep 17 00:00:00 2001 From: Holt59 Date: Sat, 10 Mar 2018 20:45:55 +0100 Subject: [PATCH] Clean code. --- .../insa/algo/datastructures/BinaryHeap.java | 48 +++++++++++-------- .../WeaklyConnectedComponentsAlgorithm.java | 4 +- .../WeaklyConnectedComponentsData.java | 3 +- 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/main/org/insa/algo/datastructures/BinaryHeap.java b/src/main/org/insa/algo/datastructures/BinaryHeap.java index 55c1a5b..79034f5 100644 --- a/src/main/org/insa/algo/datastructures/BinaryHeap.java +++ b/src/main/org/insa/algo/datastructures/BinaryHeap.java @@ -20,27 +20,37 @@ import java.util.ArrayList; */ public class BinaryHeap> { - private int currentSize; // Number of elements in heap + // Number of elements in heap. + private int currentSize; - // Java genericity does not work with arrays. - // We have to use an ArrayList - private ArrayList array; // The heap array + // The heap array. Java genericity does not work with arrays so we have to use + // an ArrayList. + private ArrayList array; /** - * Construct the binary heap. + * Construct a new empty binary heap. */ public BinaryHeap() { this.currentSize = 0; this.array = new ArrayList(); } - // Constructor used for debug. + /** + * Construct a copy of the given heap. + * + * @param heap Binary heap to copy. + */ public BinaryHeap(BinaryHeap heap) { this.currentSize = heap.currentSize; this.array = new ArrayList(heap.array); } - // Sets an element in the array + /** + * Set an element at the given index. + * + * @param index Index at which the element should be set. + * @param value Element to set. + */ private void arraySet(int index, E value) { if (index == this.array.size()) { this.array.add(value); @@ -51,41 +61,37 @@ public class BinaryHeap> { } /** - * Test if the heap is logically empty. - * - * @return true if empty, false otherwise. + * @return true if the heap is empty, false otherwise. */ public boolean isEmpty() { return this.currentSize == 0; } /** - * Returns size. - * - * @return current size. + * @return Current size (number of elements) of this heap. */ public int size() { return this.currentSize; } /** - * Returns index of parent. + * @return Index of the parent of the given index. */ private int index_parent(int index) { return (index - 1) / 2; } /** - * Returns index of left child. + * @return Index of the left child of the given index. */ private int index_left(int index) { return index * 2 + 1; } /** - * Insert into the heap. + * Insert the given element into the heap. * - * @param x the item to insert. + * @param x Item to insert. */ public void insert(E x) { int index = this.currentSize++; @@ -96,7 +102,7 @@ public class BinaryHeap> { /** * Internal method to percolate up in the heap. * - * @param index the index at which the percolate begins. + * @param index Index at which the percolate begins. */ private void percolateUp(int index) { E x = this.array.get(index); @@ -114,7 +120,7 @@ public class BinaryHeap> { /** * Internal method to percolate down in the heap. * - * @param index the index at which the percolate begins. + * @param index Index at which the percolate begins. */ private void percolateDown(int index) { int ileft = index_left(index); @@ -148,7 +154,7 @@ public class BinaryHeap> { /** * Find the smallest item in the heap. * - * @return the smallest item in the heap. + * @return The smallest item in the heap. * * @throws RuntimeException if this heap is empty. */ @@ -161,7 +167,7 @@ public class BinaryHeap> { /** * Remove the smallest item from the heap. * - * @return the smallest item in the heap. + * @return The smallest item in the heap. * * @throws RuntimeException if this heap is empty. */ diff --git a/src/main/org/insa/algo/weakconnectivity/WeaklyConnectedComponentsAlgorithm.java b/src/main/org/insa/algo/weakconnectivity/WeaklyConnectedComponentsAlgorithm.java index 932b928..436c8f8 100644 --- a/src/main/org/insa/algo/weakconnectivity/WeaklyConnectedComponentsAlgorithm.java +++ b/src/main/org/insa/algo/weakconnectivity/WeaklyConnectedComponentsAlgorithm.java @@ -17,7 +17,7 @@ public class WeaklyConnectedComponentsAlgorithm extends AbstractAlgorithm { /** - * @param data + * @param data Input data for this algorithm. */ public WeaklyConnectedComponentsAlgorithm(WeaklyConnectedComponentsData data) { super(data); @@ -69,7 +69,7 @@ public class WeaklyConnectedComponentsAlgorithm /** * @return An adjacency list for the undirected graph equivalent to the stored - * graph. + * graph. */ protected ArrayList> createUndirectedGraph() { int nNodes = getInputData().getGraph().getNodes().size(); diff --git a/src/main/org/insa/algo/weakconnectivity/WeaklyConnectedComponentsData.java b/src/main/org/insa/algo/weakconnectivity/WeaklyConnectedComponentsData.java index bf54c92..2faffa9 100644 --- a/src/main/org/insa/algo/weakconnectivity/WeaklyConnectedComponentsData.java +++ b/src/main/org/insa/algo/weakconnectivity/WeaklyConnectedComponentsData.java @@ -6,8 +6,7 @@ import org.insa.graph.Graph; public class WeaklyConnectedComponentsData extends AbstractInputData { /** - * - * @param graph + * @param graph Graph for which components should be retrieved. */ public WeaklyConnectedComponentsData(Graph graph) { super(graph);