heapless_graphs 0.2.3

Implementation of composable graphs for no_alloc environments
Documentation
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
// SPDX-License-Identifier: Apache-2.0

//! Node structures
//!
//! This module implements various structures to use in both
//! edge list graphs with associated nodes and adjacency list graphs.

use core::marker::PhantomData;

/// Node index NI array of N elements, every item is a valid node
///
/// Alternatively, use an array `[NI;N]` or a slice: `&[NI]` directly
#[derive(Debug)]
pub struct NodeStruct<const N: usize, NI>(pub [NI; N]);
/// Node index NI array of N elements, nodes optionally populated
#[derive(Debug)]
pub struct NodeStructOption<const N: usize, NI>(pub [Option<NI>; N]);

/// Node index NI array of N elements, with node value V, every item is a valid node
#[derive(Debug)]
pub struct NodeValueStruct<const N: usize, NI, V>(pub [(NI, V); N]);

/// Node index NI array of N elements, with node value V, optionally populated
#[derive(Debug)]
pub struct NodeValueStructOption<const N: usize, NI, V>(pub [Option<(NI, V)>; N]);

/// Node index NI array of N elements, values in parallel array, every item is a valid node
#[derive(Debug)]
pub struct NodeValueTwoArray<const N: usize, NI, V>(pub [NI; N], pub [V; N]);

#[cfg(feature = "heapless")]
/// [Heapless vector](heapless::Vec) of node indexes
#[derive(Debug, Default)]
pub struct NodeStructVec<const N: usize, NI>(pub heapless::Vec<NI, N>);

#[cfg(feature = "heapless")]
/// [Heapless vector](heapless::Vec) of optionally populated node indexes
#[derive(Debug, Default)]
pub struct NodeStructOptionVec<const N: usize, NI>(pub heapless::Vec<Option<NI>, N>);

#[cfg(feature = "heapless")]
/// [Heapless vector](heapless::Vec) of node indexes with values
#[derive(Debug, Default)]
pub struct NodeStructVecValue<const N: usize, NI, V>(pub heapless::Vec<(NI, V), N>);

#[cfg(feature = "heapless")]
/// [Heapless vector](heapless::Vec) of optionally populated node indexes with values
#[derive(Debug, Default)]
pub struct NodeStructVecOptionValue<const N: usize, NI, V>(pub heapless::Vec<Option<(NI, V)>, N>);

/* Doesn't provide DoubleEndedIterator - not practical to use
#[cfg(feature = "heapless")]
pub struct NodeStructOptionSet<const N: usize, NI>(pub heapless::FnvIndexSet<NI, N>);
 */

/// Reference to a node, implemented for all node structures
///
/// It's used to implement iterators [`NodeRefIterator`] and
/// [`NodeOwningIterator`] through a common accessor interface
pub trait NodeRef {
    type NodeIndex;
    /// Reference to a node at given index
    fn get_node(&self, index: usize) -> Option<&Self::NodeIndex>;
    /// Check if node at index exists
    fn valid_node(&self, index: usize) -> bool {
        index < self.capacity()
    }
    /// Total capacity of the container
    fn capacity(&self) -> usize;
}

/// Extension of [`NodeRef`] that provides access to node values
///
/// This trait allows retrieving values associated with nodes in addition
/// to the basic node reference functionality.
pub trait NodeRefValue<V>: NodeRef {
    /// Returns a reference to a value of a node at index
    fn get_node_value(&self, index: usize) -> Option<&V>;
}

/// Implement NodeRef for slices directly, similar to [NodeStruct]
impl<T> NodeRef for &[T] {
    type NodeIndex = T;

    fn get_node(&self, index: usize) -> Option<&Self::NodeIndex> {
        self.get(index)
    }

    fn capacity(&self) -> usize {
        self.len()
    }
}

/// Implement NodeRef for fixed-size arrays, similar to [NodeStruct]
impl<T, const N: usize> NodeRef for [T; N] {
    type NodeIndex = T;

    fn get_node(&self, index: usize) -> Option<&Self::NodeIndex> {
        self.get(index)
    }

    fn capacity(&self) -> usize {
        self.len()
    }
}

impl<const N: usize, NI> NodeRef for NodeStruct<N, NI> {
    type NodeIndex = NI;
    fn get_node(&self, index: usize) -> Option<&Self::NodeIndex> {
        self.0.get(index)
    }
    fn capacity(&self) -> usize {
        self.0.len()
    }
}

impl<const N: usize, NI> Default for NodeStruct<N, NI>
where
    NI: Default,
{
    fn default() -> Self {
        Self(core::array::from_fn(|_| NI::default()))
    }
}

impl<const N: usize, NI> NodeRef for NodeStructOption<N, NI> {
    type NodeIndex = NI;
    fn valid_node(&self, index: usize) -> bool {
        index < self.capacity() && self.0[index].is_some()
    }
    fn get_node(&self, index: usize) -> Option<&Self::NodeIndex> {
        self.0.get(index).and_then(|ni| ni.as_ref())
    }
    fn capacity(&self) -> usize {
        self.0.len()
    }
}

impl<const N: usize, NI> Default for NodeStructOption<N, NI> {
    fn default() -> Self {
        Self(core::array::from_fn(|_| None))
    }
}

impl<const N: usize, NI, V> NodeRef for NodeValueStruct<N, NI, V> {
    type NodeIndex = NI;
    fn get_node(&self, index: usize) -> Option<&Self::NodeIndex> {
        self.0.get(index).map(|ni| &ni.0)
    }
    fn capacity(&self) -> usize {
        self.0.len()
    }
}

impl<const N: usize, NI, V> Default for NodeValueStruct<N, NI, V>
where
    NI: Default,
    V: Default,
{
    fn default() -> Self {
        Self(core::array::from_fn(|_| (NI::default(), V::default())))
    }
}

