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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
//! A succinct sparse set representation based on the Okanohara & Sadakane 2007 paper:
//!
//! > Okanohara, D. and Sadakane, K., 2007, January. Practical entropy-compressed rank/select dictionary.
//! >  In 2007 Proceedings of the Ninth Workshop on Algorithm Engineering and Experiments (ALENEX) (pp. 60-70).
//1 > Society for Industrial and Applied Mathematics.

use crate::{
    bitvec::BitVec,
    dense64::Dense64,
    intvec::IntVec,
    persist::{load_usize, Persistent},
    rank::Rank,
    select::Select,
    select::Select0,
    set::ImpliedSet,
};

/// The `Sparse` data structure implements the succinct set representation for sparse sets
/// described by:
///
/// > Okanohara, D. and Sadakane, K., 2007, January. Practical entropy-compressed rank/select dictionary.
/// >  In 2007 Proceedings of the Ninth Workshop on Algorithm Engineering and Experiments (ALENEX) (pp. 60-70).
/// > Society for Industrial and Applied Mathematics.
///
/// #Examples
///
/// ```
/// use crate::ransel::sparse::Sparse;
/// use crate::ransel::rank::Rank;
/// use crate::ransel::select::Select;
///
/// let mix = |i:u64| i.wrapping_mul(2862933555777941757u64).wrapping_add(3037000493u64);
/// let b = 48;
/// let n = 1u64 << b;
/// let m = n - 1;
/// let mut xs: Vec<u64> = Vec::from_iter((0..100_000).map(mix).map(|x| x & m));
/// xs.sort();
/// xs.dedup();
/// let s = Sparse::new(b, &xs);
/// for i in 0..xs.len() {
///     assert_eq!(s.select(i), xs[i]);
///     assert_eq!(s.rank(xs[i]), i);
/// }
/// ```
pub struct Sparse {
    b: usize,
    n: usize,
    d: usize,
    hi: Dense64,
    lo: IntVec,
}

impl Sparse {
    /// Create a new sparse set.
    ///
    /// The parameter `b` should be the maximum number of bits required for any
    /// element of the set. Nb this means the size of the set is `2**b`.
    ///
    /// The sequence `elements` must be in sorted order, and free of duplicates.
    ///
    pub fn new(b: usize, elements: &[u64]) -> Sparse {
        let n = elements.len();
        let d = ((1u64 << b) as f64 / (1.44 * n as f64)).log2() as usize;
        let m = (1u64 << d) - 1;
        let mut hi_cursor = 0;
        let mut hi_bits = BitVec::new();
        let mut low_bits = IntVec::new(d);
        for x in elements {
            let hi = *x >> d;
            let lo = *x & m;
            while hi_cursor <= hi {
                hi_bits.push(true);
                hi_cursor += 1;
            }
            hi_bits.push(false);
            low_bits.push(lo);
        }
        let j = 1u64 << (b - d);
        while hi_cursor < j {
            hi_bits.push(true);
            hi_cursor += 1;
        }
        hi_bits.push(true);
        Sparse {
            b,
            n,
            d,
            hi: Dense64::new(hi_bits.len() as u64, hi_bits.as_words()),
            lo: low_bits,
        }
    }
}

impl ImpliedSet for Sparse {
    fn size(&self) -> u64 {
        1u64 << self.b
    }

    fn count(&self) -> usize {
        self.n
    }
}

impl Rank for Sparse {
    fn rank(&self, value: u64) -> usize {
        if value >= (1u64 << self.b) {
            return self.count();
        }
        let hi = (value >> self.d) as usize;
        let lo = value & ((1u64 << self.d) - 1);
        let r0 = self.hi.select(hi) as usize - hi;
        let r1 = self.hi.select(hi + 1) as usize - (hi + 1);
        let mut r = r0;
        while r < r1 && self.lo.get(r) < lo {
            r += 1;
        }
        r as usize
    }

    fn rank_2(&self, value_1: u64, value_2: u64) -> (usize, usize) {
        let hi_1 = (value_1 >> self.d) as usize;
        let hi_2 = (value_2 >> self.d) as usize;
        if hi_1 != hi_2 || value_2 < value_1 || value_2 >= (1u64 << self.d) {
            return (self.rank(value_1), self.rank(value_2));
        }

        let hi = hi_1;
        let lo_1 = value_1 & ((1u64 << self.d) - 1);
        let lo_2 = value_2 & ((1u64 << self.d) - 1);
        let r0 = self.hi.select(hi) as usize - hi;
        let r1 = self.hi.select(hi + 1) as usize - (hi + 1);

        let mut r_a = r0;
        while r_a < r1 && self.lo.get(r_a) < lo_1 {
            r_a += 1;
        }
        let mut r_b = r_a;
        while r_b < r1 && self.lo.get(r_b) < lo_2 {
            r_b += 1;
        }
        (r_a, r_b)
    }
}

impl Select for Sparse {
    fn select(&self, index: usize) -> u64 {
        let z = self.hi.select_0(index);
        let hi = self.hi.rank(z) as u64 - 1;
        (hi << self.d) | self.lo.get(index)
    }
}

