@@ -74,6 +74,58 @@ pub trait RelationshipSourceCollection {
74
74
}
75
75
}
76
76
77
+ /// This trait signals that a [`RelationshipSourceCollection`] is ordered.
78
+ pub trait OrderedRelationshipSourceCollection : RelationshipSourceCollection {
79
+ /// Inserts the entity at a specific index.
80
+ /// If the index is too large, the entity will be added to the end of the collection.
81
+ fn insert ( & mut self , index : usize , entity : Entity ) ;
82
+ /// Removes the entity at the specified idnex if it exists.
83
+ fn remove_at ( & mut self , index : usize ) -> Option < Entity > ;
84
+ /// Inserts the entity at a specific index.
85
+ /// This will never reorder other entities.
86
+ /// If the index is too large, the entity will be added to the end of the collection.
87
+ fn insert_stable ( & mut self , index : usize , entity : Entity ) ;
88
+ /// Removes the entity at the specified idnex if it exists.
89
+ /// This will never reorder other entities.
90
+ fn remove_at_stable ( & mut self , index : usize ) -> Option < Entity > ;
91
+ /// Sorts the source collection.
92
+ fn sort ( & mut self ) ;
93
+ /// Inserts the entity at the proper place to maintain sorting.
94
+ fn insert_sorted ( & mut self , entity : Entity ) ;
95
+
96
+ /// This places the most recently added entity at the particular index.
97
+ fn place_most_recent ( & mut self , index : usize ) ;
98
+
99
+ /// This places the given entity at the particular index.
100
+ /// This will do nothing if the entity is not in the collection.
101
+ /// If the index is out of bounds, this will put the entity at the end.
102
+ fn place ( & mut self , entity : Entity , index : usize ) ;
103
+
104
+ /// Adds the entity at index 0.
105
+ fn push_front ( & mut self , entity : Entity ) {
106
+ self . insert ( 0 , entity) ;
107
+ }
108
+
109
+ /// Adds the entity to the back of the collection.
110
+ fn push_back ( & mut self , entity : Entity ) {
111
+ self . insert ( usize:: MAX , entity) ;
112
+ }
113
+
114
+ /// Removes the first entity.
115
+ fn pop_front ( & mut self ) -> Option < Entity > {
116
+ self . remove_at ( 0 )
117
+ }
118
+
119
+ /// Removes the last entity.
120
+ fn pop_back ( & mut self ) -> Option < Entity > {
121
+ if self . is_empty ( ) {
122
+ None
123
+ } else {
124
+ self . remove_at ( self . len ( ) - 1 )
125
+ }
126
+ }
127
+ }
128
+
77
129
impl RelationshipSourceCollection for Vec < Entity > {
78
130
type SourceIter < ' a > = core:: iter:: Copied < core:: slice:: Iter < ' a , Entity > > ;
79
131
@@ -98,7 +150,6 @@ impl RelationshipSourceCollection for Vec<Entity> {
98
150
fn remove ( & mut self , entity : Entity ) -> bool {
99
151
if let Some ( index) = <[ Entity ] >:: iter ( self ) . position ( |e| * e == entity) {
100
152
Vec :: remove ( self , index) ;
101
-
102
153
return true ;
103
154
}
104
155
@@ -126,6 +177,57 @@ impl RelationshipSourceCollection for Vec<Entity> {
126
177
}
127
178
}
128
179
180
+ impl OrderedRelationshipSourceCollection for Vec < Entity > {
181
+ fn insert ( & mut self , index : usize , entity : Entity ) {
182
+ self . push ( entity) ;
183
+ let len = self . len ( ) ;
184
+ if index < len {
185
+ self . swap ( index, len - 1 ) ;
186
+ }
187
+ }
188
+
189
+ fn remove_at ( & mut self , index : usize ) -> Option < Entity > {
190
+ ( index < self . len ( ) ) . then ( || self . swap_remove ( index) )
191
+ }
192
+
193
+ fn insert_stable ( & mut self , index : usize , entity : Entity ) {
194
+ if index < self . len ( ) {
195
+ Vec :: insert ( self , index, entity) ;
196
+ } else {
197
+ self . push ( entity) ;
198
+ }
199
+ }
200
+
201
+ fn remove_at_stable ( & mut self , index : usize ) -> Option < Entity > {
202
+ ( index < self . len ( ) ) . then ( || self . remove ( index) )
203
+ }
204
+
205
+ fn sort ( & mut self ) {
206
+ self . sort_unstable ( ) ;
207
+ }
208
+
209
+ fn insert_sorted ( & mut self , entity : Entity ) {
210
+ let index = self . partition_point ( |e| e <= & entity) ;
211
+ self . insert_stable ( index, entity) ;
212
+ }
213
+
214
+ fn place_most_recent ( & mut self , index : usize ) {
215
+ if let Some ( entity) = self . pop ( ) {
216
+ let index = index. min ( self . len ( ) - 1 ) ;
217
+ self . insert ( index, entity) ;
218
+ }
219
+ }
220
+
221
+ fn place ( & mut self , entity : Entity , index : usize ) {
222
+ if let Some ( current) = <[ Entity ] >:: iter ( self ) . position ( |e| * e == entity) {
223
+ // The len is at least 1, so the subtraction is safe.
224
+ let index = index. min ( self . len ( ) - 1 ) ;
225
+ Vec :: remove ( self , current) ;
226
+ self . insert ( index, entity) ;
227
+ } ;
228
+ }
229
+ }
230
+
129
231
impl RelationshipSourceCollection for EntityHashSet {
130
232
type SourceIter < ' a > = core:: iter:: Copied < crate :: entity:: hash_set:: Iter < ' a > > ;
131
233
@@ -196,7 +298,6 @@ impl<const N: usize> RelationshipSourceCollection for SmallVec<[Entity; N]> {
196
298
fn remove ( & mut self , entity : Entity ) -> bool {
197
299
if let Some ( index) = <[ Entity ] >:: iter ( self ) . position ( |e| * e == entity) {
198
300
SmallVec :: remove ( self , index) ;
199
-
200
301
return true ;
201
302
}
202
303
@@ -277,6 +378,57 @@ impl RelationshipSourceCollection for Entity {
277
378
}
278
379
}
279
380
381
+ impl < const N : usize > OrderedRelationshipSourceCollection for SmallVec < [ Entity ; N ] > {
382
+ fn insert ( & mut self , index : usize , entity : Entity ) {
383
+ self . push ( entity) ;
384
+ let len = self . len ( ) ;
385
+ if index < len {
386
+ self . swap ( index, len - 1 ) ;
387
+ }
388
+ }
389
+
390
+ fn remove_at ( & mut self , index : usize ) -> Option < Entity > {
391
+ ( index < self . len ( ) ) . then ( || self . swap_remove ( index) )
392
+ }
393
+
394
+ fn insert_stable ( & mut self , index : usize , entity : Entity ) {
395
+ if index < self . len ( ) {
396
+ SmallVec :: < [ Entity ; N ] > :: insert ( self , index, entity) ;
397
+ } else {
398
+ self . push ( entity) ;
399
+ }
400
+ }
401
+
402
+ fn remove_at_stable ( & mut self , index : usize ) -> Option < Entity > {
403
+ ( index < self . len ( ) ) . then ( || self . remove ( index) )
404
+ }
405
+
406
+ fn sort ( & mut self ) {
407
+ self . sort_unstable ( ) ;
408
+ }
409
+
410
+ fn insert_sorted ( & mut self , entity : Entity ) {
411
+ let index = self . partition_point ( |e| e <= & entity) ;
412
+ self . insert_stable ( index, entity) ;
413
+ }
414
+
415
+ fn place_most_recent ( & mut self , index : usize ) {
416
+ if let Some ( entity) = self . pop ( ) {
417
+ let index = index. min ( self . len ( ) - 1 ) ;
418
+ self . insert ( index, entity) ;
419
+ }
420
+ }
421
+
422
+ fn place ( & mut self , entity : Entity , index : usize ) {
423
+ if let Some ( current) = <[ Entity ] >:: iter ( self ) . position ( |e| * e == entity) {
424
+ // The len is at least 1, so the subtraction is safe.
425
+ let index = index. min ( self . len ( ) - 1 ) ;
426
+ SmallVec :: < [ Entity ; N ] > :: remove ( self , current) ;
427
+ self . insert ( index, entity) ;
428
+ } ;
429
+ }
430
+ }
431
+
280
432
#[ cfg( test) ]
281
433
mod tests {
282
434
use super :: * ;
0 commit comments