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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use std::marker::PhantomData;

use super::{position_accessors, tags, PrimInt};

// TODO remove PrimInt, also in impls
pub struct Offsets<Idx: PrimInt, Config = tags::TopDownFull> {
    /// offsets to go through a tree from top to bottom
    offsets: Vec<Idx>,
    _phantom: PhantomData<Config>,
}
impl<Idx: PrimInt, C> Into<Vec<Idx>> for Offsets<Idx, C> {
    fn into(self) -> Vec<Idx> {
        self.offsets
    }
}

pub struct OffsetsRef<'a, Idx: PrimInt, Config = tags::TopDownFull> {
    /// offsets to go through a tree from top to bottom
    offsets: &'a [Idx],
    _phantom: PhantomData<Config>,
}
impl<'a, Idx: PrimInt> From<&'a [Idx]> for OffsetsRef<'a, Idx> {
    fn from(offsets: &'a [Idx]) -> Self {
        Self {
            offsets,
            _phantom: PhantomData,
        }
    }
}
impl<'a, Idx: PrimInt> OffsetsRef<'a, Idx> {
    pub fn with_root<IdN>(self, root: IdN) -> RootedOffsetsRef<'a, IdN, Idx> {
        RootedOffsetsRef {
            root,
            offsets: self.offsets,
        }
    }
}

pub struct RootedOffsets<IdN, Idx> {
    root: IdN,
    /// offsets to go through a tree from top to bottom
    offsets: Vec<Idx>,
}

pub struct RootedOffsetsRef<'a, IdN, Idx> {
    root: IdN,
    /// offsets to go through a tree from top to bottom
    offsets: &'a [Idx],
}

impl<'a, IdN, Idx> super::node_filter_traits::Full for RootedOffsetsRef<'a, IdN, Idx> {}

impl<'a, IdN: Copy, Idx> position_accessors::RootedPosition<IdN> for RootedOffsetsRef<'a, IdN, Idx> {
    fn root(&self) -> IdN {
        self.root
    }
}
impl<'a, IdN: Copy, Idx: PrimInt> position_accessors::WithOffsets
    for RootedOffsetsRef<'a, IdN, Idx>
{
    type Idx = Idx;
}

impl<'a, IdN: Copy, Idx: PrimInt> position_accessors::WithPreOrderOffsets
    for RootedOffsetsRef<'a, IdN, Idx>
{
    type It = std::iter::Copied<std::slice::Iter<'a, Idx>>;

    fn iter(&self) -> Self::It {
        self.offsets.iter().copied()
    }
}

impl<'a, IdN: Copy, Idx: PrimInt> RootedOffsetsRef<'a, IdN, Idx> {
    pub fn with_store<'store, HAST>(
        &'a self,
        stores: &'store HAST,
    ) -> super::WithHyperAstPositionConverter<'store, 'a, Self, HAST> {
        super::PositionConverter::new(self).with_stores(stores)
    }
}

pub(super) struct SolvedPathPosition<IdN, Idx> {
    root: IdN,
    /// offsets to go through a tree from top to bottom
    offsets: Vec<Idx>,
    node: IdN,
}

mod impl_receivers {
    use super::super::building;
    use building::top_down;
    use super::super::PrimInt;
    use super::tags;
    use super::Offsets;

    impl<Idx: PrimInt, C> building::top_down::CreateBuilder for Offsets<Idx, C> {
        fn create() -> Self {
            Self {
                offsets: vec![],
                _phantom: std::marker::PhantomData,
            }
        }
    }

    impl<IdN, Idx: PrimInt, C> top_down::ReceiveParent<IdN, Self> for Offsets<Idx, C> {
        fn push(self, _parent: IdN) -> Self {
            self
        }
    }

    impl<Idx: PrimInt, C> building::top_down::ReceiveDirName<Self> for Offsets<Idx, C> {
        fn push(self, _dir_name: &str) -> Self {
            self
        }
    }

    impl<Idx: PrimInt> building::top_down::ReceiveIdx<Idx, Self> for Offsets<Idx> {
        fn push(mut self, idx: Idx) -> Self {
            self.offsets.push(idx);
            self
        }
    }

    impl<Idx: PrimInt> building::top_down::ReceiveIdx<Idx, Self> for Offsets<Idx, tags::TopDownNoSpace> {
        fn push(self, _idx: Idx) -> Self {
            // self.offsets.push(idx);
            self
        }
    }

    impl<Idx: PrimInt> building::top_down::ReceiveIdxNoSpace<Idx, Self> for Offsets<Idx> {
        fn push(self, _idx: Idx) -> Self {
            //self.offsets.push(idx);
            self
        }
    }

    impl<Idx: PrimInt> building::top_down::ReceiveIdxNoSpace<Idx, Self> for Offsets<Idx, tags::TopDownNoSpace> {
        fn push(mut self, idx: Idx) -> Self {
            self.offsets.push(idx);
            self
        }
    }

    impl<Idx: PrimInt, C> top_down::FileSysReceiver for Offsets<Idx, C> {
        type InFile<O> = Self;
    }

    impl<Idx: PrimInt, IdO: PrimInt, C> building::top_down::ReceiveOffset<IdO, Self> for Offsets<Idx, C> {
        fn push(self, _bytes: IdO) -> Self {
            self
        }
    }
    impl<Idx: PrimInt, IdO, C> building::SetLen<IdO, Self> for Offsets<Idx, C> {
        fn set(self, _len: IdO) -> Self {
            self
        }
    }
    impl<IdN, Idx: PrimInt, C> top_down::SetNode<IdN, Self> for Offsets<Idx, C> {
        fn set_node(self, _node: IdN) -> Self {
            self
        }
    }
    impl<Idx: PrimInt, C> top_down::SetFileName<Self> for Offsets<Idx, C> {
        fn set_file_name(self, _file_name: &str) -> Self {
            self
        }
    }
    impl<Idx: PrimInt, C> building::Transition<Self> for Offsets<Idx, C> {
        fn transit(self) -> Self {
            self
        }
    }

    // impl<IdN, Idx, IdO: PrimInt> top_down::ReceiveInFile<IdN, Idx, Self> for Offsets<Idx> {
    //     type S1 = Self;

    //     type S2 = Self;

    //     fn finish(self) -> Self {
    //         self
    //     }
    // }
    // impl<IdN, Idx, IdO: PrimInt> top_down::ReceiveDir<IdN, Idx, Self> for Offsets<Idx> {
    //     type SA1 = Self;

    //     type SA2 = Self;

    //     type SB1 = Self;

    //     fn go_inside_file(mut self, file_name: &str) -> Self::SB1 {
    //         self.file.push(file_name);
    //         self
    //     }

    //     fn finish(self) -> Self {
    //         self
    //     }
    // }
}