package tool import ( "testing" "github.com/wangjia184/sortedset" ) // https://github.com/wangjia184/sortedset func TestOrderedSet(t *testing.T) { // create a new set set := sortedset.New() // fill in new node set.AddOrUpdate("a", 89, "Kelly") set.AddOrUpdate("b", 100, "Staley") set.AddOrUpdate("c", 100, "Jordon") set.AddOrUpdate("d", -321, "Park") set.AddOrUpdate("e", 101, "Albert") set.AddOrUpdate("f", 99, "Lyman") set.AddOrUpdate("g", 99, "Singleton") set.AddOrUpdate("h", 70, "Audrey") // update an existing node by key set.AddOrUpdate("e", 99, "ntrnrt") // get the node by key set.GetByKey("b") // remove node by key set.Remove("b") // get the number of nodes in this set set.GetCount() // find the rank(postion) in the set. set.FindRank("d") // return 1 here // get and remove the node with minimum score set.PopMin() // get the node with maximum score set.PeekMax() // get the node at rank 1 (the node with minimum score) set.GetByRank(1, false) // get & remove the node at rank -1 (the node with maximum score) set.GetByRank(-1, true) // get the node with the 2nd highest maximum score set.GetByRank(-2, false) // get nodes with in rank range [1, -1], that is all nodes actually set.GetByRankRange(1, -1, false) // get & remove the 2nd/3rd nodes in reserve order set.GetByRankRange(-2, -3, true) // get the nodes whose score are within the interval [60,100] set.GetByScoreRange(60, 100, nil) // get the nodes whose score are within the interval (60,100] set.GetByScoreRange(60, 100, &sortedset.GetByScoreRangeOptions{ ExcludeStart: true, }) // get the nodes whose score are within the interval [60,100) set.GetByScoreRange(60, 100, &sortedset.GetByScoreRangeOptions{ ExcludeEnd: true, }) // get the nodes whose score are within the interval [60,100] in reverse order set.GetByScoreRange(100, 60, nil) // get the top 2 nodes with lowest scores within the interval [60,100] set.GetByScoreRange(60, 100, &sortedset.GetByScoreRangeOptions{ Limit: 2, }) // get the top 2 nodes with highest scores within the interval [60,100] set.GetByScoreRange(100, 60, &sortedset.GetByScoreRangeOptions{ Limit: 2, }) // get the top 2 nodes with highest scores within the interval (60,100) set.GetByScoreRange(100, 60, &sortedset.GetByScoreRangeOptions{ Limit: 2, ExcludeStart: true, ExcludeEnd: true, }) }