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
//! # Slighly over-engineered Fenwick Tree implmentation.
//! 
//! Allows efficient prefix sum calculation.
//! 
//! Created for training purposes to test: 
//!
//! 1. rust typesystem, default trait implmentation, enums as a way for polymorphism
//! 2. memory management and consumption of value
//! 3. cargo tools, docs, tests, clippy and benchmarks, build and publish.
//!
//! Code is free to do whatever you feel like.
//! 
//! Provides abstraction for Fenwick tree data structure and 2 implmentations:
//!  - [`prelude::FixedSizeFenwickTree`]
//!  - [`prelude::GrowingFenwickTree`]
//! 
//! Key space for a tree lies within [`usize`] range. Tree support any value that 
//! implements [`FenwickTreeValue`] trait. [`FenwickTreeValue`] is automatically 
//! implmented for all primitive numeric types that support [`std::ops::AddAssign`], 
//! [`std::ops::Sub`], [`core::cmp::PartialEq`] and [`Copy`] traits.
//!
//! ## Installation  
//!
//! ```bash
//! cargo install fenwick-bit-tree
//! ```
//! 
//! ## Test
//! 
//! ```bash
//! cargo test
//! ```
//! 
//! ## Benchmarks
//! 
//! ```bash
//! cargo bench --features benchmarks
//! ```
//! 
//! ## Basic usage:
//! 
//! ```rust
//! use fenwick_bit_tree::prelude::*;
//! 
//! // Create the tree with capacity for 32 aggregated [`i32`] data points. 
//! // One can use whole usize range to store datapoints for unicode timestamps
//! let mut tree = FixedSizeFenwickTree::<i32>::new(32);
//!
//! // Add values
//! 
//! tree.update(0, 1); 
//! tree.update(0, 4); // Will aggregate value at index 0 so it would be 5
//! tree.update(10, 10);
//! tree.update(20, 10);
//! tree.update(30, 10);
//! 
//! // Now you can query data. 
//! // NOTE: FixedSizeFenwickTree will raise error when query goes out of bounds.
//! //       GrowingFenwickTree will automatically truncate the range to the rightmost index. 
//! 
//! assert_eq!(tree.query(4).unwrap(), 5); 
//! assert_eq!(tree.query(15).unwrap(), 15);
//! assert_eq!(tree.query(31).unwrap(), 35);
//!
//! // Also allows making range queries
//! 
//! let val = tree.range_query(2, 16).unwrap(); // Will return aggregated sum of all values between those keys.
//! assert_eq!(val, 10);
//! ```

#![forbid(unsafe_code)]
#![feature(test)]

use std::ops::{Deref, DerefMut};

mod fixed_size_tree;
mod growing_tree;

pub use fixed_size_tree::FixedSizeFenwickTree;
pub use growing_tree::GrowingFenwickTree;

/// Contains all public types
pub mod prelude {
    pub use crate::FenwickTreeValue;
    pub use crate::fixed_size_tree::FixedSizeFenwickTree;
    pub use crate::growing_tree::GrowingFenwickTree;
    pub use crate::FenwickTree;
    pub use crate::TreeError;
}

fn least_significant_bit(idx: usize) -> usize {
    let int_idx = idx as i32;
    (int_idx & -int_idx) as usize
}

/// Types that implement that trait can be stored and aggregated within Fenwick tree.
pub trait FenwickTreeValue:
    Default + Clone //
    + core::cmp::PartialEq 
{
    fn store_value(&mut self, other: &Self);
    fn substract(self, other: Self) -> Self;
}

impl<T> FenwickTreeValue for T 
where T: Default + Copy //
    + std::ops::AddAssign
    + std::ops::Sub<Output = Self>
    + core::cmp::PartialEq 
{
    fn store_value(&mut self, other: &Self) {
        *self += *other
    }

    fn substract(self, other: Self) -> Self {
        self - other
    }
}

/// Fenwick tree trait, API of that data structure
pub trait FenwickTree {
    type Value: FenwickTreeValue;

    /// Returns sum of values across all indexes lesser or equal than `idx`.
    ///
    /// # Errors
    ///
    /// This function will returns an error if idx is out of bounds.
    /// GrowingFenwick tree implementation never returns error.
    /// 
    fn query(&self, idx: usize) -> Result<Self::Value, TreeError>;
    