impl<const N: usize, NI, V> NodeRefValue<V> for NodeValueStruct<N, NI, V> {
    fn get_node_value(&self, index: usize) -> Option<&V> {
        self.0.get(index).map(|ni| &ni.1)
    }
}

impl<const N: usize, NI, V> NodeRef for NodeValueStructOption<N, NI, V> {
    type NodeIndex = NI;
    fn valid_node(&self, index: usize) -> bool {
        index < self.capacity() && self.0[index].is_some()
    }
    fn get_node(&self, index: usize) -> Option<&Self::NodeIndex> {
        if let Some(ni) = self.0.get(index) {
            ni.as_ref().map(|(ni, _)| ni)
        } else {
            None
        }
    }
    fn capacity(&self) -> usize {
        self.0.len()
    }
}
impl<const N: usize, NI, V> Default for NodeValueStructOption<N, NI, V> {
    fn default() -> Self {
        Self(core::array::from_fn(|_| None))
    }
}

impl<const N: usize, NI, V> NodeRefValue<V> for NodeValueStructOption<N, NI, V> {
    fn get_node_value(&self, index: usize) -> Option<&V> {
        if let Some(ni) = self.0.get(index) {
            ni.as_ref().map(|(_, v)| v)
        } else {
            None
        }
    }
}

impl<const N: usize, NI, V> NodeRef for NodeValueTwoArray<N, NI, V> {
    type NodeIndex = NI;
    fn get_node(&self, index: usize) -> Option<&Self::NodeIndex> {
        self.0.get(index)
    }
    fn capacity(&self) -> usize {
        self.0.len()
    }
}

impl<const N: usize, NI, V> Default for NodeValueTwoArray<N, NI, V>
where
    NI: Default,
    V: Default,
{
    fn default() -> Self {
        Self(
            core::array::from_fn(|_| NI::default()),
            core::array::from_fn(|_| V::default()),
        )
    }
}

impl<const N: usize, NI, V> NodeRefValue<V> for NodeValueTwoArray<N, NI, V> {
    fn get_node_value(&self, index: usize) -> Option<&V> {
        self.1.get(index)
    }
}

// #region NodeStructVec variants

#[cfg(feature = "heapless")]
impl<const N: usize, NI> NodeRef for NodeStructVec<N, NI> {
    type NodeIndex = NI;
    fn get_node(&self, index: usize) -> Option<&Self::NodeIndex> {
        self.0.get(index)
    }
    fn capacity(&self) -> usize {
        self.0.capacity()
    }
}

/// Implement NodeRef for heapless::Vec<NI,N>, similar to [NodeStructVec]
#[cfg(feature = "heapless")]
impl<const N: usize, NI> NodeRef for heapless::Vec<NI, N> {
    type NodeIndex = NI;
    fn get_node(&self, index: usize) -> Option<&Self::NodeIndex> {
        self.get(index)
    }
    fn capacity(&self) -> usize {
        self.capacity()
    }
}

// #endregion NodeStructVec variants

#[cfg(feature = "heapless")]
impl<const N: usize, NI> NodeRef for NodeStructOptionVec<N, NI> {
    type NodeIndex = NI;
    fn valid_node(&self, index: usize) -> bool {
        index < self.capacity() && self.0.get(index).is_some()
    }
    fn get_node(&self, index: usize) -> Option<&Self::NodeIndex> {
        self.0.get(index).and_then(|x| x.as_ref())
    }
    fn capacity(&self) -> usize {
        self.0.capacity()
    }
}

#[cfg(feature = "heapless")]
impl<const N: usize, NI, V> NodeRef for NodeStructVecValue<N, NI, V> {
    type NodeIndex = NI;
    fn valid_node(&self, index: usize) -> bool {
        index < self.capacity() && self.0.get(index).is_some()
    }
    fn get_node(&self, index: usize) -> Option<&Self::NodeIndex> {
        self.0.get(index).map(|(ni, _)| ni)
    }
    fn capacity(&self) -> usize {
        self.0.capacity()
    }
}

#[cfg(feature = "heapless")]
impl<const N: usize, NI, V> NodeRefValue<V> for NodeStructVecValue<N, NI, V> {
    fn get_node_value(&self, index: usize) -> Option<&V> {
        self.0.get(index).map(|(_, v)| v)
    }
}

#[cfg(feature = "heapless")]
impl<const N: usize, NI, V> NodeRef for NodeStructVecOptionValue<N, NI, V> {
    type NodeIndex = NI;
    fn valid_node(&self, index: usize) -> bool {
        index < self.capacity() && self.0.get(index).is_some()
    }
    fn get_node(&self, index: usize) -> Option<&Self::NodeIndex> {
        self.0.get(index).and_then(|x| x.as_ref().map(|(ni, _)| ni))
    }
    fn capacity(&self) -> usize {
        self.0.capacity()
    }
}

#[cfg(feature = "heapless")]
impl<const N: usize, NI, V> NodeRefValue<V> for NodeStructVecOptionValue<N, NI, V> {
    fn get_node_value(&self, index: usize) -> Option<&V> {
        self.0.get(index).and_then(|x| x.as_ref().map(|(_, v)| v))
    }
}

macro_rules! define_node_iterator {
    (
        $(#[$attr:meta])*
        $name:ident,
        $(lifetime: $lifetime:lifetime,)?
        struct_ref: $struct_ref:ty,
        item: $item:ty,
        $(where_clause: $where_clause:tt,)?
        get_node: $get_node:expr
    ) => {
        $(#[$attr])*
        #[derive(Clone)]
        pub struct $name<$($lifetime,)? T> {
            struct_ref: $struct_ref,
            index: usize,
            last_index: usize,
            back_index: usize,
            last_back_index: usize,
            overflow: bool,
        }
        impl<$($lifetime,)? T> $name<$($lifetime,)? T>
        where
            T: NodeRef,
            $( T::NodeIndex: $where_clause )?
        {
            pub fn new(struct_ref: $struct_ref) -> Self {
                let cap = struct_ref.capacity();
                Self {
                    struct_ref,
                    index: 0,
                    last_index: 0,
                    back_index: cap,
                    last_back_index: cap,
                    overflow: false,
                }
            }
        }

        impl<$($lifetime,)? T> Iterator for $name<$($lifetime,)? T>
        where
            T: NodeRef,
            $( T::NodeIndex: $where_clause )?
        {
            type Item = $item;
            fn next(&mut self) -> Option<Self::Item> {
                while !self.struct_ref.valid_node(self.index) {
                    self.index += 1;
                    if self.index >= self.struct_ref.capacity() {
                        return None;
                    }
                }
                if self.index < self.back_index {
                    self.last_index = self.index;
                    self.index += 1;
                    ($get_node)(self.struct_ref.get_node(self.last_index))
                } else {
                    None
                }
            }
        }

        impl<$($lifetime,)? T> DoubleEndedIterator for $name<$($lifetime,)? T>
        where
            T: NodeRef,
            $( T::NodeIndex: $where_clause )?
        {
            fn next_back(&mut self) -> Option<Self::Item> {
                if self.overflow {
                    return None;
                }
                while !self.struct_ref.valid_node(self.back_index) {
                    if let Some(val) = self.back_index.checked_sub(1) {
                        self.back_index = val;
                    } else {
                        return None;
                    }
                }
                if self.back_index >= self.index {
                    self.last_back_index = self.back_index;
                    (self.back_index, self.overflow) = self.back_index.overflowing_sub(1);
                    ($get_node)(self.struct_ref.get_node(self.last_back_index))
                } else {
                    None
                }
            }
        }
    }
}

define_node_iterator!(
    /// By-reference iterator over the nodes, for any struct that implements [`NodeRef`]
    NodeRefIterator,
    lifetime: 'a,
    struct_ref: &'a T,
    item: &'a T::NodeIndex,
    get_node: |node| node
);

define_node_iterator!(
    /// Owning iterator over the nodes, for any struct that implements [`NodeRef`]
    NodeOwningIterator,
    struct_ref: T,
    item: T::NodeIndex,
    where_clause: Copy,
    get_node: |node: Option<&T::NodeIndex> | node.copied()
);

/// Iterator that yields node value refs with indices
pub struct NodeStructValueIterator<'a, T, V> {
    inner: NodeRefIterator<'a, T>,
    _phantom: PhantomData<&'a V>,
}

impl<'a, T, V> Iterator for NodeStructValueIterator<'a, T, V>
where
    T: NodeRefValue<V>,
{
    type Item = (&'a T::NodeIndex, Option<&'a V>);
    fn next(&mut self) -> Option<Self::Item> {
        while self.inner.index < self.inner.struct_ref.capacity() {
            if let Some(node_index) = self.inner.next() {
                let val = self.inner.struct_ref.get_node_value(self.inner.last_index);
                return Some((node_index, val));
            }
        }
        None
    }
}

impl<'a, T, V> DoubleEndedIterator for NodeStructValueIterator<'a, T, V>
where
    T: NodeRefValue<V> + NodeRef,
    V: 'a,
    Self: 'a,
{
    fn next_back(&mut self) -> Option<Self::Item> {
        if let Some(node_index) = self.inner.next_back() {
            let val = self
                .inner
                .struct_ref
                .get_node_value(self.inner.last_back_index);
            Some((node_index, val))
        } else {
            None
        }
    }
}

macro_rules! node_struct_into_iter {
    ($struct_name:ident, $($gen:ident), *) => {
        impl<const N: usize, $($gen,)* > IntoIterator for $struct_name<N, $($gen,)*>
        where Self: NodeRef<NodeIndex = NI>, NI: Copy
        {
            type IntoIter = NodeOwningIterator<Self>;
            type Item = < NodeOwningIterator<Self > as Iterator>::Item;
            fn into_iter(self) -> Self::IntoIter {
                NodeOwningIterator::new(self)
            }
        }
    };
}

node_struct_into_iter!(NodeStruct, NI);
node_struct_into_iter!(NodeStructOption, NI);
node_struct_into_iter!(NodeValueStruct, NI, V);
node_struct_into_iter!(NodeValueStructOption, NI, V);
node_struct_into_iter!(NodeValueTwoArray, NI, V);

#[cfg(feature = "heapless")]
node_struct_into_iter!(NodeStructVec, NI);
#[cfg(feature = "heapless")]
node_struct_into_iter!(NodeStructOptionVec, NI);
#[cfg(feature = "heapless")]
node_struct_into_iter!(NodeStructVecValue, NI, V);
#[cfg(feature = "heapless")]
node_struct_into_iter!(NodeStructVecOptionValue, NI, V);

/// Provide a reference iterator over nodes
/// Trait for iterating over nodes in a node collection
///
/// Provides read-only iteration over node references. This trait is
/// automatically implemented for any type that implements [`NodeRef`].
pub trait NodesIterable {
    type Node;
    // todo: Maybe doesn't need to be DoubleEnded
    type Iter<'a>: DoubleEndedIterator<Item = &'a Self::Node>
    where
        Self: 'a;
    /// Return iterator that yields node references
    fn iter_nodes(&self) -> Self::Iter<'_>;
}
impl<T> NodesIterable for T
where
    T: NodeRef,
{
    type Node = T::NodeIndex;
    type Iter<'a>
        = NodeRefIterator<'a, T>
    where
        Self: 'a;

    fn iter_nodes(&self) -> Self::Iter<'_> {
        NodeRefIterator::new(self)
    }
}

/// Trait for iterating over nodes with their associated values
///
/// Extends [`NodesIterable`] to provide iteration over both nodes and their
/// values. This trait is automatically implemented for any type that implements
/// [`NodeRefValue`].
pub trait NodesValuesIterable<V>: NodesIterable {
    type IterValues<'a>: DoubleEndedIterator<Item = (&'a Self::Node, Option<&'a V>)>
    where
        <Self as NodesIterable>::Node: 'a,
        Self: 'a,
        V: 'a;
    fn iter_nodes_values(&self) -> Self::IterValues<'_>;
}

impl<T, V> NodesValuesIterable<V> for T
where
    T: NodeRefValue<V>,
{
    type IterValues<'a>
        = NodeStructValueIterator<'a, T, V>
    where
        <T as NodeRef>::NodeIndex: 'a,
        Self: 'a,
        V: 'a;
    fn iter_nodes_values(&self) -> Self::IterValues<'_> {
        Self::IterValues {
            inner: NodeRefIterator::new(self),
            _phantom: Default::default(),
        }
    }
}

/// Trait for converting node collections into owning iterators
///
/// This trait provides a way to consume a node collection and obtain an
/// iterator that owns the nodes. It's automatically implemented for any
/// type that implements [`IntoIterator`].
pub trait IntoNodesIterator {
    type Node;
    type NodeOwningIterator: Iterator<Item = Self::Node>;
    /// Returns an iterator over nodes
    fn into_nodes_iterator(self) -> Self::NodeOwningIterator;
}

impl<T> IntoNodesIterator for T
where
    T: IntoIterator,
    T::Item: PartialEq,
    <T as IntoIterator>::IntoIter: DoubleEndedIterator,
{
    type Node = T::Item;
    type NodeOwningIterator = T::IntoIter;
    fn into_nodes_iterator(self) -> Self::NodeOwningIterator {
        self.into_iter()
    }
}

/// Trait for node collections that support adding and removing nodes
///
/// This trait allows dynamic addition and removal of nodes to/from a node collection,
/// returning the index where the node was inserted/removed if successful.
pub trait MutableNodes<NI: PartialEq> {
    fn add(&mut self, node: NI) -> Option<usize>;
    fn remove(&mut self, node: NI) -> Option<usize>;
}

/// Trait for node collections that support adding and removing nodes with associated values
///
/// This trait allows dynamic addition and removal of nodes along with their values to/from a
/// node collection, returning the index where the node was inserted/removed if successful.
pub trait MutableNodeValue<NI: PartialEq, V> {
    fn add_value(&mut self, node: NI, value: V) -> Option<usize>;
    fn remove_value(&mut self, node: NI) -> Option<usize>;
}

/// # Performance Note
///
/// Both `add()` and `remove()` operations use linear search (O(n)) to find empty slots
/// or matching nodes in the array. For better performance with larger datasets, consider
/// using map-based containers instead.
impl<const N: usize, NI: PartialEq> MutableNodes<NI> for NodeStructOption<N, NI> {
    fn add(&mut self, node: NI) -> Option<usize> {
        if let Some(index) = self.0.iter().position(|opt| opt.is_none()) {
            self.0[index] = Some(node);
            return Some(index);
        }
        None
    }

    fn remove(&mut self, node: NI) -> Option<usize> {
        if let Some(index) = self.0.iter().position(|opt| opt.as_ref() == Some(&node)) {
            self.0[index] = None;
            return Some(index);
        }
        None
    }
}

/// # Performance Note
///
/// Both `add_value()` and `remove_value()` operations use linear search (O(n)) to find empty slots
/// or matching nodes in the array. For better performance with larger datasets, consider
/// using map-based containers instead.
impl<const N: usize, NI: PartialEq, V> MutableNodeValue<NI, V> for NodeValueStructOption<N, NI, V> {
    fn add_value(&mut self, node: NI, value: V) -> Option<usize> {
        if let Some(index) = self.0.iter().position(|opt| opt.is_none()) {
            self.0[index] = Some((node, value));
            return Some(index);
        }
        None
    }

    fn remove_value(&mut self, node: NI) -> Option<usize> {
        if let Some(index) = self
            .0
            .iter()
            .position(|opt| opt.as_ref().map(|(ni, _)| ni) == Some(&node))
        {
            self.0[index] = None;
            return Some(index);
        }
        None
    }
}

// Dual implementation: NodeValueStructOption can also implement MutableNodes
// by using Default values when no explicit value is provided
impl<const N: usize, NI: PartialEq, V: Default> MutableNodes<NI>
    for NodeValueStructOption<N, NI, V>
{
    fn add(&mut self, node: NI) -> Option<usize> {
        // Add with default value
        self.add_value(node, V::default())
    }

    fn remove(&mut self, node: NI) -> Option<usize> {
        // Reuse existing remove logic from MutableNodeValue
        <Self as MutableNodeValue<NI, V>>::remove_value(self, node)
    }
}

#[cfg(feature = "heapless")]
/// # Performance Note
///
/// The `remove()` operation uses linear search (O(n)) to find matching nodes in the vector,
/// followed by shifting all subsequent elements. The `add()` operation is O(1) when capacity
/// is available. For better removal performance, consider using option-based containers.
impl<const N: usize, NI: PartialEq> MutableNodes<NI> for NodeStructVec<N, NI> {
    fn add(&mut self, node: NI) -> Option<usize> {
        self.0.push(node).ok().map(|_| self.0.len() - 1)
    }

