Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #167. Refactor: centralized type handling for simple operations such as add and subtract, and added branch coverage for this #173

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
69ffed7
Added Operations file to put or refactors in.
Mar 1, 2022
670921b
Fixed #95. test: Added branch coverage in function Matrix.add, for di…
akerfel Mar 2, 2022
b656c94
Fixed #97. test: Added branch coverage tests for all TODO-functions i…
reversedpeach Mar 2, 2022
002fa0e
Fixed #103. test: Added branch coverage for Matrix.subtract, for diff…
akerfel Mar 3, 2022
0b4c224
Fixed #105. test: Added branch coverage for Matrix.multiply, for diff…
akerfel Mar 3, 2022
d618408
Fixed #108. Refactor: Removedunneeded imports from MatrixTests.java
akerfel Mar 3, 2022
a2d7124
Fixed #109. test: Added branch coverage for Matrix.multiply, for Doub…
akerfel Mar 3, 2022
6d0a6e6
Fixed #100. feat: Added function rowMultiplication for different Numb…
reversedpeach Mar 3, 2022
561102d
Merge pull request #111 from DD2480-group11/lab4refactoring
akerfel Mar 3, 2022
4de254a
Fixed #99. feat: Made function which subtracts different Number types…
akerfel Mar 4, 2022
271b379
Fixed #112. feat: Added function which subtracts different Number typ…
akerfel Mar 4, 2022
90d04ea
Fixed #115. fix: Removed unneded for loops in Matrix.add and Matrix.s…
akerfel Mar 4, 2022
a9baefd
Fixed issue 101 (#117)
Snebie Mar 7, 2022
6301210
Fixed Issue 98 (#116)
Snebie Mar 7, 2022
e97d3b7
Issue 118 (#119)
Snebie Mar 7, 2022
e44ceee
Fixed merge conflict bug, added missing brackets
akerfel Mar 7, 2022
9a9a042
Reverted change which removed unneeded for-loops.
akerfel Mar 8, 2022
d09a9b8
Revert "Issue 118 (#119)"
akerfel Mar 8, 2022
c586d45
Fixed #122. Added test coverage for Matrix.compare.
akerfel Mar 8, 2022
f0ac979
Fixed #123. Removed unnecessary whitespace.
akerfel Mar 8, 2022
e2ee75d
Removed whitespaces changes to .gitignore
akerfel Mar 8, 2022
8780d10
Second try: Removed whitespaces changes to .gitignore
akerfel Mar 8, 2022
8de8102
Added back newline at end of .gitignore
akerfel Mar 8, 2022
37dc12c
Removed duplicate import and unneeded whitespace
akerfel Mar 8, 2022
24679e0
Fixed #120. feat: Added documentation to Operations.java (#124)
reversedpeach Mar 8, 2022
f9dc749
Removed 'This is ugly' comments, since we refactored those functions
akerfel Mar 9, 2022
b66c34a
Merge branch 'lab4patch' of github.com:DD2480-group11/java-algorithms…
akerfel Mar 9, 2022
72c4c9e
Removed unnecessary whitespace.
akerfel Mar 9, 2022
fa45cbf
Removed unnecessary whitespace.
akerfel Mar 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 4 additions & 42 deletions src/com/jwetherell/algorithms/data_structures/FenwickTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.ArrayList;
import java.util.List;

import com.jwetherell.algorithms.mathematics.Operations;

/**
* A Fenwick tree or binary indexed tree is a data structure providing efficient methods
* for calculation and manipulation of the prefix sums of a table of values. Fenwick trees
Expand Down Expand Up @@ -303,27 +305,7 @@ else if (this.sum != null && data.sum == null)
else if (this.sum == null && data.sum != null)
this.sum = data.sum;
else {
/* TODO: This is ugly and how to handle number overflow? */
if (this.sum instanceof BigDecimal || data.sum instanceof BigDecimal) {
BigDecimal result = ((BigDecimal)this.sum).add((BigDecimal)data.sum);
this.sum = (N)result;
} else if (this.sum instanceof BigInteger || data.sum instanceof BigInteger) {
BigInteger result = ((BigInteger)this.sum).add((BigInteger)data.sum);
this.sum = (N)result;
} else if (this.sum instanceof Long || data.sum instanceof Long) {
Long result = (this.sum.longValue() + data.sum.longValue());
this.sum = (N)result;
} else if (this.sum instanceof Double || data.sum instanceof Double) {
Double result = (this.sum.doubleValue() + data.sum.doubleValue());
this.sum = (N)result;
} else if (this.sum instanceof Float || data.sum instanceof Float) {
Float result = (this.sum.floatValue() + data.sum.floatValue());
this.sum = (N)result;
} else {
// Integer
Integer result = (this.sum.intValue() + data.sum.intValue());
this.sum = (N)result;
}
this.sum = (N) Operations.addNumbers(this.sum, data.sum);
}
}

Expand All @@ -341,27 +323,7 @@ else if (this.sum != null && data.sum == null)
else if (this.sum == null && data.sum != null)
this.sum = data.sum;
else {
/* TODO: This is ugly and how to handle number overflow? */
if (this.sum instanceof BigDecimal || data.sum instanceof BigDecimal) {
BigDecimal result = ((BigDecimal)this.sum).subtract((BigDecimal)data.sum);
this.sum = (N)result;
} else if (this.sum instanceof BigInteger || data.sum instanceof BigInteger) {
BigInteger result = ((BigInteger)this.sum).subtract((BigInteger)data.sum);
this.sum = (N)result;
} else if (this.sum instanceof Long || data.sum instanceof Long) {
Long result = (this.sum.longValue() - data.sum.longValue());
this.sum = (N)result;
} else if (this.sum instanceof Double || data.sum instanceof Double) {
Double result = (this.sum.doubleValue() - data.sum.doubleValue());
this.sum = (N)result;
} else if (this.sum instanceof Float || data.sum instanceof Float) {
Float result = (this.sum.floatValue() - data.sum.floatValue());
this.sum = (N)result;
} else {
// Integer
Integer result = (this.sum.intValue() - data.sum.intValue());
this.sum = (N)result;
}
this.sum = (N) Operations.subtractNumbers(this.sum, data.sum);
}
}

