https://github.com/lyonnee/hmap
HMap(Happy Map) 是一个并发安全的支持泛型的Map实现,旨在提供简单易用的 API,支持泛型键值对,同时添加了对Map长度的查询以及清空Map的方法。
https://github.com/lyonnee/hmap
generic-map safemap
Last synced: 6 days ago
JSON representation
HMap(Happy Map) 是一个并发安全的支持泛型的Map实现,旨在提供简单易用的 API,支持泛型键值对,同时添加了对Map长度的查询以及清空Map的方法。
- Host: GitHub
- URL: https://github.com/lyonnee/hmap
- Owner: lyonnee
- License: mit
- Created: 2024-01-03T08:43:30.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2025-01-23T02:42:16.000Z (about 1 year ago)
- Last Synced: 2025-12-18T02:43:34.335Z (3 months ago)
- Topics: generic-map, safemap
- Language: Go
- Homepage:
- Size: 22.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go-cn - hmap - to-use API. (数据结构与算法 / 地图)
- awesome-go - hmap - HMap is a concurrent and secure, generic support Map implementation designed to provide an easy-to-use API. (Data Structures and Algorithms / Maps)
- fucking-awesome-go - hmap - HMap is a concurrent and secure, generic support Map implementation designed to provide an easy-to-use API. (Data Structures and Algorithms / Maps)
- awesome-go-with-stars - hmap - to-use API. | 2025-01-23 | (Data Integration Frameworks / Maps)
- awesome-go - hmap - HMap is a concurrent and secure, generic support Map implementation designed to provide an easy-to-use API. (Data Structures and Algorithms / Maps)
README
# HMap - Safe For Concurrent Generic Map
| English | [中文](README_zh.md) |
| --- | --- |
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.
[](https://goreportcard.com/report/github.com/lyonnee/hmap)

[](https://codecov.io/gh/lyonnee/hmap)
## Features
- **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.
## Go Version
`>= 1.20`
## Quick Start
### Using `Map`
`Map` is a concurrent-safe map implementation based on a mutex.
```go
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)
}
```
### Using `SyncMap`
`SyncMap` is a concurrent-safe map implementation based on sync.Map.
```go
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)
}
```
## Comparison of `Map` and `SyncMap`
### Map
- 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.
### SyncMap
- 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.
## Methods
### `New() *Map[K, V]`
Creates a new instance of the HMap.
### `Len() int`
Gets the length of the map.
### `Load(key K) (V, bool)`
Loads the value associated with the specified key.
### `Swap(k K, v V) (V, bool)`
Swaps the value associated with the specified key and returns the previous value. If the key does not exist, the length is incremented.
### `Store(k K, v V)`
Stores the key-value pair. If the key already exists, its value is updated.
### `LoadOrStore(k K, v V) (V, bool)`
Loads the value for the specified key. If the key does not exist, the given value is stored, and the length is incremented.
### `Delete(k K)`
Deletes the value associated with the specified key. If the key exists, the length is decremented.
### `LoadAndDelete(k K) (V, bool)`
Loads and deletes the value associated with the specified key. If the key exists, the length is decremented.
### `Range(fn func(k K, v V) bool)`
Iterates over the map, applying a given function to each key-value pair.
### `Clean()`
Clears the entire map.
### `CompareAndSwap(key K, old, new V) bool`
Compares and swaps the value associated with the specified key. Returns `true` if the swap is successful.
### `CompareAndDelete(key K, old V) bool`
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.
## License
This project is distributed under the [MIT License](LICENSE).
## Contributors
Thanks to all contributors to this project.
## Acknowledgments
Thank you for using HMap! If you have any questions or suggestions, feel free to reach out.