Beispiel #1
0
func (s *Sorted) Find(item types.Hashable) (int, bool, error) {
	var l int = 0
	var r int = s.Size() - 1
	var m int
	for l <= r {
		m = ((r - l) >> 1) + l
		im, err := s.list.Get(m)
		if err != nil {
			return -1, false, err
		}
		if item.Less(im) {
			r = m - 1
		} else if item.Equals(im) {
			for j := m; j > 0; j-- {
				ij_1, err := s.list.Get(j - 1)
				if err != nil {
					return -1, false, err
				}
				if !item.Equals(ij_1) {
					return j, true, nil
				}
			}
			return 0, true, nil
		} else {
			l = m + 1
		}
	}
	return l, false, nil
}
func (self *BpNode) find(key types.Hashable) (int, bool) {
	var l int = 0
	var r int = len(self.keys) - 1
	var m int
	for l <= r {
		m = ((r - l) >> 1) + l
		if key.Less(self.keys[m]) {
			r = m - 1
		} else if key.Equals(self.keys[m]) {
			for j := m; j >= 0; j-- {
				if j == 0 || !key.Equals(self.keys[j-1]) {
					return j, true
				}
			}
		} else {
			l = m + 1
		}
	}
	return l, false
}