Пример #1
0
func (self *TST) Get(key []byte) (value interface{}, err error) {
	type entry struct {
		n *TSTNode
		d int
	}
	if err := self.ValidateKey(key); err != nil {
		return nil, err
	}
	symbol := append(key, END)
	next := &entry{self.heads[symbol[0]], 1}
	for next != nil {
		if next.n == nil {
			return nil, errors.NotFound(key)
		} else if next.n.Internal() {
			ch := symbol[next.d]
			if ch < next.n.ch {
				next = &entry{next.n.l, next.d}
			} else if ch == next.n.ch {
				next = &entry{next.n.m, next.d + 1}
			} else if ch > next.n.ch {
				next = &entry{next.n.r, next.d}
			}
		} else if next.n.KeyEq(symbol) {
			return next.n.value, nil
		} else {
			return nil, errors.NotFound(key)
		}
	}
	// should never reach ...
	return nil, errors.NotFound(key)
}
Пример #2
0
func (self *TST) Remove(key []byte) (value interface{}, err error) {
	if err := self.ValidateKey(key); err != nil {
		return nil, err
	}
	symbol := ByteSlice(append(key, END))
	check := func(n *TSTNode, err error) (*TSTNode, error) {
		if err != nil {
			return nil, err
		} else if n == nil {
			return nil, nil
		} else if !n.Internal() && n.key == nil {
			return nil, nil
		}
		return n, nil
	}
	var remove func(n *TSTNode, d int) (*TSTNode, error)
	remove = func(n *TSTNode, d int) (*TSTNode, error) {
		if n == nil {
			return nil, errors.NotFound(key)
		} else if n.Internal() {
			n = n.Copy()
			ch := symbol[d]
			if ch < n.ch {
				l, err := check(remove(n.l, d))
				if err != nil {
					return nil, err
				}
				n.l = l
			} else if ch == n.ch {
				m, err := check(remove(n.m, d+1))
				if err != nil {
					return nil, err
				}
				n.m = m
			} else if ch > n.ch {
				r, err := check(remove(n.r, d))
				if err != nil {
					return nil, err
				}
				n.r = r
			}
		} else if n.key.Equals(symbol) {
			// found it
			value = n.value
			return nil, nil
		} else {
			return nil, errors.NotFound(key)
		}
		return n, nil
	}
	n, err := remove(self.heads[symbol[0]], 1)
	if err != nil {
		return nil, err
	}
	self.heads[symbol[0]] = n
	return value, nil
}
Пример #3
0
func (self *AvlNode) Remove(key types.Hashable) (_ *AvlNode, value interface{}, err error) {
	if self == nil {
		return nil, nil, errors.NotFound(key)
	}

	if self.key.Equals(key) {
		if self.left != nil && self.right != nil {
			if self.left.Size() < self.right.Size() {
				lmd := self.right.lmd()
				lmd.left = self.left
				return self.right, self.value, nil
			} else {
				rmd := self.left.rmd()
				rmd.right = self.right
				return self.left, self.value, nil
			}
		} else if self.left == nil {
			return self.right, self.value, nil
		} else if self.right == nil {
			return self.left, self.value, nil
		} else {
			return nil, self.value, nil
		}
	}
	if key.Less(self.key) {
		self.left, value, err = self.left.Remove(key)
	} else {
		self.right, value, err = self.right.Remove(key)
	}
	if err != nil {
		return self.balance(), value, err
	}
	return self, value, err
}
Пример #4
0
func (self *BpMap) Get(key types.Hashable) (value interface{}, err error) {
	j, l := self.root.get_start(key)
	if l.keys[j].Equals(key) {
		return l.values[j], nil
	}
	return nil, errors.NotFound(key)
}
Пример #5
0
func (self *AvlNode) Get(key types.Hashable) (value interface{}, err error) {
	if self == nil {
		return nil, errors.NotFound(key)
	}
	if self.key.Equals(key) {
		return self.value, nil
	} else if key.Less(self.key) {
		return self.left.Get(key)
	} else {
		return self.right.Get(key)
	}
}
Пример #6
0
func (self *ImmutableAvlNode) Remove(key types.Hashable) (_ *ImmutableAvlNode, value interface{}, err error) {
	if self == nil {
		return nil, nil, errors.NotFound(key)
	}

	if self.key.Equals(key) {
		if self.left != nil && self.right != nil {
			var new_root *ImmutableAvlNode
			if self.left.Size() < self.right.Size() {
				self, new_root = self.pop_node(self.right.lmd())
			} else {
				self, new_root = self.pop_node(self.left.rmd())
			}
			new_root.left = self.left
			new_root.right = self.right
			return new_root, self.value, nil
		} else if self.left == nil {
			return self.right, self.value, nil
		} else if self.right == nil {
			return self.left, self.value, nil
		} else {
			return nil, self.value, nil
		}
	}

	self = self.Copy()

	if key.Less(self.key) {
		self.left, value, err = self.left.Remove(key)
	} else {
		self.right, value, err = self.right.Remove(key)
	}
	self.height = max(self.left.Height(), self.right.Height()) + 1
	if err != nil {
		return self.balance(), value, err
	}
	return self, value, err
}