<commit_before><commit_msg>new problems solved<commit_after>package containers.p22;

import containers.p21.*;
import containers.p15.MapEntry;
import java.util.*;
import net.mindview.util.*;

public class SimpleHashMap<K, V> extends AbstractMap<K, V> {
// Choose a prime number for the hash table
// size, to achieve a uniform distribution:

    static final int SIZE = 997;
// You can’t have a physical array of generics,
// but you can upcast to one:
    @SuppressWarnings("unchecked")
    LinkedList<MapEntry<K, V>>[] buckets = new LinkedList[SIZE];

    @Override
    public V put(K key, V value) {
        V oldValue = null;
        int index = getIndex((K)key);
        if (buckets[index] == null) {
            buckets[index] = new LinkedList<>();
        }
        LinkedList<MapEntry<K, V>> bucket = buckets[index];
        MapEntry<K, V> pair = new MapEntry<>(key, value);
        boolean found = false;
        ListIterator<MapEntry<K, V>> it = bucket.listIterator();
        while (it.hasNext()) {
            MapEntry<K, V> iPair = it.next();

            if (iPair.getKey().equals(key)) {
                oldValue = iPair.getValue();
                it.set(pair); // Replace old with new
                found = true;
                break;
            }
        }
        if (!found) {
            buckets[index].add(pair);
        }
        return oldValue;
    }

    @Override
    public V get(Object key) {
        int index = getIndex((K)key);
        if (buckets[index] == null) {
            return null;
        }
        for (MapEntry<K, V> iPair : buckets[index]) {
            if (iPair.getKey().equals(key)) {
                return iPair.getValue();
            }
        }
        return null;
    }
    @Override
    public V remove(Object key) {
        V oldValue = null;
        List<MapEntry<K, V>> bucket = buckets[getIndex((K)key)];
        if (bucket != null) {
            for (MapEntry<K, V> iPair : bucket) {
                if(iPair.getKey().equals(key)) {
                    oldValue = iPair.getValue();
                    bucket.remove(iPair);
                    break;
                }
            }
        }
        return oldValue;
    }

    @Override
    public Set<Map.Entry<K, V>> entrySet() {
        Set<Map.Entry<K, V>> set = new HashSet<>();
        for (LinkedList<MapEntry<K, V>> bucket : buckets) {
            if (bucket == null) {
                continue;
            }
            for (MapEntry<K, V> mpair : bucket) {
                set.add(mpair);
            }
        }
        return set;
    }

    @Override
    public void clear() {
        buckets = new LinkedList[SIZE];
    }
    private int getIndex(K key) {
        return Math.abs(key.hashCode()) % SIZE;
    }

    public static void main(String[] args) {
        SimpleHashMap<String, String> m = new SimpleHashMap<>();
        m.putAll(Countries.capitals(25));
        System.out.println(m);
        m.remove("CHAD");
        m.remove("CHAD");
        m.remove("KENYA");
        System.out.println("m.get(\"KENYA\") = " + m.get("KENYA"));
        System.out.println(m);
        m.clear();
        System.out.println(m);
    }
}
{'fraction_non_alphanumeric': 0.13575949367088608, 'fraction_numerical': 0.003481012658227848, 'mean_word_length': 1.7391681109185442, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1994382', 'n_tokens_mistral': 969, 'n_tokens_neox': 938, 'n_words': 229}