Skip to content

Commit

Permalink
完善enumSet注释
Browse files Browse the repository at this point in the history
  • Loading branch information
lvyahui8 committed Dec 17, 2023
1 parent eabd84c commit dcdca92
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 16 additions & 1 deletion enum_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,35 @@ import (

// EnumSet 枚举set,一般在枚举非常多时使用
type EnumSet[E EnumDefinition] interface {
// Stringer 支持标准输出及格式化
fmt.Stringer
// Marshaler 支持json序列化
json.Marshaler
// Add 往set添加元素,添加成功则返回true,如果已经存在则返回false
Add(e E) bool
// AddRange 按照枚举的序数,连续添加一段枚举,返回实际添加的数量(排除已经存在的)
AddRange(begin, end E) int
// Remove 删除元素,删除成功则返回true,如果元素原本不存在则返回false
Remove(e E) bool
// RemoveRange 按照枚举的序数,连续删除一段枚举,返回实际删除的数量(排除原本不存在的)
RemoveRange(begin, end E) int
// IsEmpty set是否为空
IsEmpty() bool
// Clear 清理set
Clear()
// Len set内当前的枚举数量
Len() int
// Contains 是否包含指定的枚举,只要有1个不存在则返回false
Contains(enums ...E) bool
// ContainsAll 判断是否包含另外一个enumSet(子集关系)
ContainsAll(set EnumSet[E]) bool
// Equals 判断两个EnumSet是否相同
Equals(set EnumSet[E]) bool
Each(func(e E) bool)
// Each set迭代方法, f方法如果返回false,则中止迭代
Each(f func(e E) bool)
// Names 返回set中已有枚举的Name表示
Names() []string
// Clone 深拷贝一份set
Clone() EnumSet[E]
}

Expand Down
2 changes: 2 additions & 0 deletions enum_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ func TestEnumSet_Basic(t *testing.T) {
require.True(t, stmtSet.Len() == 0)
}

// BenchmarkUnsafeEnumSet_Contains 测试下来性能反而更差一些,原因应该是枚举数量太少,随着枚举数量的增加,map性能肯定会变差的,而EnumSet可以维持性能。
// java EnumSet,按照是否超过64个,划分了2个实现, 估计也是这个原因
func BenchmarkUnsafeEnumSet_Contains(b *testing.B) {
stmtSet := NewUnsafeEnumSet[Statement]()
stmtSet.Add(TypeSwitch)
Expand Down

0 comments on commit dcdca92

Please sign in to comment.