From 810360258d96b8f01c2b1eedf74074bac17c870e Mon Sep 17 00:00:00 2001 From: nikkiing <1031497516@qq.com> Date: Thu, 16 Mar 2023 17:40:11 +0800 Subject: [PATCH] pkg --- pkg/set/set.go | 40 ++++++++++++++++++++++++++++++++++++++++ pkg/{ => tree}/tree.go | 2 +- 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 pkg/set/set.go rename pkg/{ => tree}/tree.go (98%) diff --git a/pkg/set/set.go b/pkg/set/set.go new file mode 100644 index 0000000..e359432 --- /dev/null +++ b/pkg/set/set.go @@ -0,0 +1,40 @@ +package pkg + +type Set[T comparable] map[T]struct{} + +func (s Set[T]) Add(items ...T) { + for _, v := range items { + s[v] = struct{}{} + } +} + +func (s Set[T]) AddWithOutEmpty(items ...T) { + var empty T + for _, v := range items { + if v == empty { + continue + } + s[v] = struct{}{} + } +} + +func (s Set[T]) Has(data T) bool { + _, ok := s[data] + return ok +} + +func (s Set[T]) Delete(data T) { + delete(s, data) +} + +func (s Set[T]) Count() int32 { + return int32(len(s)) +} + +func (s Set[T]) ToSlice() []T { + res := make([]T, 0, len(s)) + for k := range s { + res = append(res, k) + } + return res +} diff --git a/pkg/tree.go b/pkg/tree/tree.go similarity index 98% rename from pkg/tree.go rename to pkg/tree/tree.go index 8118d22..100e46d 100644 --- a/pkg/tree.go +++ b/pkg/tree/tree.go @@ -1,4 +1,4 @@ -package pkg +package tree type Node[T any] struct { Data T `json:"data"`