Categorygithub.com/min1324/set
repositorypackage
1.1.0
Repository: https://github.com/min1324/set.git
Documentation: pkg.go.dev

# README

Set

Build Status codecov Go Report Card GoDoc


定义

set是一个无序且不重复的元素集合。

位运算

操作符名称说明
&位与AND 运算,指定位清零的方式。
``位或
^异或XOR 运算,切换指定位上的值。
&^位与非AND NOT运算,异或某位置。
<< 左移
>>右移

集合操作

设定有两个集合 ST

  1. Intersect 交集P: 属于S并且属于T的元素为元素的集合:P = S∩T
  2. Union 并集P: 属于S或属于T的元素为元素的集合:P = S∪T
  3. Difference 差集P: 属于S并且不属于T的元素为元素的集合:P = S-T
  4. Complement 补集P: 属于S并且不属于T和不属于S并且属于T的元素为元素的集合:P = (S∩T')∪(S'∩T)

Usage

Install

go get github.com/min1324/set

Example

package main

import (
	"fmt"
    
	"github.com/min1324/set"
)

func main() {
	var s, p set.IntSet
	s.Adds(1, 2, 3, 4, 5, 6)
	p.Adds(4, 5, 6, 7, 8, 9)
	fmt.Printf("S:%v\n", s.String())
	fmt.Printf("P:%v\n", p.String())

	fmt.Printf("Equal:%v\n", set.Equal(&s, &p))
	fmt.Printf("Union:%v\n", set.Union(&s, &p))
	fmt.Printf("Intersect:%v\n", set.Intersect(&s, &p))
	fmt.Printf("Difference:%v\n", set.Difference(&s, &p))
	fmt.Printf("Complement:%v\n", set.Complement(&s, &p))
}
// the result is:
S:{1 2 3 4 5 6}
P:{4 5 6 7 8 9}
Equal:false
Union:{1 2 3 4 5 6 7 8 9}
Intersect:{4 5 6}
Difference:{1 2 3}
Complement:{1 2 3 7 8 9}