segment-tree 2.0.0

Quickly perform interval queries or modifications.
Documentation
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use crate::ops::{Commutative, Invertible};
use crate::maybe_owned::MaybeOwned;

use std::default::Default;
use std::hash::{Hash, Hasher};

/// This data structure allows prefix queries and single element modification.
///
/// This tree allocates `n * sizeof(N)` bytes of memory, and can be resized.
///
/// This data structure is implemented using a [Fenwick tree][1], which is also known as a
/// binary indexed tree.
///
/// The similar crate [`prefix-sum`] might also be of interest.
///
/// # Examples
///
/// Showcase of functionality:
///
/// ```rust
/// use segment_tree::ops::Add;
/// use segment_tree::PrefixPoint;
///
/// let buf = vec![10, 5, 30, 40];
///
/// let mut pp = PrefixPoint::build(buf, Add);
///
/// // If we query, we get the sum up until the specified value.
/// assert_eq!(pp.query(0), 10);
/// assert_eq!(pp.query(1), 15);
/// assert_eq!(pp.query(2), 45);
/// assert_eq!(pp.query(3), 85);
///
/// // Add five to the second value.
/// pp.modify(1, 5);
/// assert_eq!(pp.query(0), 10);
/// assert_eq!(pp.query(1), 20);
/// assert_eq!(pp.query(2), 50);
/// assert_eq!(pp.query(3), 90);
///
/// // Multiply every value with 2.
/// pp.map(|v| *v *= 2);
/// assert_eq!(pp.query(0), 20);
/// assert_eq!(pp.query(1), 40);
/// assert_eq!(pp.query(2), 100);
/// assert_eq!(pp.query(3), 180);
///
/// // Divide with two to undo.
/// pp.map(|v| *v /= 2);
/// // Add some more values.
/// pp.extend(vec![0, 10].into_iter());
/// assert_eq!(pp.query(0), 10);
/// assert_eq!(pp.query(1), 20);
/// assert_eq!(pp.query(2), 50);
/// assert_eq!(pp.query(3), 90);
/// assert_eq!(pp.query(4), 90);
/// assert_eq!(pp.query(5), 100);
///
/// // Get the underlying values.
/// assert_eq!(pp.get(0), 10);
/// assert_eq!(pp.get(1), 10);
/// assert_eq!(pp.get(2), 30);
/// assert_eq!(pp.get(3), 40);
/// assert_eq!(pp.get(4), 0);
/// assert_eq!(pp.get(5), 10);
///
/// // Remove the last value
/// pp.truncate(5);
/// assert_eq!(pp.get(0), 10);
/// assert_eq!(pp.get(1), 10);
/// assert_eq!(pp.get(2), 30);
/// assert_eq!(pp.get(3), 40);
/// assert_eq!(pp.get(4), 0);
///
/// // Get back the original values.
/// assert_eq!(pp.unwrap(), vec![10, 10, 30, 40, 0]);
/// ```
///
/// You can also use other operators:
///
/// ```rust
/// use segment_tree::ops::Mul;
/// use segment_tree::PrefixPoint;
///
/// let buf = vec![10, 5, 30, 40];
///
/// let mut pp = PrefixPoint::build(buf, Mul);
///
/// assert_eq!(pp.query(0), 10);
/// assert_eq!(pp.query(1), 50);
/// assert_eq!(pp.query(2), 1500);
/// assert_eq!(pp.query(3), 60000);
/// ```
///
/// [1]: https://en.wikipedia.org/wiki/Fenwick_tree
/// [`prefix-sum`]: https://crates.io/crates/prefix-sum
pub struct PrefixPoint<N, O> where O: Commutative<N> {
    buf: Vec<N>,
    op: O
}

/// Returns the least significant bit that is one.
#[inline(always)]
fn lsb(i: usize) -> usize {
    i & (1 + !i)
}

/// Could also be done with slice_at_mut, but that's a giant pain
#[inline(always)]
unsafe fn combine_mut<N, O: Commutative<N>>(buf: &mut Vec<N>, i: usize, j: usize, op: &O) {
    debug_assert!(i != j);
    let ptr1 = &mut buf[i] as *mut N;
    let ptr2 = &buf[j] as *const N;
    op.combine_mut(&mut *ptr1, &*ptr2);
}
/// Could also be done with slice_at_mut, but that's a giant pain
#[inline(always)]
unsafe fn uncombine_mut<N, O: Invertible<N>>(buf: &mut Vec<N>, i: usize, j: usize, op: &O) {
    debug_assert!(i != j);
    let ptr1 = &mut buf[i] as *mut N;
    let ptr2 = &buf[j] as *const N;
    op.uncombine(&mut *ptr1, &*ptr2);
}

impl<N, O: Commutative<N>> PrefixPoint<N, O> {
    /// Creates a `PrefixPoint` containing the given values.
    /// Uses `O(len)` time.
    pub fn build(mut buf: Vec<N>, op: O) -> PrefixPoint<N, O> {
        let len = buf.len();
        for i in 0..len {
            let j = i + lsb(i+1);
            if j < len {
                unsafe {
                    combine_mut::<N, O>(&mut buf, j, i, &op);
                }
            }
        }
        PrefixPoint { buf: buf, op: op }
    }
    /// Returns the number of values in this tree.
    /// Uses `O(1)` time.
    pub fn len(&self) -> usize {
        self.buf.len()
    }
    /// Computes `a[0] * a[1] * ... * a[i]`.  Note that `i` is inclusive.
    /// Uses `O(log(i))` time.
    #[inline]
    pub fn query(&self, mut i: usize) -> N where N: Clone {
        let mut sum = self.buf[i].clone();
        i -= lsb(1+i) - 1;
        while i > 0 {
            sum = self.op.combine_left(sum, &self.buf[i-1]);
            i -= lsb(i);
        }
        sum
    }
    /// Computes `a[0] * a[1] * ... * a[i]`.  Note that `i` is inclusive.
    /// Uses `O(log(i))` time.
    #[inline]
    pub fn query_noclone(&self, mut i: usize) -> MaybeOwned<N> {
        let mut sum = MaybeOwned::Borrowed(&self.buf[i]);
        i -= lsb(1+i) - 1;
        while i > 0 {
            sum = MaybeOwned::Owned(match sum {
                MaybeOwned::Borrowed(ref v) => self.op.combine(v, &self.buf[i-1]),
                MaybeOwned::Owned(v) => self.op.combine_left(v, &self.buf[i-1]),
            });
            i -= lsb(i);
        }
        sum
    }
    /// Combine the value at `i` with `delta`.
    /// Uses `O(log(len))` time.
    #[inline]
    pub fn modify(&mut self, mut i: usize, delta: N) {
        let len = self.len();
        while i < len {
            self.op.combine_mut(&mut self.buf[i], &delta);
            i += lsb(i+1);
        }
    }
    /// Truncates the `PrefixPoint` to the given size.  If `size >= len`, this method does
    /// nothing.  Uses `O(1)` time.
    #[inline(always)]
    pub fn truncate(&mut self, size: usize) {
        self.buf.truncate(size);
    }
    /// Calls `shrink_to_fit` on the interval vector.
    #[inline(always)]
    pub fn shrink_to_fit(&mut self) {
        self.buf.shrink_to_fit();
    }
    /// Replace every value in the type with `f(value)`.
    /// This function assumes that `combine(f(a), f(b)) = f(combine(a, b))`.
    ///
    /// The function is applied `len` times.
    #[inline]
    pub fn map<F: FnMut(&mut N)>(&mut self, mut f: F) {
        for val in &mut self.buf {
            f(val);
        }
    }
}
impl<N, O: Commutative<N>> Extend<N> for PrefixPoint<N, O> {
    /// Adds the given values to the `PrefixPoint`, increasing its size.
    /// Uses `O(len)` time.
    fn extend<I: IntoIterator<Item=N>>(&mut self, values: I) {
        let oldlen = self.len();
        self.buf.extend(values);
        let len = self.len();
        for i in 0..len {
            let j = i + lsb(i+1);
            if oldlen <= j && j < len {
                unsafe {
                    combine_mut::<N, O>(&mut self.buf, j, i, &self.op);
                }
            }
        }
    }
}
impl<N, O: Commutative<N> + Invertible<N>> PrefixPoint<N, O> {
    /// Returns the value at `i`.
    /// Uses `O(log(i))` time.
    /// Store your own copy of the array if you want constant time.
    pub fn get(&self, mut i: usize) -> N where N: Clone {
        let mut sum = self.buf[i].clone();
        let z = 1 + i - lsb(i+1);
        while i != z {
            self.op.uncombine(&mut sum, &self.buf[i-1]);
            i -= lsb(i);
        }
        sum
    }
    /// Change the value at the index to be the specified value.
    /// Uses `O(log(i))` time.
    pub fn set(&mut self, i: usize, mut value: N) where N: Clone {
        let current = self.get(i);
        self.op.uncombine(&mut value, &current);
        self.modify(i, value);
    }
    /// Compute the underlying array of values.
    /// Uses `O(len)` time.
    pub fn unwrap(self) -> Vec<N> {
        let mut buf = self.buf;
        let len = buf.len();
        for i in (0..len).rev() {
            let j = i + lsb(i+1);
            if j < len {
                unsafe {
                    uncombine_mut::<N, O>(&mut buf, j, i, &self.op);
                }
            }
        }
        buf
    }
    /// Compute the underlying array of values.
    /// Uses `O(len)` time.
    pub fn unwrap_clone(&self) -> Vec<N> where N: Clone {
        let len = self.buf.len();
        let mut buf = self.buf.clone();
        for i in (0..len).rev() {
            let j = i + lsb(i+1);
            if j < len {
                unsafe {
                    uncombine_mut::<N, O>(&mut buf, j, i, &self.op);
                }
            }
        }
        buf
    }
}

