@@ -166,6 +166,14 @@ pub mod experimental {
166
166
pub fn iter_events < ' a > ( & ' a self ) -> impl Iterator < Item = ( ItemPosition , & ' a Event ) > {
167
167
self . chunks . iter_items ( )
168
168
}
169
+
170
+ /// Iterate over the events, starting from `position`.
171
+ pub fn iter_events_from < ' a > (
172
+ & ' a self ,
173
+ position : ItemPosition ,
174
+ ) -> Result < impl Iterator < Item = ( ItemPosition , & ' a Event ) > , LinkedChunkError > {
175
+ self . chunks . iter_items_from ( position)
176
+ }
169
177
}
170
178
171
179
#[ derive( Debug ) ]
@@ -387,8 +395,9 @@ pub mod experimental {
387
395
///
388
396
/// It iterates from the last to the first chunk.
389
397
fn iter_chunks < ' a > ( & ' a self ) -> LinkedChunkIter < ' a , T , C > {
390
- self . iter_chunks_from ( 0 )
391
- . expect ( "`iter_chunks_from` cannot fail because at least one chunk must exist" )
398
+ self . iter_chunks_from ( 0 ) . expect (
399
+ "`iter_chunks_from` cannot fail because at least one empty chunk must exist" ,
400
+ )
392
401
}
393
402
394
403
/// Iterate over the chunks, starting from `position`.
@@ -409,7 +418,16 @@ pub mod experimental {
409
418
///
410
419
/// It iterates from the last the first item.
411
420
fn iter_items < ' a > ( & ' a self ) -> impl Iterator < Item = ( ItemPosition , & ' a T ) > {
412
- self . iter_chunks ( )
421
+ self . iter_items_from ( ItemPosition ( 0 , 0 ) )
422
+ . expect ( "`iter_items_from` cannot fail because at least one empty chunk must exist" )
423
+ }
424
+
425
+ fn iter_items_from < ' a > (
426
+ & ' a self ,
427
+ position : ItemPosition ,
428
+ ) -> Result < impl Iterator < Item = ( ItemPosition , & ' a T ) > , LinkedChunkError > {
429
+ Ok ( self
430
+ . iter_chunks_from ( position. chunk_index ( ) ) ?
413
431
. filter_map ( |( chunk_index, chunk) | match & chunk. content {
414
432
ChunkContent :: Gap => None ,
415
433
ChunkContent :: Items ( items) => {
@@ -419,6 +437,7 @@ pub mod experimental {
419
437
}
420
438
} )
421
439
. flatten ( )
440
+ . skip ( position. item_index ( ) ) )
422
441
}
423
442
}
424
443
@@ -878,6 +897,41 @@ pub mod experimental {
878
897
Ok ( ( ) )
879
898
}
880
899
900
+ #[ test]
901
+ fn test_iter_items ( ) {
902
+ let mut events = Events :: < char , 2 > :: new ( ) ;
903
+ events. push_events ( [ 'a' , 'b' ] ) ;
904
+ events. push_gap ( ) ;
905
+ events. push_events ( [ 'c' , 'd' , 'e' ] ) ;
906
+
907
+ let mut iterator = events. iter_events ( ) ;
908
+
909
+ assert_matches ! ( iterator. next( ) , Some ( ( ItemPosition ( 0 , 0 ) , 'e' ) ) ) ;
910
+ assert_matches ! ( iterator. next( ) , Some ( ( ItemPosition ( 1 , 0 ) , 'd' ) ) ) ;
911
+ assert_matches ! ( iterator. next( ) , Some ( ( ItemPosition ( 1 , 1 ) , 'c' ) ) ) ;
912
+ assert_matches ! ( iterator. next( ) , Some ( ( ItemPosition ( 3 , 0 ) , 'b' ) ) ) ;
913
+ assert_matches ! ( iterator. next( ) , Some ( ( ItemPosition ( 3 , 1 ) , 'a' ) ) ) ;
914
+ assert_matches ! ( iterator. next( ) , None ) ;
915
+ }
916
+
917
+ #[ test]
918
+ fn test_iter_items_from ( ) -> Result < ( ) , LinkedChunkError > {
919
+ let mut events = Events :: < char , 2 > :: new ( ) ;
920
+ events. push_events ( [ 'a' , 'b' ] ) ;
921
+ events. push_gap ( ) ;
922
+ events. push_events ( [ 'c' , 'd' , 'e' ] ) ;
923
+
924
+ let mut iterator =
925
+ events. iter_events_from ( events. event_position ( |event| * event == 'c' ) . unwrap ( ) ) ?;
926
+
927
+ assert_matches ! ( iterator. next( ) , Some ( ( ItemPosition ( 1 , 1 ) , 'c' ) ) ) ;
928
+ assert_matches ! ( iterator. next( ) , Some ( ( ItemPosition ( 3 , 0 ) , 'b' ) ) ) ;
929
+ assert_matches ! ( iterator. next( ) , Some ( ( ItemPosition ( 3 , 1 ) , 'a' ) ) ) ;
930
+ assert_matches ! ( iterator. next( ) , None ) ;
931
+
932
+ Ok ( ( ) )
933
+ }
934
+
881
935
#[ test]
882
936
fn test_insert_items_at ( ) -> Result < ( ) , LinkedChunkError > {
883
937
let mut events = Events :: < char , 3 > :: new ( ) ;
0 commit comments