From 8e29a6318f4258baf9fb245a9c4e141fbd609e37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 7 Mar 2023 17:34:02 +0800 Subject: [PATCH] =?UTF-8?q?map=E5=A2=9E=E5=8A=A0=E8=BF=AD=E4=BB=A3?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- abstruct.go | 2 ++ define.go | 11 +++++++++++ normal.go | 16 ++++++++++++++++ segment.go | 16 ++++++++++++++++ sync_normal.go | 15 +++++++++++++++ sync_segment.go | 11 +++++++++++ 6 files changed, 71 insertions(+) create mode 100644 define.go diff --git a/abstruct.go b/abstruct.go index 66fc96e..13f4ab3 100644 --- a/abstruct.go +++ b/abstruct.go @@ -34,4 +34,6 @@ type EasyMap interface { Exist(key interface{}) bool GetAll() map[interface{}]interface{} GetAllForMapKeyString() map[string]interface{} + // Iterator 对数据的迭代 + Iterator(IteratorFunc) } diff --git a/define.go b/define.go new file mode 100644 index 0000000..ca70d44 --- /dev/null +++ b/define.go @@ -0,0 +1,11 @@ +// Package easymap ... +// +// Description : easymap ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-03-07 17:27 +package easymap + +// IteratorFunc 迭代函数 +type IteratorFunc func(key interface{}, value interface{}) diff --git a/normal.go b/normal.go index 45dbe53..2829552 100644 --- a/normal.go +++ b/normal.go @@ -286,3 +286,19 @@ func (n *normal) GetAllForMapKeyString() map[string]interface{} { } return finalData } + +// Iterator ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:32 2023/3/7 +func (n *normal) Iterator(handleFunc IteratorFunc) { + if nil == handleFunc { + return + } + n.RLock() + defer n.RUnlock() + for key, val := range n.data { + handleFunc(key, val) + } +} diff --git a/segment.go b/segment.go index e1577cf..aa36b66 100644 --- a/segment.go +++ b/segment.go @@ -155,3 +155,19 @@ func (s *segment) GetAllForMapKeyString() map[string]interface{} { } return finalData } + +// Iterator ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:32 2023/3/7 +func (s *segment) Iterator(handleFunc IteratorFunc) { + if nil == handleFunc { + return + } + for i := 0; i < s.segment; i++ { + for k, v := range s.dataTable[i].GetAll() { + handleFunc(k, v) + } + } +} diff --git a/sync_normal.go b/sync_normal.go index c8e79f4..ce0662d 100644 --- a/sync_normal.go +++ b/sync_normal.go @@ -233,3 +233,18 @@ func (s *syncMap) GetAllForMapKeyString() map[string]interface{} { } return finalData } + +// Iterator 迭代数据 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:27 2023/3/7 +func (s *syncMap) Iterator(handleFunc IteratorFunc) { + if nil == handleFunc { + return + } + s.data.Range(func(key, value interface{}) bool { + handleFunc(key, value) + return true + }) +} diff --git a/sync_segment.go b/sync_segment.go index a6eade4..c8e3cc0 100644 --- a/sync_segment.go +++ b/sync_segment.go @@ -136,3 +136,14 @@ func (s *segmentSync) GetAllForMapKeyString() map[string]interface{} { } return finalData } + +func (s *segmentSync) Iterator(handleFunc IteratorFunc) { + if nil == handleFunc { + return + } + for i := 0; i < s.segment; i++ { + for k, v := range s.dataTable[i].GetAll() { + handleFunc(k, v) + } + } +} -- 2.36.6