Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ancientlore committed Feb 27, 2018
1 parent 4ba4b94 commit b2eb526
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 47 deletions.
10 changes: 4 additions & 6 deletions objecttree.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package avltree

import ()

// ObjectTree is a specialization of Tree that hides the wrapping of Elements around objects.
// The object just needs to implement Interface
// The object just needs to implement Interface.
type ObjectTree struct {
Tree
}

// Implement Interface so that your object can be sorted in the tree
// Interface should be implemented so that your object can be sorted in the tree.
type Interface interface {
// Return -1 if this < b, 0 if this == b, and 1 if this > b
Compare(b Interface) int
Expand All @@ -18,13 +16,13 @@ func objectCompare(o1 interface{}, o2 interface{}) int {
return o1.(Interface).Compare(o2.(Interface))
}

// Initialize or reset an ObjectTree
// Init will initialize or reset an ObjectTree.
func (t *ObjectTree) Init(flags byte) *ObjectTree {
t.Tree.Init(objectCompare, flags)
return t
}

// Return an initialized ObjectTree
// NewObjectTree returns an initialized ObjectTree.
func NewObjectTree(flags byte) *ObjectTree { return new(ObjectTree).Init(flags) }

// Find returns the element where the comparison function matches
Expand Down
4 changes: 2 additions & 2 deletions objecttree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func TestObjectTree(t *testing.T) {
tree.Do(func(z interface{}) bool { x += z.(MyObject).Key; return true })

if x != "barfoofoo" {
t.Errorf("Do function did not concat values correctly, expected barfoofoo: %d\n", x)
t.Errorf("Do function did not concat values correctly, expected barfoofoo: %s\n", x)
}

// test Remove
Expand Down Expand Up @@ -238,7 +238,7 @@ func TestObjectTree(t *testing.T) {
var cur string
cur = elem.(MyObject).Key
if prev > cur {
t.Errorf("Elements not in order, previous = %d, current = %d\n", prev, cur)
t.Errorf("Elements not in order, previous = %s, current = %s\n", prev, cur)
}
prev = cur
return true
Expand Down
20 changes: 10 additions & 10 deletions pairtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ type PairTree struct {
ObjectTree
}

// Pair structure holds your key and value
// Pair combines a key and value.
type Pair struct {
Key string
Value interface{}
}

// compare function for Pairs
// Compare is the compare function for Pairs, based on Key.
func (a Pair) Compare(b Interface) int {
if a.Key < b.(Pair).Key {
return -1
Expand All @@ -25,19 +25,19 @@ func (a Pair) Compare(b Interface) int {
return 0
}

// Iterate function
// PairIterateFunc is the type of function used for iterating across Pairs.
type PairIterateFunc func(v Pair) bool

// Initialize or reset a StringTree
// Init will initialize or reset a PairTree.
func (t *PairTree) Init(flags byte) *PairTree {
t.ObjectTree.Init(flags)
return t
}

// Return an initialized StringTree
// NewPairTree returns an initialized PairTree.
func NewPairTree(flags byte) *PairTree { return new(PairTree).Init(flags) }

// At returns the value at the given index
// At returns the value at the given index.
func (t *PairTree) At(index int) *Pair {
v := t.ObjectTree.At(index)
if v != nil {
Expand All @@ -48,7 +48,7 @@ func (t *PairTree) At(index int) *Pair {
}

// Find returns the element where the comparison function matches
// the node's value and the given key value
// the node's value and the given key value.
func (t *PairTree) Find(key string) *Pair {
v := t.ObjectTree.Find(Pair{key, nil})
if v != nil {
Expand All @@ -71,7 +71,7 @@ func (t *PairTree) chanIterate(c chan<- Pair) {
close(c)
}

// Iter returns a channel you can read through to fetch all the items
// Iter returns a channel you can read through to fetch all the items.
func (t *PairTree) Iter() <-chan Pair {
c := make(chan Pair)
go t.chanIterate(c)
Expand Down Expand Up @@ -114,7 +114,7 @@ func (t *PairTree) Remove(ptr string) *Pair {
return nil
}

// Remove removes the element at the given index
// RemoveAt removes the element at the given index.
func (t *PairTree) RemoveAt(index int) *Pair {
v := t.ObjectTree.RemoveAt(index)
if v != nil {
Expand All @@ -124,7 +124,7 @@ func (t *PairTree) RemoveAt(index int) *Pair {
return nil
}

// Print the values in the tree
// Print prints the values in the tree to the writer.
func (t *PairTree) Print(w io.Writer, f PairIterateFunc, itemSiz int) {
t.ObjectTree.Print(w, func(v interface{}) bool { return f(v.(Pair)) }, itemSiz)
}
4 changes: 2 additions & 2 deletions pairtree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func TestPairTree(t *testing.T) {
tree.Do(func(z Pair) bool { x += z.Key; return true })

if x != "barfoofoo" {
t.Errorf("Do function did not concat values correctly, expected barfoofoo: %d\n", x)
t.Errorf("Do function did not concat values correctly, expected barfoofoo: %s\n", x)
}

// test Remove
Expand Down Expand Up @@ -223,7 +223,7 @@ func TestPairTree(t *testing.T) {
var cur string
cur = elem.Key
if prev > cur {
t.Errorf("Elements not in order, previous = %d, current = %d\n", prev, cur)
t.Errorf("Elements not in order, previous = %s, current = %s\n", prev, cur)
}
prev = cur
return true
Expand Down
15 changes: 8 additions & 7 deletions stringtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ func stringCompare(s1 interface{}, s2 interface{}) int {
return 0
}

// Iterate function
// StringIterateFunc defines the function type used when iterating a StringTree.
type StringIterateFunc func(v string) bool

// Initialize or reset a StringTree
// Init will initialize or reset a StringTree.
func (t *StringTree) Init(flags byte) *StringTree {
t.Tree.Init(stringCompare, flags)
return t
}

// Return an initialized StringTree
// NewStringTree returns an initialized StringTree.
func NewStringTree(flags byte) *StringTree { return new(StringTree).Init(flags) }

// At returns the value at the given index
// At returns the value at the given index.
func (t *StringTree) At(index int) string {
v := t.Tree.At(index)
if v != nil {
Expand All @@ -40,7 +40,7 @@ func (t *StringTree) At(index int) string {
}

// Find returns the element where the comparison function matches
// the node's value and the given key value
// the node's value and the given key value.
func (t *StringTree) Find(key string) string {
v := t.Tree.Find(key)
if v != nil {
Expand All @@ -62,7 +62,7 @@ func (t *StringTree) chanIterate(c chan<- string) {
close(c)
}

// Iter returns a channel you can read through to fetch all the items
// Iter returns a channel you can read through to fetch all the items.
func (t *StringTree) Iter() <-chan string {
c := make(chan string)
go t.chanIterate(c)
Expand Down Expand Up @@ -103,7 +103,7 @@ func (t *StringTree) Remove(ptr string) string {
return ""
}

// Remove removes the element at the given index
// RemoveAt removes the element at the given index.
func (t *StringTree) RemoveAt(index int) string {
v := t.Tree.RemoveAt(index)
if v != nil {
Expand All @@ -112,6 +112,7 @@ func (t *StringTree) RemoveAt(index int) string {
return ""
}

// Print prints the values of the StringTree to the given writer.
func (t *StringTree) Print(w io.Writer, f StringIterateFunc, itemSiz int) {
t.Tree.Print(w, func(v interface{}) bool { return f(v.(string)) }, itemSiz)
}
8 changes: 4 additions & 4 deletions stringtree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func TestStringTree(t *testing.T) {
// test Find
var st string
if st != "" {
t.Errorf("Init string != empty string\n", st)
t.Errorf("Init string != empty string: %s\n", st)
}

v = tree.Find("15")
Expand Down Expand Up @@ -153,7 +153,7 @@ func TestStringTree(t *testing.T) {
tree.Do(func(z string) bool { x += z; return true })

if x != "141415" {
t.Errorf("Do function did not concatvalues correctly, expected 141415: %d\n", x)
t.Errorf("Do function did not concatvalues correctly, expected 141415: %s\n", x)
}

// test Remove
Expand Down Expand Up @@ -186,7 +186,7 @@ func TestStringTree(t *testing.T) {
}

if x != "20" {
t.Errorf("Iter ran wrong number of elements, expected last element of 20, got %d\n", x)
t.Errorf("Iter ran wrong number of elements, expected last element of 20, got %s\n", x)
}

// test clear
Expand Down Expand Up @@ -228,7 +228,7 @@ func TestStringTree(t *testing.T) {
var cur string
cur = elem
if prev > cur {
t.Errorf("Elements not in order, previous = %d, current = %d\n", prev, cur)
t.Errorf("Elements not in order, previous = %s, current = %s\n", prev, cur)
}
prev = cur
return true
Expand Down
33 changes: 18 additions & 15 deletions tree.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/*
Package avltree implements a height-balanced binary tree
with array-like indexing capability.
An AVL tree (Adel'son-Vel'skii & Landis) is a binary search
tree in which the heights of the left and right subtrees
of the root differ by at most one and in which the left
Expand Down Expand Up @@ -34,13 +37,13 @@ const (
AllowDuplicates = 1
)

// Definition of a comparison function
// CompareFunc defines the function type used to compare values.
type CompareFunc func(v1 interface{}, v2 interface{}) int

// Iterate function
// IterateFunc defines the function type used for iterating a tree.
type IterateFunc func(v interface{}) bool

// Tree object
// Tree stores data about the binary tree.
type Tree struct {
// root of the tree
root *treeNode
Expand All @@ -52,19 +55,19 @@ type Tree struct {
treeFlags byte
}

// Initialize or reset a Tree
// Init initializes or resets a Tree.
func (t *Tree) Init(c CompareFunc, flags byte) *Tree {
t.compare = c
t.root = nil
t.treeFlags = flags
return t
}

// Return an initialized tree
// New returns an initialized tree.
func New(c CompareFunc, flags byte) *Tree { return new(Tree).Init(c, flags) }

// Clear removes all elements from the tree, keeping the
// current options and compare function
// current options and compare function.
func (t *Tree) Clear() { t.Init(t.compare, t.treeFlags) }

// calcHeightData contains information needed to compute the
Expand Down Expand Up @@ -105,7 +108,7 @@ func (t *Tree) Height() int {
return d.maxHeight
}

// Len returns the number of elements in the tree
// Len returns the number of elements in the tree.
func (t *Tree) Len() int {
if t.root != nil {
return t.root.size
Expand All @@ -132,7 +135,7 @@ func (t *Tree) Cap() int {
}

// indexer recursively scans the tree to find the node
// at the given position
// at the given position.
func indexer(node *treeNode, index int) *treeNode {

if index < node.leftSize() {
Expand All @@ -145,7 +148,7 @@ func indexer(node *treeNode, index int) *treeNode {
return nil
}

// At returns the value at the given index
// At returns the value at the given index.
func (t *Tree) At(index int) interface{} {

if t.root != nil && index < t.root.size && index >= 0 {
Expand All @@ -158,14 +161,14 @@ func (t *Tree) At(index int) interface{} {
return nil
}

// findData is used when searching the tree
// findData is used when searching the tree.
type findData struct {
lookingFor interface{}
compare CompareFunc
}

// finder recursively scans the tree to find the node with the
// value we're looking for
// value we're looking for.
func (d *findData) finder(node *treeNode) *treeNode {

if node != nil {
Expand All @@ -181,7 +184,7 @@ func (d *findData) finder(node *treeNode) *treeNode {
}

// Find returns the element where the comparison function matches
// the node's value and the given key value
// the node's value and the given key value.
func (t *Tree) Find(key interface{}) interface{} {
if key != nil && t.root != nil {
d := &findData{key, t.compare}
Expand All @@ -194,13 +197,13 @@ func (t *Tree) Find(key interface{}) interface{} {
return nil
}

// iterData is used when iterating the tree
// iterData is used when iterating the tree.
type iterData struct {
iter IterateFunc
}

// iterate recursively traverses the tree and executes
// the iteration function
// the iteration function.
func (d *iterData) iterate(node *treeNode) bool {
var proceed bool

Expand Down Expand Up @@ -243,7 +246,7 @@ func (t *Tree) chanIterate(c chan<- interface{}) {
close(c)
}

// Iter returns a channel you can read through to fetch all the items
// Iter returns a channel you can read through to fetch all the items.
func (t *Tree) Iter() <-chan interface{} {
c := make(chan interface{})
go t.chanIterate(c)
Expand Down
1 change: 1 addition & 0 deletions treeprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func (d *printData) printer(node *treeNode) {
d.lineDepth -= (d.itemSize + 5)
}

// Print prints the values of the Tree to the given writer.
func (t *Tree) Print(w io.Writer, f IterateFunc, itemSiz int) {

fmt.Fprintf(w, "treeNode-+-Left \t / Left High\n")
Expand Down
2 changes: 1 addition & 1 deletion treeremove.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func remove(node **treeNode, index int, shorter *bool) interface{} {
return ptr
}

// Remove removes the element at the given index
// RemoveAt removes the element at the given index.
func (t *Tree) RemoveAt(index int) interface{} {
if t.root != nil && index < t.root.size && index >= 0 {
var shorter bool
Expand Down

0 comments on commit b2eb526

Please sign in to comment.