gset

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2025 License: MIT Imports: 3 Imported by: 1 module, 3 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

gset 包

简介

gset 包提供了线程安全的集合(Set)实现。

功能特性

  • 无重复元素:自动去重
  • 线程安全:所有操作都是线程安全的
  • 集合操作:并集、交集、差集
  • 成员检查:快速检查元素是否存在

安装

go get github.com/snail007/gmc/util/set

快速开始

package main

import (
    "fmt"
    "github.com/snail007/gmc/util/set"
)

func main() {
    set := gset.New()
    
    // 添加元素
    set.Add("apple")
    set.Add("banana")
    set.Add("apple") // 重复元素不会添加
    
    // 检查是否存在
    if set.Has("apple") {
        fmt.Println("Set contains apple")
    }
    
    // 获取大小
    fmt.Println("Size:", set.Len())
    
    // 遍历
    set.Range(func(item interface{}) bool {
        fmt.Println(item)
        return true
    })
    
    // 删除
    set.Remove("banana")
    
    // 转换为切片
    items := set.Items()
    fmt.Println(items)
}

相关链接

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Set

type Set struct {
	// contains filtered or unexported fields
}

Set each value in set is unique, and keep the sequence of Add() or Merge()

func New

func New() *Set

New returns a new *Set object

func (*Set) Add

func (s *Set) Add(value ...interface{})

Add adds a value to set, if value not exists in set s.

func (*Set) AddFront

func (s *Set) AddFront(value ...interface{})

AddFront adds a value to set, if value not exists in set s. The value will be stored the first in set s if value not exists.

func (*Set) CartesianProduct

func (s *Set) CartesianProduct(sets ...*Set) [][]interface{}

CartesianProduct → {(x, y) | ∀ x ∈ s1, ∀ y ∈ s2} For example:

		CartesianProduct(A, B), where A = {1, 2} and B = {7, 8}
       => {(1, 7), (1, 8), (2, 7), (2, 8)}

func (*Set) Clear

func (s *Set) Clear() *Set

Clear duplicates the set s.

func (*Set) Clone

func (s *Set) Clone() *Set

Clone duplicates the set s.

func (*Set) CloneAndClear

func (s *Set) CloneAndClear() *Set

CloneAndClear duplicates the set s.

func (*Set) Contains

func (s *Set) Contains(value interface{}) (exists bool)

Contains returns true if value exists in set s, otherwise false.

func (*Set) Delete

func (s *Set) Delete(value ...interface{})

Delete removes a value from set.

func (*Set) IndexOf

func (s *Set) IndexOf(k interface{}) int

IndexOf indicates the index of value in Map s, if not found returns -1.

idx start with 0.

func (*Set) Len

func (s *Set) Len() int

Len returns length of the set s.

func (*Set) Merge

func (s *Set) Merge(set *Set)

Merge merges set values to set s.

Kept the Add() sequence.

func (*Set) MergeSlice

func (s *Set) MergeSlice(data []interface{})

MergeSlice merges slice values to set s.

Kept the Add() sequence.

func (*Set) MergeStringSlice

func (s *Set) MergeStringSlice(data []string)

MergeStringSlice merges slice values to set s.

Kept the Add() sequence.

func (*Set) Pop

func (s *Set) Pop() (value interface{})

Pop returns last value of set s, and remove it.

func (*Set) Range

func (s *Set) Range(f func(value interface{}) bool)

Range ranges the value in set s, if function f return false, the range loop will break.

Range keeps the Add() sequence.

func (*Set) RangeFast

func (s *Set) RangeFast(f func(value interface{}) bool)

RangeFast ranges the value in set s, if function f return false, the range loop will break.

RangeFast do not create a snapshot for range, so you can not modify list s in range loop, indicate do not call Remove(), Add(), Merge() etc.

func (*Set) Shift

func (s *Set) Shift() (value interface{})

Shift returns first value of set s, and remove it.

func (*Set) String

func (s *Set) String() string

String returns string format of the Set.

func (*Set) ToSlice

func (s *Set) ToSlice() []interface{}

ToSlice convert set s to slice []interface{}.

Kept the Add() sequence.

func (*Set) ToStringSlice

func (s *Set) ToStringSlice() []string

ToStringSlice convert set s to slice []interface{}.

Kept the Add() sequence.

Jump to

Keyboard shortcuts

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