github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/sets/set_fixed.go (about) 1 // Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. 2 3 package setutils 4 5 import ( 6 "github.com/TeaOSLab/EdgeNode/internal/zero" 7 "sync" 8 ) 9 10 // FixedSet 11 // TODO 解决已存在元素不能按顺序弹出的问题 12 type FixedSet struct { 13 maxSize int 14 locker sync.RWMutex 15 16 m map[any]zero.Zero 17 keys []any 18 } 19 20 func NewFixedSet(maxSize int) *FixedSet { 21 if maxSize <= 0 { 22 maxSize = 1024 23 } 24 return &FixedSet{ 25 maxSize: maxSize, 26 m: map[any]zero.Zero{}, 27 } 28 } 29 30 func (this *FixedSet) Push(item any) { 31 this.locker.Lock() 32 _, ok := this.m[item] 33 if !ok { 34 // 是否已满 35 if len(this.keys) == this.maxSize { 36 var firstKey = this.keys[0] 37 this.keys = this.keys[1:] 38 delete(this.m, firstKey) 39 } 40 41 this.m[item] = zero.New() 42 this.keys = append(this.keys, item) 43 } 44 this.locker.Unlock() 45 } 46 47 func (this *FixedSet) Has(item any) bool { 48 this.locker.RLock() 49 defer this.locker.RUnlock() 50 51 _, ok := this.m[item] 52 return ok 53 } 54 55 func (this *FixedSet) Size() int { 56 this.locker.RLock() 57 defer this.locker.RUnlock() 58 return len(this.keys) 59 } 60 61 func (this *FixedSet) Reset() { 62 this.locker.Lock() 63 this.m = map[any]zero.Zero{} 64 this.keys = nil 65 this.locker.Unlock() 66 }