From 647e87a1d20c3587061fac3763d80b797e523dd1 Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Fri, 30 Jan 2026 05:52:38 +0300 Subject: [PATCH] [lab2] Fixed bug in bit vector true's counter --- lab2/src/algo/bit_vector.rs | 45 ++++++++++++++++++++++++++++++------- lab2/src/algo/impl.rs | 4 ++-- lab2/src/algo/operations.rs | 32 +++++++++++++++++++++++++- 3 files changed, 70 insertions(+), 11 deletions(-) diff --git a/lab2/src/algo/bit_vector.rs b/lab2/src/algo/bit_vector.rs index d2f5c6f..228d16d 100644 --- a/lab2/src/algo/bit_vector.rs +++ b/lab2/src/algo/bit_vector.rs @@ -1,4 +1,5 @@ use std::cmp::min; +use std::fmt::{Debug, Formatter}; use std::ops::{BitAnd, BitAndAssign, Index, IndexMut}; pub struct BitVector { @@ -8,15 +9,22 @@ pub struct BitVector { impl BitVector { pub fn alloc(len: usize) -> Self { return Self { - data: vec![false; len] - .into_boxed_slice(), + data: vec![false; len].into_boxed_slice(), }; } + pub fn alloc_from(initData: &[bool; L]) -> Self { + let mut v = Self::alloc(L); + for i in 0..initData.len() { + v[i] = initData[i] + } + return v; + } + pub fn count1(&self) -> usize { let mut counter = 0usize; - for i in 0..(self.data.len() - 1) { - if self.data[i] { + for e in &self.data { + if *e { counter += 1; } } @@ -49,7 +57,11 @@ impl BitAnd for &BitVector { impl BitAndAssign<&Self> for BitVector { fn bitand_assign(&mut self, rhs: &Self) { - assert_eq!(self.data.len(), rhs.data.len(), "Vectors has different length's"); + assert_eq!( + self.data.len(), + rhs.data.len(), + "Vectors has different length's" + ); for i in 0..self.data.len() { self.data[i] &= rhs.data[i] } @@ -61,15 +73,14 @@ impl Index for BitVector { fn index(&self, index: usize) -> &Self::Output { assert!(index < self.data.len()); - return &self.data[index] + return &self.data[index]; } } - impl IndexMut for BitVector { fn index_mut(&mut self, index: usize) -> &mut Self::Output { assert!(index < self.data.len()); - return &mut self.data[index] + return &mut self.data[index]; } } @@ -82,3 +93,21 @@ impl BitVector { return new; } } + +impl Debug for BitVector { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + if let Err(e) = write!(f, "") { + return Err(e); + } + return Ok(()); + } +} diff --git a/lab2/src/algo/impl.rs b/lab2/src/algo/impl.rs index 8eadb8d..95223f9 100644 --- a/lab2/src/algo/impl.rs +++ b/lab2/src/algo/impl.rs @@ -1,5 +1,5 @@ use super::BitVector; -use super::operations::attentivenessTest; +use super::operations::attentivenessCheck; use super::operations::similarityCheck; pub fn adaptiveResonanceTheoryImpl( @@ -20,7 +20,7 @@ pub fn adaptiveResonanceTheoryImpl( if !similarityCheck(&prototypes[j], &data[i], beta) { continue; } - if !attentivenessTest(&prototypes[j], &data[i], attentiveness) { + if !attentivenessCheck(&prototypes[j], &data[i], attentiveness) { continue; } groups[i] = j; diff --git a/lab2/src/algo/operations.rs b/lab2/src/algo/operations.rs index 02455ea..564a3ba 100644 --- a/lab2/src/algo/operations.rs +++ b/lab2/src/algo/operations.rs @@ -6,6 +6,36 @@ pub(super) fn similarityCheck(prototype: &BitVector, vector: &BitVector, beta: u let r = vector.count1().to_f64() / (beta + vector.len()).to_f64(); return l > r; } -pub(super) fn attentivenessTest(prototype: &BitVector, vector: &BitVector, p: f64) -> bool { + +pub(super) fn attentivenessCheck(prototype: &BitVector, vector: &BitVector, p: f64) -> bool { return ((prototype & vector).count1().to_f64() / vector.count1().to_f64()) >= p; } + +#[test] +fn test_attentivenessCheck_1() { + assert!(attentivenessCheck( + &BitVector::alloc_from(&[false, true]), + &BitVector::alloc_from(&[false, true]), + 0.99 + )) +} + + +#[test] +fn test_attentivenessCheck_2() { + assert!(!attentivenessCheck( + &BitVector::alloc_from(&[false, true]), + &BitVector::alloc_from(&[true, true]), + 0.99 + )) +} + + +#[test] +fn test_attentivenessCheck_3() { + assert!(attentivenessCheck( + &BitVector::alloc_from(&[false, true]), + &BitVector::alloc_from(&[true, true]), + 0.49 + )) +}