func (n *MemPrefixNode) insert(z *cf.Zp, marray []*cf.Zp, bs *cf.Bitstring, depth int) error { n.updateSvalues(z, marray) n.numElements++ if n.IsLeaf() { if len(n.elements) > n.SplitThreshold() { err := n.split(depth) if err != nil { return err } } else { for _, nz := range n.elements { if nz.Cmp(z) == 0 { return fmt.Errorf("duplicate: %q", z.String()) } } n.elements = append(n.elements, z) return nil } } childIndex := NextChild(n, bs, depth) children, err := n.Children() if err != nil { return err } child := children[childIndex].(*MemPrefixNode) return child.insert(z, marray, bs, depth+1) }
func WriteZp(w io.Writer, z *cf.Zp) (err error) { num := z.Bytes() _, err = w.Write(num) if err != nil { return } if len(num) < SksZpNbytes { pad := make([]byte, SksZpNbytes-len(num)) _, err = w.Write(pad) } return }
// Insert a Z/Zp integer into the prefix tree func (t *MemPrefixTree) Insert(z *cf.Zp) error { if t.allElements.Has(z) { return fmt.Errorf("duplicate: %q", z.String()) } bs := cf.NewZpBitstring(z) marray, err := AddElementArray(t, z) if err != nil { return err } err = t.root.insert(z, marray, bs, 0) if err != nil { return err } t.allElements.Add(z) return nil }
func (t *prefixTree) Remove(z *cf.Zp) error { _, lookupErr := t.db.Get(z.Bytes(), nil) if lookupErr != nil { return lookupErr } bs := cf.NewZpBitstring(z) root, err := t.Root() if err != nil { return err } marray := recon.DelElementArray(t, z) err = root.(*prefixNode).remove(z, marray, bs, 0) if err != nil { return err } return t.db.Delete(z.Bytes(), nil) }
func (n *prefixNode) deleteElement(element *cf.Zp) error { elementBytes := element.Bytes() var elements [][]byte var removed bool for _, element := range n.NodeElements { if bytes.Equal(element, elementBytes) { removed = true } else { elements = append(elements, element) } } if !removed { return ErrElementNotFound(element) } n.NodeElements = elements return n.upsertNode() }
func (t *prefixTree) Insert(z *cf.Zp) error { _, lookupErr := t.db.Get(z.Bytes(), nil) if lookupErr == nil { return ErrDuplicateElement(z) } else if lookupErr != leveldb.ErrNotFound { return lookupErr } bs := cf.NewZpBitstring(z) root, err := t.Root() if err != nil { return err } marray, err := recon.AddElementArray(t, z) if err != nil { return err } err = root.(*prefixNode).insert(z, marray, bs, 0) if err != nil { return err } return t.db.Put(z.Bytes(), []byte{}, nil) }
func (n *prefixNode) insertElement(element *cf.Zp) error { n.NodeElements = append(n.NodeElements, element.Bytes()) return n.upsertNode() }