From: David Kalnischkies Date: Tue, 2 Sep 2014 22:26:13 +0000 (+0200) Subject: add specialisations for std::vector X-Git-Tag: 1.1.exp4~44 X-Git-Url: https://git.saurik.com/apt.git/commitdiff_plain/5c8c7321559d9136ba90d0ec647ef0234e8d6c23?hp=67caa2e6538f13ff7a0d77e98ad6c58af998376d add specialisations for std::vector Git-Dch: Ignore --- diff --git a/apt-pkg/cacheset.h b/apt-pkg/cacheset.h index 36f41c34d..736dda3cf 100644 --- a/apt-pkg/cacheset.h +++ b/apt-pkg/cacheset.h @@ -13,8 +13,10 @@ #include #include #include +#include #include #include +#include #include @@ -99,6 +101,7 @@ protected: bool ShowError; GlobalError::MsgType ErrorType; }; /*}}}*/ + class PackageContainerInterface { /*{{{*/ /** \class PackageContainerInterface @@ -243,6 +246,18 @@ public: /*{{{*/ PackageContainer() : ConstructedBy(UNKNOWN) {} PackageContainer(Constructor const &by) : ConstructedBy(by) {} + /** \brief sort all included versions with given comparer + + Some containers are sorted by default, some are not and can't be, + but a few like std::vector can be sorted if need be, so this can be + specialized in later on. The default is that this will fail though. + Specifically, already sorted containers like std::set will return + false as well as there is no easy way to check that the given comparer + would sort in the same way the set is currently sorted + + \return \b true if the set was sorted, \b false if not. */ + template bool sort(Compare /*Comp*/) { return false; } + /** \brief returns all packages in the cache who belong to the given task A simple helper responsible for search for all members of a task @@ -375,11 +390,15 @@ private: /*{{{*/ Constructor ConstructedBy; /*}}}*/ }; /*}}}*/ - +// specialisations for push_back containers: std::list & std::vector /*{{{*/ template<> template void PackageContainer >::insert(PackageContainer const &pkgcont) { for (typename PackageContainer::const_iterator p = pkgcont.begin(); p != pkgcont.end(); ++p) _cont.push_back(*p); } +template<> template void PackageContainer >::insert(PackageContainer const &pkgcont) { + for (typename PackageContainer::const_iterator p = pkgcont.begin(); p != pkgcont.end(); ++p) + _cont.push_back(*p); +} // these two are 'inline' as otherwise the linker has problems with seeing these untemplated // specializations again and again - but we need to see them, so that library users can use them template<> inline bool PackageContainer >::insert(pkgCache::PkgIterator const &P) { @@ -388,12 +407,30 @@ template<> inline bool PackageContainer >::inse _cont.push_back(P); return true; } +template<> inline bool PackageContainer >::insert(pkgCache::PkgIterator const &P) { + if (P.end() == true) + return false; + _cont.push_back(P); + return true; +} template<> inline void PackageContainer >::insert(const_iterator begin, const_iterator end) { for (const_iterator p = begin; p != end; ++p) _cont.push_back(*p); } +template<> inline void PackageContainer >::insert(const_iterator begin, const_iterator end) { + for (const_iterator p = begin; p != end; ++p) + _cont.push_back(*p); +} + /*}}}*/ + +template<> template inline bool PackageContainer >::sort(Compare Comp) { + std::sort(_cont.begin(), _cont.end(), Comp); + return true; +} + typedef PackageContainer > PackageSet; typedef PackageContainer > PackageList; +typedef PackageContainer > PackageVector; class VersionContainerInterface { /*{{{*/ /** \class APT::VersionContainerInterface @@ -568,6 +605,18 @@ public: /*{{{*/ iterator end() { return iterator(_cont.end()); } const_iterator find(pkgCache::VerIterator const &V) const { return const_iterator(_cont.find(V)); } + /** \brief sort all included versions with given comparer + + Some containers are sorted by default, some are not and can't be, + but a few like std::vector can be sorted if need be, so this can be + specialized in later on. The default is that this will fail though. + Specifically, already sorted containers like std::set will return + false as well as there is no easy way to check that the given comparer + would sort in the same way the set is currently sorted + + \return \b true if the set was sorted, \b false if not. */ + template bool sort(Compare /*Comp*/) { return false; } + /** \brief returns all versions specified on the commandline Get all versions from the commandline, uses given default version if @@ -669,11 +718,15 @@ public: /*{{{*/ } /*}}}*/ }; /*}}}*/ - +// specialisations for push_back containers: std::list & std::vector /*{{{*/ template<> template void VersionContainer >::insert(VersionContainer const &vercont) { for (typename VersionContainer::const_iterator v = vercont.begin(); v != vercont.end(); ++v) _cont.push_back(*v); } +template<> template void VersionContainer >::insert(VersionContainer const &vercont) { + for (typename VersionContainer::const_iterator v = vercont.begin(); v != vercont.end(); ++v) + _cont.push_back(*v); +} // these two are 'inline' as otherwise the linker has problems with seeing these untemplated // specializations again and again - but we need to see them, so that library users can use them template<> inline bool VersionContainer >::insert(pkgCache::VerIterator const &V) { @@ -682,11 +735,29 @@ template<> inline bool VersionContainer >::inse _cont.push_back(V); return true; } +template<> inline bool VersionContainer >::insert(pkgCache::VerIterator const &V) { + if (V.end() == true) + return false; + _cont.push_back(V); + return true; +} template<> inline void VersionContainer >::insert(const_iterator begin, const_iterator end) { for (const_iterator v = begin; v != end; ++v) _cont.push_back(*v); } +template<> inline void VersionContainer >::insert(const_iterator begin, const_iterator end) { + for (const_iterator v = begin; v != end; ++v) + _cont.push_back(*v); +} + /*}}}*/ + +template<> template inline bool VersionContainer >::sort(Compare Comp) { + std::sort(_cont.begin(), _cont.end(), Comp); + return true; +} + typedef VersionContainer > VersionSet; typedef VersionContainer > VersionList; +typedef VersionContainer > VersionVector; } #endif