diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1792d6b --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +hash/.idea +hash/hash.iml +hash/out/ \ No newline at end of file diff --git a/hash/src/HashTest.java b/hash/src/HashTest.java new file mode 100644 index 0000000..95b771c --- /dev/null +++ b/hash/src/HashTest.java @@ -0,0 +1,60 @@ +import hash.Hash; +import hash.InvalidKeyException; + +public class HashTest { + public static void main(String[] args) { + System.out.println("====== strings ======"); + System.out.println("------ display all ------"); + Hash strings = new Hash<>(); + strings.insert("one", "1"); + strings.insert("two", "2"); + strings.insert("hello", "world"); + strings.insert("hi", "wrld"); + strings.insert("Cthulhu", "Пх’нглуи мглв’нафх Ктулху Р’льех вгах’нагл фхтагн"); + strings.display(); + + System.out.println(); + System.out.println("------ display Cthulhu item ------"); + try { + System.out.println(strings.search("Cthulhu")); + } catch (InvalidKeyException e) { + e.printStackTrace(); + } + + System.out.println(); + System.out.println("====== Users ======"); + + Hash users = new Hash<>(); + users.insert("Ivanov", new User("Ivan", "Ivanov", 21)); + users.insert("Petrov", new User("Petr", "Petrov", 34)); + users.insert("Sidorov", new User("Sidor", "Sidorov", 16)); + users.insert("Rumpel", new User("Rumpelstiltskin", "", -1)); + users.display(); + + System.out.println("------ after removed 'Rumpel'------"); + + try { + users.delete("Rumpel"); + } catch (InvalidKeyException e) { + e.printStackTrace(); + } + users.display(); + + + } +} + +class User { + public String name; + public String surname; + public int age; + + public User(String name, String surname, int age) { + this.name = name; + this.surname = surname; + this.age = age; + } + public String toString() { + return String.format("name: %s\nsurname: %s\nage: %d\n", name, surname, age); + } +} diff --git a/hash/src/hash/Hash.java b/hash/src/hash/Hash.java new file mode 100644 index 0000000..f3625b2 --- /dev/null +++ b/hash/src/hash/Hash.java @@ -0,0 +1,99 @@ +package hash; + +import javax.swing.*; +import java.util.LinkedList; + +public class Hash { + private LinkedList[] buckets; + private int size; + private int elements; + + public Hash() { + size = 3; + buckets = alloc(size); + + elements = 0; + } + + public T search(String key) throws InvalidKeyException { + int hash = hashCode(key); + LinkedList bucket = buckets[hash]; + + for(Item tmpItem: bucket) { + if (tmpItem.getKey().equals(key)) { + return (T) tmpItem.getVal(); + } + } + + throw new InvalidKeyException(); + } + + public void insert(String key, T val) { + Item item = new Item(key, val); + int hash = hashCode(key); + + buckets[hash].add(item); + elements++; + + if ((elements * 100 / size) >= 70) { + realloc(); + } + } + + public Item delete(String key) throws InvalidKeyException { + int hash = hashCode(key); + LinkedList bucket = buckets[hash]; + + for(Item item: bucket) { + if (item.getKey().equals(key)) { + Item tmpItem = item; + bucket.remove(item); + elements--; + return tmpItem; + } + } + + throw new InvalidKeyException(); + } + + public void display() { + for(int i = 0; i < size; i++) { + for (Item tmpItem: buckets[i]) { + System.out.println(tmpItem); + } + } + } + + private int hashCode(String key) { + return key.length() % size; + } + + private LinkedList[] alloc(int size) { + LinkedList[] buckets = new LinkedList[size]; + + for (int i = 0; i < size; i++) { + buckets[i] = new LinkedList(); + } + + return buckets; + } + + private void realloc() { + LinkedList[] newBuckets; + int oldSize = size; + int newSize = size + 1; + + newBuckets = alloc(newSize); + size = newSize; + + for(int i = 0; i < oldSize; i++) { + for(Item tmpItem : buckets[i]) { + int hash = hashCode(tmpItem.getKey()); + newBuckets[hash].add(tmpItem); + } + } + + buckets = newBuckets; + size = newSize; + } +} diff --git a/hash/src/hash/InvalidKeyException.java b/hash/src/hash/InvalidKeyException.java new file mode 100644 index 0000000..2f0248f --- /dev/null +++ b/hash/src/hash/InvalidKeyException.java @@ -0,0 +1,4 @@ +package hash; + +public class InvalidKeyException extends Exception { +} diff --git a/hash/src/hash/Item.java b/hash/src/hash/Item.java new file mode 100644 index 0000000..8ed166f --- /dev/null +++ b/hash/src/hash/Item.java @@ -0,0 +1,23 @@ +package hash; + +public class Item { + private String key; + private T val; + + public Item(String key, T val) { + this.key = key; + this.val = val; + } + + public String getKey() { + return key; + } + + public T getVal() { + return val; + } + + public String toString() { + return String.format("key: %s, val: %s", key, val); + } +}