<commit_before>/**
 * Copyright 2010 - 2021 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jetbrains.exodus.env

import jetbrains.exodus.bindings.LongBinding.*

open class BitmapImpl(private val store: StoreImpl) : Bitmap {

    private var bitmapIterator: BitmapIterator? = null

    override fun getEnvironment() = store.environment

    override fun get(txn: Transaction, bit: Long): Boolean {
        if (bit < 0) {
            throw IllegalArgumentException("Bit number should be non-negative")
        }
        val key = getKey(bit)
        val storedBitmap = store.get(txn, longToCompressedEntry(key)) ?: return false
        val bitIndex = getBitIndex(bit)
        return entryToLong(storedBitmap).shr(bitIndex).and(1L) == 1L
    }

    override fun set(txn: Transaction, bit: Long, value: Boolean): Boolean {
        if (bit < 0) {
            throw IllegalArgumentException("Bit number should be non-negative")
        }
        val key = getKey(bit)
        val bitIndex = getBitIndex(bit)
        val storedBitmap = store.get(txn, longToCompressedEntry(key))
        val storedBitmapLong = if (storedBitmap == null) 0L else entryToLong(storedBitmap)

        val prevValue = storedBitmapLong.shr(bitIndex).and(1L) == 1L
        if (prevValue != value) {
            val modifiedBitmap = if (value) {
                storedBitmapLong.or(1L.shl(bitIndex))
            } else {
                storedBitmapLong.xor(1L.shl(bitIndex))
            }

            if (modifiedBitmap == 0L) {
                store.delete(txn, longToCompressedEntry(key))
            } else {
                store.put(txn, longToCompressedEntry(key), longToEntry(modifiedBitmap))
            }

            return true
        }

        return false
    }

    override fun clear(txn: Transaction, bit: Long): Boolean = this.set(txn, bit, false)

    private fun getKey(bit: Long): Long = bit.shr(6)

    private fun getBitIndex(bit: Long): Int = (bit % 64).toInt()

    override fun iterator(txn: Transaction): BitmapIterator {
        bitmapIterator = BitmapIterator(txn, store)
        return bitmapIterator as BitmapIterator
    }
}<commit_msg>get rid of unnecessary BitmapIterator field<commit_after>/**
 * Copyright 2010 - 2021 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jetbrains.exodus.env

import jetbrains.exodus.bindings.LongBinding.*

open class BitmapImpl(private val store: StoreImpl) : Bitmap {

    override fun getEnvironment() = store.environment

    override fun get(txn: Transaction, bit: Long): Boolean {
        if (bit < 0) {
            throw IllegalArgumentException("Bit number should be non-negative")
        }
        val key = getKey(bit)
        val storedBitmap = store.get(txn, longToCompressedEntry(key)) ?: return false
        val bitIndex = getBitIndex(bit)
        return entryToLong(storedBitmap).shr(bitIndex).and(1L) == 1L
    }

    override fun set(txn: Transaction, bit: Long, value: Boolean): Boolean {
        if (bit < 0) {
            throw IllegalArgumentException("Bit number should be non-negative")
        }
        val key = getKey(bit)
        val bitIndex = getBitIndex(bit)
        val storedBitmap = store.get(txn, longToCompressedEntry(key))
        val storedBitmapLong = if (storedBitmap == null) 0L else entryToLong(storedBitmap)

        val prevValue = storedBitmapLong.shr(bitIndex).and(1L) == 1L
        if (prevValue != value) {
            val modifiedBitmap = if (value) {
                storedBitmapLong.or(1L.shl(bitIndex))
            } else {
                storedBitmapLong.xor(1L.shl(bitIndex))
            }

            if (modifiedBitmap == 0L) {
                store.delete(txn, longToCompressedEntry(key))
            } else {
                store.put(txn, longToCompressedEntry(key), longToEntry(modifiedBitmap))
            }

            return true
        }

        return false
    }

    override fun clear(txn: Transaction, bit: Long): Boolean = this.set(txn, bit, false)

    private fun getKey(bit: Long): Long = bit.shr(6)

    private fun getBitIndex(bit: Long): Int = (bit % 64).toInt()

    override fun iterator(txn: Transaction): BitmapIterator = BitmapIterator(txn, store)
}
{'fraction_non_alphanumeric': 0.09441159641426664, 'fraction_numerical': 0.009536524890329964, 'mean_word_length': 2.7033898305084745, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3871009', 'n_tokens_mistral': 1515, 'n_tokens_neox': 1426, 'n_words': 461}