Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Bulk formatting commits; hide them from `git blame` (GitHub honours this file).
# Locally: git config blame.ignoreRevsFile .git-blame-ignore-revs

# Reformat all Java sources with palantir-java-format
f490a4f2627cc093ad2cfa4ba93809556254e7de
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ A small Java library of algorithms and data structures, plus worked problems org
Requires Java 25. Maven comes with the wrapper.

```sh
./mvnw verify # compile, run all tests, write coverage to target/site/jacoco
./mvnw verify # format check, compile, run all tests, write coverage to target/site/jacoco
./mvnw spotless:apply # format the sources (palantir-java-format)
./mvnw install # make io.github.diegourban:algorithms available to other local projects
```

Expand Down
22 changes: 22 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
<maven-resources-plugin.version>3.5.0</maven-resources-plugin.version>
<maven-surefire-plugin.version>3.6.1</maven-surefire-plugin.version>
<jacoco-maven-plugin.version>0.8.16</jacoco-maven-plugin.version>
<spotless-maven-plugin.version>3.11.0</spotless-maven-plugin.version>
<palantir-java-format.version>2.103.0</palantir-java-format.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -93,6 +95,26 @@
</pluginManagement>

<plugins>
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>${spotless-maven-plugin.version}</version>
<configuration>
<java>
<palantirJavaFormat>
<version>${palantir-java-format.version}</version>
</palantirJavaFormat>
<removeUnusedImports/>
</java>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<version>${maven-enforcer-plugin.version}</version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public String getName() {

@Override
public boolean equals(Object obj) {
if(obj == null) {
if (obj == null) {
return false;
}
Element other = (Element) obj;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

public class ArrayUtils {

private ArrayUtils() {
}
private ArrayUtils() {}

private static Random rand = new Random();

Expand All @@ -27,8 +26,7 @@ public static void shuffle(int[] theArray) {
}

// walk through from beginning to end
for (int indexWeAreChoosingFor = 0;
indexWeAreChoosingFor < theArray.length - 1; indexWeAreChoosingFor++) {
for (int indexWeAreChoosingFor = 0; indexWeAreChoosingFor < theArray.length - 1; indexWeAreChoosingFor++) {

// choose a random not-yet-placed item to place there
// (could also be the item currently in that spot)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class LinkedList {
private int totalElements = 0;

public void addAtStart(final Element element) {
if(this.totalElements == 0) {
if (this.totalElements == 0) {
LinkedListNode newNode = new LinkedListNode(element);
this.first = newNode;
this.last = newNode;
Expand All @@ -23,7 +23,7 @@ public void addAtStart(final Element element) {
}

public void addAtEnd(final Element element) {
if(this.totalElements == 0) {
if (this.totalElements == 0) {
addAtStart(element);
} else {
LinkedListNode newNode = new LinkedListNode(element);
Expand All @@ -36,9 +36,9 @@ public void addAtEnd(final Element element) {
}

public void add(final int position, final Element element) {
if(this.totalElements == 0) {
if (this.totalElements == 0) {
addAtStart(element);
} else if(position == this.totalElements) {
} else if (position == this.totalElements) {
addAtEnd(element);
} else {
LinkedListNode previous = this.getNode(position - 1);
Expand All @@ -58,11 +58,11 @@ private boolean positionOccupied(final int position) {
}

private LinkedListNode getNode(final int position) {
if(!positionOccupied(position)) {
if (!positionOccupied(position)) {
throw new IllegalArgumentException("Posição inexistente");
}
LinkedListNode current = this.first;
for(int i = 0; i < position; i++) {
for (int i = 0; i < position; i++) {
current = current.getNext();
}
return current;
Expand All @@ -77,20 +77,20 @@ public Element get(final int position) {
}

public void removeFromStart() {
if(this.totalElements == 0) {
if (this.totalElements == 0) {
throw new IllegalStateException("Lista vazia");
}

this.first = this.first.getNext();
this.totalElements--;

if(this.totalElements == 0) {
if (this.totalElements == 0) {
this.last = null;
}
}

public void removeFromEnd() {
if(this.totalElements == 1) {
if (this.totalElements == 1) {
this.removeFromStart();
} else {
LinkedListNode previous = this.last.getPrevious();
Expand All @@ -101,9 +101,9 @@ public void removeFromEnd() {
}

public void remove(final int position) {
if(position == 0) {
if (position == 0) {
this.removeFromStart();
} else if(position == totalElements - 1) {
} else if (position == totalElements - 1) {
this.removeFromEnd();
} else {
LinkedListNode previous = this.getNode(position - 1);
Expand All @@ -122,8 +122,8 @@ public int size() {

public boolean contains(Element element) {
LinkedListNode current = this.first;
while(current != null) {
if(current.getElement().equals(element)) {
while (current != null) {
if (current.getElement().equals(element)) {
return true;
}
current = current.getNext();
Expand All @@ -133,13 +133,13 @@ public boolean contains(Element element) {

@Override
public String toString() {
if(this.totalElements == 0) {
if (this.totalElements == 0) {
return "[]";
}
LinkedListNode current = this.first;

StringBuilder sb = new StringBuilder("[");
for(int i = 0; i < this.totalElements; i++) {
for (int i = 0; i < this.totalElements; i++) {
sb.append(current.getElement().getName());
sb.append(",");
current = current.getNext();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.diegourban.algorithms.datastructure.queue;

import io.github.diegourban.algorithms.datastructure.Element;

import java.util.LinkedList;

public class Queue {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ public class Set {
private List<LinkedList<String>> table = new LinkedList<LinkedList<String>>();

public Set() {
for(int i = 0; i < 26; i++) {
for (int i = 0; i < 26; i++) {
this.table.add(new LinkedList<String>());
}
}

public void add(String word) {
if(!contains(word)) {
if (!contains(word)) {
int index = calculateTableIndex(word);
List<String> list = this.table.get(index);
list.add(word);
}
}

public void remove(String word) {
if(contains(word)) {
if (contains(word)) {
int index = calculateTableIndex(word);
List<String> list = this.table.get(index);
list.remove(word);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.diegourban.algorithms.datastructure.stack;

import io.github.diegourban.algorithms.datastructure.Element;

import java.util.LinkedList;
import java.util.List;

Expand All @@ -14,7 +13,7 @@ public void push(Element element) {
}

public Element pop() {
return this.elements.remove(this.elements.size() - 1 );
return this.elements.remove(this.elements.size() - 1);
}

public boolean empty() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.diegourban.algorithms.datastructure.vector;

import io.github.diegourban.algorithms.datastructure.Element;

import java.util.Arrays;

public class Vector {
Expand All @@ -17,13 +16,13 @@ public void add(final Element element) {
}

public void add(final int position, final Element element) {
if(!isPositionValid(position)) {
if (!isPositionValid(position)) {
throw new IllegalArgumentException("Invalid position");
}

this.guaranteesSpace();
for(int i = this.totalElements - 1; i >= position; i--) {
this.elements[i+1] = this.elements[i];
for (int i = this.totalElements - 1; i >= position; i--) {
this.elements[i + 1] = this.elements[i];
}
this.elements[position] = element;
this.totalElements++;
Expand All @@ -34,7 +33,7 @@ private boolean isPositionValid(final int position) {
}

public Element get(final int position) {
if(!positionOccupied(position)) {
if (!positionOccupied(position)) {
throw new IllegalArgumentException("Invalid position");
}
return this.elements[position];
Expand All @@ -45,16 +44,16 @@ private boolean positionOccupied(final int position) {
}

public void remove(final int position) {
for(int i = position; i < this.totalElements - 1; i++) {
this.elements[i] = this.elements[i+1];
for (int i = position; i < this.totalElements - 1; i++) {
this.elements[i] = this.elements[i + 1];
}
totalElements--;
this.elements[this.totalElements] = null;
}

public boolean contains(final Element element) {
for(int i = 0; i < this.totalElements; i++) {
if(element.equals(this.elements[i])) {
for (int i = 0; i < this.totalElements; i++) {
if (element.equals(this.elements[i])) {
return true;
}
}
Expand All @@ -67,9 +66,9 @@ public int size() {
}

private void guaranteesSpace() {
if(this.totalElements == this.elements.length) {
if (this.totalElements == this.elements.length) {
Element[] newElements = new Element[this.elements.length * 2];
for(int i = 0; i < this.elements.length; i++) {
for (int i = 0; i < this.elements.length; i++) {
newElements[i] = this.elements[i];
}
this.elements = newElements;
Expand All @@ -78,8 +77,6 @@ private void guaranteesSpace() {

@Override
public String toString() {
return "Vector{" +
"elements=" + Arrays.toString(elements) +
'}';
return "Vector{" + "elements=" + Arrays.toString(elements) + '}';
}
}
3 changes: 1 addition & 2 deletions src/main/java/io/github/diegourban/algorithms/io/Reader.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private void fillBuffer() throws IOException {
}

private byte read() throws IOException {
if(isEOF()) {
if (isEOF()) {
throw new EOFException();
}
if (bufferPointer == bytesRead) {
Expand All @@ -166,5 +166,4 @@ public void close() throws IOException {
public boolean isClosed() {
return din == null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

public class Combinatorics {

private Combinatorics() {
}
private Combinatorics() {}

/**
* Pascal's Triangle
Expand Down Expand Up @@ -47,5 +46,4 @@ public static BigInteger binomialCoefficient(int n, int m) {

return binomials[m];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

public class Geometry {

private Geometry() {
}
private Geometry() {}

private static final double SIMPLE_PI = 3.14;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

public class Measurement {

private Measurement() {
}
private Measurement() {}

/**
* Converts miles to kilometers
Expand All @@ -24,5 +23,4 @@ public static double milesToKilometers(double miles) {
public static double kilometersToMiles(double kilometers) {
return kilometers * 0.621371;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
*/
public class NumberTheory {

private NumberTheory() {
}
private NumberTheory() {}

/**
* Least common multiple (lcm) of two numbers.
Expand All @@ -29,8 +28,7 @@ public static long lcm(long a, long b) {
*/
public static long lcm(long[] input) {
long result = input[0];
for (int i = 1; i < input.length; i++)
result = lcm(result, input[i]);
for (int i = 1; i < input.length; i++) result = lcm(result, input[i]);
return result;
}

Expand Down Expand Up @@ -139,5 +137,4 @@ private static void fillPrimeFactors(int number, Collection<Integer> factors) {
factors.add(n);
}
}

}
Loading
Loading