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.

64 lines
976 B

11 years ago
11 years ago
  1. package common
  2. import "sync"
  3. /*
  4. CMap is a threadsafe map
  5. */
  6. type CMap struct {
  7. m map[string]interface{}
  8. l sync.Mutex
  9. }
  10. func NewCMap() *CMap {
  11. return &CMap{
  12. m: make(map[string]interface{}, 0),
  13. }
  14. }
  15. func (cm *CMap) Set(key string, value interface{}) {
  16. cm.l.Lock()
  17. defer cm.l.Unlock()
  18. cm.m[key] = value
  19. }
  20. func (cm *CMap) Get(key string) interface{} {
  21. cm.l.Lock()
  22. defer cm.l.Unlock()
  23. return cm.m[key]
  24. }
  25. func (cm *CMap) Has(key string) bool {
  26. cm.l.Lock()
  27. defer cm.l.Unlock()
  28. _, ok := cm.m[key]
  29. return ok
  30. }
  31. func (cm *CMap) Delete(key string) {
  32. cm.l.Lock()
  33. defer cm.l.Unlock()
  34. delete(cm.m, key)
  35. }
  36. func (cm *CMap) Size() int {
  37. cm.l.Lock()
  38. defer cm.l.Unlock()
  39. return len(cm.m)
  40. }
  41. func (cm *CMap) Clear() {
  42. cm.l.Lock()
  43. defer cm.l.Unlock()
  44. cm.m = make(map[string]interface{}, 0)
  45. }
  46. func (cm *CMap) Values() []interface{} {
  47. cm.l.Lock()
  48. defer cm.l.Unlock()
  49. items := []interface{}{}
  50. for _, v := range cm.m {
  51. items = append(items, v)
  52. }
  53. return items
  54. }