From a472faec2919a027692431c7f4dc9c0b925e2ca9 Mon Sep 17 00:00:00 2001 From: amorozov Date: Wed, 19 Jun 2024 23:08:51 +0300 Subject: [PATCH] feature: added wrapper map --- .gitignore | 20 ---- go.mod | 8 ++ pkg/maps/wrapper_map/wrapper_map_factories.go | 7 ++ pkg/maps/wrapper_map/wrapper_map_methods.go | 96 +++++++++++++++++++ pkg/maps/wrapper_map/wrapper_map_struct.go | 5 + 5 files changed, 116 insertions(+), 20 deletions(-) delete mode 100644 .gitignore create mode 100644 pkg/maps/wrapper_map/wrapper_map_factories.go create mode 100644 pkg/maps/wrapper_map/wrapper_map_methods.go create mode 100644 pkg/maps/wrapper_map/wrapper_map_struct.go diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 6b72c36..0000000 --- a/.gitignore +++ /dev/null @@ -1,20 +0,0 @@ -/out/ -*.iml -/.idea/ -*.exe -/run/ - -*.exe -*.exe~ -*.dll -*.so -*.dylib - -*.test - -*.out - -go.work -go.work.sum - -.env \ No newline at end of file diff --git a/go.mod b/go.mod index 5a5692f..a4daf85 100644 --- a/go.mod +++ b/go.mod @@ -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 +//) \ No newline at end of file diff --git a/pkg/maps/wrapper_map/wrapper_map_factories.go b/pkg/maps/wrapper_map/wrapper_map_factories.go new file mode 100644 index 0000000..c52d314 --- /dev/null +++ b/pkg/maps/wrapper_map/wrapper_map_factories.go @@ -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), + } +} diff --git a/pkg/maps/wrapper_map/wrapper_map_methods.go b/pkg/maps/wrapper_map/wrapper_map_methods.go new file mode 100644 index 0000000..aba40a9 --- /dev/null +++ b/pkg/maps/wrapper_map/wrapper_map_methods.go @@ -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) + }) +} diff --git a/pkg/maps/wrapper_map/wrapper_map_struct.go b/pkg/maps/wrapper_map/wrapper_map_struct.go new file mode 100644 index 0000000..8c02740 --- /dev/null +++ b/pkg/maps/wrapper_map/wrapper_map_struct.go @@ -0,0 +1,5 @@ +package wrapper_map + +type WrapperMap[K comparable, V any] struct { + wrappedMap map[K]V +}