use super::*;
use crate::errors::PointCloudError;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone)]
pub struct BoolList {
data: Vec<bool>,
}
impl BoolList {
pub fn from_bools(data: Vec<bool>) -> Result<BoolList, PointCloudError> {
Ok(BoolList { data })
}
}
impl InternalValueList for BoolList {
fn new() -> ValueList {
ValueList::BoolList(BoolList { data: Vec::new() })
}
fn get(&self, i: usize) -> Result<Value, PointCloudError> {
Ok(Value::Bool(self.data[i]))
}
fn get_set(&self, indexes: &[usize]) -> Result<ValueList, PointCloudError> {
Ok(ValueList::BoolList(BoolList {
data: indexes.iter().map(|i| self.data[*i]).collect(),
}))
}
fn get_summary(&self, indexes: &[usize]) -> Result<ValueSummary, PointCloudError> {
let mut true_count = 0;
let mut false_count = 0;
for i in indexes {
if self.data[*i] {
true_count += 1;
} else {
false_count += 1;
}
}
Ok(ValueSummary::BoolSummary(BoolSummary {
true_count,
false_count,
}))
}
fn push(&mut self, x: Value) {
if let Value::Bool(x) = x {
self.data.push(x);
}
}
fn len(&self) -> usize {
self.data.len()
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct BoolSummary {
pub true_count: usize,
pub false_count: usize,
}
impl Summary for BoolSummary {
fn to_json(&self) -> String {
format!("t:{},f:{}", self.true_count, self.false_count)
}
fn combine(summaries: &[&ValueSummary]) -> Result<ValueSummary, PointCloudError> {
let mut true_count = 0;
let mut false_count = 0;
for vs in summaries {
if let ValueSummary::BoolSummary(vs) = vs {
true_count += vs.true_count;
false_count += vs.false_count;
} else {
return Err(PointCloudError::data_access(
0,
"Non-boolean summary passed to the boolean summary combine".to_string(),
));
}
}
Ok(ValueSummary::BoolSummary(BoolSummary {
true_count,
false_count,
}))
}
fn add(&mut self, v: &Value) -> Result<(), PointCloudError> {
if let Value::Bool(v) = v {
if *v {
self.true_count += 1;
} else {
self.false_count += 1;
}
Ok(())
} else {
Err(PointCloudError::data_access(
0,
"Non-boolean summary passed to the boolean summary update".to_string(),
))
}
}
}