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
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea/
long-map.iml
.idea/*
*.iml
target/*
16 changes: 11 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,25 @@
<version>3.3</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
6 changes: 6 additions & 0 deletions src/main/java/de/comparus/opensource/longmap/LongMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@

public interface LongMap<V> {
V put(long key, V value);

V get(long key);

V remove(long key);

boolean isEmpty();

boolean containsKey(long key);

boolean containsValue(V value);

long[] keys();

V[] values();

long size();

void clear();
}
147 changes: 142 additions & 5 deletions src/main/java/de/comparus/opensource/longmap/LongMapImpl.java
Original file line number Diff line number Diff line change
@@ -1,43 +1,180 @@
package de.comparus.opensource.longmap;

import java.util.Objects;

public class LongMapImpl<V> implements LongMap<V> {

private static final int DEFAULT_INITIAL_CAPACITY = 16;
private static final int DEFAULT_INCREASE_CAPACITY = 2;
private static final float DEFAULT_LOAD_FACTOR = 0.75F;
private int size;
private float threshold;
private Node<V>[] table;

public LongMapImpl() {
threshold = (int) (DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
table = new Node[DEFAULT_INITIAL_CAPACITY];
}

private static class Node<V> {
private V value;
private long key;
private Node<V> next;

public Node(V value, long key, Node<V> next) {
this.value = value;
this.key = key;
this.next = next;
}
}

public V put(long key, V value) {
return null;
if (threshold < size) {
resize();
}
Node<V>[] currentTable = table;
int index = getIndex(key);
if (currentTable[index] == null) {
currentTable[index] = new Node<>(value, key, null);
size++;
return value;
}

Node<V> currentNode = currentTable[index];
while (currentNode.next != null || key == currentNode.key) {
if (key == currentNode.key) {
currentNode.value = value;
return value;
}
currentNode = currentNode.next;
}

currentNode.next = new Node<>(value, key, null);
size++;

return value;
}

public V get(long key) {
Node<V> currentNode = table[getIndex(key)];
while (currentNode != null) {
if (key == currentNode.key) {
return currentNode.value;
}
currentNode = currentNode.next;
}
return null;
}

public V remove(long key) {
int index = getIndex(key);
if (isEmpty() || table[index] == null) {
return null;
}
Node<V> currentNode = table[index];
if (currentNode.key == key) {
table[index] = currentNode.next;
size--;
return currentNode.value;
}
while (currentNode.next != null) {
if (currentNode.next.key == key) {
V value = currentNode.next.value;
currentNode.next = currentNode.next.next;
size--;
return value;
}
currentNode = currentNode.next;
}
return null;
}

public boolean isEmpty() {
return false;
return size == 0;
}

public boolean containsKey(long key) {
Node<V> currentNode = table[getIndex(key)];
while (currentNode != null) {
if (key == currentNode.key) {
return true;
}
currentNode = currentNode.next;
}
return false;
}

public boolean containsValue(V value) {
Node<V>[] currentTable = table;
for (Node<V> vNode : currentTable) {
Node<V> currentNode = vNode;
while (currentNode != null) {
if (Objects.equals(value, currentNode.value)) {
return true;
}
currentNode = currentNode.next;
}
}
return false;
}

public long[] keys() {
return null;
Node<V>[] currentTable = table;
long[] keysArray = new long[size];
int k = 0;
for (Node<V> vNode : currentTable) {
Node<V> currentNode = vNode;
while (currentNode != null) {
keysArray[k++] = currentNode.key;
currentNode = currentNode.next;
}
}
return keysArray;
}

public V[] values() {
return null;
Node<V>[] currentTable = table;
V[] valuesArray = (V[]) new Object[size];
int k = 0;
for (Node<V> vNode : currentTable) {
Node<V> currentNode = vNode;
while (currentNode != null) {
valuesArray[k++] = currentNode.value;
currentNode = currentNode.next;
}
}
return valuesArray;
}

public long size() {
return 0;
return size;
}

public void clear() {
Node<V>[] currentTable = table;
if (currentTable != null && size > 0) {
size = 0;
for (int i = 0; i < currentTable.length; i++) {
currentTable[i] = null;
}
}
}

private int getIndex(long key) {
return (int) Math.abs(key % table.length);
}

private void resize() {
Node<V>[] oldTable = table;
size = 0;
table = new Node[oldTable.length * DEFAULT_INCREASE_CAPACITY];
threshold = table.length * DEFAULT_LOAD_FACTOR;
for (int i = 0; i < oldTable.length; i++) {
Node<V> nodeOld = oldTable[i];
while (nodeOld != null) {
put(nodeOld.key, nodeOld.value);
nodeOld = nodeOld.next;
}
}
}
}
139 changes: 139 additions & 0 deletions src/main/java/de/comparus/opensource/longmap/LongMapImplTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package de.comparus.opensource.longmap;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

class LongMapImplTest {

@Test
public void testMethodSize_Ok() {
LongMap<String> map = new LongMapImpl<>();
map.put(1L, "First");
assertEquals(1, map.size());
map.put(2L, "Second");
assertEquals(2, map.size());
map.put(17L, "Third");
assertEquals(3, map.size());
map.put(-100L, "Forth");
assertEquals(4, map.size());
}

@Test
public void testMethodPut_Ok() {
LongMap<String> map = new LongMapImpl<>();
assertEquals("first", map.put(20L, "first"));
assertEquals("second", map.put(120L, "second"));
assertEquals("third", map.put(-120L, "third"));
assertEquals("forth", map.put(0L, "forth"));
assertEquals("fifth", map.put(1L, "fifth"));
assertEquals("One hundred", map.put(8L, "One hundred"));
}

@Test
public void testMethodPutForCorrectWorkSameKey_Ok() {
LongMap<Integer> map = new LongMapImpl<>();
assertEquals(10, map.put(100L, 10));
assertEquals(20, map.put(100L, 20));
assertEquals(-10020, map.put(100L, -10020));
assertNull(map.put(100L, null));
}

@Test
public void testMethodGet_Ok() {
LongMap<String> map = new LongMapImpl<>();
assertEquals(map.put(20L, "first"), map.get(20L));
assertEquals(map.put(-20L, "second"), map.get(-20L));
assertEquals(map.put(120L, "third"), map.get(120L));
assertEquals(map.put(220L, null), map.get(220L));
assertNull(map.get(-123L));
}

@Test
public void testMethodRemove_Ok() {
LongMap<String> map = new LongMapImpl<>();
assertEquals(map.put(20L, "first"), map.remove(20L));
assertEquals(map.put(-20L, "second"), map.remove(-20L));
assertEquals(map.put(120L, "third"), map.remove(120L));
assertNull(map.remove(120L));
assertEquals(map.put(220L, null), map.remove(220L));
assertNull(map.remove(-11220L));
map.put(20L, "first");
map.put(52L, "second");
assertEquals(map.put(36L, "third"), map.remove(36L));
}

@Test
public void testMethodIsEmpty_Ok() {
LongMap<String> map = new LongMapImpl<>();
assertTrue(map.isEmpty());
map.put(10L, "value");
assertFalse(map.isEmpty());
}

@Test
public void testMethodClear_Ok() {
LongMap<Integer> map = new LongMapImpl<>();
map.clear();
assertEquals(0, map.size());
map.put(10L, 1);
map.put(1L, 10);
assertNotEquals(0, map.size());
map.clear();
assertEquals(0, map.size());
}

@Test
public void testMethodContainKey_Ok() {
LongMap<Integer> map = new LongMapImpl<>();
map.put(0L, 0);
assertTrue(map.containsKey(0));
assertFalse(map.containsKey(1000L));
}

@Test
public void testMethodContainValue_Ok() {
LongMap<String> map = new LongMapImpl<>();
map.put(100L, "One");
assertTrue(map.containsValue("One"));
assertFalse(map.containsValue("Three"));
}

@Test
public void testMethodKeys_Ok() {
LongMap<String> map = new LongMapImpl<>();
map.put(20L, "first");
map.put(36L, "first");
map.put(19L, "first");
map.put(-20L, "second");
map.put(120L, "third");
map.put(220L, null);
long[] expected = new long[]{19L, 20L, 36L, -20L, 120L, 220L};
long[] actual = map.keys();
Assertions.assertArrayEquals(expected, actual);
}

@Test
public void testMethodValues_Ok() {
LongMap<Integer> map = new LongMapImpl<>();
Integer[] expected = new Integer[10];
for (int i = 0; i < 10; i++) {
map.put(i, i);
expected[i] = i;
}
Assertions.assertArrayEquals(expected, map.values());
}

@Test
public void testForResize_Ok() {
LongMap<Integer> map = new LongMapImpl<>();
for (int i = 0; i < 10000; i++) {
map.put(i, i);
}
assertEquals(10000, map.size());
}
}