map.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package ctn
  2. import "kumachan/standalone/util/backports/constraints"
  3. type Map[K any, V any] struct {
  4. AVL *AVL[Pair[K,V]]
  5. Cmp Compare[Pair[K,V]]
  6. }
  7. func MakeMap[K any, V any] (cmp Compare[K], entries ...Pair[K,V]) Map[K,V] {
  8. var m = Map[K,V] {
  9. AVL: nil,
  10. Cmp: func(left Pair[K,V], right Pair[K,V]) Ordering {
  11. return cmp(left.Key(), right.Key())
  12. },
  13. }
  14. for _, entry := range entries {
  15. m = m.Inserted(entry.Key(), entry.Value())
  16. }
  17. return m
  18. }
  19. func ToOrderedMap[K constraints.Ordered, V any] (h (map[K] V)) Map[K,V] {
  20. var m = MakeMutMap[K,V](func(left K, right K) Ordering {
  21. if left < right {
  22. return Smaller
  23. } else if left == right {
  24. return Equal
  25. } else {
  26. return Bigger
  27. }
  28. })
  29. for k, v := range h {
  30. m.Insert(k, v)
  31. }
  32. return m.Map()
  33. }
  34. func (Map[K,V]) Error() string {
  35. panic("dummy method")
  36. }
  37. func (m Map[K,V]) IsEmpty() bool {
  38. return m.AVL == nil
  39. }
  40. func (m Map[K,V]) Size() int {
  41. return int(m.AVL.GetSize())
  42. }
  43. func (m Map[K,V]) from(a *AVL[Pair[K,V]]) Map[K,V] {
  44. return Map[K,V] { AVL: a, Cmp: m.Cmp }
  45. }
  46. func (m Map[K,V]) ForEach(f func(K,V)) {
  47. m.AVL.Walk(func(entry Pair[K,V]) {
  48. f(entry.Key(), entry.Value())
  49. })
  50. }
  51. func (m Map[K,V]) Has(k K) bool {
  52. var _, found = m.Lookup(k)
  53. return found
  54. }
  55. func (m Map[K,V]) Lookup(k K) (V, bool) {
  56. var entry, exists = m.AVL.Lookup(MakePair(k, zero[V]()), m.Cmp)
  57. if exists {
  58. return entry.Value(), true
  59. } else {
  60. return zero[V](), false
  61. }
  62. }
  63. func (m Map[K,V]) Inserted(k K, v V) Map[K,V] {
  64. var arg = MakePair(k, v)
  65. var inserted, _ = m.AVL.Inserted(arg, m.Cmp)
  66. return m.from(inserted)
  67. }
  68. func (m Map[K,V]) Deleted(k K) (V, Map[K,V], bool) {
  69. var arg = MakePair(k, zero[V]())
  70. var entry, deleted, exists = m.AVL.Deleted(arg, m.Cmp)
  71. if exists {
  72. return entry.Value(), m.from(deleted), true
  73. } else {
  74. return zero[V](), m, false
  75. }
  76. }
  77. func (m Map[K,V]) MergedWith(another Map[K,V]) Map[K,V] {
  78. var draft = m
  79. another.ForEach(func(k K, v V) {
  80. draft = draft.Inserted(k, v)
  81. })
  82. return draft
  83. }
  84. func (m Map[K,V]) MutMap() MutMap[K,V] {
  85. var ptr = new(Map[K,V])
  86. *ptr = m
  87. return MutMap[K,V] { ptr }
  88. }
  89. type MutMap[K any, V any] struct { ptr *Map[K,V] }
  90. func MakeMutMap[K any, V any] (cmp Compare[K], entries ...Pair[K,V]) MutMap[K,V] {
  91. var m = MakeMap[K,V](cmp, entries...)
  92. return MutMap[K,V] { &m }
  93. }
  94. func (mm MutMap[K,V]) Map() Map[K,V] {
  95. return *(mm.ptr)
  96. }
  97. func (mm MutMap[K,V]) ForEach(f func(K,V)) {
  98. mm.ptr.ForEach(f)
  99. }
  100. func (mm MutMap[K,V]) Has(k K) bool {
  101. return mm.ptr.Has(k)
  102. }
  103. func (mm MutMap[K,V]) Lookup(k K) (V, bool) {
  104. return mm.ptr.Lookup(k)
  105. }
  106. func (mm MutMap[K,V]) Insert(k K, v V) {
  107. var inserted = mm.ptr.Inserted(k, v)
  108. *(mm.ptr) = inserted
  109. }
  110. func (mm MutMap[K,V]) Delete(k K) (V, bool) {
  111. var value, deleted, ok = mm.ptr.Deleted(k)
  112. *(mm.ptr) = deleted
  113. return value, ok
  114. }
  115. func (mm MutMap[K,V]) Size() int {
  116. return mm.ptr.Size()
  117. }
  118. func (mm MutMap[K,V]) Clone() MutMap[K,V] {
  119. return (*(mm.ptr)).MutMap()
  120. }
  121. func Keys[K any, V any] (f func(func(K,V))) ([] K) {
  122. var keys = make([] K, 0)
  123. f(func(k K, _ V) {
  124. keys = append(keys, k)
  125. })
  126. return keys
  127. }
  128. func Values[K any, V any] (f func(func(K,V))) ([] V) {
  129. var values = make([] V, 0)
  130. f(func(_ K, v V) {
  131. values = append(values, v)
  132. })
  133. return values
  134. }
  135. func Entries[K any, V any] (f func(func(K,V))) ([] Pair[K,V]) {
  136. var entries = make([] Pair[K,V], 0)
  137. f(func(k K, v V) {
  138. entries = append(entries, MakePair(k, v))
  139. })
  140. return entries
  141. }