Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. use std::marker::PhantomData;
  2.  
  3. #[derive(Clone)]
  4. pub enum ColumnType {
  5. Float(Vec<f32>),
  6. Boolean(Vec<bool>),
  7. }
  8.  
  9. #[derive(Clone)]
  10. pub struct ColumnData {
  11. pub data: ColumnType,
  12. pub data_len: usize,
  13. }
  14.  
  15. // TODO - Add in Ref for ColumnType
  16. #[derive(Clone)]
  17. pub struct ColumnView<'a, T> {
  18. pub data: &'a ColumnData,
  19. pub indices_len: usize,
  20. pub indices: Vec<usize>,
  21. pub index: usize,
  22. pub phantom: PhantomData<T>,
  23. }
  24.  
  25. macro_rules! impl_view {
  26. ($e:ty, $f:path) => {
  27. impl<'a> Iterator for ColumnView<'a, $e> {
  28. type Item = $e;
  29. fn next(&mut self) -> Option<Self::Item> {
  30. if self.index >= self.indices_len {
  31. None
  32. } else {
  33. self.index += 1_usize;
  34. Some(unsafe {
  35. match &self.data.data {
  36. $f(column) => column.get_unchecked(self.indices[self.index - 1]).clone(),
  37. _ => unreachable!()
  38. }
  39. })
  40. }
  41. }
  42. }
  43. }
  44. }
  45.  
  46. impl_view!(f32, ColumnType::Float);
  47. impl_view!(bool, ColumnType::Boolean);
  48.  
  49. fn main() {
  50. let data: ColumnData = ColumnData {
  51. data: ColumnType::Float(vec![0.0, 1.0, 2.0, 3.0]),
  52. data_len: 4_usize,
  53. };
  54. let c = ColumnView::<f32> {
  55. data: &data,
  56. indices_len: 3,
  57. indices: vec![0_usize, 1, 2],
  58. index: 0,
  59. phantom: PhantomData::<f32>,
  60. };
  61. for x in c {
  62. dbg!(x);
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement