[lab2] Fixed bug in bit vector true's counter

This commit is contained in:
Andrew Golovashevich 2026-01-30 05:52:38 +03:00
parent e267807931
commit 647e87a1d2
3 changed files with 70 additions and 11 deletions

View File

@ -1,4 +1,5 @@
use std::cmp::min; use std::cmp::min;
use std::fmt::{Debug, Formatter};
use std::ops::{BitAnd, BitAndAssign, Index, IndexMut}; use std::ops::{BitAnd, BitAndAssign, Index, IndexMut};
pub struct BitVector { pub struct BitVector {
@ -8,15 +9,22 @@ pub struct BitVector {
impl BitVector { impl BitVector {
pub fn alloc(len: usize) -> Self { pub fn alloc(len: usize) -> Self {
return Self { return Self {
data: vec![false; len] data: vec![false; len].into_boxed_slice(),
.into_boxed_slice(),
}; };
} }
pub fn alloc_from<const L: usize>(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 { pub fn count1(&self) -> usize {
let mut counter = 0usize; let mut counter = 0usize;
for i in 0..(self.data.len() - 1) { for e in &self.data {
if self.data[i] { if *e {
counter += 1; counter += 1;
} }
} }
@ -49,7 +57,11 @@ impl BitAnd<Self> for &BitVector {
impl BitAndAssign<&Self> for BitVector { impl BitAndAssign<&Self> for BitVector {
fn bitand_assign(&mut self, rhs: &Self) { 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() { for i in 0..self.data.len() {
self.data[i] &= rhs.data[i] self.data[i] &= rhs.data[i]
} }
@ -61,15 +73,14 @@ impl Index<usize> for BitVector {
fn index(&self, index: usize) -> &Self::Output { fn index(&self, index: usize) -> &Self::Output {
assert!(index < self.data.len()); assert!(index < self.data.len());
return &self.data[index] return &self.data[index];
} }
} }
impl IndexMut<usize> for BitVector { impl IndexMut<usize> for BitVector {
fn index_mut(&mut self, index: usize) -> &mut Self::Output { fn index_mut(&mut self, index: usize) -> &mut Self::Output {
assert!(index < self.data.len()); assert!(index < self.data.len());
return &mut self.data[index] return &mut self.data[index];
} }
} }
@ -82,3 +93,21 @@ impl BitVector {
return new; return new;
} }
} }
impl Debug for BitVector {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if let Err(e) = write!(f, "<bit vector [") {
return Err(e);
};
for e in &self.data {
if let Err(e) = write!(f, "{}", if *e { '+' } else { '0' }) {
return Err(e);
}
}
if let Err(e) = write!(f, "]>") {
return Err(e);
}
return Ok(());
}
}

View File

@ -1,5 +1,5 @@
use super::BitVector; use super::BitVector;
use super::operations::attentivenessTest; use super::operations::attentivenessCheck;
use super::operations::similarityCheck; use super::operations::similarityCheck;
pub fn adaptiveResonanceTheoryImpl( pub fn adaptiveResonanceTheoryImpl(
@ -20,7 +20,7 @@ pub fn adaptiveResonanceTheoryImpl(
if !similarityCheck(&prototypes[j], &data[i], beta) { if !similarityCheck(&prototypes[j], &data[i], beta) {
continue; continue;
} }
if !attentivenessTest(&prototypes[j], &data[i], attentiveness) { if !attentivenessCheck(&prototypes[j], &data[i], attentiveness) {
continue; continue;
} }
groups[i] = j; groups[i] = j;

View File

@ -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(); let r = vector.count1().to_f64() / (beta + vector.len()).to_f64();
return l > r; 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; 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
))
}