main.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package maps
  2. import (
  3. "fmt"
  4. "surdeus.su/core/gods"
  5. )
  6. // Generic map interface for all the maps.
  7. type Map[K comparable, V any] interface {
  8. gods.Container[V]
  9. // Returns if the map has the key set.
  10. Has(K) bool
  11. // Get the value and if the key is not set panic.
  12. Get(K) V
  13. // The Go way to get values without panicing.
  14. Got(K) (V, bool)
  15. // Set the value or reset if it is already set.
  16. Set(K, V)
  17. // Delete the key no matter it exists or not.
  18. Del(K)
  19. // Returns slice of values.
  20. // For the order look the comment
  21. // for "Keys()".
  22. Values() []V
  23. // Get the values channel.
  24. Chan() chan V
  25. // Returns slice of keys.
  26. // Order is not guaranteed if
  27. // the is not specified otherwise
  28. // like for the NewOrdered.
  29. Keys() []K
  30. KeyChan() chan K
  31. // The function to range over the values
  32. // with the keys.
  33. /*Range() chan struct{
  34. K K
  35. V V
  36. }*/
  37. }
  38. type lMap[K comparable, V any] struct {
  39. store map[K]V
  40. }
  41. // Returns new basic map with the builtin Go type down there.
  42. // Has all the features of the builtin Go maps and same performance.
  43. func New[K comparable, V any]() Map[K, V] {
  44. ret := &lMap[K, V]{}
  45. ret.store = map[K]V{}
  46. return ret
  47. }
  48. func (m *lMap[K, V]) Clear() {
  49. m.store = map[K]V{}
  50. }
  51. func (m *lMap[K, V]) Keys() []K {
  52. r := make([]K, len(m.store))
  53. i := 0
  54. for k := range m.store {
  55. r[i] = k
  56. i++
  57. }
  58. return r
  59. }
  60. func (m *lMap[K, V]) Empty() bool {
  61. return len(m.store) == 0
  62. }
  63. func (m *lMap[K, V]) Del(key K) {
  64. delete(m.store, key)
  65. }
  66. func (m *lMap[K, V]) Values() []V {
  67. r := make([]V, len(m.store))
  68. i := 0
  69. for _, v := range m.store {
  70. r[i] = v
  71. i++
  72. }
  73. return r
  74. }
  75. func (m *lMap[K, V]) Chan() chan V {
  76. ret := make(chan V)
  77. go func() {
  78. for _, v := range m.store {
  79. ret <- v
  80. }
  81. close(ret)
  82. }()
  83. return ret
  84. }
  85. func (m *lMap[K, V]) Has(k K) bool {
  86. _, ok := m.store[k]
  87. return ok
  88. }
  89. func (m *lMap[K, V]) Set(k K, v V) {
  90. m.store[k] = v
  91. }
  92. func (m *lMap[K, V]) Get(key K) V {
  93. v, ok := m.store[key]
  94. if !ok {
  95. panic(fmt.Sprintf("there is no such key '%v'", key))
  96. }
  97. return v
  98. }
  99. func (m *lMap[K, V]) Got(key K) (V, bool) {
  100. v, ok := m.store[key]
  101. return v, ok
  102. }
  103. func (m *lMap[K, V]) Size() int {
  104. return len(m.store)
  105. }
  106. func (m *lMap[K, V]) KeyChan() chan K {
  107. ret := make(chan K)
  108. go func() {
  109. for k := range m.store {
  110. ret <- k
  111. }
  112. close(ret)
  113. }()
  114. return ret
  115. }