-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Delete removes elements at indices in idx from input slice, returns resulting slice. If an index is out of bounds, skip it.
- Loading branch information
1 parent
90bf328
commit 36b1ca2
Showing
8 changed files
with
138 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package pie | ||
|
||
import "sort" | ||
|
||
// Removes elements at indices in idx from input slice, returns resulting slice. | ||
// If an index is out of bounds, skip it. | ||
func Delete[T any](ss []T, idx ...int) []T { | ||
// short path O(n) inplace | ||
if len(idx) == 1 { | ||
i := idx[0] | ||
|
||
if i < 0 || i >= len(ss) { | ||
return ss | ||
} | ||
return append(ss[:i], ss[i+1:]...) | ||
} | ||
|
||
// long path O(mLog(m) + n) | ||
sort.Ints(idx) | ||
|
||
ss2 := make([]T, 0, len(ss)) | ||
|
||
prev := 0 | ||
for _, i := range idx { | ||
if i < 0 || i >= len(ss) { | ||
continue | ||
} | ||
// Copy by consecutive chunks instead of one by one | ||
ss2 = append(ss2, ss[prev:i]...) | ||
prev = i + 1 | ||
} | ||
ss2 = append(ss2, ss[prev:]...) | ||
|
||
return ss2 | ||
} |
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,61 @@ | ||
package pie_test | ||
|
||
import ( | ||
"github.com/elliotchance/pie/v2" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
var deleteTests = []struct { | ||
ss []int | ||
idx []int | ||
expected []int | ||
}{ | ||
// idx out of bounds | ||
{ | ||
[]int{1, 2}, | ||
[]int{-1}, | ||
[]int{1, 2}, | ||
}, | ||
{ | ||
[]int{1, 2}, | ||
[]int{2}, | ||
[]int{1, 2}, | ||
}, | ||
// remove from empty slice | ||
{ | ||
[]int{}, | ||
[]int{0}, | ||
[]int{}, | ||
}, | ||
{ | ||
[]int{1}, | ||
[]int{0}, | ||
[]int{}, | ||
}, | ||
{ | ||
[]int{1, 2, 3, 4, 5}, | ||
[]int{2}, | ||
[]int{1, 2, 4, 5}, | ||
}, | ||
{ | ||
[]int{1, 2, 3, 4, 5}, | ||
[]int{1, 3}, | ||
[]int{1, 3, 5}, | ||
}, | ||
// mixed indices | ||
{ | ||
[]int{1, 2, 3, 4, 5}, | ||
[]int{1, -1, 5, 3}, | ||
[]int{1, 3, 5}, | ||
}, | ||
} | ||
|
||
func TestDelete(t *testing.T) { | ||
for _, test := range deleteTests { | ||
|
||
t.Run("", func(t *testing.T) { | ||
assert.Equal(t, test.expected, pie.Delete(test.ss, test.idx...)) | ||
}) | ||
} | ||
} |
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
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