use std::mem;
use crate::common::BLOCK_SIZE;
#[derive(Clone, Debug)]
pub struct Block {
num_bands: usize,
num_channels: usize,
data: Vec<f32>,
}
impl Block {
pub fn new(num_bands: usize, num_channels: usize) -> Self {
Self {
num_bands,
num_channels,
data: vec![0.0; num_bands * num_channels * BLOCK_SIZE],
}
}
pub fn new_with_value(num_bands: usize, num_channels: usize, value: f32) -> Self {
Self {
num_bands,
num_channels,
data: vec![value; num_bands * num_channels * BLOCK_SIZE],
}
}
pub fn num_bands(&self) -> usize {
self.num_bands
}
pub fn num_channels(&self) -> usize {
self.num_channels
}
pub fn set_num_channels(&mut self, num_channels: usize) {
self.num_channels = num_channels;
self.data
.resize(self.num_bands * self.num_channels * BLOCK_SIZE, 0.0);
self.data.fill(0.0);
}
pub fn view(&self, band: usize, channel: usize) -> &[f32] {
let idx = self.get_index(band, channel);
&self.data[idx..idx + BLOCK_SIZE]
}
pub fn view_mut(&mut self, band: usize, channel: usize) -> &mut [f32] {
let idx = self.get_index(band, channel);
&mut self.data[idx..idx + BLOCK_SIZE]
}
pub fn swap(&mut self, other: &mut Self) {
mem::swap(&mut self.num_bands, &mut other.num_bands);
mem::swap(&mut self.num_channels, &mut other.num_channels);
mem::swap(&mut self.data, &mut other.data);
}
fn get_index(&self, band: usize, channel: usize) -> usize {
(band * self.num_channels + channel) * BLOCK_SIZE
}
}