From 92351098bc47bcc3499461be3b090f0c13f1fe3f Mon Sep 17 00:00:00 2001 From: Garet Halliday <me@garet.holiday> Date: Thu, 12 Oct 2023 18:57:46 -0500 Subject: [PATCH] Revert "a" This reverts commit 8325b0bcc602cdaeb71de95c6e7553f7a8620df1. --- lib/gat/poolers/session/pooler.go | 1 - lib/util/slices/delete.go | 16 ---------------- lib/util/slices/remove.go | 20 +++++++++++++++++++- 3 files changed, 19 insertions(+), 18 deletions(-) delete mode 100644 lib/util/slices/delete.go diff --git a/lib/gat/poolers/session/pooler.go b/lib/gat/poolers/session/pooler.go index ab067e6b..8f0dd004 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 e0ab94da..00000000 --- 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 baebe539..04ca24c1 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] -- GitLab