glist

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: 3

README

glist 包

简介

glist 包提供了线程安全的列表(List)容器,支持常见的列表操作。

功能特性

  • 线程安全:所有操作都是线程安全的
  • 动态大小:自动扩展
  • 丰富操作:添加、删除、插入、查找等
  • 批量操作:支持批量添加和合并

安装

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

快速开始

package main

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

func main() {
    list := glist.New()
    
    // 添加元素
    list.Add("apple", "banana", "cherry")
    
    // 获取元素
    fmt.Println(list.Get(0)) // apple
    
    // 设置元素
    list.Set(1, "blueberry")
    
    // 删除元素
    list.Remove(2)
    
    // 遍历
    list.Range(func(index int, value interface{}) bool {
        fmt.Printf("%d: %v\n", index, value)
        return true
    })
    
    // 长度
    fmt.Println("Length:", list.Len())
}

API 参考

  • New() *List:创建列表
  • Add(v ...interface{}):添加元素
  • AddFront(v interface{}):添加到头部
  • Get(idx int) interface{}:获取元素
  • Set(idx int, v interface{}):设置元素
  • Remove(idx int):删除元素
  • Len() int:获取长度
  • Range(func(int, interface{}) bool):遍历
  • Clear():清空
  • Clone() *List:克隆

相关链接

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type List

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

List then list container

func New

func New() *List

New returns a new *List object

func (*List) Add

func (s *List) Add(v ...interface{})

Add adds a value to end of list s.

func (*List) AddFront

func (s *List) AddFront(v interface{})

AddFront adds a value to first of list s.

func (*List) CartesianProduct

func (s *List) CartesianProduct(sets ...*List) [][]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 (*List) Clear

func (s *List) Clear()

Clear empty the list s.

func (*List) Clone

func (s *List) Clone() *List

Clone duplicates the list s.

func (*List) CloneAndClear

func (s *List) CloneAndClear() *List

CloneAndClear duplicates the list s and clear s.

func (*List) Contains

func (s *List) Contains(v interface{}) bool

Contains indicates if the list contains an element.

func (*List) Get

func (s *List) Get(idx int) interface{}

Get gets a value on idx in list s.

func (*List) IndexOf

func (s *List) IndexOf(v interface{}) int

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

idx start with 0.

func (*List) IsEmpty

func (s *List) IsEmpty() bool

IsEmpty indicates if the list is empty.

func (*List) Len

func (s *List) Len() int

Len returns length of the list s.

func (*List) Merge

func (s *List) Merge(l *List) *List

Merge merges a List to end of list s.

func (*List) MergeSlice

func (s *List) MergeSlice(arr []interface{}) *List

MergeSlice merge a array slice to end of list s.

func (*List) MergeStringSlice

func (s *List) MergeStringSlice(arr []string) *List

MergeStringSlice merge a array slice to end of list s.

func (*List) Pop

func (s *List) Pop() (v interface{})

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

func (*List) Range

func (s *List) Range(f func(index int, value interface{}) bool)

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

func (*List) RangeFast

func (s *List) RangeFast(f func(index int, value interface{}) bool)

RangeFast ranges the value in list 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 Delete(), Add(), Merge() etc.

func (*List) Remove

func (s *List) Remove(idx int)

Remove removes a value on idx in list s.

func (*List) Set

func (s *List) Set(idx int, v interface{})

Set sets a value on idx in list s.

func (*List) Shift

func (s *List) Shift() (v interface{})

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

func (*List) String

func (s *List) String() string

String returns string format of the list.

func (*List) Sub

func (s *List) Sub(start, end int) *List

Sub returns the sub list between two ranges With x the start and y the end, we are sending [x,y) sublist.

func (*List) ToSlice

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

ToSlice returns the list as an array.

func (*List) ToStringSlice

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

ToStringSlice returns the list as an array.

Jump to

Keyboard shortcuts

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