init
This commit is contained in:
42
dtask/pkg/rbtree/example/example_int/example_int.go
Normal file
42
dtask/pkg/rbtree/example/example_int/example_int.go
Normal file
@@ -0,0 +1,42 @@
|
||||
// Copyright 2015, Hu Keping. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/oofpgDLD/dtask/pkg/rbtree"
|
||||
)
|
||||
|
||||
func main() {
|
||||
rbt := rbtree.New()
|
||||
|
||||
m := 0
|
||||
n := 10
|
||||
|
||||
for m < n {
|
||||
rbt.Insert(rbtree.Int(m))
|
||||
m++
|
||||
}
|
||||
|
||||
m = 0
|
||||
for m < n {
|
||||
if m%2 == 0 {
|
||||
rbt.Delete(rbtree.Int(m))
|
||||
}
|
||||
m++
|
||||
}
|
||||
|
||||
rbt.Ascend(rbt.Min(), print)
|
||||
}
|
||||
|
||||
func print(item rbtree.Item) bool {
|
||||
i, ok := item.(rbtree.Int)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
fmt.Println(i)
|
||||
return true
|
||||
}
|
||||
29
dtask/pkg/rbtree/example/example_string/example_string.go
Normal file
29
dtask/pkg/rbtree/example/example_string/example_string.go
Normal file
@@ -0,0 +1,29 @@
|
||||
// Copyright 2015, Hu Keping. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/oofpgDLD/dtask/pkg/rbtree"
|
||||
)
|
||||
|
||||
func main() {
|
||||
rbt := rbtree.New()
|
||||
|
||||
rbt.Insert(rbtree.String("Hello"))
|
||||
rbt.Insert(rbtree.String("World"))
|
||||
|
||||
rbt.Ascend(rbt.Min(), print)
|
||||
}
|
||||
|
||||
func print(item rbtree.Item) bool {
|
||||
i, ok := item.(rbtree.String)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
fmt.Println(i)
|
||||
return true
|
||||
}
|
||||
71
dtask/pkg/rbtree/example/example_struct/example_struct.go
Normal file
71
dtask/pkg/rbtree/example/example_struct/example_struct.go
Normal file
@@ -0,0 +1,71 @@
|
||||
// Copyright 2015, Hu Keping. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/oofpgDLD/dtask/pkg/rbtree"
|
||||
)
|
||||
|
||||
// Var is the node of a struct
|
||||
type Var struct {
|
||||
Expiry time.Time `json:"expiry,omitempty"`
|
||||
ID string `json:"id,omitempty"`
|
||||
}
|
||||
|
||||
// Less will order the node by `Time`
|
||||
func (x Var) Less(than rbtree.Item) bool {
|
||||
return x.Expiry.Before(than.(Var).Expiry)
|
||||
}
|
||||
|
||||
func main() {
|
||||
rbt := rbtree.New()
|
||||
|
||||
var1 := Var{
|
||||
Expiry: time.Now().Add(time.Second * 10),
|
||||
ID: "var1",
|
||||
}
|
||||
var2 := Var{
|
||||
Expiry: time.Now().Add(time.Second * 20),
|
||||
ID: "var2",
|
||||
}
|
||||
var3 := Var{
|
||||
Expiry: var2.Expiry,
|
||||
ID: "var2-dup",
|
||||
}
|
||||
var4 := Var{
|
||||
Expiry: time.Now().Add(time.Second * 40),
|
||||
ID: "var4",
|
||||
}
|
||||
var5 := Var{
|
||||
Expiry: time.Now().Add(time.Second * 50),
|
||||
ID: "var5",
|
||||
}
|
||||
|
||||
rbt.Insert(var1)
|
||||
rbt.Insert(var2)
|
||||
rbt.Insert(var3)
|
||||
rbt.Insert(var4)
|
||||
rbt.Insert(var5)
|
||||
|
||||
tmp := Var{
|
||||
Expiry: var4.Expiry,
|
||||
ID: "This field is not the key factor",
|
||||
}
|
||||
|
||||
// var4 and var5 were expected
|
||||
rbt.Ascend(rbt.Get(tmp), print)
|
||||
}
|
||||
|
||||
func print(item rbtree.Item) bool {
|
||||
i, ok := item.(Var)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
fmt.Printf("%+v\n", i)
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user