-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
604e922
commit 010ac6e
Showing
31 changed files
with
680 additions
and
1,792 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.idea/ | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,10 @@ | ||
# Badgerodon Collections | ||
|
||
Maps and slices go a long way in Go, but sometimes you need more. This is a collection of collections that may be useful. | ||
|
||
## Queue | ||
A [queue](http://en.wikipedia.org/wiki/Queue_(data_structure%29) is a first-in first-out data structure. | ||
|
||
## Set | ||
A [set](http://en.wikipedia.org/wiki/Set_(computer_science%29) is an unordered collection of unique values typically used for testing membership. | ||
|
||
## Skip list | ||
A [skip list](http://en.wikipedia.org/wiki/Skip_list) is a data structure that stores nodes in a hierarchy of linked lists. It gives performance similar to binary search trees by using a random number of forward links to skip parts of the list. | ||
|
||
## Splay Tree | ||
|
||
A [splay tree](http://en.wikipedia.org/wiki/Splay_tree) is a type of binary search tree where every access to the tree results in the tree being rearranged so that the current node gets put on top. | ||
|
||
## Stack | ||
A [stack](http://en.wikipedia.org/wiki/Stack_(abstract_data_type%29) is a last-in last-out data structure. | ||
|
||
## Trie | ||
A [trie](http://en.wikipedia.org/wiki/Trie) is a type of tree where each node represents one byte of a key. | ||
|
||
## Ternary Search Tree | ||
|
||
A [ternary search tree](http://en.wikipedia.org/wiki/Ternary_search_tree) is similar to a trie in that nodes store the letters of the key, but instead of either using a list or hash at each node a binary tree is used. Ternary search trees have the performance benefits of a trie without the usual memory costs. | ||
Useful collections: | ||
|
||
- Deque | ||
- List | ||
- Map | ||
- Queue | ||
- Set | ||
- Stack |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
This is free and unencumbered software released into the public domain. | ||
|
||
Anyone is free to copy, modify, publish, use, compile, sell, or | ||
distribute this software, either in source code form or as a compiled | ||
binary, for any purpose, commercial or non-commercial, and by any | ||
means. | ||
|
||
In jurisdictions that recognize copyright laws, the author or authors | ||
of this software dedicate any and all copyright interest in the | ||
software to the public domain. We make this dedication for the benefit | ||
of the public at large and to the detriment of our heirs and | ||
successors. We intend this dedication to be an overt act of | ||
relinquishment in perpetuity of all present and future rights to this | ||
software under copyright law. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | ||
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | ||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
For more information, please refer to <https://unlicense.org> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package collections | ||
|
||
import "github.com/tidwall/btree" | ||
|
||
type dictionaryViaBTree[TKey, TValue any] struct { | ||
b *btree.Generic[Pair[TKey, TValue]] | ||
less func(TKey, TKey) bool | ||
} | ||
|
||
var _ interface{ Dictionary[int, int] } = (*dictionaryViaBTree[int, int])(nil) | ||
|
||
func newDictionaryViaBTree[TKey, TValue any](less func(TKey, TKey) bool) *dictionaryViaBTree[TKey, TValue] { | ||
return &dictionaryViaBTree[TKey, TValue]{ | ||
b: btree.NewGeneric(func(a, b Pair[TKey, TValue]) bool { | ||
return less(a.First, b.First) | ||
}), | ||
less: less, | ||
} | ||
} | ||
|
||
func (d *dictionaryViaBTree[TKey, TValue]) Clear() { | ||
less := d.less | ||
d.b = btree.NewGeneric(func(a, b Pair[TKey, TValue]) bool { | ||
return less(a.First, b.First) | ||
}) | ||
d.less = less | ||
} | ||
|
||
func (d *dictionaryViaBTree[TKey, TValue]) Delete(key TKey) { | ||
var zero TValue | ||
d.b.Delete(NewPair(key, zero)) | ||
} | ||
|
||
func (d *dictionaryViaBTree[TKey, TValue]) ForEach(callback func(Pair[TKey, TValue]) bool) { | ||
d.b.Scan(func(item Pair[TKey, TValue]) bool { | ||
return callback(item) | ||
}) | ||
} | ||
|
||
func (d *dictionaryViaBTree[TKey, TValue]) Get(key TKey) (value TValue, ok bool) { | ||
item, ok := d.b.Get(NewPair(key, value)) | ||
if !ok { | ||
return value, ok | ||
} | ||
|
||
return item.Second, true | ||
} | ||
|
||
func (d *dictionaryViaBTree[TKey, TValue]) Keys() Collection[TKey] { | ||
return Map[Pair[TKey, TValue]](d, func(pair Pair[TKey, TValue]) TKey { | ||
return pair.First | ||
}) | ||
} | ||
|
||
func (d *dictionaryViaBTree[TKey, TValue]) Set(key TKey, value TValue) { | ||
d.b.Set(NewPair(key, value)) | ||
} | ||
|
||
func (d *dictionaryViaBTree[TKey, TValue]) Size() int { | ||
return d.b.Len() | ||
} | ||
|
||
func (d *dictionaryViaBTree[TKey, TValue]) Values() Collection[TValue] { | ||
return Map[Pair[TKey, TValue]](d, func(pair Pair[TKey, TValue]) TValue { | ||
return pair.Second | ||
}) | ||
} | ||
|
||
type setViaBTree[T any] struct { | ||
b *btree.Generic[T] | ||
less func(T, T) bool | ||
} | ||
|
||
var _ interface{ Set[int] } = (*setViaBTree[int])(nil) | ||
|
||
func newSetViaBTree[T any](less func(T, T) bool) *setViaBTree[T] { | ||
return &setViaBTree[T]{ | ||
b: btree.NewGeneric(less), | ||
less: less, | ||
} | ||
} | ||
|
||
func (s *setViaBTree[T]) Add(value T) { | ||
s.b.Set(value) | ||
} | ||
|
||
func (s *setViaBTree[T]) Clear() { | ||
s.b = btree.NewGeneric(s.less) | ||
} | ||
|
||
func (s *setViaBTree[T]) Delete(value T) { | ||
s.b.Delete(value) | ||
} | ||
|
||
func (s *setViaBTree[T]) ForEach(callback func(T) bool) { | ||
s.b.Scan(callback) | ||
} | ||
|
||
func (s *setViaBTree[T]) Has(value T) bool { | ||
_, ok := s.b.Get(value) | ||
return ok | ||
} | ||
|
||
func (s *setViaBTree[T]) Size() int { | ||
return s.b.Len() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package collections | ||
|
||
import ( | ||
"math/rand" | ||
"sort" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMapViaBTree(t *testing.T) { | ||
m := newDictionaryViaBTree[int, int](func(a, b int) bool { | ||
return a < b | ||
}) | ||
|
||
ordered := rand.Perm(50) | ||
sort.Ints(ordered) | ||
unordered := rand.Perm(50) | ||
|
||
for _, i := range unordered { | ||
m.Set(i, i) | ||
} | ||
|
||
s := NewSlice(m.Keys()) | ||
assert.Equal(t, ordered, s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package collections | ||
|
||
type collectionImpl[T any] struct { | ||
forEach func(callback func(T) bool) | ||
size func() int | ||
} | ||
|
||
func (c collectionImpl[T]) ForEach(callback func(T) bool) { | ||
c.forEach(callback) | ||
} | ||
|
||
func (c collectionImpl[T]) Size() int { | ||
return c.size() | ||
} | ||
|
||
// Map maps all the values in a collection to a new collection. | ||
func Map[TFrom, TTo any](c Collection[TFrom], f func(TFrom) TTo) Collection[TTo] { | ||
return collectionImpl[TTo]{ | ||
forEach: func(callback func(TTo) bool) { | ||
c.ForEach(func(v TFrom) bool { | ||
return callback(f(v)) | ||
}) | ||
}, | ||
size: func() int { | ||
return c.Size() | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,114 @@ | ||
package collections | ||
|
||
type ( | ||
Collection interface { | ||
Do(func(interface{})bool) | ||
} | ||
) | ||
|
||
func GetRange(c Collection, start, length int) []interface{} { | ||
end := start + length | ||
items := make([]interface{}, length) | ||
i := 0 | ||
j := 0 | ||
c.Do(func(item interface{})bool{ | ||
if i >= start { | ||
if i < end { | ||
items[j] = item | ||
j++ | ||
} else { | ||
return false | ||
} | ||
} | ||
i++ | ||
return true | ||
}) | ||
return items[:j] | ||
} | ||
package collections | ||
|
||
type ( | ||
Collection[T any] interface { | ||
ForEach(callback func(T) bool) | ||
Size() int | ||
} | ||
|
||
// A Deque is a double-ended queue. | ||
Deque[T any] interface { | ||
Clear() | ||
PeekBack() (value T, ok bool) | ||
PeekFront() (value T, ok bool) | ||
PopBack() (value T, ok bool) | ||
PopFront() (value T, ok bool) | ||
PushBack(value T) | ||
PushFront(value T) | ||
Size() int | ||
} | ||
|
||
// A Dictionary is a collection of key value pairs. | ||
Dictionary[TKey, TValue any] interface { | ||
Clear() | ||
Delete(key TKey) | ||
ForEach(callback func(Pair[TKey, TValue]) bool) | ||
Get(key TKey) (value TValue, ok bool) | ||
Keys() Collection[TKey] | ||
Set(key TKey, value TValue) | ||
Size() int | ||
Values() Collection[TValue] | ||
} | ||
|
||
// A Queue is a collection that supports FIFO operations. | ||
Queue[T any] interface { | ||
Clear() | ||
Peek() (value T, ok bool) | ||
Pop() (value T, ok bool) | ||
Push(value T) | ||
Size() int | ||
} | ||
|
||
// A Set is a unique collection of values. | ||
Set[T any] interface { | ||
Add(value T) | ||
Clear() | ||
Delete(value T) | ||
ForEach(callback func(T) bool) | ||
Has(value T) bool | ||
Size() int | ||
} | ||
|
||
// A Stack is a collection that supports LIFO operations. | ||
Stack[T any] interface { | ||
Clear() | ||
Peek() (value T, ok bool) | ||
Pop() (value T, ok bool) | ||
Push(value T) | ||
Size() int | ||
} | ||
) | ||
|
||
// NewDeque creates a new Deque implemented using a slice as a ring buffer. | ||
func NewDeque[T any]() Deque[T] { | ||
return newDequeViaRingSlice[T]() | ||
} | ||
|
||
// NewDictionary creates a new Dictionary implemented using a map. | ||
func NewDictionary[TKey comparable, TValue any]() Dictionary[TKey, TValue] { | ||
return newDictionaryViaMap[TKey, TValue]() | ||
} | ||
|
||
// NewQueue creates a new Queue implemented using a slice. | ||
func NewQueue[T any]() Queue[T] { | ||
return newQueueViaSlice[T]() | ||
} | ||
|
||
// NewSet creates a new Set implemented using a map. | ||
func NewSet[T comparable]() Set[T] { | ||
return newSetViaMap[T]() | ||
} | ||
|
||
// NewSlice creates a new slice from a collection. | ||
func NewSlice[T any](collection Collection[T]) []T { | ||
arr := make([]T, 0, collection.Size()) | ||
collection.ForEach(func(item T) bool { | ||
arr = append(arr, item) | ||
return true | ||
}) | ||
return arr | ||
} | ||
|
||
// NewSortedDictionary creates a new Dictionary implemented using a btree, providing sorted iteration by the less function. | ||
func NewSortedDictionary[TKey, TValue any](less func(TKey, TKey) bool) Dictionary[TKey, TValue] { | ||
return newDictionaryViaBTree[TKey, TValue](less) | ||
} | ||
|
||
// NewSortedSet creates a new Set implemented using a btree, providing sorted iteration by the less function. | ||
func NewSortedSet[T any](less func(T, T) bool) Set[T] { | ||
return newSetViaBTree(less) | ||
} | ||
|
||
// NewStack creates a new Stack implemented using a slice. | ||
func NewStack[T any]() Stack[T] { | ||
return newStackViaSlice[T]() | ||
} | ||
|
||
type integer interface { | ||
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | | ||
~int | ~int8 | ~int16 | ~int32 | ~int64 | ||
} | ||
|
||
func mod[T integer](x, y T) T { | ||
return (x%y + y) % y | ||
} |
Oops, something went wrong.