From 44ce4c8a77da78161186b5211827e957917b4283 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Sat, 28 Jun 2014 00:26:56 +0400 Subject: [PATCH] Insert into right position when adding as well. #67 --- deb/list.go | 13 +++++++------ deb/list_test.go | 3 +++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/deb/list.go b/deb/list.go index cbe25d5f..4d379cad 100644 --- a/deb/list.go +++ b/deb/list.go @@ -101,7 +101,7 @@ func (l *PackageList) Add(p *Package) error { l.providesIndex[provides] = append(l.providesIndex[provides], p) } - i := sort.Search(len(l.packagesIndex), func(j int) bool { return l.packagesIndex[j].Name >= p.Name }) + i := sort.Search(len(l.packagesIndex), func(j int) bool { return l.lessPackages(p, l.packagesIndex[j]) }) // insert p into l.packagesIndex in position i l.packagesIndex = append(l.packagesIndex, nil) @@ -300,11 +300,7 @@ func (l *PackageList) Swap(i, j int) { l.packagesIndex[i], l.packagesIndex[j] = l.packagesIndex[j], l.packagesIndex[i] } -// Compare compares two packages by name (lexographical) and version (latest to oldest) -func (l *PackageList) Less(i, j int) bool { - iPkg := l.packagesIndex[i] - jPkg := l.packagesIndex[j] - +func (l *PackageList) lessPackages(iPkg, jPkg *Package) bool { if iPkg.Name == jPkg.Name { return CompareVersions(iPkg.Version, jPkg.Version) == 1 } @@ -312,6 +308,11 @@ func (l *PackageList) Less(i, j int) bool { return iPkg.Name < jPkg.Name } +// Less compares two packages by name (lexographical) and version (latest to oldest) +func (l *PackageList) Less(i, j int) bool { + return l.lessPackages(l.packagesIndex[i], l.packagesIndex[j]) +} + // PrepareIndex prepares list for indexing func (l *PackageList) PrepareIndex() { l.packagesIndex = make([]*Package, l.Len()) diff --git a/deb/list_test.go b/deb/list_test.go index 953f5e85..5a7db3be 100644 --- a/deb/list_test.go +++ b/deb/list_test.go @@ -218,6 +218,9 @@ func (s *PackageListSuite) TestSearch(c *C) { c.Check(s.il.Search(Dependency{Architecture: "i386", Pkg: "app", Relation: VersionGreaterOrEqual, Version: "1.0"}), Equals, s.packages[3]) c.Check(s.il.Search(Dependency{Architecture: "i386", Pkg: "app", Relation: VersionGreaterOrEqual, Version: "1.1~bp1"}), Equals, s.packages[3]) c.Check(s.il.Search(Dependency{Architecture: "i386", Pkg: "app", Relation: VersionGreaterOrEqual, Version: "1.2"}), IsNil) + + // search w/o version should return package with latest version + c.Check(s.il.Search(Dependency{Architecture: "source", Pkg: "dpkg"}), Equals, s.packages[13]) } func (s *PackageListSuite) TestFilter(c *C) {