mod tests;
use crate::DataType;
#[derive(Debug, Clone, PartialEq)]
pub enum DataVector {
I8(Vec<i8>),
U8(Vec<u8>),
I16(Vec<i16>),
I32(Vec<i32>),
F32(Vec<f32>),
F64(Vec<f64>),
}
impl DataVector {
pub(crate) fn new(data_type: DataType, length: usize) -> Self {
match data_type {
DataType::I8 => DataVector::I8(vec![0; length]),
DataType::U8 => DataVector::U8(vec![0; length]),
DataType::I16 => DataVector::I16(vec![0; length]),
DataType::I32 => DataVector::I32(vec![0; length]),
DataType::F32 => DataVector::F32(vec![0.0; length]),
DataType::F64 => DataVector::F64(vec![0.0; length]),
}
}
pub fn data_type(&self) -> DataType {
match self {
DataVector::I8(_) => DataType::I8,
DataVector::U8(_) => DataType::U8,
DataVector::I16(_) => DataType::I16,
DataVector::I32(_) => DataType::I32,
DataVector::F32(_) => DataType::F32,
DataVector::F64(_) => DataType::F64,
}
}
pub fn len(&self) -> usize {
match self {
DataVector::I8(data) => data.len(),
DataVector::U8(data) => data.len(),
DataVector::I16(data) => data.len(),
DataVector::I32(data) => data.len(),
DataVector::F32(data) => data.len(),
DataVector::F64(data) => data.len(),
}
}
pub fn is_empty(&self) -> bool {
match self {
DataVector::I8(data) => data.is_empty(),
DataVector::U8(data) => data.is_empty(),
DataVector::I16(data) => data.is_empty(),
DataVector::I32(data) => data.is_empty(),
DataVector::F32(data) => data.is_empty(),
DataVector::F64(data) => data.is_empty(),
}
}
pub fn get_i8(&self) -> Option<&[i8]> {
match self {
DataVector::I8(data) => Some(data),
DataVector::U8(_) => None,
DataVector::I16(_) => None,
DataVector::I32(_) => None,
DataVector::F32(_) => None,
DataVector::F64(_) => None,
}
}
pub fn get_u8(&self) -> Option<&[u8]> {
match self {
DataVector::I8(_) => None,
DataVector::U8(data) => Some(data),
DataVector::I16(_) => None,
DataVector::I32(_) => None,
DataVector::F32(_) => None,
DataVector::F64(_) => None,
}
}
pub(crate) fn get_as_string(&self) -> Option<String> {
match self {
DataVector::I8(_) => None,
DataVector::U8(data) => String::from_utf8(data.to_vec()).ok(),
DataVector::I16(_) => None,
DataVector::I32(_) => None,
DataVector::F32(_) => None,
DataVector::F64(_) => None,
}
}
pub fn get_i16(&self) -> Option<&[i16]> {
match self {
DataVector::I8(_) => None,
DataVector::U8(_) => None,
DataVector::I16(data) => Some(data),
DataVector::I32(_) => None,
DataVector::F32(_) => None,
DataVector::F64(_) => None,
}
}
pub fn get_i32(&self) -> Option<&[i32]> {
match self {
DataVector::I8(_) => None,
DataVector::U8(_) => None,
DataVector::I16(_) => None,
DataVector::I32(data) => Some(data),
DataVector::F32(_) => None,
DataVector::F64(_) => None,
}
}
pub fn get_f32(&self) -> Option<&[f32]> {
match self {
DataVector::I8(_) => None,
DataVector::U8(_) => None,
DataVector::I16(_) => None,
DataVector::I32(_) => None,
DataVector::F32(data) => Some(data),
DataVector::F64(_) => None,
}
}
pub fn get_f64(&self) -> Option<&[f64]> {
match self {
DataVector::I8(_) => None,
DataVector::U8(_) => None,
DataVector::I16(_) => None,
DataVector::I32(_) => None,
DataVector::F32(_) => None,
DataVector::F64(data) => Some(data),
}
}
pub fn get_i8_into(self) -> Result<Vec<i8>, DataVector> {
if let DataVector::I8(data) = self {
return Ok(data);
}
Err(self)
}
pub fn get_u8_into(self) -> Result<Vec<u8>, DataVector> {
if let DataVector::U8(data) = self {
return Ok(data);
}
Err(self)
}
pub fn get_i16_into(self) -> Result<Vec<i16>, DataVector> {
if let DataVector::I16(data) = self {
return Ok(data);
}
Err(self)
}
pub fn get_i32_into(self) -> Result<Vec<i32>, DataVector> {
if let DataVector::I32(data) = self {
return Ok(data);
}
Err(self)
}
pub fn get_f32_into(self) -> Result<Vec<f32>, DataVector> {
if let DataVector::F32(data) = self {
return Ok(data);
}
Err(self)
}
pub fn get_f64_into(self) -> Result<Vec<f64>, DataVector> {
if let DataVector::F64(data) = self {
return Ok(data);
}
Err(self)
}
}