Message ID | 20201227100331.1347-1-laurent.pinchart@ideasonboard.com |
---|---|
State | Accepted |
Delegated to: | Laurent Pinchart |
Headers | show |
Series |
|
Related | show |
Hi Laurent, I like template magic :-) On 2020-12-27 12:03:31 +0200, Laurent Pinchart wrote: > Add a utils::reverse() function that creates an adapter wrapping in > iterable, such that range-based iteration over the adapter iterates over > the iterable in reverse order. > > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> > --- > include/libcamera/internal/utils.h | 27 +++++++++++++++++++++++++++ > src/libcamera/utils.cpp | 8 ++++++++ > 2 files changed, 35 insertions(+) > > This will be used in an upcoming patch series, I'm already sending it > out to get the idea reviewed. Looking forward do see what you have in store ;-) Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> > > diff --git a/include/libcamera/internal/utils.h b/include/libcamera/internal/utils.h > index f08134afb5ba..d0146b71727d 100644 > --- a/include/libcamera/internal/utils.h > +++ b/include/libcamera/internal/utils.h > @@ -203,6 +203,33 @@ constexpr unsigned int alignUp(unsigned int value, unsigned int alignment) > return (value + alignment - 1) / alignment * alignment; > } > > +namespace details { > + > +template<typename T> > +struct reverse_adapter { > + T &iterable; > +}; > + > +template<typename T> > +auto begin(reverse_adapter<T> r) > +{ > + return std::rbegin(r.iterable); > +} > + > +template<typename T> > +auto end(reverse_adapter<T> r) > +{ > + return std::rend(r.iterable); > +} > + > +} /* namespace details */ > + > +template<typename T> > +details::reverse_adapter<T> reverse(T &&iterable) > +{ > + return { iterable }; > +} > + > } /* namespace utils */ > > } /* namespace libcamera */ > diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp > index e90375ae115c..c4098a74e0ab 100644 > --- a/src/libcamera/utils.cpp > +++ b/src/libcamera/utils.cpp > @@ -464,6 +464,14 @@ std::string libcameraSourcePath() > * \return The value rounded up to the nearest multiple of \a alignment > */ > > +/** > + * \fn reverse(T &&iterable) > + * \brief Wrap an iterable to reverse iteration in a range-based loop > + * \param[in] iterable The iterable > + * \return A value of unspecified type that, when used in a range-based for > + * loop, will cause the loop to iterate over the \a iterable in reverse order > + */ > + > } /* namespace utils */ > > } /* namespace libcamera */ > -- > Regards, > > Laurent Pinchart > > _______________________________________________ > libcamera-devel mailing list > libcamera-devel@lists.libcamera.org > https://lists.libcamera.org/listinfo/libcamera-devel
Hi Laurent, On Sun, Dec 27, 2020 at 12:03:31PM +0200, Laurent Pinchart wrote: > Add a utils::reverse() function that creates an adapter wrapping in > iterable, such that range-based iteration over the adapter iterates over > the iterable in reverse order. > > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> > --- > include/libcamera/internal/utils.h | 27 +++++++++++++++++++++++++++ > src/libcamera/utils.cpp | 8 ++++++++ > 2 files changed, 35 insertions(+) > > This will be used in an upcoming patch series, I'm already sending it > out to get the idea reviewed. > > diff --git a/include/libcamera/internal/utils.h b/include/libcamera/internal/utils.h > index f08134afb5ba..d0146b71727d 100644 > --- a/include/libcamera/internal/utils.h > +++ b/include/libcamera/internal/utils.h > @@ -203,6 +203,33 @@ constexpr unsigned int alignUp(unsigned int value, unsigned int alignment) > return (value + alignment - 1) / alignment * alignment; > } > > +namespace details { > + > +template<typename T> > +struct reverse_adapter { > + T &iterable; > +}; > + > +template<typename T> > +auto begin(reverse_adapter<T> r) > +{ > + return std::rbegin(r.iterable); > +} > + > +template<typename T> > +auto end(reverse_adapter<T> r) > +{ > + return std::rend(r.iterable); > +} > + > +} /* namespace details */ > + > +template<typename T> > +details::reverse_adapter<T> reverse(T &&iterable) > +{ > + return { iterable }; > +} > + Clever!! I thought we could get away with reverse_iterator, but mixin it range-based loops is cumbersome. This feels very natural on the contrary Reviewed-by: Jacopo Mondi <jacopo@jmondi.org> Thanks j > } /* namespace utils */ > > } /* namespace libcamera */ > diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp > index e90375ae115c..c4098a74e0ab 100644 > --- a/src/libcamera/utils.cpp > +++ b/src/libcamera/utils.cpp > @@ -464,6 +464,14 @@ std::string libcameraSourcePath() > * \return The value rounded up to the nearest multiple of \a alignment > */ > > +/** > + * \fn reverse(T &&iterable) > + * \brief Wrap an iterable to reverse iteration in a range-based loop > + * \param[in] iterable The iterable > + * \return A value of unspecified type that, when used in a range-based for > + * loop, will cause the loop to iterate over the \a iterable in reverse order > + */ > + > } /* namespace utils */ > > } /* namespace libcamera */ > -- > Regards, > > Laurent Pinchart > > _______________________________________________ > libcamera-devel mailing list > libcamera-devel@lists.libcamera.org > https://lists.libcamera.org/listinfo/libcamera-devel
diff --git a/include/libcamera/internal/utils.h b/include/libcamera/internal/utils.h index f08134afb5ba..d0146b71727d 100644 --- a/include/libcamera/internal/utils.h +++ b/include/libcamera/internal/utils.h @@ -203,6 +203,33 @@ constexpr unsigned int alignUp(unsigned int value, unsigned int alignment) return (value + alignment - 1) / alignment * alignment; } +namespace details { + +template<typename T> +struct reverse_adapter { + T &iterable; +}; + +template<typename T> +auto begin(reverse_adapter<T> r) +{ + return std::rbegin(r.iterable); +} + +template<typename T> +auto end(reverse_adapter<T> r) +{ + return std::rend(r.iterable); +} + +} /* namespace details */ + +template<typename T> +details::reverse_adapter<T> reverse(T &&iterable) +{ + return { iterable }; +} + } /* namespace utils */ } /* namespace libcamera */ diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp index e90375ae115c..c4098a74e0ab 100644 --- a/src/libcamera/utils.cpp +++ b/src/libcamera/utils.cpp @@ -464,6 +464,14 @@ std::string libcameraSourcePath() * \return The value rounded up to the nearest multiple of \a alignment */ +/** + * \fn reverse(T &&iterable) + * \brief Wrap an iterable to reverse iteration in a range-based loop + * \param[in] iterable The iterable + * \return A value of unspecified type that, when used in a range-based for + * loop, will cause the loop to iterate over the \a iterable in reverse order + */ + } /* namespace utils */ } /* namespace libcamera */
Add a utils::reverse() function that creates an adapter wrapping in iterable, such that range-based iteration over the adapter iterates over the iterable in reverse order. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> --- include/libcamera/internal/utils.h | 27 +++++++++++++++++++++++++++ src/libcamera/utils.cpp | 8 ++++++++ 2 files changed, 35 insertions(+) This will be used in an upcoming patch series, I'm already sending it out to get the idea reviewed.