move rbtree to types package

This commit is contained in:
c9s 2021-05-21 01:44:50 +08:00
parent edf8902b28
commit be646fbac2
6 changed files with 25 additions and 24 deletions

View File

@ -1,4 +1,5 @@
package types package types
const Green = "#228B22" const GreenColor = "#228B22"
const Red = "#800000" const RedColor = "#800000"
const GrayColor = "#f0f0f0"

View File

@ -177,11 +177,11 @@ func (k KLine) String() string {
func (k KLine) Color() string { func (k KLine) Color() string {
if k.Direction() > 0 { if k.Direction() > 0 {
return Green return GreenColor
} else if k.Direction() < 0 { } else if k.Direction() < 0 {
return Red return RedColor
} }
return "#f0f0f0" return GrayColor
} }
func (k KLine) SlackAttachment() slack.Attachment { func (k KLine) SlackAttachment() slack.Attachment {

View File

@ -1,27 +1,27 @@
package rbtbook package types
import ( import (
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
) )
type Tree struct { type RBTree struct {
Root, Neel *Node Root, Neel *Node
} }
func NewTree() *Tree { func NewRBTree() *RBTree {
var neel = &Node{ var neel = &Node{
Color: Black, Color: Black,
} }
var root = neel var root = neel
root.Parent = neel root.Parent = neel
return &Tree{ return &RBTree{
Root: root, Root: root,
Neel: neel, Neel: neel,
} }
} }
func (tree *Tree) Delete(key fixedpoint.Value) bool { func (tree *RBTree) Delete(key fixedpoint.Value) bool {
var del = tree.Search(key) var del = tree.Search(key)
if del == nil { if del == nil {
return false return false
@ -64,7 +64,7 @@ func (tree *Tree) Delete(key fixedpoint.Value) bool {
return true return true
} }
func (tree *Tree) DeleteFixup(current *Node) { func (tree *RBTree) DeleteFixup(current *Node) {
for current != tree.Root && current.Color == Black { for current != tree.Root && current.Color == Black {
if current == current.Parent.Left { if current == current.Parent.Left {
sibling := current.Parent.Right sibling := current.Parent.Right
@ -128,7 +128,7 @@ func (tree *Tree) DeleteFixup(current *Node) {
current.Color = Black current.Color = Black
} }
func (tree *Tree) Insert(key, val fixedpoint.Value) { func (tree *RBTree) Insert(key, val fixedpoint.Value) {
var y = tree.Neel var y = tree.Neel
var x = tree.Root var x = tree.Root
var node = &Node{ var node = &Node{
@ -164,7 +164,7 @@ func (tree *Tree) Insert(key, val fixedpoint.Value) {
tree.InsertFixup(node) tree.InsertFixup(node)
} }
func (tree *Tree) Search(key fixedpoint.Value) *Node { func (tree *RBTree) Search(key fixedpoint.Value) *Node {
var current = tree.Root var current = tree.Root
for current != nil && key != current.Key { for current != nil && key != current.Key {
if key < current.Key { if key < current.Key {
@ -176,7 +176,7 @@ func (tree *Tree) Search(key fixedpoint.Value) *Node {
return current return current
} }
func (tree *Tree) InsertFixup(current *Node) { func (tree *RBTree) InsertFixup(current *Node) {
// A red node can't have a red parent, we need to fix it up // A red node can't have a red parent, we need to fix it up
for current.Parent.Color == Red { for current.Parent.Color == Red {
grandParent := current.Parent.Parent grandParent := current.Parent.Parent
@ -227,7 +227,7 @@ func (tree *Tree) InsertFixup(current *Node) {
// 1. move y's left child to the x's right child // 1. move y's left child to the x's right child
// 2. change y's parent to x's parent // 2. change y's parent to x's parent
// 3. change x's parent to y // 3. change x's parent to y
func (tree *Tree) RotateLeft(x *Node) { func (tree *RBTree) RotateLeft(x *Node) {
var y = x.Right var y = x.Right
x.Right = y.Left x.Right = y.Left
@ -249,7 +249,7 @@ func (tree *Tree) RotateLeft(x *Node) {
x.Parent = y x.Parent = y
} }
func (tree *Tree) RotateRight(y *Node) { func (tree *RBTree) RotateRight(y *Node) {
x := y.Left x := y.Left
y.Left = x.Right y.Left = x.Right
@ -271,7 +271,7 @@ func (tree *Tree) RotateRight(y *Node) {
y.Parent = x y.Parent = x
} }
func (tree *Tree) LeftMost(current *Node) *Node { func (tree *RBTree) LeftMost(current *Node) *Node {
for current.Left != nil { for current.Left != nil {
current = current.Left current = current.Left
} }
@ -279,7 +279,7 @@ func (tree *Tree) LeftMost(current *Node) *Node {
return current return current
} }
func (tree *Tree) Successor(current *Node) *Node { func (tree *RBTree) Successor(current *Node) *Node {
if current.Right != nil { if current.Right != nil {
return tree.LeftMost(current.Right) return tree.LeftMost(current.Right)
} }

View File

@ -1,4 +1,4 @@
package rbtbook package types
import "github.com/c9s/bbgo/pkg/fixedpoint" import "github.com/c9s/bbgo/pkg/fixedpoint"

View File

@ -1,4 +1,4 @@
package rbtbook package types
import ( import (
"testing" "testing"
@ -8,7 +8,7 @@ import (
) )
func TestTree(t *testing.T) { func TestTree(t *testing.T) {
tree := NewTree() tree := NewRBTree()
tree.Insert(fixedpoint.NewFromFloat(3000.0), fixedpoint.NewFromFloat(10.0)) tree.Insert(fixedpoint.NewFromFloat(3000.0), fixedpoint.NewFromFloat(10.0))
assert.NotNil(t, tree.Root) assert.NotNil(t, tree.Root)

View File

@ -74,14 +74,14 @@ func (side SideType) String() string {
func (side SideType) Color() string { func (side SideType) Color() string {
if side == SideTypeBuy { if side == SideTypeBuy {
return Green return GreenColor
} }
if side == SideTypeSell { if side == SideTypeSell {
return Red return RedColor
} }
return "#f0f0f0" return GrayColor
} }
func SideToColorName(side SideType) string { func SideToColorName(side SideType) string {