impl<N: Clone, O: Commutative<N> + Clone> Clone for PrefixPoint<N, O> {
    fn clone(&self) -> PrefixPoint<N, O> {
        PrefixPoint {
            buf: self.buf.clone(), op: self.op.clone()
        }
    }
}
impl<N, O: Commutative<N> + Default> Default for PrefixPoint<N, O> {
    #[inline]
    fn default() -> PrefixPoint<N, O> {
        PrefixPoint { buf: Vec::new(), op: Default::default() }
    }
}
impl<'a, N: 'a + Hash, O: Commutative<N>> Hash for PrefixPoint<N, O> {
    #[inline]
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.buf.hash(state);
    }
}

#[cfg(test)]
mod tests {

    /// Modifies the given slice such that the n'th element becomes the sum of the first n
    /// elements.
    pub fn compute_prefix_sum<N: ::std::ops::Add<Output=N> + Copy>(buf: &mut[N]) {
        let mut iter = buf.iter_mut();
        match iter.next() {
            None => {},
            Some(s) => {
                let mut sum = *s;
                for item in iter {
                    sum = sum + *item;
                    *item = sum;
                }
            }
        }
    }

    use super::*;
    use rand::distributions::Standard;
    use rand::prelude::*;
    use std::num::Wrapping;
    use crate::ops::Add;

    fn random_vec(rng: &mut ThreadRng, len: usize) -> Vec<Wrapping<i32>> {
        rng.sample_iter(&Standard).map(|i| Wrapping(i)).take(len).collect()
    }

    #[test]
    fn fenwick_query() {
        let mut rng = thread_rng();
        for n in 0..130 {
            let mut vec = random_vec(&mut rng, n);
            let fenwick = PrefixPoint::build(vec.clone(), Add);
            compute_prefix_sum(&mut vec);
            for i in 0..vec.len() {
                assert_eq!(vec[i], fenwick.query(i));
                assert_eq!(&vec[i], fenwick.query_noclone(i).borrow());
            }
        }
    }
    #[test]
    fn fenwick_map() {
        let mut rng = thread_rng();
        for n in 0..130 {
            let mut vec = random_vec(&mut rng, n);
            let mut fenwick = PrefixPoint::build(vec.clone(), Add);
            assert_eq!(fenwick.clone().unwrap(), vec);
            assert_eq!(fenwick.clone().unwrap_clone(), vec);
            compute_prefix_sum(&mut vec);
            fenwick.map(|n| *n = Wrapping(12) * *n);
            for i in 0..vec.len() {
                assert_eq!(vec[i]*Wrapping(12), fenwick.query(i));
            }
        }
    }
    #[test]
    fn fenwick_modify() {
        let mut rng = thread_rng();
        for n in 0..130 {
            let mut vec = random_vec(&mut rng, n);
            let diff = random_vec(&mut rng, n);
            let mut fenwick = PrefixPoint::build(vec.clone(), Add);
            for i in 0..diff.len() {
                let mut ps: Vec<Wrapping<i32>> = vec.clone();
                compute_prefix_sum(&mut ps);
                assert_eq!(fenwick.clone().unwrap(), vec);
                assert_eq!(fenwick.clone().unwrap_clone(), vec);
                for j in 0..vec.len() {
                    assert_eq!(ps[j], fenwick.query(j));
                    assert_eq!(vec[j], fenwick.get(j));
                }
                vec[i] += diff[i];
                fenwick.modify(i, diff[i]);
            }
        }
    }
    #[test]
    fn fenwick_set() {
        let mut rng = thread_rng();
        for n in 0..130 {
            let mut vec = random_vec(&mut rng, n);
            let diff = random_vec(&mut rng, n);
            let mut fenwick = PrefixPoint::build(vec.clone(), Add);
            for i in 0..diff.len() {
                let mut ps: Vec<Wrapping<i32>> = vec.clone();
                compute_prefix_sum(&mut ps);
                assert_eq!(fenwick.clone().unwrap(), vec);
                assert_eq!(fenwick.clone().unwrap_clone(), vec);
                for j in 0..vec.len() {
                    assert_eq!(ps[j], fenwick.query(j));
                    assert_eq!(vec[j], fenwick.get(j));
                }
                vec[i] = diff[i];
                fenwick.set(i, diff[i]);
            }
        }
    }
    #[test]
    fn fenwick_extend() {
        let mut rng = thread_rng();
        for n in 0..130 {
            let vec = random_vec(&mut rng, n);
            let mut sum = vec.clone();
            compute_prefix_sum(&mut sum);
            for i in 0..sum.len() {
                let mut fenwick = PrefixPoint::build(vec.iter().take(i/2).map(|&i| i).collect(), Add);
                fenwick.extend(vec.iter().skip(i/2).take(i - i/2).map(|&i| i));
                for j in 0..i {
                    assert_eq!(sum[j], fenwick.query(j));
                }
            }
        }
    }
}