override fun count()

in environment/src/main/kotlin/jetbrains/exodus/env/BitmapImpl.kt [82:124]


    override fun count(txn: Transaction, firstBit: Bit, lastBit: Bit): Long {
        if (firstBit > lastBit) throw IllegalArgumentException("firstBit > lastBit")
        if (firstBit == lastBit) {
            return if (get(txn, firstBit)) 1L else 0L
        }
        store.openCursor(txn).use { cursor ->
            val firstKey = firstBit.ensureNonNegative().key
            val lastKey = lastBit.ensureNonNegative().key
            val keyEntry = LongBinding.longToCompressedEntry(firstKey)
            val valueEntry = cursor.getSearchKeyRange(keyEntry) ?: return 0L
            var count = 0L
            val key = compressedEntryToLong(cursor.key)
            if (key in (firstKey + 1) until lastKey) {
                count += valueEntry.countBits
            } else {
                val bits = valueEntry.asLong
                val lowBit = if (key == firstKey) firstBit.index else 0
                val highBit = if (key == lastKey) lastBit.index else Long.SIZE_BITS - 1
                for (i in lowBit..highBit) {
                    if (bits and (1L shl i) != 0L) {
                        ++count
                    }
                }
            }
            cursor.forEach {
                val currentKey = compressedEntryToLong(this.key)
                if (currentKey < lastKey) {
                    count += value.countBits
                } else {
                    if (currentKey == lastKey) {
                        val bits = value.asLong
                        for (i in 0..lastBit.index) {
                            if (bits and (1L shl i) != 0L) {
                                ++count
                            }
                        }
                    }
                    return count
                }
            }
            return count
        }
    }