Skip to content
Snippets Groups Projects
Commit 8a62da96 authored by Amaury Vanoorenberghe's avatar Amaury Vanoorenberghe :scroll:
Browse files

Q2.5 - Généralisation des tables de hashage (HashTable<K, V>)

parent 2fb945c5
Branches
No related tags found
No related merge requests found
...@@ -3,23 +3,24 @@ package tp4.ex2; ...@@ -3,23 +3,24 @@ package tp4.ex2;
import java.util.Arrays; import java.util.Arrays;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class Adressage implements HashTable { public class Adressage<K, V> implements HashTable<K, V> {
private static final int NOT_FOUND = -1; private static final int NOT_FOUND = -1;
public static final int DEFAULT_SIZE = 2048; public static final int DEFAULT_SIZE = 2048;
private HashCouple[] table; private HashCouple<K, V>[] table;
private int currentSize = 0; private int currentSize = 0;
public Adressage() { public Adressage() {
this(DEFAULT_SIZE); this(DEFAULT_SIZE);
} }
@SuppressWarnings("unchecked")
public Adressage(int capacity) { public Adressage(int capacity) {
table = new HashCouple[capacity]; table = new HashCouple[capacity];
} }
@Override @Override
public Integer get(String key) { public V get(K key) {
final int idx = find(key); final int idx = find(key);
if (idx != NOT_FOUND) { if (idx != NOT_FOUND) {
...@@ -30,30 +31,30 @@ public class Adressage implements HashTable { ...@@ -30,30 +31,30 @@ public class Adressage implements HashTable {
} }
@Override @Override
public Integer put(String key, Integer value) { public V put(K key, V value) {
final int hash = getHash(key); final int hash = getHash(key);
for (int idx = hash, cpt = 0; cpt < table.length; idx = next(idx), ++cpt) { for (int idx = hash, cpt = 0; cpt < table.length; idx = next(idx), ++cpt) {
if (table[idx] == null) { if (table[idx] == null) {
table[idx] = new HashCouple(key, value); table[idx] = new HashCouple<K, V>(key, value);
++currentSize; ++currentSize;
return null; return null;
} else if (table[idx].getKey().equals(key)) { } else if (table[idx].getKey().equals(key)) {
Integer oldVal = table[idx].getValue(); V oldVal = table[idx].getValue();
table[idx].setValue(value); table[idx].setValue(value);
return oldVal; return oldVal;
} }
} }
return null; throw new IllegalStateException();
} }
@Override @Override
public Integer remove(String key) { public V remove(K key) {
final int idx = find(key); final int idx = find(key);
if (idx != NOT_FOUND) { if (idx != NOT_FOUND) {
final HashCouple c = table[idx]; final HashCouple<K, V> c = table[idx];
table[idx] = null; table[idx] = null;
--currentSize; --currentSize;
...@@ -64,14 +65,14 @@ public class Adressage implements HashTable { ...@@ -64,14 +65,14 @@ public class Adressage implements HashTable {
} }
@Override @Override
public boolean contains(String key) { public boolean contains(K key) {
return find(key) != NOT_FOUND; return find(key) != NOT_FOUND;
} }
@Override @Override
public void clear() { public void clear() {
currentSize = 0; currentSize = 0;
Arrays.fill(table, (HashCouple)null); Arrays.fill(table, (HashCouple<K, V>)null);
} }
@Override @Override
...@@ -79,12 +80,12 @@ public class Adressage implements HashTable { ...@@ -79,12 +80,12 @@ public class Adressage implements HashTable {
return currentSize; return currentSize;
} }
private int find(String key) { private int find(K key) {
final int hash = getHash(key); final int hash = getHash(key);
int idx = hash, loop = 0; int idx = hash, loop = 0;
HashCouple c = null; HashCouple<K, V> c = null;
// TODO: Optimiser en transformant en while si possible // TODO: Optimiser en transformant en while si possible
for (;loop < table.length; ++loop) { for (;loop < table.length; ++loop) {
...@@ -110,7 +111,7 @@ public class Adressage implements HashTable { ...@@ -110,7 +111,7 @@ public class Adressage implements HashTable {
return result; return result;
} }
private int getHash(String key) { private int getHash(K key) {
return Math.abs(key.hashCode()) % table.length; return Math.abs(key.hashCode()) % table.length;
} }
......
...@@ -6,9 +6,9 @@ import java.util.LinkedList; ...@@ -6,9 +6,9 @@ import java.util.LinkedList;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public class Chainage implements HashTable { public class Chainage<K, V> implements HashTable<K, V> {
public static final int DEFAULT_SIZE = 16; public static final int DEFAULT_SIZE = 16;
private LinkedList<HashCouple>[] table; private LinkedList<HashCouple<K, V>>[] table;
private int currentSize = 0; private int currentSize = 0;
public Chainage() { public Chainage() {
...@@ -19,38 +19,38 @@ public class Chainage implements HashTable { ...@@ -19,38 +19,38 @@ public class Chainage implements HashTable {
table = new LinkedList[capacity]; table = new LinkedList[capacity];
for (int i = 0; i < capacity; ++i) { for (int i = 0; i < capacity; ++i) {
table[i] = new LinkedList<HashCouple>(); table[i] = new LinkedList<HashCouple<K, V>>();
} }
} }
@Override @Override
public Integer put(String key, Integer value) { public V put(K key, V value) {
HashCouple c = find(key); HashCouple<K, V> c = find(key);
if (c == null) { if (c == null) {
final int hash = getHash(key); final int hash = getHash(key);
c = new HashCouple(key, value); c = new HashCouple<K, V>(key, value);
table[hash].add(c); table[hash].add(c);
++currentSize; ++currentSize;
return null; return null;
} else { } else {
int old = c.getValue(); V old = c.getValue();
c.setValue(value); c.setValue(value);
return old; return old;
} }
} }
@Override @Override
public Integer get(String key) { public V get(K key) {
final HashCouple c = find(key); final HashCouple<K, V> c = find(key);
return c != null ? c.getValue() : null; return c != null ? c.getValue() : null;
} }
@Override @Override
public Integer remove(String key) { public V remove(K key) {
final int hash = getHash(key); final int hash = getHash(key);
final HashCouple c = find(key); final HashCouple<K, V> c = find(key);
if (c != null) { if (c != null) {
table[hash].remove(c); table[hash].remove(c);
...@@ -62,12 +62,12 @@ public class Chainage implements HashTable { ...@@ -62,12 +62,12 @@ public class Chainage implements HashTable {
} }
@Override @Override
public boolean contains(String key) { public boolean contains(K key) {
return find(key) != null; return find(key) != null;
} }
public void clear() { public void clear() {
for (LinkedList<HashCouple> list : table) { for (LinkedList<HashCouple<K, V>> list : table) {
list.clear(); list.clear();
} }
...@@ -79,18 +79,18 @@ public class Chainage implements HashTable { ...@@ -79,18 +79,18 @@ public class Chainage implements HashTable {
return currentSize; return currentSize;
} }
private HashCouple find(String key) { private HashCouple<K, V> find(K key) {
final int hash = getHash(key); final int hash = getHash(key);
Iterator<HashCouple> it = table[hash].iterator(); Iterator<HashCouple<K, V>> it = table[hash].iterator();
HashCouple c = null; HashCouple<K, V> c = null;
while (it.hasNext() && !(c = it.next()).getKey().equals(key)); while (it.hasNext() && !(c = it.next()).getKey().equals(key));
return (c != null && c.getKey().equals(key)) ? c : null; return (c != null && c.getKey().equals(key)) ? c : null;
} }
private int getHash(String key) { private int getHash(K key) {
return Math.abs(key.hashCode()) % table.length; return Math.abs(key.hashCode()) % table.length;
} }
......
...@@ -2,10 +2,10 @@ package tp4.ex2; ...@@ -2,10 +2,10 @@ package tp4.ex2;
import java.util.AbstractMap; import java.util.AbstractMap;
public class HashCouple extends AbstractMap.SimpleEntry<String, Integer> { public class HashCouple<K, V> extends AbstractMap.SimpleEntry<K, V> {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public HashCouple(String key, Integer value) { public HashCouple(K key, V value) {
super(key, value); super(key, value);
} }
...@@ -14,6 +14,7 @@ public class HashCouple extends AbstractMap.SimpleEntry<String, Integer> { ...@@ -14,6 +14,7 @@ public class HashCouple extends AbstractMap.SimpleEntry<String, Integer> {
return getKey().hashCode(); return getKey().hashCode();
} }
@SuppressWarnings("unchecked")
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) { if (this == obj) {
...@@ -27,7 +28,7 @@ public class HashCouple extends AbstractMap.SimpleEntry<String, Integer> { ...@@ -27,7 +28,7 @@ public class HashCouple extends AbstractMap.SimpleEntry<String, Integer> {
return false; return false;
} }
HashCouple other = (HashCouple)obj; HashCouple<K, V> other = (HashCouple<K, V>)obj;
if (!getKey().equals(other.getKey())) { if (!getKey().equals(other.getKey())) {
return false; return false;
......
package tp4.ex2; package tp4.ex2;
public interface HashTable { public interface HashTable<K, V> {
public Integer get(String key); public V get(K key);
public Integer put(String key, Integer value); public V put(K key, V value);
public Integer remove(String key); public V remove(K key);
public boolean contains(String key); public boolean contains(K key);
public void clear(); public void clear();
public int size(); public int size();
} }
...@@ -14,14 +14,14 @@ public class HashTableTest { ...@@ -14,14 +14,14 @@ public class HashTableTest {
protected static Stream<Arguments> arguments() { protected static Stream<Arguments> arguments() {
return Stream return Stream
.of( .of(
Arguments.of(new Chainage()), Arguments.of(new Chainage<String, Integer>()),
Arguments.of(new Adressage()) Arguments.of(new Adressage<String, Integer>())
); );
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("arguments") @MethodSource("arguments")
protected void testToString(HashTable table) { protected void testToString(HashTable<String, Integer> table) {
table.put("def", 2); table.put("def", 2);
table.put("ghi", 3); table.put("ghi", 3);
table.put("abc", 1); table.put("abc", 1);
...@@ -31,7 +31,7 @@ public class HashTableTest { ...@@ -31,7 +31,7 @@ public class HashTableTest {
@ParameterizedTest @ParameterizedTest
@MethodSource("arguments") @MethodSource("arguments")
protected void testPut(HashTable table) { protected void testPut(HashTable<String, Integer> table) {
assertEquals(null, table.put("abc", 1)); assertEquals(null, table.put("abc", 1));
assertEquals(1, table.put("abc", 2)); assertEquals(1, table.put("abc", 2));
assertEquals(null, table.put("def", 8)); assertEquals(null, table.put("def", 8));
...@@ -41,7 +41,7 @@ public class HashTableTest { ...@@ -41,7 +41,7 @@ public class HashTableTest {
@ParameterizedTest @ParameterizedTest
@MethodSource("arguments") @MethodSource("arguments")
protected void testRemove(HashTable table) { protected void testRemove(HashTable<String, Integer> table) {
table.put("abc", 123); table.put("abc", 123);
table.put("def", 456); table.put("def", 456);
table.put("ghi", 789); table.put("ghi", 789);
...@@ -53,7 +53,7 @@ public class HashTableTest { ...@@ -53,7 +53,7 @@ public class HashTableTest {
@ParameterizedTest @ParameterizedTest
@MethodSource("arguments") @MethodSource("arguments")
protected void testGet(HashTable table) { protected void testGet(HashTable<String, Integer> table) {
table.put("abc", 123); table.put("abc", 123);
table.put("def", 456); table.put("def", 456);
table.put("ghi", 789); table.put("ghi", 789);
...@@ -67,7 +67,7 @@ public class HashTableTest { ...@@ -67,7 +67,7 @@ public class HashTableTest {
@ParameterizedTest @ParameterizedTest
@MethodSource("arguments") @MethodSource("arguments")
protected void testSize(HashTable table) { protected void testSize(HashTable<String, Integer> table) {
assertEquals(0, table.size()); assertEquals(0, table.size());
table.put("abc", 100); table.put("abc", 100);
...@@ -88,7 +88,7 @@ public class HashTableTest { ...@@ -88,7 +88,7 @@ public class HashTableTest {
@ParameterizedTest @ParameterizedTest
@MethodSource("arguments") @MethodSource("arguments")
protected void testClear(HashTable table) { protected void testClear(HashTable<String, Integer> table) {
assertEquals(0, table.size()); assertEquals(0, table.size());
table.put("a", 1); table.put("a", 1);
...@@ -105,7 +105,7 @@ public class HashTableTest { ...@@ -105,7 +105,7 @@ public class HashTableTest {
@ParameterizedTest @ParameterizedTest
@MethodSource("arguments") @MethodSource("arguments")
protected void testContains(HashTable table) { protected void testContains(HashTable<String, Integer> table) {
assertFalse(table.contains("abc")); assertFalse(table.contains("abc"));
table.put("abc", 100); table.put("abc", 100);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment