[lab2] Algo implementation

This commit is contained in:
Andrew Golovashevich 2026-01-29 22:48:10 +03:00
parent dc98c25c41
commit cda30bd374
4 changed files with 58 additions and 4 deletions

View File

@ -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<Self> 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;

35
lab2/src/algo/impl.rs Normal file
View File

@ -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::<BitVector>::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;
}

View File

@ -1 +1,5 @@
mod bit_vector;
mod operations;
mod r#impl;
pub use bit_vector::BitVector;

View File

@ -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;
}