diff --git a/lab2/src/algo/bit_vector.rs b/lab2/src/algo/bit_vector.rs index 089f2e6..58d2057 100644 --- a/lab2/src/algo/bit_vector.rs +++ b/lab2/src/algo/bit_vector.rs @@ -1,7 +1,7 @@ use std::cmp::min; use std::ops::{BitAnd, BitAndAssign, Index, IndexMut, Not, Shl}; -pub(super) struct BitVector { +pub struct BitVector { len: usize, data: Box<[usize]>, } @@ -27,7 +27,12 @@ impl BitVector { return counter; } + + pub fn len(&self) -> usize { + return self.len; + } } + impl Clone for BitVector { fn clone(&self) -> Self { return Self { @@ -37,10 +42,10 @@ impl Clone for BitVector { } } -impl BitAnd<&Self> for BitVector { - type Output = Self; +impl BitAnd for &BitVector { + type Output = BitVector; - fn bitand(self, rhs: &Self) -> Self::Output { + fn bitand(self, rhs: Self) -> Self::Output { let mut intersected = self.clone(); intersected &= rhs; return intersected; diff --git a/lab2/src/algo/impl.rs b/lab2/src/algo/impl.rs new file mode 100644 index 0000000..cc506f9 --- /dev/null +++ b/lab2/src/algo/impl.rs @@ -0,0 +1,35 @@ +use super::BitVector; +use super::operations::attentivenessTest; +use super::operations::similarityCheck; + +pub fn adaptiveResonanceTheoryImpl( + data: &[BitVector], + beta: usize, + attentiveness: f64, +) -> Box<[usize]> { + let mut prototypes = Vec::::new(); + let mut groups = vec![0usize, data.len()].into_boxed_slice(); + + match data.get(0) { + None => return groups, + Some(p) => prototypes.push(p.clone()), + } + prototypes.push(data[0].clone()); + + 'elements: for i in 1..data.len() { + for j in 0..prototypes.len() { + if !similarityCheck(&prototypes[j], &data[i], beta) { + continue; + } + if !attentivenessTest(&prototypes[j], &data[i], attentiveness) { + continue; + } + groups[i] = j; + continue 'elements; + } + + prototypes.push(data[i].clone()); + } + + return groups; +} diff --git a/lab2/src/algo/mod.rs b/lab2/src/algo/mod.rs index c7f6a96..fc23934 100644 --- a/lab2/src/algo/mod.rs +++ b/lab2/src/algo/mod.rs @@ -1 +1,5 @@ mod bit_vector; +mod operations; +mod r#impl; + +pub use bit_vector::BitVector; \ No newline at end of file diff --git a/lab2/src/algo/operations.rs b/lab2/src/algo/operations.rs new file mode 100644 index 0000000..57ad7a6 --- /dev/null +++ b/lab2/src/algo/operations.rs @@ -0,0 +1,10 @@ +use super::BitVector; +use eframe::emath::Numeric; + +pub(super) fn similarityCheck(prototype: &BitVector, vector: &BitVector, beta: usize) -> bool { + return ((prototype & vector).count1() / (beta + prototype.count1())) + > (vector.count1() / (beta + vector.len())); +} +pub(super) fn attentivenessTest(prototype: &BitVector, vector: &BitVector, p: f64) -> bool { + return ((prototype & vector).count1() / (vector.count1())).to_f64() < p; +}