skl/skl.go
func (s *Skiplist) findNear(key []byte, less bool, allowEqual bool) (*node, bool) {
	x := s.head
	level := int(s.getHeight() - 1)
	for {
		// Assume x.key < key.
		next := s.getNext(x, level)
		if next == nil {
			// x.key < key < END OF LIST
			if level > 0 {
				// Can descend further to iterate closer to the end.
				level--
				continue
			}
			// Level=0. Cannot descend further. Let's return something that makes sense.
			if !less {
				return nil, false
			}
			// Try to return x. Make sure it is not a head node.
			if x == s.head {
				return nil, false
			}
			return x, false
		}

		nextKey := next.key(s.arena)
		cmp := y.CompareKeys(key, nextKey)
		if cmp > 0 {
			// x.key < next.key < key. We can continue to move right.
			x = next
			continue
		}
		if cmp == 0 {
			// x.key < key == next.key.
			if allowEqual {
				return next, true
			}
			if !less {
				// We want >, so go to base level to grab the next bigger note.
				return s.getNext(next, 0), false
			}
			// We want <. If not base level, we should go closer in the next level.
			if level > 0 {
				level--
				continue
			}
			// On base level. Return x.
			if x == s.head {
				return nil, false
			}
			return x, false
		}
		// cmp < 0. In other words, x.key < key < next.
		if level > 0 {
			level--
			continue
		}
		// At base level. Need to return something.
		if !less {
			return next, false
		}
		// Try to return x. Make sure it is not a head node.
		if x == s.head {
			return nil, false
		}
		return x, false
	}
}
func (s *Skiplist) findNear(key []byte, less bool, allowEqual bool) (*node, bool) {
	x := s.head
	level := int(s.getHeight() - 1)
	for {
		// Assume x.key < key.
		next := s.getNext(x, level)
		if next == nil {
			// x.key < key < END OF LIST
			if level > 0 {
				// Can descend further to iterate closer to the end.
				level--
				continue
			}
			// Level=0. Cannot descend further. Let's return something that makes sense.
			if !less {
				return nil, false
			}
			// Try to return x. Make sure it is not a head node.
			if x == s.head {
				return nil, false
			}
			return x, false
		}

		nextKey := next.key(s.arena)
		cmp := y.CompareKeys(key, nextKey)
		if cmp > 0 {
			// x.key < next.key < key. We can continue to move right.
			x = next
			continue
		}
		if cmp == 0 {
			// x.key < key == next.key.
			if allowEqual {
				return next, true
			}
			if !less {
				// We want >, so go to base level to grab the next bigger note.
				return s.getNext(next, 0), false
			}
			// We want <. If not base level, we should go closer in the next level.
			if level > 0 {
				level--
				continue
			}
			// On base level. Return x.
			if x == s.head {
				return nil, false
			}
			return x, false
		}
		// cmp < 0. In other words, x.key < key < next.
		if level > 0 {
			level--
			continue
		}
		// At base level. Need to return something.
		if !less {
			return next, false
		}
		// Try to return x. Make sure it is not a head node.
		if x == s.head {
			return nil, false
		}
		return x, false
	}
}
func ( s * Skiplist ) findNear ( key [ ] byte , less bool , allowEqual bool ) ( * node , bool ) { x := s . head level := int ( s . getHeight ( ) - 1 ) for { // Assume x.key < key. next := s . getNext ( x , level ) if next == nil { // x.key < key < END OF LIST if level > 0 { // Can descend further to iterate closer to the end. level -- continue } // Level=0. Cannot descend further. Let's return something that makes sense. if ! less { return nil , false } // Try to return x. Make sure it is not a head node. if x == s . head { return nil , false } return x , false } nextKey := next . key ( s . arena ) cmp := y . CompareKeys ( key , nextKey ) if cmp > 0 { // x.key < next.key < key. We can continue to move right. x = next continue } if cmp == 0 { // x.key < key == next.key. if allowEqual { return next , true } if ! less { // We want >, so go to base level to grab the next bigger note. return s . getNext ( next , 0 ) , false } // We want <. If not base level, we should go closer in the next level. if level > 0 { level -- continue } // On base level. Return x. if x == s . head { return nil , false } return x , false } // cmp < 0. In other words, x.key < key < next. if level > 0 { level -- continue } // At base level. Need to return something. if ! less { return next , false } // Try to return x. Make sure it is not a head node. if x == s . head { return nil , false } return x , false } }
// findNear finds the node near to key.
// If less=true, it finds rightmost node such that node.key < key (if allowEqual=false) or
// node.key <= key (if allowEqual=true).
// If less=false, it finds leftmost node such that node.key > key (if allowEqual=false) or
// node.key >= key (if allowEqual=true).
// Returns the node found. The bool returned is true if the node has key equal to given key.
findNear finds the node near to key . If less = true it finds rightmost node such that node . key < key ( if allowEqual = false ) or node . key < = key ( if allowEqual = true ) . If less = false it finds leftmost node such that node . key > key ( if allowEqual = false ) or node . key > = key ( if allowEqual = true ) . Returns the node found . The bool returned is true if the node has key equal to given key .