This repository has been archived by the owner on Dec 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
525 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/main/java/company/vk/polis/ads/hash/DoubleHashingMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package company.vk.polis.ads.hash; | ||
|
||
import java.util.function.BiConsumer; | ||
|
||
import org.jetbrains.annotations.Nullable; | ||
|
||
/** | ||
* Map implementation with double hashing collision resolution approach | ||
* | ||
* @param <K> key | ||
* @param <V> value | ||
*/ | ||
public final class DoubleHashingMap<K, V> implements Map<K, V> { | ||
// Do not edit these 3 instance fields!!! | ||
private K[] keys; | ||
private V[] values; | ||
private boolean[] removed; | ||
|
||
/** | ||
* Создает новый ассоциативный массив в соответствии с expectedMaxSize и loadFactor. | ||
* Сразу выделяет начальное количество памяти на основе expectedMaxSize и loadFactor. | ||
* | ||
* @param expectedMaxSize ожидаемое максимальное количество элементов в ассоциативном массие. | ||
* Это значит, что capacity - размер массивов под капотом - | ||
* не будет увеличиваться до тех пор, пока количество элементов | ||
* не станет больше чем expectedMaxSize | ||
* @param loadFactor отношение количества элементов к размеру массивов | ||
*/ | ||
public DoubleHashingMap(int expectedMaxSize, float loadFactor) { | ||
keys = allocate(0); | ||
values = allocate(0); | ||
removed = new boolean[0]; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public boolean containsKey(K key) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public V get(K key) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
/** | ||
* Если capacity * loadFactor == size() и будет добавлен новый ключ, | ||
* то нужно выполнить расширение массивов | ||
*/ | ||
@Nullable | ||
@Override | ||
public V put(K key, V value) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public V remove(K key) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void forEach(BiConsumer<K, V> consumer) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private static <T> T[] allocate(int capacity) { | ||
return (T[]) new Object[capacity]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package company.vk.polis.ads.hash; | ||
|
||
import java.util.function.BiConsumer; | ||
|
||
import org.jetbrains.annotations.Nullable; | ||
|
||
/** | ||
* Map aka Dictionary or Associative array | ||
* | ||
* @param <K> key | ||
* @param <V> value | ||
*/ | ||
public interface Map<K, V> { | ||
int size(); | ||
|
||
default boolean isEmpty() { | ||
return size() == 0; | ||
} | ||
|
||
/** | ||
* Checks if key is present. | ||
* | ||
* @param key key | ||
* @return true if key is present and false otherwise | ||
*/ | ||
boolean containsKey(K key); | ||
|
||
/** | ||
* Returns value associated with key | ||
* | ||
* @param key key | ||
* @return value associated with key | ||
*/ | ||
@Nullable | ||
V get(K key); | ||
|
||
/** | ||
* Puts key and value associated with it | ||
* | ||
* @param key key | ||
* @param value value | ||
* @return old value associated with key if one present or null otherwise | ||
*/ | ||
@Nullable | ||
V put(K key, V value); | ||
|
||
/** | ||
* Removes value associated with key | ||
* | ||
* @param key key | ||
* @return value removed from map if one was or null otherwise | ||
*/ | ||
@Nullable | ||
V remove(K key); | ||
|
||
/** | ||
* Iterates over map and passes key-value pairs to consumer | ||
* | ||
* @param consumer object that consumes key-value pairs | ||
*/ | ||
void forEach(BiConsumer<K, V> consumer); | ||
} |
85 changes: 85 additions & 0 deletions
85
src/main/java/company/vk/polis/ads/hash/SeparateChainingMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package company.vk.polis.ads.hash; | ||
|
||
import java.util.function.BiConsumer; | ||
|
||
import org.jetbrains.annotations.Nullable; | ||
|
||
/** | ||
* Map implementation with separate chaining collision resolution approach | ||
* | ||
* @param <K> key | ||
* @param <V> value | ||
*/ | ||
public final class SeparateChainingMap<K, V> implements Map<K, V> { | ||
// Do not edit this field!!! | ||
private Node<K, V>[] array; | ||
|
||
/** | ||
* Создает новый ассоциативный массив в соответствии с expectedMaxSize и loadFactor. | ||
* Сразу выделяет начальное количество памяти на основе expectedMaxSize и loadFactor. | ||
* | ||
* @param expectedMaxSize ожидаемое максимальное количество элементов в ассоциативном массие. | ||
* Это значит, что capacity - размер массива связных списков - | ||
* не будет увеличиваться до тех пор, пока количество элементов | ||
* не станет больше чем expectedMaxSize | ||
* @param loadFactor отношение количества элементов к размеру массива связных списков | ||
*/ | ||
public SeparateChainingMap(int expectedMaxSize, float loadFactor) { | ||
array = allocate(0); | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public int size() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public boolean containsKey(K key) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public V get(K key) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
/** | ||
* Если capacity * loadFactor == size() и будет добавлен новый ключ, | ||
* то нужно выполнить расширение массивов | ||
*/ | ||
@Nullable | ||
@Override | ||
public V put(K key, V value) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public V remove(K key) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void forEach(BiConsumer<K, V> consumer) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private static <K, V> Node<K, V>[] allocate(int capacity) { | ||
return (Node<K, V>[]) new Node[capacity]; | ||
} | ||
|
||
private static final class Node<K, V> { | ||
K key; | ||
V value; | ||
Node<K, V> prev; | ||
Node<K, V> next; | ||
|
||
Node(K key, V value) { | ||
this.key = key; | ||
this.value = value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
@ParametersAreNonnullByDefault | ||
package company.vk.polis.ads.hash; | ||
|
||
import javax.annotation.ParametersAreNonnullByDefault; |
53 changes: 53 additions & 0 deletions
53
src/test/java/company/vk/polis/ads/hash/HashMapMapImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package company.vk.polis.ads.hash; | ||
|
||
import java.util.HashMap; | ||
import java.util.Objects; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.BiFunction; | ||
|
||
import org.jetbrains.annotations.Nullable; | ||
|
||
final class HashMapMapImpl<K, V> implements Map<K, V> { | ||
private final java.util.Map<K, V> hashMap; | ||
|
||
HashMapMapImpl(BiFunction<Integer, Float, java.util.Map<K, V>> m, int capacity, float loadFactor) { | ||
hashMap = m.apply(capacity, loadFactor); | ||
} | ||
|
||
HashMapMapImpl(int capacity, float loadFactor) { | ||
this(HashMap::new, capacity, loadFactor); | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return hashMap.size(); | ||
} | ||
|
||
@Override | ||
public boolean containsKey(K key) { | ||
return hashMap.containsKey(Objects.requireNonNull(key)); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public V get(K key) { | ||
return hashMap.get(Objects.requireNonNull(key)); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public V put(K key, V value) { | ||
return hashMap.put(Objects.requireNonNull(key), Objects.requireNonNull(value)); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public V remove(K key) { | ||
return hashMap.remove(Objects.requireNonNull(key)); | ||
} | ||
|
||
@Override | ||
public void forEach(BiConsumer<K, V> consumer) { | ||
hashMap.forEach(consumer); | ||
} | ||
} |
Oops, something went wrong.