    /// Add new value to the `idx` stored value, which is 0 by default. 
    ///
    /// # Errors
    ///
    /// This function will return an error if idx is out of bounds.
    /// GrowingFenwick tree implementation never returns error.
    /// 
    fn update(&mut self, idx: usize, value: Self::Value) -> Result<(), TreeError>;

    /// Returns sum of values across all indexes in between `from` and `to` indexes 
    /// (including edges).
    ///
    /// # Errors
    ///
    /// This function will return an error if any index is out of bounds.
    /// GrowingFenwick tree implementation never return error.
    /// 
    fn range_query(&self, from: usize, to: usize) -> Result<Self::Value, TreeError> {
        let from_sum = self.query(from)?;
        let to_sum = self.query(to)?;
        Ok(to_sum.substract(from_sum))
    }
}

/// For the sake of clarity Tree supports 2 types of indexing. [`TreeIndex::External`] is meant to be used 
/// by library consumer. While [`TreeIndex::Internal`] is used for purposes to make tree reindexing code more
/// understable and maintainable. [`usize`] can be automatically converted using `into()` into the [`TreeIndex::External`]
#[derive(Debug, Clone, Copy)]
enum TreeIndex {
    Internal { val: usize },
    External { val: usize },
}

#[derive(Debug, PartialEq)]
pub enum TreeError {
    IndexOutOfBounds( usize )
}

impl TreeIndex {

    fn to_internal(self) -> Self {
        match self {
            TreeIndex::Internal { val: _ } => self,
            TreeIndex::External { val } => TreeIndex::Internal { val: val + 1 },
        }
    }

    fn to_external(self) -> Result<Self, String> {
        match self {
            TreeIndex::Internal { val } => {
                if val == 0 {
                    return Err("Index is out of bounds.".to_string());
                }
                Ok(TreeIndex::External { val: val - 1 })
            }
            TreeIndex::External { val: _ } => Ok(self),
        }
    }

    /// Starts with the initial value and then moves down to zero returning result of
    /// deduction of the least significant bit
    fn lsb_descending(self) -> LeastSignificantBitDescentingChain {
        LeastSignificantBitDescentingChain {
            idx: self.to_internal(),
        }
    }

    /// Starts with the initial value and then moves up until upper bound is reached 
    /// returning the result of deduction of the least significant bit
    fn lsb_ascending(self, upper_bound: usize) -> LeastSignificantBitAscendingChain {
        LeastSignificantBitAscendingChain {
            idx: self.to_internal(),
            max: upper_bound,
        }
    }

    fn is_power_of_2(self) -> bool {
        let idx = *self;
        idx.is_power_of_two()
    }

}

impl From<usize> for TreeIndex {
    fn from(value: usize) -> Self {
        Self::External { val: value }
    }
}

impl Deref for TreeIndex {
    type Target = usize;

    fn deref(&self) -> &Self::Target {
        match self {
            TreeIndex::External { val } => val,
            TreeIndex::Internal { val } => val,
        }
    }
}

impl PartialEq for TreeIndex {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Internal { val: l_val }, Self::Internal { val: r_val }) => l_val == r_val,
            (Self::External { val: l_val }, Self::External { val: r_val }) => l_val == r_val,
            _ => false,
        }
    }
}

impl DerefMut for TreeIndex {
    fn deref_mut(&mut self) -> &mut Self::Target {
        match self {
            TreeIndex::External { val } => val,
            TreeIndex::Internal { val } => val,
        }
    }
}

/// Iterator that implements changing value by deduction of the least significant bit and 
/// returning result
struct LeastSignificantBitDescentingChain {
    idx: TreeIndex,
}

impl Iterator for LeastSignificantBitDescentingChain {
    type Item = TreeIndex;

    fn next(&mut self) -> Option<Self::Item> {
        if *self.idx == 0 {
            return None;
        }
        // TODO: implement COpy?
        let res = TreeIndex::Internal { val: *self.idx };
        *self.idx -= least_significant_bit(*self.idx);
        Some(res)
    }
}