    fn remove(&mut self, node: NI) -> Option<usize> {
        if let Some(index) = self.0.iter().position(|ni| *ni == node) {
            self.0.remove(index);
            return Some(index);
        }
        None
    }
}

#[cfg(feature = "heapless")]
/// # Performance Note
///
/// Both `add()` and `remove()` operations may use linear search (O(n)). The `add()` operation
/// first searches for an empty slot, then appends if none found. The `remove()` operation
/// searches for the matching node and marks it as None, preserving indices.
impl<const N: usize, NI: PartialEq> MutableNodes<NI> for NodeStructOptionVec<N, NI> {
    fn add(&mut self, node: NI) -> Option<usize> {
        if let Some(index) = self.0.iter().position(|opt| opt.is_none()) {
            self.0[index] = Some(node);
            return Some(index);
        }
        self.0.push(Some(node)).ok().map(|_| self.0.len() - 1)
    }

    fn remove(&mut self, node: NI) -> Option<usize> {
        if let Some(index) = self.0.iter().position(|opt| opt.as_ref() == Some(&node)) {
            self.0[index] = None;
            return Some(index);
        }
        None
    }
}

#[cfg(feature = "heapless")]
/// # Performance Note
///
/// The `remove_value()` operation uses linear search (O(n)) to find matching nodes in the vector,
/// followed by shifting all subsequent elements. The `add_value()` operation is O(1) when capacity
/// is available. For better removal performance, consider using option-based containers.
impl<const N: usize, NI: PartialEq, V> MutableNodeValue<NI, V> for NodeStructVecValue<N, NI, V> {
    fn add_value(&mut self, node: NI, value: V) -> Option<usize> {
        self.0.push((node, value)).ok().map(|_| self.0.len() - 1)
    }

    fn remove_value(&mut self, node: NI) -> Option<usize> {
        if let Some(index) = self.0.iter().position(|(ni, _)| *ni == node) {
            self.0.remove(index);
            return Some(index);
        }
        None
    }
}

#[cfg(feature = "heapless")]
/// # Performance Note
///
/// Both `add_value()` and `remove_value()` operations may use linear search (O(n)). The `add_value()` operation
/// first searches for an empty slot, then appends if none found. The `remove_value()` operation
/// searches for the matching node and marks it as None, preserving indices.
impl<const N: usize, NI: PartialEq, V> MutableNodeValue<NI, V>
    for NodeStructVecOptionValue<N, NI, V>
{
    fn add_value(&mut self, node: NI, value: V) -> Option<usize> {
        if let Some(index) = self.0.iter().position(|opt| opt.is_none()) {
            self.0[index] = Some((node, value));
            return Some(index);
        }
        self.0
            .push(Some((node, value)))
            .ok()
            .map(|_| self.0.len() - 1)
    }

    fn remove_value(&mut self, node: NI) -> Option<usize> {
        if let Some(index) = self
            .0
            .iter()
            .position(|opt| opt.as_ref().map(|(ni, _)| ni) == Some(&node))
        {
            self.0[index] = None;
            return Some(index);
        }
        None
    }
}

