diff --git a/lib/gat/poolers/session/pooler.go b/lib/gat/poolers/session/pooler.go index 8f0dd004ce3ab9caf066e670325726eaf8508f23..ab067e6bfea3129164c31ce8b2a13b141a0b0c30 100644 --- a/lib/gat/poolers/session/pooler.go +++ b/lib/gat/poolers/session/pooler.go @@ -6,6 +6,7 @@ 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 new file mode 100644 index 0000000000000000000000000000000000000000..e0ab94dad73f66b09113287763938eaa40748744 --- /dev/null +++ b/lib/util/slices/delete.go @@ -0,0 +1,16 @@ +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 04ca24c193ccdf31c21187f0b9452dc78f865346..baebe539aa24287bc18e321ab52442df489103a7 100644 --- a/lib/util/slices/remove.go +++ b/lib/util/slices/remove.go @@ -1,8 +1,6 @@ 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 -// with length-1. The original slice will contain all items (though in a different order), and the new slice will contain all -// but item. +// Remove will remove the item from the slice, retaining the original order func Remove[T comparable](slice []T, item T) []T { i := Index(slice, item) if i == -1 { @@ -12,22 +10,6 @@ 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]