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

Term Project with Runtime Analysis #5

Open
wants to merge 33 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
c996c30
build tests, implement arraylist
rjtrenchard Feb 12, 2024
65fc179
fix remove, implement more arraylist tests
rjtrenchard Feb 12, 2024
e3d5423
fix infinite loop, implement iterator tests
rjtrenchard Feb 12, 2024
037d4ac
fix resize method, fix tests
rjtrenchard Feb 13, 2024
da9c717
fix iterator
rjtrenchard Feb 13, 2024
45b50b1
finalize arraylist
rjtrenchard Feb 13, 2024
165e4f9
start linkedlist
rjtrenchard Feb 13, 2024
c641e49
convert arraylist test to linklist tests
rjtrenchard Feb 13, 2024
42bf8f1
implement some linkedlist methods
rjtrenchard Feb 13, 2024
993f1be
passing linked list tests for add/get
rjtrenchard Feb 13, 2024
235b02b
implement remove methods, all tests pass.
rjtrenchard Feb 13, 2024
b273ac0
add runtime analysis in JavaDocs.
rjtrenchard Feb 14, 2024
e2b35f2
format runtime analysis in JavaDocs.
rjtrenchard Feb 14, 2024
1062e4a
clarification on BigO
rjtrenchard Feb 14, 2024
b77a8bb
Implement Dequeue. not yet tested.
rjtrenchard Feb 14, 2024
4cdca56
implement stack, queue.
rjtrenchard Feb 15, 2024
deb6d2d
generate tests
rjtrenchard Feb 15, 2024
e0feb43
create and generate tests for mathset
rjtrenchard Feb 15, 2024
4c32990
fix deque
rjtrenchard Feb 15, 2024
ea6fd47
fix stack size
rjtrenchard Feb 16, 2024
28254bf
implement stack tests
rjtrenchard Feb 16, 2024
bb7e53f
implement queue tests
rjtrenchard Feb 16, 2024
533021c
rename implementations
rjtrenchard Feb 16, 2024
7e84951
implement Bag
rjtrenchard Feb 16, 2024
38d6595
implement Bag tests, fix iterator
rjtrenchard Feb 16, 2024
060ab9c
implement resizingarraystack, add tests from linkedstack
rjtrenchard Feb 16, 2024
9eeca8e
change finals to static
rjtrenchard Feb 16, 2024
620c4b3
add Stat preview file
rjtrenchard Feb 16, 2024
9b46961
formatting
rjtrenchard Feb 16, 2024
5778ee7
formatting
rjtrenchard Feb 16, 2024
073dc66
comment headers
rjtrenchard Feb 16, 2024
b1de871
Beautify
rjtrenchard Feb 16, 2024
5e5518f
Add default constructors, test clients
rjtrenchard Feb 16, 2024
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
301 changes: 301 additions & 0 deletions src/ArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,301 @@
/**
* ArrayList
* List that uses a built-in array to store its data
*
* @author R.J. Trenchard
* 2/15/2024
*/

import org.jetbrains.annotations.NotNull;

import java.util.Iterator;

