diff options
| author | Junegunn Choi <junegunn.c@gmail.com> | 2025-06-21 23:24:38 +0900 |
|---|---|---|
| committer | Junegunn Choi <junegunn.c@gmail.com> | 2025-06-21 23:24:38 +0900 |
| commit | 247d168af6fabcc322f54ad366f4fb45f781137b (patch) | |
| tree | 75faf7646a4690a7b6868ae9a561d13c80ae0149 /src/util | |
| parent | b2a8a283c79f30f5027fb181bc27668ffc37d8b9 (diff) | |
| download | fzf-247d168af6fabcc322f54ad366f4fb45f781137b.tar.gz | |
Terminate running background transform on exit
Close #4422
Diffstat (limited to 'src/util')
| -rw-r--r-- | src/util/concurrent_set.go | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/util/concurrent_set.go b/src/util/concurrent_set.go new file mode 100644 index 00000000..c2ffc619 --- /dev/null +++ b/src/util/concurrent_set.go @@ -0,0 +1,39 @@ +package util + +import "sync" + +// ConcurrentSet is a thread-safe set implementation. +type ConcurrentSet[T comparable] struct { + lock sync.RWMutex + items map[T]struct{} +} + +// NewConcurrentSet creates a new ConcurrentSet. +func NewConcurrentSet[T comparable]() *ConcurrentSet[T] { + return &ConcurrentSet[T]{ + items: make(map[T]struct{}), + } +} + +// Add adds an item to the set. +func (s *ConcurrentSet[T]) Add(item T) { + s.lock.Lock() + defer s.lock.Unlock() + s.items[item] = struct{}{} +} + +// Remove removes an item from the set. +func (s *ConcurrentSet[T]) Remove(item T) { + s.lock.Lock() + defer s.lock.Unlock() + delete(s.items, item) +} + +// ForEach iterates over each item in the set and applies the provided function. +func (s *ConcurrentSet[T]) ForEach(fn func(item T)) { + s.lock.RLock() + defer s.lock.RUnlock() + for item := range s.items { + fn(item) + } +} |
