1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

list: add sublist method (#49)

SubList returns a sub list of the original list between the specified fromIndex, inclusive, and toIndex, exclusive.
This commit is contained in:
donutloop
2022-07-23 09:00:04 +02:00
committed by GitHub
parent 741af66404
commit 0299c454ab
3 changed files with 53 additions and 0 deletions

View File

@@ -285,3 +285,11 @@ func (l *List[T]) Intersection(other *List[T]) *List[T] {
return result
}
// SubList returns a sub list of the original list between the specified fromIndex, inclusive, and toIndex, exclusive.
func (l *List[T]) SubList(fromIndex, toIndex int) *List[T] {
data := l.data[fromIndex:toIndex]
subList := make([]T, len(data))
copy(subList, data)
return NewList(subList)
}

View File

@@ -315,3 +315,19 @@ func TestIntersection(t *testing.T) {
list3 := list1.Intersection(list2)
assert.Equal(true, expected.Equal(list3))
}
func TestSubSlice(t *testing.T) {
assert := internal.NewAssert(t, "TestSubSlice")
list := NewList([]int{1, 2, 3, 4, 5, 8})
subList := list.SubList(2, 5)
assert.Equal([]int{3, 4, 5}, subList.Data())
}
func BenchmarkSubSlice(b *testing.B) {
list := NewList([]int{1, 2, 3, 4, 5, 8})
for n := 0; n < b.N; n++ {
list.SubList(2, 5)
}
}