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 }
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() }