Update README.md

pull/12/head
Emir Pasic 8 years ago committed by GitHub
parent 49c50d0c4d
commit 07e8634b62

@ -1,8 +1,8 @@
[![Build Status](https://travis-ci.org/emirpasic/gods.svg)](https://travis-ci.org/emirpasic/gods) [![GoDoc](https://godoc.org/github.com/emirpasic/gods?status.svg)](https://godoc.org/github.com/emirpasic/gods)
[![GoDoc](https://godoc.org/github.com/emirpasic/gods?status.svg)](https://godoc.org/github.com/emirpasic/gods) [![Build Status](https://travis-ci.org/emirpasic/gods.svg)](https://travis-ci.org/emirpasic/gods) [![PyPI](https://img.shields.io/pypi/l/Django.svg?maxAge=2592000)](https://github.com/emirpasic/gods/blob/enums/LICENSE)
# GoDS (Go Data Structures)
Implementation of various data structures in Go.
Implementation of various data structures and algorithms in Go.
## Data Structures
@ -25,7 +25,14 @@ Implementation of various data structures in Go.
- [BinaryHeap](#binaryheap)
- [Functions](#functions)
- [Comparator](#comparator)
- [Iterator](#iterator)
- [IteratorWithIndex](#iteratorwithindex)
- [IteratorWithKey](#iteratorwithkey)
- [Enumerable](#enumerable)
- [EnumerableWithIndex](#enumerablewithindex)
- [EnumerableWithKey](#enumerablewithkey)
- [Sort](#sort)
- [Container](#container)
## Containers
@ -41,32 +48,27 @@ type Container interface {
}
```
Containers are either ordered or unordered:
- Ordered containers:
- ArrayList
- SinglyLinkedList
- DoublyLinkedList
- TreeSet
- LinkedListStack
- ArrayStack
- TreeMap
- RedBlackTree
- BinaryHeap
- Unordered containers:
- HashSet
- HashMap
All ordered containers have stateful iterators:
- IteratorWithIndex (iterates over containers whose elements are referenced by an index)
- IteratorWithKey (iterates over containers whose elements are referenced by a key)
Containers are either ordered or unordered. All ordered containers provide [stateful iterators](iterator) and some of them allow [enumerable functions](#enumerable).
| Container | Ordered | [Iterator](iterator) | [Enumerable](#enumerable) | Ordered by |
| :--- | :---: | :---: | :---: | :---: |
| [ArrayList](#arraylist) | yes | yes | yes | index |
| [SinglyLinkedList](#singlylinkedlist) | yes | yes | yes | index |
| [DoublyLinkedList](#doublylinkedlist) | yes | yes | yes | index |
| [HashSet](#hashset) | no | no | no | index |
| [TreeSet](#treeset) | yes | yes | yes | index |
| [LinkedListStack](#linkedliststack) | yes | yes | no | index |
| [ArrayStack](#arraystack) | yes | yes | no | index |
| [HashMap](#hashmap) | no | no | no | key |
| [TreeMap](#treemap) | yes | yes | yes | key |
| [RedBlackTree](#redblacktree) | yes | yes | yes | key |
| [BinaryHeap](#binaryheap) | yes | yes | yes | index |
### Lists
A list is a data structure that can store values and may have repeated values. There is no ordering in a list. The user can access and remove a value by the index position.
A list is a data structure that stores values and may have repeated values.
All lists implement the list interface with the following methods:
Implements [Container](#containers) interface.
```go
type List interface {
@ -88,10 +90,9 @@ type List interface {
#### ArrayList
This structure implements the List interface and is backed by a dynamic array that grows and shrinks implicitly (by 100% when capacity is reached).
Direct access method _Get(index)_ is guaranteed a constant time performance. Remove is of linear time performance. Checking with _Contains()_ is of quadratic complexity.
A [list](#lists) backed by a dynamic array that grows and shrinks implicitly.
Implements [List](#lists), [IteratorWithIndex](#iteratorwithindex) and [EnumerableWithIndex](#enumerablewithindex) interfaces.
```go
package main
@ -126,9 +127,9 @@ func main() {
#### SinglyLinkedList
This structure implements the _List_ interface and is a linked data structure where each value points to the next in the list.
A [list](#lists) where each element points to the next element in the list.
Direct access method _Get(index)_ and _Remove()_ are of linear performance. _Append_ and _Prepend_ are of constant time performance. Checking with _Contains()_ is of quadratic complexity.
Implements [List](#lists), [IteratorWithIndex](#iteratorwithindex) and [EnumerableWithIndex](#enumerablewithindex) interfaces.
```go
package main
@ -163,9 +164,9 @@ func main() {
#### DoublyLinkedList
This structure implements the _List_ interface and is a linked data structure where each value points to the next and previous element in the list.
A [list](#lists) where each element points to the next and previous elements in the list.
Direct access method _Get(index)_ and _Remove()_ are of linear performance. _Append_ and _Prepend_ are of constant time performance. Checking with _Contains()_ is of quadratic complexity.
Implements [List](#lists), [IteratorWithIndex](#iteratorwithindex) and [EnumerableWithIndex](#enumerablewithindex) interfaces.
```go
package main
@ -200,9 +201,9 @@ func main() {
### Sets
A set is a data structure that can store elements and 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 structed is often used to ensure that no duplicates are present in a collection.
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 structed is often used to ensure that no duplicates are present in a container.
All sets implement the set interface with the following methods:
Implements [Container](#containers) interface.
```go
type Set interface {
@ -220,9 +221,9 @@ type Set interface {
#### HashSet
This structure implements the Set interface and is backed by a hash table (actually a Go's map). It makes no guarantees as to the iteration order of the set, since Go randomizes this iteration order on maps.
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.
This structure offers constant time performance for the basic operations (add, remove, contains and size).
Implements [Set](#sets) interface.
```go
package main
@ -247,9 +248,9 @@ func main() {
#### TreeSet
This structure implements the Set interface and is backed by a red-black tree to keep the elements sorted with respect to the comparator.
A [set](#sets) backed by a [red-black tree](#redblacktree) to keep the elements ordered with respect to the [comparator](#comparator).
This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains).
Implements [Set](#sets), [IteratorWithIndex](#iteratorwithindex) and [EnumerableWithIndex](#enumerablewithindex) interfaces.
```go
package main
@ -274,9 +275,10 @@ func main() {
### Stacks
The stack interface represents a last-in-first-out (LIFO) collection of objects. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to check whether the stack is empty and the size (number of elements).
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.
All stacks implement the stack interface with the following methods:
```go
type Stack interface {
Push(value interface{})
@ -291,11 +293,11 @@ type Stack interface {
}
```
#####LinkedListStack
#### LinkedListStack
This stack structure is based on a linked list, i.e. each previous element has a point to the next.
A [stack](#stacks) based on a [linked list](#singlylinkedlist).
All operations are guaranteed constant time performance, except _Values()_, which is as always of linear time performance.
Implements [Stack](#stacks) and [IteratorWithIndex](#iteratorwithindex) interfaces.
```go
package main
@ -320,9 +322,9 @@ func main() {
#### ArrayStack
This stack structure is back by ArrayList.
A [stack](#stacks) based on a [array list](#arraylist).
All operations are guaranted constant time performance.
Implements [Stack](#stacks) and [IteratorWithIndex](#iteratorwithindex) interfaces.
```go
package main
@ -347,9 +349,10 @@ func main() {
### Maps
Structure that maps keys to values. A map cannot contain duplicate keys and each key can map to at most one value.
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.
All maps implement the map interface with the following methods:
```go
type Map interface {
Put(key interface{}, value interface{})
@ -367,9 +370,9 @@ type Map interface {
#### HashMap
Map structure based on hash tables, more exactly, Go's map. Keys are unordered.
A [map](#maps) based on hash tables. Keys are unordered.
All operations are guaranted constant time performance, except _Key()_ and _Values()_ retrieval that of linear time performance.
Implements [Map](#maps) interface.
```go
package main
@ -379,7 +382,7 @@ 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(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
@ -394,11 +397,9 @@ func main() {
#### TreeMap
Map structure based on our red-black tree implementation. Keys are ordered with respect to the passed comparator.
A [map](#maps) based on [red-black tree](#redblacktree). Keys are ordered ordered with respect to the [comparator](#comparator).
_Put()_, _Get()_ and _Remove()_ are guaranteed log(n) time performance.
_Key()_ and _Values()_ methods return keys and values respectively in order of the keys. These meethods are quaranteed linear time performance.
Implements [Map](#maps), [IteratorWithKey](#iteratorwithkey) and [EnumerableWithKey](#enumerablewithkey) interfaces.
```go
package main
@ -429,7 +430,8 @@ func main() {
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.
All trees implement the tree interface with the following methods:
Implements [Container](#containers) interface.
```go
type Tree interface {
containers.Container
@ -442,9 +444,11 @@ type Tree interface {
#### RedBlackTree
A redblack tree is a binary search tree with an extra bit of data per node, its color, which can be either red or black. The extra bit of storage ensures an approximately balanced tree by constraining how nodes are colored from any path from the root to the leaf. Thus, it is a data structure which is a type of self-balancing binary search tree.
A redblack [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. <small>[Wikipedia](http://en.wikipedia.org/wiki/Red%E2%80%93black_tree)</small>
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.<small>[Wikipedia](http://en.wikipedia.org/wiki/Red%E2%80%93black_tree)</small>
Implements [Tree](#trees) and [IteratorWithKey](#iteratorwithkey) interfaces.
<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" /></center>
@ -457,7 +461,7 @@ import (
)
func main() {
tree := rbt.NewWithIntComparator() // empty(keys are of type int)
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)
@ -506,7 +510,7 @@ Extending the red-black tree's functionality has been demonstrated in the follo
#### BinaryHeap
A binary heap is a heap data structure created using a binary tree. It can be seen as a binary tree with two additional constraints:
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:
@ -514,6 +518,8 @@ A binary heap is a heap data structure created using a binary tree. It can be se
- 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. <small>[Wikipedia](http://en.wikipedia.org/wiki/Binary_heap)</small>
Implements [Tree](#trees) and [IteratorWithIndex](#iteratorwithindex) interfaces.
<center><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/3/38/Max-Heap.svg/501px-Max-Heap.svg.png" width="300px" height="200px" /></center>
@ -561,70 +567,36 @@ Various helper functions used throughout the library.
### Comparator
Some data structures (e.g. TreeMap, TreeSet) require a comparator function to sort their contained elements. This comparator is necessary during the initalization.
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:
```go
Return values:
-1, if a < b
0, if a == b
1, if a > b
```go
-1, if a < b
0, if a == b
1, if a > b
```
Comparator signature:
type Comparator func(a, b interface{}) int
```go
type Comparator func(a, b interface{}) int
```
Two common comparators are included in the library:
#### IntComparator
```go
func IntComparator(a, b interface{}) int {
aInt := a.(int)
bInt := b.(int)
switch {
case aInt > bInt:
return 1
case aInt < bInt:
return -1
default:
return 0
}
}
func IntComparator(a, b interface{}) int
```
#### StringComparator
```go
func StringComparator(a, b interface{}) int {
s1 := a.(string)
s2 := b.(string)
min := len(s2)
if len(s1) < len(s2) {
min = len(s1)
}
diff := 0
for i := 0; i < min && diff == 0; i++ {
diff = int(s1[i]) - int(s2[i])
}
if diff == 0 {
diff = len(s1) - len(s2)
}
if diff < 0 {
return -1
}
if diff > 0 {
return 1
}
return 0
}
func StringComparator(a, b interface{}) int
```
#### CustomComparator
Writing custom comparators is easy:
```go
package main
@ -638,7 +610,7 @@ type User struct {
name string
}
// Comparator function (sort by IDs)
// Custom comparator (sort by IDs)
func byID(a, b interface{}) int {
// Type assertion, program will panic if this is not respected
@ -667,6 +639,104 @@ func main() {
}
```
### 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. '
#### IteratorWithIndex
A [iterator](#iterator) whose elements are referenced by an index. Typical usage:
```go
it := list.Iterator()
for it.Next() {
index, value := it.Index(), it.Value()
...
}
```
#### IteratorWithKey
A [iterator](#iterator) whose elements are referenced by a key. Typical usage:
```go
it := map.Iterator()
for it.Next() {
key, value := it.Key(), it.Value()
...
}
```
### Enumerable
Enumerable functions for ordered containers that implement [EnumerableWithIndex](#enumerablewithindex) or [EnumerableWithKey](#enumerablewithkey) interfaces.
#### EnumerableWithIndex
Enumerable function for ordered containers whose values can be fetched by an index.
Definition:
```go
type EnumerableWithIndex interface {
// Calls the given function once for each element, passing that element's index and value.
Each(func(index int, value interface{}))
// 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
// Returns a new container containing all elements for which the given function returns a true value.
Select(func(index int, value interface{}) bool) Container
// 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 interface{}) bool) bool
// 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 interface{}) bool) bool
// 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 interface{}) bool) (int, interface{})
}
```
#### EnumerableWithKey
Enumerable functions for ordered containers whose values whose elements are key/value pairs.
Definition:
```go
type EnumerableWithKey interface {
// Calls the given function once for each element, passing that element's key and value.
Each(func(key interface{}, value interface{}))
// 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
// Returns a new container containing all elements for which the given function returns a true value.
Select(func(key interface{}, value interface{}) bool) Container
// 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 interface{}, value interface{}) bool) bool
// Passes each element of the container to the given function and
// returns true if the function returns true for all elements.
All(func(key interface{}, value interface{}) bool) bool
// 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 interface{}, value interface{}) bool) (interface{}, interface{})
}
```
### Sort
Sort uses timsort for best performance on real-world data. Lists have an in-place _Sort()_ method. All containers can return their sorted elements via _GetSortedValues()_ call.
@ -688,11 +758,31 @@ func main() {
}
```
# Motivations
### Container
Container specific operations:
```go
// Returns sorted container''s elements with respect to the passed comparator.
// Does not effect the ordering of elements within the container.
// Uses timsort.
func GetSortedValues(container Container, comparator utils.Comparator) []interface{}
```
Usage:
```go
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, etc.
Collections and data structures found in other languages: Java Collections, C++ Standard Template Library (STL) containers, Qt Containers, Ruby Enumerable etc.
# Goals
### Goals
**Fast algorithms**:
@ -722,17 +812,17 @@ There is often a tug of war between speed and memory when crafting algorithms. W
Thread safety is not a concern of this project, this should be handled at a higher level.
# Testing and Benchmarking
### Testing and Benchmarking
`go test -v -bench . -benchmem -benchtime 1s ./...`
## Contributing
### Contributing
Biggest contribution towards this library is to use it and give us feedback for further improvements and additions.
For direct contributions, branch of from master and do _pull request_.
For direct contributions, _pull request_ into master or ask to become a contributor.
# License
### License
This library is distributed under the BSD-style license found in the [LICENSE](https://github.com/emirpasic/gods/blob/master/LICENSE) file.

Loading…
Cancel
Save