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
//! Incremental bundle accumulator for streaming/sliding-window memory.
use crate::error::{MemoryError, Result};
use crate::hyperdim::HVec10240;
/// Incremental bundle accumulator for streaming/sliding-window memory.
///
/// Maintains signed bit counts for efficient add/remove operations.
/// Finalize applies majority threshold to produce a bundled hypervector.
#[derive(Debug, Clone)]
pub struct BundleAccumulator {
counts: Box<[i32; HVec10240::DIMENSION]>,
n: u32,
}
impl Default for BundleAccumulator {
fn default() -> Self {
Self::new()
}
}
impl BundleAccumulator {
/// Create a new empty accumulator.
pub fn new() -> Self {
Self {
counts: Box::new([0i32; HVec10240::DIMENSION]),
n: 0,
}
}
/// Add a hypervector to the accumulator.
pub fn add(&mut self, hv: &HVec10240) {
#[allow(clippy::needless_range_loop)]
for i in 0..80 {
for j in 0..128 {
if (hv.data[i] >> j) & 1 == 1 {
self.counts[i * 128 + j] += 1;
}
}
}
self.n += 1;
}
/// Remove a hypervector from the accumulator.
///
/// Saturates at zero: removing from an empty accumulator is a no-op.
/// Use [`try_remove`] if you need to detect underflow.
pub fn remove(&mut self, hv: &HVec10240) {
if self.n == 0 {
return;
}
#[allow(clippy::needless_range_loop)]
for i in 0..80 {
for j in 0..128 {
if (hv.data[i] >> j) & 1 == 1 {
self.counts[i * 128 + j] -= 1;
}
}
}
self.n -= 1;
}
/// Remove a hypervector from the accumulator, returning an error if empty.
///
/// Returns `Err(MemoryError::InvalidInput)` when the accumulator is empty.
pub fn try_remove(&mut self, hv: &HVec10240) -> Result<()> {
if self.n == 0 {
return Err(MemoryError::InvalidInput {
field: "accumulator".to_string(),
reason: "cannot remove from empty BundleAccumulator".to_string(),
});
}
#[allow(clippy::needless_range_loop)]
for i in 0..80 {
for j in 0..128 {
if (hv.data[i] >> j) & 1 == 1 {
self.counts[i * 128 + j] -= 1;
}
}
}
self.n -= 1;
Ok(())
}
/// Finalize the accumulator into a bundled hypervector.
///
/// Applies majority threshold: bits with count > 0 are set to 1.
/// Returns zero vector if accumulator is empty.
pub fn finalize(&self) -> HVec10240 {
if self.n == 0 {
return HVec10240::zero();
}
let mut data = [0u128; 80];
let threshold = 0; // Majority threshold: count > 0
#[allow(clippy::needless_range_loop)]
for i in 0..80 {
for j in 0..128 {
if self.counts[i * 128 + j] > threshold {
data[i] |= 1u128 << j;
}
}
}
HVec10240 { data }
}
/// Get the number of hypervectors in the accumulator.
pub fn len(&self) -> u32 {
self.n
}
/// Check if the accumulator is empty.
pub fn is_empty(&self) -> bool {
self.n == 0
}
/// Clear the accumulator.
pub fn clear(&mut self) {
*self.counts = [0i32; HVec10240::DIMENSION];
self.n = 0;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_bundle_accumulator_add_finalize() {
let v1 = HVec10240::random();
let v2 = HVec10240::random();
let v3 = HVec10240::random();
let mut acc = BundleAccumulator::new();
acc.add(&v1);
acc.add(&v2);
acc.add(&v3);
let bundled = acc.finalize();
// Bundle should be valid (not zero)
assert_ne!(bundled, HVec10240::zero());
// Should have 3 vectors
assert_eq!(acc.len(), 3);
}
#[test]
fn test_bundle_accumulator_remove() {
let v1 = HVec10240::random();
let v2 = HVec10240::random();
let mut acc = BundleAccumulator::new();
acc.add(&v1);
acc.add(&v2);
acc.remove(&v2);
assert_eq!(acc.len(), 1);
let bundled = acc.finalize();
// Single vector bundle should be close to the original
assert!(bundled.cosine_similarity(&v1) > 0.9);
}
#[test]
fn test_bundle_accumulator_empty() {
let acc = BundleAccumulator::new();
assert!(acc.is_empty());
assert_eq!(acc.finalize(), HVec10240::zero());
}
#[test]
fn test_bundle_accumulator_clear() {
let mut acc = BundleAccumulator::new();
acc.add(&HVec10240::random());
acc.clear();
assert!(acc.is_empty());
}
}