Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::marker::PhantomData;
- #[derive(Clone)]
- pub enum ColumnType {
- Float(Vec<f32>),
- Boolean(Vec<bool>),
- }
- #[derive(Clone)]
- pub struct ColumnData {
- pub data: ColumnType,
- pub data_len: usize,
- }
- // TODO - Add in Ref for ColumnType
- #[derive(Clone)]
- pub struct ColumnView<'a, T> {
- pub data: &'a ColumnData,
- pub indices_len: usize,
- pub indices: Vec<usize>,
- pub index: usize,
- pub phantom: PhantomData<T>,
- }
- macro_rules! impl_view {
- ($e:ty, $f:path) => {
- impl<'a> Iterator for ColumnView<'a, $e> {
- type Item = $e;
- fn next(&mut self) -> Option<Self::Item> {
- if self.index >= self.indices_len {
- None
- } else {
- self.index += 1_usize;
- Some(unsafe {
- match &self.data.data {
- $f(column) => column.get_unchecked(self.indices[self.index - 1]).clone(),
- _ => unreachable!()
- }
- })
- }
- }
- }
- }
- }
- impl_view!(f32, ColumnType::Float);
- impl_view!(bool, ColumnType::Boolean);
- fn main() {
- let data: ColumnData = ColumnData {
- data: ColumnType::Float(vec![0.0, 1.0, 2.0, 3.0]),
- data_len: 4_usize,
- };
- let c = ColumnView::<f32> {
- data: &data,
- indices_len: 3,
- indices: vec![0_usize, 1, 2],
- index: 0,
- phantom: PhantomData::<f32>,
- };
- for x in c {
- dbg!(x);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement