<commit_before>package store.server;

import store.common.Hash;

public abstract class Table<T> {

    private static final char[] ALPHABET = new char[]{'0', '1', '2', '3',
                                                      '4', '5', '6', '7',
                                                      '8', '9', 'a', 'b',
                                                      'c', 'd', 'e', 'f'};
    private final int keyLength;
    private final Object[] buckets;

    @SuppressWarnings("OverridableMethodCallInConstructor")
    public Table(int keyLength) {
        this.keyLength = keyLength;
        buckets = new Object[1 << (4 * keyLength)];
        for (int i = 0; i < buckets.length; i++) {
            buckets[i] = initialValue(keyOf(i, keyLength));
        }
    }

    protected abstract T initialValue(String key);

    @SuppressWarnings("unchecked")
    public T get(Hash hash) {
        int index = indexOf(hash.encode().substring(0, keyLength));
        return (T) buckets[index];
    }

    private static int indexOf(String key) {
        int index = 0;
        for (int i = 0; i < key.length(); i++) {
            index += pow(key.length() - i - 1) * position(key.charAt(i));
        }
        return index;
    }

    private static String keyOf(int index, int keyLength) {
        String hex = Integer.toHexString(index);
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < keyLength - hex.length(); i++) {
            builder.append('0');
        }
        return builder.append(hex).toString();
    }

    private static int pow(int i) {
        return 1 << (4 * i);
    }

    private static int position(char c) {
        for (int i = 0; i < ALPHABET.length; i++) {
            if (ALPHABET[i] == c) {
                return i;
            }
        }
        throw new IllegalArgumentException(String.valueOf(c));
    }
}<commit_msg>Refactoring interne de Table.<commit_after>package store.server;

import store.common.Hash;

public abstract class Table<T> {

    private final int keyLength;
    private final Object[] buckets;

    @SuppressWarnings("OverridableMethodCallInConstructor")
    public Table(int keyLength) {
        this.keyLength = keyLength;
        buckets = new Object[1 << (4 * keyLength)];
        for (int i = 0; i < buckets.length; i++) {
            buckets[i] = initialValue(keyOf(i, keyLength));
        }
    }

    protected abstract T initialValue(String key);

    @SuppressWarnings("unchecked")
    public T get(Hash hash) {
        String key = hash.encode().substring(0, keyLength);
        int index = Integer.parseInt(key, 16);
        return (T) buckets[index];
    }

    private static String keyOf(int index, int keyLength) {
        String hex = Integer.toHexString(index);
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < keyLength - hex.length(); i++) {
            builder.append('0');
        }
        return builder.append(hex).toString();
    }
}
{'fraction_non_alphanumeric': 0.13020134228187918, 'fraction_numerical': 0.010067114093959731, 'mean_word_length': 2.0080726538849647, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5622842', 'n_tokens_mistral': 864, 'n_tokens_neox': 840, 'n_words': 238}