impl Persistent for Sparse {
    fn save<Sink>(&self, sink: &mut Sink) -> std::io::Result<()>
    where
        Sink: std::io::Write,
    {
        sink.write_all(&self.b.to_ne_bytes())?;
        sink.write_all(&self.n.to_ne_bytes())?;
        sink.write_all(&self.d.to_ne_bytes())?;
        self.hi.save(sink)?;
        self.lo.save(sink)?;
        Ok(())
    }

    fn load<Source>(source: &mut Source) -> std::io::Result<Box<Self>>
    where
        Source: std::io::Read,
    {
        let b: usize = load_usize(source)?;
        let n: usize = load_usize(source)?;
        let d: usize = load_usize(source)?;
        let hi: Dense64 = *(Dense64::load(source)?);
        let lo: IntVec = *(IntVec::load(source)?);
        Ok(Box::new(Sparse { b, n, d, hi, lo }))
    }
}

#[cfg(test)]
mod tests {
    use flate2;
    use std::collections::HashSet;
    use std::fs::File;
    use std::io::BufRead;
    use std::io::BufReader;

    #[allow(unused_imports)]
    use num_traits::WrappingAdd;
    #[allow(unused_imports)]
    use num_traits::WrappingMul;

    use crate::set::ImpliedSet;

    use super::*;

    struct MiniRng {
        x: u64,
    }

    impl MiniRng {
        fn new(seed: u64) -> MiniRng {
            MiniRng { x: seed }
        }

        fn rnd(&mut self) -> u64 {
            self.x = self.x.wrapping_mul(2862933555777941757u64);
            self.x = self.x.wrapping_add(3037000493u64);
            self.x
        }
    }

    fn make_set(b: usize, n: usize) -> Vec<u64> {
        let m = (1u64 << b) - 1;
        let mut rng = MiniRng::new(0xfbdb8b2bcc6674b8u64);
        let mut s = HashSet::new();
        while s.len() < n {
            let x: u64 = (rng.rnd() ^ (rng.rnd() << 32) ^ (rng.rnd() >> 32)) & m;
            s.insert(x);
        }
        let mut res = Vec::from_iter(s.iter().map(|v| *v));
        res.sort();
        res
    }

    #[test]
    fn test_sparse_build_1() {
        let b: usize = 20;
        let n: usize = 1024;
        let xs = make_set(b, n);
        let s = Sparse::new(b, &xs);
        assert_eq!(s.size(), 1u64 << b);
        assert_eq!(s.count(), n);
        assert_eq!(s.b, b);
        assert_eq!(s.n, n);
        assert_eq!(s.d, 9);
        assert_eq!(s.hi.count(), 2049);
        assert_eq!(s.hi.size(), 2049 + n as u64);
        assert_eq!(s.lo.len(), n);
    }

    #[test]
    fn test_sparse_rank_1() {
        let b: usize = 20;
        let n: usize = 1024;
        let xs = make_set(b, n);
        let s = Sparse::new(b, &xs);
        assert_eq!(s.b, b);
        assert_eq!(s.n, n);
        assert_eq!(s.d, 9);
        assert_eq!(s.hi.count(), 2049);
        assert_eq!(s.hi.size(), 2049 + n as u64);
        assert_eq!(s.lo.len(), n);
        for i in 0..xs.len() {
            let x = xs[i];
            let r = s.rank(x);
            assert_eq!(r, i);
        }
    }

    #[test]
    fn test_sparse_select_1() {
        let b: usize = 20;
        let n: usize = 1024;
        let xs = make_set(b, n);
        let s = Sparse::new(b, &xs);
        assert_eq!(s.b, b);
        assert_eq!(s.n, n);
        assert_eq!(s.d, 9);
        assert_eq!(s.hi.count(), 2049);
        assert_eq!(s.hi.size(), 2049 + n as u64);
        assert_eq!(s.lo.len(), n);
        for i in 0..xs.len() {
            let x = xs[i];
            let y = s.select(i);
            assert_eq!(y, x);
        }
    }

    #[test]
    fn test_big_sparse() {
        let b: usize = 50;
        let mut xs: Vec<u64> = Vec::new();
        {
            let file = File::open("./test_data/b_50_integers.txt.gz").expect("failed to open file");
            let buffer = BufReader::new(file);
            let gzip = flate2::bufread::GzDecoder::new(buffer);
            let reader = BufReader::new(gzip);
            for line in reader.lines() {
                let txt = line.expect("failed to read line");
                let x: u64 = txt.parse().expect("not an integer");
                xs.push(x);
            }
        }
        let s = Sparse::new(b, &xs);
        for i in 0..xs.len() {
            assert_eq!(s.rank(xs[i]), i);
            assert_eq!(s.select(i), xs[i]);
        }
        {
            let (r, c) = s.access_and_rank(0x3FFBC2C2BC000u64);
            assert_eq!(c, true);
            assert_eq!(r, s.count() - 1);
        }
        {
            let (r, c) = s.access_and_rank(0x3FFC9480BC000u64);
            assert_eq!(c, false);
            assert_eq!(r, s.count());
        }
    }
}