Expand Down
141 changes: 5 additions & 136 deletions src/com/jwetherell/algorithms/data_structures/Matrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Comparator;
import com.jwetherell.algorithms.mathematics.Operations;

/**
* Matrx. This Matrix implementation is designed to be more efficient
Expand All @@ -26,33 +27,7 @@ public class Matrix<T extends Number> {
@Override
public int compare(T o1, T o2) {
/* TODO: What if Java adds new numeric type? */
int result = 0;
if (o1 instanceof BigDecimal || o2 instanceof BigDecimal) {
BigDecimal c1 = (BigDecimal)o1;
BigDecimal c2 = (BigDecimal)o2;
result = c1.compareTo(c2);
} else if (o1 instanceof BigInteger || o2 instanceof BigInteger) {
BigInteger c1 = (BigInteger)o1;
BigInteger c2 = (BigInteger)o2;
result = c1.compareTo(c2);
} else if (o1 instanceof Long || o2 instanceof Long) {
Long c1 = o1.longValue();
Long c2 = o2.longValue();
result = c1.compareTo(c2);
} else if (o1 instanceof Double || o2 instanceof Double) {
Double c1 = o1.doubleValue();
Double c2 = o2.doubleValue();
result = c1.compareTo(c2);
} else if (o1 instanceof Float || o2 instanceof Float) {
Float c1 = o1.floatValue();
Float c2 = o2.floatValue();
result = c1.compareTo(c2);
} else {
Integer c1 = o1.intValue();
Integer c2 = o2.intValue();
result = c1.compareTo(c2);
}
return result;
return Operations.compare(o1, o2);
}
};

