pair

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: May 17, 2025 License: Apache-2.0 Imports: 1 Imported by: 2 modules, 1 package

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FlattenPairs

func FlattenPairs[K any, V any](pairs []Pair[K, V]) (flatPairs []any)

FlattenPairs 需要传入一个[]Pair[K, V],数组可以为nil 如果pairs数组为nil,则返回的flatPairs数组也为nil

设pairs数组长度为n,保证返回的flatPairs数组长度为2 * n且满足:
	对于所有的 0 <= i < n
	flatPairs[i * 2] == pairs[i].Key
	flatPairs[i * 2 + 1] == pairs[i].Value

func SplitPairs

func SplitPairs[K any, V any](pairs []Pair[K, V]) (keys []K, values []V)

SplitPairs 需要传入一个[]Pair[K, V],数组可以为nil。 设pairs数组的长度为n,返回两个长度均为n的数组keys, values。 如果pairs数组是nil, 则返回的keys与values也均为nil。

Types

type Pair

type Pair[K any, V any] struct {
	Key   K
	Value V
}

func NewPair

func NewPair[K any, V any](
	key K,
	value V,
) Pair[K, V]

func NewPairs

func NewPairs[K any, V any](
	keys []K,
	values []V,
) ([]Pair[K, V], error)

NewPairs 需要传入两个长度相同并且均不为nil的数组 keys 和 values, 设keys长度为n,返回一个长度为n的pair数组。 保证:

返回的pair数组满足条件(设pair数组为p):
	对于所有的 0 <= i < n
	p[i].Key == keys[i] 并且 p[i].Value == values[i]

如果传入的keys或者values为nil,会返回error

如果传入的keys长度与values长度不同,会返回error

func PackPairs

func PackPairs[K any, V any](flatPairs []any) (pairs []Pair[K, V])

PackPairs 需要传入一个长度为2 * n的数组flatPairs,数组可以为nil。

函数将会返回一个长度为n的pairs数组,pairs满足
	对于所有的 0 <= i < n
	pairs[i].Key == flatPairs[i * 2]
	pairs[i].Value == flatPairs[i * 2 + 1]
如果flatPairs为nil,则返回的pairs也为nil

入参flatPairs需要满足以下条件:
	对于所有的 0 <= i < n
	flatPairs[i * 2] 的类型为 K
	flatPairs[i * 2 + 1] 的类型为 V
否则会panic

func (*Pair[K, V]) Split

func (pair *Pair[K, V]) Split() (K, V)

Split 方法将Key, Value作为返回参数传出。

func (*Pair[K, V]) String

func (pair *Pair[K, V]) String() string

Jump to

Keyboard shortcuts

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