Skip to content
Open
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
194 changes: 194 additions & 0 deletions proj1b/src/deque/ArrayDeque61B.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package deque;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ArrayDeque61B<T> implements Deque61B<T> {
private T[] array;
private int nextFirst;
public int nextLast;
private int size;
public int firstElementIndex;
public int lastElementIndex;

public ArrayDeque61B() {
array = (T[]) new Object[8];;
nextFirst = 4;
nextLast = 5;
size = 0;
}

private class ArrayDequeIterator implements Iterator<T> {
private int wizPos;

@Override
public boolean hasNext() {
return wizPos < size;
}

@Override
public T next() {
int indexToReturn = wizPos;
wizPos++;
return get(indexToReturn);
}
}

@Override
public Iterator<T> iterator() {
return new ArrayDequeIterator();
}

@Override
public boolean equals(Object o) {
if (o instanceof ArrayDeque61B otherArray) {
if (size() != otherArray.size()) return false;

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

@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("[");
int count = 0;
for (T x : this) {
stringBuilder.append(x);
count++;
if (count < size) {
stringBuilder.append(", ");
}
else {
stringBuilder.append("]");
}
}
return stringBuilder.toString();
}

@Override
public void addFirst(T x) {
if (size == array.length) {
resizeUp(2);
}
array[nextFirst] = x;
firstElementIndex = nextFirst;
nextFirst = Math.floorMod(nextFirst - 1, array.length);
size++;
}

@Override
public void addLast(T x) {
if (size == array.length) {
resizeUp(2);
}

if (array[firstElementIndex] == null) firstElementIndex = nextLast;

array[nextLast] = x;
lastElementIndex = nextLast;
nextLast = Math.floorMod(nextLast + 1, array.length);
size++;
}

@Override
public List<T> toList() {
List<T> returnList = new ArrayList<>();
int cur = firstElementIndex;
for (int i = 0; i < array.length; i++) {
int curIndex = Math.floorMod(cur, array.length);
if (array[curIndex] == null) break;
returnList.add(array[curIndex]);
cur++;
}
return returnList;
}

@Override
public boolean isEmpty() {
return size == 0;
}

@Override
public int size() {
return size;
}

@Override
public T removeFirst() {
if (array[firstElementIndex] == null) return null;
T element = array[firstElementIndex];
array[firstElementIndex] = null;
nextFirst = firstElementIndex;
firstElementIndex = Math.floorMod(nextFirst + 1, array.length);
size--;
if(toResizeDown()) resizeDown();
return element;
}

@Override
public T removeLast() {
if (array[lastElementIndex] == null) return null;
T element = array[lastElementIndex];
array[lastElementIndex] = null;
nextLast = lastElementIndex;
lastElementIndex = Math.floorMod(nextLast - 1, array.length);
size--;
if(toResizeDown()) resizeDown();
return element;
}

@Override
public T get(int index) {
int getIndex = Math.floorMod(firstElementIndex + index, array.length);
if (index < 0 || index >= array.length || array[getIndex] == null) return null;
return array[getIndex];
}

@Override
public T getRecursive(int index) {
return null;
}

public void resizeUp(double by) {
T[] outputArray = (T[]) new Object[(int) (array.length * by)];
for (int i = 0; i < array.length; i++) {
int newArrayIndex = Math.floorMod(i + firstElementIndex, outputArray.length);
int curArrayIndex = Math.floorMod(i + firstElementIndex, array.length);
outputArray[newArrayIndex] = array[curArrayIndex];
lastElementIndex = newArrayIndex;
nextLast = Math.floorMod(newArrayIndex + 1, outputArray.length);
}
array = outputArray;
}

public void resizeDown() {
T[] outputArray = (T[]) new Object[(int) (array.length * 0.5)];
int curIndex = firstElementIndex;
int lastIndex = -1;
for (int i = 0; i < outputArray.length; i++) {
if (array[curIndex] == null) break;
if (array[curIndex] != null) outputArray[i] = array[curIndex];
curIndex++;
if (outputArray[i] != null) lastIndex++;
}
nextFirst = Math.floorMod(-1, outputArray.length);
nextLast = Math.floorMod(lastIndex + 1, outputArray.length);

firstElementIndex = Math.floorMod(nextFirst + 1, outputArray.length);
lastElementIndex = Math.floorMod(nextLast - 1, outputArray.length);
array = outputArray;
}

private boolean toResizeDown() {
if (array.length >= 16) {
double ratio = (double) size / array.length;
if (ratio < 0.25) return true;
}
return false;
}
}
2 changes: 1 addition & 1 deletion proj1b/src/deque/Deque61B.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Created by hug on 2/4/2017. Methods are provided in the suggested order
* that they should be completed.
*/
public interface Deque61B<T> {
public interface Deque61B<T> extends Iterable<T> {

/**
* Add {@code x} to the front of the deque. Assumes {@code x} is never null.
Expand Down
28 changes: 20 additions & 8 deletions proj1b/src/deque/Maximizer61B.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ public class Maximizer61B {
* @return the maximum element
*/
public static <T extends Comparable<T>> T max(Iterable<T> iterable) {
return null;
T max = null;
for (T x : iterable) {
if (max == null || x.compareTo(max) > 0) {
max = x;
}
}
return max;
}

/**
Expand All @@ -22,17 +28,23 @@ public static <T extends Comparable<T>> T max(Iterable<T> iterable) {
* @return the maximum element according to the comparator
*/
public static <T> T max(Iterable<T> iterable, Comparator<T> comp) {
return null;
T max = null;
for (T x : iterable) {
if (max == null || comp.compare(x, max) > 0) {
max = x;
}
}
return max;
}

public static void main(String[] args) {
// The style checker will complain about this main method, feel free to delete.

// ArrayDeque61B<Integer> ad = new ArrayDeque61B<>();
// ad.addLast(5);
// ad.addLast(12);
// ad.addLast(17);
// ad.addLast(23);
// System.out.println(max(ad));
ArrayDeque61B<Integer> ad = new ArrayDeque61B<>();
ad.addLast(5);
ad.addLast(12);
ad.addLast(17);
ad.addLast(23);
System.out.println(max(ad));
}
}
Loading