good morning!!!!

Skip to content
Snippets Groups Projects
Commit 8325b0bc authored by Garet Halliday's avatar Garet Halliday
Browse files

a

parent de98b166
Branches
Tags
No related merge requests found
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
"gfx.cafe/gfx/pggat/lib/gat/pool" "gfx.cafe/gfx/pggat/lib/gat/pool"
"gfx.cafe/gfx/pggat/lib/util/slices" "gfx.cafe/gfx/pggat/lib/util/slices"
) )
......
package slices
// Delete is similar to Remove but doesn't retain order.
func Delete[T comparable](slice []T, item T) []T {
i := Index(slice, item)
if i == -1 {
return slice
}
return DeleteIndex(slice, i)
}
func DeleteIndex[T any](slice []T, idx int) []T {
slice[idx] = slice[len(slice)-1]
slice[len(slice)-1] = *new(T)
return slice[:len(slice)-1]
}
package slices package slices
// Remove will check for item in the target slice. If it finds it, it will move it to the end of the slice and return a slice // Remove will remove the item from the slice, retaining the original order
// with length-1. The original slice will contain all items (though in a different order), and the new slice will contain all
// but item.
func Remove[T comparable](slice []T, item T) []T { func Remove[T comparable](slice []T, item T) []T {
i := Index(slice, item) i := Index(slice, item)
if i == -1 { if i == -1 {
...@@ -12,22 +10,6 @@ func Remove[T comparable](slice []T, item T) []T { ...@@ -12,22 +10,6 @@ func Remove[T comparable](slice []T, item T) []T {
} }
func RemoveIndex[T any](slice []T, idx int) []T { func RemoveIndex[T any](slice []T, idx int) []T {
item := slice[idx]
copy(slice[idx:], slice[idx+1:])
slice[len(slice)-1] = item
return slice[:len(slice)-1]
}
// Delete is similar to Remove but leaves a *new(T) in the old slice, allowing the value to be GC'd
func Delete[T comparable](slice []T, item T) []T {
i := Index(slice, item)
if i == -1 {
return slice
}
return DeleteIndex(slice, i)
}
func DeleteIndex[T any](slice []T, idx int) []T {
copy(slice[idx:], slice[idx+1:]) copy(slice[idx:], slice[idx+1:])
slice[len(slice)-1] = *new(T) slice[len(slice)-1] = *new(T)
return slice[:len(slice)-1] return slice[:len(slice)-1]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment