Help us shape the future of Go! Take the 2026 Go Developer Survey

cyalgorithm

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatDiff

func FormatDiff(diff Diff) string

func FormatDiffs

func FormatDiffs(diffs []Diff) string

func FormatDiffsSummary

func FormatDiffsSummary(diffs []Diff) string

FormatDiffsSummary formats the difference summary Example return: "Added(2): [key1, key2], Deleted(1): [key3], Modified(3): [key4, key5, key6]" Returns "No changes" if there are no differences

func GraphSort

func GraphSort(names []string, depGraph map[string]map[string]struct{}) ([]string, error)

GraphSort 执行拓扑排序,根据依赖关系对元素进行排序。

参数:

  • names: 需要排序的元素名称列表
  • depGraph: 依赖关系图,key 为元素名称,value 为该元素的依赖集合

返回值:

  • []string: 排序后的元素列表(按依赖关系从无依赖到有依赖排列)
  • error: 如果检测到循环依赖,返回错误信息

用法:

names := []string{"a", "b", "c"}
deps := map[string]map[string]struct{}{
    "b": {"a": {}},           // b 依赖 a
    "c": {"a": {}, "b": {}},  // c 依赖 a 和 b
}
sorted, err := GraphSort(names, deps)
// sorted = ["a", "b", "c"]

局限:

  • 如果存在循环依赖,返回错误
  • 时间复杂度: O(n + m),其中 n 是元素数量,m 是依赖关系数量
  • 不修改输入的 names 列表,但会修改 depGraph(清空其内容)

func TravelMap

func TravelMap(m map[string]any, f func(k string, v any) error) error

TravelMap 递归遍历嵌套的 Map 结构,对每个键值对调用回调函数。

参数:

  • m: 需要遍历的 Map
  • f: 回调函数,接收键名和值

返回值:

  • error: 如果回调函数返回错误,立即返回该错误

用法:

data := map[string]any{
    "name": "John",
    "profile": map[string]any{
        "age": 30,
    },
}
TravelMap(data, func(k string, v any) error {
    fmt.Printf("Key: %s, Value: %v\n", k, v)
    return nil
})

局限:

  • 只支持 map[string]any 和 []any 类型的嵌套
  • 不支持其他类型的嵌套结构
  • 回调函数返回错误时会中断遍历
  • 不保证遍历顺序(Map 的遍历顺序是随机的)

func TravelMapEx

func TravelMapEx(m map[string]any, parentKey []any, f func(k []any, v any) error) error

TravelMapEx 递归遍历嵌套的 Map 结构,并提供完整的路径信息。

参数:

  • m: 需要遍历的 Map
  • parentKey: 父节点的路径(初次调用时传 nil)
  • f: 回调函数,接收完整路径和值

返回值:

  • error: 如果回调函数返回错误,立即返回该错误

用法:

data := map[string]any{
    "users": []any{
        map[string]any{"name": "John"},
    },
}
TravelMapEx(data, nil, func(keys []any, v any) error {
    path := ""
    for _, k := range keys {
        if s, ok := k.(string); ok {
            path += "." + s
        } else if i, ok := k.(int); ok {
            path += fmt.Sprintf("[%d]", i)
        }
    }
    fmt.Printf("Path: %s, Value: %v\n", path, v)
    return nil
})

局限:

  • 路径中的元素可以是 string(Map 键)或 int(数组索引)
  • 只支持 map[string]any 和 []any 类型的嵌套
  • 回调函数返回错误时会中断遍历
  • 不保证遍历顺序(Map 的遍历顺序是随机的)

func TraverseString

func TraverseString(s string, separatorChars string, callback CallbackFunc) error

TraverseString 对字符串进行分词,对每个单词调用回调函数。

参数:

  • s: 需要分词的字符串
  • separatorChars: 分隔符字符集(除了空格外的其他分隔符)
  • callback: 回调函数,对每个单词(包括空单词)调用

返回值:

  • error: 如果回调函数返回错误,立即返回该错误

用法:

text := "hello,world;test"
TraverseString(text, ",;", func(w *WordInfo) error {
    if w.Word != "" {
        fmt.Printf("Word: %s at pos %d\n", w.Word, w.Pos)
    }
    return nil
})

局限:

  • 回调函数返回错误时会中断遍历
  • 空格和 separatorChars 中的字符都被视为分隔符
  • 连续的分隔符会被合并为一个 Separator

WordInfo 字段说明:

  • Word: 单词内容(可能为空字符串)
  • Pos: 单词在原字符串中的起始位置
  • Separator: 单词后的分隔符
  • Index: 单词的序号

func WalkMapLeaves

func WalkMapLeaves(root map[string]any, f func(path string, v any) error) error

WalkMapLeaves 遍历 Map 的所有叶子节点(非容器类型的值)。

参数:

  • root: 需要遍历的根 Map
  • f: 回调函数,接收路径和叶子节点的值

返回值:

  • error: 如果回调函数返回错误,立即返回该错误

用法:

data := map[string]any{
    "user": map[string]any{
        "name": "John",
        "age":  30,
    },
    "items": []any{1, 2, 3},
}
WalkMapLeaves(data, func(path string, v any) error {
    fmt.Printf("Leaf - Path: %s, Value: %v\n", path, v)
    return nil
})
// Output:
// Leaf - Path: user.name, Value: John
// Leaf - Path: user.age, Value: 30
// Leaf - Path: items[0], Value: 1
// Leaf - Path: items[1], Value: 2
// Leaf - Path: items[2], Value: 3

局限:

  • 只遍历叶子节点(非 Map 和非 []any 的值)
  • 路径格式: "key.subkey[index]"
  • 回调函数返回错误时会中断遍历
  • 不保证遍历顺序(Map 的遍历顺序是随机的)

Types

type CallbackFunc

type CallbackFunc func(w *WordInfo) error

type Diff

type Diff struct {
	Key      string // 发生变更的键名
	OldValue any    // 旧值(nil 表示新增)
	NewValue any    // 新值(nil 表示删除)
}

func CompareMap

func CompareMap(a, b map[string]any) []Diff

CompareMap 比较两个 map 的差异并返回 Diff 切片 能够检测三种变更类型:修改、删除、新增

type WordInfo

type WordInfo struct {
	Word      string
	Pos       int
	Separator string
	Index     int
}

Jump to

Keyboard shortcuts

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