// walkupDelete is similar to walkupInsert but is only done one at a time.
// This is because deletes can cause borrowing or merging with neighbors which makes
// the changes non-local.
// TODO: come up with a good way to parallelize this.
179182
func (t *Tr) walkupDelete(key *Key, node *Node, path *path, mapping map[string]*Node) error {
	needsMerged := t.config.NodeWidth / 2
	if needsMerged < 1 {
		needsMerged = 1
	}
	if node.lenValues() >= needsMerged {
		return nil
	}

	if string(node.ID) == string(t.Root) {
		if node.lenKeys() == 1 {
			id := node.keyAt(0)
			t.Root = id.UUID
		}

		return nil
	}

	var getSibling = func(parent *Node, i int) (*Node, error) {
		key := parent.keyAt(i)
		n, err := t.contextOrCachedNode(key.UUID, true)
		if err != nil {
			return nil, err
		}

		if !t.context.nodeExists(n.ID) {
			cp := t.copyNode(n)
			mapping[string(n.ID)] = cp
			parent.replaceKeyAt(&Key{UUID: cp.ID}, i)
			n = cp
		}

		return n, nil
	}

	parentBundle := path.pop()
	parent := mapping[string(parentBundle.n.ID)]

	_, i := parent.searchKey(t.config.Comparator, key.Value)
	siblingPosition := i
	if i == parent.lenValues() {
		siblingPosition--
	} else {
		siblingPosition++
	}

	sibling, err := getSibling(parent, siblingPosition)
	if err != nil {
		return err
	}

	prepend := false
	// thing are just easier if we make this swap so we can grok
	// left to right always assuming node is on the left and sibling
	// is on the right
	if siblingPosition < i {
		node, sibling = sibling, node
		prepend = true
	}

	// first case, can we just borrow?  if so, simply shift values from one node
	// to the other.  Once done, replace the parent value with the middle value
	// shifted and return.
	if (sibling.lenValues()+node.lenValues())/2 >= needsMerged {
		if i == parent.lenValues() {
			i--
		}

		var key *Key
		var value interface{}
		for node.lenValues() < needsMerged || sibling.lenValues() < needsMerged {
			if prepend {
				correctedValue, key := node.popValue(), node.popKey()
				if node.IsLeaf {
					sibling.prependValue(correctedValue)
					sibling.prependKey(key)
					parent.replaceValueAt(i, node.lastValue())
				} else {
					parentValue := parent.valueAt(i)
					sibling.prependKey(key)
					sibling.prependValue(parentValue)
					parent.replaceValueAt(i, correctedValue)
				}
			} else {
				value, key = sibling.popFirstValue(), sibling.popFirstKey()
				correctedValue := value
				if !node.IsLeaf {
					correctedValue = parent.valueAt(i)
				}
				node.appendValue(correctedValue)
				node.appendChild(key)
				parent.replaceValueAt(i, value)
			}
		}

		return nil
	}

	// the harder case, we need to merge with sibling, pull a value down
	// from the parent, and recurse on this function

	// easier case, merge the nodes and delete value and child from parent
	if node.IsLeaf {
		node.append(sibling)
		if prepend {
			parent.deleteKeyAt(i)
		} else {
			parent.deleteKeyAt(i + 1)
		}

		if i == parent.lenValues() {
			i--
		}

		parent.deleteValueAt(i)
		return t.walkupDelete(key, parent, path, mapping)
	}

	// harder case, need to pull a value down from the parent, insert
	// value into the left node, append the nodes, and then delete
	// the value from the parent

	valueIndex := i
	if i == parent.lenValues() {
		valueIndex--
	}

	parentValue := parent.valueAt(valueIndex)
	node.appendValue(parentValue)
	node.append(sibling)
	parent.deleteKeyAt(i)
	parent.deleteValueAt(valueIndex)
	parent.replaceKeyAt(&Key{UUID: node.ID}, valueIndex)
	return t.walkupDelete(key, parent, path, mapping)
}
982720
func (opts *ListOpts) Delete(key string) {
	for i, k := range *opts.values {
		if k == key {
			*opts.values = append((*opts.values)[:i], (*opts.values)[i+1:]...)
			return
		}
	}
}
1115491
func (lhu *LookupUnicodeLooseMD5HashUnique) Delete(vcursor VCursor, rowsColValues [][]sqltypes.Value, ksid []byte) error {
	v, err := vunhash(ksid)
	if err != nil {
		return fmt.Errorf("lookup.Delete.vunhash: %v", err)
	}
	rowsColValues, err = convertRows(rowsColValues)
	if err != nil {
		return fmt.Errorf("lookup.Delete.convert: %v", err)
	}
	return lhu.lkp.Delete(vcursor, rowsColValues, sqltypes.NewUint64(v))
}
1005211
func (sl *SkipList) Delete(comparators ...common.Comparator) common.Comparators {
	deleted := make(common.Comparators, 0, len(comparators))

	for _, cmp := range comparators {
		deleted = append(deleted, sl.delete(cmp))
	}

	return deleted
}
873077
func (r *AppService) Delete(id string) *AppDeleteCall {
	c := &AppDeleteCall{s: r.s, urlParams_: make(gensupport.URLParams)}
	c.id = id
	return c
}
630037
func (mem *memNamedMapping) Delete(name string) (value interface{}, found bool) {
	item, found := mem.removeNameIdxSync(name)
	if found {
		mem.publishDelToChannel(name, item.value)
		return item.value, found
	}
	return nil, false
}
443873
func (r *UpstreamService) Delete(id string) *UpstreamDeleteCall {
	c := &UpstreamDeleteCall{s: r.s, urlParams_: make(gensupport.URLParams)}
	c.id = id
	return c
}
1135519
func Delete(slice []interface{}, i int, n int) []interface{} {
	return append(slice[:i], slice[i+n:]...)
}
1083637
func (r *RollingUpdateInstanceGroup) Delete() error {
	if r.CloudGroup == nil {
		return fmt.Errorf("group has to be set")
	}
	// TODO: Leaving func in place in order to cordon nd drain nodes
	return r.Cloud.DeleteGroup(r.CloudGroup)
}
1300398
func (s *GenericStorage) Delete(key interface{}) error {
	if s.del == nil && s.delMulti == nil {
		return errors.Newf(
			"'%s' does not have Delete/DeleteMulti implementation",
			s.name)
	}

	s.rwMutex.Lock()
	defer s.rwMutex.Unlock()

	if s.del != nil {
		return s.del(key)
	}
	return s.delMulti(key)
}
1209591
func (vs *ValueMap) Delete(k Value) {
	if vs == nil {
		return
	}
	vs.hashMap.Delete(k)
}
1124903
func (m *UserManager) Delete(id int64) error {
	stmt := Users.Delete().Where(Users.C("id").Equals(id))
	return m.conn.Query(stmt)
}
1221763
func (m *Map) Delete(k KeyType) {
	hash := m.hash(k)
	for entry, ok := m.lookup[hash]; ok; entry, ok = m.lookup[hash] {
		if m.equals(entry.key.key, k) {
			m.removeMapKey(hash, entry.key)
			return
		}
		// Linear probe to "next" to this entry (really a rehash)
		hash++
	}
}
670972
func (s *Service) Delete(ctx context.Context, v interface{}, path string) error {
	return s.Do(ctx, v, "DELETE", path, nil, nil, nil)
}
966295
func Delete(path string, h interface{}, m ...interface{}) {
	Default.Delete(path, h, m...)
}
52230
func (om *OrdMap) Delete(key interface{}) {
	rkey := ty.AssertType(key, om.ktype)

	// Avoid doing work if we don't need to.
	if !om.exists(rkey) {
		return
	}

	keysLen := om.keys.Len()
	for i := 0; i < keysLen; i++ {
		if key == om.keys.Index(i).Interface() {
			// om.keys = append(om.keys[:i], om.keys[i+1:]...)
			om.keys = reflect.AppendSlice(
				om.keys.Slice(0, i), om.keys.Slice(i+1, keysLen))
			break
		}
	}

	// Setting a key to a zero reflect.Value deletes the key from the map.
	om.m.SetMapIndex(rkey, reflect.Value{})
}
953503
func (s *UserStorage) Delete(id string) error {
	_, exists := s.users[id]
	if !exists {
		return fmt.Errorf("User with id %s does not exist", id)
	}
	delete(s.users, id)

	return nil
}