feature: added wrapper map

master 1.0.0
amorozov 2024-06-19 23:08:51 +03:00
parent 9212b24d14
commit a472faec29
5 changed files with 116 additions and 20 deletions

20
.gitignore vendored
View File

@ -1,20 +0,0 @@
/out/
*.iml
/.idea/
*.exe
/run/
*.exe
*.exe~
*.dll
*.so
*.dylib
*.test
*.out
go.work
go.work.sum
.env

8
go.mod
View File

@ -1,3 +1,11 @@
module git.tswf.io/incredible-go/incredible-go-core
go 1.22.0
//replace (
// "git.tswf.io/incredible-go/incredible-go-core" lateswhict => "C:\Soft\intellij\workspaces\golang\go-workspace\library\incredible-go\incredible-go-core"
//)
//
//require (
// "git.tswf.io/incredible-go/incredible-go-core" latest
//)

View File

@ -0,0 +1,7 @@
package wrapper_map
func NewWrapperMap[K comparable, V any]() *WrapperMap[K, V] {
return &WrapperMap[K, V]{
wrappedMap: make(map[K]V),
}
}

View File

@ -0,0 +1,96 @@
package wrapper_map
import "git.tswf.io/incredible-go/incredible-go-core/pkg/collections/array_list"
func (self *WrapperMap[K, V]) ContainsKey(key K) bool {
_, ok := self.wrappedMap[key]
return ok
}
func (self *WrapperMap[K, V]) Get(key K) *V {
val, ok := self.wrappedMap[key]
if ok {
return &val
}
return nil
}
func (self *WrapperMap[K, V]) GetOfDefault(key K, defaultValue V) V {
value, ok := self.wrappedMap[key]
if ok {
return value
}
return defaultValue
}
func (self *WrapperMap[K, V]) ComputeIfAbsent(key K, newValueSupplier func(K) V) V {
if !self.ContainsKey(key) {
newValue := newValueSupplier(key)
self.Put(key, newValue)
return newValue
}
return self.wrappedMap[key]
}
func (self *WrapperMap[K, V]) GetKeys() *array_list.ArrayList[K] {
result := array_list.NewArrayList[K]()
for k := range self.wrappedMap {
result.Add(k)
}
return result
}
func (self *WrapperMap[K, V]) Put(key K, value V) {
self.wrappedMap[key] = value
}
func (self *WrapperMap[K, V]) Clear() {
clear(self.wrappedMap)
}
func (self *WrapperMap[K, V]) Size() int {
return len(self.wrappedMap)
}
func (self *WrapperMap[K, V]) Remove(key K) *V {
if self.ContainsKey(key) {
existingValue := self.Get(key)
delete(self.wrappedMap, key)
return existingValue
}
return nil
}
func (self *WrapperMap[K, V]) Replace(action func(K, V) V) {
self.ForEachEntry(func(k K, v V) {
newValue := action(k, v)
self.wrappedMap[k] = newValue
})
}
func (self *WrapperMap[K, V]) ForEachEntry(action func(K, V)) {
for key := range self.wrappedMap {
value := self.wrappedMap[key]
action(key, value)
}
}
func (self *WrapperMap[K, V]) ForEachKey(action func(K)) {
self.ForEachEntry(func(k K, v V) {
action(k)
})
}
func (self *WrapperMap[K, V]) ForEachValue(action func(V)) {
self.ForEachEntry(func(k K, v V) {
action(v)
})
}

View File

@ -0,0 +1,5 @@
package wrapper_map
type WrapperMap[K comparable, V any] struct {
wrappedMap map[K]V
}