// Dual implementation: NodeStructVecOptionValue can also implement MutableNodes
// by using Default values when no explicit value is provided
#[cfg(feature = "heapless")]
impl<const N: usize, NI: PartialEq, V: Default> MutableNodes<NI>
    for NodeStructVecOptionValue<N, NI, V>
{
    fn add(&mut self, node: NI) -> Option<usize> {
        // Add with default value
        self.add_value(node, V::default())
    }

    fn remove(&mut self, node: NI) -> Option<usize> {
        // Reuse existing remove logic from MutableNodeValue
        <Self as MutableNodeValue<NI, V>>::remove_value(self, node)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tests::collect;
    use core::fmt::Debug;

    fn iterate_over<N, NI>(nodes: &N, cmp: &[NI])
    where
        N: NodesIterable<Node = NI> + ?Sized,
        NI: core::fmt::Debug + Default + PartialEq + Copy,
    {
        let mut collect_array: [NI; 8] = core::array::from_fn(|_| NI::default());
        for node in nodes.iter_nodes().zip(collect_array.iter_mut()) {
            *node.1 = *node.0;
        }

        let final_slice = &collect_array[..cmp.len()];
        assert_eq!(final_slice, cmp);
        // Ensure we can loop twice and didn't consume anything
        for _node in nodes.iter_nodes().zip(collect_array.iter_mut()) {}
    }

    static EXPECTED: [usize; 3] = [1, 3, 2];

    #[test]
    fn test_node_array_iter() {
        let nodes = [1, 3, 2];
        iterate_over(&nodes, &EXPECTED);
        assert_eq!(3 * 8, core::mem::size_of_val(&nodes));
    }

    #[test]
    fn test_node_slice_iter() {
        let nodes = [1, 3, 2].as_slice();
        iterate_over(&nodes, &EXPECTED);
        assert_eq!(2 * 8, core::mem::size_of_val(&nodes));
    }

    #[test]
    fn test_nodestruct_iter() {
        let nodes = NodeStruct([1, 3, 2]);
        iterate_over(&nodes, &EXPECTED);
        assert_eq!(3 * 8, core::mem::size_of_val(&nodes));
    }

    #[test]
    fn test_node_iter_option_all() {
        let nodes = NodeStructOption([Some(1), Some(3), Some(2)]);
        iterate_over(&nodes, &EXPECTED);
        assert_eq!(3 * 8 * 2, core::mem::size_of_val(&nodes));
    }

    #[test]
    fn test_node_iter_option_some() {
        let nodes = NodeStructOption([None, Some(1), Some(3), None, Some(2)]);
        iterate_over(&nodes, &EXPECTED);
        assert_eq!(5 * 8 * 2, core::mem::size_of_val(&nodes));
    }

    #[test]
    fn test_node_iter_value() {
        let nodes = NodeValueStruct([(1, "a"), (3, "b"), (2, "c")]);
        iterate_over(&nodes, &EXPECTED);
        assert_eq!(3 * 8 * 3, core::mem::size_of_val(&nodes));
    }

    #[test]
    fn test_node_iter_value_option_all() {
        let nodes = NodeValueStructOption([Some((1, "a")), Some((3, "b")), Some((2, "c"))]);
        iterate_over(&nodes, &EXPECTED);
        assert_eq!(3 * 8 * 3, core::mem::size_of_val(&nodes));
    }

    #[test]
    fn test_node_iter_value_option_some() {
        let nodes =
            NodeValueStructOption([None, Some((1, "a")), Some((3, "b")), None, Some((2, "c"))]);
        iterate_over(&nodes, &EXPECTED);
        assert_eq!(5 * 8 * 3, core::mem::size_of_val(&nodes));
    }

    #[test]
    fn test_node_iter_value_two_array() {
        let nodes = NodeValueTwoArray([1, 3, 2], ["a", "b", "c"]);
        iterate_over(&nodes, &EXPECTED);
        assert_eq!(3 * 8 * 3, core::mem::size_of_val(&nodes));
    }

    #[cfg(feature = "heapless")]
    #[test]
    fn test_node_iter_vec() {
        let nodes = NodeStructVec::<3, _>(heapless::Vec::from_slice(&[1, 3, 2]).unwrap());
        iterate_over(&nodes, &EXPECTED);
    }

    #[cfg(feature = "heapless")]
    #[test]
    fn test_node_iter_vec_heapless() {
        let nodes = heapless::Vec::<_, 3>::from_slice(&[1, 3, 2]).unwrap();
        iterate_over(&nodes, &EXPECTED);
    }

    #[cfg(feature = "heapless")]
    #[test]
    fn test_node_iter_vec_option() {
        let nodes = NodeStructOptionVec::<3, _>(
            heapless::Vec::from_slice(&[Some(1), Some(3), Some(2)]).unwrap(),
        );
        iterate_over(&nodes, &EXPECTED);
    }

    #[cfg(feature = "heapless")]
    #[test]
    fn test_node_iter_vec_option_value() {
        let nodes = NodeStructVecOptionValue::<3, _, _>(
            heapless::Vec::from_slice(&[Some((1, "a")), Some((3, "b")), Some((2, "c"))]).unwrap(),
        );
        iterate_over(&nodes, &EXPECTED);
    }

    #[cfg(feature = "heapless")]
    #[test]
    fn test_node_iter_vec_value() {
        let nodes = NodeStructVecValue::<3, _, _>(
            heapless::Vec::from_slice(&[(1, "a"), (3, "b"), (2, "c")]).unwrap(),
        );
        iterate_over(&nodes, &EXPECTED);
    }

    #[test]
    fn test_node_get_values() {
        fn test_value<V, T: NodeRefValue<V>>(nodes: &T, idx: usize, val: Option<&V>)
        where
            V: PartialEq + core::fmt::Debug,
        {
            assert_eq!(nodes.get_node_value(idx), val);
        }
        fn test_123<T: NodeRefValue<&'static str>>(nodes: &T) {
            test_value(nodes, 1, Some(&"b"));
            test_value(nodes, 2, Some(&"c"));
            test_value(nodes, 3, None);
        }
        test_123(&NodeValueStruct([(1, "a"), (3, "b"), (2, "c")]));
        test_123(&NodeValueStructOption([
            Some((1, "a")),
            Some((3, "b")),
            Some((2, "c")),
        ]));
        test_123(&NodeValueTwoArray([1, 3, 2], ["a", "b", "c"]));
        #[cfg(feature = "heapless")]
        {
            test_123(&NodeStructVecValue::<3, _, _>(
                heapless::Vec::from_slice(&[(1, "a"), (3, "b"), (2, "c")]).unwrap(),
            ));
            test_123(&NodeStructVecOptionValue::<3, _, _>(
                heapless::Vec::from_slice(&[Some((1, "a")), Some((3, "b")), Some((2, "c"))])
                    .unwrap(),
            ));
        }
    }

    fn add_node<NI: PartialEq, T: MutableNodes<NI>>(f: &mut T, to_add: NI) -> Option<usize> {
        f.add(to_add)
    }
    fn add_node_value<NI: PartialEq, V, T: MutableNodeValue<NI, V>>(
        f: &mut T,
        to_add: NI,
        value: V,
    ) -> Option<usize> {
        f.add_value(to_add, value)
    }
    fn remove_node_value<NI: PartialEq, V, T: MutableNodeValue<NI, V>>(
        f: &mut T,
        to_remove: NI,
    ) -> Option<usize> {
        f.remove_value(to_remove)
    }
    fn remove_node<NI: PartialEq, T: MutableNodes<NI>>(f: &mut T, to_remove: NI) -> Option<usize> {
        f.remove(to_remove)
    }

    #[test]
    fn test_mutable_nodes() {
        let mut nodes = NodeStructOption([None, Some(1), Some(3), None, Some(2)]);
        assert_eq!(add_node(&mut nodes, 4), Some(0));
        assert_eq!(remove_node(&mut nodes, 3), Some(2));
        iterate_over(&nodes, &[4, 1, 2]);
        assert_eq!(add_node(&mut nodes, 5), Some(2));
        assert_eq!(add_node(&mut nodes, 8), Some(3));
        assert_eq!(add_node(&mut nodes, 100), None);
    }
    #[test]
    fn test_mutable_value_nodes() {
        let mut nodes =
            NodeValueStructOption([Some((5_u8, Some('b'))), None, Some((2, Some('x')))]);
        assert_eq!(add_node_value(&mut nodes, 4, Some('c')), Some(1));
        assert_eq!(remove_node_value(&mut nodes, 2), Some(2));
        iterate_over(&nodes, &[5, 4]);
    }
    #[test]
    fn node_values_iterable() {
        fn test<NI, V, T>(t: &T, cmp: &[V])
        where
            NI: PartialEq + Debug,
            V: Default + Debug + Copy + PartialEq,
            T: NodesValuesIterable<V, Node = NI>,
        {
            let mut collect_array = [V::default(); 16];
            let mut len = 0;
            for (src, dst) in t.iter_nodes_values().zip(collect_array.iter_mut()) {
                if let Some(v) = src.1 {
                    *dst = *v;
                    len += 1;
                }
            }
            assert_eq!(&collect_array[..len], cmp);
        }
        fn test_from_front_back<NI, V, T>(
            t: &T,
            from_front: isize,
            vfront: Option<&V>,
            from_back: isize,
            vback: Option<&V>,
        ) where
            NI: PartialEq + Debug,
            V: Default + Debug + Copy + PartialEq,
            T: NodesValuesIterable<V, Node = NI>,
        {
            let mut iterator = t.iter_nodes_values();
            if from_front >= 0 {
                assert_eq!(
                    iterator.nth(from_front as usize).map(|v| v.1.unwrap()),
                    vfront
                );
            }
            assert_eq!(
                iterator.rev().nth(from_back as usize).map(|v| v.1.unwrap()),
                vback
            )
        }
        let values = NodeValueStruct([(1_usize, "a"), (3, "b"), (2, "c")]);
        test(&values, &["a", "b", "c"]);
        test_from_front_back(&values, 0, Some(&"a"), 0, Some(&"c"));
        test_from_front_back(&values, 1, Some(&"b"), 0, Some(&"c"));
        test_from_front_back(&values, 2, Some(&"c"), 0, None);
        test_from_front_back(&values, 3, None, 3, None);
        test_from_front_back(&values, -1, None, 1, Some(&"b"));
        test_from_front_back(&values, -1, None, 2, Some(&"a"));
        test_from_front_back(&values, -1, None, 3, None);
        let values = NodeValueTwoArray([1_usize, 2], ["xf", "bz"]);
        test(&values, &["xf", "bz"]);
        let values = NodeValueStructOption([
            Some((1, 'b')),
            None,
            None,
            Some((2, 'f')),
            None,
            Some((3, 'x')),
            None,
            None,
            Some((4, 'y')),
            Some((100, 'z')),
        ]);
        test(&values, &['b', 'f', 'x', 'y', 'z']);
        let values = NodeValueStructOption([
            Some((1, Some('b'))),
            None,
            Some((2, None)),
            Some((100, Some('z'))),
        ]);
        test(&values, &[Some('b'), None, Some('z')]);
    }

    #[test]
    fn test_mutable_node_value_comprehensive() {
        // Test NodeValueStructOption
        let mut nodes = NodeValueStructOption::<5, u32, &str>([None; 5]);

        // Test add_value
        assert_eq!(nodes.add_value(1, "hello"), Some(0));
        assert_eq!(nodes.add_value(2, "world"), Some(1));
        assert_eq!(nodes.add_value(3, "test"), Some(2));

        // Verify current state
        let mut collected = [0u32; 8];
        let slice = collect(nodes.iter_nodes().copied(), &mut collected);
        assert_eq!(slice, &[1, 2, 3]);

        // Test remove (disambiguate since this type implements both traits)
        assert_eq!(
            <NodeValueStructOption<5, u32, &str> as MutableNodeValue<u32, &str>>::remove_value(
                &mut nodes, 2
            ),
            Some(1)
        ); // Remove middle element
        assert_eq!(
            <NodeValueStructOption<5, u32, &str> as MutableNodeValue<u32, &str>>::remove_value(
                &mut nodes, 999
            ),
            None
        ); // Remove non-existent

        // Verify after removal
        let mut collected = [0u32; 8];
        let slice = collect(nodes.iter_nodes().copied(), &mut collected);
        assert_eq!(slice, &[1, 3]);

        // Test that removed slot can be reused
        assert_eq!(nodes.add_value(4, "reuse"), Some(1));

        // Fill remaining capacity
        assert_eq!(nodes.add_value(5, "five"), Some(3));
        assert_eq!(nodes.add_value(6, "six"), Some(4));

        // Verify full state
        let mut collected = [0u32; 8];
        let slice = collect(nodes.iter_nodes().copied(), &mut collected);
        assert_eq!(slice, &[1, 4, 3, 5, 6]);

        // Test capacity limits
        assert_eq!(nodes.add_value(7, "overflow"), None);
    }

    #[cfg(feature = "heapless")]
    #[test]
    fn test_mutable_node_value_vec_types() {
        // Test NodeStructVecValue
        let mut vec_nodes = NodeStructVecValue::<5, u32, i32>(heapless::Vec::new());

        // Test add_value - should append to end
        assert_eq!(vec_nodes.add_value(10, 100), Some(0));
        assert_eq!(vec_nodes.add_value(20, 200), Some(1));
        assert_eq!(vec_nodes.add_value(30, 300), Some(2));

        // Verify state
        let mut collected = [0u32; 8];
        let slice = collect(vec_nodes.iter_nodes().copied(), &mut collected);
        assert_eq!(slice, &[10, 20, 30]);

        // Test remove from middle - should shift elements
        assert_eq!(vec_nodes.remove_value(20), Some(1));

        // Verify remaining elements
        let mut collected = [0u32; 8];
        let slice = collect(vec_nodes.iter_nodes().copied(), &mut collected);
        assert_eq!(slice, &[10, 30]);
    }

    #[test]
    fn test_mutable_nodes_edge_cases() {
        let mut nodes = NodeStructOption::<3, u32>([Some(1), None, Some(3)]);

        // Verify initial state
        let mut collected = [0u32; 8];
        let slice = collect(nodes.iter_nodes().copied(), &mut collected);
        assert_eq!(slice, &[1, 3]);

        // Test adding to middle slot
        assert_eq!(nodes.add(2), Some(1));

        let mut collected = [0u32; 8];
        let slice = collect(nodes.iter_nodes().copied(), &mut collected);
        assert_eq!(slice, &[1, 2, 3]);

        // Test removing and re-adding to same slot
        assert_eq!(nodes.remove(2), Some(1));
        assert_eq!(nodes.add(4), Some(1)); // Should reuse slot 1

        let mut collected = [0u32; 8];
        let slice = collect(nodes.iter_nodes().copied(), &mut collected);
        assert_eq!(slice, &[1, 4, 3]);

        // Test full capacity
        assert_eq!(nodes.add(5), None);

        // Test removing non-existent
        assert_eq!(nodes.remove(999), None);
    }

    #[cfg(feature = "heapless")]
    #[test]
    fn test_option_value_container_gaps() {
        // Start with a simple test - just verify basic functionality
        let mut nodes = NodeStructVecOptionValue::<8, u32, i32>(heapless::Vec::new());

        // Add one element
        assert_eq!(nodes.add_value(1, 10), Some(0));

        // Verify state
        let mut collected = [0u32; 8];
        let slice = collect(nodes.iter_nodes().copied(), &mut collected);
        assert_eq!(slice, &[1]);

        // Add second element
        assert_eq!(nodes.add_value(2, 20), Some(1));

        // Verify state
        let mut collected = [0u32; 8];
        let slice = collect(nodes.iter_nodes().copied(), &mut collected);
        assert_eq!(slice, &[1, 2]);
    }

    #[cfg(feature = "heapless")]
    #[test]
    fn test_vec_behavior_basic() {
        // Test basic Vec behavior for MutableNodes
        let mut vec_nodes = NodeStructVec::<5, u32>(heapless::Vec::new());

        // Add elements
        assert_eq!(vec_nodes.add(1), Some(0));
        assert_eq!(vec_nodes.add(2), Some(1));
        assert_eq!(vec_nodes.add(3), Some(2));

        // Verify state
        let mut collected = [0u32; 8];
        let slice = collect(vec_nodes.iter_nodes().copied(), &mut collected);
        assert_eq!(slice, &[1, 2, 3]);

        // Remove middle element (should shift)
        assert_eq!(vec_nodes.remove(2), Some(1));

        let mut collected = [0u32; 8];
        let slice = collect(vec_nodes.iter_nodes().copied(), &mut collected);
        assert_eq!(slice, &[1, 3]); // 3 shifted down
    }

    #[test]
    fn test_dual_trait_implementation() {
        // Test that NodeValueStructOption implements both traits when V: Default
        let mut nodes = NodeValueStructOption::<5, u32, i32>([None; 5]);

        // Use as MutableNodes (adds with default values)
        assert_eq!(nodes.add(1), Some(0)); // Adds (1, 0) since i32::default() = 0
        assert_eq!(nodes.add(2), Some(1)); // Adds (2, 0)

        // Use as MutableNodeValue (adds with explicit values)
        assert_eq!(nodes.add_value(3, 100), Some(2)); // Adds (3, 100)

        // Verify all nodes exist
        let mut collected = [0u32; 8];
        let slice = collect(nodes.iter_nodes().copied(), &mut collected);
        assert_eq!(slice, &[1, 2, 3]);

        // Verify values - nodes added via MutableNodes have default values
        assert_eq!(nodes.0[0], Some((1, 0))); // Default value
        assert_eq!(nodes.0[1], Some((2, 0))); // Default value
        assert_eq!(nodes.0[2], Some((3, 100))); // Explicit value

        // Remove works from either trait (need to disambiguate since both traits have remove)
        assert_eq!(
            <NodeValueStructOption<5, u32, i32> as MutableNodes<u32>>::remove(&mut nodes, 2),
            Some(1)
        );

        let mut collected = [0u32; 8];
        let slice = collect(nodes.iter_nodes().copied(), &mut collected);
        assert_eq!(slice, &[1, 3]);
    }

    #[cfg(feature = "heapless")]
    #[test]
    fn test_dual_trait_heapless_vec() {
        // Test that NodeStructVecOptionValue implements both traits when V: Default
        let mut nodes = NodeStructVecOptionValue::<8, u32, &str>(heapless::Vec::new());

        // Use as MutableNodes (adds with default values)
        assert_eq!(nodes.add(1), Some(0)); // Adds (1, "") since &str::default() = ""

        // Use as MutableNodeValue (adds with explicit values)
        assert_eq!(nodes.add_value(2, "hello"), Some(1));

        // Verify both approaches work
        let mut collected = [0u32; 8];
        let slice = collect(nodes.iter_nodes().copied(), &mut collected);
        assert_eq!(slice, &[1, 2]);

        // Verify values
        assert_eq!(nodes.0[0], Some((1, ""))); // Default value
        assert_eq!(nodes.0[1], Some((2, "hello"))); // Explicit value

        // Remove middle to create gap, then test gap filling (disambiguate remove call)
        assert_eq!(
            <NodeStructVecOptionValue<8, u32, &str> as MutableNodes<u32>>::remove(&mut nodes, 1),
            Some(0)
        );

        // Add new element - should fill the gap
        assert_eq!(nodes.add(3), Some(0));

        let mut collected = [0u32; 8];
        let slice = collect(nodes.iter_nodes().copied(), &mut collected);
        assert_eq!(slice, &[3, 2]);
    }
}