From 594f8acf32850c40c0a315657551fb13ed3b3613 Mon Sep 17 00:00:00 2001 From: bandl <1658002533@qq.com> Date: Mon, 15 Nov 2021 23:57:39 +0800 Subject: [PATCH] feat(structure-hashx): add hashx range --- pkg/structure/define.go | 1 + pkg/structure/hashx/hashx.go | 37 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/pkg/structure/define.go b/pkg/structure/define.go index 375e941..e3078bd 100644 --- a/pkg/structure/define.go +++ b/pkg/structure/define.go @@ -65,4 +65,5 @@ type HashXInterface interface { Add(renewal int, key ...string) (int, []string, error) // 访问影响成功的结果 SetX(key string, val string) (bool, UpdateLength) // 不存在才插入 Length() int + Range(consur, count int, regex string) []string } diff --git a/pkg/structure/hashx/hashx.go b/pkg/structure/hashx/hashx.go index 8d6862b..0cb7f36 100644 --- a/pkg/structure/hashx/hashx.go +++ b/pkg/structure/hashx/hashx.go @@ -1,6 +1,8 @@ package hashx import ( + "regexp" + "gitee.com/wheat-os/wheatCache/pkg/errorx" "gitee.com/wheat-os/wheatCache/pkg/structure" ) @@ -118,3 +120,38 @@ func (h HashX) SetX(key string, val string) (bool, structure.UpdateLength) { func (h HashX) Length() int { return len(h) } + +func (h HashX) Range(consur, count int, regex string) []string { + + var reComp *regexp.Regexp + if regex == "" { + reComp = nil + } else { + reComp = regexp.MustCompile(regex) + } + + result := make([]string, 0) + for _, val := range h { + if consur > 0 { + consur-- + continue + } + + if count == 0 && count != -1 { + break + } + + s := val.ToString() + if reComp == nil { + count-- + result = append(result, s) + continue + } + if reComp.MatchString(s) { + count-- + result = append(result, s) + } + } + + return result +}