Exemple #1
0
func (z *zset) Range(r1, r2 int) []string {
	if r1 < 1 {
		r1 = 1
	}
	if r2 < 1 {
		r2 = 1
	}
	var reverse, rangelen int
	if r1 <= r2 {
		reverse = 0
		rangelen = r2 - r1 + 1
	} else {
		reverse = 1
		rangelen = r1 - r2 + 1
	}
	node := C.slGetNodeByRank(z.sl, C.ulong(r1))
	result := make([]string, 0, rangelen)
	rr := C.int(reverse)
	for n := 0; node != nil && n < rangelen; {
		result = append(result, C.GoStringN(node.obj.ptr, C.int(node.obj.length)))
		node = C.getNextNode(node, rr)
		n++
	}
	return result
}
Exemple #2
0
func (z *zset) RangeByScore(s1, s2 float64) []string {
	var reverse int
	var node *C.skiplistNode
	cs1, cs2 := C.double(s1), C.double(s2)
	if s1 <= s2 {
		reverse = 0
		node = C.slFirstInRange(z.sl, cs1, cs2)
	} else {
		reverse = 1
		node = C.slLastInRange(z.sl, cs2, cs1)
	}

	result := make([]string, 0)
	rr := C.int(reverse)
	for node != nil {
		if reverse == 1 {
			if node.score < cs2 {
				break
			}
		} else {
			if node.score > cs2 {
				break
			}
		}
		result = append(result, C.GoStringN(node.obj.ptr, C.int(node.obj.length)))
		node = C.getNextNode(node, rr)
	}
	return result
}