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
49 changes: 49 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff:
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/dictionaries

# Sensitive or high-churn files:
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.xml
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml

# Gradle:
.idea/**/gradle.xml
.idea/**/libraries

# CMake
cmake-build-debug/

# Mongo Explorer plugin:
.idea/**/mongoSettings.xml

## File-based project format:
*.iws

## Plugin-specific files:

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
9 changes: 9 additions & 0 deletions .idea/HashTable.iml

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

23 changes: 23 additions & 0 deletions .idea/compiler.xml

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

1 change: 1 addition & 0 deletions .idea/description.html

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

5 changes: 5 additions & 0 deletions .idea/encodings.xml

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

12 changes: 12 additions & 0 deletions .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 .idea/modules.xml

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

7 changes: 7 additions & 0 deletions .idea/vcs.xml

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

32 changes: 32 additions & 0 deletions HashTable.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library name="JUnit5">
<CLASSES>
<root url="jar://$APPLICATION_HOME_DIR$/plugins/junit/lib/junit-jupiter-api-5.0.0-RC2.jar!/" />
<root url="jar://$APPLICATION_HOME_DIR$/plugins/junit/lib/opentest4j-1.0.0-RC1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library name="testng">
<CLASSES>
<root url="jar://$APPLICATION_HOME_DIR$/plugins/testng/lib/testng.jar!/" />
<root url="jar://$APPLICATION_HOME_DIR$/plugins/testng/lib/jcommander.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
108 changes: 108 additions & 0 deletions src/ru/spbau/mit/kazakov/HashTable/HashTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package ru.spbau.mit.kazakov.HashTable;

/**
* HashTable
* Key: String
* Value: String
* Collision resolution: Separate chaining with linked lists
*/
public class HashTable {
private int capacity = 16;
private int size = 0;
private LinkedList[] hashtable = new LinkedList[capacity];

/**
* Initialises all linked lists.
*/
public HashTable() {
for (int i = 0; i < capacity; i++) {
hashtable[i] = new LinkedList();
}
}

/**
* Returns number of keys in hashtable.
*/
public int size() {
return size;
}

/**
* Tests if the specified string is a key in this hashtable.
*/
public boolean contains(String key) {
int hash = Math.floorMod(key.hashCode(), capacity);
return hashtable[hash].contains(key);
}

/**
* Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
*/
public String get(String key) {
int hash = Math.floorMod(key.hashCode(), capacity);
return hashtable[hash].get(key);
}

/**
* Maps the specified key to the specified value in this hashtable.
* Returns previous value to which the specified key was mapped, or null if this map contained no mapping for the key.
*/
public String put(String key, String value) {
int hash = Math.floorMod(key.hashCode(), capacity);
String returnValue = hashtable[hash].put(key, value);

if (returnValue == null) {
size++;
if (size == capacity) {
rebuild();
}
}

return returnValue;
}

/**
* Removes the specified key and its corresponding value from this hashtable.
* Returns removed value, or null if this map contained no mapping for the key.
*/
public String remove(String key) {
int hash = Math.floorMod(key.hashCode(), capacity);
String returnValue = hashtable[hash].remove(key);
if (returnValue != null) {
size--;
}
return returnValue;
}

/**
* Clears this hashtable so that it contains no keys.
*/
public void clear() {
for (int i = 0; i < capacity; i++) {
hashtable[i] = new LinkedList();
}
size = 0;
}

/**
* Increases capacity by copying all entries in twice and rehashes all elements.
*/
private void rebuild() {
capacity *= 2;
size = 0;
LinkedList[] oldHashtable = hashtable;

hashtable = new LinkedList[capacity];
for (int i = 0; i < capacity; i++) {
hashtable[i] = new LinkedList();
}

for (int i = 0; i < capacity / 2; i++) {
while (!oldHashtable[i].empty()) {
put(oldHashtable[i].firstElementKey(), oldHashtable[i].firstElementValue());
oldHashtable[i].remove(oldHashtable[i].firstElementKey());
}
}
}

}
Loading