@@ -12,6 +12,21 @@ use super::tskbox::TskBox;
1212use super :: TreeSequence ;
1313use super :: TskitError ;
1414
15+ macro_rules! safe_tsk_column_access_from_tree {
16+ ( $self: ident, $u: ident, $output_type: ty, $column: ident) => { {
17+ assert!( !$self. as_ll_ref( ) . $column. is_null( ) ) ;
18+ // SAFETY: minimum length is 1 so the only requirement
19+ // is that the column is not a null pointer
20+ unsafe {
21+ super :: tsk_column_access:: <$output_type, _, _, _>(
22+ $u,
23+ $self. as_ll_ref( ) . $column,
24+ $self. treeseq. num_nodes_raw( ) + 1 ,
25+ )
26+ }
27+ } } ;
28+ }
29+
1530pub struct LLTree < ' treeseq > {
1631 inner : TskBox < tsk_tree_t > ,
1732 flags : TreeFlags ,
@@ -82,55 +97,19 @@ impl<'treeseq> LLTree<'treeseq> {
8297 }
8398
8499 pub fn left_sib ( & self , u : NodeId ) -> Option < NodeId > {
85- assert ! ( !self . as_ll_ref( ) . left_sib. is_null( ) ) ;
86- // SAFETY: since the length is at least 1,
87- // the left_sib pointer cannot be NULL
88- unsafe {
89- super :: tsk_column_access :: < NodeId , _ , _ , _ > (
90- u,
91- self . as_ll_ref ( ) . left_sib ,
92- self . treeseq . num_nodes_raw ( ) + 1 ,
93- )
94- }
100+ safe_tsk_column_access_from_tree ! ( self , u, NodeId , left_sib)
95101 }
96102
97103 pub fn right_sib ( & self , u : NodeId ) -> Option < NodeId > {
98- assert ! ( !self . as_ll_ref( ) . right_sib. is_null( ) ) ;
99- // SAFETY: since the length is at least 1,
100- // the right_sib pointer cannot be NULL
101- unsafe {
102- super :: tsk_column_access :: < NodeId , _ , _ , _ > (
103- u,
104- self . as_ll_ref ( ) . right_sib ,
105- self . treeseq . num_nodes_raw ( ) + 1 ,
106- )
107- }
104+ safe_tsk_column_access_from_tree ! ( self , u, NodeId , right_sib)
108105 }
109106
110107 pub fn left_child ( & self , u : NodeId ) -> Option < NodeId > {
111- assert ! ( !self . as_ll_ref( ) . left_child. is_null( ) ) ;
112- // SAFETY: since the length is at least 1,
113- // the left_child pointer cannot be NULL
114- unsafe {
115- super :: tsk_column_access :: < NodeId , _ , _ , _ > (
116- u,
117- self . as_ll_ref ( ) . left_child ,
118- self . treeseq . num_nodes_raw ( ) + 1 ,
119- )
120- }
108+ safe_tsk_column_access_from_tree ! ( self , u, NodeId , left_child)
121109 }
122110
123111 pub fn right_child ( & self , u : NodeId ) -> Option < NodeId > {
124- assert ! ( !self . as_ll_ref( ) . right_child. is_null( ) ) ;
125- // SAFETY: since the length is at least 1,
126- // the right_child pointer cannot be NULL
127- unsafe {
128- super :: tsk_column_access :: < NodeId , _ , _ , _ > (
129- u,
130- self . as_ll_ref ( ) . right_child ,
131- self . treeseq . num_nodes_raw ( ) + 1 ,
132- )
133- }
112+ safe_tsk_column_access_from_tree ! ( self , u, NodeId , right_child)
134113 }
135114
136115 pub fn num_tracked_samples ( & self , u : NodeId ) -> Result < SizeType , TskitError > {
@@ -144,46 +123,19 @@ impl<'treeseq> LLTree<'treeseq> {
144123 }
145124
146125 pub fn left_sample ( & self , u : NodeId ) -> Option < NodeId > {
147- assert ! ( !self . as_ll_ref( ) . left_sample. is_null( ) ) ;
148- // SAFETY: since the length is at least 1,
149- // the left_sample pointer cannot be NULL
150- unsafe {
151- super :: tsk_column_access :: < NodeId , _ , _ , _ > (
152- u,
153- self . as_ll_ref ( ) . left_sample ,
154- self . treeseq . num_nodes_raw ( ) ,
155- )
156- }
126+ safe_tsk_column_access_from_tree ! ( self , u, NodeId , left_sample)
157127 }
158128
159129 pub fn right_sample ( & self , u : NodeId ) -> Option < NodeId > {
160- assert ! ( !self . as_ll_ref( ) . right_sample. is_null( ) ) ;
161- // SAFETY: since the length is at least 1,
162- // the right_sample pointer cannot be NULL
163- unsafe {
164- super :: tsk_column_access :: < NodeId , _ , _ , _ > (
165- u,
166- self . as_ll_ref ( ) . right_sample ,
167- self . treeseq . num_nodes_raw ( ) ,
168- )
169- }
130+ safe_tsk_column_access_from_tree ! ( self , u, NodeId , right_sample)
170131 }
171132
172133 pub fn samples ( & self , u : NodeId ) -> Result < impl Iterator < Item = NodeId > + ' _ , TskitError > {
173134 Ok ( NodeIteratorAdapter ( SamplesIterator :: new ( self , u) ?) )
174135 }
175136
176137 pub fn parent ( & self , u : NodeId ) -> Option < NodeId > {
177- assert ! ( !self . as_ll_ref( ) . parent. is_null( ) ) ;
178- // SAFETY: since the length is at least 1,
179- // the parent pointer cannot be NULL
180- unsafe {
181- super :: tsk_column_access :: < NodeId , _ , _ , _ > (
182- u,
183- self . as_ll_ref ( ) . parent ,
184- self . treeseq . num_nodes_raw ( ) + 1 ,
185- )
186- }
138+ safe_tsk_column_access_from_tree ! ( self , u, NodeId , parent)
187139 }
188140
189141 pub fn flags ( & self ) -> TreeFlags {
0 commit comments