From: David Kalnischkies Date: Wed, 8 Jul 2015 14:37:04 +0000 (+0200) Subject: prepare cachesets for -std=c++11 X-Git-Tag: 1.1.exp9~108 X-Git-Url: https://git.saurik.com/apt.git/commitdiff_plain/ffb081b79263a699a612583fcf1b9957b5900a77 prepare cachesets for -std=c++11 The "problem" is mostly in the erase() definitions as they slightly conflict and in pre-c++11 are not uniformly in different containers. By differenciating based on the standard we can provide erase() methods for both standards – and as the method is in a template and inline we don't need to worry about symbols here. The rest is adding wrappings for the new forward_list and unordered_set containers and correcting our iterators to use the same trait as the iterator they are wrapping instead of having all of them be simple forward iterators. This allows the use of specialized algorithms which are picked based on iterator_traits and implementing them all is simple to do as we can declare all methods easily and only if they are called they will generate errors (if the underlying iterator doesn't support these). Git-Dch: Ignore --- diff --git a/apt-pkg/cacheset.h b/apt-pkg/cacheset.h index 29f7540ca..df437084b 100644 --- a/apt-pkg/cacheset.h +++ b/apt-pkg/cacheset.h @@ -12,6 +12,10 @@ #include #include #include +#if __cplusplus >= 201103L +#include +#include +#endif #include #include #include @@ -308,35 +312,66 @@ template class PackageContainer : public PackageContainerInterf public: /*{{{*/ /** \brief smell like a pkgCache::PkgIterator */ class const_iterator : public PackageContainerInterface::const_iterator,/*{{{*/ - public std::iterator { - typename Container::const_iterator _iter; + public std::iterator::iterator_category, typename Container::const_iterator> { + typedef typename Container::const_iterator container_iterator; + container_iterator _iter; + typedef const_iterator iterator_type; public: - explicit const_iterator(typename Container::const_iterator i) : _iter(i) {} - pkgCache::PkgIterator getPkg(void) const { return *_iter; } + explicit const_iterator(container_iterator i) : _iter(i) {} + inline pkgCache::PkgIterator getPkg(void) const { return *_iter; } inline pkgCache::PkgIterator operator*(void) const { return *_iter; } - operator typename Container::const_iterator(void) const { return _iter; } - inline const_iterator& operator++() { ++_iter; return *this; } - inline const_iterator operator++(int) { const_iterator tmp(*this); operator++(); return tmp; } - inline bool operator!=(const_iterator const &i) const { return _iter != i._iter; } - inline bool operator==(const_iterator const &i) const { return _iter == i._iter; } - friend std::ostream& operator<<(std::ostream& out, const_iterator i) { return operator<<(out, *i); } + operator container_iterator(void) const { return _iter; } + inline iterator_type& operator++() { ++_iter; return *this; } + inline iterator_type operator++(int) { iterator_type tmp(*this); operator++(); return tmp; } + inline iterator_type operator+(typename iterator_type::difference_type const &n) { return iterator_type(_iter + n); } + inline iterator_type operator+=(typename iterator_type::difference_type const &n) { _iter += n; return *this; } + inline iterator_type& operator--() { --_iter; return *this; } + inline iterator_type operator--(int) { iterator_type tmp(*this); operator--(); return tmp; } + inline iterator_type operator-(typename iterator_type::difference_type const &n) { return iterator_type(_iter - n); } + inline iterator_type operator-=(typename iterator_type::difference_type const &n) { _iter -= n; return *this; } + inline bool operator!=(iterator_type const &i) const { return _iter != i._iter; } + inline bool operator==(iterator_type const &i) const { return _iter == i._iter; } + inline bool operator<(iterator_type const &i) const { return _iter < i._iter; } + inline bool operator>(iterator_type const &i) const { return _iter > i._iter; } + inline bool operator<=(iterator_type const &i) const { return _iter <= i._iter; } + inline bool operator>=(iterator_type const &i) const { return _iter >= i._iter; } + inline typename iterator_type::reference operator[](typename iterator_type::difference_type const &n) const { return _iter[n]; } + + friend std::ostream& operator<<(std::ostream& out, iterator_type i) { return operator<<(out, *i); } + friend class PackageContainer; }; class iterator : public PackageContainerInterface::const_iterator, - public std::iterator { - typename Container::iterator _iter; + public std::iterator::iterator_category, typename Container::iterator> { + typedef typename Container::iterator container_iterator; + container_iterator _iter; + typedef iterator iterator_type; public: - explicit iterator(typename Container::iterator i) : _iter(i) {} - pkgCache::PkgIterator getPkg(void) const { return *_iter; } + explicit iterator(container_iterator i) : _iter(i) {} + inline pkgCache::PkgIterator getPkg(void) const { return *_iter; } inline pkgCache::PkgIterator operator*(void) const { return *_iter; } - operator typename Container::iterator(void) const { return _iter; } + operator container_iterator(void) const { return _iter; } + inline iterator_type& operator++() { ++_iter; return *this; } + inline iterator_type operator++(int) { iterator_type tmp(*this); operator++(); return tmp; } + inline iterator_type operator+(typename iterator_type::difference_type const &n) { return iterator_type(_iter + n); } + inline iterator_type operator+=(typename iterator_type::difference_type const &n) { _iter += n; return *this; } + inline iterator_type& operator--() { --_iter; return *this; } + inline iterator_type operator--(int) { iterator_type tmp(*this); operator--(); return tmp; } + inline iterator_type operator-(typename iterator_type::difference_type const &n) { return iterator_type(_iter - n); } + inline iterator_type operator-=(typename iterator_type::difference_type const &n) { _iter -= n; return *this; } + inline bool operator!=(iterator_type const &i) const { return _iter != i._iter; } + inline bool operator==(iterator_type const &i) const { return _iter == i._iter; } + inline bool operator<(iterator_type const &i) const { return _iter < i._iter; } + inline bool operator>(iterator_type const &i) const { return _iter > i._iter; } + inline bool operator<=(iterator_type const &i) const { return _iter <= i._iter; } + inline bool operator>=(iterator_type const &i) const { return _iter >= i._iter; } + inline typename iterator_type::reference operator[](typename iterator_type::difference_type const &n) const { return _iter[n]; } + operator typename PackageContainer::const_iterator() { return typename PackageContainer::const_iterator(_iter); } - inline iterator& operator++() { ++_iter; return *this; } - inline iterator operator++(int) { iterator tmp(*this); operator++(); return tmp; } - inline bool operator!=(iterator const &i) const { return _iter != i._iter; } - inline bool operator==(iterator const &i) const { return _iter == i._iter; } - inline iterator& operator=(iterator const &i) { _iter = i._iter; return *this; } - inline iterator& operator=(typename Container::iterator const &i) { _iter = i; return *this; } - friend std::ostream& operator<<(std::ostream& out, iterator i) { return operator<<(out, *i); } + inline iterator& operator=(iterator_type const &i) { _iter = i._iter; return *this; } + inline iterator& operator=(container_iterator const &i) { _iter = i; return *this; } + + friend std::ostream& operator<<(std::ostream& out, iterator_type i) { return operator<<(out, *i); } + friend class PackageContainer; }; /*}}}*/ @@ -347,11 +382,17 @@ public: /*{{{*/ bool empty() const { return _cont.empty(); } void clear() { return _cont.clear(); } size_t size() const { return _cont.size(); } - iterator erase( iterator pos ) { return iterator(_cont.erase(pos)); } - iterator erase( iterator first, iterator last ) { return iterator(_cont.erase(first, last)); } - size_t erase(pkgCache::PkgIterator const & P) { size_t oldsize = size(); _cont.erase(std::remove(_cont.begin(), _cont.end(), P), _cont.end()); return oldsize - size(); } +#if __cplusplus >= 201103L + iterator erase( const_iterator pos ) { return iterator(_cont.erase(pos._iter)); } + iterator erase( const_iterator first, const_iterator last ) { return iterator(_cont.erase(first._iter, last._iter)); } +#else + iterator erase( iterator pos ) { return iterator(_cont.erase(pos._iter)); } + iterator erase( iterator first, iterator last ) { return iterator(_cont.erase(first._iter, last._iter)); } +#endif const_iterator begin() const { return const_iterator(_cont.begin()); } const_iterator end() const { return const_iterator(_cont.end()); } + const_iterator cbegin() const { return const_iterator(_cont.cbegin()); } + const_iterator cend() const { return const_iterator(_cont.cend()); } iterator begin() { return iterator(_cont.begin()); } iterator end() { return iterator(_cont.end()); } const_iterator find(pkgCache::PkgIterator const &P) const { return const_iterator(_cont.find(P)); } @@ -510,11 +551,17 @@ template<> template void PackageContainer::const_iterator p = pkgcont.begin(); p != pkgcont.end(); ++p) _cont.push_back(*p); } +#if __cplusplus >= 201103L +template<> template void PackageContainer >::insert(PackageContainer const &pkgcont) { + for (typename PackageContainer::const_iterator p = pkgcont.begin(); p != pkgcont.end(); ++p) + _cont.push_front(*p); +} +#endif 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 +// these 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) { if (P.end() == true) @@ -522,6 +569,14 @@ template<> inline bool PackageContainer >::inse _cont.push_back(P); return true; } +#if __cplusplus >= 201103L +template<> inline bool PackageContainer >::insert(pkgCache::PkgIterator const &P) { + if (P.end() == true) + return false; + _cont.push_front(P); + return true; +} +#endif template<> inline bool PackageContainer >::insert(pkgCache::PkgIterator const &P) { if (P.end() == true) return false; @@ -532,16 +587,42 @@ template<> inline void PackageContainer >::inse for (const_iterator p = begin; p != end; ++p) _cont.push_back(*p); } +#if __cplusplus >= 201103L +template<> inline void PackageContainer >::insert(const_iterator begin, const_iterator end) { + for (const_iterator p = begin; p != end; ++p) + _cont.push_front(*p); +} +#endif template<> inline void PackageContainer >::insert(const_iterator begin, const_iterator end) { for (const_iterator p = begin; p != end; ++p) _cont.push_back(*p); } +#if __cplusplus < 201103L +template<> inline PackageContainer >::iterator PackageContainer >::erase(iterator i) { + _cont.erase(i._iter); + return end(); +} +template<> inline PackageContainer >::iterator PackageContainer >::erase(iterator first, iterator last) { + _cont.erase(first, last); + return end(); +} +#endif /*}}}*/ template<> template inline bool PackageContainer >::sort(Compare Comp) { std::sort(_cont.begin(), _cont.end(), Comp); return true; } +template<> template inline bool PackageContainer >::sort(Compare Comp) { + _cont.sort(Comp); + return true; +} +#if __cplusplus >= 201103L +template<> template inline bool PackageContainer >::sort(Compare Comp) { + _cont.sort(Comp); + return true; +} +#endif // class PackageUniverse - pkgCache as PackageContainerInterface /*{{{*/ /** \class PackageUniverse @@ -563,6 +644,8 @@ public: APT_PUBLIC const_iterator begin() const { return _cont->PkgBegin(); } APT_PUBLIC const_iterator end() const { return _cont->PkgEnd(); } + APT_PUBLIC const_iterator cbegin() const { return _cont->PkgBegin(); } + APT_PUBLIC const_iterator cend() const { return _cont->PkgEnd(); } APT_PUBLIC iterator begin() { return _cont->PkgBegin(); } APT_PUBLIC iterator end() { return _cont->PkgEnd(); } @@ -575,12 +658,15 @@ private: void insert(const_iterator, const_iterator) { } void clear() { } - iterator erase( iterator pos ); - iterator erase( iterator first, iterator last ); - size_t erase(pkgCache::PkgIterator const & P); + iterator erase( const_iterator pos ); + iterator erase( const_iterator first, const_iterator last ); }; /*}}}*/ typedef PackageContainer > PackageSet; +#if __cplusplus >= 201103L +typedef PackageContainer > PackageUnorderedSet; +typedef PackageContainer > PackageForwardList; +#endif typedef PackageContainer > PackageList; typedef PackageContainer > PackageVector; @@ -739,36 +825,67 @@ template class VersionContainer : public VersionContainerInterf Container _cont; public: /*{{{*/ /** \brief smell like a pkgCache::VerIterator */ - class const_iterator : public VersionContainerInterface::const_iterator, - public std::iterator {/*{{{*/ - typename Container::const_iterator _iter; + class const_iterator : public VersionContainerInterface::const_iterator,/*{{{*/ + public std::iterator::iterator_category, typename Container::const_iterator> { + typedef typename Container::const_iterator container_iterator; + container_iterator _iter; + typedef const_iterator iterator_type; public: - explicit const_iterator(typename Container::const_iterator i) : _iter(i) {} - pkgCache::VerIterator getVer(void) const { return *_iter; } + explicit const_iterator(container_iterator i) : _iter(i) {} + inline pkgCache::VerIterator getVer(void) const { return *_iter; } inline pkgCache::VerIterator operator*(void) const { return *_iter; } - operator typename Container::const_iterator(void) const { return _iter; } - inline const_iterator& operator++() { ++_iter; return *this; } - inline const_iterator operator++(int) { const_iterator tmp(*this); operator++(); return tmp; } - inline bool operator!=(const_iterator const &i) const { return _iter != i._iter; } - inline bool operator==(const_iterator const &i) const { return _iter == i._iter; } - friend std::ostream& operator<<(std::ostream& out, const_iterator i) { return operator<<(out, *i); } + operator container_iterator(void) const { return _iter; } + inline iterator_type& operator++() { ++_iter; return *this; } + inline iterator_type operator++(int) { iterator_type tmp(*this); operator++(); return tmp; } + inline iterator_type operator+(typename iterator_type::difference_type const &n) { return iterator_type(_iter + n); } + inline iterator_type operator+=(typename iterator_type::difference_type const &n) { _iter += n; return *this; } + inline iterator_type& operator--() { --_iter; return *this; } + inline iterator_type operator--(int) { iterator_type tmp(*this); operator--(); return tmp; } + inline iterator_type operator-(typename iterator_type::difference_type const &n) { return iterator_type(_iter - n); } + inline iterator_type operator-=(typename iterator_type::difference_type const &n) { _iter -= n; return *this; } + inline bool operator!=(iterator_type const &i) const { return _iter != i._iter; } + inline bool operator==(iterator_type const &i) const { return _iter == i._iter; } + inline bool operator<(iterator_type const &i) const { return _iter < i._iter; } + inline bool operator>(iterator_type const &i) const { return _iter > i._iter; } + inline bool operator<=(iterator_type const &i) const { return _iter <= i._iter; } + inline bool operator>=(iterator_type const &i) const { return _iter >= i._iter; } + inline typename iterator_type::reference operator[](typename iterator_type::difference_type const &n) const { return _iter[n]; } + + friend std::ostream& operator<<(std::ostream& out, iterator_type i) { return operator<<(out, *i); } + friend class VersionContainer; }; class iterator : public VersionContainerInterface::const_iterator, - public std::iterator { - typename Container::iterator _iter; + public std::iterator::iterator_category, typename Container::iterator> { + typedef typename Container::iterator container_iterator; + container_iterator _iter; + typedef iterator iterator_type; public: - explicit iterator(typename Container::iterator i) : _iter(i) {} - pkgCache::VerIterator getVer(void) const { return *_iter; } + explicit iterator(container_iterator i) : _iter(i) {} + inline pkgCache::VerIterator getVer(void) const { return *_iter; } inline pkgCache::VerIterator operator*(void) const { return *_iter; } - operator typename Container::iterator(void) const { return _iter; } + operator container_iterator(void) const { return _iter; } + inline iterator_type& operator++() { ++_iter; return *this; } + inline iterator_type operator++(int) { iterator_type tmp(*this); operator++(); return tmp; } + inline iterator_type operator+(typename iterator_type::difference_type const &n) { return iterator_type(_iter + n); } + inline iterator_type operator+=(typename iterator_type::difference_type const &n) { _iter += n; return *this; } + inline iterator_type& operator--() { --_iter; return *this; } + inline iterator_type operator--(int) { iterator_type tmp(*this); operator--(); return tmp; } + inline iterator_type operator-(typename iterator_type::difference_type const &n) { return iterator_type(_iter - n); } + inline iterator_type operator-=(typename iterator_type::difference_type const &n) { _iter -= n; return *this; } + inline bool operator!=(iterator_type const &i) const { return _iter != i._iter; } + inline bool operator==(iterator_type const &i) const { return _iter == i._iter; } + inline bool operator<(iterator_type const &i) const { return _iter < i._iter; } + inline bool operator>(iterator_type const &i) const { return _iter > i._iter; } + inline bool operator<=(iterator_type const &i) const { return _iter <= i._iter; } + inline bool operator>=(iterator_type const &i) const { return _iter >= i._iter; } + inline typename iterator_type::reference operator[](typename iterator_type::difference_type const &n) const { return _iter[n]; } + operator typename VersionContainer::const_iterator() { return typename VersionContainer::const_iterator(_iter); } - inline iterator& operator++() { ++_iter; return *this; } - inline iterator operator++(int) { iterator tmp(*this); operator++(); return tmp; } - inline bool operator!=(iterator const &i) const { return _iter != i._iter; } - inline bool operator==(iterator const &i) const { return _iter == i._iter; } - inline iterator& operator=(iterator const &i) { _iter = i._iter; return *this; } - inline iterator& operator=(typename Container::iterator const &i) { _iter = i; return *this; } - friend std::ostream& operator<<(std::ostream& out, iterator i) { return operator<<(out, *i); } + inline iterator& operator=(iterator_type const &i) { _iter = i._iter; return *this; } + inline iterator& operator=(container_iterator const &i) { _iter = i; return *this; } + + friend std::ostream& operator<<(std::ostream& out, iterator_type i) { return operator<<(out, *i); } + friend class VersionContainer; }; /*}}}*/ @@ -778,12 +895,18 @@ public: /*{{{*/ bool empty() const { return _cont.empty(); } void clear() { return _cont.clear(); } size_t size() const { return _cont.size(); } - iterator erase( iterator pos ) { return iterator(_cont.erase(pos)); } - iterator erase( iterator first, iterator last ) { return iterator(_cont.erase(first, last)); } - size_t erase(pkgCache::VerIterator const & V) { size_t oldsize = size(); _cont.erase(std::remove(_cont.begin(), _cont.end(), V), _cont.end()); return oldsize - size(); } +#if __cplusplus >= 201103L + iterator erase( const_iterator pos ) { return iterator(_cont.erase(pos._iter)); } + iterator erase( const_iterator first, const_iterator last ) { return iterator(_cont.erase(first._iter, last._iter)); } +#else + iterator erase( iterator pos ) { return iterator(_cont.erase(pos._iter)); } + iterator erase( iterator first, iterator last ) { return iterator(_cont.erase(first._iter, last._iter)); } +#endif const_iterator begin() const { return const_iterator(_cont.begin()); } const_iterator end() const { return const_iterator(_cont.end()); } + const_iterator cbegin() const { return const_iterator(_cont.cbegin()); } + const_iterator cend() const { return const_iterator(_cont.cend()); } iterator begin() { return iterator(_cont.begin()); } iterator end() { return iterator(_cont.end()); } const_iterator find(pkgCache::VerIterator const &V) const { return const_iterator(_cont.find(V)); } @@ -956,11 +1079,17 @@ template<> template void VersionContainer::const_iterator v = vercont.begin(); v != vercont.end(); ++v) _cont.push_back(*v); } +#if __cplusplus >= 201103L +template<> template void VersionContainer >::insert(VersionContainer const &vercont) { + for (typename VersionContainer::const_iterator v = vercont.begin(); v != vercont.end(); ++v) + _cont.push_front(*v); +} +#endif 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 +// these 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) { if (V.end() == true) @@ -968,6 +1097,14 @@ template<> inline bool VersionContainer >::inse _cont.push_back(V); return true; } +#if __cplusplus >= 201103L +template<> inline bool VersionContainer >::insert(pkgCache::VerIterator const &V) { + if (V.end() == true) + return false; + _cont.push_front(V); + return true; +} +#endif template<> inline bool VersionContainer >::insert(pkgCache::VerIterator const &V) { if (V.end() == true) return false; @@ -978,25 +1115,48 @@ template<> inline void VersionContainer >::inse for (const_iterator v = begin; v != end; ++v) _cont.push_back(*v); } +#if __cplusplus >= 201103L +template<> inline void VersionContainer >::insert(const_iterator begin, const_iterator end) { + for (const_iterator v = begin; v != end; ++v) + _cont.push_front(*v); +} +#endif 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 size_t PackageContainer >::erase(pkgCache::PkgIterator const &P) { - return _cont.erase(P); +#if __cplusplus < 201103L +template<> inline VersionContainer >::iterator VersionContainer >::erase(iterator i) { + _cont.erase(i._iter); + return end(); } -template<> inline size_t VersionContainer >::erase(pkgCache::VerIterator const &V) { - return _cont.erase(V); +template<> inline VersionContainer >::iterator VersionContainer >::erase(iterator first, iterator last) { + _cont.erase(first, last); + return end(); } +#endif + /*}}}*/ template<> template inline bool VersionContainer >::sort(Compare Comp) { std::sort(_cont.begin(), _cont.end(), Comp); return true; } +template<> template inline bool VersionContainer >::sort(Compare Comp) { + _cont.sort(Comp); + return true; +} +#if __cplusplus >= 201103L +template<> template inline bool VersionContainer >::sort(Compare Comp) { + _cont.sort(Comp); + return true; +} +#endif typedef VersionContainer > VersionSet; +#if __cplusplus >= 201103L +typedef VersionContainer > VersionUnorderedSet; +typedef VersionContainer > VersionForwardList; +#endif typedef VersionContainer > VersionList; typedef VersionContainer > VersionVector; } diff --git a/apt-private/private-install.cc b/apt-private/private-install.cc index acc6d42c2..3474d262a 100644 --- a/apt-private/private-install.cc +++ b/apt-private/private-install.cc @@ -419,7 +419,7 @@ static bool DoAutomaticRemove(CacheFile &Cache) bool Changed; do { Changed = false; - for (APT::PackageSet::const_iterator Pkg = tooMuch.begin(); + for (APT::PackageSet::iterator Pkg = tooMuch.begin(); Pkg != tooMuch.end(); ++Pkg) { APT::PackageSet too;