GoDS (Go Data Structures)
27 分钟阅读
原文:https://pkg.go.dev/github.com/emirpasic/gods/v2#section-readme
收录该文档时间:
2024-12-07T11:00:08+08:00
Implementation of various data structures and algorithms in Go.
在 Go 中实现各种数据结构和算法。
Data Structures
Containers
All data structures implement the container interface with the following methods:
所有数据结构都实现了具有以下方法的容器接口(container interface):
|
|
Containers are either ordered or unordered. All ordered containers provide stateful iterators and some of them allow enumerable functions.
容器分为有序和无序两类。所有有序容器都提供状态迭代器,其中一些还支持可枚举函数。
Data | **Structure **结构 | Ordered 有序 | Iterator 迭代器 | Enumerable 可枚举 | Referenced by 引用方式 |
---|---|---|---|---|---|
Lists | |||||
ArrayList | yes | yes* | yes | index | |
SinglyLinkedList | yes | yes | yes | index | |
DoublyLinkedList | yes | yes* | yes | index | |
Sets | |||||
HashSet | no | no | no | index | |
TreeSet | yes | yes* | yes | index | |
LinkedHashSet | yes | yes* | yes | index | |
Stacks | |||||
LinkedListStack | yes | yes | no | index | |
ArrayStack | yes | yes* | no | index | |
Maps | |||||
HashMap | no | no | no | key | |
TreeMap | yes | yes* | yes | key | |
LinkedHashMap | yes | yes* | yes | key | |
HashBidiMap | no | no | no | key* | |
TreeBidiMap | yes | yes* | yes | key* | |
Trees | |||||
RedBlackTree | yes | yes* | no | key | |
AVLTree | yes | yes* | no | key | |
BTree | yes | yes* | no | key | |
BinaryHeap | yes | yes* | no | index | |
Queues | |||||
LinkedListQueue | yes | yes | no | index | |
ArrayQueue | yes | yes* | no | index | |
CircularBuffer | yes | yes* | no | index | |
PriorityQueue | yes | yes* | no | index | |
*reversible | *bidirectional |
Lists
A list is a data structure that stores values and may have repeated values.
列表是一种存储值的数据结构,并且可以包含重复值。
Implements Container interface.
实现了 Container 接口。
|
|
ArrayList
A list backed by a dynamic array that grows and shrinks implicitly.
基于动态数组实现的列表,可根据需要动态增长或缩小。
Implements List, ReverseIteratorWithIndex, EnumerableWithIndex, JSONSerializer and JSONDeserializer interfaces.
实现了 List、ReverseIteratorWithIndex、EnumerableWithIndex、JSONSerializer 和 JSONDeserializer 接口。
|
|
SinglyLinkedList
A list where each element points to the next element in the list.
基于单链表实现的列表,每个元素指向链表中的下一个元素。
Implements List, IteratorWithIndex, EnumerableWithIndex, JSONSerializer and JSONDeserializer interfaces.
实现了 List、IteratorWithIndex、EnumerableWithIndex、JSONSerializer 和 JSONDeserializer 接口
|
|
DoublyLinkedList
A list where each element points to the next and previous elements in the list.
基于双链表实现的列表,每个元素同时指向链表中的前一个和后一个元素。
Implements List, ReverseIteratorWithIndex, EnumerableWithIndex, JSONSerializer and JSONDeserializer interfaces.
实现了 List、ReverseIteratorWithIndex、EnumerableWithIndex、JSONSerializer 和 JSONDeserializer 接口。
|
|
Sets
A set is a data structure that can store elements and has no repeated values. It is a computer implementation of the mathematical concept of a finite set. Unlike most other collection types, rather than retrieving a specific element from a set, one typically tests an element for membership in a set. This structure is often used to ensure that no duplicates are present in a container.
集合 (Set) 是一种可以存储元素且没有重复值的数据结构。它是数学概念中有限集合的计算机实现。与大多数其他集合类型不同,集合并不是用于检索特定元素,而通常用于测试某个元素是否属于集合。此结构通常用于确保容器中没有重复的元素。
Set additionally allow set operations such as intersection, union, difference, etc.
Implements Container interface.
实现了 Container 接口。
|
|
HashSet
A set backed by a hash table (actually a Go’s map). It makes no guarantees as to the iteration order of the set.
基于哈希表(实际上是 Go 的 map)实现的集合。对集合的迭代顺序没有保证。
Implements Set, JSONSerializer and JSONDeserializer interfaces.
实现了 Set、JSONSerializer 和 JSONDeserializer 接口。
|
|
TreeSet
A set backed by a red-black tree to keep the elements ordered with respect to the comparator.
Implements Set, ReverseIteratorWithIndex, EnumerableWithIndex, JSONSerializer and JSONDeserializer interfaces.
实现了 Set、ReverseIteratorWithIndex、EnumerableWithIndex、JSONSerializer 和 JSONDeserializer 接口。
|
|
LinkedHashSet
A set that preserves insertion-order. Data structure is backed by a hash table to store values and doubly-linked list to store insertion ordering.
Implements Set, ReverseIteratorWithIndex, EnumerableWithIndex, JSONSerializer and JSONDeserializer interfaces.
实现了 Set、ReverseIteratorWithIndex、EnumerableWithIndex、JSONSerializer 和 JSONDeserializer接口。
|
|
Stacks
A stack that represents a last-in-first-out (LIFO) data structure. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack.
栈是一种后进先出(LIFO)的数据结构。提供了标准的 push 和 pop 操作,以及用于查看栈顶元素的 peek 方法。
Implements Container interface.
实现了Container(容器)接口。
|
|
LinkedListStack
A stack based on a linked list.
Implements Stack, IteratorWithIndex, JSONSerializer and JSONDeserializer interfaces.
实现了 Stack、IteratorWithIndex、JSONSerializer 和 JSONDeserializer 接口。
|
|
ArrayStack
A stack based on a array list.
Implements Stack, IteratorWithIndex, JSONSerializer and JSONDeserializer interfaces.
实现了 Stack、IteratorWithIndex、JSONSerializer 和 JSONDeserializer 接口。
|
|
Maps
A Map is a data structure that maps keys to values. A map cannot contain duplicate keys and each key can map to at most one value.
映射是一种数据结构,用于将键映射到值。映射中不能包含重复的键,每个键最多只能映射到一个值。
Implements Container interface.
实现了 Container 接口。
|
|
A BidiMap is an extension to the Map. A bidirectional map (BidiMap), also called a hash bag, is an associative data structure in which the key-value pairs form a one-to-one relation. This relation works in both directions by allow the value to also act as a key to key, e.g. a pair (a,b) thus provides a coupling between ‘a’ and ‘b’ so that ‘b’ can be found when ‘a’ is used as a key and ‘a’ can be found when ‘b’ is used as a key.
双向映射 (BidiMap) 是对映射 (Map) 的扩展。双向映射 (BidiMap),也被称为哈希袋 (hash bag),是一种关联数据结构,其中键值对形成一对一关系。这种关系支持双向操作,使得值也可以作为键来访问键。例如,一个键值对 (a, b)
提供了 ‘a’ 和 ‘b’ 之间的耦合,使得在使用 ‘a’ 作为键时可以找到 ‘b’,而使用 ‘b’ 作为键时也可以找到 ‘a’。
|
|
HashMap
A map based on hash tables. Keys are unordered.
基于哈希表实现的映射。键是无序的。
Implements Map, JSONSerializer and JSONDeserializer interfaces.
实现了 Map、JSONSerializer 和 JSONDeserializer 接口。
|
|
TreeMap
A map based on red-black tree. Keys are ordered with respect to the comparator.
Implements Map, ReverseIteratorWithIndex, EnumerableWithKey, JSONSerializer and JSONDeserializer interfaces.
实现了 Map、ReverseIteratorWithIndex、EnumerableWithKey、JSONSerializer 和 JSONDeserializer 接口。
|
|
LinkedHashMap
A map that preserves insertion-order. It is backed by a hash table to store values and doubly-linked list to store ordering.
一种保持插入顺序的映射。底层使用哈希表存储值,并通过双向链表维护顺序。
Implements Map, ReverseIteratorWithIndex, EnumerableWithKey, JSONSerializer and JSONDeserializer interfaces.
实现了 Map、ReverseIteratorWithIndex、EnumerableWithKey、JSONSerializer 和 JSONDeserializer 接口。
|
|
HashBidiMap
A map based on two hashmaps. Keys are unordered.
基于两个哈希表实现的映射。键是无序的。
Implements BidiMap, JSONSerializer and JSONDeserializer interfaces.
实现了 BidiMap、JSONSerializer 和 JSONDeserializer 接口。
|
|
TreeBidiMap
A map based on red-black tree. This map guarantees that the map will be in both ascending key and value order. Other than key and value ordering, the goal with this structure is to avoid duplication of elements (unlike in HashBidiMap), which can be significant if contained elements are large.
基于红黑树实现的双向映射。此映射保证键和值均按照升序排列。与 HashBidiMap 不同,此数据结构的目标是避免元素重复,尤其是在存储大对象时显得重要。
Implements BidiMap, ReverseIteratorWithIndex, EnumerableWithKey, JSONSerializer and JSONDeserializer interfaces.
实现了 BidiMap、ReverseIteratorWithIndex、EnumerableWithKey、JSONSerializer 和 JSONDeserializer 接口。
|
|
Trees
A tree is a widely used data data structure that simulates a hierarchical tree structure, with a root value and subtrees of children, represented as a set of linked nodes; thus no cyclic links.
树是一种广泛使用的数据结构,模拟了一个层次结构的树形结构,包含一个根节点和若干子树,由一组链接的节点组成;因此,树中不存在环。
Implements Container interface.
实现了 Container 接口。
|
|
RedBlackTree
A red–black tree is a binary search tree with an extra bit of data per node, its color, which can be either red or black. The extra bit of storage ensures an approximately balanced tree by constraining how nodes are colored from any path from the root to the leaf. Thus, it is a data structure which is a type of self-balancing binary search tree.
红黑树是一种二叉搜索树,每个节点存储额外的一个位表示颜色(红或黑)。这种额外的信息通过约束从根节点到叶子节点的路径上节点的颜色分布来保证树的平衡。红黑树是一种自平衡的二叉搜索树。
The balancing of the tree is not perfect but it is good enough to allow it to guarantee searching in O(log n) time, where n is the total number of elements in the tree. The insertion and deletion operations, along with the tree rearrangement and recoloring, are also performed in O(log n) time. Wikipedia
虽然红黑树的平衡性不是完美的,但它足够保证搜索操作的时间复杂度为 O(log n),其中 n 是树中元素的总数。插入和删除操作(包括重新排列和重新着色)也都在 O(log n) 时间内完成。 维基百科
实现了 Tree、ReverseIteratorWithKey、JSONSerializer 和 JSONDeserializer 接口。
Implements Tree, ReverseIteratorWithKey, JSONSerializer and JSONDeserializer interfaces.
|
|
Extending the red-black tree’s functionality has been demonstrated in the following example.
AVLTree
AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Lookup, insertion, and deletion all take O(log n) time in both the average and worst cases, where n is the number of nodes in the tree prior to the operation. Insertions and deletions may require the tree to be rebalanced by one or more tree rotations.
AVL 树是一种自平衡二叉搜索树。在 AVL 树中,任意节点的两个子树的高度差最多为 1;如果某一时刻高度差超过 1,将进行重新平衡以恢复此特性。查找、插入和删除操作在平均和最坏情况下的时间复杂度均为 O(log n),其中 n 是操作前的节点数。插入和删除可能需要通过一次或多次树旋转来重新平衡。
AVL trees are often compared with red–black trees because both support the same set of operations and take O(log n) time for the basic operations. For lookup-intensive applications, AVL trees are faster than red–black trees because they are more strictly balanced. Wikipedia
AVL 树通常与红黑树比较,因为两者支持相同的操作集,且基本操作的时间复杂度均为 O(log n)。对于查找密集型应用,AVL 树由于更严格的平衡性,比红黑树更快。维基百科
Implements Tree, ReverseIteratorWithKey, JSONSerializer and JSONDeserializer interfaces.
实现了 Tree、ReverseIteratorWithKey、JSONSerializer 和 JSONDeserializer 接口。
AVL tree with balance factors (green)
绿色部分为 AVL 树的平衡因子
|
|
BTree
B-tree is a self-balancing tree data structure that keeps data sorted and allows searches, sequential access, insertions, and deletions in logarithmic time. The B-tree is a generalization of a binary search tree in that a node can have more than two children.
B 树是一种自平衡树数据结构,用于保持数据有序,同时允许搜索、顺序访问、插入和删除操作在对数时间内完成。B 树是二叉搜索树的一种推广形式,节点可以有两个以上的子节点。
According to Knuth’s definition, a B-tree of order m is a tree which satisfies the following properties:
根据 Knuth 的定义,阶为 m 的 B 树满足以下特性:
Every node has at most m children.
- 每个节点最多有 m 个子节点。
Every non-leaf node (except root) has at least ⌈m/2⌉ children.
- 除根节点外,每个非叶子节点至少有 ⌈m/2⌉ 个子节点。
The root has at least two children if it is not a leaf node.
- 如果根节点不是叶子节点,则至少有两个子节点。
A non-leaf node with k children contains k−1 keys.
- 一个非叶子节点包含的键的数量比其子节点多一个。
All leaves appear in the same level
- 所有叶子节点在同一层级。
Each internal node’s keys act as separation values which divide its subtrees. For example, if an internal node has 3 child nodes (or subtrees) then it must have 2 keys: a1 and a2. All values in the leftmost subtree will be less than a1, all values in the middle subtree will be between a1 and a2, and all values in the rightmost subtree will be greater than a2.Wikipedia
每个内部节点的键用作分隔值,用以划分其子树。例如,如果一个内部节点有 3 个子节点,则必须有 2 个键:a1 和 a2。左子树中的所有值小于 a1,中间子树中的所有值在 a1 和 a2 之间,右子树中的所有值大于 a2。维基百科
Implements Tree, ReverseIteratorWithKey, JSONSerializer and JSONDeserializer interfaces.
实现了 Tree、ReverseIteratorWithKey、JSONSerializer 和 JSONDeserializer 接口。
|
|
BinaryHeap
A binary heap is a tree created using a binary tree. It can be seen as a binary tree with two additional constraints:
二叉堆是一种基于二叉树的数据结构。它可以被看作是一种具有以下两个附加约束的二叉树:
Shape property: 形状性质:
A binary heap is a complete binary tree; that is, all levels of the tree, except possibly the last one (deepest) are fully filled, and, if the last level of the tree is not complete, the nodes of that level are filled from left to right.
二叉堆是一棵完全二叉树;也就是说,除最后一层(最深层)外,所有层都被完全填充,如果最后一层不完整,则从左到右填充节点。
Heap property: 堆性质:
All nodes are either greater than or equal to or less than or equal to each of its children, according to a comparison predicate defined for the heap. Wikipedia
根据堆的定义,所有节点的值要么大于等于其子节点(最大堆),要么小于等于其子节点(最小堆)。维基百科
Implements Tree, ReverseIteratorWithIndex, JSONSerializer and JSONDeserializer interfaces.
实现了 Tree、ReverseIteratorWithIndex、JSONSerializer 和 JSONDeserializer 接口。
|
|
Queues
A queue that represents a first-in-first-out (FIFO) data structure. The usual enqueue and dequeue operations are provided, as well as a method to peek at the first item in the queue.
队列是一种先进先出 (FIFO) 的数据结构。队列提供了标准的 enqueue
(入队)和 dequeue
(出队)操作,同时支持检查队列首元素的方法。
Implements Container interface.
实现了 Container 接口。
|
|
LinkedListQueue
A queue based on a linked list.
Implements Queue, IteratorWithIndex, JSONSerializer and JSONDeserializer interfaces.
实现了 Queue、IteratorWithIndex、JSONSerializer 和 JSONDeserializer 接口。
|
|
ArrayQueue
A queue based on a array list.
Implements Queue, ReverseIteratorWithIndex, JSONSerializer and JSONDeserializer interfaces.
实现了 Queue、ReverseIteratorWithIndex、JSONSerializer 和 JSONDeserializer 接口。
|
|
CircularBuffer
A circular buffer, circular queue, cyclic buffer or ring buffer is a data structure that uses a single, fixed-size buffer as if it were connected end-to-end. This structure lends itself easily to buffering data streams.
循环缓冲区(Circular Buffer),也称为循环队列、环形缓冲区或环形队列,是一种数据结构,它使用单个固定大小的缓冲区,模拟首尾相连的效果。这种结构非常适合数据流的缓冲。
Implements Queue, ReverseIteratorWithIndex, JSONSerializer and JSONDeserializer interfaces.
实现了 Queue、ReverseIteratorWithIndex、JSONSerializer 和 JSONDeserializer 接口。
|
|
PriorityQueue
A priority queue is a special type of queue in which each element is associated with a priority value. And, elements are served on the basis of their priority. That is, higher priority elements are served first. However, if elements with the same priority occur, they are served according to their order in the queue.
优先队列是一种特殊的队列,其中每个元素都关联一个优先级值,元素按照优先级服务。也就是说,优先级较高的元素会先被处理。如果出现优先级相同的元素,则按照其在队列中的顺序处理。
Implements Queue, ReverseIteratorWithIndex, JSONSerializer and JSONDeserializer interfaces.
实现了 Queue、ReverseIteratorWithIndex、JSONSerializer 和 JSONDeserializer 接口。
|
|
Functions
Various helper functions used throughout the library.
库中使用的各种辅助函数。
Comparator
Some data structures (e.g. TreeMap, TreeSet) require a comparator function to automatically keep their elements sorted upon insertion. This comparator is necessary during the initalization.
一些数据结构(例如 TreeMap、TreeSet)需要比较器函数在插入时自动保持其元素的排序。在初始化时需要提供该比较器。
Comparator is defined as:
比较器定义如下:
Return values (int):
返回值 (int):
negative , if a < b
zero , if a == b
positive , if a > b
Comparator signature:
比较器的函数签名如下:
|
|
All common comparators for builtin types are included in the library:
库中包含了所有常见的内置类型的比较器:
|
|
Writing custom comparators is easy:
自定义比较器的编写非常简单:
|
|
Iterator
All ordered containers have stateful iterators. Typically an iterator is obtained by Iterator() function of an ordered container. Once obtained, iterator’s Next() function moves the iterator to the next element and returns true if there was a next element. If there was an element, then element’s can be obtained by iterator’s Value() function. Depending on the ordering type, it’s position can be obtained by iterator’s Index() or Key() functions. Some containers even provide reversible iterators, essentially the same, but provide another extra Prev() function that moves the iterator to the previous element and returns true if there was a previous element.
所有有序容器都有状态迭代器。通常通过有序容器的 Iterator() 函数获取迭代器。一旦获得迭代器,可以使用 Next() 函数移动到下一个元素,如果存在下一个元素,则返回 true。如果存在元素,可以通过迭代器的 Value() 函数获取元素。根据排序类型,可以通过迭代器的 Index() 或 Key() 函数获取位置。一些容器还提供可反转的迭代器,除了常规的功能外,还提供额外的 Prev() 函数,用于移动到上一个元素,并在存在上一个元素时返回 true。
Note: it is unsafe to remove elements from container while iterating.
注意:在迭代过程中从容器中删除元素是不安全的。
IteratorWithIndex
An iterator whose elements are referenced by an index.
一个迭代器,其元素通过索引引用。
Typical usage:
典型用法:
it := list.Iterator()
for it.Next() {
index, value := it.Index(), it.Value()
...
}
Other usages:
其他用法:
if it.First() {
firstIndex, firstValue := it.Index(), it.Value()
...
}
for it.Begin(); it.Next(); {
...
}
Seeking to a specific element:
定位到特定元素:
// Seek function, i.e. find element starting with "b"
// 定位函数,例如查找以 "b" 开头的元素
seek := func(index int, value interface{}) bool {
return strings.HasSuffix(value.(string), "b")
}
// Seek to the condition and continue traversal from that point (forward).
// assumes it.Begin() was called.
// 定位到满足条件的位置并从该点开始继续遍历(向前)。
// 假定 it.Begin() 已被调用。
for found := it.NextTo(seek); found; found = it.Next() {
index, value := it.Index(), it.Value()
...
}
IteratorWithKey
An iterator whose elements are referenced by a key.
一个迭代器,其元素通过键引用。
Typical usage:
典型用法:
it := tree.Iterator()
for it.Next() {
key, value := it.Key(), it.Value()
...
}
Other usages:
其他用法:
if it.First() {
firstKey, firstValue := it.Key(), it.Value()
...
}
for it.Begin(); it.Next(); {
...
}
Seeking to a specific element from the current iterator position:
定位到当前迭代器位置的特定元素:
// Seek function, i.e. find element starting with "b"
// 定位函数,例如查找以 "b" 开头的元素
seek := func(key interface{}, value interface{}) bool {
return strings.HasSuffix(value.(string), "b")
}
// Seek to the condition and continue traversal from that point (forward).
// assumes it.Begin() was called.
// 定位到满足条件的位置并从该点开始继续遍历(向前)。
// 假定 it.Begin() 已被调用。
for found := it.NextTo(seek); found; found = it.Next() {
key, value := it.Key(), it.Value()
...
}
ReverseIteratorWithIndex
An iterator whose elements are referenced by an index. Provides all functions as IteratorWithIndex, but can also be used for reverse iteration.
一个迭代器,其元素通过索引引用。提供与 IteratorWithIndex 相同的功能,但也支持反向迭代。
Typical usage of iteration in reverse:
典型的反向迭代用法:
it := list.Iterator()
for it.End(); it.Prev(); {
index, value := it.Index(), it.Value()
...
}
Other usages:
其他用法:
if it.Last() {
lastIndex, lastValue := it.Index(), it.Value()
...
}
Seeking to a specific element:
定位到特定元素:
// Seek function, i.e. find element starting with "b"
// 定位函数,例如查找以 "b" 开头的元素
seek := func(index int, value interface{}) bool {
return strings.HasSuffix(value.(string), "b")
}
// Seek to the condition and continue traversal from that point (in reverse).
// assumes it.End() was called.
// 定位到满足条件的位置并从该点开始继续遍历(反向)。
// 假定 it.End() 已被调用。
for found := it.PrevTo(seek); found; found = it.Prev() {
index, value := it.Index(), it.Value()
...
}
ReverseIteratorWithKey
An iterator whose elements are referenced by a key. Provides all functions as IteratorWithKey, but can also be used for reverse iteration.
一个迭代器,其元素通过键引用。提供与 IteratorWithKey 相同的功能,但也支持反向迭代。
Typical usage of iteration in reverse:
典型的反向迭代用法:
it := tree.Iterator()
for it.End(); it.Prev(); {
key, value := it.Key(), it.Value()
...
}
Other usages:
其他用法:
if it.Last() {
lastKey, lastValue := it.Key(), it.Value()
...
}
// Seek function, i.e. find element starting with "b"
// 定位函数,例如查找以 "b" 开头的元素
seek := func(key interface{}, value interface{}) bool {
return strings.HasSuffix(value.(string), "b")
}
// Seek to the condition and continue traversal from that point (in reverse).
// assumes it.End() was called.
// 定位到满足条件的位置并从该点开始继续遍历(反向)。
// 假定 it.End() 已被调用。
for found := it.PrevTo(seek); found; found = it.Prev() {
key, value := it.Key(), it.Value()
...
}
Enumerable
Enumerable functions for ordered containers that implement EnumerableWithIndex or EnumerableWithKey interfaces.
为实现了 EnumerableWithIndex 或 EnumerableWithKey 接口的有序容器提供的可枚举函数。
EnumerableWithIndex
Enumerable functions for ordered containers whose values can be fetched by an index.
为按索引获取值的有序容器提供的可枚举函数。Each
Calls the given function once for each element, passing that element’s index and value.
对容器中的每个元素调用一次给定的函数,并传递该元素的索引和值。
Each(func(index int, value interface{}))
Map
Invokes the given function once for each element and returns a container containing the values returned by the given function.
对容器中的每个元素调用一次给定的函数,并返回一个包含由该函数返回的值的容器。
Map(func(index int, value interface{}) interface{}) Container
Select
Returns a new container containing all elements for which the given function returns a true value.
返回一个包含所有满足给定函数返回值为 true
的元素的新容器。
Select(func(index int, value interface{}) bool) Container
Any
Passes each element of the container to the given function and returns true if the function ever returns true for any element.
将容器中的每个元素传递给给定的函数,如果函数对任何元素返回 true
,则返回 true
。
Any(func(index int, value interface{}) bool) bool
All
Passes each element of the container to the given function and returns true if the function returns true for all elements.
将容器中的每个元素传递给给定的函数,如果函数对所有元素返回 true
,则返回 true
。
All(func(index int, value interface{}) bool) bool
Find
Passes each element of the container to the given function and returns the first (index,value) for which the function is true or -1,nil otherwise if no element matches the criteria.
将容器中的每个元素传递给给定的函数,并返回第一个满足函数条件的 (index, value)
,如果没有匹配的元素,则返回 -1, nil
。
Find(func(index int, value interface{}) bool) (int, interface{})}
Example:
|
|
EnumerableWithKey
Enumerable functions for ordered containers whose values whose elements are key/value pairs.
为值是键/值对的有序容器提供的可枚举函数。
Each
Calls the given function once for each element, passing that element’s key and value.
对容器中的每个元素调用一次给定的函数,并传递该元素的键和值。
Each(func(key interface{}, value interface{}))
Map
Invokes the given function once for each element and returns a container containing the values returned by the given function as key/value pairs.
对容器中的每个元素调用一次给定的函数,并返回一个包含键/值对的新容器。
Map(func(key interface{}, value interface{}) (interface{}, interface{})) Container
Select
Returns a new container containing all elements for which the given function returns a true value.
返回一个包含所有满足给定函数返回值为 true
的元素的新容器。
Select(func(key interface{}, value interface{}) bool) Container
Any
Passes each element of the container to the given function and returns true if the function ever returns true for any element.
将容器中的每个元素传递给给定的函数,如果函数对任何元素返回 true
,则返回 true
。
Any(func(key interface{}, value interface{}) bool) bool
All
Passes each element of the container to the given function and returns true if the function returns true for all elements.
将容器中的每个元素传递给给定的函数,如果函数对所有元素返回 true
,则返回 true
。
All(func(key interface{}, value interface{}) bool) bool
Find
Passes each element of the container to the given function and returns the first (key,value) for which the function is true or nil,nil otherwise if no element matches the criteria.
传递容器中的每个元素到给定的函数,返回第一个满足函数条件的 (key, value)
,如果没有元素匹配,则返回 nil, nil
。
Find(func(key interface{}, value interface{}) bool) (interface{}, interface{})
Example:
|
|
Serialization
All data structures can be serialized (marshalled) and deserialized (unmarshalled). Currently, only JSON support is available.
所有数据结构都可以进行序列化(marshal)和反序列化(unmarshal)。目前,仅支持 JSON。
JSONSerializer
Outputs the container into its JSON representation.
将容器转换为其 JSON 表示形式。
Typical usage for key-value structures:
键值结构的典型用法:
|
|
Typical usage for value-only structures:
仅值结构的典型用法:
|
|
JSONDeserializer
Populates the container with elements from the input JSON representation.
从输入的 JSON 表示形式填充容器。
Typical usage for key-value structures:
键值结构的典型用法:
|
|
Typical usage for value-only structures:
仅值结构的典型用法:
|
|
Sort
Sort is a general purpose sort function.
Sort 是一个通用的排序函数。
Lists have an in-place Sort() function and all containers can return their sorted elements via containers.GetSortedValues() function.
列表提供了一个就地的 Sort() 方法,而所有容器都可以通过 containers.GetSortedValues() 方法返回其排序后的元素。
Internally these all use the utils.Sort() method:
内部这些方法都使用 utils.Sort() 方法:
|
|
Container
Container specific operations:
容器特定的操作:
// Returns sorted container''s elements with respect to the passed comparator.
// Does not affect the ordering of elements within the container.
// 根据传入的比较器返回容器的排序元素。
// 不影响容器内元素的顺序。
func GetSortedValues(container Container, comparator utils.Comparator) []interface{}
Usage:
|
|
附录 Appendix
动机 Motivation
Collections and data structures found in other languages: Java Collections, C++ Standard Template Library (STL) containers, Qt Containers, Ruby Enumerable etc.
该库的灵感来源于其他语言中的集合和数据结构,例如 Java Collections、C++ 的标准模板库 (STL) 容器、Qt Containers、Ruby Enumerable 等。
目标 Goals
快速算法:Fast algorithms:
- Based on decades of knowledge and experiences of other libraries mentioned above.
- 基于上述其他库数十年的知识和经验。
内存高效算法:Memory efficient algorithms:
- Avoiding to consume memory by using optimal algorithms and data structures for the given set of problems, e.g. red-black tree in case of TreeMap to avoid keeping redundant sorted array of keys in memory.
- 通过针对给定问题集使用最优算法和数据结构,避免消耗不必要的内存。例如,TreeMap 使用红黑树,避免在内存中存储多余的已排序键数组。
易于使用的库:Easy to use library:
- Well-structured library with minimalistic set of atomic operations from which more complex operations can be crafted.
- 结构良好,提供了一组最小的原子操作,可由此创建更复杂的操作。
Stable library:
- Only additions are permitted keeping the library backward compatible.
完善的文档和示例:Solid documentation and examples:
- Learning by example.
- 通过示例学习。
Production ready:
- Used in production.
- 可用于生产环境。
No dependencies:
- No external imports.
- 无外部导入。
There is often a tug of war between speed and memory when crafting algorithms. We choose to optimize for speed in most cases within reasonable limits on memory consumption.
在算法设计中,速度和内存之间往往存在权衡。本库在大多数情况下选择在合理的内存消耗范围内优化速度。
Thread safety is not a concern of this project, this should be handled at a higher level.
线程安全不是本项目的关注点,应该在更高层次处理。
测试和基准测试 Testing and Benchmarking
This takes a while, so test within sub-packages:
这可能需要一些时间,因此建议在子包内测试:
go test -run=NO_TEST -bench . -benchmem -benchtime 1s ./...
Contributing
Biggest contribution towards this library is to use it and give us feedback for further improvements and additions.
对本库最大的贡献是使用它并为进一步改进和扩展提供反馈。
For direct contributions, pull request into master branch or ask to become a contributor.
对于直接贡献,请提交 pull request 到主分支,或申请成为贡献者。
代码风格: Coding style:
|
|
许可证 License
This library is distributed under the BSD-style license found in the LICENSE file.
该库根据 LICENSE 文件中列出的 BSD 样式许可证分发。
赞助商 Sponsors
BrowserStack is a cloud-based cross-browser testing tool that enables developers to test their websites across various browsers on different operating systems and mobile devices, without requiring users to install virtual machines, devices or emulators.
BrowserStack 是一个基于云的跨浏览器测试工具,开发者可以在不同操作系统和移动设备上的各种浏览器中测试他们的网站,而无需安装虚拟机、设备或模拟器。