Skip to content

Commit

Permalink
Trie docs
Browse files Browse the repository at this point in the history
  • Loading branch information
zyedidia committed Dec 18, 2021
1 parent 9a97d2d commit 330592a
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 5 deletions.
74 changes: 74 additions & 0 deletions DOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ Package generic provides types and constraints useful for implementing generic d
- [func (b ByteSlice) Hash() uint64](<#func-byteslice-hash>)
- [type Comparable](<#type-comparable>)
- [type Float32](<#type-float32>)
- [func (f Float32) Equals(other Float32) bool](<#func-float32-equals>)
- [func (f Float32) Hash() uint64](<#func-float32-hash>)
- [func (f Float32) Less(other Float32) bool](<#func-float32-less>)
- [type Float64](<#type-float64>)
- [func (f Float64) Equals(other Float64) bool](<#func-float64-equals>)
- [func (f Float64) Hash() uint64](<#func-float64-hash>)
- [func (f Float64) Less(other Float64) bool](<#func-float64-less>)
- [type Hashable](<#type-hashable>)
- [type Int](<#type-int>)
- [func (i Int) Equals(other Int) bool](<#func-int-equals>)
Expand All @@ -46,10 +52,14 @@ Package generic provides types and constraints useful for implementing generic d
- [func (i Int8) Hash() uint64](<#func-int8-hash>)
- [func (i Int8) Less(other Int8) bool](<#func-int8-less>)
- [type Lesser](<#type-lesser>)
- [type Sliceable](<#type-sliceable>)
- [type String](<#type-string>)
- [func (s String) Append(other String) String](<#func-string-append>)
- [func (s String) At(idx int) byte](<#func-string-at>)
- [func (s String) Equals(other String) bool](<#func-string-equals>)
- [func (s String) Hash() uint64](<#func-string-hash>)
- [func (s String) Less(other String) bool](<#func-string-less>)
- [func (s String) Slice(low, high int) String](<#func-string-slice>)
- [type Uint](<#type-uint>)
- [func (u Uint) Equals(other Uint) bool](<#func-uint-equals>)
- [func (u Uint) Hash() uint64](<#func-uint-hash>)
Expand Down Expand Up @@ -152,12 +162,48 @@ type Comparable[T any] interface {
type Float32 float32
```

### func \(Float32\) Equals

```go
func (f Float32) Equals(other Float32) bool
```

### func \(Float32\) Hash

```go
func (f Float32) Hash() uint64
```

### func \(Float32\) Less

```go
func (f Float32) Less(other Float32) bool
```

## type Float64

```go
type Float64 float64
```

### func \(Float64\) Equals

```go
func (f Float64) Equals(other Float64) bool
```

### func \(Float64\) Hash

```go
func (f Float64) Hash() uint64
```

### func \(Float64\) Less

```go
func (f Float64) Less(other Float64) bool
```

## type Hashable

```go
Expand Down Expand Up @@ -295,12 +341,34 @@ type Lesser[T any] interface {
}
```

## type Sliceable

```go
type Sliceable[T any] interface {
At(idx int) T
Slice(low, high int) Sliceable[T]
Append(s Sliceable[T]) Sliceable[T]
}
```

## type String

```go
type String string
```

### func \(String\) Append

```go
func (s String) Append(other String) String
```

### func \(String\) At

```go
func (s String) At(idx int) byte
```

### func \(String\) Equals

```go
Expand All @@ -319,6 +387,12 @@ func (s String) Hash() uint64
func (s String) Less(other String) bool
```

### func \(String\) Slice

```go
func (s String) Slice(low, high int) String
```

## type Uint

```go
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DOCS=avl/README.md btree/README.md cache/README.md hashmap/README.md hashset/README.md interval/README.md iter/README.md list/README.md rope/README.md stack/README.md DOC.md
DOCS=avl/README.md btree/README.md cache/README.md hashmap/README.md hashset/README.md interval/README.md iter/README.md list/README.md rope/README.md stack/README.md trie/README.md DOC.md

all: $(DOCS)

Expand Down
9 changes: 9 additions & 0 deletions btree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func main() {
- [func (t *Tree[K, V]) GetZ(key K) V](<#func-badrecv-getz>)
- [func (t *Tree[K, V]) Iter() iter.Iter[KV[K, V]]](<#func-badrecv-iter>)
- [func (t *Tree[K, V]) Put(key K, val V)](<#func-badrecv-put>)
- [func (t *Tree[K, V]) Remove(key K)](<#func-badrecv-remove>)
- [func (t *Tree[K, V]) Size() int](<#func-badrecv-size>)


Expand Down Expand Up @@ -116,6 +117,14 @@ func (t *Tree[K, V]) Put(key K, val V)

Put associates 'key' with 'val'\.

### func \(\*BADRECV\) Remove

```go
func (t *Tree[K, V]) Remove(key K)
```

Remove removes the value associated with 'key'\.

### func \(\*BADRECV\) Size

```go
Expand Down
7 changes: 5 additions & 2 deletions btree/btree.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import (
"github.com/zyedidia/generic/iter"
)

const maxChildren = 64 // must be even and >= 2
const maxChildren = 64 // must be even and > 2

type KV[K g.Lesser[K], V any] struct {
Key K
Val V
}

// Adapted from the Princeton Algorithms 4th ed. B-tree implementation.
// Adapted from the B-tree implementation in Algorithms, 4th ed., by Robert
// Sedgewick and Kevin Wayne.
// https://algs4.cs.princeton.edu/62btree/BTree.java.html.

// Tree implements a B-tree.
type Tree[K g.Lesser[K], V any] struct {
Expand Down Expand Up @@ -105,6 +107,7 @@ func (t *Tree[K, V]) Put(key K, val V) {
t.height++
}

// Remove removes the value associated with 'key'.
func (t *Tree[K, V]) Remove(key K) {
_, ok := t.Get(key)
if !ok {
Expand Down
143 changes: 143 additions & 0 deletions trie/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<!-- Code generated by gomarkdoc. DO NOT EDIT -->

# trie

```go
import "github.com/zyedidia/generic/trie"
```

Package trie provides an implementation of a ternary search trie\.

<details><summary>Example</summary>
<p>

```go
package main

import (
"fmt"
"github.com/zyedidia/generic/trie"
)

func main() {
tr := trie.New[int]()
tr.Put("foo", 1)
tr.Put("fo", 2)
tr.Put("bar", 3)

fmt.Println(tr.Contains("f"))
fmt.Println(tr.KeysWithPrefix(""))
fmt.Println(tr.KeysWithPrefix("f"))
}
```

#### Output

```
false
[bar fo foo]
[fo foo]
```

</p>
</details>

## Index

- [type Trie](<#type-trie>)
- [func New[V any]() *Trie[V]](<#func-new>)
- [func (t *Trie[V]) Contains(key string) bool](<#func-badrecv-contains>)
- [func (t *Trie[V]) Get(key string) (v V, ok bool)](<#func-badrecv-get>)
- [func (t *Trie[V]) Keys() (queue []string)](<#func-badrecv-keys>)
- [func (t *Trie[V]) KeysWithPrefix(prefix string) (queue []string)](<#func-badrecv-keyswithprefix>)
- [func (t *Trie[V]) LongestPrefix(query string) string](<#func-badrecv-longestprefix>)
- [func (t *Trie[V]) Put(key string, val V)](<#func-badrecv-put>)
- [func (t *Trie[V]) Remove(key string)](<#func-badrecv-remove>)
- [func (t *Trie[V]) Size() int](<#func-badrecv-size>)


## type Trie

A Trie is a data structure that supports common prefix operations\.

```go
type Trie[V any] struct {
// contains filtered or unexported fields
}
```

### func New

```go
func New[V any]() *Trie[V]
```

New returns an empty trie\.

### func \(\*BADRECV\) Contains

```go
func (t *Trie[V]) Contains(key string) bool
```

Contains returns whether this trie contains 'key'\.

### func \(\*BADRECV\) Get

```go
func (t *Trie[V]) Get(key string) (v V, ok bool)
```

Get returns the value associated with 'key'\.

### func \(\*BADRECV\) Keys

```go
func (t *Trie[V]) Keys() (queue []string)
```

Keys returns all keys in the trie\.

### func \(\*BADRECV\) KeysWithPrefix

```go
func (t *Trie[V]) KeysWithPrefix(prefix string) (queue []string)
```

KeysWithPrefix returns all keys with prefix 'prefix'\.

### func \(\*BADRECV\) LongestPrefix

```go
func (t *Trie[V]) LongestPrefix(query string) string
```

LongestPrefix returns the key that is the longest prefix of 'query'\.

### func \(\*BADRECV\) Put

```go
func (t *Trie[V]) Put(key string, val V)
```

Put associates 'val' with 'key'\.

### func \(\*BADRECV\) Remove

```go
func (t *Trie[V]) Remove(key string)
```

Remove removes the value associated with 'key'\.

### func \(\*BADRECV\) Size

```go
func (t *Trie[V]) Size() int
```

Size returns the size of the trie\.



Generated by [gomarkdoc](<https://github.com/princjef/gomarkdoc>)
Loading

0 comments on commit 330592a

Please sign in to comment.