Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix golint #33 #34

Merged
merged 1 commit into from
Feb 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions example/example_int/example_int.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main

import (
"fmt"

"github.com/HuKeping/rbtree"
)

Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions example/example_string/example_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main

import (
"fmt"

"github.com/HuKeping/rbtree"
)

Expand All @@ -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
Expand Down
10 changes: 6 additions & 4 deletions example/example_struct/example_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion rbtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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{
Expand Down
8 changes: 7 additions & 1 deletion stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)

Expand All @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}