Expand Down Expand Up @@ -165,29 +140,7 @@ public Matrix<T> add(Matrix<T> input) {
for (int i = 0; i < cols; i++) {
T m1 = this.get(r, c);
T m2 = input.get(r, c);
T result;
/* TODO: This is ugly and how to handle number overflow? */
if (m1 instanceof BigDecimal || m2 instanceof BigDecimal) {
BigDecimal result2 = ((BigDecimal)m1).add((BigDecimal)m2);
result = (T)result2;
} else if (m1 instanceof BigInteger || m2 instanceof BigInteger) {
BigInteger result2 = ((BigInteger)m1).add((BigInteger)m2);
result = (T)result2;
} else if (m1 instanceof Long || m2 instanceof Long) {
Long result2 = (m1.longValue() + m2.longValue());
result = (T)result2;
} else if (m1 instanceof Double || m2 instanceof Double) {
Double result2 = (m1.doubleValue() + m2.doubleValue());
result = (T)result2;
} else if (m1 instanceof Float || m2 instanceof Float) {
Float result2 = (m1.floatValue() + m2.floatValue());
result = (T)result2;
} else {
// Integer
Integer result2 = (m1.intValue() + m2.intValue());
result = (T)result2;
}
output.set(r, c, result);
output.set(r, c, (T) Operations.addNumbers(m1, m2));
}
}
}
Expand All @@ -204,29 +157,7 @@ public Matrix<T> subtract(Matrix<T> input) {
for (int i = 0; i < cols; i++) {
T m1 = this.get(r, c);
T m2 = input.get(r, c);
T result;
/* TODO: This is ugly and how to handle number overflow? */
if (m1 instanceof BigDecimal || m2 instanceof BigDecimal) {
BigDecimal result2 = ((BigDecimal)m1).subtract((BigDecimal)m2);
result = (T)result2;
} else if (m1 instanceof BigInteger || m2 instanceof BigInteger) {
BigInteger result2 = ((BigInteger)m1).subtract((BigInteger)m2);
result = (T)result2;
} else if (m1 instanceof Long || m2 instanceof Long) {
Long result2 = (m1.longValue() - m2.longValue());
result = (T)result2;
} else if (m1 instanceof Double || m2 instanceof Double) {
Double result2 = (m1.doubleValue() - m2.doubleValue());
result = (T)result2;
} else if (m1 instanceof Float || m2 instanceof Float) {
Float result2 = (m1.floatValue() - m2.floatValue());
result = (T)result2;
} else {
// Integer
Integer result2 = (m1.intValue() - m2.intValue());
result = (T)result2;
}
output.set(r, c, result);
output.set(r, c, (T) Operations.subtractNumbers(m1, m2));
}
}
}
Expand All @@ -243,69 +174,7 @@ public Matrix<T> multiply(Matrix<T> input) {
T[] row = getRow(r);
T[] column = input.getColumn(c);
T test = row[0];
/* TODO: This is ugly and how to handle number overflow? */
if (test instanceof BigDecimal) {
BigDecimal result = BigDecimal.ZERO;
for (int i = 0; i < cols; i++) {
T m1 = row[i];
T m2 = column[i];

BigDecimal result2 = ((BigDecimal)m1).multiply(((BigDecimal)m2));
result = result.add(result2);
}
output.set(r, c, (T)result);
} else if (test instanceof BigInteger) {
BigInteger result = BigInteger.ZERO;
for (int i = 0; i < cols; i++) {
T m1 = row[i];
T m2 = column[i];

BigInteger result2 = ((BigInteger)m1).multiply(((BigInteger)m2));
result = result.add(result2);
}
output.set(r, c, (T)result);
} else if (test instanceof Long) {
Long result = 0l;
for (int i = 0; i < cols; i++) {
T m1 = row[i];
T m2 = column[i];

Long result2 = m1.longValue() * m2.longValue();
result = result+result2;
}
output.set(r, c, (T)result);
} else if (test instanceof Double) {
Double result = 0d;
for (int i = 0; i < cols; i++) {
T m1 = row[i];
T m2 = column[i];

Double result2 = m1.doubleValue() * m2.doubleValue();
result = result+result2;
}
output.set(r, c, (T)result);
} else if (test instanceof Float) {
Float result = 0f;
for (int i = 0; i < cols; i++) {
T m1 = row[i];
T m2 = column[i];

Float result2 = m1.floatValue() * m2.floatValue();
result = result+result2;
}
output.set(r, c, (T)result);
} else {
// Integer
Integer result = 0;
for (int i = 0; i < cols; i++) {
T m1 = row[i];
T m2 = column[i];

Integer result2 = m1.intValue() * m2.intValue();
result = result+result2;
}
output.set(r, c, (T)result);
}
output.set(r, c, (T) Operations.rowMultiplication(test, row, column, cols));
}
}
return output;
Expand Down
68 changes: 7 additions & 61 deletions src/com/jwetherell/algorithms/data_structures/SegmentTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.util.Set;
import java.util.TreeSet;

import com.jwetherell.algorithms.mathematics.Operations;

