Skip to main content

gammalooprs/utils/
index_vec.rs

1#[macro_export]
2macro_rules! define_index {
3    (
4$(#[$idx_meta:meta])*
5$idx_vis:vis struct $Idx:ident ;) => {
6    $(#[$idx_meta])*
7    #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash,serde::Serialize, serde::Deserialize,bincode::Encode, bincode::Decode)]
8    $idx_vis struct $Idx(pub usize);
9
10    impl ::std::convert::From<usize> for $Idx {
11        fn from(value: usize) -> Self {
12            $Idx(value)
13        }
14    }
15
16    impl ::std::ops::Add<$Idx> for $Idx {
17        type Output = $Idx;
18
19        fn add(self, other: $Idx) -> Self::Output {
20            $Idx(self.0 + other.0)
21        }
22    }
23
24    impl ::std::ops::Sub<$Idx> for $Idx {
25        type Output = $Idx;
26
27        fn sub(self, other: $Idx) -> Self::Output {
28            $Idx(self.0 - other.0)
29        }
30    }
31
32    impl ::std::ops::AddAssign<$Idx> for $Idx {
33        fn add_assign(&mut self, other: $Idx) {
34            self.0 += other.0;
35        }
36    }
37
38    impl ::std::ops::SubAssign<$Idx> for $Idx {
39        fn sub_assign(&mut self, other: $Idx) {
40            self.0 -= other.0;
41        }
42    }
43
44    impl ::std::convert::From<$Idx> for usize {
45        fn from(value: $Idx) -> Self {
46            value.0
47        }
48    }
49};
50}
51
52#[macro_export]
53macro_rules! define_indexed_vec {
54    (
55        $Idx:ident ;
56
57        $(#[$vec_meta:meta])*
58        $vec_vis:vis struct $Vec:ident < $Wrapper:ident < $GenericParam:ident > > ;
59    ) => {
60
61
62
63
64
65
66        /* ——————————————————— vector new‑type ——————————————————— */
67
68        $(#[$vec_meta])*
69        #[derive(Clone, Debug, Default, Hash, PartialEq, Eq,PartialOrd,Ord,serde::Serialize, serde::Deserialize,bincode::Encode, bincode::Decode)]
70        $vec_vis struct $Vec<$GenericParam>(::std::vec::Vec<$Wrapper<$GenericParam>>);
71
72        /* --- Restricted indexing -------------------------------------------------- */
73
74        impl<$GenericParam> ::std::ops::Index<$Idx> for $Vec<$GenericParam> {
75            type Output = $Wrapper<$GenericParam>;
76            #[inline] fn index(&self, i: $Idx) -> &Self::Output { &self.0[i.0] }
77        }
78        impl<$GenericParam> ::std::ops::IndexMut<$Idx> for $Vec<$GenericParam> {
79            #[inline] fn index_mut(&mut self, i: $Idx) -> &mut Self::Output { &mut self.0[i.0] }
80        }
81
82        impl<$GenericParam>  ::std::convert::AsMut<[$Wrapper<$GenericParam>]> for $Vec<$GenericParam>{
83            fn as_mut(&mut self)->&mut [$Wrapper<$GenericParam>]{
84                self.0.as_mut()
85            }
86        }
87
88        impl<$GenericParam> linnet::half_edge::swap::Swap<$Idx> for $Vec<$GenericParam>{
89            fn swap(&mut self, a: $Idx, b: $Idx) {
90                self.0.swap(a.0, b.0);
91            }
92
93            fn len(&self) -> $Idx {
94                $Idx(self.0.len())
95            }
96            #[inline] fn is_empty(&self) -> bool { self.0.is_empty() }
97
98        }
99
100        /* --- Delegated Vec<T> API ------------------------------------------------- */
101
102        ::paste::paste! {
103                    #[derive(Clone)]
104                    $vec_vis struct [<$Vec Iter>]<'a, $GenericParam>(std::iter::Map<
105                        std::iter::Enumerate<std::slice::Iter<'a, $Wrapper<$GenericParam>>>,
106                        fn((usize, &$Wrapper<$GenericParam>)) -> ($Idx, &$Wrapper<$GenericParam>),
107                    >);
108                    impl<'a, $GenericParam> ::std::iter::Iterator for [<$Vec Iter>]<'a, $GenericParam> {
109                        type Item = ($Idx, &'a $Wrapper<$GenericParam>);
110                        #[inline] fn next(&mut self) -> Option<Self::Item> { self.0.next() }
111                        #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.0.size_hint() }
112                    }
113                    impl<'a, $GenericParam> ::std::iter::DoubleEndedIterator for [<$Vec Iter>]<'a, $GenericParam> {
114                        #[inline] fn next_back(&mut self) -> Option<Self::Item> { self.0.next_back() }
115                    }
116                    impl<'a, $GenericParam> ::std::iter::ExactSizeIterator for [<$Vec Iter>]<'a, $GenericParam> {}
117                    impl<'a, $GenericParam> ::std::iter::FusedIterator     for [<$Vec Iter>]<'a, $GenericParam> {}
118
119
120                    impl<'a, $GenericParam> ::std::iter::IntoIterator for &'a $Vec<$GenericParam> {
121                        type Item = ($Idx, &'a $Wrapper<$GenericParam>);
122                        type IntoIter = [<$Vec Iter>]<'a, $GenericParam>;
123                        fn into_iter(self) -> Self::IntoIter {
124                            [<$Vec Iter>](self.0.iter().enumerate().map(|(u, t)| ($Idx(u), t)))
125                        }
126                    }
127
128                    $vec_vis struct [<$Vec IterMut>]<'a, $GenericParam>(::std::iter::Map<
129                        std::iter::Enumerate<std::slice::IterMut<'a, $Wrapper<$GenericParam>>>,
130                        fn((usize, &mut $Wrapper<$GenericParam>)) -> ($Idx, &mut $Wrapper<$GenericParam>),
131                    >);
132                    impl<'a, $GenericParam> ::std::iter::Iterator for [<$Vec IterMut>]<'a, $GenericParam> {
133                        type Item = ($Idx, &'a mut $Wrapper<$GenericParam>);
134                        #[inline] fn next(&mut self) -> Option<Self::Item> { self.0.next() }
135                        #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.0.size_hint() }
136                    }
137                    impl<'a, $GenericParam> ::std::iter::DoubleEndedIterator for [<$Vec IterMut>]<'a, $GenericParam> {
138                        #[inline] fn next_back(&mut self) -> Option<Self::Item> { self.0.next_back() }
139                    }
140                    impl<'a, $GenericParam> ::std::iter::ExactSizeIterator for [<$Vec IterMut>]<'a, $GenericParam> {}
141                    impl<'a, $GenericParam> ::std::iter::FusedIterator     for [<$Vec IterMut>]<'a, $GenericParam> {}
142
143                    impl<'a, $GenericParam> ::std::iter::IntoIterator for &'a mut $Vec<$GenericParam> {
144                        type Item = ($Idx, &'a mut $Wrapper<$GenericParam>);
145                        type IntoIter = [<$Vec IterMut>]<'a, $GenericParam>;
146                        fn into_iter(self) -> Self::IntoIter {
147                            [<$Vec IterMut>](self.0.iter_mut().enumerate().map(|(u, t)| ($Idx(u), t)))
148                        }
149                    }
150
151
152
153                    $vec_vis struct [<$Vec IntoIter>]<$GenericParam>(::std::iter::Map<std::iter::Enumerate<std::vec::IntoIter<$Wrapper<$GenericParam>>>, fn((usize, $Wrapper<$GenericParam>)) -> ($Idx, $Wrapper<$GenericParam>)>);
154                    impl<$GenericParam> ::std::iter::Iterator for [<$Vec IntoIter>]<$GenericParam> {
155                        type Item = ($Idx,$Wrapper<$GenericParam>);
156                        #[inline] fn next(&mut self) -> Option<Self::Item> { self.0.next() }
157                        #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.0.size_hint() }
158                    }
159                    impl<$GenericParam> ::std::iter::DoubleEndedIterator for [<$Vec IntoIter>]<$GenericParam> {
160                        #[inline] fn next_back(&mut self) -> Option<Self::Item> { self.0.next_back() }
161                    }
162                    impl<$GenericParam> ::std::iter::ExactSizeIterator for [<$Vec IntoIter>]<$GenericParam> {}
163                    impl<$GenericParam> ::std::iter::FusedIterator     for [<$Vec IntoIter>]<$GenericParam> {}
164
165                    impl<$GenericParam> ::std::iter::IntoIterator for $Vec<$GenericParam> {
166                        type Item = ($Idx,$Wrapper<$GenericParam>);
167                        type IntoIter =  [<$Vec IntoIter>]<$GenericParam>;
168                        #[inline] fn into_iter(self) -> Self::IntoIter { [<$Vec IntoIter>](self.0.into_iter().enumerate().map(|(u, t)| ($Idx(u), t))) }
169                    }
170
171
172                    impl<$GenericParam> $Vec<$GenericParam>{
173                        #[inline] pub fn iter<'a>(&'a self) -> [<$Vec Iter>]<'a, $GenericParam> { self.into_iter() }
174                        #[inline] pub fn iter_mut<'a>(&'a mut self) -> [<$Vec IterMut>]<'a, $GenericParam> { self.into_iter() }
175
176                    }
177
178        }
179
180        impl<$GenericParam> $Vec<$GenericParam> {
181
182            pub fn write_display<W: ::std::fmt::Write>(&self, writer: &mut W,formater:impl Fn(&$Wrapper<$GenericParam>)->String) -> ::std::fmt::Result {
183                writer.write_str("[")?;
184
185                for (i,item) in self {
186                    if i.0 != 0{
187                        write!(writer, ", ")?;
188                    }
189                    write!(writer, "{}", formater(item))?;
190                }
191                writer.write_str("]")?;
192                Ok(())
193            }
194
195            pub fn display_string(&self,formatter:impl Fn(&$Wrapper<$GenericParam>)->String) -> String {
196                let mut result = String::new();
197                self.write_display(&mut result, formatter).unwrap();
198                result
199            }
200
201
202
203            /* construction */
204            #[inline] pub fn new() -> Self { Self(::std::vec::Vec::new()) }
205            #[inline] pub fn with_capacity(c: usize) -> Self { Self(::std::vec::Vec::with_capacity(c)) }
206
207            /* capacity */
208            #[inline] pub fn capacity(&self) -> usize { self.0.capacity() }
209            #[inline] pub fn reserve(&mut self, n: usize) { self.0.reserve(n) }
210            #[inline] pub fn reserve_exact(&mut self, n: usize) { self.0.reserve_exact(n) }
211            #[inline] pub fn shrink_to_fit(&mut self) { self.0.shrink_to_fit() }
212
213            /* push / pop */
214            #[inline] pub fn push(&mut self, value: $Wrapper<$GenericParam>) { self.0.push(value) }
215            #[inline] pub fn pop(&mut self) -> Option<$Wrapper<$GenericParam>> { self.0.pop() }
216
217
218            #[inline] pub fn swap(&mut self, a: $Idx, b: $Idx) {
219                self.0.swap(a.0, b.0);
220            }
221
222            #[inline] pub fn split_off(&mut self, at: $Idx) -> Self {
223                Self(self.0.split_off(at.0))
224            }
225
226            /* insertion / removal with the index new‑type */
227            #[inline] pub fn insert(&mut self, idx: $Idx, v: $Wrapper<$GenericParam>) { self.0.insert(idx.0, v) }
228            #[inline] pub fn remove(&mut self, idx: $Idx) -> $Wrapper<$GenericParam> { self.0.remove(idx.0) }
229            #[inline] pub fn swap_remove(&mut self, idx: $Idx) -> $Wrapper<$GenericParam> { self.0.swap_remove(idx.0) }
230
231            /* get APIs using the index new‑type */
232            #[inline] pub fn get(&self, idx: $Idx) -> Option<&$Wrapper<$GenericParam>> { self.0.get(idx.0) }
233            #[inline] pub fn get_mut(&mut self, idx: $Idx) -> Option<&mut $Wrapper<$GenericParam>> { self.0.get_mut(idx.0) }
234
235            /* iteration */
236
237            /* miscellaneous */
238            #[inline] pub fn clear(&mut self) { self.0.clear() }
239            #[inline] pub fn truncate(&mut self, len: usize) { self.0.truncate(len) }
240
241            /* fall‑back escape hatch – intentionally *not* public: */
242            #[inline] pub fn raw(&self) -> &::std::vec::Vec<$Wrapper<$GenericParam>> { &self.0 }
243        }
244
245        /* --- standard trait impls ------------------------------------------------- */
246
247        impl<$GenericParam> ::std::iter::FromIterator<($Idx,$Wrapper<$GenericParam>)> for $Vec<$GenericParam> {
248            #[inline] fn from_iter<I: ::std::iter::IntoIterator<Item = ($Idx,$Wrapper<$GenericParam>)>>(it: I) -> Self {
249                Self(::std::vec::Vec::from_iter(it.into_iter().map(|(_, val)|  val)))
250            }
251        }impl<$GenericParam> ::std::iter::FromIterator<$Wrapper<$GenericParam>> for $Vec<$GenericParam> {
252            #[inline] fn from_iter<I: ::std::iter::IntoIterator<Item = $Wrapper<$GenericParam>>>(it: I) -> Self {
253                Self(::std::vec::Vec::from_iter(it))
254            }
255        }
256
257        impl<$GenericParam> ::std::convert::AsRef<[$Wrapper<$GenericParam>]> for $Vec<$GenericParam> {
258            #[inline] fn as_ref(&self) -> &[$Wrapper<$GenericParam>] { &self.0 }
259        }
260
261
262
263        impl<$GenericParam> ::std::iter::Extend<($Idx,$Wrapper<$GenericParam>)> for $Vec<$GenericParam> {
264            #[inline] fn extend<I: ::std::iter::IntoIterator<Item = ($Idx,$Wrapper<$GenericParam>)>>(&mut self, it: I) {
265                self.0.extend(it.into_iter().map(|(_, val)|  val));
266            }
267        }
268
269
270        impl<$GenericParam> ::std::convert::From<::std::vec::Vec<$Wrapper<$GenericParam>>> for $Vec<$GenericParam> {
271            #[inline] fn from(v: ::std::vec::Vec<$Wrapper<$GenericParam>>) -> Self { Self(v) }
272        }
273
274
275
276
277        /// Permutation constructors
278
279        impl<$GenericParam> $Vec<Option<$Idx>> where $Wrapper<$GenericParam>: From<Option<$Idx>>{
280            pub fn fill_in(&mut self,contained:impl Fn(&$Idx)->bool){
281                let mut new_shifted = $Idx(0);
282
283                for (_, new_e) in self {
284                    if new_e.is_none() {
285                        while contained(&new_shifted) {
286                            new_shifted.0 += 1;
287                        }
288                        *new_e = Some(new_shifted);
289                        new_shifted.0 += 1;
290                    }
291                }
292            }
293        }
294
295        impl<$GenericParam> ::std::convert::TryFrom<$Vec<Option<$Idx>>> for $Vec<$Idx> where $Wrapper<$GenericParam>: From<Option<$Idx>> + TryInto<$Idx>, <$Wrapper<$GenericParam> as TryInto<$Idx>>::Error: std::fmt::Debug{
296            type Error = ();
297            fn try_from(vec: $Vec<Option<$Idx>>) -> Result<Self, Self::Error> {
298                vec.into_iter().map(|(i,e)| e.ok_or(()).map(|e|(i,e))).collect()
299            }
300        }
301
302        impl<$GenericParam> ::std::convert::TryFrom<$Vec<Option<$Idx>>> for linnet::permutation::Permutation where $Wrapper<$GenericParam>: From<Option<$Idx>> + TryInto<$Idx>, <$Wrapper<$GenericParam> as TryInto<$Idx>>::Error: std::fmt::Debug{
303            type Error = ();
304            fn try_from(vec: $Vec<Option<$Idx>>) -> Result<Self, Self::Error> {
305                let new_vec:Vec<usize> = vec.into_iter().map(|(i,e)| e.ok_or(()).map(|e|(i,e))).collect::<Result<$Vec<$Idx>, ()>>()?.into_iter().map(|(_,x)| usize::from(x)).collect();
306
307                Ok(linnet::permutation::Permutation::from_map(new_vec))
308            }
309        }
310    };
311    (
312        $Idx:ident ;
313
314        $(#[$vec_meta:meta])*
315        $vec_vis:vis struct $Vec:ident ;
316    ) => {
317
318
319
320
321
322
323        /* ——————————————————— vector new‑type ——————————————————— */
324
325        $(#[$vec_meta])*
326        #[derive(Clone, Debug, Default, Hash, PartialEq, Eq,PartialOrd,Ord,serde::Serialize, serde::Deserialize,bincode::Encode, bincode::Decode)]
327        $vec_vis struct $Vec<T>(::std::vec::Vec<T>);
328
329        /* --- Restricted indexing -------------------------------------------------- */
330
331        impl<T> ::std::ops::Index<$Idx> for $Vec<T> {
332            type Output = T;
333            #[inline] fn index(&self, i: $Idx) -> &Self::Output { &self.0[i.0] }
334        }
335        impl<T> ::std::ops::IndexMut<$Idx> for $Vec<T> {
336            #[inline] fn index_mut(&mut self, i: $Idx) -> &mut Self::Output { &mut self.0[i.0] }
337        }
338
339        impl<T>  ::std::convert::AsMut<[T]> for $Vec<T>{
340            fn as_mut(&mut self)->&mut [T]{
341                self.0.as_mut()
342            }
343        }
344
345        impl<T> linnet::half_edge::swap::Swap<$Idx> for $Vec<T>{
346            fn swap(&mut self, a: $Idx, b: $Idx) {
347                self.0.swap(a.0, b.0);
348            }
349
350            // type Item = T;
351
352            // fn filter(&self, id: &$Idx, filter: &impl Fn(&$Idx, &Self::Item) -> bool) -> bool {
353            //     filter(id,&self[*id])
354            // }
355
356            fn len(&self) -> $Idx {
357                $Idx(self.0.len())
358            }
359            #[inline] fn is_empty(&self) -> bool { self.0.is_empty() }
360
361        }
362
363        /* --- Delegated Vec<T> API ------------------------------------------------- */
364
365        ::paste::paste! {
366                    #[derive(Clone)]
367                    $vec_vis struct [<$Vec Iter>]<'a, T>(std::iter::Map<
368                        std::iter::Enumerate<std::slice::Iter<'a, T>>,
369                        fn((usize, &T)) -> ($Idx, &T),
370                    >);
371                    impl<'a, T> ::std::iter::Iterator for [<$Vec Iter>]<'a, T> {
372                        type Item = ($Idx, &'a T);
373                        #[inline] fn next(&mut self) -> Option<Self::Item> { self.0.next() }
374                        #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.0.size_hint() }
375                    }
376                    impl<'a, T> ::std::iter::DoubleEndedIterator for [<$Vec Iter>]<'a, T> {
377                        #[inline] fn next_back(&mut self) -> Option<Self::Item> { self.0.next_back() }
378                    }
379                    impl<'a, T> ::std::iter::ExactSizeIterator for [<$Vec Iter>]<'a, T> {}
380                    impl<'a, T> ::std::iter::FusedIterator     for [<$Vec Iter>]<'a, T> {}
381
382
383                    impl<'a, T> ::std::iter::IntoIterator for &'a $Vec<T> {
384                        type Item = ($Idx, &'a T);
385                        type IntoIter = [<$Vec Iter>]<'a, T>;
386                        fn into_iter(self) -> Self::IntoIter {
387                            [<$Vec Iter>](self.0.iter().enumerate().map(|(u, t)| ($Idx(u), t)))
388                        }
389                    }
390
391                    $vec_vis struct [<$Vec IterMut>]<'a, T>(::std::iter::Map<
392                        std::iter::Enumerate<std::slice::IterMut<'a, T>>,
393                        fn((usize, &mut T)) -> ($Idx, &mut T),
394                    >);
395                    impl<'a, T> ::std::iter::Iterator for [<$Vec IterMut>]<'a, T> {
396                        type Item = ($Idx, &'a mut T);
397                        #[inline] fn next(&mut self) -> Option<Self::Item> { self.0.next() }
398                        #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.0.size_hint() }
399                    }
400                    impl<'a, T> ::std::iter::DoubleEndedIterator for [<$Vec IterMut>]<'a, T> {
401                        #[inline] fn next_back(&mut self) -> Option<Self::Item> { self.0.next_back() }
402                    }
403                    impl<'a, T> ::std::iter::ExactSizeIterator for [<$Vec IterMut>]<'a, T> {}
404                    impl<'a, T> ::std::iter::FusedIterator     for [<$Vec IterMut>]<'a, T> {}
405
406                    impl<'a, T> ::std::iter::IntoIterator for &'a mut $Vec<T> {
407                        type Item = ($Idx, &'a mut T);
408                        type IntoIter = [<$Vec IterMut>]<'a, T>;
409                        fn into_iter(self) -> Self::IntoIter {
410                            [<$Vec IterMut>](self.0.iter_mut().enumerate().map(|(u, t)| ($Idx(u), t)))
411                        }
412                    }
413
414
415
416                    $vec_vis struct [<$Vec IntoIter>]<T>(::std::iter::Map<std::iter::Enumerate<std::vec::IntoIter<T>>, fn((usize, T)) -> ($Idx, T)>);
417                    impl<T> ::std::iter::Iterator for [<$Vec IntoIter>]<T> {
418                        type Item = ($Idx,T);
419                        #[inline] fn next(&mut self) -> Option<Self::Item> { self.0.next() }
420                        #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.0.size_hint() }
421                    }
422                    impl<T> ::std::iter::DoubleEndedIterator for [<$Vec IntoIter>]<T> {
423                        #[inline] fn next_back(&mut self) -> Option<Self::Item> { self.0.next_back() }
424                    }
425                    impl<T> ::std::iter::ExactSizeIterator for [<$Vec IntoIter>]<T> {}
426                    impl<T> ::std::iter::FusedIterator     for [<$Vec IntoIter>]<T> {}
427
428                    impl<T> ::std::iter::IntoIterator for $Vec<T> {
429                        type Item = ($Idx,T);
430                        type IntoIter =  [<$Vec IntoIter>]<T>;
431                        #[inline] fn into_iter(self) -> Self::IntoIter { [<$Vec IntoIter>](self.0.into_iter().enumerate().map(|(u, t)| ($Idx(u), t))) }
432                    }
433
434
435                    impl<T> $Vec<T>{
436                        #[inline] pub fn iter<'a>(&'a self) -> [<$Vec Iter>]<'a, T> { self.into_iter() }
437                        #[inline] pub fn iter_mut<'a>(&'a mut self) -> [<$Vec IterMut>]<'a, T> { self.into_iter() }
438
439                    }
440
441        }
442
443        impl<T> $Vec<T> {
444
445            pub fn write_display<W: ::std::fmt::Write>(&self, writer: &mut W,formater:impl Fn(&T)->String) -> ::std::fmt::Result {
446                writer.write_str("[")?;
447
448                for (i,item) in self {
449                    if i.0 != 0{
450                        write!(writer, ", ")?;
451                    }
452                    write!(writer, "{}", formater(item))?;
453                }
454                writer.write_str("]")?;
455                Ok(())
456            }
457
458            pub fn display_string(&self,formatter:impl Fn(&T)->String) -> String {
459                let mut result = String::new();
460                self.write_display(&mut result, formatter).unwrap();
461                result
462            }
463
464
465
466            /* construction */
467            #[inline] pub fn new() -> Self { Self(::std::vec::Vec::new()) }
468            #[inline] pub fn with_capacity(c: usize) -> Self { Self(::std::vec::Vec::with_capacity(c)) }
469
470            /* capacity */
471            // #[inline] pub fn len(&self) -> usize { self.0.len() }
472
473            #[inline] pub fn capacity(&self) -> usize { self.0.capacity() }
474            #[inline] pub fn reserve(&mut self, n: usize) { self.0.reserve(n) }
475            #[inline] pub fn reserve_exact(&mut self, n: usize) { self.0.reserve_exact(n) }
476            #[inline] pub fn shrink_to_fit(&mut self) { self.0.shrink_to_fit() }
477
478            /* push / pop */
479            #[inline] pub fn push(&mut self, value: T) { self.0.push(value) }
480            #[inline] pub fn pop(&mut self) -> Option<T> { self.0.pop() }
481
482
483            #[inline] pub fn swap(&mut self, a: $Idx, b: $Idx) {
484                self.0.swap(a.0, b.0);
485            }
486
487            #[inline] pub fn split_off(&mut self, at: $Idx) -> Self {
488                Self(self.0.split_off(at.0))
489            }
490
491            /* insertion / removal with the index new‑type */
492            #[inline] pub fn insert(&mut self, idx: $Idx, v: T) { self.0.insert(idx.0, v) }
493            #[inline] pub fn remove(&mut self, idx: $Idx) -> T { self.0.remove(idx.0) }
494            #[inline] pub fn swap_remove(&mut self, idx: $Idx) -> T { self.0.swap_remove(idx.0) }
495
496            /* get APIs using the index new‑type */
497            #[inline] pub fn get(&self, idx: $Idx) -> Option<&T> { self.0.get(idx.0) }
498            #[inline] pub fn get_mut(&mut self, idx: $Idx) -> Option<&mut T> { self.0.get_mut(idx.0) }
499
500            /* iteration */
501
502            /* miscellaneous */
503            #[inline] pub fn clear(&mut self) { self.0.clear() }
504            #[inline] pub fn truncate(&mut self, len: usize) { self.0.truncate(len) }
505
506            /* fall‑back escape hatch – intentionally *not* public: */
507            #[inline] pub fn raw(&self) -> &::std::vec::Vec<T> { &self.0 }
508        }
509
510        /* --- standard trait impls ------------------------------------------------- */
511
512        impl<T> ::std::iter::FromIterator<($Idx,T)> for $Vec<T> {
513            #[inline] fn from_iter<I: ::std::iter::IntoIterator<Item = ($Idx,T)>>(it: I) -> Self {
514                Self(::std::vec::Vec::from_iter(it.into_iter().map(|(_, val)|  val)))
515            }
516        }impl<T> ::std::iter::FromIterator<T> for $Vec<T> {
517            #[inline] fn from_iter<I: ::std::iter::IntoIterator<Item = T>>(it: I) -> Self {
518                Self(::std::vec::Vec::from_iter(it))
519            }
520        }
521
522        impl<T> ::std::convert::AsRef<[T]> for $Vec<T> {
523            #[inline] fn as_ref(&self) -> &[T] { &self.0 }
524        }
525
526
527
528        impl<T> ::std::iter::Extend<($Idx,T)> for $Vec<T> {
529            #[inline] fn extend<I: ::std::iter::IntoIterator<Item = ($Idx,T)>>(&mut self, it: I) {
530                self.0.extend(it.into_iter().map(|(_, val)|  val));
531            }
532        }
533
534
535        impl<T> ::std::convert::From<::std::vec::Vec<T>> for $Vec<T> {
536            #[inline] fn from(v: ::std::vec::Vec<T>) -> Self { Self(v) }
537        }
538
539
540
541
542        /// Permutation constructors
543
544        impl $Vec<Option<$Idx>>{
545            pub fn fill_in(&mut self,contained:impl Fn(&$Idx)->bool){
546                let mut new_shifted = $Idx(0);
547
548                for (_, new_e) in self {
549                    if new_e.is_none() {
550                        while contained(&new_shifted) {
551                            new_shifted.0 += 1;
552                        }
553                        *new_e = Some(new_shifted);
554                        new_shifted.0 += 1;
555                    }
556                }
557            }
558        }
559
560        impl ::std::convert::TryFrom<$Vec<Option<$Idx>>> for $Vec<$Idx>{
561            type Error = ();
562            fn try_from(vec: $Vec<Option<$Idx>>) -> Result<Self, Self::Error> {
563                vec.into_iter().map(|(i,e)| e.ok_or(()).map(|e|(i,e))).collect()
564            }
565        }
566
567        impl ::std::convert::TryFrom<$Vec<Option<$Idx>>> for linnet::permutation::Permutation{
568            type Error = ();
569            fn try_from(vec: $Vec<Option<$Idx>>) -> Result<Self, Self::Error> {
570                let new_vec:Vec<usize> = vec.into_iter().map(|(i,e)| e.ok_or(()).map(|e|(i,e))).collect::<Result<$Vec<$Idx>, ()>>()?.into_iter().map(|(_,x)| usize::from(x)).collect();
571
572                Ok(linnet::permutation::Permutation::from_map(new_vec))
573            }
574        }
575    };
576}