set

package
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 20, 2020 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrTypeCast = errors.New("failed to type cast")
)

Functions

This section is empty.

Types

type Set

type Set map[interface{}]struct{}

Set 定义集合

Example
package main

import (
	"fmt"

	"github.com/recallsong/go-utils/container/set"
	"github.com/recallsong/go-utils/container/slice/ints"
	"github.com/recallsong/go-utils/container/slice/strings"
	"github.com/recallsong/go-utils/conv"
)

func main() {
	s := set.Set{}
	// 向集合添加元素列表
	s.AddList(1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 9)
	// 向集合添加单个元素
	s.Add(10)
	// 排序并打印
	fmt.Println(ints.Ints(conv.Interfaces(s.ToList()).ToInts()).Sort())

	// 创建保存Persion结构体的集合
	type Persion struct {
		Name string
	}
	ps := set.Set{}
	ps.Add(Persion{Name: "Song"})
	ps.Add(Persion{Name: "Hello"})
	ps.Add(Persion{Name: "Song"})
	var plist []Persion
	// 将集合元素输出为[]Persion
	ps.Dump(&plist)
	fmt.Println(len(plist))

	// 集合运算
	s1 := set.Set{}
	s1.AddList("A", "B", "C")
	s2 := set.Set{}
	s2.AddList("A", "D")
	// 并集
	s3 := s1.Copy()
	s3.AddSet(s2)
	fmt.Println(strings.Strings(conv.Interfaces(s3.ToList()).ToStrings()).Sort())
	// 差集
	s3 = s1.Copy()
	s3.RemoveSet(s2)
	fmt.Println(strings.Strings(conv.Interfaces(s3.ToList()).ToStrings()).Sort())
	// 交集
	s3 = s1.Copy()
	s3.RetainSet(s2)
	fmt.Println(strings.Strings(conv.Interfaces(s3.ToList()).ToStrings()).Sort())

}
Output:

[1 2 3 4 5 6 7 8 9 10]
2
[A B C D]
[B C]
[A]

func (Set) Add

func (s Set) Add(elem interface{}) bool

Add 向集合添加元素,如果已存在,则返回false, 否则返回true

func (Set) AddList

func (s Set) AddList(elems ...interface{}) int

AddList 向集合中添加一组元素,自动过滤重复元素,返回实际添加的元素数量

func (Set) AddSet

func (s Set) AddSet(set Set)

AddSet 集合并集

func (Set) Clear

func (s Set) Clear()

Clear 清空集合元素

func (Set) Contains

func (s Set) Contains(elem interface{}) bool

Contains 判断集合是否包含该元素

func (Set) Copy

func (s Set) Copy() Set

Copy 对集合进行浅拷贝

func (Set) Dump

func (s Set) Dump(list interface{}) (err error)

Dump 将集合输出到list,失败则返回错误信息

func (Set) Equals

func (s Set) Equals(set interface{}) bool

Equals 对两个集合进行深度比较,返回是否相等

func (Set) Foreach

func (s Set) Foreach(f func(v interface{}) bool)

Foreach 遍历集合,如果f返回 false ,则终止遍历

func (Set) IsEmpty

func (s Set) IsEmpty() bool

IsEmpty 判断集合是否为空

func (Set) Remove

func (s Set) Remove(elem interface{}) bool

Remove 从集合中删除元素,如果不存在,则返回false, 否则返回true

func (Set) RemoveSet

func (s Set) RemoveSet(set Set)

removeSet 集合差集

func (Set) RetainSet

func (s Set) RetainSet(set Set)

retainAll 集合交集

func (Set) Size

func (s Set) Size() int

Size 返回集合的大小

func (Set) String

func (s Set) String() string

String 将集合输出为字符串

func (Set) ToList

func (s Set) ToList() []interface{}

ToList 将集合转换为[]interface{}

type SyncSetPair

type SyncSetPair struct {
	Set
	sync.RWMutex
}

SyncSetPair 带锁的Set类型

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL