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
6 changes: 6 additions & 0 deletions hw1/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions hw1/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions hw1/hw1.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" scope="TEST">
<library name="JUnit4">
<CLASSES>
<root url="jar://$APPLICATION_HOME_DIR$/lib/junit-4.12.jar!/" />
<root url="jar://$APPLICATION_HOME_DIR$/lib/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$APPLICATION_HOME_DIR$/lib/junit-4.12.jar!/" />
<root url="jar://$APPLICATION_HOME_DIR$/lib/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
157 changes: 157 additions & 0 deletions hw1/ru/spbau/solikov/hw1/src/HashTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package ru.spbau.solikov.hw1.src;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ну и зачем это тут? тут только тесты должны быть.
-1


/**
* Implementation of hash table structure using separate chaining
*/
public class HashTable {

/**
* Number used in hash function
*/
private static final int DEFAULT_HASH_PRIME_NUMBER = 31;

private List[] table;
private int capacity = 2;
private int size;

/**
* Creates a HashTable of default capacity
*/
public HashTable() {
table = new List[2];
size = 0;
}

/**
* Returns number of keys
*
* @return size
*/
public int size() {
return size;
}


/**
* Iterates on each list in HashTable and clears it, capacity is the same
*/
public void clear() {
for (List list : table) {
if (list != null) {
list.clear();
}
}
size = 0;
}

/**
* Hash function for obtaining a code from key
*
* @param key
* @return hash code
*/
private int getHash(String key) {
int hash = 0;
for (int i = 0; i < key.length(); i++) {
hash = (DEFAULT_HASH_PRIME_NUMBER * hash + key.charAt(i)) % capacity;
}
return hash;
}

/**
* Returns data stored by key or null if not found
*
* @param key
* @return data or null if not found
*/
public String get(String key) {
int hash = getHash(key);
if (table[hash] == null){
return null;
}
return table[hash].find(key);
}

/**
* Increases the capacity of HashTable by 2 times
*/
private void increase() {
List[] oldTable = table;
table = new List[2 * capacity];
capacity *= 2;

for (List list : oldTable) {
if (list != null) {
while (!list.isEmpty()) {
String key = list.getHeadsKey();
int hash = getHash(key);
if (table[hash] == null) {
table[hash] = new List();
}
table[hash].insert(key, list.getHeadsData());
list.delete(key);
}
}
}
}

/**
* Allows to add the pair of key and data into HashTable, if there's not enough space calls increase()
*
* @param key
* @param data
* @return data that was stored before by the same key or null if this space was empty
*/
public String put(String key, String data) {
int hash = getHash(key);
if (table[hash] == null) {
table[hash] = new List();
}

List current = table[hash];
String deleted = current.delete(key);
current.insert(key, data);

if (deleted == null) {
size++;
}

if (size >= capacity) {
increase();
}

return deleted;
}

/**
* Allows to remove data stored by key
*
* @param key
* @return data stored by key
*/
public String remove(String key) {
int hash = getHash(key);

if (table[hash] == null) {
return null;
}

String deleted = table[hash].delete(key);
if (deleted != null) {
size--;
return deleted;
}

return null;
}

/**
* Checks whether HashTable contains some data by passed key
*
* @param key
* @return data or null if the HashTable doesn't contain key
*/
public boolean contains(String key) {
return get(key) != null;
}
}
121 changes: 121 additions & 0 deletions hw1/ru/spbau/solikov/hw1/src/List.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package ru.spbau.solikov.hw1.src;

/**
* Implementation of single-linked list that stores Nodes of keys and data
*/
public class List {

/**
* Class that stores keys and data and has link for next Node
*/
private class Node {

private Node next;

private String key;

private String data;

public Node(Node n, String k, String d) {
next = n;
key = k;
data = d;
}
}

private Node head;

private Node tail;

public Node getHead() {
return head;
}

/**
* Adds a pair of key and data in List
*
* @param key
* @param data
*/
public void insert(String key, String data) {
if (head != null) {
tail.next = new Node(null, key, data);
tail = tail.next;
} else {
tail = head = new Node(null, key, data);
}
}

/**
* Checks if the list does not contain any elements
*
* @return boolean - true if the list is empty
*/
public boolean isEmpty() {
return head == null;
}

/**
* Searches for data by key in List
*
* @param key
* @return data or null if not found
*/
public String find(String key) {
Node current = head;
while (current != null) {
if (current.key.equals(key)) {
return current.data;
}
current = current.next;
}

return null;
}

public String getHeadsKey() {
return head.key;
}

public String getHeadsData() {
return head.data;
}

/**
* Deletes a data stored by key in List
*
* @param key
* @return data that was deleted
*/
public String delete(String key) {
if (head == null) {
return null;
}

if (head.key.equals(key)) {
String deleted = head.data;
head = head.next;
return deleted;
}

Node current = head;
while (current.next != null) {
if (current.next.key.equals(key)) {
String deleted = current.next.data;
current.next = current.next.next;
return deleted;
}
current = current.next;
}

return null;
}

/**
* Clears the List
*/
public void clear() {
head = null;
tail = null;
}
}
41 changes: 41 additions & 0 deletions hw1/ru/spbau/solikov/hw1/src/primitive/test/HashTableTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ru.spbau.solikov.hw1.src.primitive.test;

import ru.spbau.solikov.hw1.src.HashTable;

/**
* Class for testing all the HashTable functions
*/

public class HashTableTest {
public static void main(String[] args) {
HashTable hashTable = new HashTable();

for (int i = 0; i < 100; i++){
hashTable.put(String.valueOf(i), String.valueOf(i * i));
}

System.out.println(hashTable.size() + " - size of hashtable (must be 100)");

System.out.println();

System.out.println(hashTable.get("10") + " - square of 10 (must be 100)");

System.out.println();

System.out.println(hashTable.put("10", "200") + " - changed square of 10 (must have been 100)");

System.out.println();

System.out.println(hashTable.contains("10") + " - contains '10' (must be true)");

System.out.println();

System.out.println(hashTable.remove("10") + " - removed '10'");

System.out.println();

hashTable.clear();

System.out.println(hashTable.size() + " - size of hashtable (must be 0)");
}
}
Loading