Library: integrate generic min/max function

...built while investigating type deduction problems on PtrDerefIter
...also allow PtrDerefIter to work with std::unique_ptr
This commit is contained in:
Fischlurch 2020-03-08 02:05:39 +01:00
parent 4070cc0d83
commit c7d157e295
7 changed files with 107 additions and 60 deletions

View file

@ -68,6 +68,7 @@ template <typename _Tp, typename _Dp>
#include "lib/iter-adapter-ptr-deref.hpp" #include "lib/iter-adapter-ptr-deref.hpp"
#include "lib/iter-adapter-stl.hpp" #include "lib/iter-adapter-stl.hpp"
#include "lib/itertools.hpp" #include "lib/itertools.hpp"
#include "lib/util-coll.hpp"
#include <utility> #include <utility>
#include <string> #include <string>
@ -77,6 +78,7 @@ template <typename _Tp, typename _Dp>
using std::string; using std::string;
using std::make_unique; using std::make_unique;
using util::max;
@ -90,27 +92,6 @@ using Strs = std::vector<PStr>;
constexpr auto elems = [](auto& coll) { return lib::ptrDeref (lib::iter_stl::eachElm (coll)); }; constexpr auto elems = [](auto& coll) { return lib::ptrDeref (lib::iter_stl::eachElm (coll)); };
namespace lib {
namespace {
template<typename T, typename D>
struct RemovePtr<std::unique_ptr<T,D>> { typedef T Type; };
}
}
namespace {
template<class IT>
inline auto
max (IT&& elms)
{
using Val = std::remove_reference_t<typename IT::value_type>;
Val res = std::numeric_limits<Val>::min();
for (auto& elm : std::forward<IT> (elms))
if (elm > res)
res = elm;
return res;
}
}
int int
main (int, char**) main (int, char**)

View file

