diff --git a/lib/gat/poolers/session/pooler.go b/lib/gat/poolers/session/pooler.go index ab067e6bfea3129164c31ce8b2a13b141a0b0c30..8f0dd004ce3ab9caf066e670325726eaf8508f23 100644 --- a/lib/gat/poolers/session/pooler.go +++ b/lib/gat/poolers/session/pooler.go @@ -6,7 +6,6 @@ import ( "github.com/google/uuid" "gfx.cafe/gfx/pggat/lib/gat/pool" - "gfx.cafe/gfx/pggat/lib/util/slices" ) diff --git a/lib/util/slices/delete.go b/lib/util/slices/delete.go deleted file mode 100644 index e0ab94dad73f66b09113287763938eaa40748744..0000000000000000000000000000000000000000 --- a/lib/util/slices/delete.go +++ /dev/null @@ -1,16 +0,0 @@ -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] -} diff --git a/lib/util/slices/remove.go b/lib/util/slices/remove.go index baebe539aa24287bc18e321ab52442df489103a7..04ca24c193ccdf31c21187f0b9452dc78f865346 100644 --- a/lib/util/slices/remove.go +++ b/lib/util/slices/remove.go @@ -1,6 +1,8 @@ package slices -// Remove will remove the item from the slice, retaining the original order +// 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 +// 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 { i := Index(slice, item) if i == -1 { @@ -10,6 +12,22 @@ func Remove[T comparable](slice []T, item T) []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:]) slice[len(slice)-1] = *new(T) return slice[:len(slice)-1]