Skip to content

Commit 85f73f6

Browse files
rjNemoclaude
andauthored
feat: add Sliding window function (#47)
- Add Sliding: creates sliding window views of a slice - Pre-allocated for optimal performance - Returns independent window copies (non-mutating) - Comprehensive tests including edge cases - Benchmark included Example: Sliding([1,2,3,4,5], 3) → [[1,2,3], [2,3,4], [3,4,5]] Resolves Issue 19 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <noreply@anthropic.com>
1 parent 4f02db2 commit 85f73f6

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

sliding.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package underscore
2+
3+
// Sliding creates a sliding window view of the slice with the specified window size.
4+
// Returns an empty slice if size is less than or equal to 0.
5+
// Returns an empty slice if size is greater than the slice length.
6+
//
7+
// Example: Sliding([]int{1,2,3,4,5}, 3) → [[1,2,3], [2,3,4], [3,4,5]]
8+
func Sliding[T any](values []T, size int) [][]T {
9+
if size <= 0 || size > len(values) {
10+
return [][]T{}
11+
}
12+
13+
windowCount := len(values) - size + 1
14+
res := make([][]T, 0, windowCount)
15+
16+
for i := 0; i <= len(values)-size; i++ {
17+
window := make([]T, size)
18+
copy(window, values[i:i+size])
19+
res = append(res, window)
20+
}
21+
22+
return res
23+
}

sliding_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package underscore_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
u "github.com/rjNemo/underscore"
9+
)
10+
11+
func TestSliding(t *testing.T) {
12+
nums := []int{1, 2, 3, 4, 5}
13+
result := u.Sliding(nums, 3)
14+
expected := [][]int{{1, 2, 3}, {2, 3, 4}, {3, 4, 5}}
15+
assert.Equal(t, expected, result)
16+
}
17+
18+
func TestSlidingEmpty(t *testing.T) {
19+
result := u.Sliding([]int{}, 3)
20+
assert.Equal(t, [][]int{}, result)
21+
}
22+
23+
func TestSlidingSizeOne(t *testing.T) {
24+
nums := []int{1, 2, 3}
25+
result := u.Sliding(nums, 1)
26+
expected := [][]int{{1}, {2}, {3}}
27+
assert.Equal(t, expected, result)
28+
}
29+
30+
func TestSlidingSizeEqualLength(t *testing.T) {
31+
nums := []int{1, 2, 3}
32+
result := u.Sliding(nums, 3)
33+
expected := [][]int{{1, 2, 3}}
34+
assert.Equal(t, expected, result)
35+
}
36+
37+
func TestSlidingSizeGreaterThanLength(t *testing.T) {
38+
nums := []int{1, 2, 3}
39+
result := u.Sliding(nums, 5)
40+
assert.Equal(t, [][]int{}, result)
41+
}
42+
43+
func TestSlidingSizeZero(t *testing.T) {
44+
nums := []int{1, 2, 3}
45+
result := u.Sliding(nums, 0)
46+
assert.Equal(t, [][]int{}, result)
47+
}
48+
49+
func TestSlidingSizeNegative(t *testing.T) {
50+
nums := []int{1, 2, 3}
51+
result := u.Sliding(nums, -1)
52+
assert.Equal(t, [][]int{}, result)
53+
}
54+
55+
func TestSlidingTwoElements(t *testing.T) {
56+
nums := []int{1, 2, 3, 4}
57+
result := u.Sliding(nums, 2)
58+
expected := [][]int{{1, 2}, {2, 3}, {3, 4}}
59+
assert.Equal(t, expected, result)
60+
}
61+
62+
func TestSlidingStrings(t *testing.T) {
63+
words := []string{"a", "b", "c", "d"}
64+
result := u.Sliding(words, 2)
65+
expected := [][]string{{"a", "b"}, {"b", "c"}, {"c", "d"}}
66+
assert.Equal(t, expected, result)
67+
}
68+
69+
func TestSlidingDoesNotMutate(t *testing.T) {
70+
original := []int{1, 2, 3, 4}
71+
result := u.Sliding(original, 2)
72+
73+
// Modify a window
74+
result[0][0] = 999
75+
76+
// Original should be unchanged
77+
assert.Equal(t, 1, original[0])
78+
}
79+
80+
func BenchmarkSliding(b *testing.B) {
81+
nums := make([]int, 100)
82+
for i := range nums {
83+
nums[i] = i
84+
}
85+
86+
b.ResetTimer()
87+
for i := 0; i < b.N; i++ {
88+
u.Sliding(nums, 10)
89+
}
90+
}

0 commit comments

Comments
 (0)