Skip to content

Commit

Permalink
Merge pull request #3 from badgerodon/rewrite
Browse files Browse the repository at this point in the history
complete rewrite
  • Loading branch information
calebdoxsey authored Mar 21, 2022
2 parents 604e922 + 1a974f7 commit f0935ab
Show file tree
Hide file tree
Showing 32 changed files with 741 additions and 1,792 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Test
on:
push:
tags:
- v*
branches:
- main
pull_request:
jobs:
test:
name: Go
runs-on: ubuntu-latest
steps:
- name: install-go
uses: actions/setup-go@v3
with:
go-version: "1.18"

- name: checkout
uses: actions/checkout@v3

- name: mod-verify
run: go mod verify

- name: vet
run: go vet ./...

- name: test
run: go test -race -covermode atomic -coverprofile=covprofile ./...

- name: install-goveralls
run: go install github.com/mattn/goveralls@latest

- name: run-goveralls
env:
COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: goveralls -coverprofile=covprofile -service=github
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea/
.vscode/
20 changes: 0 additions & 20 deletions LICENSE

This file was deleted.

32 changes: 8 additions & 24 deletions Readme.md
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
24 changes: 24 additions & 0 deletions UNLICENSE
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>
106 changes: 106 additions & 0 deletions btree.go
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()
}
26 changes: 26 additions & 0 deletions btree_test.go
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)
}
28 changes: 28 additions & 0 deletions collection.go
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()
},
}
}
Loading

0 comments on commit f0935ab

Please sign in to comment.