1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
   Copyright 2018 DarkOtter

   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.
*/
//! Core operations to create indexes used to perform
//! fast rank & select operations on bitvectors.
#![no_std]

#[cfg(test)]
#[macro_use]
extern crate std;

#[cfg(test)]
extern crate rand;
#[cfg(test)]
extern crate rand_xorshift;

#[cfg(test)]
#[macro_use]
extern crate quickcheck;

#[cfg(test)]
#[macro_use]
extern crate proptest;

#[cold]
fn ceil_div_slow(n: usize, d: usize) -> usize {
    n / d + (if n % d > 0 { 1 } else { 0 })
}

#[inline(always)]
pub(crate) fn ceil_div(n: usize, d: usize) -> usize {
    let nb = n.wrapping_add(d - 1);
    if nb < n {
        return ceil_div_slow(n, d);
    };
    nb / d
}

#[cold]
fn ceil_div_u64_slow(n: u64, d: u64) -> u64 {
    n / d + (if n % d > 0 { 1 } else { 0 })
}

#[inline(always)]
pub(crate) fn ceil_div_u64(n: u64, d: u64) -> u64 {
    let nb = n.wrapping_add(d - 1);
    if nb < n {
        return ceil_div_u64_slow(n, d);
    };
    nb / d
}

mod ones_or_zeros;

mod word;

mod bytes;

pub mod bits_ref;

mod with_offset;

pub mod index_raw;

#[cfg(test)]
mod tests {
    use super::*;
    use proptest::prelude::*;

    #[test]
    fn check_max_bits_in_bytes() {
        assert!(<u64>::max_value() / 8 <= <usize>::max_value() as u64);
    }

    #[test]
    fn test_ceil_div_examples() {
        assert_eq!(0, ceil_div(0, 4));
        assert_eq!(1, ceil_div(1, 4));
        assert_eq!(1, ceil_div(4, 4));
        assert_eq!(2, ceil_div(5, 4));
        assert_eq!(2, ceil_div(6, 4));
        assert_eq!(2, ceil_div(7, 4));
        assert_eq!(2, ceil_div(8, 4));

        assert_eq!(ceil_div_slow(0, 43), ceil_div(0, 43));
        assert_eq!(ceil_div_slow(1, 43), ceil_div(1, 43));
        assert_eq!(
            ceil_div_slow(usize::max_value(), 43),
            ceil_div(usize::max_value(), 43)
        );
    }

    #[test]
    fn test_ceil_div_u64_examples() {
        assert_eq!(0, ceil_div_u64(0, 4));
        assert_eq!(1, ceil_div_u64(1, 4));
        assert_eq!(1, ceil_div_u64(4, 4));
        assert_eq!(2, ceil_div_u64(5, 4));
        assert_eq!(2, ceil_div_u64(6, 4));
        assert_eq!(2, ceil_div_u64(7, 4));
        assert_eq!(2, ceil_div_u64(8, 4));

        assert_eq!(ceil_div_u64_slow(0, 43), ceil_div_u64(0, 43));
        assert_eq!(ceil_div_u64_slow(1, 43), ceil_div_u64(1, 43));
        assert_eq!(
            ceil_div_u64_slow(u64::max_value(), 43),
            ceil_div_u64(u64::max_value(), 43)
        );
    }

    proptest! {
        #[test]
        fn test_ceil_div(x in any::<usize>(), d in 1..999999usize) {
            prop_assert_eq!(ceil_div_slow(x, d), ceil_div(x, d));
        }

        #[test]
        fn test_ceil_div_64(x in any::<u64>(), d in 1..999999u64) {
            prop_assert_eq!(ceil_div_u64_slow(x, d), ceil_div_u64(x, d));
        }
    }

}