func (l *PackageList) Remove(p *Package) {
	delete(l.packages, l.keyFunc(p))
	if l.indexed {
		for _, provides := range p.Provides {
			for i, pkg := range l.providesIndex[provides] {
				if pkg.Equals(p) {
					l.providesIndex[provides][len(l.providesIndex[provides])-1], l.providesIndex[provides][i], l.providesIndex[provides] =
						nil, l.providesIndex[provides][len(l.providesIndex[provides])-1], l.providesIndex[provides][:len(l.providesIndex[provides])-1]
					break
				}
			}
		}
		i := sort.Search(len(l.packagesIndex), func(j int) bool { return l.packagesIndex[j].Name >= p.Name })
		for i < len(l.packagesIndex) && l.packagesIndex[i].Name == p.Name {
			if l.packagesIndex[i].Equals(p) {
				copy(l.packagesIndex[i:], l.packagesIndex[i+1:])
				l.packagesIndex[len(l.packagesIndex)-1] = nil
				l.packagesIndex = l.packagesIndex[:len(l.packagesIndex)-1]
				break
			}
			i++
		}
	}
}
Remove removes package from the list, and updates index when required
package deb
import (
	"fmt"
	"sort"
	"strings"
	"github.com/aptly-dev/aptly/aptly"
	"github.com/aptly-dev/aptly/utils"
)
const (
	DepFollowSource = 1 << iota
	DepFollowSuggests
	DepFollowRecommends
	DepFollowAllVariants
	DepFollowBuild
	DepVerboseResolve
)
type PackageList struct {
	packages map[string]*Package
	packagesIndex []*Package
	providesIndex map[string][]*Package
	keyFunc func(p *Package) string
	duplicatesAllowed bool
	indexed bool
}
type PackageConflictError struct {
	error
}
var (
	_ sort.Interface = &PackageList{}
	_ PackageCatalog = &PackageList{}
)
func packageShortKey(p *Package) string {
	return string(p.ShortKey(""))
}
func packageFullKey(p *Package) string {
	return string(p.Key(""))
}
func NewPackageList() *PackageList {
	return NewPackageListWithDuplicates(false, 1000)
}
func NewPackageListWithDuplicates(duplicates bool, capacity int) *PackageList {
	if capacity == 0 {
		capacity = 1000
	}
	result := &PackageList{
		packages:          make(map[string]*Package, capacity),
		duplicatesAllowed: duplicates,
		keyFunc:           packageShortKey,
	}
	if duplicates {
		result.keyFunc = packageFullKey
	}
	return result
}
func NewPackageListFromRefList(reflist *PackageRefList, collection *PackageCollection, progress aptly.Progress) (*PackageList, error) {
	if reflist == nil {
		return NewPackageList(), nil
	}
	result := NewPackageListWithDuplicates(false, reflist.Len())
	if progress != nil {
		progress.InitBar(int64(reflist.Len()), false)
	}
	err := reflist.ForEach(func(key []byte) error {
		p, err2 := collection.ByKey(key)
		if err2 != nil {
			return fmt.Errorf("unable to load package with key %s: %s", key, err2)
		}
		if progress != nil {
			progress.AddBar(1)
		}
		return result.Add(p)
	})
	if progress != nil {
		progress.ShutdownBar()
	}
	if err != nil {
		return nil, err
	}
	return result, nil
}
func (l *PackageList) Has(p *Package) bool {
	key := l.keyFunc(p)
	_, ok := l.packages[key]
	return ok
}
func (l *PackageList) Add(p *Package) error {
	key := l.keyFunc(p)
	existing, ok := l.packages[key]
	if ok {
		if !existing.Equals(p) {
			return &PackageConflictError{fmt.Errorf("conflict in package %s", p)}
		}
		return nil
	}
	l.packages[key] = p
	if l.indexed {
		for _, provides := range p.Provides {
			l.providesIndex[provides] = append(l.providesIndex[provides], p)
		}
		i := sort.Search(len(l.packagesIndex), func(j int) bool { return l.lessPackages(p, l.packagesIndex[j]) })
		l.packagesIndex = append(l.packagesIndex, nil)
		copy(l.packagesIndex[i+1:], l.packagesIndex[i:])
		l.packagesIndex[i] = p
	}
	return nil
}
func (l *PackageList) ForEach(handler func(*Package) error) error {
	var err error
	for _, p := range l.packages {
		err = handler(p)
		if err != nil {
			return err
		}
	}
	return err
}
func (l *PackageList) ForEachIndexed(handler func(*Package) error) error {
	if !l.indexed {
		panic("list not indexed, can't iterate")
	}
	var err error
	for _, p := range l.packagesIndex {
		err = handler(p)
		if err != nil {
			return err
		}
	}
	return err
}
func (l *PackageList) Len() int {
	return len(l.packages)
}
func (l *PackageList) Append(pl *PackageList) error {
	if l.indexed {
		panic("Append not supported when indexed")
	}
	for k, p := range pl.packages {
		existing, ok := l.packages[k]
		if ok {
			if !existing.Equals(p) {
				return fmt.Errorf("conflict in package %s", p)
			}
		} else {
			l.packages[k] = p
		}
	}
	return nil
}