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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
hash/.idea
hash/hash.iml
hash/out/
60 changes: 60 additions & 0 deletions hash/src/HashTest.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<User> 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);
}
}
99 changes: 99 additions & 0 deletions hash/src/hash/Hash.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package hash;

import javax.swing.*;
import java.util.LinkedList;

public class Hash<T> {
private LinkedList<Item>[] 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<Item> 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<Item> 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<Item>[] alloc(int size) {
LinkedList<Item>[] buckets = new LinkedList[size];

for (int i = 0; i < size; i++) {
buckets[i] = new LinkedList<Item>();
}

return buckets;
}

private void realloc() {
LinkedList<Item>[] 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;
}
}
4 changes: 4 additions & 0 deletions hash/src/hash/InvalidKeyException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package hash;

public class InvalidKeyException extends Exception {
}
23 changes: 23 additions & 0 deletions hash/src/hash/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package hash;

public class Item<T> {
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);
}
}