/**
* Segment tree using objects and pointers. A segment tree is a tree data
* structure for storing intervals, or segments. It allows querying which of the
Expand Down Expand Up @@ -340,26 +342,8 @@ else if (this.maximum != null && data.maximum == null)
else if (this.maximum == null && data.maximum != null)
this.maximum = data.maximum;
else {
/* TODO: This is ugly */
if (this.maximum instanceof BigDecimal || data.maximum instanceof BigDecimal) {
if (((BigDecimal)data.maximum).compareTo(((BigDecimal)this.maximum))==1)
this.maximum = data.maximum;
} else if (this.maximum instanceof BigInteger || data.maximum instanceof BigInteger) {
if (((BigInteger)data.maximum).compareTo(((BigInteger)this.maximum))==1)
this.maximum = data.maximum;
} else if (this.maximum instanceof Long || data.maximum instanceof Long) {
if (((Long)data.maximum).compareTo(((Long)this.maximum))==1)
this.maximum = data.maximum;
} else if (this.maximum instanceof Double || data.maximum instanceof Double) {
if (((Double)data.maximum).compareTo(((Double)this.maximum))==1)
this.maximum = data.maximum;
} else if (this.maximum instanceof Float || data.maximum instanceof Float) {
if (((Float)data.maximum).compareTo(((Float)this.maximum))==1)
this.maximum = data.maximum;
} else {
// Integer
if (((Integer)data.maximum).compareTo(((Integer)this.maximum))==1)
this.maximum = data.maximum;
if(Operations.compare(data.maximum, this.maximum) == 1){
this.maximum = data.maximum;
}
}
}
Expand Down Expand Up @@ -471,26 +455,8 @@ else if (this.minimum != null && data.minimum == null)
else if (this.minimum == null && data.minimum != null)
this.minimum = data.minimum;
else {
/* TODO: This is ugly */
if (this.minimum instanceof BigDecimal || data.minimum instanceof BigDecimal) {
if (((BigDecimal)data.minimum).compareTo(((BigDecimal)this.minimum))==-1)
this.minimum = data.minimum;
} else if (this.minimum instanceof BigInteger || data.minimum instanceof BigInteger) {
if (((BigInteger)data.minimum).compareTo(((BigInteger)this.minimum))==-1)
this.minimum = data.minimum;
} else if (this.minimum instanceof Long || data.minimum instanceof Long) {
if (((Long)data.minimum).compareTo(((Long)this.minimum))==-1)
this.minimum = data.minimum;
} else if (this.minimum instanceof Double || data.minimum instanceof Double) {
if (((Double)data.minimum).compareTo(((Double)this.minimum))==-1)
this.minimum = data.minimum;
} else if (this.minimum instanceof Float || data.minimum instanceof Float) {
if (((Float)data.minimum).compareTo(((Float)this.minimum))==-1)
this.minimum = data.minimum;
} else {
// Integer
if (((Integer)data.minimum).compareTo(((Integer)this.minimum))==-1)
this.minimum = data.minimum;
if(Operations.compare(data.minimum, this.minimum) == -1){
this.minimum = data.minimum;
}
}
}
Expand Down Expand Up @@ -603,27 +569,7 @@ else if (this.sum != null && data.sum == null)
else if (this.sum == null && data.sum != null)
this.sum = data.sum;
else {
/* TODO: This is ugly and how to handle number overflow? */
if (this.sum instanceof BigDecimal || data.sum instanceof BigDecimal) {
BigDecimal result = ((BigDecimal)this.sum).add((BigDecimal)data.sum);
this.sum = (N)result;
} else if (this.sum instanceof BigInteger || data.sum instanceof BigInteger) {
BigInteger result = ((BigInteger)this.sum).add((BigInteger)data.sum);
this.sum = (N)result;
} else if (this.sum instanceof Long || data.sum instanceof Long) {
Long result = (this.sum.longValue() + data.sum.longValue());
this.sum = (N)result;
} else if (this.sum instanceof Double || data.sum instanceof Double) {
Double result = (this.sum.doubleValue() + data.sum.doubleValue());
this.sum = (N)result;
} else if (this.sum instanceof Float || data.sum instanceof Float) {
Float result = (this.sum.floatValue() + data.sum.floatValue());
this.sum = (N)result;
} else {
// Integer
Integer result = (this.sum.intValue() + data.sum.intValue());
this.sum = (N)result;
}
this.sum = (N) Operations.addNumbers(this.sum, data.sum);
}
}

Expand Down