/// Iterator that implements changing value by addition of the least significant bit and 
/// returning result
struct LeastSignificantBitAscendingChain {
    idx: TreeIndex,
    max: usize,
}

impl Iterator for LeastSignificantBitAscendingChain {
    type Item = TreeIndex;

    fn next(&mut self) -> Option<Self::Item> {
        if *self.idx > self.max {
            return None;
        }
        // TODO: implement COpy?
        let res = TreeIndex::Internal { val: *self.idx };
        *self.idx += least_significant_bit(*self.idx);
        Some(res)
    }
}

#[cfg(test)]
mod tests {

    use pretty_assertions::assert_eq;

    use crate::{least_significant_bit, TreeIndex};

    fn to_internal_index_vec(indexes: &[usize]) -> Vec<TreeIndex> {
        indexes
            .into_iter()
            .map(|i| TreeIndex::Internal { val: *i })
            .collect::<Vec<TreeIndex>>()
    }

    #[test]
    fn test_index_transform_from_internal_to_external_with_error() {
        let idx = TreeIndex::Internal { val: 0 };
        idx.to_external().expect_err("Index is out of bounds.");
    }

    #[test]
    fn test_index_transform_from_internal_to_external() {
        for val in 1..100 {
            let idx = TreeIndex::Internal { val: val };
            assert_eq!(
                idx.to_external().unwrap(),
                TreeIndex::External { val: val - 1 }
            );
        }
    }

    #[test]
    fn test_index_transform_from_external_to_internal() {
        for val in 0..100 {
            let idx = TreeIndex::External { val: val };
            assert_eq!(idx.to_internal(), TreeIndex::Internal { val: val + 1 });
        }
    }

    #[test]
    fn test_index_transform_to_itseld() {
        for val in 0..100 {
            let idx = TreeIndex::External { val: val };
            assert_eq!(idx.to_external().unwrap(), TreeIndex::External { val });
        }

        for val in 0..100 {
            let idx = TreeIndex::Internal { val: val };
            assert_eq!(idx.to_internal(), TreeIndex::Internal { val: val });
        }
    }

    #[test]
    fn test_ascending_lsb_chain() {
        let idx: TreeIndex = 0.into();
        assert_eq!(
            idx.lsb_ascending(64).collect::<Vec<TreeIndex>>(),
            to_internal_index_vec(&[1, 2, 4, 8, 16, 32, 64])
        );

        let idx: TreeIndex = 1.into();
        assert_eq!(
            idx.lsb_ascending(64).collect::<Vec<TreeIndex>>(),
            to_internal_index_vec(&[2, 4, 8, 16, 32, 64])
        );

        let idx: TreeIndex = 6.into();
        assert_eq!(
            idx.lsb_ascending(64).collect::<Vec<TreeIndex>>(),
            to_internal_index_vec(&[7, 8, 16, 32, 64])
        );

        let idx: TreeIndex = 6.into();
        assert_eq!(idx.lsb_ascending(0).collect::<Vec<TreeIndex>>(), vec![]);
    }

    #[test]
    fn test_descending_lsb_chain() {
        let idx: TreeIndex = 5.into();
        assert_eq!(idx, TreeIndex::External { val: 5 });
        assert_eq!(
            idx.lsb_descending().collect::<Vec<TreeIndex>>(),
            to_internal_index_vec(&[6, 4])
        );

        let idx: TreeIndex = 4.into();
        assert_eq!(
            idx.lsb_descending().collect::<Vec<TreeIndex>>(),
            to_internal_index_vec(&[5, 4])
        );

        let idx = TreeIndex::Internal { val: 3 };
        assert_eq!(
            idx.lsb_descending().collect::<Vec<TreeIndex>>(),
            to_internal_index_vec(&[3, 2])
        );

        let idx = TreeIndex::Internal { val: 12 };
        assert_eq!(
            idx.lsb_descending().collect::<Vec<TreeIndex>>(),
            to_internal_index_vec(&[12, 8])
        );
    }

    #[test]
    fn test_lsb() {
        assert_eq!(least_significant_bit(12), 4)
    }

    #[test]
    fn test_bitwise_op() {
        assert_eq!(12usize.next_power_of_two(), 16);
        assert_eq!(12usize.next_power_of_two() >> 1, 8);
    }
}