From 768171ecd5ce5c6d9eb4d6b0338e51f7b24f92a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Phan=20Ph=C3=BA=20Th=C3=A0nh?= Date: Wed, 4 Sep 2024 16:08:06 +0700 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20hashset:=20keys=20(#80)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: thanhpp --- pkg/hashset/hashset.go | 4 ++++ pkg/hashset/hashset_sync.go | 11 ++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/pkg/hashset/hashset.go b/pkg/hashset/hashset.go index e52a05e..b39e55b 100644 --- a/pkg/hashset/hashset.go +++ b/pkg/hashset/hashset.go @@ -33,3 +33,7 @@ func (h *HashSet[K]) Size() int { func (h *HashSet[K]) Clear() { maps.Clear(h.m) } + +func (h *HashSet[K]) Keys() []K { + return maps.Keys(h.m) +} diff --git a/pkg/hashset/hashset_sync.go b/pkg/hashset/hashset_sync.go index 374deae..cef5e65 100644 --- a/pkg/hashset/hashset_sync.go +++ b/pkg/hashset/hashset_sync.go @@ -1,6 +1,8 @@ package hashset -import "sync" +import ( + "sync" +) type SyncHashSet[K comparable] struct { rw sync.RWMutex @@ -60,3 +62,10 @@ func (sh *SyncHashSet[K]) Clear() { sh.s.Clear() } + +func (sh *SyncHashSet[K]) Keys() []K { + sh.rw.RLock() + defer sh.rw.RUnlock() + + return sh.s.Keys() +}