Beispiel #1
0
func (b *Branch) Find(key tree.Key) (interface{}, bool) {
	i := 0
	for ; i < b.numKeys; i++ {
		if key.Less(b.key[i]) {
			break
		}
	}
	return b.twig[i].Find(key)
}
Beispiel #2
0
func (b *Branch) Delete(key tree.Key) {
	i := 0
	for ; i < b.numKeys; i++ {
		if key.Less(b.key[i]) {
			break
		}
	}
	b.twig[i].Delete(key)
}
Beispiel #3
0
func (l *Leaf) Find(key tree.Key) (interface{}, bool) {
	for i := 0; i < l.numKeys; i++ {
		if key.Less(l.record[i].Key) {
			return nil, false
		}
		if !l.record[i].Key.Less(key) {
			return l.record[i].Value, true
		}
	}
	return nil, false
}
Beispiel #4
0
func (t *Tree) Check(key tree.Key) tree.Key {
	var next tree.Key
	for _, r := range t.rec {
		next = r.Key
		if next.Less(key) {
			log.Fatal("keys out of order")
		}
		key = next
	}
	return key
}
Beispiel #5
0
func (l *Leaf) Insert(key tree.Key, value interface{}) {
	var i int
	for i = 0; i < l.numKeys; i++ {
		if key.Less(l.record[i].Key) {
			for j := l.numKeys; j > i; j-- {
				l.record[j] = l.record[j-1]
			}
			break
		}
	}
	l.numKeys++
	l.record[i] = tree.Record{key, value}
}
Beispiel #6
0
func (l *Leaf) Delete(key tree.Key) {
	for i := 0; i < l.numKeys; i++ {
		if key == l.record[i].Key {
			l.numKeys--
			for j := i; j < l.numKeys; j++ {
				l.record[j] = l.record[j+1]
			}
		}
		if key.Less(l.record[i].Key) {
			return
		}
	}
}
Beispiel #7
0
func (t *Tree) Find(key tree.Key) (interface{}, bool) {
	for node := t.root; node != nil; {
		switch {
		case node.rec.Key.Less(key):
			node = node.right
		case key.Less(node.rec.Key):
			node = node.left
		default:
			return node.rec.Value, true
		}
	}
	return nil, false
}
Beispiel #8
0
func (b *Branch) Succ(key tree.Key) (tree.Record, bool) {
	i := 0
	for ; i < b.numKeys; i++ {
		if key.Less(b.key[i]) {
			break
		}
	}
	for ; i < b.numKeys; i++ {
		rec, found := b.twig[i].Succ(key)
		if found {
			return rec, found
		}
	}
	return b.twig[i].Succ(key)
}
Beispiel #9
0
func (t *Tree) bsearch(key tree.Key) (int, bool) {
	left := 0
	right := len(t.rec) - 1
	for left <= right {
		middle := (left + right) / 2
		if key.Less(t.rec[middle].Key) {
			right = middle - 1
		} else if key == t.rec[middle].Key {
			return middle, true
		} else {
			left = middle + 1
		}
	}
	return left, false
}
Beispiel #10
0
func (l *Leaf) Succ(key tree.Key) (tree.Record, bool) {
	for i := 0; i < l.numKeys; i++ {
		if key.Less(l.record[i].Key) {
			return l.record[i], true
		}
		if !l.record[i].Key.Less(key) {
			i++
			if i < l.numKeys {
				return l.record[i], true
			} else {
				return tree.Record{}, false
			}
		}
	}
	return tree.Record{}, false
}
Beispiel #11
0
func (r *Tree) Insert(key tree.Key, value interface{}) {
	if r.last != nil && key.Less(r.last) {
		log.Fatal("keys must be inserted in order in an mtree: last=",
			r.last, " key=", key)
		return // XXX should be an error return
	}
	r.last = key
	if r.nextLevel == 0 {
		r.level[0] = new(Leaf)
		r.nextLevel++
	}
	if !r.level[0].isFull() {
		r.level[0].Insert(key, value)
		return
	}
	var node Node
	node = new(Leaf)
	node.Insert(key, value)
	var i int
	for i = 1; i < r.nextLevel; i++ {
		r.level[i-1] = node
		branch := r.level[i]
		if branch.isFull() {
			branch = new(Branch)
			branch.append(key, node)
			node = branch
		} else {
			branch.append(key, node)
			return
		}
	}
	if i == MAX_LEVELS {
		log.Fatal("Out of levels", i)
	}
	branch := new(Branch)
	branch.append(key, r.level[i-1])
	branch.append(key, node)
	r.level[i-1] = node
	r.level[i] = branch
	r.nextLevel++
}
Beispiel #12
0
func (t *Tree) Succ(key tree.Key) (tree.Record, bool) {
	var next *Node
	for node := t.root; node != nil; {
		if node.rec.Key.Less(key) {
			node = node.right
		} else if key.Less(node.rec.Key) {
			next = node
			node = node.left
		} else {
			if node.right != nil {
				for next = node.right; next.left != nil; next = next.left {
				}
			}
			break
		}
	}
	if next == nil {
		return tree.Record{}, false
	}
	return next.rec, true
}
Beispiel #13
0
func (b *Branch) Insert(key tree.Key, value interface{}) {
	var i int
	for i = 0; i < b.numKeys; i++ {
		if key.Less(b.key[i]) {
			break
		}
	}
	twig := b.twig[i]
	if twig.isFull() {
		sibling, newKey := twig.split()
		for j := b.numKeys; j > i; j-- {
			b.key[j] = b.key[j-1]
			b.twig[j+1] = b.twig[j] // One more twig than keys
		}
		b.key[i] = newKey
		b.twig[i+1] = sibling
		b.numKeys++
		if newKey.Less(key) {
			twig = sibling
		}
	}
	twig.Insert(key, value)
}
Beispiel #14
0
func (t *Tree) Delete(key tree.Key) {
	np := &t.root
	for node := t.root; node != nil; node = *np {
		switch {
		case node.rec.Key.Less(key):
			np = &node.right
		case key.Less(node.rec.Key):
			np = &node.left
		default:
			switch {
			case node.left == nil:
				*np = node.right
			case node.right == nil:
				*np = node.left
			default:
				replacement := delRight(&node.left)
				replacement.left = node.left
				replacement.right = node.right
				*np = replacement
			}
			return
		}
	}
}