Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

13 lesson #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
32 changes: 5 additions & 27 deletions src/main/java/io/sidorovgreg/otus/App.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,11 @@
package io.sidorovgreg.otus;

import java.lang.reflect.Constructor;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;
import java.util.stream.IntStream;

public class App {
public static void main(String[] args) throws Exception {
Class<?> clazz = Class.forName("io.sidorovgreg.otus.tasks." + args[0]);
Constructor<?> ctor = clazz.getConstructor();
Task task = (Task) ctor.newInstance();
String path = args[1];
for (int i = 0; true; i++) {
try {
List<String> in = Files.readAllLines(Paths.get(String.format("%s/test.%s.in", path, i)));
String out = Files.readAllLines(Paths.get(String.format("%s/test.%s.out", path, i))).stream()
.findFirst()
.orElseThrow(() -> new RuntimeException("expected result isn't exist"));
String result = task.execute(in);
if (Objects.equals(result, out)) {
System.out.printf("[%s] success%n", in);
} else {
System.out.printf("[%s] fail: got %s, expected %s %n", in, result, out);
}
} catch (NoSuchFileException e) {
break;
}
}
public static void main(String[] args) {
CustomMap<Integer, Double> map = new CustomMap<>();
IntStream.range(0, 100).forEach(i -> map.put(i, Math.pow(10, i)));
System.out.println(map.get(99));
}
}
120 changes: 120 additions & 0 deletions src/main/java/io/sidorovgreg/otus/CustomMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package io.sidorovgreg.otus;

import java.util.ArrayList;
import java.util.Objects;

public class CustomMap<K, V> {
private ArrayList<CustomMapNode<K, V>> bucketArray;

private int numBuckets;

private int size;

public CustomMap() {
bucketArray = new ArrayList<>();
numBuckets = 10;
size = 0;

// Create empty chains
for (int i = 0; i < numBuckets; i++)
bucketArray.add(null);
}

public int size() {
return size;
}

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

private int hashCode(K key) {
return Objects.hashCode(key);
}

private int getBucketIndex(K key) {
int hashCode = hashCode(key);
int index = hashCode % numBuckets;
index = index < 0 ? index * -1 : index;
return index;
}

public V del(K key) {
int bucketIndex = getBucketIndex(key);
int hashCode = hashCode(key);
CustomMapNode<K, V> head = bucketArray.get(bucketIndex);

CustomMapNode<K, V> prev = null;
while (head != null) {
if (head.key.equals(key) && hashCode == head.hashCode)
break;

prev = head;
head = head.next;
}

if (head == null)
return null;

size--;

if (prev != null)
prev.next = head.next;
else
bucketArray.set(bucketIndex, head.next);

return head.value;
}

public V get(K key) {
int bucketIndex = getBucketIndex(key);
int hashCode = hashCode(key);

CustomMapNode<K, V> head = bucketArray.get(bucketIndex);

while (head != null) {
if (head.key.equals(key) && head.hashCode == hashCode)
return head.value;
head = head.next;
}

return null;
}

public void put(K key, V value) {
int bucketIndex = getBucketIndex(key);
int hashCode = hashCode(key);
CustomMapNode<K, V> head = bucketArray.get(bucketIndex);

while (head != null) {
if (head.key.equals(key) && head.hashCode == hashCode) {
head.value = value;
return;
}
head = head.next;
}

size++;
head = bucketArray.get(bucketIndex);
CustomMapNode<K, V> newNode
= new CustomMapNode<>(key, value, hashCode);
newNode.next = head;
bucketArray.set(bucketIndex, newNode);

if ((1.0 * size) / numBuckets >= 0.7) {
ArrayList<CustomMapNode<K, V>> temp = bucketArray;
bucketArray = new ArrayList<>();
numBuckets = 2 * numBuckets;
size = 0;
for (int i = 0; i < numBuckets; i++)
bucketArray.add(null);

for (CustomMapNode<K, V> headNode : temp) {
while (headNode != null) {
put(headNode.key, headNode.value);
headNode = headNode.next;
}
}
}
}
}
18 changes: 18 additions & 0 deletions src/main/java/io/sidorovgreg/otus/CustomMapNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.sidorovgreg.otus;

public class CustomMapNode<K, V> {
K key;
V value;
final int hashCode;

// Reference to next node
CustomMapNode<K, V> next;

// Constructor
public CustomMapNode(K key, V value, int hashCode)
{
this.key = key;
this.value = value;
this.hashCode = hashCode;
}
}