Created
September 10, 2017 01:04
-
-
Save deckarep/45527da11a10ccb0ca127ca7a82027e2 to your computer and use it in GitHub Desktop.
Regular Thread-Safe Int Map
This file contains hidden or 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
package RegularIntMap | |
type RegularIntMap struct { | |
sync.RWMutex | |
internal map[int]int | |
} | |
func NewRegularIntMap() *RegularIntMap { | |
return &RegularIntMap{ | |
internal: make(map[int]int), | |
} | |
} | |
func (rm *RegularIntMap) Load(key int) (value int, ok bool) { | |
rm.RLock() | |
result, ok := rm.internal[key] | |
rm.RUnlock() | |
return result, ok | |
} | |
func (rm *RegularIntMap) Delete(key int) { | |
rm.Lock() | |
delete(rm.internal, key) | |
rm.Unlock() | |
} | |
func (rm *RegularIntMap) Store(key, value int) { | |
rm.Lock() | |
rm.internal[key] = value | |
rm.Unlock() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment