vortex_layout/
context.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use vortex_array::aliases::hash_map::HashMap;

use crate::layouts::chunked::ChunkedLayout;
use crate::layouts::flat::FlatLayout;
use crate::layouts::struct_::StructLayout;
use crate::vtable::LayoutVTableRef;
use crate::LayoutId;

#[derive(Debug, Clone)]
pub struct LayoutContext {
    layout_refs: HashMap<LayoutId, LayoutVTableRef>,
}

pub type LayoutContextRef = std::sync::Arc<LayoutContext>;

impl LayoutContext {
    pub fn new(layout_refs: HashMap<LayoutId, LayoutVTableRef>) -> Self {
        Self { layout_refs }
    }

    pub fn with_layout(mut self, layout: LayoutVTableRef) -> Self {
        self.layout_refs.insert(layout.id(), layout);
        self
    }

    pub fn with_layouts<E: IntoIterator<Item = LayoutVTableRef>>(mut self, layouts: E) -> Self {
        self.layout_refs
            .extend(layouts.into_iter().map(|e| (e.id(), e)));
        self
    }

    pub fn lookup_layout(&self, id: LayoutId) -> Option<LayoutVTableRef> {
        self.layout_refs.get(&id).cloned()
    }
}

impl Default for LayoutContext {
    fn default() -> Self {
        Self::new(
            [
                LayoutVTableRef::from_static(&ChunkedLayout),
                LayoutVTableRef::from_static(&FlatLayout),
                LayoutVTableRef::from_static(&StructLayout),
            ]
            .into_iter()
            .map(|l| (l.id(), l))
            .collect(),
        )
    }
}