-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rotate returns slice circularly rotated by a number of positions n. If n is positive, the slice is rotated right. If n is negative, the slice is rotated left.
- Loading branch information
1 parent
f1ace5f
commit 4912a95
Showing
4 changed files
with
137 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package pie | ||
|
||
// Rotate return slice circularly rotated by a number of positions n. | ||
// If n is positive, the slice is rotated right. | ||
// If n is negative, the slice is rotated left. | ||
func Rotate[T any](ss []T, n int) []T { | ||
|
||
length := len(ss) | ||
|
||
// Avoid the allocation. | ||
// If there is one element or less, then already rotated. | ||
if length < 2 { | ||
return ss | ||
} | ||
|
||
// Normalize shift | ||
// no div by 0 since length >= 2 | ||
shift := -n % length | ||
if shift < 0 { | ||
shift = length + shift | ||
} | ||
|
||
// Avoid the allocation. | ||
// If normalized shift is 0, then already rotated. | ||
if shift == 0 { | ||
return ss | ||
} | ||
|
||
return append(DropTop(ss, shift), Top(ss, shift)...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package pie_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/elliotchance/pie/v2" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var rotateTests = []struct { | ||
ss []float64 | ||
rotated []float64 | ||
n int | ||
}{ | ||
{ | ||
nil, | ||
nil, | ||
0, | ||
}, | ||
{ | ||
[]float64{1.23, 2.34}, | ||
[]float64{1.23, 2.34}, | ||
0, | ||
}, | ||
{ | ||
[]float64{}, | ||
[]float64{}, | ||
0, | ||
}, | ||
{ | ||
[]float64{}, | ||
[]float64{}, | ||
3, | ||
}, | ||
{ | ||
[]float64{1.23, 2.34}, | ||
[]float64{2.34, 1.23}, | ||
1, | ||
}, | ||
{ | ||
[]float64{1.23, 2.34}, | ||
[]float64{2.34, 1.23}, | ||
-1, | ||
}, | ||
{ | ||
[]float64{1.23}, | ||
[]float64{1.23}, | ||
1000, | ||
}, | ||
{ | ||
[]float64{1.23, 2.34, 3.45}, | ||
[]float64{1.23, 2.34, 3.45}, | ||
3, | ||
}, | ||
{ | ||
[]float64{1.23, 2.34, 3.45}, | ||
[]float64{1.23, 2.34, 3.45}, | ||
-3, | ||
}, | ||
{ | ||
[]float64{1.23, 2.34, 3.45}, | ||
[]float64{1.23, 2.34, 3.45}, | ||
6, | ||
}, | ||
{ | ||
[]float64{1.23, 2.34, 3.45}, | ||
[]float64{1.23, 2.34, 3.45}, | ||
-6, | ||
}, | ||
{ | ||
[]float64{1.23, 2.34, 3.45}, | ||
[]float64{2.34, 3.45, 1.23}, | ||
-1, | ||
}, | ||
{ | ||
[]float64{1.23, 2.34, 3.45}, | ||
[]float64{3.45, 1.23, 2.34}, | ||
1, | ||
}, | ||
} | ||
|
||
func TestRotate(t *testing.T) { | ||
for _, test := range rotateTests { | ||
t.Run("", func(t *testing.T) { | ||
rotated := pie.Rotate(test.ss, test.n) | ||
assert.Equal(t, test.rotated, rotated) | ||
}) | ||
} | ||
} |