package main import ( "fmt" "github.com/wangjia184/sortedset" "git.wecise.com/wecise/common/alg/ca" "math" ) type SimpleClusterable struct { position float64 } func (s SimpleClusterable) Distance(c interface{}) float64 { distance := math.Abs(c.(SimpleClusterable).position - s.position) return distance } func (s SimpleClusterable) String() string { return fmt.Sprint( s.position ) } func main() { clusterList := sortedset.New() clusterList.AddOrUpdate("1", 10, SimpleClusterable{10} ) clusterList.AddOrUpdate("2", 5, SimpleClusterable{5} ) clusterList.AddOrUpdate("3", 42, SimpleClusterable{42} ) clusterList.AddOrUpdate("4", 0, SimpleClusterable{0} ) clusterList.AddOrUpdate("5", 50, SimpleClusterable{50} ) clusterList.AddOrUpdate("6", 40, SimpleClusterable{40} ) clusterList.AddOrUpdate("7", 41, SimpleClusterable{41} ) clusterList.AddOrUpdate("8", 102, SimpleClusterable{102} ) clusterList.AddOrUpdate("9", 43, SimpleClusterable{43} ) clusterList.AddOrUpdate("10", 200, SimpleClusterable{200} ) clusterList.AddOrUpdate("11", 40, SimpleClusterable{40} ) clusterList.AddOrUpdate("12", 40, SimpleClusterable{40} ) clusterList.AddOrUpdate("13", 101, SimpleClusterable{101} ) clusterList.AddOrUpdate("14", 45, SimpleClusterable{45} ) clusterList.AddOrUpdate("15", 103, SimpleClusterable{103} ) clusterList.AddOrUpdate("16", 200, SimpleClusterable{200} ) clusterList.AddOrUpdate("17", 400, SimpleClusterable{400} ) clusterList.AddOrUpdate("18", 200, SimpleClusterable{200} ) clusterList.AddOrUpdate("19", 53, SimpleClusterable{53} ) clusterList.AddOrUpdate("20", 63, SimpleClusterable{63} ) alg := ca.New(2, 10, true) clusters := alg.Clusterize(clusterList) fmt.Println("total cluster len==>", len(clusters)) for _, cluster := range clusters { nodes := cluster.GetByRankRange(-1, 1, false) fmt.Printf(" cluster len %d : ==>\n", cluster.GetCount()) for i := 0; i < len(nodes); i++ { fmt.Printf(" %s - %s\n", nodes[i].Value, nodes[i].Key) //fmt.Print(reflect.TypeOf(nodes[i].Value)) } } }