Skip to content

Commit

Permalink
Change Indices implementation to treeset
Browse files Browse the repository at this point in the history
  • Loading branch information
butteredyakiimo committed Oct 14, 2023
1 parent 6c630d5 commit 213a812
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 26 deletions.
9 changes: 7 additions & 2 deletions src/main/java/seedu/address/commons/core/index/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

/**
* Represents a zero-based or one-based index.
*
* <p>
* {@code Index} should be used right from the start (when parsing in a new user input), so that if the current
* component wants to communicate with another component, it can send an {@code Index} to avoid having to know what
* base the other component is using for its index. However, after receiving the {@code Index}, that component can
* convert it back to an int if the index will not be passed to a different component again.
*/
public class Index {
public class Index implements Comparable<Index> {
private int zeroBasedIndex;

/**
Expand Down Expand Up @@ -47,6 +47,11 @@ public static Index fromOneBased(int oneBasedIndex) {
return new Index(oneBasedIndex - 1);
}

@Override
public int compareTo(Index other) {
return this.zeroBasedIndex - other.zeroBasedIndex;
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
47 changes: 23 additions & 24 deletions src/main/java/seedu/address/commons/core/index/Indices.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package seedu.address.commons.core.index;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;

import seedu.address.commons.util.ToStringBuilder;

Expand All @@ -13,14 +16,14 @@
* one index provided.
*/
public class Indices {
private final ArrayList<Index> zeroBasedIndices;
private final SortedSet<Index> zeroBasedIndices;
private int size;

/**
* Indices can only be created by calling {@link Indices#fromZeroBased} or
* {@link Indices#fromOneBased(int[])}.
*/
private Indices(ArrayList<Index> zeroBasedIndices) {
private Indices(SortedSet<Index> zeroBasedIndices) {
this.zeroBasedIndices = zeroBasedIndices;
this.size = zeroBasedIndices.size();
}
Expand All @@ -30,13 +33,11 @@ private Indices(ArrayList<Index> zeroBasedIndices) {
*/
public static Indices fromZeroBased(int[] zeroBasedIndices) {
Arrays.sort(zeroBasedIndices);
ArrayList<Index> result = new ArrayList<>();
SortedSet<Index> result = new TreeSet<>();

for (int index : zeroBasedIndices) {
Index zeroBasedIndex = Index.fromZeroBased(index);
if (!result.contains(zeroBasedIndex)) {
result.add(zeroBasedIndex);
}
result.add(zeroBasedIndex);
}
return new Indices(result);
}
Expand All @@ -46,29 +47,33 @@ public static Indices fromZeroBased(int[] zeroBasedIndices) {
*/
public static Indices fromOneBased(int[] oneBasedIndices) {
Arrays.sort(oneBasedIndices);
ArrayList<Index> result = new ArrayList<>();
SortedSet<Index> result = new TreeSet<>();

for (int index : oneBasedIndices) {
Index oneBasedIndex = Index.fromOneBased(index);
if (!result.contains(oneBasedIndex)) {
result.add(oneBasedIndex);
}
result.add(oneBasedIndex);
}
return new Indices(result);
}

public int[] getZeroBased() {
int[] result = new int[size];
for (int i = 0; i < size; i++) {
result[i] = this.zeroBasedIndices.get(i).getZeroBased();
Iterator<Index> iterator = this.zeroBasedIndices.iterator();
int counter = 0;
while (iterator.hasNext()) {
result[counter] = iterator.next().getZeroBased();
counter++;
}
return result;
}

public int[] getOneBased() {
int[] result = new int[size];
for (int i = 0; i < size; i++) {
result[i] = this.zeroBasedIndices.get(i).getOneBased();
Iterator<Index> iterator = this.zeroBasedIndices.iterator();
int counter = 0;
while (iterator.hasNext()) {
result[counter] = iterator.next().getOneBased();
counter++;
}
return result;
}
Expand Down Expand Up @@ -111,14 +116,14 @@ public int getSize() {
* Returns the smallest zero-based index in indices.
*/
public int getZeroBasedMin() {
return this.zeroBasedIndices.get(0).getZeroBased();
return Collections.min(this.zeroBasedIndices).getZeroBased();
}

/**
* Returns the largest zero-based index in indices.
*/
public int getZeroBasedMax() {
return this.zeroBasedIndices.get(size - 1).getZeroBased();
return Collections.max(this.zeroBasedIndices).getZeroBased();
}

@Override
Expand All @@ -137,13 +142,7 @@ public boolean equals(Object other) {
if (this.size != otherIndices.size) {
return false;

Check warning on line 143 in src/main/java/seedu/address/commons/core/index/Indices.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/commons/core/index/Indices.java#L143

Added line #L143 was not covered by tests
}

for (int i = 0; i < this.size; i++) {
if (!this.zeroBasedIndices.get(i).equals(otherIndices.zeroBasedIndices.get(i))) {
return false;
}
}
return true;
return this.zeroBasedIndices.equals(otherIndices.zeroBasedIndices);
}

@Override
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/seedu/address/commons/core/index/IndexTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ public void createZeroBasedIndex() {
assertEquals(6, Index.fromZeroBased(5).getOneBased());
}

@Test
public void compareTo() {
//equal
assertEquals(0, Index.fromOneBased(1).compareTo(Index.fromOneBased(1)));
assertEquals(0, Index.fromZeroBased(0).compareTo(Index.fromOneBased(1)));

//less than
assertEquals(true, Index.fromOneBased(1)
.compareTo(Index.fromOneBased(2)) < 0);
assertEquals(true, Index.fromZeroBased(0)
.compareTo(Index.fromOneBased(2)) < 0);

//more than
assertEquals(true, Index.fromOneBased(2)
.compareTo(Index.fromOneBased(1)) > 0);
assertEquals(true, Index.fromZeroBased(1)
.compareTo(Index.fromOneBased(1)) > 0);
}

@Test
public void equals() {
final Index fifthPersonIndex = Index.fromOneBased(5);
Expand Down

0 comments on commit 213a812

Please sign in to comment.