From e88501a68488bde15b371e7984b5f5efa68213b1 Mon Sep 17 00:00:00 2001 From: u5surf Date: Sat, 18 Jan 2020 23:22:05 +0900 Subject: [PATCH] Fix golint #33 --- example/example_int/example_int.go | 5 +++-- example/example_string/example_string.go | 5 +++-- example/example_struct/example_struct.go | 10 ++++++---- iterator.go | 12 ++++++++++++ rbtree.go | 8 +++++++- stats.go | 8 +++++++- util.go | 4 ++++ 7 files changed, 42 insertions(+), 10 deletions(-) diff --git a/example/example_int/example_int.go b/example/example_int/example_int.go index 5ebed3b..efd294f 100644 --- a/example/example_int/example_int.go +++ b/example/example_int/example_int.go @@ -6,6 +6,7 @@ package main import ( "fmt" + "github.com/HuKeping/rbtree" ) @@ -28,10 +29,10 @@ func main() { m++ } - rbt.Ascend(rbt.Min(), Print) + rbt.Ascend(rbt.Min(), print) } -func Print(item rbtree.Item) bool { +func print(item rbtree.Item) bool { i, ok := item.(rbtree.Int) if !ok { return false diff --git a/example/example_string/example_string.go b/example/example_string/example_string.go index f55f422..dee5341 100644 --- a/example/example_string/example_string.go +++ b/example/example_string/example_string.go @@ -6,6 +6,7 @@ package main import ( "fmt" + "github.com/HuKeping/rbtree" ) @@ -15,10 +16,10 @@ func main() { rbt.Insert(rbtree.String("Hello")) rbt.Insert(rbtree.String("World")) - rbt.Ascend(rbt.Min(), Print) + rbt.Ascend(rbt.Min(), print) } -func Print(item rbtree.Item) bool { +func print(item rbtree.Item) bool { i, ok := item.(rbtree.String) if !ok { return false diff --git a/example/example_struct/example_struct.go b/example/example_struct/example_struct.go index 62b2fff..6ce89c1 100644 --- a/example/example_struct/example_struct.go +++ b/example/example_struct/example_struct.go @@ -6,16 +6,18 @@ package main import ( "fmt" - "github.com/HuKeping/rbtree" "time" + + "github.com/HuKeping/rbtree" ) +// Var is the node of a struct type Var struct { Expiry time.Time `json:"expiry,omitempty"` ID string `json:"id,omitempty"` } -// We will order the node by `Time` +// Less will order the node by `Time` func (x Var) Less(than rbtree.Item) bool { return x.Expiry.Before(than.(Var).Expiry) } @@ -56,10 +58,10 @@ func main() { } // var4 and var5 were expected - rbt.Ascend(rbt.Get(tmp), Print) + rbt.Ascend(rbt.Get(tmp), print) } -func Print(item rbtree.Item) bool { +func print(item rbtree.Item) bool { i, ok := item.(Var) if !ok { return false diff --git a/iterator.go b/iterator.go index 35aeade..a4d77e6 100644 --- a/iterator.go +++ b/iterator.go @@ -4,6 +4,18 @@ package rbtree +// Iterator is the function of iteration entity which would be +// used by those functions like `Ascend`, `Dscend`, etc. +// +// A typical Iterator with Print : +// func loop_with_print(item rbtree.Item) bool { +// i, ok := item.(XXX) +// if !ok { +// return false +// } +// fmt.Printf("%+v\n", i) +// return true +// } type Iterator func(i Item) bool // Ascend will call iterator once for each element greater or equal than pivot diff --git a/rbtree.go b/rbtree.go index 66dff9f..bc77a38 100644 --- a/rbtree.go +++ b/rbtree.go @@ -15,6 +15,8 @@ package rbtree // 5) Every simple path from root to leaves contains the same number // of black nodes. // + +// Node of the rbtree has a pointer of the node of parent, left, right, also has own color and Item which client uses type Node struct { Left *Node Right *Node @@ -26,10 +28,13 @@ type Node struct { } const ( - RED = 0 + // RED represents the color of the node is red + RED = 0 + // BLACK represents the color of the node is black BLACK = 1 ) +// Item has a method to compare items which is less type Item interface { Less(than Item) bool } @@ -48,6 +53,7 @@ func less(x, y Item) bool { // New returns an initialized Red-Black tree func New() *Rbtree { return new(Rbtree).Init() } +// Init returns the initial of rbtree func (t *Rbtree) Init() *Rbtree { node := &Node{nil, nil, nil, BLACK, nil} return &Rbtree{ diff --git a/stats.go b/stats.go index c8333d4..64f9d66 100644 --- a/stats.go +++ b/stats.go @@ -8,9 +8,10 @@ package rbtree // by the user. Anyone who wants to look for some API about // the rbtree, this is the right place. -// Number of nodes in the tree. +// Len returns number of nodes in the tree. func (t *Rbtree) Len() uint { return t.count } +// Insert func inserts a item as a new RED node func (t *Rbtree) Insert(item Item) { if item == nil { return @@ -32,6 +33,7 @@ func (t *Rbtree) InsertOrGet(item Item) Item { return t.insert(&Node{t.NIL, t.NIL, t.NIL, RED, item}).Item } +//Delete delete the item in the tree func (t *Rbtree) Delete(item Item) Item { if item == nil { return nil @@ -41,6 +43,7 @@ func (t *Rbtree) Delete(item Item) Item { return t.delete(&Node{t.NIL, t.NIL, t.NIL, RED, item}).Item } +//Get search for the specified items which is carried by a Node func (t *Rbtree) Get(item Item) Item { if item == nil { return nil @@ -55,12 +58,14 @@ func (t *Rbtree) Get(item Item) Item { return ret.Item } +// Search does only search the node which includes it node //TODO: This is for debug, delete it in the future func (t *Rbtree) Search(item Item) *Node { return t.search(&Node{t.NIL, t.NIL, t.NIL, RED, item}) } +// Min return the item minimum one func (t *Rbtree) Min() Item { x := t.min(t.root) @@ -71,6 +76,7 @@ func (t *Rbtree) Min() Item { return x.Item } +// Max return the item maxmum one func (t *Rbtree) Max() Item { x := t.max(t.root) diff --git a/util.go b/util.go index 737f4af..dcb2e36 100644 --- a/util.go +++ b/util.go @@ -4,14 +4,18 @@ package rbtree +// Int is type of int type Int int +// Less returns whether x(Int) is smaller than specified item func (x Int) Less(than Item) bool { return x < than.(Int) } +// String is type of string type String string +// Less returns whether x(String) is smaller than specified item func (x String) Less(than Item) bool { return x < than.(String) }