{"id":32948792,"url":"https://github.com/lyonnee/hmap","last_synced_at":"2026-03-09T16:37:25.241Z","repository":{"id":215265076,"uuid":"738445088","full_name":"lyonnee/hmap","owner":"lyonnee","description":"HMap(Happy Map) 是一个并发安全的支持泛型的Map实现，旨在提供简单易用的 API，支持泛型键值对，同时添加了对Map长度的查询以及清空Map的方法。","archived":false,"fork":false,"pushed_at":"2025-01-23T02:42:16.000Z","size":23,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-12-18T02:43:34.335Z","etag":null,"topics":["generic-map","safemap"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lyonnee.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-03T08:43:30.000Z","updated_at":"2025-01-23T02:42:19.000Z","dependencies_parsed_at":"2024-01-03T13:29:30.850Z","dependency_job_id":"270fed66-29fb-465d-b330-6b53e83c908c","html_url":"https://github.com/lyonnee/hmap","commit_stats":null,"previous_names":["lyonnee/hmap"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/lyonnee/hmap","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lyonnee%2Fhmap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lyonnee%2Fhmap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lyonnee%2Fhmap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lyonnee%2Fhmap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lyonnee","download_url":"https://codeload.github.com/lyonnee/hmap/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lyonnee%2Fhmap/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30302661,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-09T14:33:48.460Z","status":"ssl_error","status_checked_at":"2026-03-09T14:33:48.027Z","response_time":61,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["generic-map","safemap"],"created_at":"2025-11-12T20:00:35.661Z","updated_at":"2026-03-09T16:37:25.204Z","avatar_url":"https://github.com/lyonnee.png","language":"Go","readme":"\u003cdiv align=\"center\"\u003e\n\u003c/br\u003e\n\n# HMap - Safe For Concurrent Generic Map\n\n| English | [中文](README_zh.md) |\n| --- | --- |\n\nHMap 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.\n\n\u003c/div\u003e\n\n[![Go Report Card](https://goreportcard.com/badge/github.com/lyonnee/hmap)](https://goreportcard.com/report/github.com/lyonnee/hmap)\n![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/lyonnee/hmap)\n[![codecov](https://codecov.io/gh/lyonnee/hmap/graph/badge.svg?token=FU9T3QWBVJ)](https://codecov.io/gh/lyonnee/hmap)\n\n## Features\n\n- **Generic Support:** Utilizes Go's generic features for flexibility with different types of key-value pairs.\n- **Length Querying:** Easily determine the length of the map using the `Len` method.\n- **Clear Map:** Clear the entire map using the `Clean` method.\n\n## Go Version\n\n`\u003e= 1.20`\n\n## Quick Start\n\n### Using `Map`\n\n`Map` is a concurrent-safe map implementation based on a mutex.\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/lyonnee/hmap\"\n)\n\nfunc main() {\n\t// Create a new Map instance\n\tmyMap := hmap.NewMap[string, int](10)\n\n\t// Store key-value pairs\n\tmyMap.Store(\"key1\", 1)\n\tmyMap.Store(\"key2\", 2)\n\n\t// Query the length of the map\n\tlength := myMap.Len()\n\tfmt.Printf(\"Map Length: %d\\n\", length)\n\n\t// Clear the map\n\tmyMap.Clean()\n\n\t// Query the length of the map after cleaning\n\tlength = myMap.Len()\n\tfmt.Printf(\"Map Length after Clean: %d\\n\", length)\n}\n```\n\n### Using `SyncMap`\n\n`SyncMap` is a concurrent-safe map implementation based on sync.Map.\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/lyonnee/hmap\"\n)\n\nfunc main() {\n\t// Create a new SyncMap instance\n\tmySyncMap := hmap.NewSyncMap[string, int]()\n\n\t// Store key-value pairs\n\tmySyncMap.Store(\"key1\", 1)\n\tmySyncMap.Store(\"key2\", 2)\n\n\t// Query the length of the map\n\tlength := mySyncMap.Len()\n\tfmt.Printf(\"SyncMap Length: %d\\n\", length)\n\n\t// Clear the map\n\tmySyncMap.Clean()\n\n\t// Query the length of the map after cleaning\n\tlength = mySyncMap.Len()\n\tfmt.Printf(\"SyncMap Length after Clean: %d\\n\", length)\n}\n```\n\n## Comparison of `Map` and `SyncMap`\n### Map\n- Advantages:\n\t- mplemented using a mutex, simple and intuitive.\n\t- Suitable for scenarios with low concurrency read/write operations.\n- Disadvantages:\n\t- Performance may be lower than `SyncMap` in high-concurrency write scenarios, as the mutex causes write operations to block.\n### SyncMap\n- Advantages:\n\t- Implemented using sync.Map, suitable for high-concurrency scenarios.\n\t- Write operations do not block read operations, providing better performance.\n- Disadvantages:\n\t- Implementation is more complex and has a larger codebase.\n\t- Performance may be lower than `Map` in low-concurrency scenarios.\n\n## Methods\n\n### `New() *Map[K, V]`\n\nCreates a new instance of the HMap.\n\n### `Len() int`\n\nGets the length of the map.\n\n### `Load(key K) (V, bool)`\n\nLoads the value associated with the specified key.\n\n### `Swap(k K, v V) (V, bool)`\n\nSwaps the value associated with the specified key and returns the previous value. If the key does not exist, the length is incremented.\n\n### `Store(k K, v V)`\n\nStores the key-value pair. If the key already exists, its value is updated.\n\n### `LoadOrStore(k K, v V) (V, bool)`\n\nLoads the value for the specified key. If the key does not exist, the given value is stored, and the length is incremented.\n\n### `Delete(k K)`\n\nDeletes the value associated with the specified key. If the key exists, the length is decremented.\n\n### `LoadAndDelete(k K) (V, bool)`\n\nLoads and deletes the value associated with the specified key. If the key exists, the length is decremented.\n\n### `Range(fn func(k K, v V) bool)`\n\nIterates over the map, applying a given function to each key-value pair.\n\n### `Clean()`\n\nClears the entire map.\n\n### `CompareAndSwap(key K, old, new V) bool`\n\nCompares and swaps the value associated with the specified key. Returns `true` if the swap is successful.\n\n### `CompareAndDelete(key K, old V) bool`\n\nCompares 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.\n\n## License\n\nThis project is distributed under the [MIT License](LICENSE).\n\n## Contributors\n\nThanks to all contributors to this project.\n\n## Acknowledgments\n\nThank you for using HMap! If you have any questions or suggestions, feel free to reach out.\n","funding_links":[],"categories":["数据结构与算法","Data Structures and Algorithms","Data Integration Frameworks"],"sub_categories":["地图","Maps"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flyonnee%2Fhmap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flyonnee%2Fhmap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flyonnee%2Fhmap/lists"}