Showing preview only (717K chars total). Download the full file or copy to clipboard to get everything.
Repository: emirpasic/gods
Branch: master
Commit: 1d83d5ae39fb
Files: 142
Total size: 676.3 KB
Directory structure:
gitextract_653tot9y/
├── .circleci/
│ └── config.yml
├── .github/
│ ├── dependabot.yml
│ └── workflows/
│ └── codeql-analysis.yml
├── .gitignore
├── LICENSE
├── README.md
├── containers/
│ ├── containers.go
│ ├── containers_test.go
│ ├── enumerable.go
│ ├── iterator.go
│ └── serialization.go
├── examples/
│ ├── README.md
│ ├── arraylist/
│ │ └── arraylist.go
│ ├── arrayqueue/
│ │ └── arrayqqueue.go
│ ├── arraystack/
│ │ └── arraystack.go
│ ├── avltree/
│ │ └── avltree.go
│ ├── binaryheap/
│ │ └── binaryheap.go
│ ├── btree/
│ │ └── btree.go
│ ├── circularbuffer/
│ │ └── circularbuffer.go
│ ├── customcomparator/
│ │ └── customcomparator.go
│ ├── doublylinkedlist/
│ │ └── doublylinkedlist.go
│ ├── enumerablewithindex/
│ │ └── enumerablewithindex.go
│ ├── enumerablewithkey/
│ │ └── enumerablewithkey.go
│ ├── hashbidimap/
│ │ └── hashbidimap.go
│ ├── hashmap/
│ │ └── hashmap.go
│ ├── hashset/
│ │ └── hashset.go
│ ├── iteratorwithindex/
│ │ └── iteratorwithindex.go
│ ├── iteratorwithkey/
│ │ └── iteratorwithkey.go
│ ├── linkedhashmap/
│ │ └── linkedhashmap.go
│ ├── linkedhashset/
│ │ └── linkedhashset.go
│ ├── linkedlistqueue/
│ │ └── linkedlistqueue.go
│ ├── linkedliststack/
│ │ └── linkedliststack.go
│ ├── priorityqueue/
│ │ └── priorityqueue.go
│ ├── redblacktree/
│ │ └── redblacktree.go
│ ├── redblacktreeextended/
│ │ └── redblacktreeextended.go
│ ├── serialization/
│ │ └── serialization.go
│ ├── singlylinkedlist/
│ │ └── singlylinkedlist.go
│ ├── treebidimap/
│ │ └── treebidimap.go
│ ├── treemap/
│ │ └── treemap.go
│ └── treeset/
│ └── treeset.go
├── go.mod
├── go.sum
├── lists/
│ ├── arraylist/
│ │ ├── arraylist.go
│ │ ├── arraylist_test.go
│ │ ├── enumerable.go
│ │ ├── iterator.go
│ │ └── serialization.go
│ ├── doublylinkedlist/
│ │ ├── doublylinkedlist.go
│ │ ├── doublylinkedlist_test.go
│ │ ├── enumerable.go
│ │ ├── iterator.go
│ │ └── serialization.go
│ ├── lists.go
│ └── singlylinkedlist/
│ ├── enumerable.go
│ ├── iterator.go
│ ├── serialization.go
│ ├── singlylinkedlist.go
│ └── singlylinkedlist_test.go
├── maps/
│ ├── hashbidimap/
│ │ ├── hashbidimap.go
│ │ ├── hashbidimap_test.go
│ │ └── serialization.go
│ ├── hashmap/
│ │ ├── hashmap.go
│ │ ├── hashmap_test.go
│ │ └── serialization.go
│ ├── linkedhashmap/
│ │ ├── enumerable.go
│ │ ├── iterator.go
│ │ ├── linkedhashmap.go
│ │ ├── linkedhashmap_test.go
│ │ └── serialization.go
│ ├── maps.go
│ ├── treebidimap/
│ │ ├── enumerable.go
│ │ ├── iterator.go
│ │ ├── serialization.go
│ │ ├── treebidimap.go
│ │ └── treebidimap_test.go
│ └── treemap/
│ ├── enumerable.go
│ ├── iterator.go
│ ├── serialization.go
│ ├── treemap.go
│ └── treemap_test.go
├── queues/
│ ├── arrayqueue/
│ │ ├── arrayqueue.go
│ │ ├── arrayqueue_test.go
│ │ ├── iterator.go
│ │ └── serialization.go
│ ├── circularbuffer/
│ │ ├── circularbuffer.go
│ │ ├── circularbuffer_test.go
│ │ ├── iterator.go
│ │ └── serialization.go
│ ├── linkedlistqueue/
│ │ ├── iterator.go
│ │ ├── linkedlistqueue.go
│ │ ├── linkedlistqueue_test.go
│ │ └── serialization.go
│ ├── priorityqueue/
│ │ ├── iterator.go
│ │ ├── priorityqueue.go
│ │ ├── priorityqueue_test.go
│ │ └── serialization.go
│ └── queues.go
├── sets/
│ ├── hashset/
│ │ ├── hashset.go
│ │ ├── hashset_test.go
│ │ └── serialization.go
│ ├── linkedhashset/
│ │ ├── enumerable.go
│ │ ├── iterator.go
│ │ ├── linkedhashset.go
│ │ ├── linkedhashset_test.go
│ │ └── serialization.go
│ ├── sets.go
│ └── treeset/
│ ├── enumerable.go
│ ├── iterator.go
│ ├── serialization.go
│ ├── treeset.go
│ └── treeset_test.go
├── stacks/
│ ├── arraystack/
│ │ ├── arraystack.go
│ │ ├── arraystack_test.go
│ │ ├── iterator.go
│ │ └── serialization.go
│ ├── linkedliststack/
│ │ ├── iterator.go
│ │ ├── linkedliststack.go
│ │ ├── linkedliststack_test.go
│ │ └── serialization.go
│ └── stacks.go
├── testutils/
│ └── testutils.go
├── trees/
│ ├── avltree/
│ │ ├── avltree.go
│ │ ├── avltree_test.go
│ │ ├── iterator.go
│ │ └── serialization.go
│ ├── binaryheap/
│ │ ├── binaryheap.go
│ │ ├── binaryheap_test.go
│ │ ├── iterator.go
│ │ └── serialization.go
│ ├── btree/
│ │ ├── btree.go
│ │ ├── btree_test.go
│ │ ├── iterator.go
│ │ └── serialization.go
│ ├── redblacktree/
│ │ ├── iterator.go
│ │ ├── redblacktree.go
│ │ ├── redblacktree_test.go
│ │ └── serialization.go
│ └── trees.go
└── utils/
├── comparator.go
├── comparator_test.go
├── utils.go
└── utils_test.go
================================================
FILE CONTENTS
================================================
================================================
FILE: .circleci/config.yml
================================================
version: 2.1
jobs:
test:
parameters:
version:
type: string
default: "1.21"
docker:
- image: cimg/go:<<parameters.version>>
environment:
TEST_RESULTS: /tmp/test-results
working_directory: ~/gods
steps:
- run:
name: Print Go version (go version)
command: |
go version
- checkout
- run:
name: Run tests
command: |
mkdir -p $TEST_RESULTS
go install gotest.tools/gotestsum@latest
go test -v ./... | go tool test2json > $TEST_RESULTS/test2json-output.json
gotestsum --junitfile $TEST_RESULTS/gotestsum-report.xml
- run:
name: Calculate test coverage
command: |
mkdir -p $TEST_RESULTS
go test -coverprofile=coverage.out ./... > /dev/null
go test -race -coverprofile=coverage.txt -covermode=atomic ./... > /dev/null
go tool cover -html=coverage.out -o coverage.html
mv coverage.html $TEST_RESULTS
- run:
name: Upload test coverage
command: |
bash <(curl -s https://codecov.io/bash)
- run:
name: Lint (golint)
command: |
go install golang.org/x/lint/golint@latest
golint -set_exit_status ./...
- run:
name: Enforce formatted code (go fmt)
command: |
! go fmt ./... 2>&1 | read
- run:
name: Examine and report suspicious constructs (go vet)
command: |
go vet -v ./...
- run:
name: Calculate cyclomatic complexity (gocyclo)
command: |
go install github.com/fzipp/gocyclo/cmd/gocyclo@latest
gocyclo -avg -over 15 ../gods
- run:
name: Check for unchecked errors (errcheck)
command: |
go install github.com/kisielk/errcheck@latest
errcheck ./...
- store_artifacts:
path: /tmp/test-results
destination: raw-test-output
- store_test_results:
path: /tmp/test-results
workflows:
test:
jobs:
- test:
matrix:
parameters:
# To test with and without generics (versions prior to 1.18)
version: [ "1.21" ]
================================================
FILE: .github/dependabot.yml
================================================
# Ref: https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
version: 2
updates:
- package-ecosystem: "gomod"
directory: "/"
schedule:
interval: "daily"
================================================
FILE: .github/workflows/codeql-analysis.yml
================================================
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"
on:
push:
branches: [ master ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ master ]
schedule:
- cron: '30 4 * * 5'
jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write
strategy:
fail-fast: false
matrix:
language: [ 'go' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
# Learn more about CodeQL language support at https://git.io/codeql-language-support
steps:
- name: Checkout repository
uses: actions/checkout@v3
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2
# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language
#- run: |
# make bootstrap
# make release
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
================================================
FILE: .gitignore
================================================
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
# Folders
_obj
_test
# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
*.exe
*.test
*.prof
.idea
================================================
FILE: LICENSE
================================================
Copyright (c) 2015, Emir Pasic
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-------------------------------------------------------------------------------
AVL Tree:
Copyright (c) 2017 Benjamin Scher Purcell <benjapurcell@gmail.com>
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
================================================
FILE: README.md
================================================
[](https://godoc.org/github.com/emirpasic/gods)
[](https://circleci.com/gh/emirpasic/gods?branch=master)
[](https://goreportcard.com/report/github.com/emirpasic/gods)
[](https://codecov.io/gh/emirpasic/gods)
[](https://sourcegraph.com/github.com/emirpasic/gods?badge)
[](https://github.com/emirpasic/gods/releases)
[](https://sonarcloud.io/summary/new_code?id=gods)
[](https://github.com/emirpasic/gods/blob/master/LICENSE)
# GoDS (Go Data Structures)
Implementation of various data structures and algorithms in Go.
## Data Structures
- [Containers](#containers)
- [Lists](#lists)
- [ArrayList](#arraylist)
- [SinglyLinkedList](#singlylinkedlist)
- [DoublyLinkedList](#doublylinkedlist)
- [Sets](#sets)
- [HashSet](#hashset)
- [TreeSet](#treeset)
- [LinkedHashSet](#linkedhashset)
- [Stacks](#stacks)
- [LinkedListStack](#linkedliststack)
- [ArrayStack](#arraystack)
- [Maps](#maps)
- [HashMap](#hashmap)
- [TreeMap](#treemap)
- [LinkedHashMap](#linkedhashmap)
- [HashBidiMap](#hashbidimap)
- [TreeBidiMap](#treebidimap)
- [Trees](#trees)
- [RedBlackTree](#redblacktree)
- [AVLTree](#avltree)
- [BTree](#btree)
- [BinaryHeap](#binaryheap)
- [Queues](#queues)
- [LinkedListQueue](#linkedlistqueue)
- [ArrayQueue](#arrayqueue)
- [CircularBuffer](#circularbuffer)
- [PriorityQueue](#priorityqueue)
- [Functions](#functions)
- [Comparator](#comparator)
- [Iterator](#iterator)
- [IteratorWithIndex](#iteratorwithindex)
- [IteratorWithKey](#iteratorwithkey)
- [ReverseIteratorWithIndex](#reverseiteratorwithindex)
- [ReverseIteratorWithKey](#reverseiteratorwithkey)
- [Enumerable](#enumerable)
- [EnumerableWithIndex](#enumerablewithindex)
- [EnumerableWithKey](#enumerablewithkey)
- [Serialization](#serialization)
- [JSONSerializer](#jsonserializer)
- [JSONDeserializer](#jsondeserializer)
- [Sort](#sort)
- [Container](#container)
- [Appendix](#appendix)
## Containers
All data structures implement the container interface with the following methods:
```go
type Container interface {
Empty() bool
Size() int
Clear()
Values() []interface{}
String() string
}
```
Containers are either ordered or unordered. All ordered containers provide [stateful iterators](#iterator) and some of them allow [enumerable functions](#enumerable).
| **Data** | **Structure** | **Ordered** | **[Iterator](#iterator)** | **[Enumerable](#enumerable)** | **Referenced by** |
| :--- |:--------------------------------------| :---: | :---: | :---: | :---: |
| [Lists](#lists) |
| | [ArrayList](#arraylist) | yes | yes* | yes | index |
| | [SinglyLinkedList](#singlylinkedlist) | yes | yes | yes | index |
| | [DoublyLinkedList](#doublylinkedlist) | yes | yes* | yes | index |
| [Sets](#sets) |
| | [HashSet](#hashset) | no | no | no | index |
| | [TreeSet](#treeset) | yes | yes* | yes | index |
| | [LinkedHashSet](#linkedhashset) | yes | yes* | yes | index |
| [Stacks](#stacks) |
| | [LinkedListStack](#linkedliststack) | yes | yes | no | index |
| | [ArrayStack](#arraystack) | yes | yes* | no | index |
| [Maps](#maps) |
| | [HashMap](#hashmap) | no | no | no | key |
| | [TreeMap](#treemap) | yes | yes* | yes | key |
| | [LinkedHashMap](#linkedhashmap) | yes | yes* | yes | key |
| | [HashBidiMap](#hashbidimap) | no | no | no | key* |
| | [TreeBidiMap](#treebidimap) | yes | yes* | yes | key* |
| [Trees](#trees) |
| | [RedBlackTree](#redblacktree) | yes | yes* | no | key |
| | [AVLTree](#avltree) | yes | yes* | no | key |
| | [BTree](#btree) | yes | yes* | no | key |
| | [BinaryHeap](#binaryheap) | yes | yes* | no | index |
| [Queues](#queues) |
| | [LinkedListQueue](#linkedlistqueue) | yes | yes | no | index |
| | [ArrayQueue](#arrayqueue) | yes | yes* | no | index |
| | [CircularBuffer](#circularbuffer) | yes | yes* | no | index |
| | [PriorityQueue](#priorityqueue) | yes | yes* | no | index |
| | | | <sub><sup>*reversible</sup></sub> | | <sub><sup>*bidirectional</sup></sub> |
### Lists
A list is a data structure that stores values and may have repeated values.
Implements [Container](#containers) interface.
```go
type List interface {
Get(index int) (interface{}, bool)
Remove(index int)
Add(values ...interface{})
Contains(values ...interface{}) bool
Sort(comparator utils.Comparator)
Swap(index1, index2 int)
Insert(index int, values ...interface{})
Set(index int, value interface{})
containers.Container
// Empty() bool
// Size() int
// Clear()
// Values() []interface{}
// String() string
}
```
#### ArrayList
A [list](#lists) backed by a dynamic array that grows and shrinks implicitly.
Implements [List](#lists), [ReverseIteratorWithIndex](#reverseiteratorwithindex), [EnumerableWithIndex](#enumerablewithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
```go
package main
import (
"github.com/emirpasic/gods/lists/arraylist"
"github.com/emirpasic/gods/utils"
)
func main() {
list := arraylist.New()
list.Add("a") // ["a"]
list.Add("c", "b") // ["a","c","b"]
list.Sort(utils.StringComparator) // ["a","b","c"]
_, _ = list.Get(0) // "a",true
_, _ = list.Get(100) // nil,false
_ = list.Contains("a", "b", "c") // true
_ = list.Contains("a", "b", "c", "d") // false
list.Swap(0, 1) // ["b","a",c"]
list.Remove(2) // ["b","a"]
list.Remove(1) // ["b"]
list.Remove(0) // []
list.Remove(0) // [] (ignored)
_ = list.Empty() // true
_ = list.Size() // 0
list.Add("a") // ["a"]
list.Clear() // []
list.Insert(0, "b") // ["b"]
list.Insert(0, "a") // ["a","b"]
}
```
#### SinglyLinkedList
A [list](#lists) where each element points to the next element in the list.
Implements [List](#lists), [IteratorWithIndex](#iteratorwithindex), [EnumerableWithIndex](#enumerablewithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
```go
package main
import (
sll "github.com/emirpasic/gods/lists/singlylinkedlist"
"github.com/emirpasic/gods/utils"
)
func main() {
list := sll.New()
list.Add("a") // ["a"]
list.Add("c", "b") // ["a","c","b"]
list.Sort(utils.StringComparator) // ["a","b","c"]
_, _ = list.Get(0) // "a",true
_, _ = list.Get(100) // nil,false
_ = list.Contains("a", "b", "c") // true
_ = list.Contains("a", "b", "c", "d") // false
list.Swap(0, 1) // ["b","a",c"]
list.Remove(2) // ["b","a"]
list.Remove(1) // ["b"]
list.Remove(0) // []
list.Remove(0) // [] (ignored)
_ = list.Empty() // true
_ = list.Size() // 0
list.Add("a") // ["a"]
list.Clear() // []
list.Insert(0, "b") // ["b"]
list.Insert(0, "a") // ["a","b"]
}
```
#### DoublyLinkedList
A [list](#lists) where each element points to the next and previous elements in the list.
Implements [List](#lists), [ReverseIteratorWithIndex](#reverseiteratorwithindex), [EnumerableWithIndex](#enumerablewithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
```go
package main
import (
dll "github.com/emirpasic/gods/lists/doublylinkedlist"
"github.com/emirpasic/gods/utils"
)
func main() {
list := dll.New()
list.Add("a") // ["a"]
list.Add("c", "b") // ["a","c","b"]
list.Sort(utils.StringComparator) // ["a","b","c"]
_, _ = list.Get(0) // "a",true
_, _ = list.Get(100) // nil,false
_ = list.Contains("a", "b", "c") // true
_ = list.Contains("a", "b", "c", "d") // false
list.Swap(0, 1) // ["b","a",c"]
list.Remove(2) // ["b","a"]
list.Remove(1) // ["b"]
list.Remove(0) // []
list.Remove(0) // [] (ignored)
_ = list.Empty() // true
_ = list.Size() // 0
list.Add("a") // ["a"]
list.Clear() // []
list.Insert(0, "b") // ["b"]
list.Insert(0, "a") // ["a","b"]
}
```
### 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 additionally allow set operations such as [intersection](https://en.wikipedia.org/wiki/Intersection_(set_theory)), [union](https://en.wikipedia.org/wiki/Union_(set_theory)), [difference](https://proofwiki.org/wiki/Definition:Set_Difference), etc.
Implements [Container](#containers) interface.
```go
type Set interface {
Add(elements ...interface{})
Remove(elements ...interface{})
Contains(elements ...interface{}) bool
// Intersection(another *Set) *Set
// Union(another *Set) *Set
// Difference(another *Set) *Set
containers.Container
// Empty() bool
// Size() int
// Clear()
// Values() []interface{}
// String() string
}
```
#### HashSet
A [set](#sets) backed by a hash table (actually a Go's map). It makes no guarantees as to the iteration order of the set.
Implements [Set](#sets), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
```go
package main
import "github.com/emirpasic/gods/sets/hashset"
func main() {
set := hashset.New() // empty
set.Add(1) // 1
set.Add(2, 2, 3, 4, 5) // 3, 1, 2, 4, 5 (random order, duplicates ignored)
set.Remove(4) // 5, 3, 2, 1 (random order)
set.Remove(2, 3) // 1, 5 (random order)
set.Contains(1) // true
set.Contains(1, 5) // true
set.Contains(1, 6) // false
_ = set.Values() // []int{5,1} (random order)
set.Clear() // empty
set.Empty() // true
set.Size() // 0
}
```
#### TreeSet
A [set](#sets) backed by a [red-black tree](#redblacktree) to keep the elements ordered with respect to the [comparator](#comparator).
Implements [Set](#sets), [ReverseIteratorWithIndex](#reverseiteratorwithindex), [EnumerableWithIndex](#enumerablewithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
```go
package main
import "github.com/emirpasic/gods/sets/treeset"
func main() {
set := treeset.NewWithIntComparator() // empty (keys are of type int)
set.Add(1) // 1
set.Add(2, 2, 3, 4, 5) // 1, 2, 3, 4, 5 (in order, duplicates ignored)
set.Remove(4) // 1, 2, 3, 5 (in order)
set.Remove(2, 3) // 1, 5 (in order)
set.Contains(1) // true
set.Contains(1, 5) // true
set.Contains(1, 6) // false
_ = set.Values() // []int{1,5} (in order)
set.Clear() // empty
set.Empty() // true
set.Size() // 0
}
```
#### LinkedHashSet
A [set](#sets) that preserves insertion-order. Data structure is backed by a hash table to store values and [doubly-linked list](#doublylinkedlist) to store insertion ordering.
Implements [Set](#sets), [ReverseIteratorWithIndex](#reverseiteratorwithindex), [EnumerableWithIndex](#enumerablewithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
```go
package main
import "github.com/emirpasic/gods/sets/linkedhashset"
func main() {
set := linkedhashset.New() // empty
set.Add(5) // 5
set.Add(4, 4, 3, 2, 1) // 5, 4, 3, 2, 1 (in insertion-order, duplicates ignored)
set.Add(4) // 5, 4, 3, 2, 1 (duplicates ignored, insertion-order unchanged)
set.Remove(4) // 5, 3, 2, 1 (in insertion-order)
set.Remove(2, 3) // 5, 1 (in insertion-order)
set.Contains(1) // true
set.Contains(1, 5) // true
set.Contains(1, 6) // false
_ = set.Values() // []int{5, 1} (in insertion-order)
set.Clear() // empty
set.Empty() // true
set.Size() // 0
}
```
### 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.
Implements [Container](#containers) interface.
```go
type Stack interface {
Push(value interface{})
Pop() (value interface{}, ok bool)
Peek() (value interface{}, ok bool)
containers.Container
// Empty() bool
// Size() int
// Clear()
// Values() []interface{}
// String() string
}
```
#### LinkedListStack
A [stack](#stacks) based on a [linked list](#singlylinkedlist).
Implements [Stack](#stacks), [IteratorWithIndex](#iteratorwithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
```go
package main
import lls "github.com/emirpasic/gods/stacks/linkedliststack"
func main() {
stack := lls.New() // empty
stack.Push(1) // 1
stack.Push(2) // 1, 2
stack.Values() // 2, 1 (LIFO order)
_, _ = stack.Peek() // 2,true
_, _ = stack.Pop() // 2, true
_, _ = stack.Pop() // 1, true
_, _ = stack.Pop() // nil, false (nothing to pop)
stack.Push(1) // 1
stack.Clear() // empty
stack.Empty() // true
stack.Size() // 0
}
```
#### ArrayStack
A [stack](#stacks) based on a [array list](#arraylist).
Implements [Stack](#stacks), [IteratorWithIndex](#iteratorwithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
```go
package main
import "github.com/emirpasic/gods/stacks/arraystack"
func main() {
stack := arraystack.New() // empty
stack.Push(1) // 1
stack.Push(2) // 1, 2
stack.Values() // 2, 1 (LIFO order)
_, _ = stack.Peek() // 2,true
_, _ = stack.Pop() // 2, true
_, _ = stack.Pop() // 1, true
_, _ = stack.Pop() // nil, false (nothing to pop)
stack.Push(1) // 1
stack.Clear() // empty
stack.Empty() // true
stack.Size() // 0
}
```
### 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](#containers) interface.
```go
type Map interface {
Put(key interface{}, value interface{})
Get(key interface{}) (value interface{}, found bool)
Remove(key interface{})
Keys() []interface{}
containers.Container
// Empty() bool
// Size() int
// Clear()
// Values() []interface{}
// String() string
}
```
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.
```go
type BidiMap interface {
GetKey(value interface{}) (key interface{}, found bool)
Map
}
```
#### HashMap
A [map](#maps) based on hash tables. Keys are unordered.
Implements [Map](#maps), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
```go
package main
import "github.com/emirpasic/gods/maps/hashmap"
func main() {
m := hashmap.New() // empty
m.Put(1, "x") // 1->x
m.Put(2, "b") // 2->b, 1->x (random order)
m.Put(1, "a") // 2->b, 1->a (random order)
_, _ = m.Get(2) // b, true
_, _ = m.Get(3) // nil, false
_ = m.Values() // []interface {}{"b", "a"} (random order)
_ = m.Keys() // []interface {}{1, 2} (random order)
m.Remove(1) // 2->b
m.Clear() // empty
m.Empty() // true
m.Size() // 0
}
```
#### TreeMap
A [map](#maps) based on [red-black tree](#redblacktree). Keys are ordered with respect to the [comparator](#comparator).
Implements [Map](#maps), [ReverseIteratorWithIndex](#reverseiteratorwithindex), [EnumerableWithKey](#enumerablewithkey), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
```go
package main
import "github.com/emirpasic/gods/maps/treemap"
func main() {
m := treemap.NewWithIntComparator() // empty (keys are of type int)
m.Put(1, "x") // 1->x
m.Put(2, "b") // 1->x, 2->b (in order)
m.Put(1, "a") // 1->a, 2->b (in order)
_, _ = m.Get(2) // b, true
_, _ = m.Get(3) // nil, false
_ = m.Values() // []interface {}{"a", "b"} (in order)
_ = m.Keys() // []interface {}{1, 2} (in order)
m.Remove(1) // 2->b
m.Clear() // empty
m.Empty() // true
m.Size() // 0
// Other:
m.Min() // Returns the minimum key and its value from map.
m.Max() // Returns the maximum key and its value from map.
}
```
#### LinkedHashMap
A [map](#maps) that preserves insertion-order. It is backed by a hash table to store values and [doubly-linked list](doublylinkedlist) to store ordering.
Implements [Map](#maps), [ReverseIteratorWithIndex](#reverseiteratorwithindex), [EnumerableWithKey](#enumerablewithkey), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
```go
package main
import "github.com/emirpasic/gods/maps/linkedhashmap"
func main() {
m := linkedhashmap.New() // empty (keys are of type int)
m.Put(2, "b") // 2->b
m.Put(1, "x") // 2->b, 1->x (insertion-order)
m.Put(1, "a") // 2->b, 1->a (insertion-order)
_, _ = m.Get(2) // b, true
_, _ = m.Get(3) // nil, false
_ = m.Values() // []interface {}{"b", "a"} (insertion-order)
_ = m.Keys() // []interface {}{2, 1} (insertion-order)
m.Remove(1) // 2->b
m.Clear() // empty
m.Empty() // true
m.Size() // 0
}
```
#### HashBidiMap
A [map](#maps) based on two hashmaps. Keys are unordered.
Implements [BidiMap](#maps), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
```go
package main
import "github.com/emirpasic/gods/maps/hashbidimap"
func main() {
m := hashbidimap.New() // empty
m.Put(1, "x") // 1->x
m.Put(3, "b") // 1->x, 3->b (random order)
m.Put(1, "a") // 1->a, 3->b (random order)
m.Put(2, "b") // 1->a, 2->b (random order)
_, _ = m.GetKey("a") // 1, true
_, _ = m.Get(2) // b, true
_, _ = m.Get(3) // nil, false
_ = m.Values() // []interface {}{"a", "b"} (random order)
_ = m.Keys() // []interface {}{1, 2} (random order)
m.Remove(1) // 2->b
m.Clear() // empty
m.Empty() // true
m.Size() // 0
}
```
#### TreeBidiMap
A [map](#maps) 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](#hashbidimap)), which can be significant if contained elements are large.
Implements [BidiMap](#maps), [ReverseIteratorWithIndex](#reverseiteratorwithindex), [EnumerableWithKey](#enumerablewithkey), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
```go
package main
import (
"github.com/emirpasic/gods/maps/treebidimap"
"github.com/emirpasic/gods/utils"
)
func main() {
m := treebidimap.NewWith(utils.IntComparator, utils.StringComparator)
m.Put(1, "x") // 1->x
m.Put(3, "b") // 1->x, 3->b (ordered)
m.Put(1, "a") // 1->a, 3->b (ordered)
m.Put(2, "b") // 1->a, 2->b (ordered)
_, _ = m.GetKey("a") // 1, true
_, _ = m.Get(2) // b, true
_, _ = m.Get(3) // nil, false
_ = m.Values() // []interface {}{"a", "b"} (ordered)
_ = m.Keys() // []interface {}{1, 2} (ordered)
m.Remove(1) // 2->b
m.Clear() // empty
m.Empty() // true
m.Size() // 0
}
```
### 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](#containers) interface.
```go
type Tree interface {
containers.Container
// Empty() bool
// Size() int
// Clear()
// Values() []interface{}
// String() string
}
```
#### RedBlackTree
A red–black [tree](#trees) 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. <sub><sup>[Wikipedia](http://en.wikipedia.org/wiki/Red%E2%80%93black_tree)</sup></sub>
Implements [Tree](#trees), [ReverseIteratorWithKey](#reverseiteratorwithkey), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
<p align="center"><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/6/66/Red-black_tree_example.svg/500px-Red-black_tree_example.svg.png" width="400px" height="200px" /></p>
```go
package main
import (
"fmt"
rbt "github.com/emirpasic/gods/trees/redblacktree"
)
func main() {
tree := rbt.NewWithIntComparator() // empty (keys are of type int)
tree.Put(1, "x") // 1->x
tree.Put(2, "b") // 1->x, 2->b (in order)
tree.Put(1, "a") // 1->a, 2->b (in order, replacement)
tree.Put(3, "c") // 1->a, 2->b, 3->c (in order)
tree.Put(4, "d") // 1->a, 2->b, 3->c, 4->d (in order)
tree.Put(5, "e") // 1->a, 2->b, 3->c, 4->d, 5->e (in order)
tree.Put(6, "f") // 1->a, 2->b, 3->c, 4->d, 5->e, 6->f (in order)
fmt.Println(tree)
//
// RedBlackTree
// │ ┌── 6
// │ ┌── 5
// │ ┌── 4
// │ │ └── 3
// └── 2
// └── 1
_ = tree.Values() // []interface {}{"a", "b", "c", "d", "e", "f"} (in order)
_ = tree.Keys() // []interface {}{1, 2, 3, 4, 5, 6} (in order)
tree.Remove(2) // 1->a, 3->c, 4->d, 5->e, 6->f (in order)
fmt.Println(tree)
//
// RedBlackTree
// │ ┌── 6
// │ ┌── 5
// └── 4
// │ ┌── 3
// └── 1
tree.Clear() // empty
tree.Empty() // true
tree.Size() // 0
// Other:
tree.Left() // gets the left-most (min) node
tree.Right() // get the right-most (max) node
tree.Floor(1) // get the floor node
tree.Ceiling(1) // get the ceiling node
}
```
Extending the red-black tree's functionality has been demonstrated in the following [example](https://github.com/emirpasic/gods/blob/master/examples/redblacktreeextended/redblacktreeextended.go).
#### AVLTree
AVL [tree](#trees) 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 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. <sub><sup>[Wikipedia](https://en.wikipedia.org/wiki/AVL_tree)</sup></sub>
Implements [Tree](#trees), [ReverseIteratorWithKey](#reverseiteratorwithkey), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
<p align="center"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/a/ad/AVL-tree-wBalance_K.svg/262px-AVL-tree-wBalance_K.svg.png" width="300px" height="180px" /><br/><sub>AVL tree with balance factors (green)</sub></p>
```go
package main
import (
"fmt"
avl "github.com/emirpasic/gods/trees/avltree"
)
func main() {
tree := avl.NewWithIntComparator() // empty(keys are of type int)
tree.Put(1, "x") // 1->x
tree.Put(2, "b") // 1->x, 2->b (in order)
tree.Put(1, "a") // 1->a, 2->b (in order, replacement)
tree.Put(3, "c") // 1->a, 2->b, 3->c (in order)
tree.Put(4, "d") // 1->a, 2->b, 3->c, 4->d (in order)
tree.Put(5, "e") // 1->a, 2->b, 3->c, 4->d, 5->e (in order)
tree.Put(6, "f") // 1->a, 2->b, 3->c, 4->d, 5->e, 6->f (in order)
fmt.Println(tree)
//
// AVLTree
// │ ┌── 6
// │ ┌── 5
// └── 4
// │ ┌── 3
// └── 2
// └── 1
_ = tree.Values() // []interface {}{"a", "b", "c", "d", "e", "f"} (in order)
_ = tree.Keys() // []interface {}{1, 2, 3, 4, 5, 6} (in order)
tree.Remove(2) // 1->a, 3->c, 4->d, 5->e, 6->f (in order)
fmt.Println(tree)
//
// AVLTree
// │ ┌── 6
// │ ┌── 5
// └── 4
// └── 3
// └── 1
tree.Clear() // empty
tree.Empty() // true
tree.Size() // 0
}
```
#### 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.
According to Knuth's definition, a B-tree of order m is a tree which satisfies the following properties:
- Every node has at most m children.
- Every non-leaf node (except root) has at least ⌈m/2⌉ children.
- 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.<sub><sup>[Wikipedia](http://en.wikipedia.org/wiki/Red%E2%80%93black_tree)</sub></sup>
Implements [Tree](#trees), [ReverseIteratorWithKey](#reverseiteratorwithkey), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
<p align="center"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/6/65/B-tree.svg/831px-B-tree.svg.png" width="400px" height="111px" /></p>
```go
package main
import (
"fmt"
"github.com/emirpasic/gods/trees/btree"
)
func main() {
tree := btree.NewWithIntComparator(3) // empty (keys are of type int)
tree.Put(1, "x") // 1->x
tree.Put(2, "b") // 1->x, 2->b (in order)
tree.Put(1, "a") // 1->a, 2->b (in order, replacement)
tree.Put(3, "c") // 1->a, 2->b, 3->c (in order)
tree.Put(4, "d") // 1->a, 2->b, 3->c, 4->d (in order)
tree.Put(5, "e") // 1->a, 2->b, 3->c, 4->d, 5->e (in order)
tree.Put(6, "f") // 1->a, 2->b, 3->c, 4->d, 5->e, 6->f (in order)
tree.Put(7, "g") // 1->a, 2->b, 3->c, 4->d, 5->e, 6->f, 7->g (in order)
fmt.Println(tree)
// BTree
// 1
// 2
// 3
// 4
// 5
// 6
// 7
_ = tree.Values() // []interface {}{"a", "b", "c", "d", "e", "f", "g"} (in order)
_ = tree.Keys() // []interface {}{1, 2, 3, 4, 5, 6, 7} (in order)
tree.Remove(2) // 1->a, 3->c, 4->d, 5->e, 6->f, 7->g (in order)
fmt.Println(tree)
// BTree
// 1
// 3
// 4
// 5
// 6
// 7
tree.Clear() // empty
tree.Empty() // true
tree.Size() // 0
// Other:
tree.Height() // gets the height of the tree
tree.Left() // gets the left-most (min) node
tree.LeftKey() // get the left-most (min) node's key
tree.LeftValue() // get the left-most (min) node's value
tree.Right() // get the right-most (max) node
tree.RightKey() // get the right-most (max) node's key
tree.RightValue() // get the right-most (max) node's value
}
```
#### BinaryHeap
A binary heap is a [tree](#trees) 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. <sub><sup>[Wikipedia](http://en.wikipedia.org/wiki/Binary_heap)</sub></sup>
Implements [Tree](#trees), [ReverseIteratorWithIndex](#reverseiteratorwithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
<p align="center"><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/3/38/Max-Heap.svg/501px-Max-Heap.svg.png" width="300px" height="200px" /></p>
```go
package main
import (
"github.com/emirpasic/gods/trees/binaryheap"
"github.com/emirpasic/gods/utils"
)
func main() {
// Min-heap
heap := binaryheap.NewWithIntComparator() // empty (min-heap)
heap.Push(2) // 2
heap.Push(3) // 2, 3
heap.Push(1) // 1, 3, 2
heap.Values() // 1, 3, 2
_, _ = heap.Peek() // 1,true
_, _ = heap.Pop() // 1, true
_, _ = heap.Pop() // 2, true
_, _ = heap.Pop() // 3, true
_, _ = heap.Pop() // nil, false (nothing to pop)
heap.Push(1) // 1
heap.Clear() // empty
heap.Empty() // true
heap.Size() // 0
// Max-heap
inverseIntComparator := func(a, b interface{}) int {
return -utils.IntComparator(a, b)
}
heap = binaryheap.NewWith(inverseIntComparator) // empty (min-heap)
heap.Push(2, 3, 1) // 3, 2, 1 (bulk optimized)
heap.Values() // 3, 2, 1
}
```
### 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.
<p align="center"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/52/Data_Queue.svg/300px-Data_Queue.svg.png" width="200px" height="120px" /></p>
Implements [Container](#containers) interface.
```go
type Queue interface {
Enqueue(value interface{})
Dequeue() (value interface{}, ok bool)
Peek() (value interface{}, ok bool)
containers.Container
// Empty() bool
// Size() int
// Clear()
// Values() []interface{}
// String() string
}
```
#### LinkedListQueue
A [queue](#queues) based on a [linked list](#singlylinkedlist).
Implements [Queue](#queues), [IteratorWithIndex](#iteratorwithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
```go
package main
import llq "github.com/emirpasic/gods/queues/linkedlistqueue"
// LinkedListQueueExample to demonstrate basic usage of LinkedListQueue
func main() {
queue := llq.New() // empty
queue.Enqueue(1) // 1
queue.Enqueue(2) // 1, 2
_ = queue.Values() // 1, 2 (FIFO order)
_, _ = queue.Peek() // 1,true
_, _ = queue.Dequeue() // 1, true
_, _ = queue.Dequeue() // 2, true
_, _ = queue.Dequeue() // nil, false (nothing to deque)
queue.Enqueue(1) // 1
queue.Clear() // empty
queue.Empty() // true
_ = queue.Size() // 0
}
```
#### ArrayQueue
A [queue](#queues) based on a [array list](#arraylist).
Implements [Queue](#queues), [ReverseIteratorWithIndex](#iteratorwithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
```go
package main
import aq "github.com/emirpasic/gods/queues/arrayqueue"
// ArrayQueueExample to demonstrate basic usage of ArrayQueue
func main() {
queue := aq.New() // empty
queue.Enqueue(1) // 1
queue.Enqueue(2) // 1, 2
_ = queue.Values() // 1, 2 (FIFO order)
_, _ = queue.Peek() // 1,true
_, _ = queue.Dequeue() // 1, true
_, _ = queue.Dequeue() // 2, true
_, _ = queue.Dequeue() // nil, false (nothing to deque)
queue.Enqueue(1) // 1
queue.Clear() // empty
queue.Empty() // true
_ = queue.Size() // 0
}
```
#### CircularBuffer
A circular buffer, circular [queue](#queues), 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.
<p align="center"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/fd/Circular_Buffer_Animation.gif/400px-Circular_Buffer_Animation.gif" width="300px" height="300px" /></p>
Implements [Queue](#queues), [ReverseIteratorWithIndex](#iteratorwithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
```go
package main
import cb "github.com/emirpasic/gods/queues/circularbuffer"
// CircularBufferExample to demonstrate basic usage of CircularBuffer
func main() {
queue := cb.New(3) // empty (max size is 3)
queue.Enqueue(1) // 1
queue.Enqueue(2) // 1, 2
queue.Enqueue(3) // 1, 2, 3
_ = queue.Values() // 1, 2, 3
queue.Enqueue(3) // 4, 2, 3
_, _ = queue.Peek() // 4,true
_, _ = queue.Dequeue() // 4, true
_, _ = queue.Dequeue() // 2, true
_, _ = queue.Dequeue() // 3, true
_, _ = queue.Dequeue() // nil, false (nothing to deque)
queue.Enqueue(1) // 1
queue.Clear() // empty
queue.Empty() // true
_ = queue.Size() // 0
}
```
#### PriorityQueue
A priority queue is a special type of [queue](#queues) 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](#queues), [ReverseIteratorWithIndex](#iteratorwithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
```go
package main
import (
pq "github.com/emirpasic/gods/queues/priorityqueue"
"github.com/emirpasic/gods/utils"
)
// Element is an entry in the priority queue
type Element struct {
name string
priority int
}
// Comparator function (sort by element's priority value in descending order)
func byPriority(a, b interface{}) int {
priorityA := a.(Element).priority
priorityB := b.(Element).priority
return -utils.IntComparator(priorityA, priorityB) // "-" descending order
}
// PriorityQueueExample to demonstrate basic usage of BinaryHeap
func main() {
a := Element{name: "a", priority: 1}
b := Element{name: "b", priority: 2}
c := Element{name: "c", priority: 3}
queue := pq.NewWith(byPriority) // empty
queue.Enqueue(a) // {a 1}
queue.Enqueue(c) // {c 3}, {a 1}
queue.Enqueue(b) // {c 3}, {b 2}, {a 1}
_ = queue.Values() // [{c 3} {b 2} {a 1}]
_, _ = queue.Peek() // {c 3} true
_, _ = queue.Dequeue() // {c 3} true
_, _ = queue.Dequeue() // {b 2} true
_, _ = queue.Dequeue() // {a 1} true
_, _ = queue.Dequeue() // <nil> false (nothing to dequeue)
queue.Clear() // empty
_ = queue.Empty() // true
_ = queue.Size() // 0
}
```
## 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.
Comparator is defined as:
Return values (int):
```go
negative , if a < b
zero , if a == b
positive , if a > b
```
Comparator signature:
```go
type Comparator func(a, b interface{}) int
```
All common comparators for builtin types are included in the library:
```go
func StringComparator(a, b interface{}) int
func IntComparator(a, b interface{}) int
func Int8Comparator(a, b interface{}) int
func Int16Comparator(a, b interface{}) int
func Int32Comparator(a, b interface{}) int
func Int64Comparator(a, b interface{}) int
func UIntComparator(a, b interface{}) int
func UInt8Comparator(a, b interface{}) int
func UInt16Comparator(a, b interface{}) int
func UInt32Comparator(a, b interface{}) int
func UInt64Comparator(a, b interface{}) int
func Float32Comparator(a, b interface{}) int
func Float64Comparator(a, b interface{}) int
func ByteComparator(a, b interface{}) int
func RuneComparator(a, b interface{}) int
func TimeComparator(a, b interface{}) int
```
Writing custom comparators is easy:
```go
package main
import (
"fmt"
"github.com/emirpasic/gods/sets/treeset"
)
type User struct {
id int
name string
}
// Custom comparator (sort by IDs)
func byID(a, b interface{}) int {
// Type assertion, program will panic if this is not respected
c1 := a.(User)
c2 := b.(User)
switch {
case c1.id > c2.id:
return 1
case c1.id < c2.id:
return -1
default:
return 0
}
}
func main() {
set := treeset.NewWith(byID)
set.Add(User{2, "Second"})
set.Add(User{3, "Third"})
set.Add(User{1, "First"})
set.Add(User{4, "Fourth"})
fmt.Println(set) // {1 First}, {2 Second}, {3 Third}, {4 Fourth}
}
```
### 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.
Note: it is unsafe to remove elements from container while iterating.
#### IteratorWithIndex
An [iterator](#iterator) whose elements are referenced by an index.
Typical usage:
```go
it := list.Iterator()
for it.Next() {
index, value := it.Index(), it.Value()
...
}
```
Other usages:
```go
if it.First() {
firstIndex, firstValue := it.Index(), it.Value()
...
}
```
```go
for it.Begin(); it.Next(); {
...
}
```
Seeking to a specific element:
```go
// Seek function, i.e. find element starting with "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.
for found := it.NextTo(seek); found; found = it.Next() {
index, value := it.Index(), it.Value()
...
}
```
#### IteratorWithKey
An [iterator](#iterator) whose elements are referenced by a key.
Typical usage:
```go
it := tree.Iterator()
for it.Next() {
key, value := it.Key(), it.Value()
...
}
```
Other usages:
```go
if it.First() {
firstKey, firstValue := it.Key(), it.Value()
...
}
```
```go
for it.Begin(); it.Next(); {
...
}
```
Seeking to a specific element from the current iterator position:
```go
// Seek function, i.e. find element starting with "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.
for found := it.NextTo(seek); found; found = it.Next() {
key, value := it.Key(), it.Value()
...
}
```
#### ReverseIteratorWithIndex
An [iterator](#iterator) whose elements are referenced by an index. Provides all functions as [IteratorWithIndex](#iteratorwithindex), but can also be used for reverse iteration.
Typical usage of iteration in reverse:
```go
it := list.Iterator()
for it.End(); it.Prev(); {
index, value := it.Index(), it.Value()
...
}
```
Other usages:
```go
if it.Last() {
lastIndex, lastValue := it.Index(), it.Value()
...
}
```
Seeking to a specific element:
```go
// Seek function, i.e. find element starting with "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.
for found := it.PrevTo(seek); found; found = it.Prev() {
index, value := it.Index(), it.Value()
...
}
```
#### ReverseIteratorWithKey
An [iterator](#iterator) whose elements are referenced by a key. Provides all functions as [IteratorWithKey](#iteratorwithkey), but can also be used for reverse iteration.
Typical usage of iteration in reverse:
```go
it := tree.Iterator()
for it.End(); it.Prev(); {
key, value := it.Key(), it.Value()
...
}
```
Other usages:
```go
if it.Last() {
lastKey, lastValue := it.Key(), it.Value()
...
}
```
```go
// Seek function, i.e. find element starting with "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.
for found := it.PrevTo(seek); found; found = it.Prev() {
key, value := it.Key(), it.Value()
...
}
```
### Enumerable
Enumerable functions for ordered containers that implement [EnumerableWithIndex](#enumerablewithindex) or [EnumerableWithKey](#enumerablewithkey) interfaces.
#### EnumerableWithIndex
[Enumerable](#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.
```go
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.
```go
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.
```go
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.
```go
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.
```go
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.
```go
Find(func(index int, value interface{}) bool) (int, interface{})}
```
**Example:**
```go
package main
import (
"fmt"
"github.com/emirpasic/gods/sets/treeset"
)
func printSet(txt string, set *treeset.Set) {
fmt.Print(txt, "[ ")
set.Each(func(index int, value interface{}) {
fmt.Print(value, " ")
})
fmt.Println("]")
}
func main() {
set := treeset.NewWithIntComparator()
set.Add(2, 3, 4, 2, 5, 6, 7, 8)
printSet("Initial", set) // [ 2 3 4 5 6 7 8 ]
even := set.Select(func(index int, value interface{}) bool {
return value.(int)%2 == 0
})
printSet("Even numbers", even) // [ 2 4 6 8 ]
foundIndex, foundValue := set.Find(func(index int, value interface{}) bool {
return value.(int)%2 == 0 && value.(int)%3 == 0
})
if foundIndex != -1 {
fmt.Println("Number divisible by 2 and 3 found is", foundValue, "at index", foundIndex) // value: 6, index: 4
}
square := set.Map(func(index int, value interface{}) interface{} {
return value.(int) * value.(int)
})
printSet("Numbers squared", square) // [ 4 9 16 25 36 49 64 ]
bigger := set.Any(func(index int, value interface{}) bool {
return value.(int) > 5
})
fmt.Println("Set contains a number bigger than 5 is ", bigger) // true
positive := set.All(func(index int, value interface{}) bool {
return value.(int) > 0
})
fmt.Println("All numbers are positive is", positive) // true
evenNumbersSquared := set.Select(func(index int, value interface{}) bool {
return value.(int)%2 == 0
}).Map(func(index int, value interface{}) interface{} {
return value.(int) * value.(int)
})
printSet("Chaining", evenNumbersSquared) // [ 4 16 36 64 ]
}
```
#### 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.
```go
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.
```go
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.
```go
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.
```go
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.
```go
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.
```go
Find(func(key interface{}, value interface{}) bool) (interface{}, interface{})
```
**Example:**
```go
package main
import (
"fmt"
"github.com/emirpasic/gods/maps/treemap"
)
func printMap(txt string, m *treemap.Map) {
fmt.Print(txt, " { ")
m.Each(func(key interface{}, value interface{}) {
fmt.Print(key, ":", value, " ")
})
fmt.Println("}")
}
func main() {
m := treemap.NewWithStringComparator()
m.Put("g", 7)
m.Put("f", 6)
m.Put("e", 5)
m.Put("d", 4)
m.Put("c", 3)
m.Put("b", 2)
m.Put("a", 1)
printMap("Initial", m) // { a:1 b:2 c:3 d:4 e:5 f:6 g:7 }
even := m.Select(func(key interface{}, value interface{}) bool {
return value.(int) % 2 == 0
})
printMap("Elements with even values", even) // { b:2 d:4 f:6 }
foundKey, foundValue := m.Find(func(key interface{}, value interface{}) bool {
return value.(int) % 2 == 0 && value.(int) % 3 == 0
})
if foundKey != nil {
fmt.Println("Element with value divisible by 2 and 3 found is", foundValue, "with key", foundKey) // value: 6, index: 4
}
square := m.Map(func(key interface{}, value interface{}) (interface{}, interface{}) {
return key.(string) + key.(string), value.(int) * value.(int)
})
printMap("Elements' values squared and letters duplicated", square) // { aa:1 bb:4 cc:9 dd:16 ee:25 ff:36 gg:49 }
bigger := m.Any(func(key interface{}, value interface{}) bool {
return value.(int) > 5
})
fmt.Println("Map contains element whose value is bigger than 5 is", bigger) // true
positive := m.All(func(key interface{}, value interface{}) bool {
return value.(int) > 0
})
fmt.Println("All map's elements have positive values is", positive) // true
evenNumbersSquared := m.Select(func(key interface{}, value interface{}) bool {
return value.(int) % 2 == 0
}).Map(func(key interface{}, value interface{}) (interface{}, interface{}) {
return key, value.(int) * value.(int)
})
printMap("Chaining", evenNumbersSquared) // { b:4 d:16 f:36 }
}
```
### Serialization
All data structures can be serialized (marshalled) and deserialized (unmarshalled). Currently, only JSON support is available.
#### JSONSerializer
Outputs the container into its JSON representation.
Typical usage for key-value structures:
```go
package main
import (
"encoding/json"
"fmt"
"github.com/emirpasic/gods/maps/hashmap"
)
func main() {
m := hashmap.New()
m.Put("a", "1")
m.Put("b", "2")
m.Put("c", "3")
bytes, err := json.Marshal(m) // Same as "m.ToJSON(m)"
if err != nil {
fmt.Println(err)
}
fmt.Println(string(bytes)) // {"a":"1","b":"2","c":"3"}
}
```
Typical usage for value-only structures:
```go
package main
import (
"encoding/json"
"fmt"
"github.com/emirpasic/gods/lists/arraylist"
)
func main() {
list := arraylist.New()
list.Add("a", "b", "c")
bytes, err := json.Marshal(list) // Same as "list.ToJSON(list)"
if err != nil {
fmt.Println(err)
}
fmt.Println(string(bytes)) // ["a","b","c"]
}
```
#### JSONDeserializer
Populates the container with elements from the input JSON representation.
Typical usage for key-value structures:
```go
package main
import (
"encoding/json"
"fmt"
"github.com/emirpasic/gods/maps/hashmap"
)
func main() {
hm := hashmap.New()
bytes := []byte(`{"a":"1","b":"2"}`)
err := json.Unmarshal(bytes, &hm) // Same as "hm.FromJSON(bytes)"
if err != nil {
fmt.Println(err)
}
fmt.Println(hm) // HashMap map[b:2 a:1]
}
```
Typical usage for value-only structures:
```go
package main
import (
"encoding/json"
"fmt"
"github.com/emirpasic/gods/lists/arraylist"
)
func main() {
list := arraylist.New()
bytes := []byte(`["a","b"]`)
err := json.Unmarshal(bytes, &list) // Same as "list.FromJSON(bytes)"
if err != nil {
fmt.Println(err)
}
fmt.Println(list) // ArrayList ["a","b"]
}
```
### Sort
Sort is a general purpose sort function.
Lists have an in-place _Sort()_ function and all containers can return their sorted elements via _containers.GetSortedValues()_ function.
Internally these all use the _utils.Sort()_ method:
```go
package main
import "github.com/emirpasic/gods/utils"
func main() {
strings := []interface{}{} // []
strings = append(strings, "d") // ["d"]
strings = append(strings, "a") // ["d","a"]
strings = append(strings, "b") // ["d","a",b"
strings = append(strings, "c") // ["d","a",b","c"]
utils.Sort(strings, utils.StringComparator) // ["a","b","c","d"]
}
```
### Container
Container specific operations:
```go
// 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:
```go
package main
import (
"github.com/emirpasic/gods/lists/arraylist"
"github.com/emirpasic/gods/utils"
)
func main() {
list := arraylist.New()
list.Add(2, 1, 3)
values := GetSortedValues(container, utils.StringComparator) // [1, 2, 3]
}
```
## Appendix
### Motivation
Collections and data structures found in other languages: Java Collections, C++ Standard Template Library (STL) containers, Qt Containers, Ruby Enumerable etc.
### 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.
**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 ./...`
<p align="center"><img src="https://cloud.githubusercontent.com/assets/3115942/16892979/5e698d46-4b27-11e6-864b-cb2b865327b6.png" /></p>
### 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.
Coding style:
```shell
# Install tooling and set path:
go install gotest.tools/gotestsum@latest
go install golang.org/x/lint/golint@latest
go install github.com/kisielk/errcheck@latest
export PATH=$PATH:$GOPATH/bin
# Fix errors and warnings:
go fmt ./... &&
go test -v ./... &&
golint -set_exit_status ./... &&
! go fmt ./... 2>&1 | read &&
go vet -v ./... &&
gocyclo -avg -over 15 ../gods &&
errcheck ./...
```
### License
This library is distributed under the BSD-style license found in the [LICENSE](https://github.com/emirpasic/gods/blob/master/LICENSE) file.
### Sponsors
## <a href="https://www.browserstack.com/?ref=gods"><img src="http://www.hajdarevic.net/browserstack.svg" alt="BrowserStack" width="250"/></a>
[BrowserStack](https://www.browserstack.com/?ref=webhook) 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.
================================================
FILE: containers/containers.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package containers provides core interfaces and functions for data structures.
//
// Container is the base interface for all data structures to implement.
//
// Iterators provide stateful iterators.
//
// Enumerable provides Ruby inspired (each, select, map, find, any?, etc.) container functions.
//
// Serialization provides serializers (marshalers) and deserializers (unmarshalers).
package containers
import (
"cmp"
"slices"
"github.com/emirpasic/gods/v2/utils"
)
// Container is base interface that all data structures implement.
type Container[T any] interface {
Empty() bool
Size() int
Clear()
Values() []T
String() string
}
// GetSortedValues returns sorted container's elements with respect to the passed comparator.
// Does not affect the ordering of elements within the container.
func GetSortedValues[T cmp.Ordered](container Container[T]) []T {
values := container.Values()
if len(values) < 2 {
return values
}
slices.Sort(values)
return values
}
// GetSortedValuesFunc is the equivalent of GetSortedValues for containers of values that are not ordered.
func GetSortedValuesFunc[T any](container Container[T], comparator utils.Comparator[T]) []T {
values := container.Values()
if len(values) < 2 {
return values
}
slices.SortFunc(values, comparator)
return values
}
================================================
FILE: containers/containers_test.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// All data structures must implement the container structure
package containers
import (
"cmp"
"fmt"
"strings"
"testing"
)
// For testing purposes
type ContainerTest[T any] struct {
values []T
}
func (container ContainerTest[T]) Empty() bool {
return len(container.values) == 0
}
func (container ContainerTest[T]) Size() int {
return len(container.values)
}
func (container ContainerTest[T]) Clear() {
container.values = []T{}
}
func (container ContainerTest[T]) Values() []T {
return container.values
}
func (container ContainerTest[T]) String() string {
str := "ContainerTest\n"
var values []string
for _, value := range container.values {
values = append(values, fmt.Sprintf("%v", value))
}
str += strings.Join(values, ", ")
return str
}
func TestGetSortedValuesInts(t *testing.T) {
container := ContainerTest[int]{}
GetSortedValues(container)
container.values = []int{5, 1, 3, 2, 4}
values := GetSortedValues(container)
for i := 1; i < container.Size(); i++ {
if values[i-1] > values[i] {
t.Errorf("Not sorted!")
}
}
}
type NotInt struct {
i int
}
func TestGetSortedValuesNotInts(t *testing.T) {
container := ContainerTest[NotInt]{}
GetSortedValuesFunc(container, func(x, y NotInt) int {
return cmp.Compare(x.i, y.i)
})
container.values = []NotInt{{5}, {1}, {3}, {2}, {4}}
values := GetSortedValuesFunc(container, func(x, y NotInt) int {
return cmp.Compare(x.i, y.i)
})
for i := 1; i < container.Size(); i++ {
if values[i-1].i > values[i].i {
t.Errorf("Not sorted!")
}
}
}
================================================
FILE: containers/enumerable.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package containers
// EnumerableWithIndex provides functions for ordered containers whose values can be fetched by an index.
type EnumerableWithIndex[T any] interface {
// Each calls the given function once for each element, passing that element's index and value.
Each(func(index int, value T))
// 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.
// 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.
Any(func(index int, value T) bool) bool
// All passes each element of the container to the given function and
// returns true if the function returns true for all elements.
All(func(index int, value T) 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.
Find(func(index int, value T) bool) (int, T)
}
// EnumerableWithKey provides functions for ordered containers whose values whose elements are key/value pairs.
type EnumerableWithKey[K, V any] interface {
// Each calls the given function once for each element, passing that element's key and value.
Each(func(key K, value V))
// 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.
// 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.
Any(func(key K, value V) bool) bool
// All passes each element of the container to the given function and
// returns true if the function returns true for all elements.
All(func(key K, value V) 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.
Find(func(key K, value V) bool) (K, V)
}
================================================
FILE: containers/iterator.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package containers
// IteratorWithIndex is stateful iterator for ordered containers whose values can be fetched by an index.
type IteratorWithIndex[T any] interface {
// Next moves the iterator to the next element and returns true if there was a next element in the container.
// If Next() returns true, then next element's index and value can be retrieved by Index() and Value().
// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
// Modifies the state of the iterator.
Next() bool
// Value returns the current element's value.
// Does not modify the state of the iterator.
Value() T
// Index returns the current element's index.
// Does not modify the state of the iterator.
Index() int
// Begin resets the iterator to its initial state (one-before-first)
// Call Next() to fetch the first element if any.
Begin()
// First moves the iterator to the first element and returns true if there was a first element in the container.
// If First() returns true, then first element's index and value can be retrieved by Index() and Value().
// Modifies the state of the iterator.
First() bool
// NextTo moves the iterator to the next element from current position that satisfies the condition given by the
// passed function, and returns true if there was a next element in the container.
// If NextTo() returns true, then next element's index and value can be retrieved by Index() and Value().
// Modifies the state of the iterator.
NextTo(func(index int, value T) bool) bool
}
// IteratorWithKey is a stateful iterator for ordered containers whose elements are key value pairs.
type IteratorWithKey[K, V any] interface {
// Next moves the iterator to the next element and returns true if there was a next element in the container.
// If Next() returns true, then next element's key and value can be retrieved by Key() and Value().
// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
// Modifies the state of the iterator.
Next() bool
// Value returns the current element's value.
// Does not modify the state of the iterator.
Value() V
// Key returns the current element's key.
// Does not modify the state of the iterator.
Key() K
// Begin resets the iterator to its initial state (one-before-first)
// Call Next() to fetch the first element if any.
Begin()
// First moves the iterator to the first element and returns true if there was a first element in the container.
// If First() returns true, then first element's key and value can be retrieved by Key() and Value().
// Modifies the state of the iterator.
First() bool
// NextTo moves the iterator to the next element from current position that satisfies the condition given by the
// passed function, and returns true if there was a next element in the container.
// If NextTo() returns true, then next element's key and value can be retrieved by Key() and Value().
// Modifies the state of the iterator.
NextTo(func(key K, value V) bool) bool
}
// ReverseIteratorWithIndex is stateful iterator for ordered containers whose values can be fetched by an index.
//
// Essentially it is the same as IteratorWithIndex, but provides additional:
//
// # Prev() function to enable traversal in reverse
//
// Last() function to move the iterator to the last element.
//
// End() function to move the iterator past the last element (one-past-the-end).
type ReverseIteratorWithIndex[T any] interface {
// Prev moves the iterator to the previous element and returns true if there was a previous element in the container.
// If Prev() returns true, then previous element's index and value can be retrieved by Index() and Value().
// Modifies the state of the iterator.
Prev() bool
// End moves the iterator past the last element (one-past-the-end).
// Call Prev() to fetch the last element if any.
End()
// Last moves the iterator to the last element and returns true if there was a last element in the container.
// If Last() returns true, then last element's index and value can be retrieved by Index() and Value().
// Modifies the state of the iterator.
Last() bool
// PrevTo moves the iterator to the previous element from current position that satisfies the condition given by the
// passed function, and returns true if there was a next element in the container.
// If PrevTo() returns true, then next element's index and value can be retrieved by Index() and Value().
// Modifies the state of the iterator.
PrevTo(func(index int, value T) bool) bool
IteratorWithIndex[T]
}
// ReverseIteratorWithKey is a stateful iterator for ordered containers whose elements are key value pairs.
//
// Essentially it is the same as IteratorWithKey, but provides additional:
//
// # Prev() function to enable traversal in reverse
//
// Last() function to move the iterator to the last element.
type ReverseIteratorWithKey[K, V any] interface {
// Prev moves the iterator to the previous element and returns true if there was a previous element in the container.
// If Prev() returns true, then previous element's key and value can be retrieved by Key() and Value().
// Modifies the state of the iterator.
Prev() bool
// End moves the iterator past the last element (one-past-the-end).
// Call Prev() to fetch the last element if any.
End()
// Last moves the iterator to the last element and returns true if there was a last element in the container.
// If Last() returns true, then last element's key and value can be retrieved by Key() and Value().
// Modifies the state of the iterator.
Last() bool
// PrevTo moves the iterator to the previous element from current position that satisfies the condition given by the
// passed function, and returns true if there was a next element in the container.
// If PrevTo() returns true, then next element's key and value can be retrieved by Key() and Value().
// Modifies the state of the iterator.
PrevTo(func(key K, value V) bool) bool
IteratorWithKey[K, V]
}
================================================
FILE: containers/serialization.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package containers
// JSONSerializer provides JSON serialization
type JSONSerializer interface {
// ToJSON outputs the JSON representation of containers's elements.
ToJSON() ([]byte, error)
// MarshalJSON @implements json.Marshaler
MarshalJSON() ([]byte, error)
}
// JSONDeserializer provides JSON deserialization
type JSONDeserializer interface {
// FromJSON populates containers's elements from the input JSON representation.
FromJSON([]byte) error
// UnmarshalJSON @implements json.Unmarshaler
UnmarshalJSON([]byte) error
}
================================================
FILE: examples/README.md
================================================
# GoDS (Go Data Structures)
Various examples on how to use data structures.
## Examples
- [ArrayList](https://github.com/emirpasic/gods/blob/master/examples/arraylist/arraylist.go)
- [ArrayStack](https://github.com/emirpasic/gods/blob/master/examples/arraystack/arraystack.go)
- [AVLTree](https://github.com/emirpasic/gods/blob/master/examples/avltree/avltree.go)
- [BinaryHeap](https://github.com/emirpasic/gods/blob/master/examples/binaryheap/binaryheap.go)
- [BTree](https://github.com/emirpasic/gods/blob/master/examples/btree/btree.go)
- [Custom Comparator](https://github.com/emirpasic/gods/blob/master/examples/customcomparator/customcomparator.go)
- [DoublyLinkedList](https://github.com/emirpasic/gods/blob/master/examples/doublylinkedlist/doublylinkedlist.go)
- [EnumerableWithIndex](https://github.com/emirpasic/gods/blob/master/examples/enumerablewithindex/enumerablewithindex.go)
- [EnumerableWithKey](https://github.com/emirpasic/gods/blob/master/examples/enumerablewithkey/enumerablewithkey.go)
- [HashBidiMap](https://github.com/emirpasic/gods/blob/master/examples/hashbidimap/hashbidimap.go)
- [HashMap](https://github.com/emirpasic/gods/blob/master/examples/hashmap/hashmap.go)
- [HashSet](https://github.com/emirpasic/gods/blob/master/examples/hashset/hashset.go)
- [IteratorWithIndex](https://github.com/emirpasic/gods/blob/master/examples/iteratorwithindex/iteratorwithindex.go)
- [iteratorwithkey](https://github.com/emirpasic/gods/blob/master/examples/iteratorwithkey/iteratorwithkey.go)
- [IteratorWithKey](https://github.com/emirpasic/gods/blob/master/examples/linkedliststack/linkedliststack.go)
- [RedBlackTree](https://github.com/emirpasic/gods/blob/master/examples/redblacktree/redblacktree.go)
- [RedBlackTreeExtended](https://github.com/emirpasic/gods/blob/master/examples/redblacktreeextended/redblacktreeextended.go)
- [Serialization](https://github.com/emirpasic/gods/blob/master/examples/serialization/serialization.go)
- [SinglyLinkedList](https://github.com/emirpasic/gods/blob/master/examples/singlylinkedlist/singlylinkedlist.go)
- [Sort](https://github.com/emirpasic/gods/blob/master/examples/sort/sort.go)
- [TreeBidiMap](https://github.com/emirpasic/gods/blob/master/examples/treebidimap/treebidimap.go)
- [TreeMap](https://github.com/emirpasic/gods/blob/master/examples/treemap/treemap.go)
- [TreeSet](https://github.com/emirpasic/gods/blob/master/examples/treeset/treeset.go)
================================================
FILE: examples/arraylist/arraylist.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"cmp"
"github.com/emirpasic/gods/v2/lists/arraylist"
)
// ArrayListExample to demonstrate basic usage of ArrayList
func main() {
list := arraylist.New[string]()
list.Add("a") // ["a"]
list.Add("c", "b") // ["a","c","b"]
list.Sort(cmp.Compare[string]) // ["a","b","c"]
_, _ = list.Get(0) // "a",true
_, _ = list.Get(100) // nil,false
_ = list.Contains("a", "b", "c") // true
_ = list.Contains("a", "b", "c", "d") // false
list.Swap(0, 1) // ["b","a",c"]
list.Remove(2) // ["b","a"]
list.Remove(1) // ["b"]
list.Remove(0) // []
list.Remove(0) // [] (ignored)
_ = list.Empty() // true
_ = list.Size() // 0
list.Add("a") // ["a"]
list.Clear() // []
}
================================================
FILE: examples/arrayqueue/arrayqqueue.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import aq "github.com/emirpasic/gods/v2/queues/arrayqueue"
// ArrayQueueExample to demonstrate basic usage of ArrayQueue
func main() {
queue := aq.New[int]() // empty
queue.Enqueue(1) // 1
queue.Enqueue(2) // 1, 2
_ = queue.Values() // 1, 2 (FIFO order)
_, _ = queue.Peek() // 1,true
_, _ = queue.Dequeue() // 1, true
_, _ = queue.Dequeue() // 2, true
_, _ = queue.Dequeue() // nil, false (nothing to deque)
queue.Enqueue(1) // 1
queue.Clear() // empty
queue.Empty() // true
_ = queue.Size() // 0
}
================================================
FILE: examples/arraystack/arraystack.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import "github.com/emirpasic/gods/v2/stacks/arraystack"
// ArrayStackExample to demonstrate basic usage of ArrayStack
func main() {
stack := arraystack.New[int]() // empty
stack.Push(1) // 1
stack.Push(2) // 1, 2
stack.Values() // 2, 1 (LIFO order)
_, _ = stack.Peek() // 2,true
_, _ = stack.Pop() // 2, true
_, _ = stack.Pop() // 1, true
_, _ = stack.Pop() // nil, false (nothing to pop)
stack.Push(1) // 1
stack.Clear() // empty
stack.Empty() // true
stack.Size() // 0
}
================================================
FILE: examples/avltree/avltree.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
avl "github.com/emirpasic/gods/v2/trees/avltree"
)
// AVLTreeExample to demonstrate basic usage of AVLTree
func main() {
tree := avl.New[int, string]() // empty(keys are of type int)
tree.Put(1, "x") // 1->x
tree.Put(2, "b") // 1->x, 2->b (in order)
tree.Put(1, "a") // 1->a, 2->b (in order, replacement)
tree.Put(3, "c") // 1->a, 2->b, 3->c (in order)
tree.Put(4, "d") // 1->a, 2->b, 3->c, 4->d (in order)
tree.Put(5, "e") // 1->a, 2->b, 3->c, 4->d, 5->e (in order)
tree.Put(6, "f") // 1->a, 2->b, 3->c, 4->d, 5->e, 6->f (in order)
fmt.Println(tree)
//
// AVLTree
// │ ┌── 6
// │ ┌── 5
// └── 4
// │ ┌── 3
// └── 2
// └── 1
_ = tree.Values() // []interface {}{"a", "b", "c", "d", "e", "f"} (in order)
_ = tree.Keys() // []interface {}{1, 2, 3, 4, 5, 6} (in order)
tree.Remove(2) // 1->a, 3->c, 4->d, 5->e, 6->f (in order)
fmt.Println(tree)
//
// AVLTree
// │ ┌── 6
// │ ┌── 5
// └── 4
// └── 3
// └── 1
tree.Clear() // empty
tree.Empty() // true
tree.Size() // 0
}
================================================
FILE: examples/binaryheap/binaryheap.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"cmp"
"github.com/emirpasic/gods/v2/trees/binaryheap"
)
// BinaryHeapExample to demonstrate basic usage of BinaryHeap
func main() {
// Min-heap
heap := binaryheap.New[int]() // empty (min-heap)
heap.Push(2) // 2
heap.Push(3) // 2, 3
heap.Push(1) // 1, 3, 2
heap.Values() // 1, 3, 2
_, _ = heap.Peek() // 1,true
_, _ = heap.Pop() // 1, true
_, _ = heap.Pop() // 2, true
_, _ = heap.Pop() // 3, true
_, _ = heap.Pop() // nil, false (nothing to pop)
heap.Push(1) // 1
heap.Clear() // empty
heap.Empty() // true
heap.Size() // 0
// Max-heap
inverseIntComparator := func(a, b int) int {
return -cmp.Compare(a, b)
}
heap = binaryheap.NewWith(inverseIntComparator) // empty (min-heap)
heap.Push(2) // 2
heap.Push(3) // 3, 2
heap.Push(1) // 3, 2, 1
heap.Values() // 3, 2, 1
}
================================================
FILE: examples/btree/btree.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"github.com/emirpasic/gods/v2/trees/btree"
)
// BTreeExample to demonstrate basic usage of BTree
func main() {
tree := btree.New[int, string](3) // empty (keys are of type int)
tree.Put(1, "x") // 1->x
tree.Put(2, "b") // 1->x, 2->b (in order)
tree.Put(1, "a") // 1->a, 2->b (in order, replacement)
tree.Put(3, "c") // 1->a, 2->b, 3->c (in order)
tree.Put(4, "d") // 1->a, 2->b, 3->c, 4->d (in order)
tree.Put(5, "e") // 1->a, 2->b, 3->c, 4->d, 5->e (in order)
tree.Put(6, "f") // 1->a, 2->b, 3->c, 4->d, 5->e, 6->f (in order)
tree.Put(7, "g") // 1->a, 2->b, 3->c, 4->d, 5->e, 6->f, 7->g (in order)
fmt.Println(tree)
// BTree
// 1
// 2
// 3
// 4
// 5
// 6
// 7
_ = tree.Values() // []interface {}{"a", "b", "c", "d", "e", "f", "g"} (in order)
_ = tree.Keys() // []interface {}{1, 2, 3, 4, 5, 6, 7} (in order)
tree.Remove(2) // 1->a, 3->c, 4->d, 5->e, 6->f (in order)
fmt.Println(tree)
// BTree
// 1
// 3
// 4
// 5
// 6
tree.Clear() // empty
tree.Empty() // true
tree.Size() // 0
// Other:
tree.Height() // gets the height of the tree
tree.Left() // gets the left-most (min) node
tree.LeftKey() // get the left-most (min) node's key
tree.LeftValue() // get the left-most (min) node's value
tree.Right() // get the right-most (max) node
tree.RightKey() // get the right-most (max) node's key
tree.RightValue() // get the right-most (max) node's value
}
================================================
FILE: examples/circularbuffer/circularbuffer.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import cb "github.com/emirpasic/gods/v2/queues/circularbuffer"
// CircularBufferExample to demonstrate basic usage of CircularBuffer
func main() {
queue := cb.New[int](3) // empty (max size is 3)
queue.Enqueue(1) // 1
queue.Enqueue(2) // 1, 2
queue.Enqueue(3) // 1, 2, 3
_ = queue.Values() // 1, 2, 3
queue.Enqueue(3) // 4, 2, 3
_, _ = queue.Peek() // 4,true
_, _ = queue.Dequeue() // 4, true
_, _ = queue.Dequeue() // 2, true
_, _ = queue.Dequeue() // 3, true
_, _ = queue.Dequeue() // nil, false (nothing to deque)
queue.Enqueue(1) // 1
queue.Clear() // empty
queue.Empty() // true
_ = queue.Size() // 0
}
================================================
FILE: examples/customcomparator/customcomparator.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"github.com/emirpasic/gods/v2/sets/treeset"
)
// User model (id and name)
type User struct {
id int
name string
}
// Comparator function (sort by IDs)
func byID(a, b User) int {
switch {
case a.id > b.id:
return 1
case a.id < b.id:
return -1
default:
return 0
}
}
// CustomComparatorExample to demonstrate basic usage of CustomComparator
func main() {
set := treeset.NewWith(byID)
set.Add(User{2, "Second"})
set.Add(User{3, "Third"})
set.Add(User{1, "First"})
set.Add(User{4, "Fourth"})
fmt.Println(set) // {1 First}, {2 Second}, {3 Third}, {4 Fourth}
}
================================================
FILE: examples/doublylinkedlist/doublylinkedlist.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"cmp"
dll "github.com/emirpasic/gods/v2/lists/doublylinkedlist"
)
// DoublyLinkedListExample to demonstrate basic usage of DoublyLinkedList
func main() {
list := dll.New[string]()
list.Add("a") // ["a"]
list.Append("b") // ["a","b"] (same as Add())
list.Prepend("c") // ["c","a","b"]
list.Sort(cmp.Compare[string]) // ["a","b","c"]
_, _ = list.Get(0) // "a",true
_, _ = list.Get(100) // nil,false
_ = list.Contains("a", "b", "c") // true
_ = list.Contains("a", "b", "c", "d") // false
list.Remove(2) // ["a","b"]
list.Remove(1) // ["a"]
list.Remove(0) // []
list.Remove(0) // [] (ignored)
_ = list.Empty() // true
_ = list.Size() // 0
list.Add("a") // ["a"]
list.Clear() // []
}
================================================
FILE: examples/enumerablewithindex/enumerablewithindex.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"github.com/emirpasic/gods/v2/sets/treeset"
)
func printSet(txt string, set *treeset.Set[int]) {
fmt.Print(txt, "[ ")
set.Each(func(index int, value int) {
fmt.Print(value, " ")
})
fmt.Println("]")
}
// EnumerableWithIndexExample to demonstrate basic usage of EnumerableWithIndex
func main() {
set := treeset.New[int]()
set.Add(2, 3, 4, 2, 5, 6, 7, 8)
printSet("Initial", set) // [ 2 3 4 5 6 7 8 ]
even := set.Select(func(index int, value int) bool {
return value%2 == 0
})
printSet("Even numbers", even) // [ 2 4 6 8 ]
foundIndex, foundValue := set.Find(func(index int, value int) bool {
return value%2 == 0 && value%3 == 0
})
if foundIndex != -1 {
fmt.Println("Number divisible by 2 and 3 found is", foundValue, "at index", foundIndex) // value: 6, index: 4
}
square := set.Map(func(index int, value int) int {
return value * value
})
printSet("Numbers squared", square) // [ 4 9 16 25 36 49 64 ]
bigger := set.Any(func(index int, value int) bool {
return value > 5
})
fmt.Println("Set contains a number bigger than 5 is ", bigger) // true
positive := set.All(func(index int, value int) bool {
return value > 0
})
fmt.Println("All numbers are positive is", positive) // true
evenNumbersSquared := set.Select(func(index int, value int) bool {
return value%2 == 0
}).Map(func(index int, value int) int {
return value * value
})
printSet("Chaining", evenNumbersSquared) // [ 4 16 36 64 ]
}
================================================
FILE: examples/enumerablewithkey/enumerablewithkey.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"github.com/emirpasic/gods/v2/maps/treemap"
)
func printMap(txt string, m *treemap.Map[string, int]) {
fmt.Print(txt, " { ")
m.Each(func(key string, value int) {
fmt.Print(key, ":", value, " ")
})
fmt.Println("}")
}
// EunumerableWithKeyExample to demonstrate basic usage of EunumerableWithKey
func main() {
m := treemap.New[string, int]()
m.Put("g", 7)
m.Put("f", 6)
m.Put("e", 5)
m.Put("d", 4)
m.Put("c", 3)
m.Put("b", 2)
m.Put("a", 1)
printMap("Initial", m) // { a:1 b:2 c:3 d:4 e:5 f:6 g:7 }
even := m.Select(func(key string, value int) bool {
return value%2 == 0
})
printMap("Elements with even values", even) // { b:2 d:4 f:6 }
foundKey, foundValue := m.Find(func(key string, value int) bool {
return value%2 == 0 && value%3 == 0
})
if foundKey != "" {
fmt.Println("Element with value divisible by 2 and 3 found is", foundValue, "with key", foundKey) // value: 6, index: 4
}
square := m.Map(func(key string, value int) (string, int) {
return key + key, value * value
})
printMap("Elements' values squared and letters duplicated", square) // { aa:1 bb:4 cc:9 dd:16 ee:25 ff:36 gg:49 }
bigger := m.Any(func(key string, value int) bool {
return value > 5
})
fmt.Println("Map contains element whose value is bigger than 5 is", bigger) // true
positive := m.All(func(key string, value int) bool {
return value > 0
})
fmt.Println("All map's elements have positive values is", positive) // true
evenNumbersSquared := m.Select(func(key string, value int) bool {
return value%2 == 0
}).Map(func(key string, value int) (string, int) {
return key, value * value
})
printMap("Chaining", evenNumbersSquared) // { b:4 d:16 f:36 }
}
================================================
FILE: examples/hashbidimap/hashbidimap.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import "github.com/emirpasic/gods/v2/maps/hashbidimap"
// HashBidiMapExample to demonstrate basic usage of HashMap
func main() {
m := hashbidimap.New[int, string]() // empty
m.Put(1, "x") // 1->x
m.Put(3, "b") // 1->x, 3->b (random order)
m.Put(1, "a") // 1->a, 3->b (random order)
m.Put(2, "b") // 1->a, 2->b (random order)
_, _ = m.GetKey("a") // 1, true
_, _ = m.Get(2) // b, true
_, _ = m.Get(3) // nil, false
_ = m.Values() // []interface {}{"a", "b"} (random order)
_ = m.Keys() // []interface {}{1, 2} (random order)
m.Remove(1) // 2->b
m.Clear() // empty
m.Empty() // true
m.Size() // 0
}
================================================
FILE: examples/hashmap/hashmap.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import "github.com/emirpasic/gods/v2/maps/hashmap"
// HashMapExample to demonstrate basic usage of HashMap
func main() {
m := hashmap.New[int, string]() // empty
m.Put(1, "x") // 1->x
m.Put(2, "b") // 2->b, 1->x (random order)
m.Put(1, "a") // 2->b, 1->a (random order)
_, _ = m.Get(2) // b, true
_, _ = m.Get(3) // nil, false
_ = m.Values() // []interface {}{"b", "a"} (random order)
_ = m.Keys() // []interface {}{1, 2} (random order)
m.Remove(1) // 2->b
m.Clear() // empty
m.Empty() // true
m.Size() // 0
}
================================================
FILE: examples/hashset/hashset.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import "github.com/emirpasic/gods/v2/sets/hashset"
// HashSetExample to demonstrate basic usage of HashSet
func main() {
set := hashset.New[int]() // empty (keys are of type int)
set.Add(1) // 1
set.Add(2, 2, 3, 4, 5) // 3, 1, 2, 4, 5 (random order, duplicates ignored)
set.Remove(4) // 5, 3, 2, 1 (random order)
set.Remove(2, 3) // 1, 5 (random order)
set.Contains(1) // true
set.Contains(1, 5) // true
set.Contains(1, 6) // false
_ = set.Values() // []int{5,1} (random order)
set.Clear() // empty
set.Empty() // true
set.Size() // 0
}
================================================
FILE: examples/iteratorwithindex/iteratorwithindex.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"strings"
"github.com/emirpasic/gods/v2/sets/treeset"
)
// IteratorWithIndexExample to demonstrate basic usage of IteratorWithIndex
func main() {
set := treeset.New[string]()
set.Add("a", "b", "c")
it := set.Iterator()
fmt.Print("\nForward iteration\n")
for it.Next() {
index, value := it.Index(), it.Value()
fmt.Print("[", index, ":", value, "]") // [0:a][1:b][2:c]
}
fmt.Print("\nForward iteration (again)\n")
for it.Begin(); it.Next(); {
index, value := it.Index(), it.Value()
fmt.Print("[", index, ":", value, "]") // [0:a][1:b][2:c]
}
fmt.Print("\nBackward iteration\n")
for it.Prev() {
index, value := it.Index(), it.Value()
fmt.Print("[", index, ":", value, "]") // [2:c][1:b][0:a]
}
fmt.Print("\nBackward iteration (again)\n")
for it.End(); it.Prev(); {
index, value := it.Index(), it.Value()
fmt.Print("[", index, ":", value, "]") // [2:c][1:b][0:a]
}
if it.First() {
fmt.Print("\nFirst index: ", it.Index()) // First index: 0
fmt.Print("\nFirst value: ", it.Value()) // First value: a
}
if it.Last() {
fmt.Print("\nLast index: ", it.Index()) // Last index: 3
fmt.Print("\nLast value: ", it.Value()) // Last value: c
}
// Seek element starting with "b"
seek := func(index int, value string) bool {
return strings.HasSuffix(value, "b")
}
it.Begin()
for found := it.NextTo(seek); found; found = it.Next() {
fmt.Print("\nNextTo index: ", it.Index())
fmt.Print("\nNextTo value: ", it.Value())
} /*
NextTo index: 1
NextTo value: "b"
NextTo index: 2
NextTo value: "c"
*/
it.End()
for found := it.PrevTo(seek); found; found = it.Prev() {
fmt.Print("\nNextTo index: ", it.Index())
fmt.Print("\nNextTo value: ", it.Value())
} /*
NextTo index: 1
NextTo value: "b"
NextTo index: 0
NextTo value: "a"
*/
}
================================================
FILE: examples/iteratorwithkey/iteratorwithkey.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"strings"
"github.com/emirpasic/gods/v2/maps/treemap"
)
// IteratorWithKeyExample to demonstrate basic usage of IteratorWithKey
func main() {
m := treemap.New[int, string]()
m.Put(0, "a")
m.Put(1, "b")
m.Put(2, "c")
it := m.Iterator()
fmt.Print("\nForward iteration\n")
for it.Next() {
key, value := it.Key(), it.Value()
fmt.Print("[", key, ":", value, "]") // [0:a][1:b][2:c]
}
fmt.Print("\nForward iteration (again)\n")
for it.Begin(); it.Next(); {
key, value := it.Key(), it.Value()
fmt.Print("[", key, ":", value, "]") // [0:a][1:b][2:c]
}
fmt.Print("\nBackward iteration\n")
for it.Prev() {
key, value := it.Key(), it.Value()
fmt.Print("[", key, ":", value, "]") // [2:c][1:b][0:a]
}
fmt.Print("\nBackward iteration (again)\n")
for it.End(); it.Prev(); {
key, value := it.Key(), it.Value()
fmt.Print("[", key, ":", value, "]") // [2:c][1:b][0:a]
}
if it.First() {
fmt.Print("\nFirst key: ", it.Key()) // First key: 0
fmt.Print("\nFirst value: ", it.Value()) // First value: a
}
if it.Last() {
fmt.Print("\nLast key: ", it.Key()) // Last key: 2
fmt.Print("\nLast value: ", it.Value()) // Last value: c
}
// Seek key-value pair whose value starts with "b"
seek := func(key int, value string) bool {
return strings.HasSuffix(value, "b")
}
it.Begin()
for found := it.NextTo(seek); found; found = it.Next() {
fmt.Print("\nNextTo key: ", it.Key())
fmt.Print("\nNextTo value: ", it.Value())
} /*
NextTo key: 1
NextTo value: "b"
NextTo key: 2
NextTo value: "c"
*/
it.End()
for found := it.PrevTo(seek); found; found = it.Prev() {
fmt.Print("\nNextTo key: ", it.Key())
fmt.Print("\nNextTo value: ", it.Value())
} /*
NextTo key: 1
NextTo value: "b"
NextTo key: 0
NextTo value: "a"
*/
}
================================================
FILE: examples/linkedhashmap/linkedhashmap.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import "github.com/emirpasic/gods/v2/maps/linkedhashmap"
// LinkedHashMapExample to demonstrate basic usage of LinkedHashMapExample
func main() {
m := linkedhashmap.New[int, string]() // empty (keys are of type int)
m.Put(2, "b") // 2->b
m.Put(1, "x") // 2->b, 1->x (insertion-order)
m.Put(1, "a") // 2->b, 1->a (insertion-order)
_, _ = m.Get(2) // b, true
_, _ = m.Get(3) // nil, false
_ = m.Values() // []interface {}{"b", "a"} (insertion-order)
_ = m.Keys() // []interface {}{2, 1} (insertion-order)
m.Remove(1) // 2->b
m.Clear() // empty
m.Empty() // true
m.Size() // 0
}
================================================
FILE: examples/linkedhashset/linkedhashset.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import "github.com/emirpasic/gods/v2/sets/linkedhashset"
// LinkedHashSetExample to demonstrate basic usage of LinkedHashSet
func main() {
set := linkedhashset.New[int]() // empty
set.Add(5) // 5
set.Add(4, 4, 3, 2, 1) // 5, 4, 3, 2, 1 (in insertion-order, duplicates ignored)
set.Remove(4) // 5, 3, 2, 1 (in insertion-order)
set.Remove(2, 3) // 5, 1 (in insertion-order)
set.Contains(1) // true
set.Contains(1, 5) // true
set.Contains(1, 6) // false
_ = set.Values() // []int{5, 1} (in insertion-order)
set.Clear() // empty
set.Empty() // true
set.Size() // 0
}
================================================
FILE: examples/linkedlistqueue/linkedlistqueue.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import llq "github.com/emirpasic/gods/v2/queues/linkedlistqueue"
// LinkedListQueueExample to demonstrate basic usage of LinkedListQueue
func main() {
queue := llq.New[int]() // empty
queue.Enqueue(1) // 1
queue.Enqueue(2) // 1, 2
_ = queue.Values() // 1, 2 (FIFO order)
_, _ = queue.Peek() // 1,true
_, _ = queue.Dequeue() // 1, true
_, _ = queue.Dequeue() // 2, true
_, _ = queue.Dequeue() // nil, false (nothing to deque)
queue.Enqueue(1) // 1
queue.Clear() // empty
queue.Empty() // true
_ = queue.Size() // 0
}
================================================
FILE: examples/linkedliststack/linkedliststack.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import lls "github.com/emirpasic/gods/v2/stacks/linkedliststack"
// LinkedListStackExample to demonstrate basic usage of LinkedListStack
func main() {
stack := lls.New[int]() // empty
stack.Push(1) // 1
stack.Push(2) // 1, 2
stack.Values() // 2, 1 (LIFO order)
_, _ = stack.Peek() // 2,true
_, _ = stack.Pop() // 2, true
_, _ = stack.Pop() // 1, true
_, _ = stack.Pop() // nil, false (nothing to pop)
stack.Push(1) // 1
stack.Clear() // empty
stack.Empty() // true
stack.Size() // 0
}
================================================
FILE: examples/priorityqueue/priorityqueue.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"cmp"
pq "github.com/emirpasic/gods/v2/queues/priorityqueue"
)
// Element is an entry in the priority queue
type Element struct {
name string
priority int
}
// Comparator function (sort by element's priority value in descending order)
func byPriority(a, b Element) int {
return -cmp.Compare(a.priority, b.priority) // "-" descending order
}
// PriorityQueueExample to demonstrate basic usage of BinaryHeap
func main() {
a := Element{name: "a", priority: 1}
b := Element{name: "b", priority: 2}
c := Element{name: "c", priority: 3}
queue := pq.NewWith(byPriority) // empty
queue.Enqueue(a) // {a 1}
queue.Enqueue(c) // {c 3}, {a 1}
queue.Enqueue(b) // {c 3}, {b 2}, {a 1}
_ = queue.Values() // [{c 3} {b 2} {a 1}]
_, _ = queue.Peek() // {c 3} true
_, _ = queue.Dequeue() // {c 3} true
_, _ = queue.Dequeue() // {b 2} true
_, _ = queue.Dequeue() // {a 1} true
_, _ = queue.Dequeue() // <nil> false (nothing to dequeue)
queue.Clear() // empty
_ = queue.Empty() // true
_ = queue.Size() // 0
}
================================================
FILE: examples/redblacktree/redblacktree.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
rbt "github.com/emirpasic/gods/v2/trees/redblacktree"
)
// RedBlackTreeExample to demonstrate basic usage of RedBlackTree
func main() {
tree := rbt.New[int, string]() // empty(keys are of type int)
tree.Put(1, "x") // 1->x
tree.Put(2, "b") // 1->x, 2->b (in order)
tree.Put(1, "a") // 1->a, 2->b (in order, replacement)
tree.Put(3, "c") // 1->a, 2->b, 3->c (in order)
tree.Put(4, "d") // 1->a, 2->b, 3->c, 4->d (in order)
tree.Put(5, "e") // 1->a, 2->b, 3->c, 4->d, 5->e (in order)
tree.Put(6, "f") // 1->a, 2->b, 3->c, 4->d, 5->e, 6->f (in order)
fmt.Println(tree)
//
// RedBlackTree
// │ ┌── 6
// │ ┌── 5
// │ ┌── 4
// │ │ └── 3
// └── 2
// └── 1
_ = tree.Values() // []interface {}{"a", "b", "c", "d", "e", "f"} (in order)
_ = tree.Keys() // []interface {}{1, 2, 3, 4, 5, 6} (in order)
tree.Remove(2) // 1->a, 3->c, 4->d, 5->e, 6->f (in order)
fmt.Println(tree)
//
// RedBlackTree
// │ ┌── 6
// │ ┌── 5
// └── 4
// │ ┌── 3
// └── 1
tree.Clear() // empty
tree.Empty() // true
tree.Size() // 0
}
================================================
FILE: examples/redblacktreeextended/redblacktreeextended.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package redblacktreeextended
import (
"fmt"
rbt "github.com/emirpasic/gods/v2/trees/redblacktree"
)
// RedBlackTreeExtended to demonstrate how to extend a RedBlackTree to include new functions
type RedBlackTreeExtended[K comparable, V any] struct {
*rbt.Tree[K, V]
}
// GetMin gets the min value and flag if found
func (tree *RedBlackTreeExtended[K, V]) GetMin() (value V, found bool) {
node, found := tree.getMinFromNode(tree.Root)
if found {
return node.Value, found
}
return value, false
}
// GetMax gets the max value and flag if found
func (tree *RedBlackTreeExtended[K, V]) GetMax() (value V, found bool) {
node, found := tree.getMaxFromNode(tree.Root)
if found {
return node.Value, found
}
return value, false
}
// RemoveMin removes the min value and flag if found
func (tree *RedBlackTreeExtended[K, V]) RemoveMin() (value V, deleted bool) {
node, found := tree.getMinFromNode(tree.Root)
if found {
tree.Remove(node.Key)
return node.Value, found
}
return value, false
}
// RemoveMax removes the max value and flag if found
func (tree *RedBlackTreeExtended[K, V]) RemoveMax() (value V, deleted bool) {
node, found := tree.getMaxFromNode(tree.Root)
if found {
tree.Remove(node.Key)
return node.Value, found
}
return value, false
}
func (tree *RedBlackTreeExtended[K, V]) getMinFromNode(node *rbt.Node[K, V]) (foundNode *rbt.Node[K, V], found bool) {
if node == nil {
return nil, false
}
if node.Left == nil {
return node, true
}
return tree.getMinFromNode(node.Left)
}
func (tree *RedBlackTreeExtended[K, V]) getMaxFromNode(node *rbt.Node[K, V]) (foundNode *rbt.Node[K, V], found bool) {
if node == nil {
return nil, false
}
if node.Right == nil {
return node, true
}
return tree.getMaxFromNode(node.Right)
}
func print(tree *RedBlackTreeExtended[int, string]) {
max, _ := tree.GetMax()
min, _ := tree.GetMin()
fmt.Printf("Value for max key: %v \n", max)
fmt.Printf("Value for min key: %v \n", min)
fmt.Println(tree)
}
// RedBlackTreeExtendedExample main method on how to use the custom red-black tree above
func main() {
tree := RedBlackTreeExtended[int, string]{rbt.New[int, string]()}
tree.Put(1, "a") // 1->x (in order)
tree.Put(2, "b") // 1->x, 2->b (in order)
tree.Put(3, "c") // 1->x, 2->b, 3->c (in order)
tree.Put(4, "d") // 1->x, 2->b, 3->c, 4->d (in order)
tree.Put(5, "e") // 1->x, 2->b, 3->c, 4->d, 5->e (in order)
print(&tree)
// Value for max key: e
// Value for min key: a
// RedBlackTree
// │ ┌── 5
// │ ┌── 4
// │ │ └── 3
// └── 2
// └── 1
tree.RemoveMin() // 2->b, 3->c, 4->d, 5->e (in order)
tree.RemoveMax() // 2->b, 3->c, 4->d (in order)
tree.RemoveMin() // 3->c, 4->d (in order)
tree.RemoveMax() // 3->c (in order)
print(&tree)
// Value for max key: c
// Value for min key: c
// RedBlackTree
// └── 3
}
================================================
FILE: examples/serialization/serialization.go
================================================
package serialization
import (
"fmt"
"github.com/emirpasic/gods/v2/lists/arraylist"
"github.com/emirpasic/gods/v2/maps/hashmap"
)
// ListSerializationExample demonstrates how to serialize and deserialize lists to and from JSON
func ListSerializationExample() {
list := arraylist.New[string]()
list.Add("a", "b", "c")
// Serialization (marshalling)
json, err := list.ToJSON()
if err != nil {
fmt.Println(err)
}
fmt.Println(string(json)) // ["a","b","c"]
// Deserialization (unmarshalling)
json = []byte(`["a","b"]`)
err = list.FromJSON(json)
if err != nil {
fmt.Println(err)
}
fmt.Println(list) // ArrayList ["a","b"]
}
// MapSerializationExample demonstrates how to serialize and deserialize maps to and from JSON
func MapSerializationExample() {
m := hashmap.New[string, string]()
m.Put("a", "1")
m.Put("b", "2")
m.Put("c", "3")
// Serialization (marshalling)
json, err := m.ToJSON()
if err != nil {
fmt.Println(err)
}
fmt.Println(string(json)) // {"a":"1","b":"2","c":"3"}
// Deserialization (unmarshalling)
json = []byte(`{"a":"1","b":"2"}`)
err = m.FromJSON(json)
if err != nil {
fmt.Println(err)
}
fmt.Println(m) // HashMap {"a":"1","b":"2"}
}
================================================
FILE: examples/singlylinkedlist/singlylinkedlist.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"cmp"
sll "github.com/emirpasic/gods/v2/lists/singlylinkedlist"
)
// SinglyLinkedListExample to demonstrate basic usage of SinglyLinkedList
func main() {
list := sll.New[string]()
list.Add("a") // ["a"]
list.Append("b") // ["a","b"] (same as Add())
list.Prepend("c") // ["c","a","b"]
list.Sort(cmp.Compare[string]) // ["a","b","c"]
_, _ = list.Get(0) // "a",true
_, _ = list.Get(100) // nil,false
_ = list.Contains("a", "b", "c") // true
_ = list.Contains("a", "b", "c", "d") // false
list.Remove(2) // ["a","b"]
list.Remove(1) // ["a"]
list.Remove(0) // []
list.Remove(0) // [] (ignored)
_ = list.Empty() // true
_ = list.Size() // 0
list.Add("a") // ["a"]
list.Clear() // []
}
================================================
FILE: examples/treebidimap/treebidimap.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"github.com/emirpasic/gods/v2/maps/treebidimap"
)
// TreeBidiMapExample to demonstrate basic usage of TreeBidiMap
func main() {
m := treebidimap.New[int, string]()
m.Put(1, "x") // 1->x
m.Put(3, "b") // 1->x, 3->b (ordered)
m.Put(1, "a") // 1->a, 3->b (ordered)
m.Put(2, "b") // 1->a, 2->b (ordered)
_, _ = m.GetKey("a") // 1, true
_, _ = m.Get(2) // b, true
_, _ = m.Get(3) // nil, false
_ = m.Values() // []interface {}{"a", "b"} (ordered)
_ = m.Keys() // []interface {}{1, 2} (ordered)
m.Remove(1) // 2->b
m.Clear() // empty
m.Empty() // true
m.Size() // 0
}
================================================
FILE: examples/treemap/treemap.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import "github.com/emirpasic/gods/v2/maps/treemap"
// TreeMapExample to demonstrate basic usage of TreeMap
func main() {
m := treemap.New[int, string]() // empty
m.Put(1, "x") // 1->x
m.Put(2, "b") // 1->x, 2->b (in order)
m.Put(1, "a") // 1->a, 2->b (in order)
_, _ = m.Get(2) // b, true
_, _ = m.Get(3) // nil, false
_ = m.Values() // []interface {}{"a", "b"} (in order)
_ = m.Keys() // []interface {}{1, 2} (in order)
m.Remove(1) // 2->b
m.Clear() // empty
m.Empty() // true
m.Size() // 0
}
================================================
FILE: examples/treeset/treeset.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import "github.com/emirpasic/gods/v2/sets/treeset"
// TreeSetExample to demonstrate basic usage of TreeSet
func main() {
set := treeset.New[int]() // empty
set.Add(1) // 1
set.Add(2, 2, 3, 4, 5) // 1, 2, 3, 4, 5 (in order, duplicates ignored)
set.Remove(4) // 1, 2, 3, 5 (in order)
set.Remove(2, 3) // 1, 5 (in order)
set.Contains(1) // true
set.Contains(1, 5) // true
set.Contains(1, 6) // false
_ = set.Values() // []int{1,5} (in order)
set.Clear() // empty
set.Empty() // true
set.Size() // 0
}
================================================
FILE: go.mod
================================================
module github.com/emirpasic/gods/v2
go 1.21
================================================
FILE: go.sum
================================================
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
================================================
FILE: lists/arraylist/arraylist.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package arraylist implements the array list.
//
// Structure is not thread safe.
//
// Reference: https://en.wikipedia.org/wiki/List_%28abstract_data_type%29
package arraylist
import (
"fmt"
"slices"
"strings"
"github.com/emirpasic/gods/v2/lists"
"github.com/emirpasic/gods/v2/utils"
)
// Assert List implementation
var _ lists.List[int] = (*List[int])(nil)
// List holds the elements in a slice
type List[T comparable] struct {
elements []T
}
const (
growthFactor = float32(2.0) // growth by 100%
shrinkFactor = float32(0.25) // shrink when size is 25% of capacity (0 means never shrink)
)
// New instantiates a new list and adds the passed values, if any, to the list
func New[T comparable](values ...T) *List[T] {
list := &List[T]{}
if len(values) > 0 {
list.Add(values...)
}
return list
}
// Add appends a value at the end of the list
func (list *List[T]) Add(values ...T) {
l := len(list.elements)
list.growBy(len(values))
for i := range values {
list.elements[l+i] = values[i]
}
}
// Get returns the element at index.
// Second return parameter is true if index is within bounds of the array and array is not empty, otherwise false.
func (list *List[T]) Get(index int) (T, bool) {
if !list.withinRange(index) {
var t T
return t, false
}
return list.elements[index], true
}
// Remove removes the element at the given index from the list.
func (list *List[T]) Remove(index int) {
if !list.withinRange(index) {
return
}
list.elements = slices.Delete(list.elements, index, index+1)
list.shrink()
}
// Contains checks if elements (one or more) are present in the set.
// All elements have to be present in the set for the method to return true.
// Performance time complexity of n^2.
// Returns true if no arguments are passed at all, i.e. set is always super-set of empty set.
func (list *List[T]) Contains(values ...T) bool {
for _, searchValue := range values {
if !slices.Contains(list.elements, searchValue) {
return false
}
}
return true
}
// Values returns all elements in the list.
func (list *List[T]) Values() []T {
return slices.Clone(list.elements)
}
// IndexOf returns index of provided element
func (list *List[T]) IndexOf(value T) int {
return slices.Index(list.elements, value)
}
// Empty returns true if list does not contain any elements.
func (list *List[T]) Empty() bool {
return len(list.elements) == 0
}
// Size returns number of elements within the list.
func (list *List[T]) Size() int {
return len(list.elements)
}
// Clear removes all elements from the list.
func (list *List[T]) Clear() {
clear(list.elements[:cap(list.elements)])
list.elements = list.elements[:0]
}
// Sort sorts values (in-place) using.
func (list *List[T]) Sort(comparator utils.Comparator[T]) {
if len(list.elements) < 2 {
return
}
slices.SortFunc(list.elements, comparator)
}
// Swap swaps the two values at the specified positions.
func (list *List[T]) Swap(i, j int) {
if list.withinRange(i) && list.withinRange(j) {
list.elements[i], list.elements[j] = list.elements[j], list.elements[i]
}
}
// Insert inserts values at specified index position shifting the value at that position (if any) and any subsequent elements to the right.
// Does not do anything if position is negative or bigger than list's size
// Note: position equal to list's size is valid, i.e. append.
func (list *List[T]) Insert(index int, values ...T) {
if !list.withinRange(index) {
// Append
if index == len(list.elements) {
list.Add(values...)
}
return
}
l := len(list.elements)
list.growBy(len(values))
list.elements = slices.Insert(list.elements[:l], index, values...)
}
// Set the value at specified index
// Does not do anything if position is negative or bigger than list's size
// Note: position equal to list's size is valid, i.e. append.
func (list *List[T]) Set(index int, value T) {
if !list.withinRange(index) {
// Append
if index == len(list.elements) {
list.Add(value)
}
return
}
list.elements[index] = value
}
// String returns a string representation of container
func (list *List[T]) String() string {
str := "ArrayList\n"
values := make([]string, 0, len(list.elements))
for _, value := range list.elements {
values = append(values, fmt.Sprintf("%v", value))
}
str += strings.Join(values, ", ")
return str
}
// Check that the index is within bounds of the list
func (list *List[T]) withinRange(index int) bool {
return index >= 0 && index < len(list.elements)
}
func (list *List[T]) resize(len, cap int) {
newElements := make([]T, len, cap)
copy(newElements, list.elements)
list.elements = newElements
}
// Expand the array if necessary, i.e. capacity will be reached if we add n elements
func (list *List[T]) growBy(n int) {
// When capacity is reached, grow by a factor of growthFactor and add number of elements
currentCapacity := cap(list.elements)
if newLength := len(list.elements) + n; newLength >= currentCapacity {
newCapacity := int(growthFactor * float32(currentCapacity+n))
list.resize(newLength, newCapacity)
} else {
list.elements = list.elements[:newLength]
}
}
// Shrink the array if necessary, i.e. when size is shrinkFactor percent of current capacity
func (list *List[T]) shrink() {
if shrinkFactor == 0.0 {
return
}
// Shrink when size is at shrinkFactor * capacity
currentCapacity := cap(list.elements)
if len(list.elements) <= int(float32(currentCapacity)*shrinkFactor) {
list.resize(len(list.elements), len(list.elements))
}
}
================================================
FILE: lists/arraylist/arraylist_test.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package arraylist
import (
"cmp"
"encoding/json"
"slices"
"strings"
"testing"
)
func TestListNew(t *testing.T) {
list1 := New[int]()
if actualValue := list1.Empty(); actualValue != true {
t.Errorf("Got %v expected %v", actualValue, true)
}
list2 := New[int](1, 2)
if actualValue := list2.Size(); actualValue != 2 {
t.Errorf("Got %v expected %v", actualValue, 2)
}
if actualValue, ok := list2.Get(0); actualValue != 1 || !ok {
t.Errorf("Got %v expected %v", actualValue, 1)
}
if actualValue, ok := list2.Get(1); actualValue != 2 || !ok {
t.Errorf("Got %v expected %v", actualValue, 2)
}
if actualValue, ok := list2.Get(2); actualValue != 0 || ok {
t.Errorf("Got %v expected %v", actualValue, 0)
}
}
func TestListAdd(t *testing.T) {
list := New[string]()
list.Add("a")
list.Add("b", "c")
if actualValue := list.Empty(); actualValue != false {
t.Errorf("Got %v expected %v", actualValue, false)
}
if actualValue := list.Size(); actualValue != 3 {
t.Errorf("Got %v expected %v", actualValue, 3)
}
if actualValue, ok := list.Get(2); actualValue != "c" || !ok {
t.Errorf("Got %v expected %v", actualValue, "c")
}
}
func TestListIndexOf(t *testing.T) {
list := New[string]()
expectedIndex := -1
if index := list.IndexOf("a"); index != expectedIndex {
t.Errorf("Got %v expected %v", index, expectedIndex)
}
list.Add("a")
list.Add("b", "c")
expectedIndex = 0
if index := list.IndexOf("a"); index != expectedIndex {
t.Errorf("Got %v expected %v", index, expectedIndex)
}
expectedIndex = 1
if index := list.IndexOf("b"); index != expectedIndex {
t.Errorf("Got %v expected %v", index, expectedIndex)
}
expectedIndex = 2
if index := list.IndexOf("c"); index != expectedIndex {
t.Errorf("Got %v expected %v", index, expectedIndex)
}
}
func TestListRemove(t *testing.T) {
list := New[string]()
list.Add("a")
list.Add("b", "c")
list.Remove(2)
if actualValue, ok := list.Get(2); actualValue != "" || ok {
t.Errorf("Got %v expected %v", actualValue, "")
}
list.Remove(1)
list.Remove(0)
list.Remove(0) // no effect
if actualValue := list.Empty(); actualValue != true {
t.Errorf("Got %v expected %v", actualValue, true)
}
if actualValue := list.Size(); actualValue != 0 {
t.Errorf("Got %v expected %v", actualValue, 0)
}
}
func TestListGet(t *testing.T) {
list := New[string]()
list.Add("a")
list.Add("b", "c")
if actualValue, ok := list.Get(0); actualValue != "a" || !ok {
t.Errorf("Got %v expected %v", actualValue, "a")
}
if actualValue, ok := list.Get(1); actualValue != "b" || !ok {
t.Errorf("Got %v expected %v", actualValue, "b")
}
if actualValue, ok := list.Get(2); actualValue != "c" || !ok {
t.Errorf("Got %v expected %v", actualValue, "c")
}
if actualValue, ok := list.Get(3); actualValue != "" || ok {
t.Errorf("Got %v expected %v", actualValue, "")
}
list.Remove(0)
if actualValue, ok := list.Get(0); actualValue != "b" || !ok {
t.Errorf("Got %v expected %v", actualValue, "b")
}
}
func TestListSwap(t *testing.T) {
list := New[string]()
list.Add("a")
list.Add("b", "c")
list.Swap(0, 1)
if actualValue, ok := list.Get(0); actualValue != "b" || !ok {
t.Errorf("Got %v expected %v", actualValue, "b")
}
}
func TestListSort(t *testing.T) {
list := New[string]()
list.Sort(cmp.Compare[string])
list.Add("e", "f", "g", "a", "b", "c", "d")
list.Sort(cmp.Compare[string])
for i := 1; i < list.Size(); i++ {
a, _ := list.Get(i - 1)
b, _ := list.Get(i)
if a > b {
t.Errorf("Not sorted! %s > %s", a, b)
}
}
}
func TestListClear(t *testing.T) {
list := New[string]()
list.Add("e", "f", "g", "a", "b", "c", "d")
list.Clear()
if actualValue := list.Empty(); actualValue != true {
t.Errorf("Got %v expected %v", actualValue, true)
}
if actualValue := list.Size(); actualValue != 0 {
t.Errorf("Got %v expected %v", actualValue, 0)
}
}
func TestListContains(t *testing.T) {
list := New[string]()
list.Add("a")
list.Add("b", "c")
if actualValue := list.Contains("a"); actualValue != true {
t.Errorf("Got %v expected %v", actualValue, true)
}
if actualValue := list.Contains(""); actualValue != false {
t.Errorf("Got %v expected %v", actualValue, false)
}
if actualValue := list.Contains("a", "b", "c"); actualValue != true {
t.Errorf("Got %v expected %v", actualValue, true)
}
if actualValue := list.Contains("a", "b", "c", "d"); actualValue != false {
t.Errorf("Got %v expected %v", actualValue, false)
}
list.Clear()
if actualValue := list.Contains("a"); actualValue != false {
t.Errorf("Got %v expected %v", actualValue, false)
}
if actualValue := list.Contains("a", "b", "c"); actualValue != false {
t.Errorf("Got %v expected %v", actualValue, false)
}
}
func TestListValues(t *testing.T) {
list := New[string]()
list.Add("a")
list.Add("b", "c")
if actualValue, expectedValue := list.Values(), []string{"a", "b", "c"}; !slices.Equal(actualValue, expectedValue) {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
}
func TestListInsert(t *testing.T) {
list := New[string]()
list.Insert(0, "b", "c")
list.Insert(0, "a")
list.Insert(10, "x") // ignore
if actualValue := list.Size(); actualValue != 3 {
t.Errorf("Got %v expected %v", actualValue, 3)
}
list.Insert(3, "d") // append
if actualValue := list.Size(); actualValue != 4 {
t.Errorf("Got %v expected %v", actualValue, 4)
}
if actualValue, expectedValue := strings.Join(list.Values(), ""), "abcd"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
}
func TestListSet(t *testing.T) {
list := New[string]()
list.Set(0, "a")
list.Set(1, "b")
if actualValue := list.Size(); actualValue != 2 {
t.Errorf("Got %v expected %v", actualValue, 2)
}
list.Set(2, "c") // append
if actualValue := list.Size(); actualValue != 3 {
t.Errorf("Got %v expected %v", actualValue, 3)
}
list.Set(4, "d") // ignore
list.Set(1, "bb") // update
if actualValue := list.Size(); actualValue != 3 {
t.Errorf("Got %v expected %v", actualValue, 3)
}
if actualValue, expectedValue := list.Values(), []string{"a", "bb", "c"}; !slices.Equal(actualValue, expectedValue) {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
}
func TestListEach(t *testing.T) {
list := New[string]()
list.Add("a", "b", "c")
list.Each(func(index int, value string) {
switch index {
case 0:
if actualValue, expectedValue := value, "a"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
case 1:
if actualValue, expectedValue := value, "b"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
case 2:
if actualValue, expectedValue := value, "c"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
default:
t.Errorf("Too many")
}
})
}
func TestListMap(t *testing.T) {
list := New[string]()
list.Add("a", "b", "c")
mappedList := list.Map(func(index int, value string) string {
return "mapped: " + value
})
if actualValue, _ := mappedList.Get(0); actualValue != "mapped: a" {
t.Errorf("Got %v expected %v", actualValue, "mapped: a")
}
if actualValue, _ := mappedList.Get(1); actualValue != "mapped: b" {
t.Errorf("Got %v expected %v", actualValue, "mapped: b")
}
if actualValue, _ := mappedList.Get(2); actualValue != "mapped: c" {
t.Errorf("Got %v expected %v", actualValue, "mapped: c")
}
if mappedList.Size() != 3 {
t.Errorf("Got %v expected %v", mappedList.Size(), 3)
}
}
func TestListSelect(t *testing.T) {
list := New[string]()
list.Add("a", "b", "c")
selectedList := list.Select(func(index int, value string) bool {
return value >= "a" && value <= "b"
})
if actualValue, _ := selectedList.Get(0); actualValue != "a" {
t.Errorf("Got %v expected %v", actualValue, "value: a")
}
if actualValue, _ := selectedList.Get(1); actualValue != "b" {
t.Errorf("Got %v expected %v", actualValue, "value: b")
}
if selectedList.Size() != 2 {
t.Errorf("Got %v expected %v", selectedList.Size(), 3)
}
}
func TestListAny(t *testing.T) {
list := New[string]()
list.Add("a", "b", "c")
any := list.Any(func(index int, value string) bool {
return value == "c"
})
if any != true {
t.Errorf("Got %v expected %v", any, true)
}
any = list.Any(func(index int, value string) bool {
return value == "x"
})
if any != false {
t.Errorf("Got %v expected %v", any, false)
}
}
func TestListAll(t *testing.T) {
list := New[string]()
list.Add("a", "b", "c")
all := list.All(func(index int, value string) bool {
return value >= "a" && value <= "c"
})
if all != true {
t.Errorf("Got %v expected %v", all, true)
}
all = list.All(func(index int, value string) bool {
return value >= "a" && value <= "b"
})
if all != false {
t.Errorf("Got %v expected %v", all, false)
}
}
func TestListFind(t *testing.T) {
list := New[string]()
list.Add("a", "b", "c")
foundIndex, foundValue := list.Find(func(index int, value string) bool {
return value == "c"
})
if foundValue != "c" || foundIndex != 2 {
t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, "c", 2)
}
foundIndex, foundValue = list.Find(func(index int, value string) bool {
return value == "x"
})
if foundValue != "" || foundIndex != -1 {
t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, nil, nil)
}
}
func TestListChaining(t *testing.T) {
list := New[string]()
list.Add("a", "b", "c")
chainedList := list.Select(func(index int, value string) bool {
return value > "a"
}).Map(func(index int, value string) string {
return value + value
})
if chainedList.Size() != 2 {
t.Errorf("Got %v expected %v", chainedList.Size(), 2)
}
if actualValue, ok := chainedList.Get(0); actualValue != "bb" || !ok {
t.Errorf("Got %v expected %v", actualValue, "b")
}
if actualValue, ok := chainedList.Get(1); actualValue != "cc" || !ok {
t.Errorf("Got %v expected %v", actualValue, "c")
}
}
func TestListIteratorNextOnEmpty(t *testing.T) {
list := New[string]()
it := list.Iterator()
for it.Next() {
t.Errorf("Shouldn't iterate on empty list")
}
}
func TestListIteratorNext(t *testing.T) {
list := New[string]()
list.Add("a", "b", "c")
it := list.Iterator()
count := 0
for it.Next() {
count++
index := it.Index()
value := it.Value()
switch index {
case 0:
if actualValue, expectedValue := value, "a"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
case 1:
if actualValue, expectedValue := value, "b"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
case 2:
if actualValue, expectedValue := value, "c"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
default:
t.Errorf("Too many")
}
}
if actualValue, expectedValue := count, 3; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
}
func TestListIteratorPrevOnEmpty(t *testing.T) {
list := New[string]()
it := list.Iterator()
for it.Prev() {
t.Errorf("Shouldn't iterate on empty list")
}
}
func TestListIteratorPrev(t *testing.T) {
list := New[string]()
list.Add("a", "b", "c")
it := list.Iterator()
for it.Next() {
}
count := 0
for it.Prev() {
count++
index := it.Index()
value := it.Value()
switch index {
case 0:
if actualValue, expectedValue := value, "a"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
case 1:
if actualValue, expectedValue := value, "b"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
case 2:
if actualValue, expectedValue := value, "c"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
default:
t.Errorf("Too many")
}
}
if actualValue, expectedValue := count, 3; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
}
func TestListIteratorBegin(t *testing.T) {
list := New[string]()
it := list.Iterator()
it.Begin()
list.Add("a", "b", "c")
for it.Next() {
}
it.Begin()
it.Next()
if index, value := it.Index(), it.Value(); index != 0 || value != "a" {
t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a")
}
}
func TestListIteratorEnd(t *testing.T) {
list := New[string]()
it := list.Iterator()
if index := it.Index(); index != -1 {
t.Errorf("Got %v expected %v", index, -1)
}
it.End()
if index := it.Index(); index != 0 {
t.Errorf("Got %v expected %v", index, 0)
}
list.Add("a", "b", "c")
it.End()
if index := it.Index(); index != list.Size() {
t.Errorf("Got %v expected %v", index, list.Size())
}
it.Prev()
if index, value := it.Index(), it.Value(); index != list.Size()-1 || value != "c" {
t.Errorf("Got %v,%v expected %v,%v", index, value, list.Size()-1, "c")
}
}
func TestListIteratorFirst(t *testing.T) {
list := New[string]()
it := list.Iterator()
if actualValue, expectedValue := it.First(), false; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
list.Add("a", "b", "c")
if actualValue, expectedValue := it.First(), true; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
if index, value := it.Index(), it.Value(); index != 0 || value != "a" {
t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a")
}
}
func TestListIteratorLast(t *testing.T) {
list := New[string]()
it := list.Iterator()
if actualValue, expectedValue := it.Last(), false; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
list.Add("a", "b", "c")
if actualValue, expectedValue := it.Last(), true; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
if index, value := it.Index(), it.Value(); index != 2 || value != "c" {
t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "c")
}
}
func TestListIteratorNextTo(t *testing.T) {
// Sample seek function, i.e. string starting with "b"
seek := func(index int, value string) bool {
return strings.HasSuffix(value, "b")
}
// NextTo (empty)
{
list := New[string]()
it := list.Iterator()
for it.NextTo(seek) {
t.Errorf("Shouldn't iterate on empty list")
}
}
// NextTo (not found)
{
list := New[string]()
list.Add("xx", "yy")
it := list.Iterator()
for it.NextTo(seek) {
t.Errorf("Shouldn't iterate on empty list")
}
}
// NextTo (found)
{
list := New[string]()
list.Add("aa", "bb", "cc")
it := list.Iterator()
it.Begin()
if !it.NextTo(seek) {
t.Errorf("Shouldn't iterate on empty list")
}
if index, value := it.Index(), it.Value(); index != 1 || value != "bb" {
t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb")
}
if !it.Next() {
t.Errorf("Should go to first element")
}
if index, value := it.Index(), it.Value(); index != 2 || value != "cc" {
t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "cc")
}
if it.Next() {
t.Errorf("Should not go past last element")
}
}
}
func TestListIteratorPrevTo(t *testing.T) {
// Sample seek function, i.e. string starting with "b"
seek := func(index int, value string) bool {
return strings.HasSuffix(value, "b")
}
// PrevTo (empty)
{
list := New[string]()
it := list.Iterator()
it.End()
for it.PrevTo(seek) {
t.Errorf("Shouldn't iterate on empty list")
}
}
// PrevTo (not found)
{
list := New[string]()
list.Add("xx", "yy")
it := list.Iterator()
it.End()
for it.PrevTo(seek) {
t.Errorf("Shouldn't iterate on empty list")
}
}
// PrevTo (found)
{
list := New[string]()
list.Add("aa", "bb", "cc")
it := list.Iterator()
it.End()
if !it.PrevTo(seek) {
t.Errorf("Shouldn't iterate on empty list")
}
if index, value := it.Index(), it.Value(); index != 1 || value != "bb" {
t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb")
}
if !it.Prev() {
t.Errorf("Should go to first element")
}
if index, value := it.Index(), it.Value(); index != 0 || value != "aa" {
t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "aa")
}
if it.Prev() {
t.Errorf("Should not go before first element")
}
}
}
func TestListSerialization(t *testing.T) {
list := New[string]()
list.Add("a", "b", "c")
var err error
assert := func() {
if actualValue, expectedValue := list.Values(), []string{"a", "b", "c"}; !slices.Equal(actualValue, expectedValue) {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
if actualValue, expectedValue := list.Size(), 3; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
if err != nil {
t.Errorf("Got error %v", err)
}
}
assert()
bytes, err := list.ToJSON()
assert()
err = list.FromJSON(bytes)
assert()
bytes, err = json.Marshal([]any{"a", "b", "c", list})
if err != nil {
t.Errorf("Got error %v", err)
}
err = json.Unmarshal([]byte(`["a","b","c"]`), &list)
if err != nil {
t.Errorf("Got error %v", err)
}
assert()
}
func TestListString(t *testing.T) {
c := New[int]()
c.Add(1)
if !strings.HasPrefix(c.String(), "ArrayList") {
t.Errorf("String should start with container name")
}
}
func benchmarkGet(b *testing.B, list *List[int], size int) {
for i := 0; i < b.N; i++ {
for n := 0; n < size; n++ {
list.Get(n)
}
}
}
func benchmarkAdd(b *testing.B, list *List[int], size int) {
for i := 0; i < b.N; i++ {
for n := 0; n < size; n++ {
list.Add(n)
}
}
}
func benchmarkRemove(b *testing.B, list *List[int], size int) {
for i := 0; i < b.N; i++ {
for n := 0; n < size; n++ {
list.Remove(n)
}
}
}
func BenchmarkArrayListGet100(b *testing.B) {
b.StopTimer()
size := 100
list := New[int]()
for n := 0; n < size; n++ {
list.Add(n)
}
b.StartTimer()
benchmarkGet(b, list, size)
}
func BenchmarkArrayListGet1000(b *testing.B) {
b.StopTimer()
size := 1000
list := New[int]()
for n := 0; n < size; n++ {
list.Add(n)
}
b.StartTimer()
benchmarkGet(b, list, size)
}
func BenchmarkArrayListGet10000(b *testing.B) {
b.StopTimer()
size := 10000
list := New[int]()
for n := 0; n < size; n++ {
list.Add(n)
}
b.StartTimer()
benchmarkGet(b, list, size)
}
func BenchmarkArrayListGet100000(b *testing.B) {
b.StopTimer()
size := 100000
list := New[int]()
for n := 0; n < size; n++ {
list.Add(n)
}
b.StartTimer()
benchmarkGet(b, list, size)
}
func BenchmarkArrayListAdd100(b *testing.B) {
b.StopTimer()
size := 100
list := New[int]()
b.StartTimer()
benchmarkAdd(b, list, size)
}
func BenchmarkArrayListAdd1000(b *testing.B) {
b.StopTimer()
size := 1000
list := New[int]()
for n := 0; n < size; n++ {
list.Add(n)
}
b.StartTimer()
benchmarkAdd(b, list, size)
}
func BenchmarkArrayListAdd10000(b *testing.B) {
b.StopTimer()
size := 10000
list := New[int]()
for n := 0; n < size; n++ {
list.Add(n)
}
b.StartTimer()
benchmarkAdd(b, list, size)
}
func BenchmarkArrayListAdd100000(b *testing.B) {
b.StopTimer()
size := 100000
list := New[int]()
for n := 0; n < size; n++ {
list.Add(n)
}
b.StartTimer()
benchmarkAdd(b, list, size)
}
func BenchmarkArrayListRemove100(b *testing.B) {
b.StopTimer()
size := 100
list := New[int]()
for n := 0; n < size; n++ {
list.Add(n)
}
b.StartTimer()
benchmarkRemove(b, list, size)
}
func BenchmarkArrayListRemove1000(b *testing.B) {
b.StopTimer()
size := 1000
list := New[int]()
for n := 0; n < size; n++ {
list.Add(n)
}
b.StartTimer()
benchmarkRemove(b, list, size)
}
func BenchmarkArrayListRemove10000(b *testing.B) {
b.StopTimer()
size := 10000
list := New[int]()
for n := 0; n < size; n++ {
list.Add(n)
}
b.StartTimer()
benchmarkRemove(b, list, size)
}
func BenchmarkArrayListRemove100000(b *testing.B) {
b.StopTimer()
size := 100000
list := New[int]()
for n := 0; n < size; n++ {
list.Add(n)
}
b.StartTimer()
benchmarkRemove(b, list, size)
}
================================================
FILE: lists/arraylist/enumerable.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package arraylist
import "github.com/emirpasic/gods/v2/containers"
// Assert Enumerable implementation
var _ containers.EnumerableWithIndex[int] = (*List[int])(nil)
// Each calls the given function once for each element, passing that element's index and value.
func (list *List[T]) Each(f func(index int, value T)) {
iterator := list.Iterator()
for iterator.Next() {
f(iterator.Index(), iterator.Value())
}
}
// Map invokes the given function once for each element and returns a
// container containing the values returned by the given function.
func (list *List[T]) Map(f func(index int, value T) T) *List[T] {
newList := &List[T]{}
iterator := list.Iterator()
for iterator.Next() {
newList.Add(f(iterator.Index(), iterator.Value()))
}
return newList
}
// Select returns a new container containing all elements for which the given function returns a true value.
func (list *List[T]) Select(f func(index int, value T) bool) *List[T] {
newList := &List[T]{}
iterator := list.Iterator()
for iterator.Next() {
if f(iterator.Index(), iterator.Value()) {
newList.Add(iterator.Value())
}
}
return newList
}
// Any passes each element of the collection to the given function and
// returns true if the function ever returns true for any element.
func (list *List[T]) Any(f func(index int, value T) bool) bool {
iterator := list.Iterator()
for iterator.Next() {
if f(iterator.Index(), iterator.Value()) {
return true
}
}
return false
}
// All passes each element of the collection to the given function and
// returns true if the function returns true for all elements.
func (list *List[T]) All(f func(index int, value T) bool) bool {
iterator := list.Iterator()
for iterator.Next() {
if !f(iterator.Index(), iterator.Value()) {
return false
}
}
return true
}
// 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.
func (list *List[T]) Find(f func(index int, value T) bool) (int, T) {
iterator := list.Iterator()
for iterator.Next() {
if f(iterator.Index(), iterator.Value()) {
return iterator.Index(), iterator.Value()
}
}
var t T
return -1, t
}
================================================
FILE: lists/arraylist/iterator.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package arraylist
import "github.com/emirpasic/gods/v2/containers"
// Assert Iterator implementation
var _ containers.ReverseIteratorWithIndex[int] = (*Iterator[int])(nil)
// Iterator holding the iterator's state
type Iterator[T comparable] struct {
list *List[T]
index int
}
// Iterator returns a stateful iterator whose values can be fetched by an index.
func (list *List[T]) Iterator() *Iterator[T] {
return &Iterator[T]{list: list, index: -1}
}
// Next moves the iterator to the next element and returns true if there was a next element in the container.
// If Next() returns true, then next element's index and value can be retrieved by Index() and Value().
// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
// Modifies the state of the iterator.
func (iterator *Iterator[T]) Next() bool {
if iterator.index < iterator.list.Size() {
iterator.index++
}
return iterator.list.withinRange(iterator.index)
}
// Prev moves the iterator to the previous element and returns true if there was a previous element in the container.
// If Prev() returns true, then previous element's index and value can be retrieved by Index() and Value().
// Modifies the state of the iterator.
func (iterator *Iterator[T]) Prev() bool {
if iterator.index >= 0 {
iterator.index--
}
return iterator.list.withinRange(iterator.index)
}
// Value returns the current element's value.
// Does not modify the state of the iterator.
func (iterator *Iterator[T]) Value() T {
return iterator.list.elements[iterator.index]
}
// Index returns the current element's index.
// Does not modify the state of the iterator.
func (iterator *Iterator[T]) Index() int {
return iterator.index
}
// Begin resets the iterator to its initial state (one-before-first)
// Call Next() to fetch the first element if any.
func (iterator *Iterator[T]) Begin() {
iterator.index = -1
}
// End moves the iterator past the last element (one-past-the-end).
// Call Prev() to fetch the last element if any.
func (iterator *Iterator[T]) End() {
iterator.index = iterator.list.Size()
}
// First moves the iterator to the first element and returns true if there was a first element in the container.
// If First() returns true, then first element's index and value can be retrieved by Index() and Value().
// Modifies the state of the iterator.
func (iterator *Iterator[T]) First() bool {
iterator.Begin()
return iterator.Next()
}
// Last moves the iterator to the last element and returns true if there was a last element in the container.
// If Last() returns true, then last element's index and value can be retrieved by Index() and Value().
// Modifies the state of the iterator.
func (iterator *Iterator[T]) Last() bool {
iterator.End()
return iterator.Prev()
}
// NextTo moves the iterator to the next element from current position that satisfies the condition given by the
// passed function, and returns true if there was a next element in the container.
// If NextTo() returns true, then next element's index and value can be retrieved by Index() and Value().
// Modifies the state of the iterator.
func (iterator *Iterator[T]) NextTo(f func(index int, value T) bool) bool {
for iterator.Next() {
index, value := iterator.Index(), iterator.Value()
if f(index, value) {
return true
}
}
return false
}
// PrevTo moves the iterator to the previous element from current position that satisfies the condition given by the
// passed function, and returns true if there was a next element in the container.
// If PrevTo() returns true, then next element's index and value can be retrieved by Index() and Value().
// Modifies the state of the iterator.
func (iterator *Iterator[T]) PrevTo(f func(index int, value T) bool) bool {
for iterator.Prev() {
index, value := iterator.Index(), iterator.Value()
if f(index, value) {
return true
}
}
return false
}
================================================
FILE: lists/arraylist/serialization.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package arraylist
import (
"encoding/json"
"github.com/emirpasic/gods/v2/containers"
)
// Assert Serialization implementation
var _ containers.JSONSerializer = (*List[int])(nil)
var _ containers.JSONDeserializer = (*List[int])(nil)
// ToJSON outputs the JSON representation of list's elements.
func (list *List[T]) ToJSON() ([]byte, error) {
return json.Marshal(list.elements)
}
// FromJSON populates list's elements from the input JSON representation.
func (list *List[T]) FromJSON(data []byte) error {
err := json.Unmarshal(data, &list.elements)
return err
}
// UnmarshalJSON @implements json.Unmarshaler
func (list *List[T]) UnmarshalJSON(bytes []byte) error {
return list.FromJSON(bytes)
}
// MarshalJSON @implements json.Marshaler
func (list *List[T]) MarshalJSON() ([]byte, error) {
return list.ToJSON()
}
================================================
FILE: lists/doublylinkedlist/doublylinkedlist.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package doublylinkedlist implements the doubly-linked list.
//
// Structure is not thread safe.
//
// Reference: https://en.wikipedia.org/wiki/List_%28abstract_data_type%29
package doublylinkedlist
import (
"fmt"
"slices"
"strings"
"github.com/emirpasic/gods/v2/lists"
"github.com/emirpasic/gods/v2/utils"
)
// Assert List implementation
var _ lists.List[any] = (*List[any])(nil)
// List holds the elements, where each element points to the next and previous element
type List[T comparable] struct {
first *element[T]
last *element[T]
size int
}
type element[T comparable] struct {
value T
prev *element[T]
next *element[T]
}
// New instantiates a new list and adds the passed values, if any, to the list
func New[T comparable](values ...T) *List[T] {
list := &List[T]{}
if len(values) > 0 {
list.Add(values...)
}
return list
}
// Add appends a value (one or more) at the end of the list (same as Append())
func (list *List[T]) Add(values ...T) {
for _, value := range values {
newElement := &element[T]{value: value, prev: list.last}
if list.size == 0 {
list.first = newElement
list.last = newElement
} else {
list.last.next = newElement
list.last = newElement
}
list.size++
}
}
// Append appends a value (one or more) at the end of the list (same as Add())
func (list *List[T]) Append(values ...T) {
list.Add(values...)
}
// Prepend prepends a values (or more)
func (list *List[T]) Prepend(values ...T) {
// in reverse to keep passed order i.e. ["c","d"] -> Prepend(["a","b"]) -> ["a","b","c",d"]
for v := len(values) - 1; v >= 0; v-- {
newElement := &element[T]{value: values[v], next: list.first}
if list.size == 0 {
list.first = newElement
list.last = newElement
} else {
list.first.prev = newElement
list.first = newElement
}
list.size++
}
}
// Get returns the element at index.
// Second return parameter is true if index is within bounds of the array and array is not empty, otherwise false.
func (list *List[T]) Get(index int) (T, bool) {
if !list.withinRange(index) {
var t T
return t, false
}
// determine traversal direction, last to first or first to last
if list.size-index < index {
element := list.last
for e := list.size - 1; e != index; e, element = e-1, element.prev {
}
return element.value, true
}
element := list.first
for e := 0; e != index; e, element = e+1, element.next {
}
return element.value, true
}
// Remove removes the element at the given index from the list.
func (list *List[T]) Remove(index int) {
if !list.withinRange(index) {
return
}
if list.size == 1 {
list.Clear()
return
}
var element *element[T]
// determine traversal direction, last to first or first to last
if list.size-index < index {
element = list.last
for e := list.size - 1; e != index; e, element = e-1, element.prev {
}
} else {
element = list.first
for e := 0; e != index; e, element = e+1, element.next {
}
}
if element == list.first {
list.first = element.next
}
if element == list.last {
list.last = element.prev
}
if element.prev != nil {
element.prev.next = element.next
}
if element.next != nil {
element.next.prev = element.prev
}
element = nil
list.size--
}
// Contains check if values (one or more) are present in the set.
// All values have to be present in the set for the method to return true.
// Performance time complexity of n^2.
// Returns true if no arguments are passed at all, i.e. set is always super-set of empty set.
func (list *List[T]) Contains(values ...T) bool {
if len(values) == 0 {
return true
}
if list.size == 0 {
return false
}
for _, value := range values {
found := false
for element := list.first; element != nil; element = element.next {
if element.value == value {
found = true
break
}
}
if !found {
return false
}
}
return true
}
// Values returns all elements in the list.
func (list *List[T]) Values() []T {
values := make([]T, list.size, list.size)
for e, element := 0, list.first; element != nil; e, element = e+1, element.next {
values[e] = element.value
}
return values
}
// IndexOf returns index of provided element
func (list *List[T]) IndexOf(value T) int {
if list.size == 0 {
return -1
}
for index, element := range list.Values() {
if element == value {
return index
}
}
return -1
}
// Empty returns true if list does not contain any elements.
func (list *List[T]) Empty() bool {
return list.size == 0
}
// Size returns number of elements within the list.
func (list *List[T]) Size() int {
return list.size
}
// Clear removes all elements from the list.
func (list *List[T]) Clear() {
list.size = 0
list.first = nil
list.last = nil
}
// Sort sorts values (in-place) using a Comparator.
func (list *List[T]) Sort(comparator utils.Comparator[T]) {
if list.size < 2 {
return
}
values := list.Values()
slices.SortFunc(values, comparator)
list.Clear()
list.Add(values...)
}
// Swap swaps values of two elements at the given indices.
func (list *List[T]) Swap(i, j int) {
if list.withinRange(i) && list.withinRange(j) && i != j {
var element1, element2 *element[T]
for e, currentElement := 0, list.first; element1 == nil || element2 == nil; e, currentElement = e+1, currentElement.next {
switch e {
case i:
element1 = currentElement
case j:
element2 = currentElement
}
}
element1.value, element2.value = element2.value, element1.value
}
}
// Insert inserts values at specified index position shifting the value at that position (if any) and any subsequent elements to the right.
// Does not do anything if position is negative or bigger than list's size
// Note: position equal to list's size is valid, i.e. append.
func (list *List[T]) Insert(index int, values ...T) {
if !list.withinRange(index) {
// Append
if index == list.size {
list.Add(values...)
}
return
}
var beforeElement *element[T]
var foundElement *element[T]
// determine traversal direction, last to first or first to last
if list.size-index < index {
foundElement = list.last
beforeElement = list.last.prev
for e := list.size - 1; e != index; e, foundElement = e-1, foundElement.prev {
beforeElement = beforeElement.prev
}
} else {
foundElement = list.first
for e := 0; e != index; e, foundElement = e+1, foundElement.next {
beforeElement = foundElement
}
}
if foundElement == list.first {
oldNextElement := list.first
for i, value := range values {
newElement := &element[T]{value: value}
if i == 0 {
list.first = newElement
} else {
newElement.prev = beforeElement
beforeElement.next = newElement
}
beforeElement = newElement
}
oldNextElement.prev = beforeElement
beforeElement.next = oldNextElement
} else {
oldNextElement := beforeElement.next
for _, value := range values {
newElement := &element[T]{value: value}
newElement.prev = beforeElement
beforeElement.next = newElement
beforeElement = newElement
}
oldNextElement.prev = beforeElement
beforeElement.next = oldNextElement
}
list.size += len(values)
}
// Set value at specified index position
// Does not do anything if position is negative or bigger than list's size
// Note: position equal to list's size is valid, i.e. append.
func (list *List[T]) Set(index int, value T) {
if !list.withinRange(index) {
// Append
if index == list.size {
list.Add(value)
}
return
}
var foundElement *element[T]
// determine traversal direction, last to first or first to last
if list.size-index < index {
foundElement = list.last
for e := list.size - 1; e != index; {
fmt.Println("Set last", index, value, foundElement, foundElement.prev)
e, foundElement = e-1, foundElement.prev
}
} else {
foundElement = list.first
for e := 0; e != index; {
e, foundElement = e+1, foundElement.next
}
}
foundElement.value = value
}
// String returns a string representation of container
func (list *List[T]) String() string {
str := "DoublyLinkedList\n"
values := []string{}
for element := list.first; element != nil; element = element.next {
values = append(values, fmt.Sprintf("%v", element.value))
}
str += strings.Join(values, ", ")
return str
}
// Check that the index is within bounds of the list
func (list *List[T]) withinRange(index int) bool {
return index >= 0 && index < list.size
}
================================================
FILE: lists/doublylinkedlist/doublylinkedlist_test.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package doublylinkedlist
import (
"cmp"
"encoding/json"
"slices"
"strings"
"testing"
)
func TestListNew(t *testing.T) {
list1 := New[int]()
if actualValue := list1.Empty(); actualValue != true {
t.Errorf("Got %v expected %v", actualValue, true)
}
list2 := New[int](1, 2)
if actualValue := list2.Size(); actualValue != 2 {
t.Errorf("Got %v expected %v", actualValue, 2)
}
if actualValue, ok := list2.Get(0); actualValue != 1 || !ok {
t.Errorf("Got %v expected %v", actualValue, 1)
}
if actualValue, ok := list2.Get(1); actualValue != 2 || !ok {
t.Errorf("Got %v expected %v", actualValue, 2)
}
if actualValue, ok := list2.Get(2); actualValue != 0 || ok {
t.Errorf("Got %v expected %v", actualValue, 0)
}
}
func TestListAdd(t *testing.T) {
list := New[string]()
list.Add("a")
list.Add("b", "c")
if actualValue := list.Empty(); actualValue != false {
t.Errorf("Got %v expected %v", actualValue, false)
}
if actualValue := list.Size(); actualValue != 3 {
t.Errorf("Got %v expected %v", actualValue, 3)
}
if actualValue, ok := list.Get(2); actualValue != "c" || !ok {
t.Errorf("Got %v expected %v", actualValue, "c")
}
}
func TestListAppendAndPrepend(t *testing.T) {
list := New[string]()
list.Add("b")
list.Prepend("a")
list.Append("c")
if actualValue := list.Empty(); actualValue != false {
t.Errorf("Got %v expected %v", actualValue, false)
}
if actualValue := list.Size(); actualValue != 3 {
t.Errorf("Got %v expected %v", actualValue, 3)
}
if actualValue, ok := list.Get(0); actualValue != "a" || !ok {
t.Errorf("Got %v expected %v", actualValue, "c")
}
if actualValue, ok := list.Get(1); actualValue != "b" || !ok {
t.Errorf("Got %v expected %v", actualValue, "c")
}
if actualValue, ok := list.Get(2); actualValue != "c" || !ok {
t.Errorf("Got %v expected %v", actualValue, "c")
}
}
func TestListRemove(t *testing.T) {
list := New[string]()
list.Add("a")
list.Add("b", "c")
list.Remove(2)
if actualValue, ok := list.Get(2); actualValue != "" || ok {
t.Errorf("Got %v expected %v", actualValue, "")
}
list.Remove(1)
list.Remove(0)
list.Remove(0) // no effect
if actualValue := list.Empty(); actualValue != true {
t.Errorf("Got %v expected %v", actualValue, true)
}
if actualValue := list.Size(); actualValue != 0 {
t.Errorf("Got %v expected %v", actualValue, 0)
}
}
func TestListGet(t *testing.T) {
list := New[string]()
list.Add("a")
list.Add("b", "c")
if actualValue, ok := list.Get(0); actualValue != "a" || !ok {
t.Errorf("Got %v expected %v", actualValue, "a")
}
if actualValue, ok := list.Get(1); actualValue != "b" || !ok {
t.Errorf("Got %v expected %v", actualValue, "b")
}
if actualValue, ok := list.Get(2); actualValue != "c" || !ok {
t.Errorf("Got %v expected %v", actualValue, "c")
}
if actualValue, ok := list.Get(3); actualValue != "" || ok {
t.Errorf("Got %v expected %v", actualValue, "")
}
list.Remove(0)
if actualValue, ok := list.Get(0); actualValue != "b" || !ok {
t.Errorf("Got %v expected %v", actualValue, "b")
}
}
func TestListSwap(t *testing.T) {
list := New[string]()
list.Add("a")
list.Add("b", "c")
list.Swap(0, 1)
if actualValue, ok := list.Get(0); actualValue != "b" || !ok {
t.Errorf("Got %v expected %v", actualValue, "b")
}
}
func TestListSort(t *testing.T) {
list := New[string]()
list.Sort(cmp.Compare[string])
list.Add("e", "f", "g", "a", "b", "c", "d")
list.Sort(cmp.Compare[string])
for i := 1; i < list.Size(); i++ {
a, _ := list.Get(i - 1)
b, _ := list.Get(i)
if a > b {
t.Errorf("Not sorted! %s > %s", a, b)
}
}
}
func TestListClear(t *testing.T) {
list := New[string]()
list.Add("e", "f", "g", "a", "b", "c", "d")
list.Clear()
if actualValue := list.Empty(); actualValue != true {
t.Errorf("Got %v expected %v", actualValue, true)
}
if actualValue := list.Size(); actualValue != 0 {
t.Errorf("Got %v expected %v", actualValue, 0)
}
}
func TestListContains(t *testing.T) {
list := New[string]()
list.Add("a")
list.Add("b", "c")
if actualValue := list.Contains("a"); actualValue != true {
t.Errorf("Got %v expected %v", actualValue, true)
}
if actualValue := list.Contains(""); actualValue != false {
t.Errorf("Got %v expected %v", actualValue, false)
}
if actualValue := list.Contains("a", "b", "c"); actualValue != true {
t.Errorf("Got %v expected %v", actualValue, true)
}
if actualValue := list.Contains("a", "b", "c", "d"); actualValue != false {
t.Errorf("Got %v expected %v", actualValue, false)
}
list.Clear()
if actualValue := list.Contains("a"); actualValue != false {
t.Errorf("Got %v expected %v", actualValue, false)
}
if actualValue := list.Contains("a", "b", "c"); actualValue != false {
t.Errorf("Got %v expected %v", actualValue, false)
}
}
func TestListValues(t *testing.T) {
list := New[string]()
list.Add("a")
list.Add("b", "c")
if actualValue, expectedValue := list.Values(), []string{"a", "b", "c"}; !slices.Equal(actualValue, expectedValue) {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
}
func TestListInsert(t *testing.T) {
list := New[string]()
list.Insert(0, "b", "c", "d")
list.Insert(0, "a")
list.Insert(10, "x") // ignore
if actualValue := list.Size(); actualValue != 4 {
t.Errorf("Got %v expected %v", actualValue, 4)
}
list.Insert(4, "g") // append
if actualValue := list.Size(); actualValue != 5 {
t.Errorf("Got %v expected %v", actualValue, 5)
}
if actualValue, expectedValue := strings.Join(list.Values(), ""), "abcdg"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
list.Insert(4, "e", "f") // last to first traversal
if actualValue := list.Size(); actualValue != 7 {
t.Errorf("Got %v expected %v", actualValue, 7)
}
if actualValue, expectedValue := strings.Join(list.Values(), ""), "abcdefg"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
}
func TestListSet(t *testing.T) {
list := New[string]()
list.Set(0, "a")
list.Set(1, "b")
if actualValue := list.Size(); actualValue != 2 {
t.Errorf("Got %v expected %v", actualValue, 2)
}
list.Set(2, "c") // append
if actualValue := list.Size(); actualValue != 3 {
t.Errorf("Got %v expected %v", actualValue, 3)
}
list.Set(4, "d") // ignore
list.Set(1, "bb") // update
if actualValue := list.Size(); actualValue != 3 {
t.Errorf("Got %v expected %v", actualValue, 3)
}
if actualValue, expectedValue := list.Values(), []string{"a", "bb", "c"}; !slices.Equal(actualValue, expectedValue) {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
}
func TestListEach(t *testing.T) {
list := New[string]()
list.Add("a", "b", "c")
list.Each(func(index int, value string) {
switch index {
case 0:
if actualValue, expectedValue := value, "a"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
case 1:
if actualValue, expectedValue := value, "b"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
case 2:
if actualValue, expectedValue := value, "c"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
default:
t.Errorf("Too many")
}
})
}
func TestListMap(t *testing.T) {
list := New[string]()
list.Add("a", "b", "c")
mappedList := list.Map(func(index int, value string) string {
return "mapped: " + value
})
if actualValue, _ := mappedList.Get(0); actualValue != "mapped: a" {
t.Errorf("Got %v expected %v", actualValue, "mapped: a")
}
if actualValue, _ := mappedList.Get(1); actualValue != "mapped: b" {
t.Errorf("Got %v expected %v", actualValue, "mapped: b")
}
if actualValue, _ := mappedList.Get(2); actualValue != "mapped: c" {
t.Errorf("Got %v expected %v", actualValue, "mapped: c")
}
if mappedList.Size() != 3 {
t.Errorf("Got %v expected %v", mappedList.Size(), 3)
}
}
func TestListSelect(t *testing.T) {
list := New[string]()
list.Add("a", "b", "c")
selectedList := list.Select(func(index int, value string) bool {
return value >= "a" && value <= "b"
})
if actualValue, _ := selectedList.Get(0); actualValue != "a" {
t.Errorf("Got %v expected %v", actualValue, "value: a")
}
if actualValue, _ := selectedList.Get(1); actualValue != "b" {
t.Errorf("Got %v expected %v", actualValue, "value: b")
}
if selectedList.Size() != 2 {
t.Errorf("Got %v expected %v", selectedList.Size(), 3)
}
}
func TestListAny(t *testing.T) {
list := New[string]()
list.Add("a", "b", "c")
any := list.Any(func(index int, value string) bool {
return value == "c"
})
if any != true {
t.Errorf("Got %v expected %v", any, true)
}
any = list.Any(func(index int, value string) bool {
return value == "x"
})
if any != false {
t.Errorf("Got %v expected %v", any, false)
}
}
func TestListAll(t *testing.T) {
list := New[string]()
list.Add("a", "b", "c")
all := list.All(func(index int, value string) bool {
return value >= "a" && value <= "c"
})
if all != true {
t.Errorf("Got %v expected %v", all, true)
}
all = list.All(func(index int, value string) bool {
return value >= "a" && value <= "b"
})
if all != false {
t.Errorf("Got %v expected %v", all, false)
}
}
func TestListFind(t *testing.T) {
list := New[string]()
list.Add("a", "b", "c")
foundIndex, foundValue := list.Find(func(index int, value string) bool {
return value == "c"
})
if foundValue != "c" || foundIndex != 2 {
t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, "c", 2)
}
foundIndex, foundValue = list.Find(func(index int, value string) bool {
return value == "x"
})
if foundValue != "" || foundIndex != -1 {
t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, nil, nil)
}
}
func TestListChaining(t *testing.T) {
list := New[string]()
list.Add("a", "b", "c")
chainedList := list.Select(func(index int, value string) bool {
return value > "a"
}).Map(func(index int, value string) string {
return value + value
})
if chainedList.Size() != 2 {
t.Errorf("Got %v expected %v", chainedList.Size(), 2)
}
if actualValue, ok := chainedList.Get(0); actualValue != "bb" || !ok {
t.Errorf("Got %v expected %v", actualValue, "b")
}
if actualValue, ok := chainedList.Get(1); actualValue != "cc" || !ok {
t.Errorf("Got %v expected %v", actualValue, "c")
}
}
func TestListIteratorNextOnEmpty(t *testing.T) {
list := New[string]()
it := list.Iterator()
for it.Next() {
t.Errorf("Shouldn't iterate on empty list")
}
}
func TestListIteratorNext(t *testing.T) {
list := New[string]()
list.Add("a", "b", "c")
it := list.Iterator()
count := 0
for it.Next() {
count++
index := it.Index()
value := it.Value()
switch index {
case 0:
if actualValue, expectedValue := value, "a"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
case 1:
if actualValue, expectedValue := value, "b"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
case 2:
if actualValue, expectedValue := value, "c"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
default:
t.Errorf("Too many")
}
}
if actualValue, expectedValue := count, 3; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
}
func TestListIteratorPrevOnEmpty(t *testing.T) {
list := New[string]()
it := list.Iterator()
for it.Prev() {
t.Errorf("Shouldn't iterate on empty list")
}
}
func TestListIteratorPrev(t *testing.T) {
list := New[string]()
list.Add("a", "b", "c")
it := list.Iterator()
for it.Next() {
}
count := 0
for it.Prev() {
count++
index := it.Index()
value := it.Value()
switch index {
case 0:
if actualValue, expectedValue := value, "a"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
case 1:
if actualValue, expectedValue := value, "b"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
case 2:
if actualValue, expectedValue := value, "c"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
default:
t.Errorf("Too many")
}
}
if actualValue, expectedValue := count, 3; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
}
func TestListIteratorBegin(t *testing.T) {
list := New[string]()
it := list.Iterator()
it.Begin()
list.Add("a", "b", "c")
for it.Next() {
}
it.Begin()
it.Next()
if index, value := it.Index(), it.Value(); index != 0 || value != "a" {
t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a")
}
}
func TestListIteratorEnd(t *testing.T) {
list := New[string]()
it := list.Iterator()
if index := it.Index(); index != -1 {
t.Errorf("Got %v expected %v", index, -1)
}
it.End()
if index := it.Index(); index != 0 {
t.Errorf("Got %v expected %v", index, 0)
}
list.Add("a", "b", "c")
it.End()
if index := it.Index(); index != list.Size() {
t.Errorf("Got %v expected %v", index, list.Size())
}
it.Prev()
if index, value := it.Index(), it.Value(); index != list.Size()-1 || value != "c" {
t.Errorf("Got %v,%v expected %v,%v", index, value, list.Size()-1, "c")
}
}
func TestListIteratorFirst(t *testing.T) {
list := New[string]()
it := list.Iterator()
if actualValue, expectedValue := it.First(), false; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
list.Add("a", "b", "c")
if actualValue, expectedValue := it.First(), true; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
if index, value := it.Index(), it.Value(); index != 0 || value != "a" {
t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a")
}
}
func TestListIteratorLast(t *testing.T) {
list := New[string]()
it := list.Iterator()
if actualValue, expectedValue := it.Last(), false; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
list.Add("a", "b", "c")
if actualValue, expectedValue := it.Last(), true; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
if index, value := it.Index(), it.Value(); index != 2 || value != "c" {
t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "c")
}
}
func TestListIteratorNextTo(t *testing.T) {
// Sample seek function, i.e. string starting with "b"
seek := func(index int, value string) bool {
return strings.HasSuffix(value, "b")
}
// NextTo (empty)
{
list := New[string]()
it := list.Iterator()
for it.NextTo(seek) {
t.Errorf("Shouldn't iterate on empty list")
}
}
// NextTo (not found)
{
list := New[string]()
list.Add("xx", "yy")
it := list.Iterator()
for it.NextTo(seek) {
t.Errorf("Shouldn't iterate on empty list")
}
}
// NextTo (found)
{
list := New[string]()
list.Add("aa", "bb", "cc")
it := list.Iterator()
it.Begin()
if !it.NextTo(seek) {
t.Errorf("Shouldn't iterate on empty list")
}
if index, value := it.Index(), it.Value(); index != 1 || value != "bb" {
t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb")
}
if !it.Next() {
t.Errorf("Should go to first element")
}
if index, value := it.Index(), it.Value(); index != 2 || value != "cc" {
t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "cc")
}
if it.Next() {
t.Errorf("Should not go past last element")
}
}
}
func TestListIteratorPrevTo(t *testing.T) {
// Sample seek function, i.e. string starting with "b"
seek := func(index int, value string) bool {
return strings.HasSuffix(value, "b")
}
// PrevTo (empty)
{
list := New[string]()
it := list.Iterator()
it.End()
for it.PrevTo(seek) {
t.Errorf("Shouldn't iterate on empty list")
}
}
// PrevTo (not found)
{
list := New[string]()
list.Add("xx", "yy")
it := list.Iterator()
it.End()
for it.PrevTo(seek) {
t.Errorf("Shouldn't iterate on empty list")
}
}
// PrevTo (found)
{
list := New[string]()
list.Add("aa", "bb", "cc")
it := list.Iterator()
it.End()
if !it.PrevTo(seek) {
t.Errorf("Shouldn't iterate on empty list")
}
if index, value := it.Index(), it.Value(); index != 1 || value != "bb" {
t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb")
}
if !it.Prev() {
t.Errorf("Should go to first element")
}
if index, value := it.Index(), it.Value(); index != 0 || value != "aa" {
t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "aa")
}
if it.Prev() {
t.Errorf("Should not go before first element")
}
}
}
func TestListSerialization(t *testing.T) {
list := New[string]()
list.Add("a", "b", "c")
var err error
assert := func() {
if actualValue, expectedValue := list.Values(), []string{"a", "b", "c"}; !slices.Equal(actualValue, expectedValue) {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
if actualValue, expectedValue := list.Size(), 3; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
if err != nil {
t.Errorf("Got error %v", err)
}
}
assert()
bytes, err := list.ToJSON()
assert()
err = list.FromJSON(bytes)
assert()
bytes, err = json.Marshal([]any{"a", "b", "c", list})
if err != nil {
t.Errorf("Got error %v", err)
}
err = json.Unmarshal([]byte(`["a","b","c"]`), &list)
if err != nil {
t.Errorf("Got error %v", err)
}
assert()
}
func TestListString(t *testing.T) {
c := New[int]()
c.Add(1)
if !strings.HasPrefix(c.String(), "DoublyLinkedList") {
t.Errorf("String should start with container name")
}
}
func benchmarkGet(b *testing.B, list *List[int], size int) {
for i := 0; i < b.N; i++ {
for n := 0; n < size; n++ {
list.Get(n)
}
}
}
func benchmarkAdd(b *testing.B, list *List[int], size int) {
for i := 0; i < b.N; i++ {
for n := 0; n < size; n++ {
list.Add(n)
}
}
}
func benchmarkRemove(b *testing.B, list *List[int], size int) {
for i := 0; i < b.N; i++ {
for n := 0; n < size; n++ {
list.Remove(n)
}
}
}
func BenchmarkDoublyLinkedListGet100(b *testing.B) {
b.StopTimer()
size := 100
list := New[int]()
for n := 0; n < size; n++ {
list.Add(n)
}
b.StartTimer()
benchmarkGet(b, list, size)
}
func BenchmarkDoublyLinkedListGet1000(b *testing.B) {
b.StopTimer()
size := 1000
list := New[int]()
for n := 0; n < size; n++ {
list.Add(n)
}
b.StartTimer()
benchmarkGet(b, list, size)
}
func BenchmarkDoublyLinkedListGet10000(b *testing.B) {
b.StopTimer()
size := 10000
list := New[int]()
for n := 0; n < size; n++ {
list.Add(n)
}
b.StartTimer()
benchmarkGet(b, list, size)
}
func BenchmarkDoublyLinkedListGet100000(b *testing.B) {
b.StopTimer()
size := 100000
list := New[int]()
for n := 0; n < size; n++ {
list.Add(n)
}
b.StartTimer()
benchmarkGet(b, list, size)
}
func BenchmarkDoublyLinkedListAdd100(b *testing.B) {
b.StopTimer()
size := 100
list := New[int]()
b.StartTimer()
benchmarkAdd(b, list, size)
}
func BenchmarkDoublyLinkedListAdd1000(b *testing.B) {
b.StopTimer()
size := 1000
list := New[int]()
for n := 0; n < size; n++ {
list.Add(n)
}
b.StartTimer()
benchmarkAdd(b, list, size)
}
func BenchmarkDoublyLinkedListAdd10000(b *testing.B) {
b.StopTimer()
size := 10000
list := New[int]()
for n := 0; n < size; n++ {
list.Add(n)
}
b.StartTimer()
benchmarkAdd(b, list, size)
}
func BenchmarkDoublyLinkedListAdd100000(b *testing.B) {
b.StopTimer()
size := 100000
list := New[int]()
for n := 0; n < size; n++ {
list.Add(n)
}
b.StartTimer()
benchmarkAdd(b, list, size)
}
func BenchmarkDoublyLinkedListRemove100(b *testing.B) {
b.StopTimer()
size := 100
list := New[int]()
for n := 0; n < size; n++ {
list.Add(n)
}
b.StartTimer()
benchmarkRemove(b, list, size)
}
func BenchmarkDoublyLinkedListRemove1000(b *testing.B) {
b.StopTimer()
size := 1000
list := New[int]()
for n := 0; n < size; n++ {
list.Add(n)
}
b.StartTimer()
benchmarkRemove(b, list, size)
}
func BenchmarkDoublyLinkedListRemove10000(b *testing.B) {
b.StopTimer()
size := 10000
list := New[int]()
for n := 0; n < size; n++ {
list.Add(n)
}
b.StartTimer()
benchmarkRemove(b, list, size)
}
func BenchmarkDoublyLinkedListRemove100000(b *testing.B) {
b.StopTimer()
size := 100000
list := New[int]()
for n := 0; n < size; n++ {
list.Add(n)
}
b.StartTimer()
benchmarkRemove(b, list, size)
}
================================================
FILE: lists/doublylinkedlist/enumerable.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package doublylinkedlist
import "github.com/emirpasic/gods/v2/containers"
// Assert Enumerable implementation
var _ containers.EnumerableWithIndex[int] = (*List[int])(nil)
// Each calls the given function once for each element, passing that element's index and value.
func (list *List[T]) Each(f func(index int, value T)) {
iterator := list.Iterator()
for iterator.Next() {
f(iterator.Index(), iterator.Value())
}
}
// Map invokes the given function once for each element and returns a
// container containing the values returned by the given function.
func (list *List[T]) Map(f func(index int, value T) T) *List[T] {
newList := &List[T]{}
iterator := list.Iterator()
for iterator.Next() {
newList.Add(f(iterator.Index(), iterator.Value()))
}
return newList
}
// Select returns a new container containing all elements for which the given function returns a true value.
func (list *List[T]) Select(f func(index int, value T) bool) *List[T] {
newList := &List[T]{}
iterator := list.Iterator()
for iterator.Next() {
if f(iterator.Index(), iterator.Value()) {
newList.Add(iterator.Value())
}
}
return newList
}
// Any passes each element of the container to the given function and
// returns true if the function ever returns true for any element.
func (list *List[T]) Any(f func(index int, value T) bool) bool {
iterator := list.Iterator()
for iterator.Next() {
if f(iterator.Index(), iterator.Value()) {
return true
}
}
return false
}
// All passes each element of the container to the given function and
// returns true if the function returns true for all elements.
func (list *List[T]) All(f func(index int, value T) bool) bool {
iterator := list.Iterator()
for iterator.Next() {
if !f(iterator.Index(), iterator.Value()) {
return false
}
}
return true
}
// 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.
func (list *List[T]) Find(f func(index int, value T) bool) (index int, value T) {
iterator := list.Iterator()
for iterator.Next() {
if f(iterator.Index(), iterator.Value()) {
return iterator.Index(), iterator.Value()
}
}
var t T
return -1, t
}
================================================
FILE: lists/doublylinkedlist/iterator.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package doublylinkedlist
import "github.com/emirpasic/gods/v2/containers"
// Assert Iterator implementation
var _ containers.ReverseIteratorWithIndex[int] = (*Iterator[int])(nil)
// Iterator holding the iterator's state
type Iterator[T comparable] struct {
list *List[T]
index int
element *element[T]
}
// Iterator returns a stateful iterator whose values can be fetched by an index.
func (list *List[T]) Iterator() Iterator[T] {
return Iterator[T]{list: list, index: -1, element: nil}
}
// Next moves the iterator to the next element and returns true if there was a next element in the container.
// If Next() returns true, then next element's index and value can be retrieved by Index() and Value().
// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
// Modifies the state of the iterator.
func (iterator *Iterator[T]) Next() bool {
if iterator.index < iterator.list.size {
iterator.index++
}
if !iterator.list.withinRange(iterator.index) {
iterator.element = nil
return false
}
if iterator.index != 0 {
iterator.element = iterator.element.next
} else {
iterator.element = iterator.list.first
}
return true
}
// Prev moves the iterator to the previous element and returns true if there was a previous element in the container.
// If Prev() returns true, then previous element's index and value can be retrieved by Index() and Value().
// Modifies the state of the iterator.
func (iterator *Iterator[T]) Prev() bool {
if iterator.index >= 0 {
iterator.index--
}
if !iterator.list.withinRange(iterator.index) {
iterator.element = nil
return false
}
if iterator.index == iterator.list.size-1 {
iterator.element = iterator.list.last
} else {
iterator.element = iterator.element.prev
}
return iterator.list.withinRange(iterator.index)
}
// Value returns the current element's value.
// Does not modify the state of the iterator.
func (iterator *Iterator[T]) Value() T {
return iterator.element.value
}
// Index returns the current element's index.
// Does not modify the state of the iterator.
func (iterator *Iterator[T]) Index() int {
return iterator.index
}
// Begin resets the iterator to its initial state (one-before-first)
// Call Next() to fetch the first element if any.
func (iterator *Iterator[T]) Begin() {
iterator.index = -1
iterator.element = nil
}
// End moves the iterator past the last element (one-past-the-end).
// Call Prev() to fetch the last element if any.
func (iterator *Iterator[T]) End() {
iterator.index = iterator.list.size
iterator.element = iterator.list.last
}
// First moves the iterator to the first element and returns true if there was a first element in the container.
// If First() returns true, then first element's index and value can be retrieved by Index() and Value().
// Modifies the state of the iterator.
func (iterator *Iterator[T]) First() bool {
iterator.Begin()
return iterator.Next()
}
// Last moves the iterator to the last element and returns true if there was a last element in the container.
// If Last() returns true, then last element's index and value can be retrieved by Index() and Value().
// Modifies the state of the iterator.
func (iterator *Iterator[T]) Last() bool {
iterator.End()
return iterator.Prev()
}
// NextTo moves the iterator to the next element from current position that satisfies the condition given by the
// passed function, and returns true if there was a next element in the container.
// If NextTo() returns true, then next element's index and value can be retrieved by Index() and Value().
// Modifies the state of the iterator.
func (iterator *Iterator[T]) NextTo(f func(index int, value T) bool) bool {
for iterator.Next() {
index, value := iterator.Index(), iterator.Value()
if f(index, value) {
return true
}
}
return false
}
// PrevTo moves the iterator to the previous element from current position that satisfies the condition given by the
// passed function, and returns true if there was a next element in the container.
// If PrevTo() returns true, then next element's index and value can be retrieved by Index() and Value().
// Modifies the state of the iterator.
func (iterator *Iterator[T]) PrevTo(f func(index int, value T) bool) bool {
for iterator.Prev() {
index, value := iterator.Index(), iterator.Value()
if f(index, value) {
return true
}
}
return false
}
================================================
FILE: lists/doublylinkedlist/serialization.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package doublylinkedlist
import (
"encoding/json"
"github.com/emirpasic/gods/v2/containers"
)
// Assert Serialization implementation
var _ containers.JSONSerializer = (*List[int])(nil)
var _ containers.JSONDeserializer = (*List[int])(nil)
// ToJSON outputs the JSON representation of list's elements.
func (list *List[T]) ToJSON() ([]byte, error) {
return json.Marshal(list.Values())
}
// FromJSON populates list's elements from the input JSON representation.
func (list *List[T]) FromJSON(data []byte) error {
var elements []T
err := json.Unmarshal(data, &elements)
if err == nil {
list.Clear()
list.Add(elements...)
}
return err
}
// UnmarshalJSON @implements json.Unmarshaler
func (list *List[T]) UnmarshalJSON(bytes []byte) error {
return list.FromJSON(bytes)
}
// MarshalJSON @implements json.Marshaler
func (list *List[T]) MarshalJSON() ([]byte, error) {
return list.ToJSON()
}
================================================
FILE: lists/lists.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package lists provides an abstract List interface.
//
// In computer science, a list or sequence is an abstract data type that represents an ordered sequence of values, where the same value may occur more than once. An instance of a list is a computer representation of the mathematical concept of a finite sequence; the (potentially) infinite analog of a list is a stream. Lists are a basic example of containers, as they contain other values. If the same value occurs multiple times, each occurrence is considered a distinct item.
//
// Reference: https://en.wikipedia.org/wiki/List_%28abstract_data_type%29
package lists
import (
"github.com/emirpasic/gods/v2/containers"
"github.com/emirpasic/gods/v2/utils"
)
// List interface that all lists implement
type List[T comparable] interface {
Get(index int) (T, bool)
Remove(index int)
Add(values ...T)
Contains(values ...T) bool
Sort(comparator utils.Comparator[T])
Swap(index1, index2 int)
Insert(index int, values ...T)
Set(index int, value T)
containers.Container[T]
// Empty() bool
// Size() int
// Clear()
// Values() []interface{}
// String() string
}
================================================
FILE: lists/singlylinkedlist/enumerable.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package singlylinkedlist
import "github.com/emirpasic/gods/v2/containers"
// Assert Enumerable implementation
var _ containers.EnumerableWithIndex[int] = (*List[int])(nil)
// Each calls the given function once for each element, passing that element's index and value.
func (list *List[T]) Each(f func(index int, value T)) {
iterator := list.Iterator()
for iterator.Next() {
f(iterator.Index(), iterator.Value())
}
}
// Map invokes the given function once for each element and returns a
// container containing the values returned by the given function.
func (list *List[T]) Map(f func(index int, value T) T) *List[T] {
newList := &List[T]{}
iterator := list.Iterator()
for iterator.Next() {
newList.Add(f(iterator.Index(), iterator.Value()))
}
return newList
}
// Select returns a new container containing all elements for which the given function returns a true value.
func (list *List[T]) Select(f func(index int, value T) bool) *List[T] {
newList := &List[T]{}
iterator := list.Iterator()
for iterator.Next() {
if f(iterator.Index(), iterator.Value()) {
newList.Add(iterator.Value())
}
}
return newList
}
// Any passes each element of the container to the given function and
// returns true if the function ever returns true for any element.
func (list *List[T]) Any(f func(index int, value T) bool) bool {
iterator := list.Iterator()
for iterator.Next() {
if f(iterator.Index(), iterator.Value()) {
return true
}
}
return false
}
// All passes each element of the container to the given function and
// returns true if the function returns true for all elements.
func (list *List[T]) All(f func(index int, value T) bool) bool {
iterator := list.Iterator()
for iterator.Next() {
if !f(iterator.Index(), iterator.Value()) {
return false
}
}
return true
}
// 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.
func (list *List[T]) Find(f func(index int, value T) bool) (index int, value T) {
iterator := list.Iterator()
for iterator.Next() {
if f(iterator.Index(), iterator.Value()) {
return iterator.Index(), iterator.Value()
}
}
return -1, value
}
================================================
FILE: lists/singlylinkedlist/iterator.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package singlylinkedlist
import "github.com/emirpasic/gods/v2/containers"
// Assert Iterator implementation
var _ containers.IteratorWithIndex[int] = (*Iterator[int])(nil)
// Iterator holding the iterator's state
type Iterator[T comparable] struct {
list *List[T]
index int
element *element[T]
}
// Iterator returns a stateful iterator whose values can be fetched by an index.
func (list *List[T]) Iterator() *Iterator[T] {
return &Iterator[T]{list: list, index: -1, element: nil}
}
// Next moves the iterator to the next element and returns true if there was a next element in the container.
// If Next() returns true, then next element's index and value can be retrieved by Index() and Value().
// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
// Modifies the state of the iterator.
func (iterator *Iterator[T]) Next() bool {
if iterator.index < iterator.list.size {
iterator.index++
}
if !iterator.list.withinRange(iterator.index) {
iterator.element = nil
return false
}
if iterator.index == 0 {
iterator.element = iterator.list.first
} else {
iterator.element = iterator.element.next
}
return true
}
// Value returns the current element's value.
// Does not modify the state of the iterator.
func (iterator *Iterator[T]) Value() T {
return iterator.element.value
}
// Index returns the current element's index.
// Does not modify the state of the iterator.
func (iterator *Iterator[T]) Index() int {
return iterator.index
}
// Begin resets the iterator to its initial state (one-before-first)
// Call Next() to fetch the first element if any.
func (iterator *Iterator[T]) Begin() {
iterator.index = -1
iterator.element = nil
}
// First moves the iterator to the first element and returns true if there was a first element in the container.
// If First() returns true, then first element's index and value can be retrieved by Index() and Value().
// Modifies the state of the iterator.
func (iterator *Iterator[T]) First() bool {
iterator.Begin()
return iterator.Next()
}
// NextTo moves the iterator to the next element from current position that satisfies the condition given by the
// passed function, and returns true if there was a next element in the container.
// If NextTo() returns true, then next element's index and value can be retrieved by Index() and Value().
// Modifies the state of the iterator.
func (iterator *Iterator[T]) NextTo(f func(index int, value T) bool) bool {
for iterator.Next() {
index, value := iterator.Index(), iterator.Value()
if f(index, value) {
return true
}
}
return false
}
================================================
FILE: lists/singlylinkedlist/serialization.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package singlylinkedlist
import (
"encoding/json"
"github.com/emirpasic/gods/v2/containers"
)
// Assert Serialization implementation
var _ containers.JSONSerializer = (*List[int])(nil)
var _ containers.JSONDeserializer = (*List[int])(nil)
// ToJSON outputs the JSON representation of list's elements.
func (list *List[T]) ToJSON() ([]byte, error) {
return json.Marshal(list.Values())
}
// FromJSON populates list's elements from the input JSON representation.
func (list *List[T]) FromJSON(data []byte) error {
var elements []T
err := json.Unmarshal(data, &elements)
if err == nil {
list.Clear()
list.Add(elements...)
}
return err
}
// UnmarshalJSON @implements json.Unmarshaler
func (list *List[T]) UnmarshalJSON(bytes []byte) error {
return list.FromJSON(bytes)
}
// MarshalJSON @implements json.Marshaler
func (list *List[T]) MarshalJSON() ([]byte, error) {
return list.ToJSON()
}
================================================
FILE: lists/singlylinkedlist/singlylinkedlist.go
================================================
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package singlylinkedlist implements the singly-linked list.
//
// Structure is not thread safe.
//
// Reference: https://en.wikipedia.org/wiki/List_%28abstract_data_type%29
package singlylinkedlist
import (
"fmt"
"slices"
"strings"
"github.com/emirpasic/gods/v2/lists"
"github.com/emirpasic/gods/v2/utils"
)
// Assert List implementation
var _ lists.List[int] = (*List[int])(nil)
// List holds the elements, where each element points to the next element
type List[T comparable] struct {
first *element[T]
last *element[T]
size int
}
type element[T comparable] struct {
value T
next *element[T]
}
// New instantiates a new list and adds the passed values, if any, to the list
func New[T comparable](values ...T) *List[T] {
list := &List[T]{}
if
gitextract_653tot9y/
├── .circleci/
│ └── config.yml
├── .github/
│ ├── dependabot.yml
│ └── workflows/
│ └── codeql-analysis.yml
├── .gitignore
├── LICENSE
├── README.md
├── containers/
│ ├── containers.go
│ ├── containers_test.go
│ ├── enumerable.go
│ ├── iterator.go
│ └── serialization.go
├── examples/
│ ├── README.md
│ ├── arraylist/
│ │ └── arraylist.go
│ ├── arrayqueue/
│ │ └── arrayqqueue.go
│ ├── arraystack/
│ │ └── arraystack.go
│ ├── avltree/
│ │ └── avltree.go
│ ├── binaryheap/
│ │ └── binaryheap.go
│ ├── btree/
│ │ └── btree.go
│ ├── circularbuffer/
│ │ └── circularbuffer.go
│ ├── customcomparator/
│ │ └── customcomparator.go
│ ├── doublylinkedlist/
│ │ └── doublylinkedlist.go
│ ├── enumerablewithindex/
│ │ └── enumerablewithindex.go
│ ├── enumerablewithkey/
│ │ └── enumerablewithkey.go
│ ├── hashbidimap/
│ │ └── hashbidimap.go
│ ├── hashmap/
│ │ └── hashmap.go
│ ├── hashset/
│ │ └── hashset.go
│ ├── iteratorwithindex/
│ │ └── iteratorwithindex.go
│ ├── iteratorwithkey/
│ │ └── iteratorwithkey.go
│ ├── linkedhashmap/
│ │ └── linkedhashmap.go
│ ├── linkedhashset/
│ │ └── linkedhashset.go
│ ├── linkedlistqueue/
│ │ └── linkedlistqueue.go
│ ├── linkedliststack/
│ │ └── linkedliststack.go
│ ├── priorityqueue/
│ │ └── priorityqueue.go
│ ├── redblacktree/
│ │ └── redblacktree.go
│ ├── redblacktreeextended/
│ │ └── redblacktreeextended.go
│ ├── serialization/
│ │ └── serialization.go
│ ├── singlylinkedlist/
│ │ └── singlylinkedlist.go
│ ├── treebidimap/
│ │ └── treebidimap.go
│ ├── treemap/
│ │ └── treemap.go
│ └── treeset/
│ └── treeset.go
├── go.mod
├── go.sum
├── lists/
│ ├── arraylist/
│ │ ├── arraylist.go
│ │ ├── arraylist_test.go
│ │ ├── enumerable.go
│ │ ├── iterator.go
│ │ └── serialization.go
│ ├── doublylinkedlist/
│ │ ├── doublylinkedlist.go
│ │ ├── doublylinkedlist_test.go
│ │ ├── enumerable.go
│ │ ├── iterator.go
│ │ └── serialization.go
│ ├── lists.go
│ └── singlylinkedlist/
│ ├── enumerable.go
│ ├── iterator.go
│ ├── serialization.go
│ ├── singlylinkedlist.go
│ └── singlylinkedlist_test.go
├── maps/
│ ├── hashbidimap/
│ │ ├── hashbidimap.go
│ │ ├── hashbidimap_test.go
│ │ └── serialization.go
│ ├── hashmap/
│ │ ├── hashmap.go
│ │ ├── hashmap_test.go
│ │ └── serialization.go
│ ├── linkedhashmap/
│ │ ├── enumerable.go
│ │ ├── iterator.go
│ │ ├── linkedhashmap.go
│ │ ├── linkedhashmap_test.go
│ │ └── serialization.go
│ ├── maps.go
│ ├── treebidimap/
│ │ ├── enumerable.go
│ │ ├── iterator.go
│ │ ├── serialization.go
│ │ ├── treebidimap.go
│ │ └── treebidimap_test.go
│ └── treemap/
│ ├── enumerable.go
│ ├── iterator.go
│ ├── serialization.go
│ ├── treemap.go
│ └── treemap_test.go
├── queues/
│ ├── arrayqueue/
│ │ ├── arrayqueue.go
│ │ ├── arrayqueue_test.go
│ │ ├── iterator.go
│ │ └── serialization.go
│ ├── circularbuffer/
│ │ ├── circularbuffer.go
│ │ ├── circularbuffer_test.go
│ │ ├── iterator.go
│ │ └── serialization.go
│ ├── linkedlistqueue/
│ │ ├── iterator.go
│ │ ├── linkedlistqueue.go
│ │ ├── linkedlistqueue_test.go
│ │ └── serialization.go
│ ├── priorityqueue/
│ │ ├── iterator.go
│ │ ├── priorityqueue.go
│ │ ├── priorityqueue_test.go
│ │ └── serialization.go
│ └── queues.go
├── sets/
│ ├── hashset/
│ │ ├── hashset.go
│ │ ├── hashset_test.go
│ │ └── serialization.go
│ ├── linkedhashset/
│ │ ├── enumerable.go
│ │ ├── iterator.go
│ │ ├── linkedhashset.go
│ │ ├── linkedhashset_test.go
│ │ └── serialization.go
│ ├── sets.go
│ └── treeset/
│ ├── enumerable.go
│ ├── iterator.go
│ ├── serialization.go
│ ├── treeset.go
│ └── treeset_test.go
├── stacks/
│ ├── arraystack/
│ │ ├── arraystack.go
│ │ ├── arraystack_test.go
│ │ ├── iterator.go
│ │ └── serialization.go
│ ├── linkedliststack/
│ │ ├── iterator.go
│ │ ├── linkedliststack.go
│ │ ├── linkedliststack_test.go
│ │ └── serialization.go
│ └── stacks.go
├── testutils/
│ └── testutils.go
├── trees/
│ ├── avltree/
│ │ ├── avltree.go
│ │ ├── avltree_test.go
│ │ ├── iterator.go
│ │ └── serialization.go
│ ├── binaryheap/
│ │ ├── binaryheap.go
│ │ ├── binaryheap_test.go
│ │ ├── iterator.go
│ │ └── serialization.go
│ ├── btree/
│ │ ├── btree.go
│ │ ├── btree_test.go
│ │ ├── iterator.go
│ │ └── serialization.go
│ ├── redblacktree/
│ │ ├── iterator.go
│ │ ├── redblacktree.go
│ │ ├── redblacktree_test.go
│ │ └── serialization.go
│ └── trees.go
└── utils/
├── comparator.go
├── comparator_test.go
├── utils.go
└── utils_test.go
SYMBOL INDEX (1495 symbols across 133 files)
FILE: containers/containers.go
type Container (line 24) | type Container interface
function GetSortedValues (line 34) | func GetSortedValues[T cmp.Ordered](container Container[T]) []T {
function GetSortedValuesFunc (line 44) | func GetSortedValuesFunc[T any](container Container[T], comparator utils...
FILE: containers/containers_test.go
type ContainerTest (line 17) | type ContainerTest struct
method Empty (line 21) | func (container ContainerTest[T]) Empty() bool {
method Size (line 25) | func (container ContainerTest[T]) Size() int {
method Clear (line 29) | func (container ContainerTest[T]) Clear() {
method Values (line 33) | func (container ContainerTest[T]) Values() []T {
method String (line 37) | func (container ContainerTest[T]) String() string {
function TestGetSortedValuesInts (line 47) | func TestGetSortedValuesInts(t *testing.T) {
type NotInt (line 59) | type NotInt struct
function TestGetSortedValuesNotInts (line 63) | func TestGetSortedValuesNotInts(t *testing.T) {
FILE: containers/enumerable.go
type EnumerableWithIndex (line 8) | type EnumerableWithIndex interface
type EnumerableWithKey (line 34) | type EnumerableWithKey interface
FILE: containers/iterator.go
type IteratorWithIndex (line 8) | type IteratorWithIndex interface
type IteratorWithKey (line 40) | type IteratorWithKey interface
type ReverseIteratorWithIndex (line 80) | type ReverseIteratorWithIndex interface
type ReverseIteratorWithKey (line 111) | type ReverseIteratorWithKey interface
FILE: containers/serialization.go
type JSONSerializer (line 8) | type JSONSerializer interface
type JSONDeserializer (line 16) | type JSONDeserializer interface
FILE: examples/arraylist/arraylist.go
function main (line 14) | func main() {
FILE: examples/arrayqueue/arrayqqueue.go
function main (line 10) | func main() {
FILE: examples/arraystack/arraystack.go
function main (line 10) | func main() {
FILE: examples/avltree/avltree.go
function main (line 14) | func main() {
FILE: examples/binaryheap/binaryheap.go
function main (line 14) | func main() {
FILE: examples/btree/btree.go
function main (line 14) | func main() {
FILE: examples/circularbuffer/circularbuffer.go
function main (line 10) | func main() {
FILE: examples/customcomparator/customcomparator.go
type User (line 14) | type User struct
function byID (line 20) | func byID(a, b User) int {
function main (line 33) | func main() {
FILE: examples/doublylinkedlist/doublylinkedlist.go
function main (line 14) | func main() {
FILE: examples/enumerablewithindex/enumerablewithindex.go
function printSet (line 13) | func printSet(txt string, set *treeset.Set[int]) {
function main (line 22) | func main() {
FILE: examples/enumerablewithkey/enumerablewithkey.go
function printMap (line 13) | func printMap(txt string, m *treemap.Map[string, int]) {
function main (line 22) | func main() {
FILE: examples/hashbidimap/hashbidimap.go
function main (line 10) | func main() {
FILE: examples/hashmap/hashmap.go
function main (line 10) | func main() {
FILE: examples/hashset/hashset.go
function main (line 10) | func main() {
FILE: examples/iteratorwithindex/iteratorwithindex.go
function main (line 15) | func main() {
FILE: examples/iteratorwithkey/iteratorwithkey.go
function main (line 15) | func main() {
FILE: examples/linkedhashmap/linkedhashmap.go
function main (line 10) | func main() {
FILE: examples/linkedhashset/linkedhashset.go
function main (line 10) | func main() {
FILE: examples/linkedlistqueue/linkedlistqueue.go
function main (line 10) | func main() {
FILE: examples/linkedliststack/linkedliststack.go
function main (line 10) | func main() {
FILE: examples/priorityqueue/priorityqueue.go
type Element (line 14) | type Element struct
function byPriority (line 20) | func byPriority(a, b Element) int {
function main (line 25) | func main() {
FILE: examples/redblacktree/redblacktree.go
function main (line 14) | func main() {
FILE: examples/redblacktreeextended/redblacktreeextended.go
type RedBlackTreeExtended (line 14) | type RedBlackTreeExtended struct
method GetMin (line 19) | func (tree *RedBlackTreeExtended[K, V]) GetMin() (value V, found bool) {
method GetMax (line 28) | func (tree *RedBlackTreeExtended[K, V]) GetMax() (value V, found bool) {
method RemoveMin (line 37) | func (tree *RedBlackTreeExtended[K, V]) RemoveMin() (value V, deleted bo...
method RemoveMax (line 47) | func (tree *RedBlackTreeExtended[K, V]) RemoveMax() (value V, deleted bo...
method getMinFromNode (line 56) | func (tree *RedBlackTreeExtended[K, V]) getMinFromNode(node *rbt.Node[K,...
method getMaxFromNode (line 66) | func (tree *RedBlackTreeExtended[K, V]) getMaxFromNode(node *rbt.Node[K,...
function print (line 76) | func print(tree *RedBlackTreeExtended[int, string]) {
function main (line 85) | func main() {
FILE: examples/serialization/serialization.go
function ListSerializationExample (line 11) | func ListSerializationExample() {
function MapSerializationExample (line 32) | func MapSerializationExample() {
FILE: examples/singlylinkedlist/singlylinkedlist.go
function main (line 14) | func main() {
FILE: examples/treebidimap/treebidimap.go
function main (line 12) | func main() {
FILE: examples/treemap/treemap.go
function main (line 10) | func main() {
FILE: examples/treeset/treeset.go
function main (line 10) | func main() {
FILE: lists/arraylist/arraylist.go
type List (line 25) | type List struct
constant growthFactor (line 30) | growthFactor = float32(2.0)
constant shrinkFactor (line 31) | shrinkFactor = float32(0.25)
function New (line 35) | func New[T comparable](values ...T) *List[T] {
method Add (line 44) | func (list *List[T]) Add(values ...T) {
method Get (line 54) | func (list *List[T]) Get(index int) (T, bool) {
method Remove (line 65) | func (list *List[T]) Remove(index int) {
method Contains (line 79) | func (list *List[T]) Contains(values ...T) bool {
method Values (line 89) | func (list *List[T]) Values() []T {
method IndexOf (line 94) | func (list *List[T]) IndexOf(value T) int {
method Empty (line 99) | func (list *List[T]) Empty() bool {
method Size (line 104) | func (list *List[T]) Size() int {
method Clear (line 109) | func (list *List[T]) Clear() {
method Sort (line 115) | func (list *List[T]) Sort(comparator utils.Comparator[T]) {
method Swap (line 123) | func (list *List[T]) Swap(i, j int) {
method Insert (line 132) | func (list *List[T]) Insert(index int, values ...T) {
method Set (line 149) | func (list *List[T]) Set(index int, value T) {
method String (line 163) | func (list *List[T]) String() string {
method withinRange (line 174) | func (list *List[T]) withinRange(index int) bool {
method resize (line 178) | func (list *List[T]) resize(len, cap int) {
method growBy (line 185) | func (list *List[T]) growBy(n int) {
method shrink (line 198) | func (list *List[T]) shrink() {
FILE: lists/arraylist/arraylist_test.go
function TestListNew (line 15) | func TestListNew(t *testing.T) {
function TestListAdd (line 41) | func TestListAdd(t *testing.T) {
function TestListIndexOf (line 56) | func TestListIndexOf(t *testing.T) {
function TestListRemove (line 83) | func TestListRemove(t *testing.T) {
function TestListGet (line 102) | func TestListGet(t *testing.T) {
function TestListSwap (line 124) | func TestListSwap(t *testing.T) {
function TestListSort (line 134) | func TestListSort(t *testing.T) {
function TestListClear (line 148) | func TestListClear(t *testing.T) {
function TestListContains (line 160) | func TestListContains(t *testing.T) {
function TestListValues (line 185) | func TestListValues(t *testing.T) {
function TestListInsert (line 194) | func TestListInsert(t *testing.T) {
function TestListSet (line 211) | func TestListSet(t *testing.T) {
function TestListEach (line 232) | func TestListEach(t *testing.T) {
function TestListMap (line 255) | func TestListMap(t *testing.T) {
function TestListSelect (line 275) | func TestListSelect(t *testing.T) {
function TestListAny (line 292) | func TestListAny(t *testing.T) {
function TestListAll (line 308) | func TestListAll(t *testing.T) {
function TestListFind (line 324) | func TestListFind(t *testing.T) {
function TestListChaining (line 340) | func TestListChaining(t *testing.T) {
function TestListIteratorNextOnEmpty (line 359) | func TestListIteratorNextOnEmpty(t *testing.T) {
function TestListIteratorNext (line 367) | func TestListIteratorNext(t *testing.T) {
function TestListIteratorPrevOnEmpty (line 398) | func TestListIteratorPrevOnEmpty(t *testing.T) {
function TestListIteratorPrev (line 406) | func TestListIteratorPrev(t *testing.T) {
function TestListIteratorBegin (line 439) | func TestListIteratorBegin(t *testing.T) {
function TestListIteratorEnd (line 453) | func TestListIteratorEnd(t *testing.T) {
function TestListIteratorFirst (line 478) | func TestListIteratorFirst(t *testing.T) {
function TestListIteratorLast (line 493) | func TestListIteratorLast(t *testing.T) {
function TestListIteratorNextTo (line 508) | func TestListIteratorNextTo(t *testing.T) {
function TestListIteratorPrevTo (line 557) | func TestListIteratorPrevTo(t *testing.T) {
function TestListSerialization (line 608) | func TestListSerialization(t *testing.T) {
function TestListString (line 645) | func TestListString(t *testing.T) {
function benchmarkGet (line 653) | func benchmarkGet(b *testing.B, list *List[int], size int) {
function benchmarkAdd (line 661) | func benchmarkAdd(b *testing.B, list *List[int], size int) {
function benchmarkRemove (line 669) | func benchmarkRemove(b *testing.B, list *List[int], size int) {
function BenchmarkArrayListGet100 (line 677) | func BenchmarkArrayListGet100(b *testing.B) {
function BenchmarkArrayListGet1000 (line 688) | func BenchmarkArrayListGet1000(b *testing.B) {
function BenchmarkArrayListGet10000 (line 699) | func BenchmarkArrayListGet10000(b *testing.B) {
function BenchmarkArrayListGet100000 (line 710) | func BenchmarkArrayListGet100000(b *testing.B) {
function BenchmarkArrayListAdd100 (line 721) | func BenchmarkArrayListAdd100(b *testing.B) {
function BenchmarkArrayListAdd1000 (line 729) | func BenchmarkArrayListAdd1000(b *testing.B) {
function BenchmarkArrayListAdd10000 (line 740) | func BenchmarkArrayListAdd10000(b *testing.B) {
function BenchmarkArrayListAdd100000 (line 751) | func BenchmarkArrayListAdd100000(b *testing.B) {
function BenchmarkArrayListRemove100 (line 762) | func BenchmarkArrayListRemove100(b *testing.B) {
function BenchmarkArrayListRemove1000 (line 773) | func BenchmarkArrayListRemove1000(b *testing.B) {
function BenchmarkArrayListRemove10000 (line 784) | func BenchmarkArrayListRemove10000(b *testing.B) {
function BenchmarkArrayListRemove100000 (line 795) | func BenchmarkArrayListRemove100000(b *testing.B) {
FILE: lists/arraylist/enumerable.go
method Each (line 13) | func (list *List[T]) Each(f func(index int, value T)) {
method Map (line 22) | func (list *List[T]) Map(f func(index int, value T) T) *List[T] {
method Select (line 32) | func (list *List[T]) Select(f func(index int, value T) bool) *List[T] {
method Any (line 45) | func (list *List[T]) Any(f func(index int, value T) bool) bool {
method All (line 57) | func (list *List[T]) All(f func(index int, value T) bool) bool {
method Find (line 70) | func (list *List[T]) Find(f func(index int, value T) bool) (int, T) {
FILE: lists/arraylist/iterator.go
type Iterator (line 13) | type Iterator struct
method Iterator (line 19) | func (list *List[T]) Iterator() *Iterator[T] {
method Next (line 27) | func (iterator *Iterator[T]) Next() bool {
method Prev (line 37) | func (iterator *Iterator[T]) Prev() bool {
method Value (line 46) | func (iterator *Iterator[T]) Value() T {
method Index (line 52) | func (iterator *Iterator[T]) Index() int {
method Begin (line 58) | func (iterator *Iterator[T]) Begin() {
method End (line 64) | func (iterator *Iterator[T]) End() {
method First (line 71) | func (iterator *Iterator[T]) First() bool {
method Last (line 79) | func (iterator *Iterator[T]) Last() bool {
method NextTo (line 88) | func (iterator *Iterator[T]) NextTo(f func(index int, value T) bool) bool {
method PrevTo (line 102) | func (iterator *Iterator[T]) PrevTo(f func(index int, value T) bool) bool {
FILE: lists/arraylist/serialization.go
method ToJSON (line 18) | func (list *List[T]) ToJSON() ([]byte, error) {
method FromJSON (line 23) | func (list *List[T]) FromJSON(data []byte) error {
method UnmarshalJSON (line 29) | func (list *List[T]) UnmarshalJSON(bytes []byte) error {
method MarshalJSON (line 34) | func (list *List[T]) MarshalJSON() ([]byte, error) {
FILE: lists/doublylinkedlist/doublylinkedlist.go
type List (line 25) | type List struct
type element (line 31) | type element struct
function New (line 38) | func New[T comparable](values ...T) *List[T] {
method Add (line 47) | func (list *List[T]) Add(values ...T) {
method Append (line 62) | func (list *List[T]) Append(values ...T) {
method Prepend (line 67) | func (list *List[T]) Prepend(values ...T) {
method Get (line 84) | func (list *List[T]) Get(index int) (T, bool) {
method Remove (line 105) | func (list *List[T]) Remove(index int) {
method Contains (line 150) | func (list *List[T]) Contains(values ...T) bool {
method Values (line 174) | func (list *List[T]) Values() []T {
method IndexOf (line 183) | func (list *List[T]) IndexOf(value T) int {
method Empty (line 196) | func (list *List[T]) Empty() bool {
method Size (line 201) | func (list *List[T]) Size() int {
method Clear (line 206) | func (list *List[T]) Clear() {
method Sort (line 213) | func (list *List[T]) Sort(comparator utils.Comparator[T]) {
method Swap (line 229) | func (list *List[T]) Swap(i, j int) {
method Insert (line 247) | func (list *List[T]) Insert(index int, values ...T) {
method Set (line 305) | func (list *List[T]) Set(index int, value T) {
method String (line 334) | func (list *List[T]) String() string {
method withinRange (line 345) | func (list *List[T]) withinRange(index int) bool {
FILE: lists/doublylinkedlist/doublylinkedlist_test.go
function TestListNew (line 15) | func TestListNew(t *testing.T) {
function TestListAdd (line 41) | func TestListAdd(t *testing.T) {
function TestListAppendAndPrepend (line 56) | func TestListAppendAndPrepend(t *testing.T) {
function TestListRemove (line 78) | func TestListRemove(t *testing.T) {
function TestListGet (line 97) | func TestListGet(t *testing.T) {
function TestListSwap (line 119) | func TestListSwap(t *testing.T) {
function TestListSort (line 129) | func TestListSort(t *testing.T) {
function TestListClear (line 143) | func TestListClear(t *testing.T) {
function TestListContains (line 155) | func TestListContains(t *testing.T) {
function TestListValues (line 180) | func TestListValues(t *testing.T) {
function TestListInsert (line 189) | func TestListInsert(t *testing.T) {
function TestListSet (line 213) | func TestListSet(t *testing.T) {
function TestListEach (line 234) | func TestListEach(t *testing.T) {
function TestListMap (line 257) | func TestListMap(t *testing.T) {
function TestListSelect (line 277) | func TestListSelect(t *testing.T) {
function TestListAny (line 294) | func TestListAny(t *testing.T) {
function TestListAll (line 310) | func TestListAll(t *testing.T) {
function TestListFind (line 326) | func TestListFind(t *testing.T) {
function TestListChaining (line 342) | func TestListChaining(t *testing.T) {
function TestListIteratorNextOnEmpty (line 361) | func TestListIteratorNextOnEmpty(t *testing.T) {
function TestListIteratorNext (line 369) | func TestListIteratorNext(t *testing.T) {
function TestListIteratorPrevOnEmpty (line 400) | func TestListIteratorPrevOnEmpty(t *testing.T) {
function TestListIteratorPrev (line 408) | func TestListIteratorPrev(t *testing.T) {
function TestListIteratorBegin (line 441) | func TestListIteratorBegin(t *testing.T) {
function TestListIteratorEnd (line 455) | func TestListIteratorEnd(t *testing.T) {
function TestListIteratorFirst (line 480) | func TestListIteratorFirst(t *testing.T) {
function TestListIteratorLast (line 495) | func TestListIteratorLast(t *testing.T) {
function TestListIteratorNextTo (line 510) | func TestListIteratorNextTo(t *testing.T) {
function TestListIteratorPrevTo (line 559) | func TestListIteratorPrevTo(t *testing.T) {
function TestListSerialization (line 610) | func TestListSerialization(t *testing.T) {
function TestListString (line 647) | func TestListString(t *testing.T) {
function benchmarkGet (line 655) | func benchmarkGet(b *testing.B, list *List[int], size int) {
function benchmarkAdd (line 663) | func benchmarkAdd(b *testing.B, list *List[int], size int) {
function benchmarkRemove (line 671) | func benchmarkRemove(b *testing.B, list *List[int], size int) {
function BenchmarkDoublyLinkedListGet100 (line 679) | func BenchmarkDoublyLinkedListGet100(b *testing.B) {
function BenchmarkDoublyLinkedListGet1000 (line 690) | func BenchmarkDoublyLinkedListGet1000(b *testing.B) {
function BenchmarkDoublyLinkedListGet10000 (line 701) | func BenchmarkDoublyLinkedListGet10000(b *testing.B) {
function BenchmarkDoublyLinkedListGet100000 (line 712) | func BenchmarkDoublyLinkedListGet100000(b *testing.B) {
function BenchmarkDoublyLinkedListAdd100 (line 723) | func BenchmarkDoublyLinkedListAdd100(b *testing.B) {
function BenchmarkDoublyLinkedListAdd1000 (line 731) | func BenchmarkDoublyLinkedListAdd1000(b *testing.B) {
function BenchmarkDoublyLinkedListAdd10000 (line 742) | func BenchmarkDoublyLinkedListAdd10000(b *testing.B) {
function BenchmarkDoublyLinkedListAdd100000 (line 753) | func BenchmarkDoublyLinkedListAdd100000(b *testing.B) {
function BenchmarkDoublyLinkedListRemove100 (line 764) | func BenchmarkDoublyLinkedListRemove100(b *testing.B) {
function BenchmarkDoublyLinkedListRemove1000 (line 775) | func BenchmarkDoublyLinkedListRemove1000(b *testing.B) {
function BenchmarkDoublyLinkedListRemove10000 (line 786) | func BenchmarkDoublyLinkedListRemove10000(b *testing.B) {
function BenchmarkDoublyLinkedListRemove100000 (line 797) | func BenchmarkDoublyLinkedListRemove100000(b *testing.B) {
FILE: lists/doublylinkedlist/enumerable.go
method Each (line 13) | func (list *List[T]) Each(f func(index int, value T)) {
method Map (line 22) | func (list *List[T]) Map(f func(index int, value T) T) *List[T] {
method Select (line 32) | func (list *List[T]) Select(f func(index int, value T) bool) *List[T] {
method Any (line 45) | func (list *List[T]) Any(f func(index int, value T) bool) bool {
method All (line 57) | func (list *List[T]) All(f func(index int, value T) bool) bool {
method Find (line 70) | func (list *List[T]) Find(f func(index int, value T) bool) (index int, v...
FILE: lists/doublylinkedlist/iterator.go
type Iterator (line 13) | type Iterator struct
method Iterator (line 20) | func (list *List[T]) Iterator() Iterator[T] {
method Next (line 28) | func (iterator *Iterator[T]) Next() bool {
method Prev (line 47) | func (iterator *Iterator[T]) Prev() bool {
method Value (line 65) | func (iterator *Iterator[T]) Value() T {
method Index (line 71) | func (iterator *Iterator[T]) Index() int {
method Begin (line 77) | func (iterator *Iterator[T]) Begin() {
method End (line 84) | func (iterator *Iterator[T]) End() {
method First (line 92) | func (iterator *Iterator[T]) First() bool {
method Last (line 100) | func (iterator *Iterator[T]) Last() bool {
method NextTo (line 109) | func (iterator *Iterator[T]) NextTo(f func(index int, value T) bool) bool {
method PrevTo (line 123) | func (iterator *Iterator[T]) PrevTo(f func(index int, value T) bool) bool {
FILE: lists/doublylinkedlist/serialization.go
method ToJSON (line 18) | func (list *List[T]) ToJSON() ([]byte, error) {
method FromJSON (line 23) | func (list *List[T]) FromJSON(data []byte) error {
method UnmarshalJSON (line 34) | func (list *List[T]) UnmarshalJSON(bytes []byte) error {
method MarshalJSON (line 39) | func (list *List[T]) MarshalJSON() ([]byte, error) {
FILE: lists/lists.go
type List (line 18) | type List interface
FILE: lists/singlylinkedlist/enumerable.go
method Each (line 13) | func (list *List[T]) Each(f func(index int, value T)) {
method Map (line 22) | func (list *List[T]) Map(f func(index int, value T) T) *List[T] {
method Select (line 32) | func (list *List[T]) Select(f func(index int, value T) bool) *List[T] {
method Any (line 45) | func (list *List[T]) Any(f func(index int, value T) bool) bool {
method All (line 57) | func (list *List[T]) All(f func(index int, value T) bool) bool {
method Find (line 70) | func (list *List[T]) Find(f func(index int, value T) bool) (index int, v...
FILE: lists/singlylinkedlist/iterator.go
type Iterator (line 13) | type Iterator struct
method Iterator (line 20) | func (list *List[T]) Iterator() *Iterator[T] {
method Next (line 28) | func (iterator *Iterator[T]) Next() bool {
method Value (line 46) | func (iterator *Iterator[T]) Value() T {
method Index (line 52) | func (iterator *Iterator[T]) Index() int {
method Begin (line 58) | func (iterator *Iterator[T]) Begin() {
method First (line 66) | func (iterator *Iterator[T]) First() bool {
method NextTo (line 75) | func (iterator *Iterator[T]) NextTo(f func(index int, value T) bool) bool {
FILE: lists/singlylinkedlist/serialization.go
method ToJSON (line 18) | func (list *List[T]) ToJSON() ([]byte, error) {
method FromJSON (line 23) | func (list *List[T]) FromJSON(data []byte) error {
method UnmarshalJSON (line 34) | func (list *List[T]) UnmarshalJSON(bytes []byte) error {
method MarshalJSON (line 39) | func (list *List[T]) MarshalJSON() ([]byte, error) {
FILE: lists/singlylinkedlist/singlylinkedlist.go
type List (line 25) | type List struct
type element (line 31) | type element struct
function New (line 37) | func New[T comparable](values ...T) *List[T] {
method Add (line 46) | func (list *List[T]) Add(values ...T) {
method Append (line 61) | func (list *List[T]) Append(values ...T) {
method Prepend (line 66) | func (list *List[T]) Prepend(values ...T) {
method Get (line 80) | func (list *List[T]) Get(index int) (T, bool) {
method Remove (line 95) | func (list *List[T]) Remove(index int) {
method Contains (line 131) | func (list *List[T]) Contains(values ...T) bool {
method Values (line 155) | func (list *List[T]) Values() []T {
method IndexOf (line 164) | func (list *List[T]) IndexOf(value T) int {
method Empty (line 177) | func (list *List[T]) Empty() bool {
method Size (line 182) | func (list *List[T]) Size() int {
method Clear (line 187) | func (list *List[T]) Clear() {
method Sort (line 194) | func (list *List[T]) Sort(comparator utils.Comparator[T]) {
method Swap (line 210) | func (list *List[T]) Swap(i, j int) {
method Insert (line 228) | func (list *List[T]) Insert(index int, values ...T) {
method Set (line 272) | func (list *List[T]) Set(index int, value T) {
method String (line 290) | func (list *List[T]) String() string {
method withinRange (line 301) | func (list *List[T]) withinRange(index int) bool {
FILE: lists/singlylinkedlist/singlylinkedlist_test.go
function TestListNew (line 15) | func TestListNew(t *testing.T) {
function TestListAdd (line 41) | func TestListAdd(t *testing.T) {
function TestListAppendAndPrepend (line 56) | func TestListAppendAndPrepend(t *testing.T) {
function TestListRemove (line 78) | func TestListRemove(t *testing.T) {
function TestListGet (line 97) | func TestListGet(t *testing.T) {
function TestListSwap (line 119) | func TestListSwap(t *testing.T) {
function TestListSort (line 129) | func TestListSort(t *testing.T) {
function TestListClear (line 143) | func TestListClear(t *testing.T) {
function TestListContains (line 155) | func TestListContains(t *testing.T) {
function TestListValues (line 180) | func TestListValues(t *testing.T) {
function TestListIndexOf (line 189) | func TestListIndexOf(t *testing.T) {
function TestListInsert (line 216) | func TestListInsert(t *testing.T) {
function TestListSet (line 233) | func TestListSet(t *testing.T) {
function TestListEach (line 254) | func TestListEach(t *testing.T) {
function TestListMap (line 277) | func TestListMap(t *testing.T) {
function TestListSelect (line 297) | func TestListSelect(t *testing.T) {
function TestListAny (line 314) | func TestListAny(t *testing.T) {
function TestListAll (line 330) | func TestListAll(t *testing.T) {
function TestListFind (line 346) | func TestListFind(t *testing.T) {
function TestListChaining (line 362) | func TestListChaining(t *testing.T) {
function TestListIteratorNextOnEmpty (line 381) | func TestListIteratorNextOnEmpty(t *testing.T) {
function TestListIteratorNext (line 389) | func TestListIteratorNext(t *testing.T) {
function TestListIteratorBegin (line 420) | func TestListIteratorBegin(t *testing.T) {
function TestListIteratorFirst (line 434) | func TestListIteratorFirst(t *testing.T) {
function TestListIteratorNextTo (line 449) | func TestListIteratorNextTo(t *testing.T) {
function TestListSerialization (line 498) | func TestListSerialization(t *testing.T) {
function TestListString (line 535) | func TestListString(t *testing.T) {
function benchmarkGet (line 543) | func benchmarkGet(b *testing.B, list *List[int], size int) {
function benchmarkAdd (line 551) | func benchmarkAdd(b *testing.B, list *List[int], size int) {
function benchmarkRemove (line 559) | func benchmarkRemove(b *testing.B, list *List[int], size int) {
function BenchmarkSinglyLinkedListGet100 (line 567) | func BenchmarkSinglyLinkedListGet100(b *testing.B) {
function BenchmarkSinglyLinkedListGet1000 (line 578) | func BenchmarkSinglyLinkedListGet1000(b *testing.B) {
function BenchmarkSinglyLinkedListGet10000 (line 589) | func BenchmarkSinglyLinkedListGet10000(b *testing.B) {
function BenchmarkSinglyLinkedListGet100000 (line 600) | func BenchmarkSinglyLinkedListGet100000(b *testing.B) {
function BenchmarkSinglyLinkedListAdd100 (line 611) | func BenchmarkSinglyLinkedListAdd100(b *testing.B) {
function BenchmarkSinglyLinkedListAdd1000 (line 619) | func BenchmarkSinglyLinkedListAdd1000(b *testing.B) {
function BenchmarkSinglyLinkedListAdd10000 (line 630) | func BenchmarkSinglyLinkedListAdd10000(b *testing.B) {
function BenchmarkSinglyLinkedListAdd100000 (line 641) | func BenchmarkSinglyLinkedListAdd100000(b *testing.B) {
function BenchmarkSinglyLinkedListRemove100 (line 652) | func BenchmarkSinglyLinkedListRemove100(b *testing.B) {
function BenchmarkSinglyLinkedListRemove1000 (line 663) | func BenchmarkSinglyLinkedListRemove1000(b *testing.B) {
function BenchmarkSinglyLinkedListRemove10000 (line 674) | func BenchmarkSinglyLinkedListRemove10000(b *testing.B) {
function BenchmarkSinglyLinkedListRemove100000 (line 685) | func BenchmarkSinglyLinkedListRemove100000(b *testing.B) {
FILE: maps/hashbidimap/hashbidimap.go
type Map (line 29) | type Map struct
function New (line 35) | func New[K, V comparable]() *Map[K, V] {
method Put (line 40) | func (m *Map[K, V]) Put(key K, value V) {
method Get (line 53) | func (m *Map[K, V]) Get(key K) (value V, found bool) {
method GetKey (line 59) | func (m *Map[K, V]) GetKey(value V) (key K, found bool) {
method Remove (line 64) | func (m *Map[K, V]) Remove(key K) {
method Empty (line 72) | func (m *Map[K, V]) Empty() bool {
method Size (line 77) | func (m *Map[K, V]) Size() int {
method Keys (line 82) | func (m *Map[K, V]) Keys() []K {
method Values (line 87) | func (m *Map[K, V]) Values() []V {
method Clear (line 92) | func (m *Map[K, V]) Clear() {
method String (line 98) | func (m *Map[K, V]) String() string {
FILE: maps/hashbidimap/hashbidimap_test.go
function TestMapPut (line 15) | func TestMapPut(t *testing.T) {
function TestMapRemove (line 53) | func TestMapRemove(t *testing.T) {
function TestMapGetKey (line 112) | func TestMapGetKey(t *testing.T) {
function TestMapSerialization (line 144) | func TestMapSerialization(t *testing.T) {
function TestMapString (line 181) | func TestMapString(t *testing.T) {
function benchmarkGet (line 189) | func benchmarkGet(b *testing.B, m *Map[int, int], size int) {
function benchmarkPut (line 197) | func benchmarkPut(b *testing.B, m *Map[int, int], size int) {
function benchmarkRemove (line 205) | func benchmarkRemove(b *testing.B, m *Map[int, int], size int) {
function BenchmarkHashBidiMapGet100 (line 213) | func BenchmarkHashBidiMapGet100(b *testing.B) {
function BenchmarkHashBidiMapGet1000 (line 224) | func BenchmarkHashBidiMapGet1000(b *testing.B) {
function BenchmarkHashBidiMapGet10000 (line 235) | func BenchmarkHashBidiMapGet10000(b *testing.B) {
function BenchmarkHashBidiMapGet100000 (line 246) | func BenchmarkHashBidiMapGet100000(b *testing.B) {
function BenchmarkHashBidiMapPut100 (line 257) | func BenchmarkHashBidiMapPut100(b *testing.B) {
function BenchmarkHashBidiMapPut1000 (line 265) | func BenchmarkHashBidiMapPut1000(b *testing.B) {
function BenchmarkHashBidiMapPut10000 (line 276) | func BenchmarkHashBidiMapPut10000(b *testing.B) {
function BenchmarkHashBidiMapPut100000 (line 287) | func BenchmarkHashBidiMapPut100000(b *testing.B) {
function BenchmarkHashBidiMapRemove100 (line 298) | func BenchmarkHashBidiMapRemove100(b *testing.B) {
function BenchmarkHashBidiMapRemove1000 (line 309) | func BenchmarkHashBidiMapRemove1000(b *testing.B) {
function BenchmarkHashBidiMapRemove10000 (line 320) | func BenchmarkHashBidiMapRemove10000(b *testing.B) {
function BenchmarkHashBidiMapRemove100000 (line 331) | func BenchmarkHashBidiMapRemove100000(b *testing.B) {
FILE: maps/hashbidimap/serialization.go
method ToJSON (line 18) | func (m *Map[K, V]) ToJSON() ([]byte, error) {
method FromJSON (line 23) | func (m *Map[K, V]) FromJSON(data []byte) error {
method UnmarshalJSON (line 39) | func (m *Map[K, V]) UnmarshalJSON(bytes []byte) error {
method MarshalJSON (line 44) | func (m *Map[K, V]) MarshalJSON() ([]byte, error) {
FILE: maps/hashmap/hashmap.go
type Map (line 24) | type Map struct
function New (line 29) | func New[K comparable, V any]() *Map[K, V] {
method Put (line 34) | func (m *Map[K, V]) Put(key K, value V) {
method Get (line 40) | func (m *Map[K, V]) Get(key K) (value V, found bool) {
method Remove (line 46) | func (m *Map[K, V]) Remove(key K) {
method Empty (line 51) | func (m *Map[K, V]) Empty() bool {
method Size (line 56) | func (m *Map[K, V]) Size() int {
method Keys (line 61) | func (m *Map[K, V]) Keys() []K {
method Values (line 72) | func (m *Map[K, V]) Values() []V {
method Clear (line 83) | func (m *Map[K, V]) Clear() {
method String (line 88) | func (m *Map[K, V]) String() string {
FILE: maps/hashmap/hashmap_test.go
function TestMapPut (line 15) | func TestMapPut(t *testing.T) {
function TestMapRemove (line 53) | func TestMapRemove(t *testing.T) {
function TestMapSerialization (line 112) | func TestMapSerialization(t *testing.T) {
function TestMapString (line 149) | func TestMapString(t *testing.T) {
function benchmarkGet (line 157) | func benchmarkGet(b *testing.B, m *Map[int, int], size int) {
function benchmarkPut (line 165) | func benchmarkPut(b *testing.B, m *Map[int, int], size int) {
function benchmarkRemove (line 173) | func benchmarkRemove(b *testing.B, m *Map[int, int], size int) {
function BenchmarkHashMapGet100 (line 181) | func BenchmarkHashMapGet100(b *testing.B) {
function BenchmarkHashMapGet1000 (line 192) | func BenchmarkHashMapGet1000(b *testing.B) {
function BenchmarkHashMapGet10000 (line 203) | func BenchmarkHashMapGet10000(b *testing.B) {
function BenchmarkHashMapGet100000 (line 214) | func BenchmarkHashMapGet100000(b *testing.B) {
function BenchmarkHashMapPut100 (line 225) | func BenchmarkHashMapPut100(b *testing.B) {
function BenchmarkHashMapPut1000 (line 233) | func BenchmarkHashMapPut1000(b *testing.B) {
function BenchmarkHashMapPut10000 (line 244) | func BenchmarkHashMapPut10000(b *testing.B) {
function BenchmarkHashMapPut100000 (line 255) | func BenchmarkHashMapPut100000(b *testing.B) {
function BenchmarkHashMapRemove100 (line 266) | func BenchmarkHashMapRemove100(b *testing.B) {
function BenchmarkHashMapRemove1000 (line 277) | func BenchmarkHashMapRemove1000(b *testing.B) {
function BenchmarkHashMapRemove10000 (line 288) | func BenchmarkHashMapRemove10000(b *testing.B) {
function BenchmarkHashMapRemove100000 (line 299) | func BenchmarkHashMapRemove100000(b *testing.B) {
FILE: maps/hashmap/serialization.go
method ToJSON (line 18) | func (m *Map[K, V]) ToJSON() ([]byte, error) {
method FromJSON (line 23) | func (m *Map[K, V]) FromJSON(data []byte) error {
method UnmarshalJSON (line 28) | func (m *Map[K, V]) UnmarshalJSON(bytes []byte) error {
method MarshalJSON (line 33) | func (m *Map[K, V]) MarshalJSON() ([]byte, error) {
FILE: maps/linkedhashmap/enumerable.go
method Each (line 13) | func (m *Map[K, V]) Each(f func(key K, value V)) {
method Map (line 22) | func (m *Map[K, V]) Map(f func(key1 K, value1 V) (K, V)) *Map[K, V] {
method Select (line 33) | func (m *Map[K, V]) Select(f func(key K, value V) bool) *Map[K, V] {
method Any (line 46) | func (m *Map[K, V]) Any(f func(key K, value V) bool) bool {
method All (line 58) | func (m *Map[K, V]) All(f func(key K, value V) bool) bool {
method Find (line 71) | func (m *Map[K, V]) Find(f func(key K, value V) bool) (k K, v V) {
FILE: maps/linkedhashmap/iterator.go
type Iterator (line 16) | type Iterator struct
method Iterator (line 22) | func (m *Map[K, V]) Iterator() *Iterator[K, V] {
method Next (line 33) | func (iterator *Iterator[K, V]) Next() bool {
method Prev (line 40) | func (iterator *Iterator[K, V]) Prev() bool {
method Value (line 46) | func (iterator *Iterator[K, V]) Value() V {
method Key (line 53) | func (iterator *Iterator[K, V]) Key() K {
method Begin (line 59) | func (iterator *Iterator[K, V]) Begin() {
method End (line 65) | func (iterator *Iterator[K, V]) End() {
method First (line 72) | func (iterator *Iterator[K, V]) First() bool {
method Last (line 79) | func (iterator *Iterator[K, V]) Last() bool {
method NextTo (line 87) | func (iterator *Iterator[K, V]) NextTo(f func(key K, value V) bool) bool {
method PrevTo (line 101) | func (iterator *Iterator[K, V]) PrevTo(f func(key K, value V) bool) bool {
FILE: maps/linkedhashmap/linkedhashmap.go
type Map (line 26) | type Map struct
function New (line 32) | func New[K comparable, V any]() *Map[K, V] {
method Put (line 41) | func (m *Map[K, V]) Put(key K, value V) {
method Get (line 51) | func (m *Map[K, V]) Get(key K) (value V, found bool) {
method Remove (line 58) | func (m *Map[K, V]) Remove(key K) {
method Empty (line 67) | func (m *Map[K, V]) Empty() bool {
method Size (line 72) | func (m *Map[K, V]) Size() int {
method Keys (line 77) | func (m *Map[K, V]) Keys() []K {
method Values (line 82) | func (m *Map[K, V]) Values() []V {
method Clear (line 94) | func (m *Map[K, V]) Clear() {
method String (line 100) | func (m *Map[K, V]) String() string {
FILE: maps/linkedhashmap/linkedhashmap_test.go
function TestMapPut (line 15) | func TestMapPut(t *testing.T) {
function TestMapRemove (line 53) | func TestMapRemove(t *testing.T) {
function TestMapEach (line 112) | func TestMapEach(t *testing.T) {
function TestMapMap (line 142) | func TestMapMap(t *testing.T) {
function TestMapSelect (line 164) | func TestMapSelect(t *testing.T) {
function TestMapAny (line 183) | func TestMapAny(t *testing.T) {
function TestMapAll (line 202) | func TestMapAll(t *testing.T) {
function TestMapFind (line 221) | func TestMapFind(t *testing.T) {
function TestMapChaining (line 240) | func TestMapChaining(t *testing.T) {
function TestMapIteratorNextOnEmpty (line 264) | func TestMapIteratorNextOnEmpty(t *testing.T) {
function TestMapIteratorPrevOnEmpty (line 273) | func TestMapIteratorPrevOnEmpty(t *testing.T) {
function TestMapIteratorNext (line 282) | func TestMapIteratorNext(t *testing.T) {
function TestMapIteratorPrev (line 319) | func TestMapIteratorPrev(t *testing.T) {
function TestMapIteratorBegin (line 358) | func TestMapIteratorBegin(t *testing.T) {
function TestMapIteratorEnd (line 374) | func TestMapIteratorEnd(t *testing.T) {
function TestMapIteratorFirst (line 387) | func TestMapIteratorFirst(t *testing.T) {
function TestMapIteratorLast (line 401) | func TestMapIteratorLast(t *testing.T) {
function TestMapIteratorNextTo (line 415) | func TestMapIteratorNextTo(t *testing.T) {
function TestMapIteratorPrevTo (line 467) | func TestMapIteratorPrevTo(t *testing.T) {
function TestMapSerialization (line 521) | func TestMapSerialization(t *testing.T) {
function TestMapString (line 568) | func TestMapString(t *testing.T) {
function benchmarkGet (line 576) | func benchmarkGet(b *testing.B, m *Map[int, int], size int) {
function benchmarkPut (line 584) | func benchmarkPut(b *testing.B, m *Map[int, int], size int) {
function benchmarkRemove (line 592) | func benchmarkRemove(b *testing.B, m *Map[int, int], size int) {
function BenchmarkTreeMapGet100 (line 600) | func BenchmarkTreeMapGet100(b *testing.B) {
function BenchmarkTreeMapGet1000 (line 611) | func BenchmarkTreeMapGet1000(b *testing.B) {
function BenchmarkTreeMapGet10000 (line 622) | func BenchmarkTreeMapGet10000(b *testing.B) {
function BenchmarkTreeMapGet100000 (line 633) | func BenchmarkTreeMapGet100000(b *testing.B) {
function BenchmarkTreeMapPut100 (line 644) | func BenchmarkTreeMapPut100(b *testing.B) {
function BenchmarkTreeMapPut1000 (line 652) | func BenchmarkTreeMapPut1000(b *testing.B) {
function BenchmarkTreeMapPut10000 (line 663) | func BenchmarkTreeMapPut10000(b *testing.B) {
function BenchmarkTreeMapPut100000 (line 674) | func BenchmarkTreeMapPut100000(b *testing.B) {
function BenchmarkTreeMapRemove100 (line 685) | func BenchmarkTreeMapRemove100(b *testing.B) {
function BenchmarkTreeMapRemove1000 (line 696) | func BenchmarkTreeMapRemove1000(b *testing.B) {
function BenchmarkTreeMapRemove10000 (line 707) | func BenchmarkTreeMapRemove10000(b *testing.B) {
function BenchmarkTreeMapRemove100000 (line 718) | func BenchmarkTreeMapRemove100000(b *testing.B) {
FILE: maps/linkedhashmap/serialization.go
method ToJSON (line 21) | func (m *Map[K, V]) ToJSON() ([]byte, error) {
method FromJSON (line 72) | func (m *Map[K, V]) FromJSON(data []byte) error {
method UnmarshalJSON (line 103) | func (m *Map[K, V]) UnmarshalJSON(bytes []byte) error {
method MarshalJSON (line 108) | func (m *Map[K, V]) MarshalJSON() ([]byte, error) {
FILE: maps/maps.go
type Map (line 21) | type Map interface
type BidiMap (line 36) | type BidiMap interface
FILE: maps/treebidimap/enumerable.go
method Each (line 13) | func (m *Map[K, V]) Each(f func(key K, value V)) {
method Map (line 22) | func (m *Map[K, V]) Map(f func(key1 K, value1 V) (K, V)) *Map[K, V] {
method Select (line 33) | func (m *Map[K, V]) Select(f func(key K, value V) bool) *Map[K, V] {
method Any (line 46) | func (m *Map[K, V]) Any(f func(key K, value V) bool) bool {
method All (line 58) | func (m *Map[K, V]) All(f func(key K, value V) bool) bool {
method Find (line 71) | func (m *Map[K, V]) Find(f func(key K, value V) bool) (k K, v V) {
FILE: maps/treebidimap/iterator.go
type Iterator (line 16) | type Iterator struct
method Iterator (line 21) | func (m *Map[K, V]) Iterator() *Iterator[K, V] {
method Next (line 29) | func (iterator *Iterator[K, V]) Next() bool {
method Prev (line 36) | func (iterator *Iterator[K, V]) Prev() bool {
method Value (line 42) | func (iterator *Iterator[K, V]) Value() V {
method Key (line 48) | func (iterator *Iterator[K, V]) Key() K {
method Begin (line 54) | func (iterator *Iterator[K, V]) Begin() {
method End (line 60) | func (iterator *Iterator[K, V]) End() {
method First (line 67) | func (iterator *Iterator[K, V]) First() bool {
method Last (line 74) | func (iterator *Iterator[K, V]) Last() bool {
method NextTo (line 82) | func (iterator *Iterator[K, V]) NextTo(f func(key K, value V) bool) bool {
method PrevTo (line 96) | func (iterator *Iterator[K, V]) PrevTo(f func(key K, value V) bool) bool {
FILE: maps/treebidimap/serialization.go
method ToJSON (line 18) | func (m *Map[K, V]) ToJSON() ([]byte, error) {
method FromJSON (line 23) | func (m *Map[K, V]) FromJSON(data []byte) error {
method UnmarshalJSON (line 39) | func (m *Map[K, V]) UnmarshalJSON(bytes []byte) error {
method MarshalJSON (line 44) | func (m *Map[K, V]) MarshalJSON() ([]byte, error) {
FILE: maps/treebidimap/treebidimap.go
type Map (line 35) | type Map struct
function New (line 41) | func New[K, V cmp.Ordered]() *Map[K, V] {
function NewWith (line 49) | func NewWith[K, V comparable](keyComparator utils.Comparator[K], valueCo...
method Put (line 57) | func (m *Map[K, V]) Put(key K, value V) {
method Get (line 70) | func (m *Map[K, V]) Get(key K) (value V, found bool) {
method GetKey (line 76) | func (m *Map[K, V]) GetKey(value V) (key K, found bool) {
method Remove (line 81) | func (m *Map[K, V]) Remove(key K) {
method Empty (line 89) | func (m *Map[K, V]) Empty() bool {
method Size (line 94) | func (m *Map[K, V]) Size() int {
method Keys (line 99) | func (m *Map[K, V]) Keys() []K {
method Values (line 104) | func (m *Map[K, V]) Values() []V {
method Clear (line 109) | func (m *Map[K, V]) Clear() {
method String (line 115) | func (m *Map[K, V]) String() string {
FILE: maps/treebidimap/treebidimap_test.go
function TestMapPut (line 15) | func TestMapPut(t *testing.T) {
function TestMapRemove (line 53) | func TestMapRemove(t *testing.T) {
function TestMapGetKey (line 112) | func TestMapGetKey(t *testing.T) {
function sameElements (line 144) | func sameElements(a []interface{}, b []interface{}) bool {
function TestMapEach (line 163) | func TestMapEach(t *testing.T) {
function TestMapMap (line 193) | func TestMapMap(t *testing.T) {
function TestMapSelect (line 215) | func TestMapSelect(t *testing.T) {
function TestMapAny (line 234) | func TestMapAny(t *testing.T) {
function TestMapAll (line 253) | func TestMapAll(t *testing.T) {
function TestMapFind (line 272) | func TestMapFind(t *testing.T) {
function TestMapChaining (line 291) | func TestMapChaining(t *testing.T) {
function TestMapIteratorNextOnEmpty (line 315) | func TestMapIteratorNextOnEmpty(t *testing.T) {
function TestMapIteratorPrevOnEmpty (line 324) | func TestMapIteratorPrevOnEmpty(t *testing.T) {
function TestMapIteratorNext (line 333) | func TestMapIteratorNext(t *testing.T) {
function TestMapIteratorPrev (line 370) | func TestMapIteratorPrev(t *testing.T) {
function TestMapIteratorBegin (line 409) | func TestMapIteratorBegin(t *testing.T) {
function TestMapIteratorEnd (line 425) | func TestMapIteratorEnd(t *testing.T) {
function TestMapIteratorFirst (line 438) | func TestMapIteratorFirst(t *testing.T) {
function TestMapIteratorLast (line 452) | func TestMapIteratorLast(t *testing.T) {
function TestMapIteratorNextTo (line 466) | func TestMapIteratorNextTo(t *testing.T) {
function TestMapIteratorPrevTo (line 518) | func TestMapIteratorPrevTo(t *testing.T) {
function TestMapSerialization (line 572) | func TestMapSerialization(t *testing.T) {
function TestMapString (line 619) | func TestMapString(t *testing.T) {
function benchmarkGet (line 627) | func benchmarkGet(b *testing.B, m *Map[int, int], size int) {
function benchmarkPut (line 635) | func benchmarkPut(b *testing.B, m *Map[int, int], size int) {
function benchmarkRemove (line 643) | func benchmarkRemove(b *testing.B, m *Map[int, int], size int) {
function BenchmarkTreeBidiMapGet100 (line 651) | func BenchmarkTreeBidiMapGet100(b *testing.B) {
function BenchmarkTreeBidiMapGet1000 (line 662) | func BenchmarkTreeBidiMapGet1000(b *testing.B) {
function BenchmarkTreeBidiMapGet10000 (line 673) | func BenchmarkTreeBidiMapGet10000(b *testing.B) {
function BenchmarkTreeBidiMapGet100000 (line 684) | func BenchmarkTreeBidiMapGet100000(b *testing.B) {
function BenchmarkTreeBidiMapPut100 (line 695) | func BenchmarkTreeBidiMapPut100(b *testing.B) {
function BenchmarkTreeBidiMapPut1000 (line 703) | func BenchmarkTreeBidiMapPut1000(b *testing.B) {
function BenchmarkTreeBidiMapPut10000 (line 714) | func BenchmarkTreeBidiMapPut10000(b *testing.B) {
function BenchmarkTreeBidiMapPut100000 (line 725) | func BenchmarkTreeBidiMapPut100000(b *testing.B) {
function BenchmarkTreeBidiMapRemove100 (line 736) | func BenchmarkTreeBidiMapRemove100(b *testing.B) {
function BenchmarkTreeBidiMapRemove1000 (line 747) | func BenchmarkTreeBidiMapRemove1000(b *testing.B) {
function BenchmarkTreeBidiMapRemove10000 (line 758) | func BenchmarkTreeBidiMapRemove10000(b *testing.B) {
function BenchmarkTreeBidiMapRemove100000 (line 769) | func BenchmarkTreeBidiMapRemove100000(b *testing.B) {
FILE: maps/treemap/enumerable.go
method Each (line 16) | func (m *Map[K, V]) Each(f func(key K, value V)) {
method Map (line 25) | func (m *Map[K, V]) Map(f func(key1 K, value1 V) (K, V)) *Map[K, V] {
method Select (line 36) | func (m *Map[K, V]) Select(f func(key K, value V) bool) *Map[K, V] {
method Any (line 49) | func (m *Map[K, V]) Any(f func(key K, value V) bool) bool {
method All (line 61) | func (m *Map[K, V]) All(f func(key K, value V) bool) bool {
method Find (line 74) | func (m *Map[K, V]) Find(f func(key K, value V) bool) (k K, v V) {
FILE: maps/treemap/iterator.go
type Iterator (line 16) | type Iterator struct
method Iterator (line 21) | func (m *Map[K, V]) Iterator() *Iterator[K, V] {
method Next (line 29) | func (iterator *Iterator[K, V]) Next() bool {
method Prev (line 36) | func (iterator *Iterator[K, V]) Prev() bool {
method Value (line 42) | func (iterator *Iterator[K, V]) Value() V {
method Key (line 48) | func (iterator *Iterator[K, V]) Key() K {
method Begin (line 54) | func (iterator *Iterator[K, V]) Begin() {
method End (line 60) | func (iterator *Iterator[K, V]) End() {
method First (line 67) | func (iterator *Iterator[K, V]) First() bool {
method Last (line 74) | func (iterator *Iterator[K, V]) Last() bool {
method NextTo (line 82) | func (iterator *Iterator[K, V]) NextTo(f func(key K, value V) bool) bool {
method PrevTo (line 96) | func (iterator *Iterator[K, V]) PrevTo(f func(key K, value V) bool) bool {
FILE: maps/treemap/serialization.go
method ToJSON (line 16) | func (m *Map[K, V]) ToJSON() ([]byte, error) {
method FromJSON (line 21) | func (m *Map[K, V]) FromJSON(data []byte) error {
method UnmarshalJSON (line 26) | func (m *Map[K, V]) UnmarshalJSON(bytes []byte) error {
method MarshalJSON (line 31) | func (m *Map[K, V]) MarshalJSON() ([]byte, error) {
FILE: maps/treemap/treemap.go
type Map (line 28) | type Map struct
function New (line 33) | func New[K cmp.Ordered, V any]() *Map[K, V] {
function NewWith (line 38) | func NewWith[K comparable, V any](comparator utils.Comparator[K]) *Map[K...
method Put (line 44) | func (m *Map[K, V]) Put(key K, value V) {
method Get (line 51) | func (m *Map[K, V]) Get(key K) (value V, found bool) {
method Remove (line 57) | func (m *Map[K, V]) Remove(key K) {
method Empty (line 62) | func (m *Map[K, V]) Empty() bool {
method Size (line 67) | func (m *Map[K, V]) Size() int {
method Keys (line 72) | func (m *Map[K, V]) Keys() []K {
method Values (line 77) | func (m *Map[K, V]) Values() []V {
method Clear (line 82) | func (m *Map[K, V]) Clear() {
method Min (line 88) | func (m *Map[K, V]) Min() (key K, value V, ok bool) {
method Max (line 97) | func (m *Map[K, V]) Max() (key K, value V, ok bool) {
method Floor (line 113) | func (m *Map[K, V]) Floor(key K) (foundKey K, foundValue V, ok bool) {
method Ceiling (line 130) | func (m *Map[K, V]) Ceiling(key K) (foundKey K, foundValue V, ok bool) {
method String (line 139) | func (m *Map[K, V]) String() string {
FILE: maps/treemap/treemap_test.go
function TestMapPut (line 15) | func TestMapPut(t *testing.T) {
function TestMapMin (line 53) | func TestMapMin(t *testing.T) {
function TestMapMax (line 82) | func TestMapMax(t *testing.T) {
function TestMapClear (line 111) | func TestMapClear(t *testing.T) {
function TestMapRemove (line 126) | func TestMapRemove(t *testing.T) {
function TestMapFloor (line 185) | func TestMapFloor(t *testing.T) {
function TestMapCeiling (line 212) | func TestMapCeiling(t *testing.T) {
function TestMapEach (line 239) | func TestMapEach(t *testing.T) {
function TestMapMap (line 269) | func TestMapMap(t *testing.T) {
function TestMapSelect (line 291) | func TestMapSelect(t *testing.T) {
function TestMapAny (line 310) | func TestMapAny(t *testing.T) {
function TestMapAll (line 329) | func TestMapAll(t *testing.T) {
function TestMapFind (line 348) | func TestMapFind(t *testing.T) {
function TestMapChaining (line 367) | func TestMapChaining(t *testing.T) {
function TestMapIteratorNextOnEmpty (line 391) | func TestMapIteratorNextOnEmpty(t *testing.T) {
function TestMapIteratorPrevOnEmpty (line 400) | func TestMapIteratorPrevOnEmpty(t *testing.T) {
function TestMapIteratorNext (line 409) | func TestMapIteratorNext(t *testing.T) {
function TestMapIteratorPrev (line 446) | func TestMapIteratorPrev(t *testing.T) {
function TestMapIteratorBegin (line 485) | func TestMapIteratorBegin(t *testing.T) {
function TestMapIteratorEnd (line 501) | func TestMapIteratorEnd(t *testing.T) {
function TestMapIteratorFirst (line 514) | func TestMapIteratorFirst(t *testing.T) {
function TestMapIteratorLast (line 528) | func TestMapIteratorLast(t *testing.T) {
function TestMapIteratorNextTo (line 542) | func TestMapIteratorNextTo(t *testing.T) {
function TestMapIteratorPrevTo (line 594) | func TestMapIteratorPrevTo(t *testing.T) {
function TestMapSerialization (line 648) | func TestMapSerialization(t *testing.T) {
function TestMapString (line 689) | func TestMapString(t *testing.T) {
function assertSerialization (line 698) | func assertSerialization(m *Map[string, string], txt string, t *testing....
function benchmarkGet (line 720) | func benchmarkGet(b *testing.B, m *Map[int, struct{}], size int) {
function benchmarkPut (line 728) | func benchmarkPut(b *testing.B, m *Map[int, struct{}], size int) {
function benchmarkRemove (line 736) | func benchmarkRemove(b *testing.B, m *Map[int, struct{}], size int) {
function BenchmarkTreeMapGet100 (line 744) | func BenchmarkTreeMapGet100(b *testing.B) {
function BenchmarkTreeMapGet1000 (line 755) | func BenchmarkTreeMapGet1000(b *testing.B) {
function BenchmarkTreeMapGet10000 (line 766) | func BenchmarkTreeMapGet10000(b *testing.B) {
function BenchmarkTreeMapGet100000 (line 777) | func BenchmarkTreeMapGet100000(b *testing.B) {
function BenchmarkTreeMapPut100 (line 788) | func BenchmarkTreeMapPut100(b *testing.B) {
function BenchmarkTreeMapPut1000 (line 796) | func BenchmarkTreeMapPut1000(b *testing.B) {
function BenchmarkTreeMapPut10000 (line 807) | func BenchmarkTreeMapPut10000(b *testing.B) {
function BenchmarkTreeMapPut100000 (line 818) | func BenchmarkTreeMapPut100000(b *testing.B) {
function BenchmarkTreeMapRemove100 (line 829) | func BenchmarkTreeMapRemove100(b *testing.B) {
function BenchmarkTreeMapRemove1000 (line 840) | func BenchmarkTreeMapRemove1000(b *testing.B) {
function BenchmarkTreeMapRemove10000 (line 851) | func BenchmarkTreeMapRemove10000(b *testing.B) {
function BenchmarkTreeMapRemove100000 (line 862) | func BenchmarkTreeMapRemove100000(b *testing.B) {
FILE: queues/arrayqueue/arrayqueue.go
type Queue (line 24) | type Queue struct
function New (line 29) | func New[T comparable]() *Queue[T] {
method Enqueue (line 34) | func (queue *Queue[T]) Enqueue(value T) {
method Dequeue (line 40) | func (queue *Queue[T]) Dequeue() (value T, ok bool) {
method Peek (line 50) | func (queue *Queue[T]) Peek() (value T, ok bool) {
method Empty (line 55) | func (queue *Queue[T]) Empty() bool {
method Size (line 60) | func (queue *Queue[T]) Size() int {
method Clear (line 65) | func (queue *Queue[T]) Clear() {
method Values (line 70) | func (queue *Queue[T]) Values() []T {
method String (line 75) | func (queue *Queue[T]) String() string {
method withinRange (line 86) | func (queue *Queue[T]) withinRange(index int) bool {
FILE: queues/arrayqueue/arrayqueue_test.go
function TestQueueEnqueue (line 15) | func TestQueueEnqueue(t *testing.T) {
function TestQueuePeek (line 38) | func TestQueuePeek(t *testing.T) {
function TestQueueDequeue (line 51) | func TestQueueDequeue(t *testing.T) {
function TestQueueIteratorOnEmpty (line 77) | func TestQueueIteratorOnEmpty(t *testing.T) {
function TestQueueIteratorNext (line 85) | func TestQueueIteratorNext(t *testing.T) {
function TestQueueIteratorPrev (line 128) | func TestQueueIteratorPrev(t *testing.T) {
function TestQueueIteratorBegin (line 167) | func TestQueueIteratorBegin(t *testing.T) {
function TestQueueIteratorEnd (line 183) | func TestQueueIteratorEnd(t *testing.T) {
function TestQueueIteratorFirst (line 210) | func TestQueueIteratorFirst(t *testing.T) {
function TestQueueIteratorLast (line 227) | func TestQueueIteratorLast(t *testing.T) {
function TestQueueIteratorNextTo (line 244) | func TestQueueIteratorNextTo(t *testing.T) {
function TestQueueIteratorPrevTo (line 296) | func TestQueueIteratorPrevTo(t *testing.T) {
function TestQueueSerialization (line 350) | func TestQueueSerialization(t *testing.T) {
function TestQueueString (line 387) | func TestQueueString(t *testing.T) {
function benchmarkEnqueue (line 395) | func benchmarkEnqueue(b *testing.B, queue *Queue[int], size int) {
function benchmarkDequeue (line 403) | func benchmarkDequeue(b *testing.B, queue *Queue[int], size int) {
function BenchmarkArrayQueueDequeue100 (line 411) | func BenchmarkArrayQueueDequeue100(b *testing.B) {
function BenchmarkArrayQueueDequeue1000 (line 422) | func BenchmarkArrayQueueDequeue1000(b *testing.B) {
function BenchmarkArrayQueueDequeue10000 (line 433) | func BenchmarkArrayQueueDequeue10000(b *testing.B) {
function BenchmarkArrayQueueDequeue100000 (line 444) | func BenchmarkArrayQueueDequeue100000(b *testing.B) {
function BenchmarkArrayQueueEnqueue100 (line 455) | func BenchmarkArrayQueueEnqueue100(b *testing.B) {
function BenchmarkArrayQueueEnqueue1000 (line 463) | func BenchmarkArrayQueueEnqueue1000(b *testing.B) {
function BenchmarkArrayQueueEnqueue10000 (line 474) | func BenchmarkArrayQueueEnqueue10000(b *testing.B) {
function BenchmarkArrayQueueEnqueue100000 (line 485) | func BenchmarkArrayQueueEnqueue100000(b *testing.B) {
FILE: queues/arrayqueue/iterator.go
type Iterator (line 13) | type Iterator struct
method Iterator (line 19) | func (queue *Queue[T]) Iterator() *Iterator[T] {
method Next (line 27) | func (iterator *Iterator[T]) Next() bool {
method Prev (line 37) | func (iterator *Iterator[T]) Prev() bool {
method Value (line 46) | func (iterator *Iterator[T]) Value() T {
method Index (line 53) | func (iterator *Iterator[T]) Index() int {
method Begin (line 59) | func (iterator *Iterator[T]) Begin() {
method End (line 65) | func (iterator *Iterator[T]) End() {
method First (line 72) | func (iterator *Iterator[T]) First() bool {
method Last (line 80) | func (iterator *Iterator[T]) Last() bool {
method NextTo (line 89) | func (iterator *Iterator[T]) NextTo(f func(index int, value T) bool) bool {
method PrevTo (line 103) | func (iterator *Iterator[T]) PrevTo(f func(index int, value T) bool) bool {
FILE: queues/arrayqueue/serialization.go
method ToJSON (line 16) | func (queue *Queue[T]) ToJSON() ([]byte, error) {
method FromJSON (line 21) | func (queue *Queue[T]) FromJSON(data []byte) error {
method UnmarshalJSON (line 26) | func (queue *Queue[T]) UnmarshalJSON(bytes []byte) error {
method MarshalJSON (line 31) | func (queue *Queue[T]) MarshalJSON() ([]byte, error) {
FILE: queues/circularbuffer/circularbuffer.go
type Queue (line 25) | type Queue struct
function New (line 36) | func New[T comparable](maxSize int) *Queue[T] {
method Enqueue (line 46) | func (queue *Queue[T]) Enqueue(value T) {
method Dequeue (line 64) | func (queue *Queue[T]) Dequeue() (value T, ok bool) {
method Peek (line 82) | func (queue *Queue[T]) Peek() (value T, ok bool) {
method Empty (line 90) | func (queue *Queue[T]) Empty() bool {
method Full (line 95) | func (queue *Queue[T]) Full() bool {
method Size (line 100) | func (queue *Queue[T]) Size() int {
method Clear (line 105) | func (queue *Queue[T]) Clear() {
method Values (line 114) | func (queue *Queue[T]) Values() []T {
method String (line 123) | func (queue *Queue[T]) String() string {
method withinRange (line 134) | func (queue *Queue[T]) withinRange(index int) bool {
method calculateSize (line 138) | func (queue *Queue[T]) calculateSize() int {
FILE: queues/circularbuffer/circularbuffer_test.go
function TestQueueEnqueue (line 15) | func TestQueueEnqueue(t *testing.T) {
function TestQueuePeek (line 38) | func TestQueuePeek(t *testing.T) {
function TestQueueDequeue (line 51) | func TestQueueDequeue(t *testing.T) {
function TestQueueDequeueFull (line 103) | func TestQueueDequeueFull(t *testing.T) {
function TestQueueIteratorOnEmpty (line 154) | func TestQueueIteratorOnEmpty(t *testing.T) {
function TestQueueIteratorNext (line 162) | func TestQueueIteratorNext(t *testing.T) {
function TestQueueIteratorPrev (line 205) | func TestQueueIteratorPrev(t *testing.T) {
function TestQueueIteratorBegin (line 244) | func TestQueueIteratorBegin(t *testing.T) {
function TestQueueIteratorEnd (line 260) | func TestQueueIteratorEnd(t *testing.T) {
function TestQueueIteratorFirst (line 287) | func TestQueueIteratorFirst(t *testing.T) {
function TestQueueIteratorLast (line 304) | func TestQueueIteratorLast(t *testing.T) {
function TestQueueIteratorNextTo (line 321) | func TestQueueIteratorNextTo(t *testing.T) {
function TestQueueIteratorPrevTo (line 373) | func TestQueueIteratorPrevTo(t *testing.T) {
function TestQueueIterator (line 427) | func TestQueueIterator(t *testing.T) {
function TestQueueSerialization (line 485) | func TestQueueSerialization(t *testing.T) {
function TestQueueString (line 522) | func TestQueueString(t *testing.T) {
function benchmarkEnqueue (line 530) | func benchmarkEnqueue(b *testing.B, queue *Queue[int], size int) {
function benchmarkDequeue (line 538) | func benchmarkDequeue(b *testing.B, queue *Queue[int], size int) {
function BenchmarkArrayQueueDequeue100 (line 546) | func BenchmarkArrayQueueDequeue100(b *testing.B) {
function BenchmarkArrayQueueDequeue1000 (line 557) | func BenchmarkArrayQueueDequeue1000(b *testing.B) {
function BenchmarkArrayQueueDequeue10000 (line 568) | func BenchmarkArrayQueueDequeue10000(b *testing.B) {
function BenchmarkArrayQueueDequeue100000 (line 579) | func BenchmarkArrayQueueDequeue100000(b *testing.B) {
function BenchmarkArrayQueueEnqueue100 (line 590) | func BenchmarkArrayQueueEnqueue100(b *testing.B) {
function BenchmarkArrayQueueEnqueue1000 (line 598) | func BenchmarkArrayQueueEnqueue1000(b *testing.B) {
function BenchmarkArrayQueueEnqueue10000 (line 609) | func BenchmarkArrayQueueEnqueue10000(b *testing.B) {
function BenchmarkArrayQueueEnqueue100000 (line 620) | func BenchmarkArrayQueueEnqueue100000(b *testing.B) {
FILE: queues/circularbuffer/iterator.go
type Iterator (line 13) | type Iterator struct
method Iterator (line 19) | func (queue *Queue[T]) Iterator() *Iterator[T] {
method Next (line 27) | func (iterator *Iterator[T]) Next() bool {
method Prev (line 37) | func (iterator *Iterator[T]) Prev() bool {
method Value (line 46) | func (iterator *Iterator[T]) Value() T {
method Index (line 54) | func (iterator *Iterator[T]) Index() int {
method Begin (line 60) | func (iterator *Iterator[T]) Begin() {
method End (line 66) | func (iterator *Iterator[T]) End() {
method First (line 73) | func (iterator *Iterator[T]) First() bool {
method Last (line 81) | func (iterator *Iterator[T]) Last() bool {
method NextTo (line 90) | func (iterator *Iterator[T]) NextTo(f func(index int, value T) bool) bool {
method PrevTo (line 104) | func (iterator *Iterator[T]) PrevTo(f func(index int, value T) bool) bool {
FILE: queues/circularbuffer/serialization.go
method ToJSON (line 18) | func (queue *Queue[T]) ToJSON() ([]byte, error) {
method FromJSON (line 23) | func (queue *Queue[T]) FromJSON(data []byte) error {
method UnmarshalJSON (line 35) | func (queue *Queue[T]) UnmarshalJSON(bytes []byte) error {
method MarshalJSON (line 40) | func (queue *Queue[T]) MarshalJSON() ([]byte, error) {
FILE: queues/linkedlistqueue/iterator.go
type Iterator (line 13) | type Iterator struct
method Iterator (line 19) | func (queue *Queue[T]) Iterator() *Iterator[T] {
method Next (line 27) | func (iterator *Iterator[T]) Next() bool {
method Value (line 36) | func (iterator *Iterator[T]) Value() T {
method Index (line 43) | func (iterator *Iterator[T]) Index() int {
method Begin (line 49) | func (iterator *Iterator[T]) Begin() {
method First (line 56) | func (iterator *Iterator[T]) First() bool {
method NextTo (line 65) | func (iterator *Iterator[T]) NextTo(f func(index int, value T) bool) bool {
FILE: queues/linkedlistqueue/linkedlistqueue.go
type Queue (line 24) | type Queue struct
function New (line 29) | func New[T comparable]() *Queue[T] {
method Enqueue (line 34) | func (queue *Queue[T]) Enqueue(value T) {
method Dequeue (line 40) | func (queue *Queue[T]) Dequeue() (value T, ok bool) {
method Peek (line 50) | func (queue *Queue[T]) Peek() (value T, ok bool) {
method Empty (line 55) | func (queue *Queue[T]) Empty() bool {
method Size (line 60) | func (queue *Queue[T]) Size() int {
method Clear (line 65) | func (queue *Queue[T]) Clear() {
method Values (line 70) | func (queue *Queue[T]) Values() []T {
method String (line 75) | func (queue *Queue[T]) String() string {
method withinRange (line 86) | func (queue *Queue[T]) withinRange(index int) bool {
FILE: queues/linkedlistqueue/linkedlistqueue_test.go
function TestQueueEnqueue (line 15) | func TestQueueEnqueue(t *testing.T) {
function TestQueuePeek (line 38) | func TestQueuePeek(t *testing.T) {
function TestQueueDequeue (line 51) | func TestQueueDequeue(t *testing.T) {
function TestQueueIteratorOnEmpty (line 77) | func TestQueueIteratorOnEmpty(t *testing.T) {
function TestQueueIteratorNext (line 85) | func TestQueueIteratorNext(t *testing.T) {
function TestQueueIteratorBegin (line 128) | func TestQueueIteratorBegin(t *testing.T) {
function TestQueueIteratorFirst (line 144) | func TestQueueIteratorFirst(t *testing.T) {
function TestQueueIteratorNextTo (line 161) | func TestQueueIteratorNextTo(t *testing.T) {
function TestQueueSerialization (line 213) | func TestQueueSerialization(t *testing.T) {
function TestQueueString (line 250) | func TestQueueString(t *testing.T) {
function benchmarkEnqueue (line 258) | func benchmarkEnqueue(b *testing.B, queue *Queue[int], size int) {
function benchmarkDequeue (line 266) | func benchmarkDequeue(b *testing.B, queue *Queue[int], size int) {
function BenchmarkArrayQueueDequeue100 (line 274) | func BenchmarkArrayQueueDequeue100(b *testing.B) {
function BenchmarkArrayQueueDequeue1000 (line 285) | func BenchmarkArrayQueueDequeue1000(b *testing.B) {
function BenchmarkArrayQueueDequeue10000 (line 296) | func BenchmarkArrayQueueDequeue10000(b *testing.B) {
function BenchmarkArrayQueueDequeue100000 (line 307) | func BenchmarkArrayQueueDequeue100000(b *testing.B) {
function BenchmarkArrayQueueEnqueue100 (line 318) | func BenchmarkArrayQueueEnqueue100(b *testing.B) {
function BenchmarkArrayQueueEnqueue1000 (line 326) | func BenchmarkArrayQueueEnqueue1000(b *testing.B) {
function BenchmarkArrayQueueEnqueue10000 (line 337) | func BenchmarkArrayQueueEnqueue10000(b *testing.B) {
function BenchmarkArrayQueueEnqueue100000 (line 348) | func BenchmarkArrayQueueEnqueue100000(b *testing.B) {
FILE: queues/linkedlistqueue/serialization.go
method ToJSON (line 16) | func (queue *Queue[T]) ToJSON() ([]byte, error) {
method FromJSON (line 21) | func (queue *Queue[T]) FromJSON(data []byte) error {
method UnmarshalJSON (line 26) | func (queue *Queue[T]) UnmarshalJSON(bytes []byte) error {
method MarshalJSON (line 31) | func (queue *Queue[T]) MarshalJSON() ([]byte, error) {
FILE: queues/priorityqueue/iterator.go
type Iterator (line 16) | type Iterator struct
method Iterator (line 21) | func (queue *Queue[T]) Iterator() *Iterator[T] {
method Next (line 29) | func (iterator *Iterator[T]) Next() bool {
method Prev (line 36) | func (iterator *Iterator[T]) Prev() bool {
method Value (line 42) | func (iterator *Iterator[T]) Value() T {
method Index (line 48) | func (iterator *Iterator[T]) Index() int {
method Begin (line 54) | func (iterator *Iterator[T]) Begin() {
method End (line 60) | func (iterator *Iterator[T]) End() {
method First (line 67) | func (iterator *Iterator[T]) First() bool {
method Last (line 74) | func (iterator *Iterator[T]) Last() bool {
method NextTo (line 82) | func (iterator *Iterator[T]) NextTo(f func(index int, value T) bool) bool {
method PrevTo (line 90) | func (iterator *Iterator[T]) PrevTo(f func(index int, value T) bool) bool {
FILE: queues/priorityqueue/priorityqueue.go
type Queue (line 32) | type Queue struct
function New (line 37) | func New[T cmp.Ordered]() *Queue[T] {
function NewWith (line 42) | func NewWith[T comparable](comparator utils.Comparator[T]) *Queue[T] {
method Enqueue (line 47) | func (queue *Queue[T]) Enqueue(value T) {
method Dequeue (line 53) | func (queue *Queue[T]) Dequeue() (value T, ok bool) {
method Peek (line 59) | func (queue *Queue[T]) Peek() (value T, ok bool) {
method Empty (line 64) | func (queue *Queue[T]) Empty() bool {
method Size (line 69) | func (queue *Queue[T]) Size() int {
method Clear (line 74) | func (queue *Queue[T]) Clear() {
method Values (line 79) | func (queue *Queue[T]) Values() []T {
method String (line 84) | func (queue *Queue[T]) String() string {
FILE: queues/priorityqueue/priorityqueue_test.go
type Element (line 16) | type Element struct
method String (line 21) | func (element Element) String() string {
function byPriority (line 26) | func byPriority(a, b Element) int {
function TestBinaryQueueEnqueue (line 30) | func TestBinaryQueueEnqueue(t *testing.T) {
function TestBinaryQueueEnqueueBulk (line 77) | func TestBinaryQueueEnqueueBulk(t *testing.T) {
function TestBinaryQueueDequeue (line 108) | func TestBinaryQueueDequeue(t *testing.T) {
function TestBinaryQueueRandom (line 137) | func TestBinaryQueueRandom(t *testing.T) {
function TestBinaryQueueIteratorOnEmpty (line 156) | func TestBinaryQueueIteratorOnEmpty(t *testing.T) {
function TestBinaryQueueIteratorNext (line 164) | func TestBinaryQueueIteratorNext(t *testing.T) {
function TestBinaryQueueIteratorPrev (line 201) | func TestBinaryQueueIteratorPrev(t *testing.T) {
function TestBinaryQueueIteratorBegin (line 240) | func TestBinaryQueueIteratorBegin(t *testing.T) {
function TestBinaryQueueIteratorEnd (line 256) | func TestBinaryQueueIteratorEnd(t *testing.T) {
function TestBinaryQueueIteratorFirst (line 283) | func TestBinaryQueueIteratorFirst(t *testing.T) {
function TestBinaryQueueIteratorLast (line 300) | func TestBinaryQueueIteratorLast(t *testing.T) {
function TestBinaryQueueIteratorNextTo (line 317) | func TestBinaryQueueIteratorNextTo(t *testing.T) {
function TestBinaryQueueIteratorPrevTo (line 369) | func TestBinaryQueueIteratorPrevTo(t *testing.T) {
function TestBinaryQueueSerialization (line 423) | func TestBinaryQueueSerialization(t *testing.T) {
function TestBTreeString (line 466) | func TestBTreeString(t *testing.T) {
function benchmarkEnqueue (line 474) | func benchmarkEnqueue(b *testing.B, queue *Queue[Element], size int) {
function benchmarkDequeue (line 482) | func benchmarkDequeue(b *testing.B, queue *Queue[Element], size int) {
function BenchmarkBinaryQueueDequeue100 (line 490) | func BenchmarkBinaryQueueDequeue100(b *testing.B) {
function BenchmarkBinaryQueueDequeue1000 (line 501) | func BenchmarkBinaryQueueDequeue1000(b *testing.B) {
function BenchmarkBinaryQueueDequeue10000 (line 512) | func BenchmarkBinaryQueueDequeue10000(b *testing.B) {
function BenchmarkBinaryQueueDequeue100000 (line 523) | func BenchmarkBinaryQueueDequeue100000(b *testing.B) {
function BenchmarkBinaryQueueEnqueue100 (line 534) | func BenchmarkBinaryQueueEnqueue100(b *testing.B) {
function BenchmarkBinaryQueueEnqueue1000 (line 542) | func BenchmarkBinaryQueueEnqueue1000(b *testing.B) {
function BenchmarkBinaryQueueEnqueue10000 (line 553) | func BenchmarkBinaryQueueEnqueue10000(b *testing.B) {
function BenchmarkBinaryQueueEnqueue100000 (line 564) | func BenchmarkBinaryQueueEnqueue100000(b *testing.B) {
FILE: queues/priorityqueue/serialization.go
method ToJSON (line 16) | func (queue *Queue[T]) ToJSON() ([]byte, error) {
method FromJSON (line 21) | func (queue *Queue[T]) FromJSON(data []byte) error {
method UnmarshalJSON (line 26) | func (queue *Queue[T]) UnmarshalJSON(bytes []byte) error {
method MarshalJSON (line 31) | func (queue *Queue[T]) MarshalJSON() ([]byte, error) {
FILE: queues/queues.go
type Queue (line 16) | type Queue interface
FILE: sets/hashset/hashset.go
type Set (line 23) | type Set struct
function New (line 30) | func New[T comparable](values ...T) *Set[T] {
method Add (line 39) | func (set *Set[T]) Add(items ...T) {
method Remove (line 46) | func (set *Set[T]) Remove(items ...T) {
method Contains (line 55) | func (set *Set[T]) Contains(items ...T) bool {
method Empty (line 65) | func (set *Set[T]) Empty() bool {
method Size (line 70) | func (set *Set[T]) Size() int {
method Clear (line 75) | func (set *Set[T]) Clear() {
method Values (line 80) | func (set *Set[T]) Values() []T {
method String (line 91) | func (set *Set[T]) String() string {
method Intersection (line 104) | func (set *Set[T]) Intersection(another *Set[T]) *Set[T] {
method Union (line 128) | func (set *Set[T]) Union(another *Set[T]) *Set[T] {
method Difference (line 144) | func (set *Set[T]) Difference(another *Set[T]) *Set[T] {
FILE: sets/hashset/hashset_test.go
function TestSetNew (line 13) | func TestSetNew(t *testing.T) {
function TestSetAdd (line 30) | func TestSetAdd(t *testing.T) {
function TestSetContains (line 45) | func TestSetContains(t *testing.T) {
function TestSetRemove (line 64) | func TestSetRemove(t *testing.T) {
function TestSetSerialization (line 84) | func TestSetSerialization(t *testing.T) {
function TestSetString (line 121) | func TestSetString(t *testing.T) {
function TestSetIntersection (line 129) | func TestSetIntersection(t *testing.T) {
function TestSetUnion (line 151) | func TestSetUnion(t *testing.T) {
function TestSetDifference (line 173) | func TestSetDifference(t *testing.T) {
function benchmarkContains (line 195) | func benchmarkContains(b *testing.B, set *Set[int], size int) {
function benchmarkAdd (line 203) | func benchmarkAdd(b *testing.B, set *Set[int], size int) {
function benchmarkRemove (line 211) | func benchmarkRemove(b *testing.B, set *Set[int], size int) {
function BenchmarkHashSetContains100 (line 219) | func BenchmarkHashSetContains100(b *testing.B) {
function BenchmarkHashSetContains1000 (line 230) | func BenchmarkHashSetContains1000(b *testing.B) {
function BenchmarkHashSetContains10000 (line 241) | func BenchmarkHashSetContains10000(b *testing.B) {
function BenchmarkHashSetContains100000 (line 252) | func BenchmarkHashSetContains100000(b *testing.B) {
function BenchmarkHashSetAdd100 (line 263) | func BenchmarkHashSetAdd100(b *testing.B) {
function BenchmarkHashSetAdd1000 (line 271) | func BenchmarkHashSetAdd1000(b *testing.B) {
function BenchmarkHashSetAdd10000 (line 282) | func BenchmarkHashSetAdd10000(b *testing.B) {
function BenchmarkHashSetAdd100000 (line 293) | func BenchmarkHashSetAdd100000(b *testing.B) {
function BenchmarkHashSetRemove100 (line 304) | func BenchmarkHashSetRemove100(b *testing.B) {
function BenchmarkHashSetRemove1000 (line 315) | func BenchmarkHashSetRemove1000(b *testing.B) {
function BenchmarkHashSetRemove10000 (line 326) | func BenchmarkHashSetRemove10000(b *testing.B) {
function BenchmarkHashSetRemove100000 (line 337) | func BenchmarkHashSetRemove100000(b *testing.B) {
FILE: sets/hashset/serialization.go
method ToJSON (line 18) | func (set *Set[T]) ToJSON() ([]byte, error) {
method FromJSON (line 23) | func (set *Set[T]) FromJSON(data []byte) error {
method UnmarshalJSON (line 34) | func (set *Set[T]) UnmarshalJSON(bytes []byte) error {
method MarshalJSON (line 39) | func (set *Set[T]) MarshalJSON() ([]byte, error) {
FILE: sets/linkedhashset/enumerable.go
method Each (line 13) | func (set *Set[T]) Each(f func(index int, value T)) {
method Map (line 22) | func (set *Set[T]) Map(f func(index int, value T) T) *Set[T] {
method Select (line 32) | func (set *Set[T]) Select(f func(index int, value T) bool) *Set[T] {
method Any (line 45) | func (set *Set[T]) Any(f func(index int, value T) bool) bool {
method All (line 57) | func (set *Set[T]) All(f func(index int, value T) bool) bool {
method Find (line 70) | func (set *Set[T]) Find(f func(index int, value T) bool) (int, T) {
FILE: sets/linkedhashset/iterator.go
type Iterator (line 16) | type Iterator struct
method Iterator (line 21) | func (set *Set[T]) Iterator() Iterator[T] {
method Next (line 29) | func (iterator *Iterator[T]) Next() bool {
method Prev (line 36) | func (iterator *Iterator[T]) Prev() bool {
method Value (line 42) | func (iterator *Iterator[T]) Value() T {
method Index (line 48) | func (iterator *Iterator[T]) Index() int {
method Begin (line 54) | func (iterator *Iterator[T]) Begin() {
method End (line 60) | func (iterator *Iterator[T]) End() {
method First (line 67) | func (iterator *Iterator[T]) First() bool {
method Last (line 74) | func (iterator *Iterator[T]) Last() bool {
method NextTo (line 82) | func (iterator *Iterator[T]) NextTo(f func(index int, value T) bool) bool {
method PrevTo (line 96) | func (iterator *Iterator[T]) PrevTo(f func(index int, value T) bool) bool {
FILE: sets/linkedhashset/linkedhashset.go
type Set (line 28) | type Set struct
function New (line 36) | func New[T comparable](values ...T) *Set[T] {
method Add (line 49) | func (set *Set[T]) Add(items ...T) {
method Remove (line 60) | func (set *Set[T]) Remove(items ...T) {
method Contains (line 73) | func (set *Set[T]) Contains(items ...T) bool {
method Empty (line 83) | func (set *Set[T]) Empty() bool {
method Size (line 88) | func (set *Set[T]) Size() int {
method Clear (line 93) | func (set *Set[T]) Clear() {
method Values (line 99) | func (set *Set[T]) Values() []T {
method String (line 109) | func (set *Set[T]) String() string {
method Intersection (line 123) | func (set *Set[T]) Intersection(another *Set[T]) *Set[T] {
method Union (line 147) | func (set *Set[T]) Union(another *Set[T]) *Set[T] {
method Difference (line 163) | func (set *Set[T]) Difference(another *Set[T]) *Set[T] {
FILE: sets/linkedhashset/linkedhashset_test.go
function TestSetNew (line 14) | func TestSetNew(t *testing.T) {
function TestSetAdd (line 31) | func TestSetAdd(t *testing.T) {
function TestSetContains (line 46) | func TestSetContains(t *testing.T) {
function TestSetRemove (line 65) | func TestSetRemove(t *testing.T) {
function TestSetEach (line 85) | func TestSetEach(t *testing.T) {
function TestSetMap (line 108) | func TestSetMap(t *testing.T) {
function TestSetSelect (line 125) | func TestSetSelect(t *testing.T) {
function TestSetAny (line 143) | func TestSetAny(t *testing.T) {
function TestSetAll (line 160) | func TestSetAll(t *testing.T) {
function TestSetFind (line 177) | func TestSetFind(t *testing.T) {
function TestSetChaining (line 194) | func TestSetChaining(t *testing.T) {
function TestSetIteratorPrevOnEmpty (line 199) | func TestSetIteratorPrevOnEmpty(t *testing.T) {
function TestSetIteratorNext (line 207) | func TestSetIteratorNext(t *testing.T) {
function TestSetIteratorPrev (line 241) | func TestSetIteratorPrev(t *testing.T) {
function TestSetIteratorBegin (line 277) | func TestSetIteratorBegin(t *testing.T) {
function TestSetIteratorEnd (line 291) | func TestSetIteratorEnd(t *testing.T) {
function TestSetIteratorFirst (line 316) | func TestSetIteratorFirst(t *testing.T) {
function TestSetIteratorLast (line 328) | func TestSetIteratorLast(t *testing.T) {
function TestSetIteratorNextTo (line 340) | func TestSetIteratorNextTo(t *testing.T) {
function TestSetIteratorPrevTo (line 389) | func TestSetIteratorPrevTo(t *testing.T) {
function TestSetSerialization (line 440) | func TestSetSerialization(t *testing.T) {
function TestSetString (line 477) | func TestSetString(t *testing.T) {
function TestSetIntersection (line 485) | func TestSetIntersection(t *testing.T) {
function TestSetUnion (line 507) | func TestSetUnion(t *testing.T) {
function TestSetDifference (line 529) | func TestSetDifference(t *testing.T) {
function benchmarkContains (line 551) | func benchmarkContains(b *testing.B, set *Set[int], size int) {
function benchmarkAdd (line 559) | func benchmarkAdd(b *testing.B, set *Set[int], size int) {
function benchmarkRemove (line 567) | func benchmarkRemove(b *testing.B, set *Set[int], size int) {
function BenchmarkHashSetContains100 (line 575) | func BenchmarkHashSetContains100(b *testing.B) {
function BenchmarkHashSetContains1000 (line 586) | func BenchmarkHashSetContains1000(b *testing.B) {
function BenchmarkHashSetContains10000 (line 597) | func BenchmarkHashSetContains10000(b *testing.B) {
function BenchmarkHashSetContains100000 (line 608) | func BenchmarkHashSetContains100000(b *testing.B) {
function BenchmarkHashSetAdd100 (line 619) | func BenchmarkHashSetAdd100(b *testing.B) {
function BenchmarkHashSetAdd1000 (line 627) | func BenchmarkHashSetAdd1000(b *testing.B) {
function BenchmarkHashSetAdd10000 (line 638) | func BenchmarkHashSetAdd10000(b *testing.B) {
function BenchmarkHashSetAdd100000 (line 649) | func BenchmarkHashSetAdd100000(b *testing.B) {
function BenchmarkHashSetRemove100 (line 660) | func BenchmarkHashSetRemove100(b *testing.B) {
function BenchmarkHashSetRemove1000 (line 671) | func BenchmarkHashSetRemove1000(b *testing.B) {
function BenchmarkHashSetRemove10000 (line 682) | func BenchmarkHashSetRemove10000(b *testing.B) {
function BenchmarkHashSetRemove100000 (line 693) | func BenchmarkHashSetRemove100000(b *testing.B) {
FILE: sets/linkedhashset/serialization.go
method ToJSON (line 18) | func (set *Set[T]) ToJSON() ([]byte, error) {
method FromJSON (line 23) | func (set *Set[T]) FromJSON(data []byte) error {
method UnmarshalJSON (line 34) | func (set *Set[T]) UnmarshalJSON(bytes []byte) error {
method MarshalJSON (line 39) | func (set *Set[T]) MarshalJSON() ([]byte, error) {
FILE: sets/sets.go
type Set (line 17) | type Set interface
FILE: sets/treeset/enumerable.go
method Each (line 16) | func (set *Set[T]) Each(f func(index int, value T)) {
method Map (line 25) | func (set *Set[T]) Map(f func(index int, value T) T) *Set[T] {
method Select (line 35) | func (set *Set[T]) Select(f func(index int, value T) bool) *Set[T] {
method Any (line 48) | func (set *Set[T]) Any(f func(index int, value T) bool) bool {
method All (line 60) | func (set *Set[T]) All(f func(index int, value T) bool) bool {
method Find (line 73) | func (set *Set[T]) Find(f func(index int, value T) bool) (int, T) {
FILE: sets/treeset/iterator.go
type Iterator (line 16) | type Iterator struct
method Iterator (line 23) | func (set *Set[T]) Iterator() Iterator[T] {
method Next (line 31) | func (iterator *Iterator[T]) Next() bool {
method Prev (line 41) | func (iterator *Iterator[T]) Prev() bool {
method Value (line 50) | func (iterator *Iterator[T]) Value() T {
method Index (line 56) | func (iterator *Iterator[T]) Index() int {
method Begin (line 62) | func (iterator *Iterator[T]) Begin() {
method End (line 69) | func (iterator *Iterator[T]) End() {
method First (line 77) | func (iterator *Iterator[T]) First() bool {
method Last (line 85) | func (iterator *Iterator[T]) Last() bool {
method NextTo (line 94) | func (iterator *Iterator[T]) NextTo(f func(index int, value T) bool) bool {
method PrevTo (line 108) | func (iterator *Iterator[T]) PrevTo(f func(index int, value T) bool) bool {
FILE: sets/treeset/serialization.go
method ToJSON (line 18) | func (set *Set[T]) ToJSON() ([]byte, error) {
method FromJSON (line 23) | func (set *Set[T]) FromJSON(data []byte) error {
method UnmarshalJSON (line 34) | func (set *Set[T]) UnmarshalJSON(bytes []byte) error {
method MarshalJSON (line 39) | func (set *Set[T]) MarshalJSON() ([]byte, error) {
FILE: sets/treeset/treeset.go
type Set (line 27) | type Set struct
function New (line 33) | func New[T cmp.Ordered](values ...T) *Set[T] {
function NewWith (line 38) | func NewWith[T comparable](comparator utils.Comparator[T], values ...T) ...
method Add (line 47) | func (set *Set[T]) Add(items ...T) {
method Remove (line 54) | func (set *Set[T]) Remove(items ...T) {
method Contains (line 63) | func (set *Set[T]) Contains(items ...T) bool {
method Empty (line 73) | func (set *Set[T]) Empty() bool {
method Size (line 78) | func (set *Set[T]) Size() int {
method Clear (line 83) | func (set *Set[T]) Clear() {
method Values (line 88) | func (set *Set[T]) Values() []T {
method String (line 93) | func (set *Set[T]) String() string {
method Intersection (line 107) | func (set *Set[T]) Intersection(another *Set[T]) *Set[T] {
method Union (line 138) | func (set *Set[T]) Union(another *Set[T]) *Set[T] {
method Difference (line 161) | func (set *Set[T]) Difference(another *Set[T]) *Set[T] {
FILE: sets/treeset/treeset_test.go
function TestSetNew (line 16) | func TestSetNew(t *testing.T) {
function TestSetAdd (line 30) | func TestSetAdd(t *testing.T) {
function TestSetContains (line 46) | func TestSetContains(t *testing.T) {
function TestSetRemove (line 63) | func TestSetRemove(t *testing.T) {
function TestSetEach (line 83) | func TestSetEach(t *testing.T) {
function TestSetMap (line 106) | func TestSetMap(t *testing.T) {
function TestSetSelect (line 123) | func TestSetSelect(t *testing.T) {
function TestSetAny (line 141) | func TestSetAny(t *testing.T) {
function TestSetAll (line 158) | func TestSetAll(t *testing.T) {
function TestSetFind (line 175) | func TestSetFind(t *testing.T) {
function TestSetChaining (line 192) | func TestSetChaining(t *testing.T) {
function TestSetIteratorNextOnEmpty (line 197) | func TestSetIteratorNextOnEmpty(t *testing.T) {
function TestSetIteratorPrevOnEmpty (line 205) | func TestSetIteratorPrevOnEmpty(t *testing.T) {
function TestSetIteratorNext (line 213) | func TestSetIteratorNext(t *testing.T) {
function TestSetIteratorPrev (line 247) | func TestSetIteratorPrev(t *testing.T) {
function TestSetIteratorBegin (line 283) | func TestSetIteratorBegin(t *testing.T) {
function TestSetIteratorEnd (line 297) | func TestSetIteratorEnd(t *testing.T) {
function TestSetIteratorFirst (line 322) | func TestSetIteratorFirst(t *testing.T) {
function TestSetIteratorLast (line 334) | func TestSetIteratorLast(t *testing.T) {
function TestSetIteratorNextTo (line 346) | func TestSetIteratorNextTo(t *testing.T) {
function TestSetIteratorPrevTo (line 395) | func TestSetIteratorPrevTo(t *testing.T) {
function TestSetSerialization (line 446) | func TestSetSerialization(t *testing.T) {
function TestSetString (line 482) | func TestSetString(t *testing.T) {
function TestSetIntersection (line 490) | func TestSetIntersection(t *testing.T) {
function TestSetUnion (line 512) | func TestSetUnion(t *testing.T) {
function TestSetDifference (line 534) | func TestSetDifference(t *testing.T) {
function benchmarkContains (line 556) | func benchmarkContains(b *testing.B, set *Set[int], size int) {
function benchmarkAdd (line 564) | func benchmarkAdd(b *testing.B, set *Set[int], size int) {
function benchmarkRemove (line 572) | func benchmarkRemove(b *testing.B, set *Set[int], size int) {
function BenchmarkTreeSetContains100 (line 580) | func BenchmarkTreeSetContains100(b *testing.B) {
function BenchmarkTreeSetContains1000 (line 591) | func BenchmarkTreeSetContains1000(b *testing.B) {
function BenchmarkTreeSetContains10000 (line 602) | func BenchmarkTreeSetContains10000(b *testing.B) {
function BenchmarkTreeSetContains100000 (line 613) | func BenchmarkTreeSetContains100000(b *testing.B) {
function BenchmarkTreeSetAdd100 (line 624) | func BenchmarkTreeSetAdd100(b *testing.B) {
function BenchmarkTreeSetAdd1000 (line 632) | func BenchmarkTreeSetAdd1000(b *testing.B) {
function BenchmarkTreeSetAdd10000 (line 643) | func BenchmarkTreeSetAdd10000(b *testing.B) {
function BenchmarkTreeSetAdd100000 (line 654) | func BenchmarkTreeSetAdd100000(b *testing.B) {
function BenchmarkTreeSetRemove100 (line 665) | func BenchmarkTreeSetRemove100(b *testing.B) {
function BenchmarkTreeSetRemove1000 (line 676) | func BenchmarkTreeSetRemove1000(b *testing.B) {
function BenchmarkTreeSetRemove10000 (line 687) | func BenchmarkTreeSetRemove10000(b *testing.B) {
function BenchmarkTreeSetRemove100000 (line 698) | func BenchmarkTreeSetRemove100000(b *testing.B) {
FILE: stacks/arraystack/arraystack.go
type Stack (line 24) | type Stack struct
function New (line 29) | func New[T comparable]() *Stack[T] {
method Push (line 34) | func (stack *Stack[T]) Push(value T) {
method Pop (line 40) | func (stack *Stack[T]) Pop() (value T, ok bool) {
method Peek (line 48) | func (stack *Stack[T]) Peek() (value T, ok bool) {
method Empty (line 53) | func (stack *Stack[T]) Empty() bool {
method Size (line 58) | func (stack *Stack[T]) Size() int {
method Clear (line 63) | func (stack *Stack[T]) Clear() {
method Values (line 68) | func (stack *Stack[T]) Values() []T {
method String (line 78) | func (stack *Stack[T]) String() string {
method withinRange (line 89) | func (stack *Stack[T]) withinRange(index int) bool {
FILE: stacks/arraystack/arraystack_test.go
function TestStackPush (line 15) | func TestStackPush(t *testing.T) {
function TestStackPeek (line 38) | func TestStackPeek(t *testing.T) {
function TestStackPop (line 51) | func TestStackPop(t *testing.T) {
function TestStackIteratorOnEmpty (line 77) | func TestStackIteratorOnEmpty(t *testing.T) {
function TestStackIteratorNext (line 85) | func TestStackIteratorNext(t *testing.T) {
function TestStackIteratorPrev (line 128) | func TestStackIteratorPrev(t *testing.T) {
function TestStackIteratorBegin (line 167) | func TestStackIteratorBegin(t *testing.T) {
function TestStackIteratorEnd (line 183) | func TestStackIteratorEnd(t *testing.T) {
function TestStackIteratorFirst (line 210) | func TestStackIteratorFirst(t *testing.T) {
function TestStackIteratorLast (line 227) | func TestStackIteratorLast(t *testing.T) {
function TestStackIteratorNextTo (line 244) | func TestStackIteratorNextTo(t *testing.T) {
function TestStackIteratorPrevTo (line 296) | func TestStackIteratorPrevTo(t *testing.T) {
function TestStackSerialization (line 350) | func TestStackSerialization(t *testing.T) {
function TestStackString (line 387) | func TestStackString(t *testing.T) {
function benchmarkPush (line 395) | func benchmarkPush(b *testing.B, stack *Stack[int], size int) {
function benchmarkPop (line 403) | func benchmarkPop(b *testing.B, stack *Stack[int], size int) {
function BenchmarkArrayStackPop100 (line 411) | func BenchmarkArrayStackPop100(b *testing.B) {
function BenchmarkArrayStackPop1000 (line 422) | func BenchmarkArrayStackPop1000(b *testing.B) {
function BenchmarkArrayStackPop10000 (line 433) | func BenchmarkArrayStackPop10000(b *testing.B) {
function BenchmarkArrayStackPop100000 (line 444) | func BenchmarkArrayStackPop100000(b *testing.B) {
function BenchmarkArrayStackPush100 (line 455) | func BenchmarkArrayStackPush100(b *testing.B) {
function BenchmarkArrayStackPush1000 (line 463) | func BenchmarkArrayStackPush1000(b *testing.B) {
function BenchmarkArrayStackPush10000 (line 474) | func BenchmarkArrayStackPush10000(b *testing.B) {
function BenchmarkArrayStackPush100000 (line 485) | func BenchmarkArrayStackPush100000(b *testing.B) {
FILE: stacks/arraystack/iterator.go
type Iterator (line 13) | type Iterator struct
method Iterator (line 19) | func (stack *Stack[T]) Iterator() *Iterator[T] {
method Next (line 27) | func (iterator *Iterator[T]) Next() bool {
method Prev (line 37) | func (iterator *Iterator[T]) Prev() bool {
method Value (line 46) | func (iterator *Iterator[T]) Value() T {
method Index (line 53) | func (iterator *Iterator[T]) Index() int {
method Begin (line 59) | func (iterator *Iterator[T]) Begin() {
method End (line 65) | func (iterator *Iterator[T]) End() {
method First (line 72) | func (iterator *Iterator[T]) First() bool {
method Last (line 80) | func (iterator *Iterator[T]) Last() bool {
method NextTo (line 89) | func (iterator *Iterator[T]) NextTo(f func(index int, value T) bool) bool {
method PrevTo (line 103) | func (iterator *Iterator[T]) PrevTo(f func(index int, value T) bool) bool {
FILE: stacks/arraystack/serialization.go
method ToJSON (line 16) | func (stack *Stack[T]) ToJSON() ([]byte, error) {
method FromJSON (line 21) | func (stack *Stack[T]) FromJSON(data []byte) error {
method UnmarshalJSON (line 26) | func (stack *Stack[T]) UnmarshalJSON(bytes []byte) error {
method MarshalJSON (line 31) | func (stack *Stack[T]) MarshalJSON() ([]byte, error) {
FILE: stacks/linkedliststack/iterator.go
type Iterator (line 13) | type Iterator struct
method Iterator (line 19) | func (stack *Stack[T]) Iterator() *Iterator[T] {
method Next (line 27) | func (iterator *Iterator[T]) Next() bool {
method Value (line 36) | func (iterator *Iterator[T]) Value() T {
method Index (line 43) | func (iterator *Iterator[T]) Index() int {
method Begin (line 49) | func (iterator *Iterator[T]) Begin() {
method First (line 56) | func (iterator *Iterator[T]) First() bool {
method NextTo (line 65) | func (iterator *Iterator[T]) NextTo(f func(index int, value T) bool) bool {
FILE: stacks/linkedliststack/linkedliststack.go
type Stack (line 24) | type Stack struct
function New (line 29) | func New[T comparable]() *Stack[T] {
method Push (line 34) | func (stack *Stack[T]) Push(value T) {
method Pop (line 40) | func (stack *Stack[T]) Pop() (value T, ok bool) {
method Peek (line 48) | func (stack *Stack[T]) Peek() (value T, ok bool) {
method Empty (line 53) | func (stack *Stack[T]) Empty() bool {
method Size (line 58) | func (stack *Stack[T]) Size() int {
method Clear (line 63) | func (stack *Stack[T]) Clear() {
method Values (line 68) | func (stack *Stack[T]) Values() []T {
method String (line 73) | func (stack *Stack[T]) String() string {
method withinRange (line 84) | func (stack *Stack[T]) withinRange(index int) bool {
FILE: stacks/linkedliststack/linkedliststack_test.go
function TestStackPush (line 15) | func TestStackPush(t *testing.T) {
function TestStackPeek (line 38) | func TestStackPeek(t *testing.T) {
function TestStackPop (line 51) | func TestStackPop(t *testing.T) {
function TestStackIterator (line 77) | func TestStackIterator(t *testing.T) {
function TestStackIteratorBegin (line 121) | func TestStackIteratorBegin(t *testing.T) {
function TestStackIteratorFirst (line 137) | func TestStackIteratorFirst(t *testing.T) {
function TestStackIteratorNextTo (line 154) | func TestStackIteratorNextTo(t *testing.T) {
function TestStackSerialization (line 206) | func TestStackSerialization(t *testing.T) {
function TestStackString (line 243) | func TestStackString(t *testing.T) {
function benchmarkPush (line 251) | func benchmarkPush(b *testing.B, stack *Stack[int], size int) {
function benchmarkPop (line 259) | func benchmarkPop(b *testing.B, stack *Stack[int], size int) {
function BenchmarkLinkedListStackPop100 (line 267) | func BenchmarkLinkedListStackPop100(b *testing.B) {
function BenchmarkLinkedListStackPop1000 (line 278) | func BenchmarkLinkedListStackPop1000(b *testing.B) {
function BenchmarkLinkedListStackPop10000 (line 289) | func BenchmarkLinkedListStackPop10000(b *testing.B) {
function BenchmarkLinkedListStackPop100000 (line 300) | func BenchmarkLinkedListStackPop100000(b *testing.B) {
function BenchmarkLinkedListStackPush100 (line 311) | func BenchmarkLinkedListStackPush100(b *testing.B) {
function BenchmarkLinkedListStackPush1000 (line 319) | func BenchmarkLinkedListStackPush1000(b *testing.B) {
function BenchmarkLinkedListStackPush10000 (line 330) | func BenchmarkLinkedListStackPush10000(b *testing.B) {
function BenchmarkLinkedListStackPush100000 (line 341) | func BenchmarkLinkedListStackPush100000(b *testing.B) {
FILE: stacks/linkedliststack/serialization.go
method ToJSON (line 16) | func (stack *Stack[T]) ToJSON() ([]byte, error) {
method FromJSON (line 21) | func (stack *Stack[T]) FromJSON(data []byte) error {
method UnmarshalJSON (line 26) | func (stack *Stack[T]) UnmarshalJSON(bytes []byte) error {
method MarshalJSON (line 31) | func (stack *Stack[T]) MarshalJSON() ([]byte, error) {
FILE: stacks/stacks.go
type Stack (line 15) | type Stack interface
FILE: testutils/testutils.go
function SameElements (line 5) | func SameElements[T comparable](t *testing.T, actual, expected []T) {
FILE: trees/avltree/avltree.go
type Tree (line 24) | type Tree struct
type Node (line 31) | type Node struct
function New (line 40) | func New[K cmp.Ordered, V any]() *Tree[K, V] {
function NewWith (line 45) | func NewWith[K comparable, V any](comparator utils.Comparator[K]) *Tree[...
method Put (line 51) | func (tree *Tree[K, V]) Put(key K, value V) {
method Get (line 58) | func (tree *Tree[K, V]) Get(key K) (value V, found bool) {
method GetNode (line 68) | func (tree *Tree[K, V]) GetNode(key K) *Node[K, V] {
method Remove (line 86) | func (tree *Tree[K, V]) Remove(key K) {
method Empty (line 91) | func (tree *Tree[K, V]) Empty() bool {
method Size (line 96) | func (tree *Tree[K, V]) Size() int {
method Size (line 102) | func (n *Node[K, V]) Size() int {
method Keys (line 117) | func (tree *Tree[K, V]) Keys() []K {
method Values (line 127) | func (tree *Tree[K, V]) Values() []V {
method Left (line 138) | func (tree *Tree[K, V]) Left() *Node[K, V] {
method Right (line 144) | func (tree *Tree[K, V]) Right() *Node[K, V] {
method Floor (line 156) | func (tree *Tree[K, V]) Floor(key K) (floor *Node[K, V], found bool) {
method Ceiling (line 185) | func (tree *Tree[K, V]) Ceiling(key K) (floor *Node[K, V], found bool) {
method Clear (line 207) | func (tree *Tree[K, V]) Clear() {
method String (line 213) | func (tree *Tree[K, V]) String() string {
method String (line 221) | func (n *Node[K, V]) String() string {
method put (line 225) | func (tree *Tree[K, V]) put(key K, value V, p *Node[K, V], qp **Node[K, ...
method remove (line 254) | func (tree *Tree[K, V]) remove(key K, qp **Node[K, V]) bool {
function removeMin (line 290) | func removeMin[K comparable, V any](qp **Node[K, V], minKey *K, minVal *...
function putFix (line 308) | func putFix[K comparable, V any](c int8, t **Node[K, V]) bool {
function removeFix (line 329) | func removeFix[K comparable, V any](c int8, t **Node[K, V]) bool {
function singlerot (line 358) | func singlerot[K comparable, V any](c int8, s *Node[K, V]) *Node[K, V] {
function doublerot (line 365) | func doublerot[K comparable, V any](c int8, s *Node[K, V]) *Node[K, V] {
function rotate (line 387) | func rotate[K comparable, V any](c int8, s *Node[K, V]) *Node[K, V] {
method bottom (line 400) | func (tree *Tree[K, V]) bottom(d int) *Node[K, V] {
method Prev (line 414) | func (n *Node[K, V]) Prev() *Node[K, V] {
method Next (line 420) | func (n *Node[K, V]) Next() *Node[K, V] {
method walk1 (line 424) | func (n *Node[K, V]) walk1(a int) *Node[K, V] {
function output (line 445) | func output[K comparable, V any](node *Node[K, V], prefix string, isTail...
FILE: trees/avltree/avltree_test.go
function TestAVLTreeGet (line 13) | func TestAVLTreeGet(t *testing.T) {
function TestAVLTreePut (line 57) | func TestAVLTreePut(t *testing.T) {
function TestAVLTreeRemove (line 98) | func TestAVLTreeRemove(t *testing.T) {
function TestAVLTreeLeftAndRight (line 162) | func TestAVLTreeLeftAndRight(t *testing.T) {
function TestAVLTreeCeilingAndFloor (line 196) | func TestAVLTreeCeilingAndFloor(t *testing.T) {
function TestAVLTreeIteratorNextOnEmpty (line 229) | func TestAVLTreeIteratorNextOnEmpty(t *testing.T) {
function TestAVLTreeIteratorPrevOnEmpty (line 237) | func TestAVLTreeIteratorPrevOnEmpty(t *testing.T) {
function TestAVLTreeIterator1Next (line 245) | func TestAVLTreeIterator1Next(t *testing.T) {
function TestAVLTreeIterator1Prev (line 277) | func TestAVLTreeIterator1Prev(t *testing.T) {
function TestAVLTreeIterator2Next (line 310) | func TestAVLTreeIterator2Next(t *testing.T) {
function TestAVLTreeIterator2Prev (line 329) | func TestAVLTreeIterator2Prev(t *testing.T) {
function TestAVLTreeIterator3Next (line 350) | func TestAVLTreeIterator3Next(t *testing.T) {
function TestAVLTreeIterator3Prev (line 367) | func TestAVLTreeIterator3Prev(t *testing.T) {
function TestAVLTreeIterator4Next (line 386) | func TestAVLTreeIterator4Next(t *testing.T) {
function TestAVLTreeIterator4Prev (line 422) | func TestAVLTreeIterator4Prev(t *testing.T) {
function TestAVLTreeIteratorBegin (line 460) | func TestAVLTreeIteratorBegin(t *testing.T) {
function TestAVLTreeIteratorEnd (line 492) | func TestAVLTreeIteratorEnd(t *testing.T) {
function TestAVLTreeIteratorFirst (line 519) | func TestAVLTreeIteratorFirst(t *testing.T) {
function TestAVLTreeIteratorLast (line 533) | func TestAVLTreeIteratorLast(t *testing.T) {
function TestAVLTreeIteratorNextTo (line 547) | func TestAVLTreeIteratorNextTo(t *testing.T) {
function TestAVLTreeIteratorPrevTo (line 599) | func TestAVLTreeIteratorPrevTo(t *testing.T) {
function TestAVLTreeSerialization (line 653) | func TestAVLTreeSerialization(t *testing.T) {
function TestAVLTreeString (line 704) | func TestAVLTreeString(t *testing.T) {
function benchmarkGet (line 720) | func benchmarkGet(b *testing.B, tree *Tree[int, struct{}], size int) {
function benchmarkPut (line 728) | func benchmarkPut(b *testing.B, tree *Tree[int, struct{}], size int) {
function benchmarkRemove (line 736) | func benchmarkRemove(b *testing.B, tree *Tree[int, struct{}], size int) {
function BenchmarkAVLTreeGet100 (line 744) | func BenchmarkAVLTreeGet100(b *testing.B) {
function BenchmarkAVLTreeGet1000 (line 755) | func BenchmarkAVLTreeGet1000(b *testing.B) {
function BenchmarkAVLTreeGet10000 (line 766) | func BenchmarkAVLTreeGet10000(b *testing.B) {
function BenchmarkAVLTreeGet100000 (line 777) | func BenchmarkAVLTreeGet100000(b *testing.B) {
function BenchmarkAVLTreePut100 (line 788) | func BenchmarkAVLTreePut100(b *testing.B) {
function BenchmarkAVLTreePut1000 (line 796) | func BenchmarkAVLTreePut1000(b *testing.B) {
function BenchmarkAVLTreePut10000 (line 807) | func BenchmarkAVLTreePut10000(b *testing.B) {
function BenchmarkAVLTreePut100000 (line 818) | func BenchmarkAVLTreePut100000(b *testing.B) {
function BenchmarkAVLTreeRemove100 (line 829) | func BenchmarkAVLTreeRemove100(b *testing.B) {
function BenchmarkAVLTreeRemove1000 (line 840) | func BenchmarkAVLTreeRemove1000(b *testing.B) {
function BenchmarkAVLTreeRemove10000 (line 851) | func BenchmarkAVLTreeRemove10000(b *testing.B) {
function BenchmarkAVLTreeRemove100000 (line 862) | func BenchmarkAVLTreeRemove100000(b *testing.B) {
FILE: trees/avltree/iterator.go
type Iterator (line 13) | type Iterator struct
type position (line 19) | type position
constant begin (line 22) | begin, between, end position = 0, 1, 2
method Iterator (line 26) | func (tree *Tree[K, V]) Iterator() *Iterator[K, V] {
method Next (line 34) | func (iterator *Iterator[K, V]) Next() bool {
method Prev (line 54) | func (iterator *Iterator[K, V]) Prev() bool {
method Value (line 72) | func (iterator *Iterator[K, V]) Value() (v V) {
method Key (line 81) | func (iterator *Iterator[K, V]) Key() (k K) {
method Node (line 90) | func (iterator *Iterator[K, V]) Node() *Node[K, V] {
method Begin (line 96) | func (iterator *Iterator[K, V]) Begin() {
method End (line 103) | func (iterator *Iterator[K, V]) End() {
method First (line 111) | func (iterator *Iterator[K, V]) First() bool {
method Last (line 119) | func (iterator *Iterator[K, V]) Last() bool {
method NextTo (line 128) | func (iterator *Iterator[K, V]) NextTo(f func(key K, value V) bool) bool {
method PrevTo (line 142) | func (iterator *Iterator[K, V]) PrevTo(f func(key K, value V) bool) bool {
FILE: trees/avltree/serialization.go
method ToJSON (line 18) | func (tree *Tree[K, V]) ToJSON() ([]byte, error) {
method FromJSON (line 28) | func (tree *Tree[K, V]) FromJSON(data []byte) error {
method UnmarshalJSON (line 44) | func (tree *Tree[K, V]) UnmarshalJSON(bytes []byte) error {
method MarshalJSON (line 49) | func (tree *Tree[K, V]) MarshalJSON() ([]byte, error) {
FILE: trees/binaryheap/binaryheap.go
type Heap (line 28) | type Heap struct
function New (line 34) | func New[T cmp.Ordered]() *Heap[T] {
function NewWith (line 39) | func NewWith[T comparable](comparator utils.Comparator[T]) *Heap[T] {
method Push (line 44) | func (heap *Heap[T]) Push(values ...T) {
method Pop (line 62) | func (heap *Heap[T]) Pop() (value T, ok bool) {
method Peek (line 76) | func (heap *Heap[T]) Peek() (value T, ok bool) {
method Empty (line 81) | func (heap *Heap[T]) Empty() bool {
method Size (line 86) | func (heap *Heap[T]) Size() int {
method Clear (line 91) | func (heap *Heap[T]) Clear() {
method Values (line 96) | func (heap *Heap[T]) Values() []T {
method String (line 105) | func (heap *Heap[T]) String() string {
method bubbleDown (line 117) | func (heap *Heap[T]) bubbleDown() {
method bubbleDownIndex (line 123) | func (heap *Heap[T]) bubbleDownIndex(index int) {
method bubbleUp (line 147) | func (heap *Heap[T]) bubbleUp() {
method withinRange (line 161) | func (heap *Heap[T]) withinRange(index int) bool {
FILE: trees/binaryheap/binaryheap_test.go
function TestBinaryHeapPush (line 15) | func TestBinaryHeapPush(t *testing.T) {
function TestBinaryHeapPushBulk (line 40) | func TestBinaryHeapPushBulk(t *testing.T) {
function TestBinaryHeapPop (line 53) | func TestBinaryHeapPop(t *testing.T) {
function TestBinaryHeapRandom (line 85) | func TestBinaryHeapRandom(t *testing.T) {
function TestBinaryHeapIteratorOnEmpty (line 104) | func TestBinaryHeapIteratorOnEmpty(t *testing.T) {
function TestBinaryHeapIteratorNext (line 112) | func TestBinaryHeapIteratorNext(t *testing.T) {
function TestBinaryHeapIteratorPrev (line 149) | func TestBinaryHeapIteratorPrev(t *testing.T) {
function TestBinaryHeapIteratorBegin (line 188) | func TestBinaryHeapIteratorBegin(t *testing.T) {
function TestBinaryHeapIteratorEnd (line 204) | func TestBinaryHeapIteratorEnd(t *testing.T) {
function TestBinaryHeapIteratorFirst (line 231) | func TestBinaryHeapIteratorFirst(t *testing.T) {
function TestBinaryHeapIteratorLast (line 248) | func TestBinaryHeapIteratorLast(t *testing.T) {
function TestBinaryHeapIteratorNextTo (line 265) | func TestBinaryHeapIteratorNextTo(t *testing.T) {
function TestBinaryHeapIteratorPrevTo (line 317) | func TestBinaryHeapIteratorPrevTo(t *testing.T) {
function TestBinaryHeapSerialization (line 371) | func TestBinaryHeapSerialization(t *testing.T) {
function TestBTreeString (line 417) | func TestBTreeString(t *testing.T) {
function benchmarkPush (line 425) | func benchmarkPush(b *testing.B, heap *Heap[int], size int) {
function benchmarkPop (line 433) | func benchmarkPop(b *testing.B, heap *Heap[int], size int) {
function BenchmarkBinaryHeapPop100 (line 441) | func BenchmarkBinaryHeapPop100(b *testing.B) {
function BenchmarkBinaryHeapPop1000 (line 452) | func BenchmarkBinaryHeapPop1000(b *testing.B) {
function BenchmarkBinaryHeapPop10000 (line 463) | func BenchmarkBinaryHeapPop10000(b *testing.B) {
function BenchmarkBinaryHeapPop100000 (line 474) | func BenchmarkBinaryHeapPop100000(b *testing.B) {
function BenchmarkBinaryHeapPush100 (line 485) | func BenchmarkBinaryHeapPush100(b *testing.B) {
function BenchmarkBinaryHeapPush1000 (line 493) | func BenchmarkBinaryHeapPush1000(b *testing.B) {
function BenchmarkBinaryHeapPush10000 (line 504) | func BenchmarkBinaryHeapPush10000(b *testing.B) {
function BenchmarkBinaryHeapPush100000 (line 515) | func BenchmarkBinaryHeapPush100000(b *testing.B) {
FILE: trees/binaryheap/iterator.go
type Iterator (line 15) | type Iterator struct
method Iterator (line 21) | func (heap *Heap[T]) Iterator() *Iterator[T] {
method Next (line 29) | func (iterator *Iterator[T]) Next() bool {
method Prev (line 39) | func (iterator *Iterator[T]) Prev() bool {
method Value (line 48) | func (iterator *Iterator[T]) Value() T {
method Index (line 67) | func (iterator *Iterator[T]) Index() int {
method Begin (line 73) | func (iterator *Iterator[T]) Begin() {
method End (line 79) | func (iterator *Iterator[T]) End() {
method First (line 86) | func (iterator *Iterator[T]) First() bool {
method Last (line 94) | func (iterator *Iterator[T]) Last() bool {
method NextTo (line 103) | func (iterator *Iterator[T]) NextTo(f func(index int, value T) bool) bool {
method PrevTo (line 117) | func (iterator *Iterator[T]) PrevTo(f func(index int, value T) bool) bool {
function numOfBits (line 128) | func numOfBits(n int) uint {
function evaluateRange (line 138) | func evaluateRange(index int) (start int, end int) {
FILE: trees/binaryheap/serialization.go
method ToJSON (line 16) | func (heap *Heap[T]) ToJSON() ([]byte, error) {
method FromJSON (line 21) | func (heap *Heap[T]) FromJSON(data []byte) error {
method UnmarshalJSON (line 26) | func (heap *Heap[T]) UnmarshalJSON(bytes []byte) error {
method MarshalJSON (line 31) | func (heap *Heap[T]) MarshalJSON() ([]byte, error) {
FILE: trees/btree/btree.go
type Tree (line 33) | type Tree struct
type Node (line 41) | type Node struct
type Entry (line 48) | type Entry struct
function New (line 54) | func New[K cmp.Ordered, V any](order int) *Tree[K, V] {
function NewWith (line 59) | func NewWith[K comparable, V any](order int, comparator utils.Comparator...
method Put (line 69) | func (tree *Tree[K, V]) Put(key K, value V) {
method Get (line 86) | func (tree *Tree[K, V]) Get(key K) (value V, found bool) {
method GetNode (line 96) | func (tree *Tree[K, V]) GetNode(key K) *Node[K, V] {
method Remove (line 103) | func (tree *Tree[K, V]) Remove(key K) {
method Empty (line 112) | func (tree *Tree[K, V]) Empty() bool {
method Size (line 117) | func (tree *Tree[K, V]) Size() int {
method Size (line 123) | func (node *Node[K, V]) Size() int {
method Keys (line 135) | func (tree *Tree[K, V]) Keys() []K {
method Values (line 145) | func (tree *Tree[K, V]) Values() []V {
method Clear (line 155) | func (tree *Tree[K, V]) Clear() {
method Height (line 161) | func (tree *Tree[K, V]) Height() int {
method Left (line 166) | func (tree *Tree[K, V]) Left() *Node[K, V] {
method LeftKey (line 171) | func (tree *Tree[K, V]) LeftKey() interface{} {
method LeftValue (line 179) | func (tree *Tree[K, V]) LeftValue() interface{} {
method Right (line 187) | func (tree *Tree[K, V]) Right() *Node[K, V] {
method RightKey (line 192) | func (tree *Tree[K, V]) RightKey() interface{} {
method RightValue (line 200) | func (tree *Tree[K, V]) RightValue() interface{} {
method String (line 208) | func (tree *Tree[K, V]) String() string {
method String (line 217) | func (entry *Entry[K, V]) String() string {
method output (line 221) | func (tree *Tree[K, V]) output(buffer *bytes.Buffer, node *Node[K, V], l...
method height (line 233) | func (node *Node[K, V]) height() int {
method isLeaf (line 244) | func (tree *Tree[K, V]) isLeaf(node *Node[K, V]) bool {
method isFull (line 248) | func (tree *Tree[K, V]) isFull(node *Node[K, V]) bool {
method shouldSplit (line 252) | func (tree *Tree[K, V]) shouldSplit(node *Node[K, V]) bool {
method maxChildren (line 256) | func (tree *Tree[K, V]) maxChildren() int {
method minChildren (line 260) | func (tree *Tree[K, V]) minChildren() int {
method maxEntries (line 264) | func (tree *Tree[K, V]) maxEntries() int {
method minEntries (line 268) | func (tree *Tree[K, V]) minEntries() int {
method middle (line 272) | func (tree *Tree[K, V]) middle() int {
method search (line 277) | func (tree *Tree[K, V]) search(node *Node[K, V], key K) (index int, foun...
method searchRecursively (line 296) | func (tree *Tree[K, V]) searchRecursively(startNode *Node[K, V], key K) ...
method insert (line 313) | func (tree *Tree[K, V]) insert(node *Node[K, V], entry *Entry[K, V]) (in...
method insertIntoLeaf (line 320) | func (tree *Tree[K, V]) insertIntoLeaf(node *Node[K, V], entry *Entry[K,...
method insertIntoInternal (line 334) | func (tree *Tree[K, V]) insertIntoInternal(node *Node[K, V], entry *Entr...
method split (line 343) | func (tree *Tree[K, V]) split(node *Node[K, V]) {
method splitNonRoot (line 356) | func (tree *Tree[K, V]) splitNonRoot(node *Node[K, V]) {
method splitRoot (line 389) | func (tree *Tree[K, V]) splitRoot() {
function setParent (line 414) | func setParent[K comparable, V any](nodes []*Node[K, V], parent *Node[K,...
method left (line 420) | func (tree *Tree[K, V]) left(node *Node[K, V]) *Node[K, V] {
method right (line 433) | func (tree *Tree[K, V]) right(node *Node[K, V]) *Node[K, V] {
method leftSibling (line 448) | func (tree *Tree[K, V]) leftSibling(node *Node[K, V], key K) (*Node[K, V...
method rightSibling (line 461) | func (tree *Tree[K, V]) rightSibling(node *Node[K, V], key K) (*Node[K, ...
method delete (line 474) | func (tree *Tree[K, V]) delete(node *Node[K, V], index int) {
method rebalance (line 497) | func (tree *Tree[K, V]) rebalance(node *Node[K, V], deletedKey K) {
method prependChildren (line 566) | func (tree *Tree[K, V]) prependChildren(fromNode *Node[K, V], toNode *No...
method appendChildren (line 572) | func (tree *Tree[K, V]) appendChildren(fromNode *Node[K, V], toNode *Nod...
method deleteEntry (line 577) | func (tree *Tree[K, V]) deleteEntry(node *Node[K, V], index int) {
method deleteChild (line 583) | func (tree *Tree[K, V]) deleteChild(node *Node[K, V], index int) {
FILE: trees/btree/btree_test.go
function TestBTreeGet1 (line 14) | func TestBTreeGet1(t *testing.T) {
function TestBTreeGet2 (line 43) | func TestBTreeGet2(t *testing.T) {
function TestBTreeGet3 (line 78) | func TestBTreeGet3(t *testing.T) {
function TestBTreePut1 (line 124) | func TestBTreePut1(t *testing.T) {
function TestBTreePut2 (line 174) | func TestBTreePut2(t *testing.T) {
function TestBTreePut3 (line 214) | func TestBTreePut3(t *testing.T) {
function TestBTreePut4 (line 265) | func TestBTreePut4(t *testing.T) {
function TestBTreeRemove1 (line 359) | func TestBTreeRemove1(t *testing.T) {
function TestBTreeRemove2 (line 366) | func TestBTreeRemove2(t *testing.T) {
function TestBTreeRemove3 (line 380) | func TestBTreeRemove3(t *testing.T) {
function TestBTreeRemove4 (line 405) | func TestBTreeRemove4(t *testing.T) {
function TestBTreeRemove5 (line 425) | func TestBTreeRemove5(t *testing.T) {
function TestBTreeRemove6 (line 445) | func TestBTreeRemove6(t *testing.T) {
function TestBTreeRemove7 (line 474) | func TestBTreeRemove7(t *testing.T) {
function TestBTreeRemove8 (line 534) | func TestBTreeRemove8(t *testing.T) {
function TestBTreeRemove9 (line 568) | func TestBTreeRemove9(t *testing.T) {
function TestBTreeHeight (line 613) | func TestBTreeHeight(t *testing.T) {
function TestBTreeLeftAndRight (line 666) | func TestBTreeLeftAndRight(t *testing.T) {
function TestBTreeIteratorValuesAndKeys (line 700) | func TestBTreeIteratorValuesAndKeys(t *testing.T) {
function TestBTreeIteratorNextOnEmpty (line 721) | func TestBTreeIteratorNextOnEmpty(t *testing.T) {
function TestBTreeIteratorPrevOnEmpty (line 729) | func TestBTreeIteratorPrevOnEmpty(t *testing.T) {
function TestBTreeIterator1Next (line 737) | func TestBTreeIterator1Next(t *testing.T) {
function TestBTreeIterator1Prev (line 761) | func TestBTreeIterator1Prev(t *testing.T) {
function TestBTreeIterator2Next (line 787) | func TestBTreeIterator2Next(t *testing.T) {
function TestBTreeIterator2Prev (line 806) | func TestBTreeIterator2Prev(t *testing.T) {
function TestBTreeIterator3Next (line 827) | func TestBTreeIterator3Next(t *testing.T) {
function TestBTreeIterator3Prev (line 844) | func TestBTreeIterator3Prev(t *testing.T) {
function TestBTreeIterator4Next (line 863) | func TestBTreeIterator4Next(t *testing.T) {
function TestBTreeIterator4Prev (line 889) | func TestBTreeIterator4Prev(t *testing.T) {
function TestBTreeIteratorBegin (line 917) | func TestBTreeIteratorBegin(t *testing.T) {
function TestBTreeIteratorEnd (line 949) | func TestBTreeIteratorEnd(t *testing.T) {
function TestBTreeIteratorFirst (line 976) | func TestBTreeIteratorFirst(t *testing.T) {
function TestBTreeIteratorLast (line 990) | func TestBTreeIteratorLast(t *testing.T) {
function TestBTreeSearch (line 1004) | func TestBTreeSearch(t *testing.T) {
function assertValidTree (line 1046) | func assertValidTree[K comparable, V any](t *testing.T, tree *Tree[K, V]...
function assertValidTreeNode (line 1052) | func assertValidTreeNode[K comparable, V any](t *testing.T, node *Node[K...
function TestBTreeIteratorNextTo (line 1069) | func TestBTreeIteratorNextTo(t *testing.T) {
function TestBTreeIteratorPrevTo (line 1121) | func TestBTreeIteratorPrevTo(t *testing.T) {
function TestBTreeSerialization (line 1175) | func TestBTreeSerialization(t *testing.T) {
function TestBTreeString (line 1226) | func TestBTreeString(t *testing.T) {
function benchmarkGet (line 1234) | func benchmarkGet(b *testing.B, tree *Tree[int, struct{}], size int) {
function benchmarkPut (line 1242) | func benchmarkPut(b *testing.B, tree *Tree[int, struct{}], size int) {
function benchmarkRemove (line 1250) | func benchmarkRemove(b *testing.B, tree *Tree[int, struct{}], size int) {
function BenchmarkBTreeGet100 (line 1258) | func BenchmarkBTreeGet100(b *testing.B) {
function BenchmarkBTreeGet1000 (line 1269) | func BenchmarkBTreeGet1000(b *testing.B) {
function BenchmarkBTreeGet10000 (line 1280) | func BenchmarkBTreeGet10000(b *testing.B) {
function BenchmarkBTreeGet100000 (line 1291) | func BenchmarkBTreeGet100000(b *testing.B) {
function BenchmarkBTreePut100 (line 1302) | func BenchmarkBTreePut100(b *testing.B) {
function BenchmarkBTreePut1000 (line 1310) | func BenchmarkBTreePut1000(b *testing.B) {
function BenchmarkBTreePut10000 (line 1321) | func BenchmarkBTreePut10000(b *testing.B) {
function BenchmarkBTreePut100000 (line 1332) | func BenchmarkBTreePut100000(b *testing.B) {
function BenchmarkBTreeRemove100 (line 1343) | func BenchmarkBTreeRemove100(b *testing.B) {
function BenchmarkBTreeRemove1000 (line 1354) | func BenchmarkBTreeRemove1000(b *testing.B) {
function BenchmarkBTreeRemove10000 (line 1365) | func BenchmarkBTreeRemove10000(b *testing.B) {
function BenchmarkBTreeRemove100000 (line 1376) | func BenchmarkBTreeRemove100000(b *testing.B) {
FILE: trees/btree/iterator.go
type Iterator (line 13) | type Iterator struct
type position (line 20) | type position
constant begin (line 23) | begin, between, end position = 0, 1, 2
method Iterator (line 27) | func (tree *Tree[K, V]) Iterator() *Iterator[K, V] {
method Next (line 35) | func (iterator *Iterator[K, V]) Next() bool {
method Prev (line 94) | func (iterator *Iterator[K, V]) Prev() bool {
method Value (line 152) | func (iterator *Iterator[K, V]) Value() V {
method Key (line 158) | func (iterator *Iterator[K, V]) Key() K {
method Node (line 164) | func (iterator *Iterator[K, V]) Node() *Node[K, V] {
method Begin (line 170) | func (iterator *Iterator[K, V]) Begin() {
method End (line 178) | func (iterator *Iterator[K, V]) End() {
method First (line 187) | func (iterator *Iterator[K, V]) First() bool {
method Last (line 195) | func (iterator *Iterator[K, V]) Last() bool {
method NextTo (line 204) | func (iterator *Iterator[K, V]) NextTo(f func(key K, value V) bool) bool {
method PrevTo (line 218) | func (iterator *Iterator[K, V]) PrevTo(f func(key K, value V) bool) bool {
FILE: trees/btree/serialization.go
method ToJSON (line 18) | func (tree *Tree[K, V]) ToJSON() ([]byte, error) {
method FromJSON (line 28) | func (tree *Tree[K, V]) FromJSON(data []byte) error {
method UnmarshalJSON (line 44) | func (tree *Tree[K, V]) UnmarshalJSON(bytes []byte) error {
method MarshalJSON (line 49) | func (tree *Tree[K, V]) MarshalJSON() ([]byte, error) {
FILE: trees/redblacktree/iterator.go
type Iterator (line 13) | type Iterator struct
type position (line 19) | type position
constant begin (line 22) | begin, between, end position = 0, 1, 2
method Iterator (line 26) | func (tree *Tree[K, V]) Iterator() *Iterator[K, V] {
method IteratorAt (line 31) | func (tree *Tree[K, V]) IteratorAt(node *Node[K, V]) *Iterator[K, V] {
method Next (line 39) | func (iterator *Iterator[K, V]) Next() bool {
method Prev (line 79) | func (iterator *Iterator[K, V]) Prev() bool {
method Value (line 118) | func (iterator *Iterator[K, V]) Value() V {
method Key (line 124) | func (iterator *Iterator[K, V]) Key() K {
method Node (line 130) | func (iterator *Iterator[K, V]) Node() *Node[K, V] {
method Begin (line 136) | func (iterator *Iterator[K, V]) Begin() {
method End (line 143) | func (iterator *Iterator[K, V]) End() {
method First (line 151) | func (iterator *Iterator[K, V]) First() bool {
method Last (line 159) | func (iterator *Iterator[K, V]) Last() bool {
method NextTo (line 168) | func (iterator *Iterator[K, V]) NextTo(f func(key K, value V) bool) bool {
method PrevTo (line 182) | func (iterator *Iterator[K, V]) PrevTo(f func(key K, value V) bool) bool {
FILE: trees/redblacktree/redblacktree.go
type color (line 25) | type color
constant black (line 28) | black, red color = true, false
type Tree (line 32) | type Tree struct
type Node (line 39) | type Node struct
function New (line 49) | func New[K cmp.Ordered, V any]() *Tree[K, V] {
function NewWith (line 54) | func NewWith[K comparable, V any](comparator utils.Comparator[K]) *Tree[...
method Put (line 60) | func (tree *Tree[K, V]) Put(key K, value V) {
method Get (line 104) | func (tree *Tree[K, V]) Get(key K) (value V, found bool) {
method GetNode (line 114) | func (tree *Tree[K, V]) GetNode(key K) *Node[K, V] {
method Remove (line 120) | func (tree *Tree[K, V]) Remove(key K) {
method Empty (line 151) | func (tree *Tree[K, V]) Empty() bool {
method Size (line 156) | func (tree *Tree[K, V]) Size() int {
method Size (line 162) | func (node *Node[K, V]) Size() int {
method Keys (line 177) | func (tree *Tree[K, V]) Keys() []K {
method Values (line 187) | func (tree *Tree[K, V]) Values() []V {
method Left (line 197) | func (tree *Tree[K, V]) Left() *Node[K, V] {
method Right (line 208) | func (tree *Tree[K, V]) Right() *Node[K, V] {
method Floor (line 226) | func (tree *Tree[K, V]) Floor(key K) (floor *Node[K, V], found bool) {
method Ceiling (line 255) | func (tree *Tree[K, V]) Ceiling(key K) (ceiling *Node[K, V], found bool) {
method Clear (line 277) | func (tree *Tree[K, V]) Clear() {
method String (line 283) | func (tree *Tree[K, V]) String() string {
method String (line 291) | func (node *Node[K, V]) String() string {
function output (line 295) | func output[K comparable, V any](node *Node[K, V], prefix string, isTail...
method lookup (line 323) | func (tree *Tree[K, V]) lookup(key K) *Node[K, V] {
method grandparent (line 339) | func (node *Node[K, V]) grandparent() *Node[K, V] {
method uncle (line 346) | func (node *Node[K, V]) uncle() *Node[K, V] {
method sibling (line 353) | func (node *Node[K, V]) sibling() *Node[K, V] {
method rotateLeft (line 363) | func (tree *Tree[K, V]) rotateLeft(node *Node[K, V]) {
method rotateRight (line 374) | func (tree *Tree[K, V]) rotateRight(node *Node[K, V]) {
method replaceNode (line 385) | func (tree *Tree[K, V]) replaceNode(old *Node[K, V], new *Node[K, V]) {
method insertCase1 (line 400) | func (tree *Tree[K, V]) insertCase1(node *Node[K, V]) {
method insertCase2 (line 408) | func (tree *Tree[K, V]) insertCase2(node *Node[K, V]) {
method insertCase3 (line 415) | func (tree *Tree[K, V]) insertCase3(node *Node[K, V]) {
method insertCase4 (line 427) | func (tree *Tree[K, V]) insertCase4(node *Node[K, V]) {
method insertCase5 (line 439) | func (tree *Tree[K, V]) insertCase5(node *Node[K, V]) {
method maximumNode (line 450) | func (node *Node[K, V]) maximumNode() *Node[K, V] {
method deleteCase1 (line 460) | func (tree *Tree[K, V]) deleteCase1(node *Node[K, V]) {
method deleteCase2 (line 467) | func (tree *Tree[K, V]) deleteCase2(node *Node[K, V]) {
method deleteCase3 (line 481) | func (tree *Tree[K, V]) deleteCase3(node *Node[K, V]) {
method deleteCase4 (line 494) | func (tree *Tree[K, V]) deleteCase4(node *Node[K, V]) {
method deleteCase5 (line 507) | func (tree *Tree[K, V]) deleteCase5(node *Node[K, V]) {
method deleteCase6 (line 527) | func (tree *Tree[K, V]) deleteCase6(node *Node[K, V]) {
function nodeColor (line 540) | func nodeColor[K comparable, V any](node *Node[K, V]) color {
FILE: trees/redblacktree/redblacktree_test.go
function TestRedBlackTreeGet (line 15) | func TestRedBlackTreeGet(t *testing.T) {
function TestRedBlackTreePut (line 61) | func TestRedBlackTreePut(t *testing.T) {
function TestRedBlackTreeRemove (line 102) | func TestRedBlackTreeRemove(t *testing.T) {
function TestRedBlackTreeLeftAndRight (line 166) | func TestRedBlackTreeLeftAndRight(t *testing.T) {
function TestRedBlackTreeCeilingAndFloor (line 200) | func TestRedBlackTreeCeilingAndFloor(t *testing.T) {
function TestRedBlackTreeIteratorNextOnEmpty (line 233) | func TestRedBlackTreeIteratorNextOnEmpty(t *testing.T) {
function TestRedBlackTreeIteratorPrevOnEmpty (line 241) | func TestRedBlackTreeIteratorPrevOnEmpty(t *testing.T) {
function TestRedBlackTreeIterator1Next (line 249) | func TestRedBlackTreeIterator1Next(t *testing.T) {
function TestRedBlackTreeIterator1Prev (line 280) | func TestRedBlackTreeIterator1Prev(t *testing.T) {
function TestRedBlackTreeIterator2Next (line 313) | func TestRedBlackTreeIterator2Next(t *testing.T) {
function TestRedBlackTreeIterator2Prev (line 332) | func TestRedBlackTreeIterator2Prev(t *testing.T) {
function TestRedBlackTreeIterator3Next (line 353) | func TestRedBlackTreeIterator3Next(t *testing.T) {
function TestRedBlackTreeIterator3Prev (line 370) | func TestRedBlackTreeIterator3Prev(t *testing.T) {
function TestRedBlackTreeIterator4Next (line 389) | func TestRedBlackTreeIterator4Next(t *testing.T) {
function TestRedBlackTreeIterator4Prev (line 425) | func TestRedBlackTreeIterator4Prev(t *testing.T) {
function TestRedBlackTreeIteratorBegin (line 463) | func TestRedBlackTreeIteratorBegin(t *testing.T) {
function TestRedBlackTreeIteratorEnd (line 495) | func TestRedBlackTreeIteratorEnd(t *testing.T) {
function TestRedBlackTreeIteratorFirst (line 522) | func TestRedBlackTreeIteratorFirst(t *testing.T) {
function TestRedBlackTreeIteratorLast (line 536) | func TestRedBlackTreeIteratorLast(t *testing.T) {
function TestRedBlackTreeIteratorNextTo (line 550) | func TestRedBlackTreeIteratorNextTo(t *testing.T) {
function TestRedBlackTreeIteratorPrevTo (line 602) | func TestRedBlackTreeIteratorPrevTo(t *testing.T) {
function TestRedBlackTreeSerialization (line 656) | func TestRedBlackTreeSerialization(t *testing.T) {
function TestRedBlackTreeString (line 707) | func TestRedBlackTreeString(t *testing.T) {
function benchmarkGet (line 715) | func benchmarkGet(b *testing.B, tree *Tree[int, struct{}], size int) {
function benchmarkPut (line 723) | func benchmarkPut(b *testing.B, tree *Tree[int, struct{}], size int) {
function benchmarkRemove (line 731) | func benchmarkRemove(b *testing.B, tree *Tree[int, struct{}], size int) {
function BenchmarkRedBlackTreeGet100 (line 739) | func BenchmarkRedBlackTreeGet100(b *testing.B) {
function BenchmarkRedBlackTreeGet1000 (line 750) | func BenchmarkRedBlackTreeGet1000(b *testing.B) {
function BenchmarkRedBlackTreeGet10000 (line 761) | func BenchmarkRedBlackTreeGet10000(b *testing.B) {
function BenchmarkRedBlackTreeGet100000 (line 772) | func BenchmarkRedBlackTreeGet100000(b *testing.B) {
function BenchmarkRedBlackTreePut100 (line 783) | func BenchmarkRedBlackTreePut100(b *testing.B) {
function BenchmarkRedBlackTreePut1000 (line 791) | func BenchmarkRedBlackTreePut1000(b *testing.B) {
function BenchmarkRedBlackTreePut10000 (line 802) | func BenchmarkRedBlackTreePut10000(b *testing.B) {
function BenchmarkRedBlackTreePut100000 (line 813) | func BenchmarkRedBlackTreePut100000(b *testing.B) {
function BenchmarkRedBlackTreeRemove100 (line 824) | func BenchmarkRedBlackTreeRemove100(b *testing.B) {
function BenchmarkRedBlackTreeRemove1000 (line 835) | func BenchmarkRedBlackTreeRemove1000(b *testing.B) {
function BenchmarkRedBlackTreeRemove10000 (line 846) | func BenchmarkRedBlackTreeRemove10000(b *testing.B) {
function BenchmarkRedBlackTreeRemove100000 (line 857) | func BenchmarkRedBlackTreeRemove100000(b *testing.B) {
FILE: trees/redblacktree/serialization.go
method ToJSON (line 18) | func (tree *Tree[K, V]) ToJSON() ([]byte, error) {
method FromJSON (line 28) | func (tree *Tree[K, V]) FromJSON(data []byte) error {
method UnmarshalJSON (line 41) | func (tree *Tree[K, V]) UnmarshalJSON(bytes []byte) error {
method MarshalJSON (line 46) | func (tree *Tree[K, V]) MarshalJSON() ([]byte, error) {
FILE: trees/trees.go
type Tree (line 15) | type Tree interface
FILE: utils/comparator.go
type Comparator (line 9) | type Comparator
function TimeComparator (line 12) | func TimeComparator(a, b time.Time) int {
FILE: utils/comparator_test.go
function TestTimeComparator (line 12) | func TestTimeComparator(t *testing.T) {
FILE: utils/utils.go
function ToString (line 18) | func ToString(value interface{}) string {
FILE: utils/utils_test.go
function TestToStringInts (line 12) | func TestToStringInts(t *testing.T) {
function TestToStringUInts (line 41) | func TestToStringUInts(t *testing.T) {
function TestToStringFloats (line 70) | func TestToStringFloats(t *testing.T) {
function TestToStringOther (line 83) | func TestToStringOther(t *testing.T) {
Condensed preview — 142 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (759K chars).
[
{
"path": ".circleci/config.yml",
"chars": 2312,
"preview": "version: 2.1\n\njobs:\n test:\n parameters:\n version:\n type: string\n default: \"1.21\"\n docker:\n "
},
{
"path": ".github/dependabot.yml",
"chars": 219,
"preview": "# Ref: https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates\n\nversion: "
},
{
"path": ".github/workflows/codeql-analysis.yml",
"chars": 2322,
"preview": "# For most projects, this workflow file will not need changing; you simply need\n# to commit it to your repository.\n#\n# Y"
},
{
"path": ".gitignore",
"chars": 272,
"preview": "# Compiled Object files, Static and Dynamic libs (Shared Objects)\n*.o\n*.a\n*.so\n\n# Folders\n_obj\n_test\n\n# Architecture spe"
},
{
"path": "LICENSE",
"chars": 2148,
"preview": "Copyright (c) 2015, Emir Pasic\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without\n"
},
{
"path": "README.md",
"chars": 56050,
"preview": "[](https://godoc.org/github.com/emirpasic/gods)\n[ 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "examples/arrayqueue/arrayqqueue.go",
"chars": 736,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "examples/arraystack/arraystack.go",
"chars": 827,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "examples/avltree/avltree.go",
"chars": 1258,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "examples/binaryheap/binaryheap.go",
"chars": 1317,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "examples/btree/btree.go",
"chars": 1670,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "examples/circularbuffer/circularbuffer.go",
"chars": 874,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "examples/customcomparator/customcomparator.go",
"chars": 774,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "examples/doublylinkedlist/doublylinkedlist.go",
"chars": 1181,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "examples/enumerablewithindex/enumerablewithindex.go",
"chars": 1635,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "examples/enumerablewithkey/enumerablewithkey.go",
"chars": 1876,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "examples/hashbidimap/hashbidimap.go",
"chars": 1077,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "examples/hashmap/hashmap.go",
"chars": 908,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "examples/hashset/hashset.go",
"chars": 840,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "examples/iteratorwithindex/iteratorwithindex.go",
"chars": 1987,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "examples/iteratorwithkey/iteratorwithkey.go",
"chars": 1975,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "examples/linkedhashmap/linkedhashmap.go",
"chars": 1039,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "examples/linkedhashset/linkedhashset.go",
"chars": 932,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "examples/linkedlistqueue/linkedlistqueue.go",
"chars": 764,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "examples/linkedliststack/linkedliststack.go",
"chars": 762,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "examples/priorityqueue/priorityqueue.go",
"chars": 1357,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "examples/redblacktree/redblacktree.go",
"chars": 1288,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "examples/redblacktreeextended/redblacktreeextended.go",
"chars": 3002,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "examples/serialization/serialization.go",
"chars": 1196,
"preview": "package serialization\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/emirpasic/gods/v2/lists/arraylist\"\n\t\"github.com/emirpasic/gods/v2/m"
},
{
"path": "examples/singlylinkedlist/singlylinkedlist.go",
"chars": 1181,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "examples/treebidimap/treebidimap.go",
"chars": 857,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "examples/treemap/treemap.go",
"chars": 891,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "examples/treeset/treeset.go",
"chars": 801,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "go.mod",
"chars": 45,
"preview": "module github.com/emirpasic/gods/v2\n\ngo 1.21\n"
},
{
"path": "go.sum",
"chars": 171,
"preview": "github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=\ngithub.com/emirpasic/gods v1.18.1/go.m"
},
{
"path": "lists/arraylist/arraylist.go",
"chars": 5659,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "lists/arraylist/arraylist_test.go",
"chars": 20206,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "lists/arraylist/enumerable.go",
"chars": 2407,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "lists/arraylist/iterator.go",
"chars": 4075,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "lists/arraylist/serialization.go",
"chars": 987,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "lists/doublylinkedlist/doublylinkedlist.go",
"chars": 8540,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "lists/doublylinkedlist/doublylinkedlist_test.go",
"chars": 20691,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "lists/doublylinkedlist/enumerable.go",
"chars": 2424,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "lists/doublylinkedlist/iterator.go",
"chars": 4573,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "lists/doublylinkedlist/serialization.go",
"chars": 1066,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "lists/lists.go",
"chars": 1293,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "lists/singlylinkedlist/enumerable.go",
"chars": 2419,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "lists/singlylinkedlist/iterator.go",
"chars": 2795,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "lists/singlylinkedlist/serialization.go",
"chars": 1066,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "lists/singlylinkedlist/singlylinkedlist.go",
"chars": 7132,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "lists/singlylinkedlist/singlylinkedlist_test.go",
"chars": 17668,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "maps/hashbidimap/hashbidimap.go",
"chars": 3074,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "maps/hashbidimap/hashbidimap_test.go",
"chars": 6716,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "maps/hashbidimap/serialization.go",
"chars": 1081,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "maps/hashmap/hashmap.go",
"chars": 2069,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "maps/hashmap/hashmap_test.go",
"chars": 6028,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "maps/hashmap/serialization.go",
"chars": 941,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "maps/linkedhashmap/enumerable.go",
"chars": 2422,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "maps/linkedhashmap/iterator.go",
"chars": 4059,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "maps/linkedhashmap/linkedhashmap.go",
"chars": 2890,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "maps/linkedhashmap/linkedhashmap_test.go",
"chars": 16724,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "maps/linkedhashmap/serialization.go",
"chars": 2168,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "maps/maps.go",
"chars": 1288,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "maps/treebidimap/enumerable.go",
"chars": 2524,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "maps/treebidimap/iterator.go",
"chars": 3975,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "maps/treebidimap/serialization.go",
"chars": 1093,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "maps/treebidimap/treebidimap.go",
"chars": 3758,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "maps/treebidimap/treebidimap_test.go",
"chars": 17686,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "maps/treemap/enumerable.go",
"chars": 2562,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "maps/treemap/iterator.go",
"chars": 3965,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "maps/treemap/serialization.go",
"chars": 916,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "maps/treemap/treemap.go",
"chars": 4671,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "maps/treemap/treemap_test.go",
"chars": 20544,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "queues/arrayqueue/arrayqueue.go",
"chars": 2398,
"preview": "// Copyright (c) 2021, Aryan Ahadinia. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// lic"
},
{
"path": "queues/arrayqueue/arrayqueue_test.go",
"chars": 11979,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "queues/arrayqueue/iterator.go",
"chars": 4149,
"preview": "// Copyright (c) 2021, Aryan Ahadinia. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// lic"
},
{
"path": "queues/arrayqueue/serialization.go",
"chars": 939,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "queues/circularbuffer/circularbuffer.go",
"chars": 3968,
"preview": "// Copyright (c) 2021, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "queues/circularbuffer/circularbuffer_test.go",
"chars": 16035,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "queues/circularbuffer/iterator.go",
"chars": 4206,
"preview": "// Copyright (c) 2021, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "queues/circularbuffer/serialization.go",
"chars": 1110,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "queues/linkedlistqueue/iterator.go",
"chars": 2638,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "queues/linkedlistqueue/linkedlistqueue.go",
"chars": 2451,
"preview": "// Copyright (c) 2021, Aryan Ahadinia. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// lic"
},
{
"path": "queues/linkedlistqueue/linkedlistqueue_test.go",
"chars": 8489,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "queues/linkedlistqueue/serialization.go",
"chars": 944,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "queues/priorityqueue/iterator.go",
"chars": 3821,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "queues/priorityqueue/priorityqueue.go",
"chars": 2843,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "queues/priorityqueue/priorityqueue_test.go",
"chars": 14167,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "queues/priorityqueue/serialization.go",
"chars": 942,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "queues/queues.go",
"chars": 1471,
"preview": "// Copyright (c) 2021, Aryan Ahadinia. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// lic"
},
{
"path": "sets/hashset/hashset.go",
"chars": 3766,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "sets/hashset/hashset_test.go",
"chars": 7713,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "sets/hashset/serialization.go",
"chars": 1026,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "sets/linkedhashset/enumerable.go",
"chars": 2378,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "sets/linkedhashset/iterator.go",
"chars": 3986,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "sets/linkedhashset/linkedhashset.go",
"chars": 4485,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "sets/linkedhashset/linkedhashset_test.go",
"chars": 17292,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "sets/linkedhashset/serialization.go",
"chars": 1032,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "sets/sets.go",
"chars": 958,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "sets/treeset/enumerable.go",
"chars": 2536,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "sets/treeset/iterator.go",
"chars": 4224,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "sets/treeset/serialization.go",
"chars": 1026,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "sets/treeset/treeset.go",
"chars": 4865,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "sets/treeset/treeset_test.go",
"chars": 17394,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "stacks/arraystack/arraystack.go",
"chars": 2578,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "stacks/arraystack/arraystack_test.go",
"chars": 11745,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "stacks/arraystack/iterator.go",
"chars": 4199,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "stacks/arraystack/serialization.go",
"chars": 939,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "stacks/linkedliststack/iterator.go",
"chars": 2659,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "stacks/linkedliststack/linkedliststack.go",
"chars": 2429,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "stacks/linkedliststack/linkedliststack_test.go",
"chars": 8182,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "stacks/linkedliststack/serialization.go",
"chars": 944,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "stacks/stacks.go",
"chars": 1082,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "testutils/testutils.go",
"chars": 384,
"preview": "package testutils\n\nimport \"testing\"\n\nfunc SameElements[T comparable](t *testing.T, actual, expected []T) {\n\tif len(actua"
},
{
"path": "trees/avltree/avltree.go",
"chars": 10082,
"preview": "// Copyright (c) 2017, Benjamin Scher Purcell. All rights reserved.\n// Use of this source code is governed by a BSD-styl"
},
{
"path": "trees/avltree/avltree_test.go",
"chars": 20924,
"preview": "// Use of this source code is governed by a BSD-style\n// license that can be found in the LICENSE file.\n\npackage avltree"
},
{
"path": "trees/avltree/iterator.go",
"chars": 4881,
"preview": "// Copyright (c) 2017, Benjamin Scher Purcell. All rights reserved.\n// Use of this source code is governed by a BSD-styl"
},
{
"path": "trees/avltree/serialization.go",
"chars": 1232,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "trees/binaryheap/binaryheap.go",
"chars": 4904,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "trees/binaryheap/binaryheap_test.go",
"chars": 12375,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "trees/binaryheap/iterator.go",
"chars": 4820,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "trees/binaryheap/serialization.go",
"chars": 923,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "trees/btree/btree.go",
"chars": 18074,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "trees/btree/btree_test.go",
"chars": 40779,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "trees/btree/iterator.go",
"chars": 7584,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "trees/btree/serialization.go",
"chars": 1230,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "trees/redblacktree/iterator.go",
"chars": 5695,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "trees/redblacktree/redblacktree.go",
"chars": 13081,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "trees/redblacktree/redblacktree_test.go",
"chars": 21126,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "trees/redblacktree/serialization.go",
"chars": 1226,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "trees/trees.go",
"chars": 806,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "utils/comparator.go",
"chars": 429,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "utils/comparator_test.go",
"chars": 634,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "utils/utils.go",
"chars": 1156,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
},
{
"path": "utils/utils_test.go",
"chars": 3132,
"preview": "// Copyright (c) 2015, Emir Pasic. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license"
}
]
About this extraction
This page contains the full source code of the emirpasic/gods GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 142 files (676.3 KB), approximately 203.3k tokens, and a symbol index with 1495 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.