<commit_before>/**
 * https://github.com/dominictarr/hashlru with Map
 * @param {number} max
 * @param {function} [onDispose]
 * @return {{set, get, clear, has, delete}}
 */
const createLRU = ({ max, onDispose }) => {
    let size = 0;
    let cache = new Map();
    let oldCache = new Map();

    const update = (key, value) => {
        cache.set(key, value);
        size++;
        if (size >= max) {
            size = 0;
            const disposedCache = cache;
            oldCache = cache;
            cache = new Map();
            onDispose && onDispose(disposedCache);
        }
    };

    return {
        has: (key) => {
            return cache.has(key) || oldCache.has(key);
        },
        delete: (key) => {
            cache.delete(key);
            oldCache.delete(key);
        },
        get: (key) => {
            if (cache.has(key)) {
                return cache.get(key);
            }
            if (oldCache.has(key)) {
                const v = oldCache.get(key);
                update(key, v);
                return v;
            }
        },
        set: (key, value) => {
            if (cache.has(key)) {
                return cache.set(key, value);
            }
            return update(key, value);
        },
        clear: () => {
            cache = new Map();
            oldCache = new Map();
        }
    };
};

export default createLRU;<commit_msg>Makes LRU cache iterrable<commit_after>/**
 * https://github.com/dominictarr/hashlru with Map
 * @param {number} max
 * @param {function} [onDispose]
 * @return {{set, get, clear, has, delete}}
 */
const createLRU = ({ max, onDispose }) => {
    let size = 0;
    let cache = new Map();
    let oldCache = new Map();

    const update = (key, value) => {
        cache.set(key, value);
        size++;
        if (size >= max) {
            size = 0;
            const disposedCache = cache;
            oldCache = cache;
            cache = new Map();
            onDispose && onDispose(disposedCache);
        }
    };

    return {
        has: (key) => {
            return cache.has(key) || oldCache.has(key);
        },
        delete: (key) => {
            cache.delete(key);
            oldCache.delete(key);
        },
        get: (key) => {
            if (cache.has(key)) {
                return cache.get(key);
            }
            if (oldCache.has(key)) {
                const v = oldCache.get(key);
                update(key, v);
                return v;
            }
        },
        set: (key, value) => {
            if (cache.has(key)) {
                return cache.set(key, value);
            }
            return update(key, value);
        },
        clear: () => {
            cache = new Map();
            oldCache = new Map();
        },
        [Symbol.iterator]() {
            return cache[Symbol.iterator]();
        }
    };
};

export default createLRU;
{'fraction_non_alphanumeric': 0.1474844934527912, 'fraction_numerical': 0.0013783597518952446, 'mean_word_length': 1.335478680611424, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 2, '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': '5506253', 'n_tokens_mistral': 871, 'n_tokens_neox': 843, 'n_words': 203}