-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: some issues * mod: code review
- Loading branch information
Showing
12 changed files
with
104 additions
and
74 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
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
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
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
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
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
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
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
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
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,52 @@ | ||
package utils | ||
|
||
import "sync" | ||
|
||
type mutexMap[T any] struct { | ||
mu sync.RWMutex | ||
data map[string]T | ||
} | ||
|
||
func NewMutexMap[T any]() *mutexMap[T] { | ||
return &mutexMap[T]{ | ||
data: make(map[string]T), | ||
} | ||
} | ||
|
||
func (m *mutexMap[T]) Get(key string) (T, bool) { | ||
m.mu.RLock() | ||
defer m.mu.RUnlock() | ||
val, ok := m.data[key] | ||
return val, ok | ||
} | ||
|
||
func (m *mutexMap[T]) Set(key string, val T) { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
m.data[key] = val | ||
} | ||
|
||
func (m *mutexMap[T]) Delete(key string) { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
delete(m.data, key) | ||
} | ||
func (m *mutexMap[T]) Size() int { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
return len(m.data) | ||
} | ||
|
||
func (m *mutexMap[T]) ForEach(f func(key string, val T)) { | ||
m.mu.RLock() | ||
defer m.mu.RUnlock() | ||
for k, v := range m.data { | ||
f(k, v) | ||
} | ||
} | ||
|
||
func (m *mutexMap[T]) Clear() { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
m.data = make(map[string]T) | ||
} |
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 |
---|---|---|
@@ -1,52 +1,10 @@ | ||
package utils | ||
|
||
import "sync" | ||
|
||
type mutexMap[T any] struct { | ||
mu sync.RWMutex | ||
data map[string]T | ||
} | ||
|
||
func NewMutexMap[T any]() *mutexMap[T] { | ||
return &mutexMap[T]{ | ||
data: make(map[string]T), | ||
func Contains[T comparable](s []T, e T) bool { | ||
for _, a := range s { | ||
if a == e { | ||
return true | ||
} | ||
} | ||
} | ||
|
||
func (m *mutexMap[T]) Get(key string) (T, bool) { | ||
m.mu.RLock() | ||
defer m.mu.RUnlock() | ||
val, ok := m.data[key] | ||
return val, ok | ||
} | ||
|
||
func (m *mutexMap[T]) Set(key string, val T) { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
m.data[key] = val | ||
} | ||
|
||
func (m *mutexMap[T]) Delete(key string) { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
delete(m.data, key) | ||
} | ||
func (m *mutexMap[T]) Size() int { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
return len(m.data) | ||
} | ||
|
||
func (m *mutexMap[T]) ForEach(f func(key string, val T)) { | ||
m.mu.RLock() | ||
defer m.mu.RUnlock() | ||
for k, v := range m.data { | ||
f(k, v) | ||
} | ||
} | ||
|
||
func (m *mutexMap[T]) Clear() { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
m.data = make(map[string]T) | ||
return false | ||
} |
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