You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.2 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package cmap
  2. import (
  3. tmsync "github.com/tendermint/tendermint/internal/libs/sync"
  4. )
  5. // CMap is a goroutine-safe map
  6. type CMap struct {
  7. m map[string]interface{}
  8. l tmsync.Mutex
  9. }
  10. func NewCMap() *CMap {
  11. return &CMap{
  12. m: make(map[string]interface{}),
  13. }
  14. }
  15. func (cm *CMap) Set(key string, value interface{}) {
  16. cm.l.Lock()
  17. cm.m[key] = value
  18. cm.l.Unlock()
  19. }
  20. func (cm *CMap) Get(key string) interface{} {
  21. cm.l.Lock()
  22. val := cm.m[key]
  23. cm.l.Unlock()
  24. return val
  25. }
  26. func (cm *CMap) Has(key string) bool {
  27. cm.l.Lock()
  28. _, ok := cm.m[key]
  29. cm.l.Unlock()
  30. return ok
  31. }
  32. func (cm *CMap) Delete(key string) {
  33. cm.l.Lock()
  34. delete(cm.m, key)
  35. cm.l.Unlock()
  36. }
  37. func (cm *CMap) Size() int {
  38. cm.l.Lock()
  39. size := len(cm.m)
  40. cm.l.Unlock()
  41. return size
  42. }
  43. func (cm *CMap) Clear() {
  44. cm.l.Lock()
  45. cm.m = make(map[string]interface{})
  46. cm.l.Unlock()
  47. }
  48. func (cm *CMap) Keys() []string {
  49. cm.l.Lock()
  50. keys := make([]string, 0, len(cm.m))
  51. for k := range cm.m {
  52. keys = append(keys, k)
  53. }
  54. cm.l.Unlock()
  55. return keys
  56. }
  57. func (cm *CMap) Values() []interface{} {
  58. cm.l.Lock()
  59. items := make([]interface{}, 0, len(cm.m))
  60. for _, v := range cm.m {
  61. items = append(items, v)
  62. }
  63. cm.l.Unlock()
  64. return items
  65. }