Skip to content

Commit

Permalink
Added lab 9 starter files
Browse files Browse the repository at this point in the history
  • Loading branch information
joshHug committed Mar 14, 2018
1 parent 2e2cd37 commit 8bad0fe
Show file tree
Hide file tree
Showing 8 changed files with 832 additions and 0 deletions.
129 changes: 129 additions & 0 deletions lab9/lab9/ArrayMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package lab9;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
* An array based implementation of the Map61B class.
* @author Josh Hug (mostly done in lecture)
*/
public class ArrayMap<K, V> implements Map61B<K, V> {
private K[] keys;
private V[] values;
int size;

public ArrayMap() {
keys = (K[]) new Object[100];
values = (V[]) new Object[100];
size = 0;
}

/** Returns the index of the given key if it exists,
* -1 otherwise. */
private int keyIndex(K key) {
for (int i = 0; i < size; i += 1) {
if (keys[i].equals(key)) {
return i;
}
}
return -1;
}

@Override
public boolean containsKey(K key) {
int index = keyIndex(key);
return index > -1;
}

@Override
public void put(K key, V value) {
if (key == null) {
throw new IllegalArgumentException("Null key not allowed.");
}
if (value == null) {
throw new IllegalArgumentException("Null values not allowed.");
}
int index = keyIndex(key);
if (index == -1) {
if (size == keys.length) {
resize(keys.length * 2);
}
keys[size] = key;
values[size] = value;
size += 1;
return;
}
values[index] = value;
}

private void resize(int capacity) {
K[] newKeys = (K[]) new Object[capacity];
V[] newValues = (V[]) new Object[capacity];
System.arraycopy(keys, 0, newKeys, 0, size);
System.arraycopy(values, 0, newValues, 0, size);
keys = newKeys;
values = newValues;
}

@Override
public V get(K key) {
int index = keyIndex(key);
if (index == -1) {
return null;
}
return values[index];
}

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

@Override
public Set<K> keySet() {
Set<K> keyset = new HashSet<>();
for (int i = 0; i < size; i += 1) {
keyset.add(keys[i]);
}
return keyset;
}

@Override
public V remove(K key) {
int keyLocation = keyIndex(key);
V returnValue = null;
if (keyLocation > -1) {
returnValue = values[keyLocation];
keys[keyLocation] = keys[size - 1];
values[keyLocation] = values[size - 1];
size -= 1;
}
return returnValue;
}

@Override
public Iterator<K> iterator() {
return keySet().iterator();
}

@Override
public void clear() {
keys = (K[]) new Object[100];
values = (V[]) new Object[100];
size = 0;
}

@Override
public V remove(K key, V value) {
int keyLocation = keyIndex(key);
V returnValue = null;
if (keyLocation > -1 && values[keyLocation].equals(value)) {
returnValue = values[keyLocation];
keys[keyLocation] = keys[size - 1];
values[keyLocation] = values[size - 1];
size -= 1;
}
return returnValue;
}
}
108 changes: 108 additions & 0 deletions lab9/lab9/BSTMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package lab9;

import java.util.Iterator;
import java.util.Set;

/**
* Implementation of interface Map61B with BST as core data structure.
*
* @author Your name here
*/
public class BSTMap<K extends Comparable<K>, V> implements Map61B<K, V> {

private class Node {
/* (K, V) pair stored in this Node. */
private K key;
private V value;

/* Children of this Node. */
private Node left;
private Node right;

private Node(K k, V v) {
key = k;
value = v;
}
}

private Node root; /* Root node of the tree. */
private int size; /* The number of key-value pairs in the tree */

/* Creates an empty BSTMap. */
public BSTMap() {
this.clear();
}

/* Removes all of the mappings from this map. */
@Override
public void clear() {
root = null;
size = 0;
}

/** Returns the value mapped to by KEY in the subtree rooted in P.
* or null if this map contains no mapping for the key.
*/
private V getHelper(K key, Node p) {
throw new UnsupportedOperationException();
}

/** Returns the value to which the specified key is mapped, or null if this
* map contains no mapping for the key.
*/
@Override
public V get(K key) {
throw new UnsupportedOperationException();
}

/** Returns a BSTMap rooted in p with (KEY, VALUE) added as a key-value mapping.
* Or if p is null, it returns a one node BSTMap containing (KEY, VALUE).
*/
private Node putHelper(K key, V value, Node p) {
throw new UnsupportedOperationException();
}

/** Inserts the key KEY
* If it is already present, updates value to be VALUE.
*/
@Override
public void put(K key, V value) {
throw new UnsupportedOperationException();
}

/* Returns the number of key-value mappings in this map. */
@Override
public int size() {
throw new UnsupportedOperationException();
}

//////////////// EVERYTHING BELOW THIS LINE IS OPTIONAL ////////////////

/* Returns a Set view of the keys contained in this map. */
@Override
public Set<K> keySet() {
throw new UnsupportedOperationException();
}

/** Removes KEY from the tree if present
* returns true on successful removal, false otherwise.
*/
@Override
public V remove(K key) {
throw new UnsupportedOperationException();
}

/** Removes the key-value entry for the specified key only if it is
* currently mapped to the specified value. Returns the VALUE removed,
* null on failed removal.
**/
@Override
public V remove(K key, V value) {
throw new UnsupportedOperationException();
}

@Override
public Iterator<K> iterator() {
throw new UnsupportedOperationException();
}
}
39 changes: 39 additions & 0 deletions lab9/lab9/Map61B.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package lab9;
import java.util.Set;
/* Your implementation BSTMap should implement this interface. To do so,
* append "implements Map61B<K,V>" to the end of your "public class..."
* declaration, though you can use other formal type parameters if you'd like.
*/
public interface Map61B<K, V> extends Iterable<K> {
/** Removes all of the mappings from this map. */
void clear();

/* Returns the value to which the specified key is mapped, or null if this
* map contains no mapping for the key.
*/
V get(K key);

/* Returns true if this map contains a mapping for the specified key. */
default boolean containsKey(K key) {
return get(key) != null;
}

/* Associates the specified value with the specified key in this map. */
void put(K key, V value);

/* Returns the number of key-value mappings in this map. */
int size();

/* Returns a Set view of the keys contained in this map. */
Set<K> keySet();

/* Removes the mapping for the specified key from this map if present.
* Not required for Lab 9. If you don't implement this, throw an
* UnsupportedOperationException. */
V remove(K key);

/* Removes the entry for the specified key only if it is currently mapped to
* the specified value. Not required for Lab 9. If you don't implement this,
* throw an UnsupportedOperationException.*/
V remove(K key, V value);
}
99 changes: 99 additions & 0 deletions lab9/lab9/MyHashMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package lab9;

import java.util.Iterator;
import java.util.Set;

/**
* A hash table-backed Map implementation. Provides amortized constant time
* access to elements via get(), remove(), and put() in the best case.
*
* @author Your name here
*/
public class MyHashMap<K, V> implements Map61B<K, V> {

private static final int DEFAULT_SIZE = 16;
private static final double MAX_LF = 0.75;

private ArrayMap<K, V>[] buckets;
private int size;

private int loadFactor() {
return size / buckets.length;
}

public MyHashMap() {
buckets = new ArrayMap[DEFAULT_SIZE];
this.clear();
}

/* Removes all of the mappings from this map. */
@Override
public void clear() {
this.size = 0;
for (int i = 0; i < this.buckets.length; i += 1) {
this.buckets[i] = new ArrayMap<>();
}
}

/** Computes the hash function of the given key. Consists of
* computing the hashcode, followed by modding by the number of buckets.
* To handle negative numbers properly, uses floorMod instead of %.
*/
private int hash(K key) {
if (key == null) {
return 0;
}

int numBuckets = buckets.length;
return Math.floorMod(key.hashCode(), numBuckets);
}

/* Returns the value to which the specified key is mapped, or null if this
* map contains no mapping for the key.
*/
@Override
public V get(K key) {
throw new UnsupportedOperationException();
}

/* Associates the specified value with the specified key in this map. */
@Override
public void put(K key, V value) {
throw new UnsupportedOperationException();
}

/* Returns the number of key-value mappings in this map. */
@Override
public int size() {
throw new UnsupportedOperationException();
}

//////////////// EVERYTHING BELOW THIS LINE IS OPTIONAL ////////////////

/* Returns a Set view of the keys contained in this map. */
@Override
public Set<K> keySet() {
throw new UnsupportedOperationException();
}

/* Removes the mapping for the specified key from this map if exists.
* Not required for this lab. If you don't implement this, throw an
* UnsupportedOperationException. */
@Override
public V remove(K key) {
throw new UnsupportedOperationException();
}

/* Removes the entry for the specified key only if it is currently mapped to
* the specified value. Not required for this lab. If you don't implement this,
* throw an UnsupportedOperationException.*/
@Override
public V remove(K key, V value) {
throw new UnsupportedOperationException();
}

@Override
public Iterator<K> iterator() {
throw new UnsupportedOperationException();
}
}
Loading

0 comments on commit 8bad0fe

Please sign in to comment.