-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from KyberNetwork:feat/hashset
feat(prod): ✨ pkg: hashset
- Loading branch information
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package hashset | ||
|
||
import "golang.org/x/exp/maps" | ||
|
||
type HashSet[K comparable] struct { | ||
m map[K]struct{} | ||
} | ||
|
||
func New[K comparable]() *HashSet[K] { | ||
return &HashSet[K]{ | ||
m: map[K]struct{}{}, | ||
} | ||
} | ||
|
||
func (h *HashSet[K]) Contains(k K) bool { | ||
_, ok := h.m[k] | ||
|
||
return ok | ||
} | ||
|
||
func (h *HashSet[K]) Add(k K) { | ||
h.m[k] = struct{}{} | ||
} | ||
|
||
func (h *HashSet[K]) Remove(k K) { | ||
delete(h.m, k) | ||
} | ||
|
||
func (h *HashSet[K]) Size() int { | ||
return len(h.m) | ||
} | ||
|
||
func (h *HashSet[K]) Clear() { | ||
maps.Clear(h.m) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package hashset | ||
|
||
import "sync" | ||
|
||
type SyncHashSet[K comparable] struct { | ||
rw sync.RWMutex | ||
s *HashSet[K] | ||
} | ||
|
||
func NewSync[K comparable]() *SyncHashSet[K] { | ||
return &SyncHashSet[K]{ | ||
rw: sync.RWMutex{}, | ||
s: New[K](), | ||
} | ||
} | ||
|
||
func (sh *SyncHashSet[K]) Contains(k K) bool { | ||
sh.rw.RLock() | ||
defer sh.rw.RUnlock() | ||
|
||
return sh.s.Contains(k) | ||
} | ||
|
||
func (sh *SyncHashSet[K]) ContainsOrAdd(k K) (isContains bool) { | ||
sh.rw.Lock() | ||
defer sh.rw.Unlock() | ||
if sh.s.Contains(k) { | ||
return true | ||
} | ||
|
||
sh.s.Add(k) | ||
|
||
return false | ||
} | ||
|
||
func (sh *SyncHashSet[K]) Add(k K) { | ||
sh.rw.Lock() | ||
defer sh.rw.Unlock() | ||
|
||
sh.s.Add(k) | ||
} | ||
|
||
func (sh *SyncHashSet[K]) Remove(k K) { | ||
sh.rw.Lock() | ||
defer sh.rw.Unlock() | ||
|
||
sh.s.Remove(k) | ||
} | ||
|
||
func (sh *SyncHashSet[K]) Size() int { | ||
sh.rw.RLock() | ||
defer sh.rw.RUnlock() | ||
|
||
return sh.s.Size() | ||
} | ||
|
||
func (sh *SyncHashSet[K]) Clear() { | ||
sh.rw.Lock() | ||
defer sh.rw.Unlock() | ||
|
||
sh.s.Clear() | ||
} |