|
| 1 | +use alga::general::AbstractMonoid; |
| 2 | +use alga::general::Operator; |
| 3 | +use std::collections::HashSet; |
| 4 | + |
| 5 | +pub trait FAT<Value, BinOp> |
| 6 | +where |
| 7 | + Value: AbstractMonoid<BinOp> + Clone, |
| 8 | + BinOp: Operator, |
| 9 | +{ |
| 10 | + /// Creates a new FAT from a batch of values |
| 11 | + fn new(batch: &[Value]) -> Self; |
| 12 | + |
| 13 | + /// Creates a new FAT with uninitialized values |
| 14 | + fn with_capacity(capacity: usize) -> Self; |
| 15 | + |
| 16 | + /// Updates a non-contiguous batch of leaves |
| 17 | + fn update<I>(&mut self, batch: I) |
| 18 | + where |
| 19 | + I: IntoIterator<Item = (usize, Value)>; |
| 20 | + |
| 21 | + /// Updates a contiguous batch of leaves |
| 22 | + fn update_ordered<I>(&mut self, values: I) |
| 23 | + where |
| 24 | + I: IntoIterator<Item = Value>; |
| 25 | + |
| 26 | + /// Aggregates all nodes in the FAT and returns the result |
| 27 | + fn aggregate(&self) -> Value; |
| 28 | + |
| 29 | + /// Aggregates a prefix of nodes in the FAT and returns the result |
| 30 | + fn prefix(&self, i: usize) -> Value; |
| 31 | + |
| 32 | + /// Aggregates a suffix of nodes in the FAT and returns the result |
| 33 | + fn suffix(&self, i: usize) -> Value; |
| 34 | +} |
| 35 | + |
| 36 | +pub struct FlatFAT<Value, BinOp> |
| 37 | +where |
| 38 | + Value: AbstractMonoid<BinOp> + Clone, |
| 39 | + BinOp: Operator, |
| 40 | +{ |
| 41 | + /// A flat binary tree, indexed as: |
| 42 | + /// 0 |
| 43 | + /// / \ |
| 44 | + /// / \ |
| 45 | + /// 1 2 |
| 46 | + /// / \ / \ |
| 47 | + /// 3 4 5 6 |
| 48 | + pub(crate) tree: Vec<Value>, |
| 49 | + /// Number of leaves which can be stored in the tree |
| 50 | + pub(crate) capacity: usize, |
| 51 | + binop: std::marker::PhantomData<BinOp>, |
| 52 | +} |
| 53 | + |
| 54 | +impl<Value, BinOp> FlatFAT<Value, BinOp> |
| 55 | +where |
| 56 | + Value: AbstractMonoid<BinOp> + Clone, |
| 57 | + BinOp: Operator, |
| 58 | +{ |
| 59 | + /// Returns all leaf nodes of the tree |
| 60 | + pub(crate) fn leaves(&self) -> &[Value] { |
| 61 | + &self.tree[self.leaf(0)..] |
| 62 | + } |
| 63 | + /// Returns the index of the root node |
| 64 | + fn root(&self) -> usize { |
| 65 | + 0 |
| 66 | + } |
| 67 | + /// Returns the index of a leaf node |
| 68 | + fn leaf(&self, i: usize) -> usize { |
| 69 | + i + self.capacity - 1 |
| 70 | + } |
| 71 | + /// Returns the index of an node's left child |
| 72 | + fn left(&self, i: usize) -> usize { |
| 73 | + 2 * (i + 1) - 1 |
| 74 | + } |
| 75 | + /// Returns the index of an node's right child |
| 76 | + fn right(&self, i: usize) -> usize { |
| 77 | + 2 * (i + 1) |
| 78 | + } |
| 79 | + /// Returns the index of an node's parent |
| 80 | + fn parent(&self, i: usize) -> usize { |
| 81 | + (i - 1) / 2 |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +impl<Value, BinOp> FAT<Value, BinOp> for FlatFAT<Value, BinOp> |
| 86 | +where |
| 87 | + Value: AbstractMonoid<BinOp> + Clone, |
| 88 | + BinOp: Operator, |
| 89 | +{ |
| 90 | + fn new(values: &[Value]) -> Self { |
| 91 | + let capacity = values.len(); |
| 92 | + let mut new = Self::with_capacity(capacity); |
| 93 | + new.update_ordered(values.iter().cloned()); |
| 94 | + new |
| 95 | + } |
| 96 | + |
| 97 | + fn with_capacity(capacity: usize) -> Self { |
| 98 | + assert_ne!(capacity, 0, "Capacity of window must be greater than 0"); |
| 99 | + Self { |
| 100 | + tree: vec![Value::identity(); 2 * capacity - 1], |
| 101 | + binop: std::marker::PhantomData, |
| 102 | + capacity, |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + fn update<I>(&mut self, batch: I) |
| 107 | + where |
| 108 | + I: IntoIterator<Item = (usize, Value)>, |
| 109 | + { |
| 110 | + let mut parents: HashSet<usize> = batch |
| 111 | + .into_iter() |
| 112 | + .map(|(idx, val)| { |
| 113 | + let leaf = self.leaf(idx); |
| 114 | + self.tree[leaf] = val; |
| 115 | + self.parent(leaf) |
| 116 | + }) |
| 117 | + .collect(); |
| 118 | + let mut new_parents: HashSet<usize> = HashSet::new(); |
| 119 | + loop { |
| 120 | + parents.drain().for_each(|parent| { |
| 121 | + let left = self.left(parent); |
| 122 | + let right = self.right(parent); |
| 123 | + self.tree[parent] = self.tree[left].operate(&self.tree[right]); |
| 124 | + if parent != self.root() { |
| 125 | + new_parents.insert(self.parent(parent)); |
| 126 | + } |
| 127 | + }); |
| 128 | + if new_parents.is_empty() { |
| 129 | + break; |
| 130 | + } else { |
| 131 | + std::mem::swap(&mut parents, &mut new_parents); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + fn update_ordered<I>(&mut self, values: I) |
| 137 | + where |
| 138 | + I: IntoIterator<Item = Value>, |
| 139 | + { |
| 140 | + values.into_iter().enumerate().for_each(|(idx, val)| { |
| 141 | + let leaf = self.leaf(idx); |
| 142 | + self.tree[leaf] = val; |
| 143 | + }); |
| 144 | + (0..self.leaf(0)).into_iter().rev().for_each(|parent| { |
| 145 | + let left = self.left(parent); |
| 146 | + let right = self.right(parent); |
| 147 | + self.tree[parent] = self.tree[left].operate(&self.tree[right]); |
| 148 | + }); |
| 149 | + } |
| 150 | + |
| 151 | + fn aggregate(&self) -> Value { |
| 152 | + self.tree[self.root()].clone() |
| 153 | + } |
| 154 | + |
| 155 | + fn prefix(&self, idx: usize) -> Value { |
| 156 | + let mut node = self.leaf(idx); |
| 157 | + let mut agg = self.tree[node].clone(); |
| 158 | + while node != self.root() { |
| 159 | + let parent = self.parent(node); |
| 160 | + if node == self.right(parent) { |
| 161 | + let left = self.left(parent); |
| 162 | + agg = self.tree[left].operate(&agg); |
| 163 | + } |
| 164 | + node = parent; |
| 165 | + } |
| 166 | + return agg; |
| 167 | + } |
| 168 | + |
| 169 | + fn suffix(&self, i: usize) -> Value { |
| 170 | + let mut node = self.leaf(i); |
| 171 | + let mut agg = self.tree[node].clone(); |
| 172 | + while node != self.root() { |
| 173 | + let parent = self.parent(node); |
| 174 | + if node == self.left(parent) { |
| 175 | + agg = agg.operate(&self.tree[self.right(parent)]); |
| 176 | + } |
| 177 | + node = parent; |
| 178 | + } |
| 179 | + return agg; |
| 180 | + } |
| 181 | +} |
0 commit comments