public class ArrayList<E> implements List<E> {

private static final double MAX_CAPACITY = 0.75;
private static final double MIN_CAPACITY = 0.5;
private static final int DEFAULT_BUFFER = 10;

private E[] arrayData;
int arraySize;

/**
* Generate an arraylist of the default buffer size
*/
public ArrayList() {
this(DEFAULT_BUFFER);
}

/**
* Generate an array list of a specified buffer size
* @param bufsize size of the initial underlying buffer.
*/
public ArrayList(int bufsize) {
arrayData = (E[])new Object[bufsize];
arraySize = 0;
}

// performs resizing if too large or too small of a buffer size.
// O(n), linear progression each time we need to make new spaces.
//
// grow/shrink are included in this

private void autoResize() {
double capacity = this.size() / (double)arrayData.length;

// grow when we reach max capacity
if (capacity > MAX_CAPACITY) grow();
// shrink if we're below the min capacity, but the default buffer must have a size.
else if (capacity < MIN_CAPACITY && arrayData.length > DEFAULT_BUFFER) shrink();
}

// general index checker
private void checkIfInRange(int i) {
if (i < 0 || i >= arraySize) {
throw new IndexOutOfBoundsException(String.format("%d is not a valid index.", i));
}
}

private void grow() {
// hold old data
E[] buf = arrayData;
int newLength = arrayData.length + DEFAULT_BUFFER;

// create new array of a larger size
arrayData = (E[]) new Object[newLength];

this.copy(buf, arrayData);
}

private void shrink() {
// hold old data
E[] buf = arrayData;
int newLength = arrayData.length - DEFAULT_BUFFER;

// don't try if the new length is too small
if (newLength < DEFAULT_BUFFER) return;

// create new array of a smaller size
arrayData = (E[]) new Object[newLength];

this.copy(buf, arrayData);
}

// O(n), linear copy.
private void copy(E[] from, E[] to) {
// copy old data to new array
int shortest = Math.min(to.length, from.length);
for (int i = 0; i < shortest; i++) {
to[i] = from[i];
}
}

/**
* Add item to the front.
* O(n) - adding a new item to the beginning means you need to look at every element
*
* @param item the item to be added
*/
@Override
public void addFront(E item) {
add(0, item);
}

/**
* Add item to the back.
* O(n) - would be O(1) if arrays could be expanded without copying, but no luck there.
*
* @param item the item to be added
*/
@Override
public void addBack(E item) {
add(this.size(), item);
}

/**
* Add an item at specified index (position).
* O(n) - item added to the beginning would move the whole array.
* additionally, changes in array capacity would result in a second pass.
*
* @param i the index where the item should be added
* @param item the item to be added
*/
@Override
public void add(int i, E item) {
// special range check, add will be able to perform addBack
if (i < 0 || i > this.size()) throw new IndexOutOfBoundsException(String.format("%d is not a valid index.", i));

autoResize();

// move all from rhs of index right by one
for (int idx = this.size(); idx > i; idx--) {
arrayData[idx] = arrayData[idx-1];
}

// set new value and array size
arrayData[i] = item;
arraySize++;
}

/**
* Get the item at a specified index.
* O(1) - Access is constant in an array
*
* @param i the index where the item should be retrieved
* @return the item located at that index
*/
@Override
public E get(int i) {
checkIfInRange(i);
return arrayData[i];
}

/**
* Set (save) an item at a specified index. Previous
* item at that index is overwritten.
* O(1) - access/setting is constant
*
* @param i the index where the item should be saved
* @param item the item to be saved
*/
@Override
public void set(int i, E item) {
checkIfInRange(i);
arrayData[i] = item;
}

/**
* Remove item at the front of the list.
* O(n) - removing an element from the front results in moving the whole array
*
* @return the item that was removed
*/
@Override
public E removeFront() {
return remove(0);
}

/**
* Remove item at the back of the list
* O(n) - changes in array capacity will result in copying all data
*
* @return the item that was removed
*/
@Override
public E removeBack() {
return remove(this.size()-1);
}

/**
* Remove item from the list
* O(n) - changes in array capacity will result in copying all data
*
* @param item the item to be removed
*/
@Override
public void remove(E item) {
for (int i = 0; i < this.size(); i++) {
if (arrayData[i].equals(item)) {
remove(i); // call the integer type remove, since it will do the same thing once we find the index
return; // we probably only want to remove one item at a time
}

}
}

/**
* Remove item at a specified index.
* O(n) - changes in array capacity will result in copying all data
*
* @param i the index where the item should be removed
* @return the item that was removed
*/
@Override
public E remove(int i) {
checkIfInRange(i);

autoResize();

E data = arrayData[i];

// shift all from rhs left to shrink, adjust array size
for (int idx = i; idx < this.size(); idx++) {
arrayData[idx] = arrayData[idx+1];
}
arraySize--;

return data;
}

/**
* Checks if an item is in the list.
* O(n) - item not present will go over whole array
*
* @param item the item to search for
* @return true if the item is in the list, false otherwise
*/
@Override
public boolean contains(E item) {
for (int i = 0; i < arraySize; i++)
if (arrayData[i].equals(item)) return true;
return false;
}

/**
* Checks if the list is empty.
* O(1) - instant access to size
*
* @return true if the list is empty, false otherwise
*/
@Override
public boolean isEmpty() {
return this.size() == 0;
}

/**
* Provides a count of the number of items in the list.
* O(1) - instant access to size
*
* @return number of items in the list
*/
@Override
public int size() {
return arraySize;
}

/**
* Returns an iterator over elements of type {@code T}.
* O(n) - by its nature
*
* @return an Iterator.
*/
@NotNull
@Override
public Iterator<E> iterator() {

E[] readOnly = arrayData.clone();

return new Iterator<>() {
int index = 0;
final int size = size();

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

@Override
public E next() {
if (!hasNext()) throw new IndexOutOfBoundsException();
E value = readOnly[index];
index++;
return value;
}
};
}

// for use in testing
protected int getArraySize() {
return arrayData.length;
}
}
26 changes: 26 additions & 0 deletions src/Bag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Bag interface only supports adding and iterating.
*
* @param <E> Type to be added
* @author R.J. Trenchard
*/

public interface Bag<E> extends Iterable<E> {
/**
* Adds an item to the bag
* @param item An item of the generic type.
*/
void add(E item);

/**
* Returns true if the bag has no items.
* @return true if empty.
*/
boolean isEmpty();

/**
* Gets the amount of items in the bag.
* @return size
*/
int size();
}
Loading