<commit_before>package kotlin.collections

class HashMap<K, V> private constructor(
        private var keysArray: Array<K>,
        private var valuesArray: Array<V>?, // allocated only when actually used, always null in pure HashSet
        private var presenceArray: IntArray,
        private var hashArray: IntArray,
        private var maxProbeDistance: Int,
        private var length: Int
) : MutableMap<K, V> {
    private var hashShift: Int = computeShift(hashSize)

    override var size: Int = 0
        private set

    private var keysView: HashSet<K>? = null
    private var valuesView: HashMapValues<V>? = null
    private var entriesView: HashMapEntrySet<K, V>? = null

    // ---------------------------- functions ----------------------------

    constructor() : this(INITIAL_CAPACITY)

    constructor(capacity: Int) : this(
            arrayOfUninitializedElements(capacity),
            null,
            IntArray(capacity),
            IntArray(computeHashSize(capacity)),
            INITIAL_MAX_PROBE_DISTANCE,
            0)

    constructor(m: Map<K, V>) : this(m.size) {
        putAll(m)
    }

    override fun isEmpty(): Boolean = size == 0
    override fun containsKey(key: K): Boolean = findKey(key) >= 0
    override fun containsValue(value: V): Boolean = findValue(value) >= 0

    override fun get(key: K): V? {
        val index = findKey(key)
        if (index < 0) return null
        return valuesArray!![index]
    }

    override fun put(key: K, value: V): V? {
        val index = addKey(key)
        val valuesArray = allocateValuesArray()
        if (index < 0) {
            val oldValue = valuesArray[-index - 1]
            valuesArray[-index - 1] = value
            return oldValue
        } else {
            valuesArray[index] = value
            return null
        }
    }

    override fun putAll(from: Map<out K, V>) {
        putAllEntries(from.entries)
    }

    override fun remove(key: K): V? {
        val index = removeKey(key)
        if (index < 0) return null
        val valuesArray = valuesArray!!
        val oldValue = valuesArray[index]
        valuesArray.resetAt(index)
        return oldValue
    }

    override fun clear() {
        // O(length) implementation for hashArray cleanup
        for (i in 0..length - 1) {
            val hash = presenceArray[i]
            if (hash >= 0) {
                hashArray[hash] = 0
                presenceArray[i] = TOMBSTONE
            }
        }
        keysArray.resetRange(0, length)
        valuesArray?.resetRange(0, length)
        size = 0
        length = 0
    }

    override val keys: MutableSet<K> get() {
        val cur = keysView
        return if (cur == null) {
            val new = HashSet(this)
            keysView = new
            new
        } else cur
    }

    override val values: MutableCollection<V> get() {
        val cur = valuesView
        return if (cur == null) {
            val new = HashMapValues(this)
            valuesView = new
            new
        } else cur
    }

    override val entries: MutableSet<MutableMap.MutableEntry<K, V>> get() {
        val cur = entriesView
        return if (cur == null) {
            val new = HashMapEntrySet(this)
            entriesView = new
            return new
        } else cur
    }

    override fun equals(other: Any?): Boolean {
        return other === this ||
                (other is Map<*, *>) &&
                        contentEquals(other)
    }

    override fun hashCode(): Int {
        var result = 0
        val it = entriesIterator()
        while (it.hasNext()) {
            result += it.nextHashCode()
        }
        return result
    }

    override fun toString(): String {
        val sb = StringBuilder(2 + size * 3)
        sb.append("{")
        var i = 0
        val it = entriesIterator()<commit_msg>stdlib: disable HashMap views caching because it leads to memory leaks<commit_after>package kotlin.collections

class HashMap<K, V> private constructor(
        private var keysArray: Array<K>,
        private var valuesArray: Array<V>?, // allocated only when actually used, always null in pure HashSet
        private var presenceArray: IntArray,
        private var hashArray: IntArray,
        private var maxProbeDistance: Int,
        private var length: Int
) : MutableMap<K, V> {
    private var hashShift: Int = computeShift(hashSize)

    override var size: Int = 0
        private set

    // private var keysView: HashSet<K>? = null
    // private var valuesView: HashMapValues<V>? = null
    // private var entriesView: HashMapEntrySet<K, V>? = null

    // ---------------------------- functions ----------------------------

    constructor() : this(INITIAL_CAPACITY)

    constructor(capacity: Int) : this(
            arrayOfUninitializedElements(capacity),
            null,
            IntArray(capacity),
            IntArray(computeHashSize(capacity)),
            INITIAL_MAX_PROBE_DISTANCE,
            0)

    constructor(m: Map<K, V>) : this(m.size) {
        putAll(m)
    }

    override fun isEmpty(): Boolean = size == 0
    override fun containsKey(key: K): Boolean = findKey(key) >= 0
    override fun containsValue(value: V): Boolean = findValue(value) >= 0

    override fun get(key: K): V? {
        val index = findKey(key)
        if (index < 0) return null
        return valuesArray!![index]
    }

    override fun put(key: K, value: V): V? {
        val index = addKey(key)
        val valuesArray = allocateValuesArray()
        if (index < 0) {
            val oldValue = valuesArray[-index - 1]
            valuesArray[-index - 1] = value
            return oldValue
        } else {
            valuesArray[index] = value
            return null
        }
    }

    override fun putAll(from: Map<out K, V>) {
        putAllEntries(from.entries)
    }

    override fun remove(key: K): V? {
        val index = removeKey(key)
        if (index < 0) return null
        val valuesArray = valuesArray!!
        val oldValue = valuesArray[index]
        valuesArray.resetAt(index)
        return oldValue
    }

    override fun clear() {
        // O(length) implementation for hashArray cleanup
        for (i in 0..length - 1) {
            val hash = presenceArray[i]
            if (hash >= 0) {
                hashArray[hash] = 0
                presenceArray[i] = TOMBSTONE
            }
        }
        keysArray.resetRange(0, length)
        valuesArray?.resetRange(0, length)
        size = 0
        length = 0
    }

    override val keys: MutableSet<K> get() {
        return HashSet(this)

        // Caching creates a reference cycle and thus leads to memory leaks;
        // TODO: fix and enable.

        // val cur = keysView
        // return if (cur == null) {
        //     val new = HashSet(this)
        //     keysView = new
        //     new
        // } else cur
    }

    override val values: MutableCollection<V> get() {
        return HashMapValues(this)

        // Caching creates a reference cycle and thus leads to memory leaks;
        // TODO: fix and enable.

        // val cur = valuesView
        // return if (cur == null) {
        //     val new = HashMapValues(this)
        //     valuesView = new
        //     new
        // } else cur
    }

    override val entries: MutableSet<MutableMap.MutableEntry<K, V>> get() {
        return HashMapEntrySet(this)

        // Caching creates a reference cycle and thus leads to memory leaks;
        // TODO: fix and enable.

        // val cur = entriesView
        // return if (cur == null) {
        //     val new = HashMapEntrySet(this)
        //     entriesView = new
        //     return new
        // } else cur
    }

    override fun equals(other: Any?): Boolean {
        return other === this ||
                (other is Map<*, *>) &&
                        contentEquals(other)
    }

    override fun hashCode(): Int {
        var result = 0
        val it = entriesIterator()
        while (it.hasNext()) {
            result += it.nextHashCode()
        }
        return result
    }

    override fun toString(): String {
        val sb = StringBuilder(2 + size * 3)
        sb.append("{")
        var i = 0
        val it = entriesIterator()
{'fraction_non_alphanumeric': 0.11082817994422214, 'fraction_numerical': 0.005335273432763429, 'mean_word_length': 1.7095926412614981, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, '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': '975313', 'n_tokens_mistral': 2308, 'n_tokens_neox': 2219, 'n_words': 698}