@ -51,6 +51,11 @@
#include <boost/type_traits/remove_const.hpp> #include <boost/type_traits/remove_const.hpp>
namespace std {
template <typename _Tp, typename _Dp>
class unique_ptr;
}
namespace lib { namespace lib {
namespace { namespace {
@ -72,6 +77,9 @@ namespace lib {
template<typename T> template<typename T>
struct RemovePtr<const T* const> { typedef const T Type; }; struct RemovePtr<const T* const> { typedef const T Type; };
/** allow automatic dereferencing of std::unique_ptr */
template<typename T, typename D>
struct RemovePtr<std::unique_ptr<T,D>> { typedef T Type; };
} }

View file

@ -26,8 +26,11 @@
** collections and sequences (given by iterator). Mostly, these are tiny bits of ** collections and sequences (given by iterator). Mostly, these are tiny bits of
** existing functionality, just packaged in a more fluent and readable way. ** existing functionality, just packaged in a more fluent and readable way.
** - accessors ** - accessors
** - \c first() to get the first element ** - util::first() to get the first element
** - \c last() to access the last element ** - util::last() to access the last element
** - aggregate functions
** - util::max() compute the maximum of comparable numbers
** - util::min()
** **
** @warning some functions only available when including itertools.hpp beforehand ** @warning some functions only available when including itertools.hpp beforehand
** **
@ -43,6 +46,8 @@
#include "lib/util.hpp" #include "lib/util.hpp"
#include "lib/meta/trait.hpp" #include "lib/meta/trait.hpp"
#include <limits>
namespace util { namespace util {
@ -157,21 +162,60 @@ namespace util {
__ensure_nonempty(ii); __ensure_nonempty(ii);
return lib::pull_last (ii); return lib::pull_last (ii);
} }
#endif #endif
/* === generic container helpers === */ /* === generic container helpers === */
struct WeakPtrComparator template<class IT>
{ inline auto
template<typename T> max (IT&& elms)
bool {
operator() (std::weak_ptr<T> const& l, std::weak_ptr<T> const& r) const using Val = typename std::remove_reference_t<IT>::value_type;
{ Val res = std::numeric_limits<Val>::min();
return l.lock().get() < r.lock().get(); for (auto const& elm : std::forward<IT> (elms))
} if (elm > res)
}; res = elm;
return res;
}
template<class CON>
inline auto
max (CON const& elms)
{
using Val = typename std::remove_reference_t<CON>::value_type;
Val res = std::numeric_limits<Val>::min();
for (auto const& elm : elms)
if (elm > res)
res = elm;
return res;
}
template<class IT>
inline auto
min (IT&& elms)
{
using Val = typename std::remove_reference_t<IT>::value_type;
Val res = std::numeric_limits<Val>::max();
for (auto const& elm : std::forward<IT> (elms))
if (elm < res)
res = elm;
return res;
}
template<class CON>
inline auto
min (CON const& elms)
{
using Val = typename std::remove_reference_t<CON>::value_type;
Val res = std::numeric_limits<Val>::max();
for (auto const& elm : elms)
if (elm < res)
res = elm;
return res;
}

View file

@ -22,7 +22,7 @@
/** @file timeline-panel-obsolete.hpp /** @file timeline-panel-obsolete.hpp
** This file contains the definition of the timeline panel ** This file contains the definition of the timeline panel
** @deprecated rework of timeline widget iminent ** @deprecated rework of timeline widget imminent
*/ */

View file

@ -92,6 +92,7 @@
#include "stage/timeline/track-body.hpp" #include "stage/timeline/track-body.hpp"
#include "lib/iter-adapter-ptr-deref.hpp" #include "lib/iter-adapter-ptr-deref.hpp"
#include "lib/iter-adapter-stl.hpp" #include "lib/iter-adapter-stl.hpp"
#include "lib/util-coll.hpp"
#include "lib/itertools.hpp" #include "lib/itertools.hpp"
/////////////////////////////////////////////////////////////////////////////////////////////////////////////TICKET #1201 : test/code... remove this /////////////////////////////////////////////////////////////////////////////////////////////////////////////TICKET #1201 : test/code... remove this
#include "lib/format-cout.hpp" #include "lib/format-cout.hpp"
@ -359,39 +360,18 @@ namespace timeline {
subTrack->establishLayout (displayEvaluation); subTrack->establishLayout (displayEvaluation);
} }
namespace {
template<class IT>
inline auto
max (IT&& elms)
{
using Val = typename IT::value_type;
Val res = std::numeric_limits<Val>::min();
for (auto& elm : std::forward<IT> (elms))
if (elm > res)
res = elm;
return res;
}
}
/** */ /** */
template<class CLPS, class MRKS> template<class CLPS, class MRKS>
inline void inline void
DisplayFrame::establishExtension (CLPS clips, MRKS markers) DisplayFrame::establishExtension (CLPS clips, MRKS markers)
{ {
// int maxVSize = max (lib::transformIterator(clips, int maxVSize = util::max (lib::transformIterator(clips,
// [](ClipPresenter const& clip) [](ClipPresenter const& clip)
// { {
// return clip.determineRequiredVerticalExtension(); return clip.determineRequiredVerticalExtension();
// })); }));
int headSize = this->head_.get_height(); int headSize = this->head_.get_height();
int bodySize = this->body_.calcHeight(); int bodySize = this->body_.calcHeight();
////////////////////TODO: was ist?
// typedef typename IT::value_type pointer;
// typedef typename RemovePtr<pointer>::Type value_type;
/// wobei IT = lib::RangeIter<__gnu_cxx::__normal_iterator<std::unique_ptr<stage::timeline::MarkerWidget>*, ...
} }

View file

@ -37,6 +37,18 @@
#include <boost/optional.hpp> #include <boost/optional.hpp>
namespace util { ///////////////////////////////////////////////////////////////////////////////TICKET #959 : obsolete helper utility inlined here directly
struct WeakPtrComparator ///< @deprecated unsafe and generally a bad idea
{
template<typename T>
bool
operator() (std::weak_ptr<T> const& l, std::weak_ptr<T> const& r) const
{
return l.lock().get() < r.lock().get();
}
};
} ///////////////////////////////////////////////////////////////////////////////TICKET #959 : obsolete helper utility inlined here directly
namespace stage { namespace stage {
namespace model { namespace model {

View file

@ -98,6 +98,9 @@ namespace test {
verify_accessFirstLast (container, NUM_ELMS); verify_accessFirstLast (container, NUM_ELMS);
verify_accessFirstLast (iterator, NUM_ELMS); verify_accessFirstLast (iterator, NUM_ELMS);
verify_Min_Max (container, NUM_ELMS);
verify_Min_Max (iterator, NUM_ELMS);
} }
@ -113,6 +116,25 @@ namespace test {
} }
template<class COL>
void
verify_Min_Max (COL const& col, uint lim)
{
uint expectedMax = lim;
uint expectedMin = 1;
CHECK (max (col) == expectedMax);
CHECK (min (col) == expectedMin);
COL empty;
using Val = typename COL::value_type;
CHECK (max (empty) == std::numeric_limits<Val>::min());
CHECK (min (empty) == std::numeric_limits<Val>::max());
}
void void
verify_typeDetectors() verify_typeDetectors()
{ {