diff options
author | Daniel Hartwig <mandyke@gmail.com> | 2012-02-24 23:59:38 +0800 |
---|---|---|
committer | Daniel Hartwig <mandyke@gmail.com> | 2012-02-24 23:59:38 +0800 |
commit | b2813fb245a6c852fa8f4fdb900bb639bc32ae04 (patch) | |
tree | a250c749e049120610c6e4013a3a7a59b2d6eb18 | |
parent | a87341c87ce485b4e47bdab8771c9828d015e223 (diff) | |
download | aptitude-b2813fb245a6c852fa8f4fdb900bb639bc32ae04.tar.gz |
Refactor various apt-related comparison functors to apt.h
The resulting functors:
- pkg_name_lt
- pkg_ptr_lt
- ver_name_lt
- ver_ptr_lt
- dep_type_lt
-rw-r--r-- | src/cmdline/cmdline_prompt.cc | 13 | ||||
-rw-r--r-- | src/cmdline/cmdline_why.cc | 57 | ||||
-rw-r--r-- | src/generic/apt/apt.h | 75 | ||||
-rw-r--r-- | src/generic/apt/infer_reason.cc | 11 | ||||
-rw-r--r-- | src/generic/apt/log.cc | 4 | ||||
-rw-r--r-- | src/gtk/dashboardtab.cc | 16 | ||||
-rw-r--r-- | src/gtk/entitysummary.cc | 4 | ||||
-rw-r--r-- | src/pkg_ver_item.h | 19 | ||||
-rw-r--r-- | src/reason_fragment.cc | 27 |
9 files changed, 102 insertions, 124 deletions
diff --git a/src/cmdline/cmdline_prompt.cc b/src/cmdline/cmdline_prompt.cc index c98a38a4..c65f779e 100644 --- a/src/cmdline/cmdline_prompt.cc +++ b/src/cmdline/cmdline_prompt.cc @@ -74,11 +74,6 @@ struct fetchinfo } }; -static bool pkg_byname_compare(const pkgCache::PkgIterator &a, const pkgCache::PkgIterator &b) -{ - return strcmp(a.Name(), b.Name())<0; -} - static bool get_fetchinfo(fetchinfo &f) { download_signal_log m; @@ -137,6 +132,8 @@ namespace // action. struct compare_first_action { + pkg_name_lt base; + public: typedef aptitude::why::action action; bool operator()(const std::vector<action> &reason1, const std::vector<action> &reason2) @@ -146,8 +143,8 @@ namespace else if(reason2.empty()) return false; else - return strcmp(reason1.front().get_dep().ParentPkg().Name(), - reason2.front().get_dep().ParentPkg().Name()); + return base(reason1.front().get_dep().ParentPkg(), + reason2.front().get_dep().ParentPkg()); } }; @@ -261,7 +258,7 @@ static void cmdline_show_instinfo(pkgvector &items, bool showwhy, const shared_ptr<terminal_metrics> &term_metrics) { - sort(items.begin(), items.end(), pkg_byname_compare); + sort(items.begin(), items.end(), pkg_name_lt()); strvector output; for(pkgvector::iterator i=items.begin(); i!=items.end(); ++i) diff --git a/src/cmdline/cmdline_why.cc b/src/cmdline/cmdline_why.cc index d03fda8c..a8034c08 100644 --- a/src/cmdline/cmdline_why.cc +++ b/src/cmdline/cmdline_why.cc @@ -89,62 +89,11 @@ namespace dep.TargetPkg().Name()); } - struct compare_pkgs - { - bool operator()(const pkgCache::PkgIterator &p1, - const pkgCache::PkgIterator &p2) - { - return p1->ID < p2->ID; - } - }; - - struct compare_dep_levels - { - static int dep_level_to_int(const pkgCache::Dep::DepType dt) - { - switch(dt) - { - case pkgCache::Dep::PreDepends: - return 7; - - case pkgCache::Dep::Depends: - return 6; - - case pkgCache::Dep::Recommends: - return 5; - - case pkgCache::Dep::Conflicts: - return 4; - - case pkgCache::Dep::DpkgBreaks: - return 3; - - case pkgCache::Dep::Suggests: - return 2; - - case pkgCache::Dep::Replaces: - return 1; - - case pkgCache::Dep::Obsoletes: - return 0; - - default: - return -1; - } - } - - bool operator()(const pkgCache::Dep::DepType dt1, - const pkgCache::Dep::DepType dt2) const - { - return dep_level_to_int(dt1) < dep_level_to_int(dt2); - } - }; - // Place weaker dependencies first, then order alphabetically. struct compare_pair_by_dep_type { - compare_dep_levels dep_type_less_than; - + dep_type_lt dep_type_less_than; + public: bool operator()(const std::pair<std::string, pkgCache::Dep::DepType> &p1, const std::pair<std::string, pkgCache::Dep::DepType> &p2) const { @@ -1551,7 +1500,7 @@ namespace aptitude if(mode == show_requiring_packages || mode == show_requiring_packages_and_strength) { - compare_dep_levels dep_less_than; + dep_type_lt dep_less_than; // Maps root names to strongest dependency type. std::map<std::string, pkgCache::Dep::DepType> roots; for(std::vector<std::vector<action> >::const_iterator it = diff --git a/src/generic/apt/apt.h b/src/generic/apt/apt.h index 208542f9..fc6fb64a 100644 --- a/src/generic/apt/apt.h +++ b/src/generic/apt/apt.h @@ -318,6 +318,22 @@ public: } }; +/** Compare two packages by memory location (useful for inserting into + * maps when the particular order is uninteresting). + * + * It is safe to use < and not std::less here because the pointers + * share an array in the package cache. + */ +struct pkg_ptr_lt +{ +public: + bool operator()(const pkgCache::PkgIterator &p1, + const pkgCache::PkgIterator &p2) const + { + return &*p1 < &*p2; + } +}; + /** Sort versions by package name. */ struct ver_name_lt { @@ -329,6 +345,65 @@ public: } }; +/** Compare two versions by memory location (useful for inserting into + * maps when the particular order is uninteresting). + * + * It is safe to use < and not std::less here because the pointers + * share an array in the package cache. + */ +struct ver_ptr_lt +{ +public: + bool operator()(const pkgCache::VerIterator &v1, + const pkgCache::VerIterator &v2) const + { + return &*v1 < &*v2; + } +}; + +struct dep_type_lt +{ + static int dep_type_to_int(const pkgCache::Dep::DepType dt) + { + switch(dt) + { + case pkgCache::Dep::PreDepends: + return 7; + + case pkgCache::Dep::Depends: + return 6; + + case pkgCache::Dep::Recommends: + return 5; + + case pkgCache::Dep::Conflicts: + return 4; + + case pkgCache::Dep::DpkgBreaks: + return 3; + + case pkgCache::Dep::Suggests: + return 2; + + case pkgCache::Dep::Replaces: + return 1; + + case pkgCache::Dep::Obsoletes: + return 0; + + default: + return -1; + } + } + +public: + bool operator()(const pkgCache::Dep::DepType dt1, + const pkgCache::Dep::DepType dt2) const + { + return dep_type_to_int(dt1) < dep_type_to_int(dt2); + } +}; + /** \return \b true if the given dependency type is Conflicts or * Breaks. * diff --git a/src/generic/apt/infer_reason.cc b/src/generic/apt/infer_reason.cc index 233ba985..ef283827 100644 --- a/src/generic/apt/infer_reason.cc +++ b/src/generic/apt/infer_reason.cc @@ -294,22 +294,13 @@ void infer_reverse_breakage(pkgCache::PkgIterator &pkg, } } -struct cmp_pkg_mem -{ - bool operator()(const pkgCache::PkgIterator &P1, - const pkgCache::PkgIterator &P2) const - { - return less<const pkgCache::Package *>()(&*P1, &*P2); - } -}; - void infer_reverse_breakage(pkgCache::PkgIterator pkg, set<reason> &reasons) { for(pkgCache::DepIterator D=pkg.RevDependsList(); !D.end(); ++D) infer_reverse_breakage(pkg, D, reasons); - set<pkgCache::PkgIterator, cmp_pkg_mem> seen; + set<pkgCache::PkgIterator, pkg_ptr_lt> seen; // Look at the list of stuff provided by any version. for(pkgCache::VerIterator V=pkg.VersionList(); !V.end(); ++V) diff --git a/src/generic/apt/log.cc b/src/generic/apt/log.cc index a2554a65..77c3551a 100644 --- a/src/generic/apt/log.cc +++ b/src/generic/apt/log.cc @@ -154,6 +154,7 @@ bool do_log(const string &log, struct log_sorter { + pkg_name_lt plt; public: inline bool operator()(const logitem &a, const logitem &b) { @@ -161,7 +162,8 @@ public: return true; else if(a.second>b.second) return false; - else return strcmp(a.first.Name(), b.first.Name())<0; + else + return plt(a.first, b.first); } }; diff --git a/src/gtk/dashboardtab.cc b/src/gtk/dashboardtab.cc index 1bdfd208..ce0911de 100644 --- a/src/gtk/dashboardtab.cc +++ b/src/gtk/dashboardtab.cc @@ -272,19 +272,6 @@ namespace gui upgrades_pkg_view->show_edit_columns_dialog(); } - namespace - { - struct sort_versions_by_package_name - { - bool operator()(const pkgCache::VerIterator &v1, - const pkgCache::VerIterator &v2) - { - return strcmp(v1.ParentPkg().Name(), - v2.ParentPkg().Name()) < 0; - } - }; - } - // Download all the changelogs and show the new entries. void DashboardTab::create_upgrade_summary() { @@ -329,8 +316,7 @@ namespace gui p->Done(); } - std::sort(versions.begin(), versions.end(), - sort_versions_by_package_name()); + std::sort(versions.begin(), versions.end(), ver_name_lt()); upgrades_summary_textview->set_buffer(text_buffer); diff --git a/src/gtk/entitysummary.cc b/src/gtk/entitysummary.cc index c4efc66e..bc651488 100644 --- a/src/gtk/entitysummary.cc +++ b/src/gtk/entitysummary.cc @@ -238,10 +238,12 @@ namespace gui struct compare_provider_lists_by_name { + pkg_name_lt base; + public: bool operator()(const std::pair<pkgCache::PkgIterator, std::vector<pkgCache::VerIterator> > &p1, const std::pair<pkgCache::PkgIterator, std::vector<pkgCache::VerIterator> > &p2) const { - return strcmp(p1.first.Name(), p2.first.Name()) < 0; + return base(p1.first, p2.first); } }; } diff --git a/src/pkg_ver_item.h b/src/pkg_ver_item.h index 1d2d64e1..ddbd2519 100644 --- a/src/pkg_ver_item.h +++ b/src/pkg_ver_item.h @@ -122,16 +122,8 @@ public: class versort:public cwidget::widgets::sortpolicy { + pkg_name_lt plt; public: - inline int compare(const pkg_ver_item *item1, const pkg_ver_item *item2) const - { - if(item1->get_version().ParentPkg()!=item2->get_version().ParentPkg()) - return strcmp(item1->get_version().ParentPkg().Name(), - item2->get_version().ParentPkg().Name()); - - return _system->VS->CmpVersion(item1->get_version().VerStr(), item2->get_version().VerStr()); - } - bool operator()(cwidget::widgets::treeitem *item1, cwidget::widgets::treeitem *item2) { // FIXME: this is horrible. @@ -139,7 +131,14 @@ public: const pkg_ver_item *pitem2=dynamic_cast<const pkg_ver_item *>(item2); if(pitem1 && pitem2) - return (compare(pitem1,pitem2)<0); + { + if(pitem1->get_version().ParentPkg()!=pitem2->get_version().ParentPkg()) + return plt(pitem1->get_version().ParentPkg(), + pitem2->get_version().ParentPkg()); + + return _system->VS->CmpVersion(pitem1->get_version().VerStr(), + pitem2->get_version().VerStr()) < 0; + } else return false; // we shouldn't get here! } diff --git a/src/reason_fragment.cc b/src/reason_fragment.cc index d7f2cd95..03a257c7 100644 --- a/src/reason_fragment.cc +++ b/src/reason_fragment.cc @@ -15,7 +15,6 @@ #include <cwidget/config/colors.h> #include <cwidget/fragment.h> -#include <functional> #include <set> using namespace std; @@ -51,28 +50,6 @@ cw::fragment *depname_frag(pkgCache::DepIterator dep) return cw::text_fragment("has an invalid dependency type!", cw::get_style("Error")); } -/** Compare two packages by name */ -struct pkg_name_cmp -{ - bool operator()(pkgCache::PkgIterator P1, - pkgCache::PkgIterator P2) - { - return strcmp(P1.Name(), P2.Name())<0; - } -}; - -/** Compare two versions by memory location (useful for inserting into - * maps when the particular order is uninteresting) - */ -struct ver_ptr_cmp -{ - bool operator()(pkgCache::VerIterator V1, - pkgCache::VerIterator V2) - { - return less<void*>()(&*V1, &*V2); - } -}; - /** Generate a cw::fragment describing the packages providing a given package. * * \param dep the dependency that the provides are related to; @@ -100,8 +77,8 @@ cw::fragment *prvfrag(pkgCache::DepIterator dep, vector<cw::fragment*> fragments; - set<pkgCache::VerIterator, ver_ptr_cmp> providing_versions; - set<pkgCache::PkgIterator, pkg_name_cmp> providing_packages; + set<pkgCache::VerIterator, ver_ptr_lt> providing_versions; + set<pkgCache::PkgIterator, pkg_name_lt> providing_packages; pkgCache::VerIterator candver=(*apt_cache_file)[pkg].CandidateVerIter(*apt_cache_file); |