English | 中文 |
---|
HMap is a safe for concurrent generic map implementation in Go that provides a simple and efficient API. It supports generic key-value pairs and includes methods for querying the length of the map as well as clearing the entire map.
- Generic Support: Utilizes Go's generic features for flexibility with different types of key-value pairs.
- Length Querying: Easily determine the length of the map using the
Len
method. - Clear Map: Clear the entire map using the
Clean
method.
>= 1.20
Map
is a concurrent-safe map implementation based on a mutex.
package main
import (
"fmt"
"github.com/lyonnee/hmap"
)
func main() {
// Create a new Map instance
myMap := hmap.NewMap[string, int](10)
// Store key-value pairs
myMap.Store("key1", 1)
myMap.Store("key2", 2)
// Query the length of the map
length := myMap.Len()
fmt.Printf("Map Length: %d\n", length)
// Clear the map
myMap.Clean()
// Query the length of the map after cleaning
length = myMap.Len()
fmt.Printf("Map Length after Clean: %d\n", length)
}
SyncMap
is a concurrent-safe map implementation based on sync.Map.
package main
import (
"fmt"
"github.com/lyonnee/hmap"
)
func main() {
// Create a new SyncMap instance
mySyncMap := hmap.NewSyncMap[string, int]()
// Store key-value pairs
mySyncMap.Store("key1", 1)
mySyncMap.Store("key2", 2)
// Query the length of the map
length := mySyncMap.Len()
fmt.Printf("SyncMap Length: %d\n", length)
// Clear the map
mySyncMap.Clean()
// Query the length of the map after cleaning
length = mySyncMap.Len()
fmt.Printf("SyncMap Length after Clean: %d\n", length)
}
- Advantages:
- mplemented using a mutex, simple and intuitive.
- Suitable for scenarios with low concurrency read/write operations.
- Disadvantages:
- Performance may be lower than
SyncMap
in high-concurrency write scenarios, as the mutex causes write operations to block.
- Performance may be lower than
- Advantages:
- Implemented using sync.Map, suitable for high-concurrency scenarios.
- Write operations do not block read operations, providing better performance.
- Disadvantages:
- Implementation is more complex and has a larger codebase.
- Performance may be lower than
Map
in low-concurrency scenarios.
Creates a new instance of the HMap.
Gets the length of the map.
Loads the value associated with the specified key.
Swaps the value associated with the specified key and returns the previous value. If the key does not exist, the length is incremented.
Stores the key-value pair. If the key already exists, its value is updated.
Loads the value for the specified key. If the key does not exist, the given value is stored, and the length is incremented.
Deletes the value associated with the specified key. If the key exists, the length is decremented.
Loads and deletes the value associated with the specified key. If the key exists, the length is decremented.
Iterates over the map, applying a given function to each key-value pair.
Clears the entire map.
Compares and swaps the value associated with the specified key. Returns true
if the swap is successful.
Compares and deletes the value associated with the specified key, given the old value. Returns true
if the delete is successful. If successful, the length is decremented.
This project is distributed under the MIT License.
Thanks to all contributors to this project.
Thank you for using HMap! If you have any questions or suggestions, feel free to reach out.