From dadfec9a702af80559451b5f4410e90c746c33a3 Mon Sep 17 00:00:00 2001 From: dkaznacheev Date: Sat, 9 Sep 2017 17:40:09 +0300 Subject: [PATCH 1/5] added HW1 v1.0 --- HashTable.iml | 12 +++ src/ru/spbau/dkaznacheev/HashTable.java | 84 ++++++++++++++++++++ src/ru/spbau/dkaznacheev/List.java | 101 ++++++++++++++++++++++++ src/ru/spbau/dkaznacheev/Main.java | 46 +++++++++++ 4 files changed, 243 insertions(+) create mode 100644 HashTable.iml create mode 100644 src/ru/spbau/dkaznacheev/HashTable.java create mode 100644 src/ru/spbau/dkaznacheev/List.java create mode 100644 src/ru/spbau/dkaznacheev/Main.java diff --git a/HashTable.iml b/HashTable.iml new file mode 100644 index 0000000..d5c0743 --- /dev/null +++ b/HashTable.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/src/ru/spbau/dkaznacheev/HashTable.java b/src/ru/spbau/dkaznacheev/HashTable.java new file mode 100644 index 0000000..1710e74 --- /dev/null +++ b/src/ru/spbau/dkaznacheev/HashTable.java @@ -0,0 +1,84 @@ +package ru.spbau.dkaznacheev; + +public class HashTable { + + private int size; + private int capacity; + private List storage[]; + private final double SIZE_PERCENT_LIMIT = 0.75; + + public HashTable () { + this(4); + } + + public HashTable (int capacity) { + this.capacity = capacity; + storage = new List[capacity]; + for (int i = 0; i < capacity; i++) + storage[i] = new List(); + size = 0; + } + + private int getHash(String string) { + int stringHash = 7; + for (int i = 0; i < string.length(); i++) { + stringHash = (stringHash * 31 + string.charAt(i)); + } + return stringHash; + } + + public int getIndex (String key) { + int hash = getHash(key); + return hash & (capacity - 1); + } + + private void resize (int newCapacity) { + HashTable newTable = new HashTable(newCapacity); + for (int i = 0; i < capacity; i++) { + while (storage[i].size() > 0) { + List.Node node = storage[i].popFront(); + newTable.put(node.key, node.value); + } + } + this.storage = newTable.storage; + this.capacity = newTable.capacity; + } + + public int size () { + return size; + } + + public boolean contains (String key) { + int index = getIndex(key); + return storage[index].contains(key); + } + + public String put (String key, String value) { + int index = getIndex(key); + if (!storage[index].contains(key)) + size++; + String result = storage[index].put(key,value); + if (size >= SIZE_PERCENT_LIMIT * capacity) + resize(2 * capacity); + return result; + } + + public String get (String key) { + int index = getIndex(key); + return storage[index].get(key); + } + + public String remove (String key) { + int index = getIndex(key); + if (!storage[index].contains(key)) + size--; + return storage[index].remove(key); + } + + public void clear() { + for (int i = 0; i < capacity; i++) + storage[i] = new List(); + size = 0; + } + +} diff --git a/src/ru/spbau/dkaznacheev/List.java b/src/ru/spbau/dkaznacheev/List.java new file mode 100644 index 0000000..aa306c0 --- /dev/null +++ b/src/ru/spbau/dkaznacheev/List.java @@ -0,0 +1,101 @@ +package ru.spbau.dkaznacheev; + +public class List { + + class Node { + String key; + String value; + Node next; + Node (String key, String value) { + this.key = key; + this.value = value; + } + } + + private Node head; + private int size; + + public List () { + size = 0; + head = null; + } + + public List (String key, String value) { + head = new Node(key, value); + head.next = null; + size = 1; + } + + public int size() { + return size; + } + + public String get (String key) { + if (head == null) + return null; + Node node = head; + do { + if (node.key.equals(key)) + return node.value; + node = node.next; + } while (node != null); + return null; + } + + public String put (String key, String value) { + if (head == null) { + head = new Node(key, value); + size++; + return null; + } + Node node = head; + Node tail; + do { + if (node.key.equals(key)) { + String oldValue = node.value; + node.value = value; + return oldValue; + } + tail = node; + node = node.next; + } while (node != null); + tail.next = new Node(key, value); + size++; + return null; + } + + public Node popFront () { + if (head == null) + return null; + Node result = head; + head = head.next; + size--; + return result; + } + + public String remove (String key) { + if (head == null) + return null; + Node node = head; + Node prev = null; + do { + if (node.key.equals(key)) { + String oldValue = node.value; + if (prev != null) { + prev.next = node.next; + } else { + head = node.next; + } + size--; + return oldValue; + } + prev = node; + node = node.next; + } while (node != null); + return null; + } + + public boolean contains (String key) { + return (get(key) != null); + } +} diff --git a/src/ru/spbau/dkaznacheev/Main.java b/src/ru/spbau/dkaznacheev/Main.java new file mode 100644 index 0000000..4a0dbf8 --- /dev/null +++ b/src/ru/spbau/dkaznacheev/Main.java @@ -0,0 +1,46 @@ +package ru.spbau.dkaznacheev; + +public class Main { + static void test1() { + HashTable hashTable = new HashTable(); + hashTable.put("Vasya", "1 group"); + hashTable.put("Petya", "2 group"); + hashTable.put("Igor", "3 group"); + System.out.println(hashTable.get("Vasya")); + System.out.println(hashTable.get("Petya")); + System.out.println(hashTable.put("Igor", "4 group")); + System.out.println(hashTable.get("Igor")); + System.out.println(hashTable.get("Oleg")); + } + + static void test2 () { + List list = new List("ka", "va"); + System.out.println(list.get("ka")); + System.out.println(list.put("ka", "v1")); + System.out.println(list.contains("ka")); + + list.put("kb", "vb"); + System.out.println(list.get("kb")); + list.remove("ka"); + System.out.println(list.get("ka")); + } + + static void test3 () { + HashTable table = new HashTable(4); + table.put("ccc", "0"); + table.put("ddd", "1"); + table.put("aaa", "2"); + table.put("bbb", "3"); + System.out.println(table.get("aaa")); + System.out.println(table.get("bbb")); + System.out.println(table.get("ccc")); + System.out.println(table.get("ddd")); + + } + + public static void main(String[] args) { + test1(); + test2(); + test3(); + } +} From 085cd434103d6cc66cebd63ab14c27e7973d4f2b Mon Sep 17 00:00:00 2001 From: dkaznacheev Date: Wed, 20 Sep 2017 22:13:52 +0300 Subject: [PATCH 2/5] Added Javadoc comments --- src/ru/spbau/dkaznacheev/HashTable.java | 62 +++++++++++++++++++++++-- src/ru/spbau/dkaznacheev/List.java | 48 +++++++++++++++---- 2 files changed, 99 insertions(+), 11 deletions(-) diff --git a/src/ru/spbau/dkaznacheev/HashTable.java b/src/ru/spbau/dkaznacheev/HashTable.java index 1710e74..eea6c3e 100644 --- a/src/ru/spbau/dkaznacheev/HashTable.java +++ b/src/ru/spbau/dkaznacheev/HashTable.java @@ -1,10 +1,26 @@ package ru.spbau.dkaznacheev; +/** +* Stores elements of java.lang.String inside by their hash codes. +* Automatically resizes if the size exceeds a certain fraction of capacity. + **/ public class HashTable { - + /** + * Current number of elements in the table. + **/ private int size; + /** + * Current capacity of the table. + **/ private int capacity; + /** + * The storage, consists of Lists, which contain the elements. + **/ private List storage[]; + + /** + * The size-to-capacity ratio after which the resizing happens. + **/ private final double SIZE_PERCENT_LIMIT = 0.75; public HashTable () { @@ -19,6 +35,11 @@ public HashTable (int capacity) { size = 0; } + /** + * Returns the hash code of the given string. + * @param string The string that gets hashed. + * @return The hash. + **/ private int getHash(String string) { int stringHash = 7; for (int i = 0; i < string.length(); i++) { @@ -27,11 +48,20 @@ private int getHash(String string) { return stringHash; } + /** + * Finds the position in the storage array by the key. + * @param key The given key + * @return The index in the array. + **/ public int getIndex (String key) { int hash = getHash(key); return hash & (capacity - 1); } + /** + * Resizes the table. + * @param newCapacity New capacity of the table. + **/ private void resize (int newCapacity) { HashTable newTable = new HashTable(newCapacity); for (int i = 0; i < capacity; i++) { @@ -44,15 +74,30 @@ private void resize (int newCapacity) { this.capacity = newTable.capacity; } + /** + * Returns how many elements is in the table. + * @return The table size. + **/ public int size () { return size; } + /** + * Checks if there is an element with the given key in the table. + * @param key The given key. + * @return Whether there is an element. + **/ public boolean contains (String key) { int index = getIndex(key); return storage[index].contains(key); } + /** + * Changes or adds the value of the given key, returns the previous value. + * @param key The key + * @param value The new value + * @return Previous value, null if there was no value with this key. + **/ public String put (String key, String value) { int index = getIndex(key); if (!storage[index].contains(key)) @@ -63,18 +108,29 @@ public String put (String key, String value) { return result; } + /** + * Returns the value stored with given key. + * @param key The key. + * @return The value stored, null if there was no value with this key. + **/ public String get (String key) { int index = getIndex(key); return storage[index].get(key); } - + /** + * Returns the value stored with given key and removes the key-value pair from the table. + * @param key The key. + * @return The value stored, null if there was no value with this key. + **/ public String remove (String key) { int index = getIndex(key); if (!storage[index].contains(key)) size--; return storage[index].remove(key); } - + /** + * Clears the table. + **/ public void clear() { for (int i = 0; i < capacity; i++) storage[i] = new List(); diff --git a/src/ru/spbau/dkaznacheev/List.java b/src/ru/spbau/dkaznacheev/List.java index aa306c0..033a30e 100644 --- a/src/ru/spbau/dkaznacheev/List.java +++ b/src/ru/spbau/dkaznacheev/List.java @@ -1,18 +1,30 @@ package ru.spbau.dkaznacheev; - +/** + * Linked list that stores elements of java.lang.String. + **/ public class List { - + /** + * Node Subclass that describes the key-value pair. + **/ class Node { String key; String value; + /** + * The next node. + **/ Node next; Node (String key, String value) { this.key = key; this.value = value; } } - + /** + * The first element of the list. + **/ private Node head; + /** + * Size of the list. + **/ private int size; public List () { @@ -29,7 +41,11 @@ public List (String key, String value) { public int size() { return size; } - + /** + * Returns the value of the node with given key. + * @param key The key. + * @return The value, null if there is no such key. + **/ public String get (String key) { if (head == null) return null; @@ -41,7 +57,12 @@ public String get (String key) { } while (node != null); return null; } - + /** + * Changes or adds the value of the given key, returns the previous value. + * @param key The key + * @param value The new value + * @return Previous value, null if there was no value with this key. + **/ public String put (String key, String value) { if (head == null) { head = new Node(key, value); @@ -63,7 +84,10 @@ public String put (String key, String value) { size++; return null; } - + /** + * Returns the first key-value pair of the list, and then deletes it. + * @return First node, or null if the list is empty. + **/ public Node popFront () { if (head == null) return null; @@ -72,7 +96,11 @@ public Node popFront () { size--; return result; } - + /** + * Returns the value stored with given key and removes the key-value pair from the list. + * @param key The key. + * @return The value stored, null if there was no value with this key. + **/ public String remove (String key) { if (head == null) return null; @@ -94,7 +122,11 @@ public String remove (String key) { } while (node != null); return null; } - + /** + * Checks if there is an element with the given key in the list. + * @param key The given key. + * @return Whether there is an element. + **/ public boolean contains (String key) { return (get(key) != null); } From 6420856803fe37fdeb2ae9a7470bb1bfded43de7 Mon Sep 17 00:00:00 2001 From: dkaznacheev Date: Tue, 26 Sep 2017 20:07:47 +0300 Subject: [PATCH 3/5] Fixed issues in HW1 --- src/ru/spbau/dkaznacheev/HashTable.java | 25 +++++++---- src/ru/spbau/dkaznacheev/List.java | 57 ++++++++++++++++--------- 2 files changed, 54 insertions(+), 28 deletions(-) diff --git a/src/ru/spbau/dkaznacheev/HashTable.java b/src/ru/spbau/dkaznacheev/HashTable.java index eea6c3e..f1249dc 100644 --- a/src/ru/spbau/dkaznacheev/HashTable.java +++ b/src/ru/spbau/dkaznacheev/HashTable.java @@ -1,18 +1,20 @@ package ru.spbau.dkaznacheev; /** -* Stores elements of java.lang.String inside by their hash codes. -* Automatically resizes if the size exceeds a certain fraction of capacity. + * Stores elements of java.lang.String inside by their hash codes. + * Automatically resizes if the size exceeds a certain fraction of capacity. **/ public class HashTable { /** * Current number of elements in the table. **/ private int size; + /** * Current capacity of the table. **/ private int capacity; + /** * The storage, consists of Lists, which contain the elements. **/ @@ -23,15 +25,22 @@ public class HashTable { **/ private final double SIZE_PERCENT_LIMIT = 0.75; - public HashTable () { + /** + * Creates a HashTable with default capacity 4. + */ + public HashTable() { this(4); } + /** + * Creates a HashTable with given capacity. + */ public HashTable (int capacity) { this.capacity = capacity; storage = new List[capacity]; - for (int i = 0; i < capacity; i++) + for (int i = 0; i < capacity; i++) { storage[i] = new List(); + } size = 0; } @@ -66,8 +75,8 @@ private void resize (int newCapacity) { HashTable newTable = new HashTable(newCapacity); for (int i = 0; i < capacity; i++) { while (storage[i].size() > 0) { - List.Node node = storage[i].popFront(); - newTable.put(node.key, node.value); + List.Pair pair = storage[i].popFront(); + newTable.put(pair.key, pair.value); } } this.storage = newTable.storage; @@ -102,7 +111,7 @@ public String put (String key, String value) { int index = getIndex(key); if (!storage[index].contains(key)) size++; - String result = storage[index].put(key,value); + String result = storage[index].put(key, value); if (size >= SIZE_PERCENT_LIMIT * capacity) resize(2 * capacity); return result; @@ -124,7 +133,7 @@ public String get (String key) { **/ public String remove (String key) { int index = getIndex(key); - if (!storage[index].contains(key)) + if (storage[index].contains(key)) size--; return storage[index].remove(key); } diff --git a/src/ru/spbau/dkaznacheev/List.java b/src/ru/spbau/dkaznacheev/List.java index 033a30e..3278571 100644 --- a/src/ru/spbau/dkaznacheev/List.java +++ b/src/ru/spbau/dkaznacheev/List.java @@ -1,21 +1,40 @@ package ru.spbau.dkaznacheev; + +import com.sun.org.apache.bcel.internal.generic.LUSHR; + /** * Linked list that stores elements of java.lang.String. **/ public class List { + /** - * Node Subclass that describes the key-value pair. + * Subclass that describes the key-value pair. + */ + public class Pair { + public String key; + public String value; + + public Pair(String key, String value) { + this.key = key; + this.value = value; + } + } + /** + * Node Subclass that describes the list's node. **/ - class Node { - String key; - String value; + private class Node { + /** + * The key-value pair inside the node. + */ + private Pair pair; /** * The next node. **/ - Node next; - Node (String key, String value) { - this.key = key; - this.value = value; + private Node next; + + private Node (String key, String value) { + this.pair = new Pair(key, value); + this.next = null; } } /** @@ -27,9 +46,7 @@ class Node { **/ private int size; - public List () { - size = 0; - head = null; + public List() { } public List (String key, String value) { @@ -51,8 +68,8 @@ public String get (String key) { return null; Node node = head; do { - if (node.key.equals(key)) - return node.value; + if (node.pair.key.equals(key)) + return node.pair.value; node = node.next; } while (node != null); return null; @@ -72,9 +89,9 @@ public String put (String key, String value) { Node node = head; Node tail; do { - if (node.key.equals(key)) { - String oldValue = node.value; - node.value = value; + if (node.pair.key.equals(key)) { + String oldValue = node.pair.value; + node.pair.value = value; return oldValue; } tail = node; @@ -88,10 +105,10 @@ public String put (String key, String value) { * Returns the first key-value pair of the list, and then deletes it. * @return First node, or null if the list is empty. **/ - public Node popFront () { + public Pair popFront () { if (head == null) return null; - Node result = head; + Pair result = head.pair; head = head.next; size--; return result; @@ -107,8 +124,8 @@ public String remove (String key) { Node node = head; Node prev = null; do { - if (node.key.equals(key)) { - String oldValue = node.value; + if (node.pair.key.equals(key)) { + String oldValue = node.pair.value; if (prev != null) { prev.next = node.next; } else { From cb095b6eee848ce380a2bf855cceb76d6a6b5c1f Mon Sep 17 00:00:00 2001 From: dkaznacheev Date: Tue, 26 Sep 2017 20:47:33 +0300 Subject: [PATCH 4/5] Added tests for HashTable --- src/ru/spbau/dkaznacheev/HashTableTest.java | 75 ++++++++++++++++++ src/ru/spbau/dkaznacheev/ListTest.java | 88 +++++++++++++++++++++ 2 files changed, 163 insertions(+) create mode 100644 src/ru/spbau/dkaznacheev/HashTableTest.java create mode 100644 src/ru/spbau/dkaznacheev/ListTest.java diff --git a/src/ru/spbau/dkaznacheev/HashTableTest.java b/src/ru/spbau/dkaznacheev/HashTableTest.java new file mode 100644 index 0000000..28412a5 --- /dev/null +++ b/src/ru/spbau/dkaznacheev/HashTableTest.java @@ -0,0 +1,75 @@ +package ru.spbau.dkaznacheev; +import org.junit.Test; + +public class HashTableTest { + @Test + public void size() throws Exception { + HashTable table = new HashTable(4); + assert(table.size() == 0); + table.put("1", "v1"); + assert(table.size() == 1); + table.put("2", "v2"); + assert(table.size() == 2); + table.put("3", "v3"); + assert(table.size() == 3); + table.put("4", "v4"); + assert(table.size() == 4); + } + + @Test + public void contains() throws Exception { + HashTable table = new HashTable(4); + table.put("1", "v1"); + assert(table.contains("1")); + assert(!table.contains("2")); + } + + @Test + public void put() throws Exception { + HashTable table = new HashTable(4); + table.put("1", "v1"); + assert(table.size() == 1); + assert(table.contains("1")); + assert(!table.contains("v1")); + String result = table.put("1", "v11"); + assert(result.equals("v1")); + assert(table.size() == 1); + } + + @Test + public void get() throws Exception { + HashTable table = new HashTable(4); + table.put("1", "v1"); + table.put("2", "v2"); + assert(table.get("1").equals("v1")); + assert(table.get("2").equals("v2")); + assert(table.get("3") == null); + } + + @Test + public void remove() throws Exception { + HashTable table = new HashTable(4); + table.put("1", "v1"); + table.put("2", "v2"); + String result = table.remove("1"); + assert(result.equals("v1")); + assert(!table.contains("1")); + assert(table.contains("2")); + assert(table.size() == 1); + result = table.remove("3"); + assert(result == null); + assert(table.size() == 1); + } + + @Test + public void clear() throws Exception { + HashTable table = new HashTable(4); + table.put("1", "v1"); + table.put("2", "v2"); + table.clear(); + assert(table.size() == 0); + assert(!table.contains("1")); + assert(!table.contains("2")); + } + +} \ No newline at end of file diff --git a/src/ru/spbau/dkaznacheev/ListTest.java b/src/ru/spbau/dkaznacheev/ListTest.java new file mode 100644 index 0000000..b55a3ad --- /dev/null +++ b/src/ru/spbau/dkaznacheev/ListTest.java @@ -0,0 +1,88 @@ +package ru.spbau.dkaznacheev; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class ListTest { + @Test + public void size() throws Exception { + List list = new List(); + assert (list.size() == 0); + list.put("1", "v1"); + assert (list.size() == 1); + list.put("2", "v2"); + assert (list.size() == 2); + list.put("2", "v3"); + assert (list.size() == 2); + } + + @Test + public void get() throws Exception { + List list = new List("1", "v1"); + assert(list.get("1").equals("v1")); + assert(list.get("2") == null); + } + + @Test + public void put() throws Exception { + List list = new List(); + list.put("1", "v1"); + assert(list.size() == 1); + assert(list.contains("1")); + assert(!list.contains("v1")); + String result = list.put("1", "v11"); + assert(result.equals("v1")); + assert(list.size() == 1); + } + + @Test + public void popFront() throws Exception { + List list = new List(); + list.put("1", "v1"); + list.put("2", "v2"); + List.Pair pair = list.popFront(); + assert(pair.key.equals("1")); + assert(pair.value.equals("v1")); + assert(list.size() == 1); + assert(!list.contains("1")); + pair = list.popFront(); + assert(pair.key.equals("2")); + assert(pair.value.equals("v2")); + assert(list.size() == 0); + pair = list.popFront(); + assert(pair == null); + } + + @Test + public void remove() throws Exception { + List list = new List(); + list.put("1", "v1"); + list.put("2", "v2"); + list.put("3", "v3"); + String result = list.remove("2"); + assert(result.equals("v2")); + assert(!list.contains("2")); + assert(list.size() == 2); + result = list.remove("4"); + assert(result == null); + assert(list.size() == 2); + list.remove("1"); + list.remove("3"); + assert(list.size() == 0); + result = list.remove("1"); + assert(result == null); + assert(list.size() == 0); + } + + @Test + public void contains() throws Exception { + List list = new List(); + list.put("1", "v1"); + list.put("2", "v2"); + assert(list.contains("1")); + assert(!list.contains("3")); + } + + +} \ No newline at end of file From 9dc67e9933c48115f41f1e9ef3b40cef31711fbe Mon Sep 17 00:00:00 2001 From: dkaznacheev Date: Sun, 22 Oct 2017 19:28:37 +0300 Subject: [PATCH 5/5] First fixes in HashTable test --- src/ru/spbau/dkaznacheev/HashTable.java | 25 ++++--- src/ru/spbau/dkaznacheev/HashTableTest.java | 81 +++++++++++++------- src/ru/spbau/dkaznacheev/List.java | 82 +++++++++++++++++++-- src/ru/spbau/dkaznacheev/ListTest.java | 70 +++++++++--------- 4 files changed, 179 insertions(+), 79 deletions(-) diff --git a/src/ru/spbau/dkaznacheev/HashTable.java b/src/ru/spbau/dkaznacheev/HashTable.java index f1249dc..1c7a8b7 100644 --- a/src/ru/spbau/dkaznacheev/HashTable.java +++ b/src/ru/spbau/dkaznacheev/HashTable.java @@ -35,13 +35,12 @@ public HashTable() { /** * Creates a HashTable with given capacity. */ - public HashTable (int capacity) { + public HashTable(int capacity) { this.capacity = capacity; storage = new List[capacity]; for (int i = 0; i < capacity; i++) { storage[i] = new List(); } - size = 0; } /** @@ -62,7 +61,7 @@ private int getHash(String string) { * @param key The given key * @return The index in the array. **/ - public int getIndex (String key) { + private int getIndex(String key) { int hash = getHash(key); return hash & (capacity - 1); } @@ -71,12 +70,12 @@ public int getIndex (String key) { * Resizes the table. * @param newCapacity New capacity of the table. **/ - private void resize (int newCapacity) { + private void resize(int newCapacity) { HashTable newTable = new HashTable(newCapacity); for (int i = 0; i < capacity; i++) { while (storage[i].size() > 0) { List.Pair pair = storage[i].popFront(); - newTable.put(pair.key, pair.value); + newTable.put(pair.getKey(), pair.getValue()); } } this.storage = newTable.storage; @@ -87,7 +86,7 @@ private void resize (int newCapacity) { * Returns how many elements is in the table. * @return The table size. **/ - public int size () { + public int size() { return size; } @@ -96,7 +95,7 @@ public int size () { * @param key The given key. * @return Whether there is an element. **/ - public boolean contains (String key) { + public boolean contains(String key) { int index = getIndex(key); return storage[index].contains(key); } @@ -107,7 +106,7 @@ public boolean contains (String key) { * @param value The new value * @return Previous value, null if there was no value with this key. **/ - public String put (String key, String value) { + public String put(String key, String value) { int index = getIndex(key); if (!storage[index].contains(key)) size++; @@ -122,21 +121,24 @@ public String put (String key, String value) { * @param key The key. * @return The value stored, null if there was no value with this key. **/ - public String get (String key) { + public String get(String key) { int index = getIndex(key); return storage[index].get(key); } + /** * Returns the value stored with given key and removes the key-value pair from the table. * @param key The key. * @return The value stored, null if there was no value with this key. **/ - public String remove (String key) { + public String remove(String key) { int index = getIndex(key); - if (storage[index].contains(key)) + if (storage[index].contains(key)) { size--; + } return storage[index].remove(key); } + /** * Clears the table. **/ @@ -145,5 +147,4 @@ public void clear() { storage[i] = new List(); size = 0; } - } diff --git a/src/ru/spbau/dkaznacheev/HashTableTest.java b/src/ru/spbau/dkaznacheev/HashTableTest.java index 28412a5..e08eb80 100644 --- a/src/ru/spbau/dkaznacheev/HashTableTest.java +++ b/src/ru/spbau/dkaznacheev/HashTableTest.java @@ -1,68 +1,95 @@ package ru.spbau.dkaznacheev; import org.junit.Test; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + public class HashTableTest { @Test - public void size() throws Exception { + public void emptyHashTableSize() throws Exception { + HashTable table = new HashTable(4); + assertEquals(0, table.size()); + } + + + @Test + public void sizeChangesOnAdd() throws Exception { HashTable table = new HashTable(4); - assert(table.size() == 0); table.put("1", "v1"); - assert(table.size() == 1); + assertEquals(1, table.size()); table.put("2", "v2"); - assert(table.size() == 2); + assertEquals(2, table.size()); table.put("3", "v3"); - assert(table.size() == 3); + assertEquals(3, table.size()); table.put("4", "v4"); - assert(table.size() == 4); + assertEquals(4, table.size()); } @Test - public void contains() throws Exception { + public void containsAdded() throws Exception { HashTable table = new HashTable(4); table.put("1", "v1"); - assert(table.contains("1")); - assert(!table.contains("2")); + assertTrue(table.contains("1")); + assertTrue(!table.contains("2")); } @Test - public void put() throws Exception { + public void containsKey() throws Exception { HashTable table = new HashTable(4); table.put("1", "v1"); - assert(table.size() == 1); - assert(table.contains("1")); - assert(!table.contains("v1")); + assertEquals(1, table.size()); + assertTrue(table.contains("1")); + assertTrue(!table.contains("v1")); String result = table.put("1", "v11"); - assert(result.equals("v1")); - assert(table.size() == 1); + assertTrue(result.equals("v1")); + assertEquals(1, table.size()); + } + + + @Test + public void getAdded() throws Exception { + HashTable table = new HashTable(4); + table.put("1", "v1"); + table.put("2", "v2"); + assertTrue(table.get("1").equals("v1")); + assertTrue(table.get("2").equals("v2")); } @Test - public void get() throws Exception { + public void getNotExisting() throws Exception { HashTable table = new HashTable(4); table.put("1", "v1"); table.put("2", "v2"); - assert(table.get("1").equals("v1")); - assert(table.get("2").equals("v2")); - assert(table.get("3") == null); + assertEquals(null, table.get("3")); } @Test - public void remove() throws Exception { + public void removeChangesSize() throws Exception { + HashTable table = new HashTable(4); + table.put("1", "v1"); + table.put("2", "v2"); + table.remove("1"); + assertEquals(1, table.size()); + table.remove("3"); + assertEquals(1, table.size()); + } + + @Test + public void removedElements() throws Exception { HashTable table = new HashTable(4); table.put("1", "v1"); table.put("2", "v2"); String result = table.remove("1"); - assert(result.equals("v1")); - assert(!table.contains("1")); - assert(table.contains("2")); - assert(table.size() == 1); + assertTrue(result.equals("v1")); + assertTrue(!table.contains("1")); + assertTrue(table.contains("2")); result = table.remove("3"); - assert(result == null); - assert(table.size() == 1); + assertNull(result); } @Test - public void clear() throws Exception { + public void clearTable() throws Exception { HashTable table = new HashTable(4); table.put("1", "v1"); table.put("2", "v2"); diff --git a/src/ru/spbau/dkaznacheev/List.java b/src/ru/spbau/dkaznacheev/List.java index 3278571..68d4f07 100644 --- a/src/ru/spbau/dkaznacheev/List.java +++ b/src/ru/spbau/dkaznacheev/List.java @@ -10,54 +10,122 @@ public class List { /** * Subclass that describes the key-value pair. */ - public class Pair { - public String key; - public String value; + public static class Pair { + /** + * Key of pair + */ + private String key; + + /** + * Value of pair + */ + private String value; + + /** + * Creates key-value pair + * @param key key of pair + * @param value value of pair + */ public Pair(String key, String value) { this.key = key; this.value = value; } + + /** + * Returns key + * @return key + */ + public String getKey() { + return key; + } + + /** + * Returns value + * @return value + */ + public String getValue() { + return value; + } + + /** + * Sets key + * @param key the new key + */ + public void setKey(String key) { + this.key = key; + } + + /** + * Sets value + * @param value the new value + */ + public void setValue(String value) { + this.value = value; + } + } + /** * Node Subclass that describes the list's node. **/ private class Node { + /** * The key-value pair inside the node. */ private Pair pair; + /** * The next node. **/ private Node next; + /** + * Creates a node with key and value + * @param key key of node + * @param value value of node + */ private Node (String key, String value) { this.pair = new Pair(key, value); this.next = null; } } + /** * The first element of the list. **/ private Node head; + /** * Size of the list. **/ private int size; - public List() { - } + /** + * Creates empty list + */ + public List() { } + /** + * Creates List with one node + * @param key key of node + * @param value value of node + */ public List (String key, String value) { head = new Node(key, value); head.next = null; size = 1; } + /** + * Returns the size of list. + * @return size of list + **/ public int size() { return size; } + /** * Returns the value of the node with given key. * @param key The key. @@ -74,6 +142,7 @@ public String get (String key) { } while (node != null); return null; } + /** * Changes or adds the value of the given key, returns the previous value. * @param key The key @@ -101,6 +170,7 @@ public String put (String key, String value) { size++; return null; } + /** * Returns the first key-value pair of the list, and then deletes it. * @return First node, or null if the list is empty. @@ -113,6 +183,7 @@ public Pair popFront () { size--; return result; } + /** * Returns the value stored with given key and removes the key-value pair from the list. * @param key The key. @@ -139,6 +210,7 @@ public String remove (String key) { } while (node != null); return null; } + /** * Checks if there is an element with the given key in the list. * @param key The given key. diff --git a/src/ru/spbau/dkaznacheev/ListTest.java b/src/ru/spbau/dkaznacheev/ListTest.java index b55a3ad..934c0fd 100644 --- a/src/ru/spbau/dkaznacheev/ListTest.java +++ b/src/ru/spbau/dkaznacheev/ListTest.java @@ -6,82 +6,82 @@ public class ListTest { @Test - public void size() throws Exception { + public void sizeChangesOnAdd() throws Exception { List list = new List(); - assert (list.size() == 0); + assertEquals (0, list.size()); list.put("1", "v1"); - assert (list.size() == 1); + assertEquals (1, list.size()); list.put("2", "v2"); - assert (list.size() == 2); + assertEquals (2, list.size()); list.put("2", "v3"); - assert (list.size() == 2); + assertEquals (2, list.size()); } @Test - public void get() throws Exception { + public void getTest() throws Exception { List list = new List("1", "v1"); - assert(list.get("1").equals("v1")); - assert(list.get("2") == null); + assertTrue(list.get("1").equals("v1")); + assertNull(list.get("2")); } @Test - public void put() throws Exception { + public void putTest() throws Exception { List list = new List(); list.put("1", "v1"); - assert(list.size() == 1); - assert(list.contains("1")); - assert(!list.contains("v1")); + assertEquals(1, list.size()); + assertTrue(list.contains("1")); + assertTrue(!list.contains("v1")); String result = list.put("1", "v11"); - assert(result.equals("v1")); - assert(list.size() == 1); + assertTrue(result.equals("v1")); + assertEquals(1, list.size()); } @Test - public void popFront() throws Exception { + public void popFrontTest() throws Exception { List list = new List(); list.put("1", "v1"); list.put("2", "v2"); List.Pair pair = list.popFront(); - assert(pair.key.equals("1")); - assert(pair.value.equals("v1")); - assert(list.size() == 1); - assert(!list.contains("1")); + assertTrue(pair.getKey().equals("1")); + assertTrue(pair.getValue().equals("v1")); + assertEquals(1, list.size()); + assertTrue(!list.contains("1")); pair = list.popFront(); - assert(pair.key.equals("2")); - assert(pair.value.equals("v2")); - assert(list.size() == 0); + assertTrue(pair.getKey().equals("2")); + assertTrue(pair.getValue().equals("v2")); + assertEquals(0, list.size()); pair = list.popFront(); - assert(pair == null); + assertNull(pair); } @Test - public void remove() throws Exception { + public void removeTest() throws Exception { List list = new List(); list.put("1", "v1"); list.put("2", "v2"); list.put("3", "v3"); String result = list.remove("2"); - assert(result.equals("v2")); - assert(!list.contains("2")); - assert(list.size() == 2); + assertTrue(result.equals("v2")); + assertTrue(!list.contains("2")); + assertEquals(2, list.size()); result = list.remove("4"); - assert(result == null); - assert(list.size() == 2); + assertNull(result); + assertEquals(2, list.size()); list.remove("1"); list.remove("3"); - assert(list.size() == 0); + assertEquals(0, list.size()); result = list.remove("1"); - assert(result == null); - assert(list.size() == 0); + assertNull(result); + assertEquals(0, list.size()); } @Test - public void contains() throws Exception { + public void containsAdded() throws Exception { List list = new List(); list.put("1", "v1"); list.put("2", "v2"); - assert(list.contains("1")); - assert(!list.contains("3")); + assertTrue(list.contains("1")); + assertTrue(!list.contains("3")); }