@@ -408,65 +408,21 @@ func Clip[S ~[]E, E any](s S) S {
408
408
return s [:len (s ):len (s )]
409
409
}
410
410
411
- // Rotation algorithm explanation:
412
- //
413
- // rotate left by 2
414
- // start with
415
- // 0123456789
416
- // split up like this
417
- // 01 234567 89
418
- // swap first 2 and last 2
419
- // 89 234567 01
420
- // join first parts
421
- // 89234567 01
422
- // recursively rotate first left part by 2
423
- // 23456789 01
424
- // join at the end
425
- // 2345678901
426
- //
427
- // rotate left by 8
428
- // start with
429
- // 0123456789
430
- // split up like this
431
- // 01 234567 89
432
- // swap first 2 and last 2
433
- // 89 234567 01
434
- // join last parts
435
- // 89 23456701
436
- // recursively rotate second part left by 6
437
- // 89 01234567
438
- // join at the end
439
- // 8901234567
440
-
441
411
// TODO: There are other rotate algorithms.
442
- // This algorithm has the desirable property that it moves each element exactly twice.
443
- // The triple-reverse algorithm is simpler and more cache friendly, but takes more writes.
412
+ // This algorithm has the desirable property that it moves each element at most twice.
444
413
// The follow-cycles algorithm can be 1-write but it is not very cache friendly.
445
414
446
- // rotateLeft rotates b left by n spaces.
415
+ // rotateLeft rotates s left by r spaces.
447
416
// s_final[i] = s_orig[i+r], wrapping around.
448
417
func rotateLeft [E any ](s []E , r int ) {
449
- for r != 0 && r != len (s ) {
450
- if r * 2 <= len (s ) {
451
- swap (s [:r ], s [len (s )- r :])
452
- s = s [:len (s )- r ]
453
- } else {
454
- swap (s [:len (s )- r ], s [r :])
455
- s , r = s [len (s )- r :], r * 2 - len (s )
456
- }
457
- }
418
+ Reverse (s [:r ])
419
+ Reverse (s [r :])
420
+ Reverse (s )
458
421
}
459
422
func rotateRight [E any ](s []E , r int ) {
460
423
rotateLeft (s , len (s )- r )
461
424
}
462
425
463
- // swap swaps the contents of x and y. x and y must be equal length and disjoint.
464
- func swap [E any ](x , y []E ) {
465
- for i := 0 ; i < len (x ); i ++ {
466
- x [i ], y [i ] = y [i ], x [i ]
467
- }
468
- }
469
-
470
426
// overlaps reports whether the memory ranges a[0:len(a)] and b[0:len(b)] overlap.
471
427
func overlaps [E any ](a , b []E ) bool {
472
428
if len (a ) == 0 || len (b ) == 0 {
0 commit comments