diff --git a/README.md b/README.md index 9881cee..cdc1830 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,9 @@ Implementation of various data structures and algorithms in Go. - [Enumerable](#enumerable) - [EnumerableWithIndex](#enumerablewithindex) - [EnumerableWithKey](#enumerablewithkey) + - [Serialization](#serialization) + - [JSONSerializer](#jsonserializer) + - [JSONDeserializer](#jsondeserializer) - [Sort](#sort) - [Container](#container) - [Appendix](#appendix) @@ -104,7 +107,7 @@ type List interface { A [list](#lists) backed by a dynamic array that grows and shrinks implicitly. -Implements [List](#lists), [IteratorWithIndex](#iteratorwithindex) and [EnumerableWithIndex](#enumerablewithindex) interfaces. +Implements [List](#lists), [IteratorWithIndex](#iteratorwithindex), [EnumerableWithIndex](#enumerablewithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces. ```go package main @@ -141,7 +144,7 @@ func main() { A [list](#lists) where each element points to the next element in the list. -Implements [List](#lists), [IteratorWithIndex](#iteratorwithindex) and [EnumerableWithIndex](#enumerablewithindex) interfaces. +Implements [List](#lists), [IteratorWithIndex](#iteratorwithindex), [EnumerableWithIndex](#enumerablewithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces. ```go package main @@ -178,7 +181,7 @@ func main() { A [list](#lists) where each element points to the next and previous elements in the list. -Implements [List](#lists), [IteratorWithIndex](#iteratorwithindex) and [EnumerableWithIndex](#enumerablewithindex) interfaces. +Implements [List](#lists), [IteratorWithIndex](#iteratorwithindex), [EnumerableWithIndex](#enumerablewithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces. ```go package main @@ -235,7 +238,7 @@ type Set interface { 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) interface. +Implements [Set](#sets), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces. ```go package main @@ -262,7 +265,7 @@ func main() { A [set](#sets) backed by a [red-black tree](#redblacktree) to keep the elements ordered with respect to the [comparator](#comparator). -Implements [Set](#sets), [IteratorWithIndex](#iteratorwithindex) and [EnumerableWithIndex](#enumerablewithindex) interfaces. +Implements [Set](#sets), [IteratorWithIndex](#iteratorwithindex), [EnumerableWithIndex](#enumerablewithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces. ```go package main @@ -309,7 +312,7 @@ type Stack interface { A [stack](#stacks) based on a [linked list](#singlylinkedlist). -Implements [Stack](#stacks) and [IteratorWithIndex](#iteratorwithindex) interfaces. +Implements [Stack](#stacks), [IteratorWithIndex](#iteratorwithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces. ```go package main @@ -336,7 +339,7 @@ func main() { A [stack](#stacks) based on a [array list](#arraylist). -Implements [Stack](#stacks) and [IteratorWithIndex](#iteratorwithindex) interfaces. +Implements [Stack](#stacks), [IteratorWithIndex](#iteratorwithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces. ```go package main @@ -394,7 +397,7 @@ type BidiMap interface { A [map](#maps) based on hash tables. Keys are unordered. -Implements [Map](#maps) interface. +Implements [Map](#maps), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces. ```go package main @@ -421,7 +424,7 @@ func main() { A [map](#maps) based on [red-black tree](#redblacktree). Keys are ordered ordered with respect to the [comparator](#comparator). -Implements [Map](#maps), [IteratorWithKey](#iteratorwithkey) and [EnumerableWithKey](#enumerablewithkey) interfaces. +Implements [Map](#maps), [IteratorWithKey](#iteratorwithkey), [EnumerableWithKey](#enumerablewithkey), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces. ```go package main @@ -452,7 +455,7 @@ func main() { A [map](#maps) based on two hashmaps. Keys are unordered. -Implements [BidiMap](#maps) interface. +Implements [BidiMap](#maps), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces. ```go package main @@ -481,7 +484,7 @@ func main() { 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), [IteratorWithKey](#iteratorwithkey) and [EnumerableWithKey](#enumerablewithkey) interfaces. +Implements [BidiMap](#maps), [IteratorWithKey](#iteratorwithkey), [EnumerableWithKey](#enumerablewithkey), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces. ```go package main @@ -531,7 +534,7 @@ A red–black [tree](#trees) is a binary search tree with an extra bit of data p The balancing of the tree is not perfect but it is good enough to allow it to guarantee searching in O(log n) time, where n is the total number of elements in the tree. The insertion and deletion operations, along with the tree rearrangement and recoloring, are also performed in O(log n) time. [Wikipedia](http://en.wikipedia.org/wiki/Red%E2%80%93black_tree) -Implements [Tree](#trees) and [ReverseIteratorWithKey](#reverseiteratorwithkey) interfaces. +Implements [Tree](#trees), [ReverseIteratorWithKey](#reverseiteratorwithkey), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.

@@ -597,7 +600,7 @@ AVL [tree](#trees) is a self-balancing binary search tree. In an AVL tree, the h AVL trees are often compared with red–black trees because both support the same set of operations and take O(log n) time for the basic operations. For lookup-intensive applications, AVL trees are faster than red–black trees because they are more strictly balanced. [Wikipedia](https://en.wikipedia.org/wiki/AVL_tree) -Implements [Tree](#trees) and [ReverseIteratorWithKey](#reverseiteratorwithkey) interfaces. +Implements [Tree](#trees), [ReverseIteratorWithKey](#reverseiteratorwithkey), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.


AVL tree with balance factors (green)

@@ -664,7 +667,7 @@ According to Knuth's definition, a B-tree of order m is a tree which satisfies t Each internal node’s keys act as separation values which divide its subtrees. For example, if an internal node has 3 child nodes (or subtrees) then it must have 2 keys: a1 and a2. All values in the leftmost subtree will be less than a1, all values in the middle subtree will be between a1 and a2, and all values in the rightmost subtree will be greater than a2.[Wikipedia](http://en.wikipedia.org/wiki/Red%E2%80%93black_tree) -Implements [Tree](#trees) and [ReverseIteratorWithKey](#reverseiteratorwithkey) interfaces. +Implements [Tree](#trees), [ReverseIteratorWithKey](#reverseiteratorwithkey), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.

@@ -736,7 +739,7 @@ A binary heap is a [tree](#trees) created using a binary tree. It can be seen as All nodes are either greater than or equal to or less than or equal to each of its children, according to a comparison predicate defined for the heap. [Wikipedia](http://en.wikipedia.org/wiki/Binary_heap) -Implements [Tree](#trees) and [ReverseIteratorWithIndex](#reverseiteratorwithindex) interfaces. +Implements [Tree](#trees), [ReverseIteratorWithIndex](#reverseiteratorwithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.

@@ -1214,6 +1217,103 @@ func main() { } ``` +### 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 ( + "fmt" + "github.com/emirpasic/gods/maps/hashmap" +) + +func main() { + m := hashmap.New() + m.Put("a", "1") + m.Put("b", "2") + m.Put("c", "3") + + json, err := m.ToJSON() + if err != nil { + fmt.Println(err) + } + fmt.Println(string(json)) // {"a":"1","b":"2","c":"3"} +``` + +Typical usage for value-only structures: +```go +package main + +import ( + "fmt" + "github.com/emirpasic/gods/lists/arraylist" +) + +func main() { + list := arraylist.New() + list.Add("a", "b", "c") + + json, err := list.ToJSON() + if err != nil { + fmt.Println(err) + } + fmt.Println(string(json)) // ["a","b","c"] +} +``` + +#### JSONDeserializer + +Populates the container with elements from the input JSON representation. + +Typical usage for key-value structures: +```go +package main + +import ( + "fmt" + "github.com/emirpasic/gods/lists/arraylist" +) + +func main() { + list := arraylist.New() + + json := []byte(`["a","b"]`) + err := list.FromJSON(json) + if err != nil { + fmt.Println(err) + } + fmt.Println(list) // ArrayList ["a","b"] +} +``` + +Typical usage for value-only structures: +```go +package main + +import ( + "fmt" + "github.com/emirpasic/gods/lists/arraylist" +) + +func main() { + list := arraylist.New() + list.Add("a", "b", "c") + + json, err := list.ToJSON() + if err != nil { + fmt.Println(err) + } + fmt.Println(string(json)) // ["a","b","c"] +} +``` + ### Sort Sort is a general purpose sort function.