@@ -63,29 +63,13 @@ pub struct ChildBuilder<'a> {
63
63
64
64
impl Command for PushChildren {
65
65
fn write ( self : Box < Self > , world : & mut World , _resources : & mut Resources ) {
66
- let mut childset = HashSet :: default ( ) ;
67
-
68
- let mut new_children = if let Ok ( children) = world. get :: < Children > ( self . parent ) {
69
- childset. extend ( children. iter ( ) . copied ( ) ) ;
70
- children. 0 . clone ( )
71
- } else {
72
- Default :: default ( )
73
- } ;
74
-
75
66
for child in self . children . iter ( ) {
76
- if !childset. contains ( child) {
77
- new_children. push ( * child) ;
78
- }
79
-
80
67
if let Ok ( Parent ( old_parent) ) = world. get :: < Parent > ( * child) {
81
68
let old_parent = * old_parent;
82
69
83
70
// clean old parent of children references
84
71
if let Ok ( mut children) = world. get_mut :: < Children > ( old_parent) {
85
- let vec = children
86
- . iter ( )
87
- . filter_map ( |c| if c != child { Some ( * c) } else { None } )
88
- . collect ( ) ;
72
+ let vec = children. iter ( ) . copied ( ) . filter ( |c| c != child) . collect ( ) ;
89
73
children. 0 = vec;
90
74
}
91
75
@@ -99,9 +83,21 @@ impl Command for PushChildren {
99
83
}
100
84
}
101
85
102
- world
103
- . insert_one ( self . parent , Children ( self . children ) )
104
- . unwrap ( ) ;
86
+ if let Ok ( mut children) = world. get_mut :: < Children > ( self . parent ) {
87
+ let vec = & mut children. 0 ;
88
+ let index = vec. len ( ) ;
89
+
90
+ // note that for cases with many children a HashSet might be better for contains() check
91
+ for child in self . children . iter ( ) . rev ( ) {
92
+ if !vec. contains ( child) {
93
+ vec. insert ( index, * child) ;
94
+ }
95
+ }
96
+ } else {
97
+ world
98
+ . insert_one ( self . parent , Children ( self . children ) )
99
+ . unwrap ( ) ;
100
+ } ;
105
101
}
106
102
}
107
103
0 commit comments