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:
parent
4070cc0d83
commit
c7d157e295
7 changed files with 107 additions and 60 deletions
|
|
@ -68,6 +68,7 @@ template <typename _Tp, typename _Dp>
|
|||
#include "lib/iter-adapter-ptr-deref.hpp"
|
||||
#include "lib/iter-adapter-stl.hpp"
|
||||
#include "lib/itertools.hpp"
|
||||
#include "lib/util-coll.hpp"
|
||||
|
||||
#include <utility>
|
||||
#include <string>
|
||||
|
|
@ -77,6 +78,7 @@ template <typename _Tp, typename _Dp>
|
|||
|
||||
using std::string;
|
||||
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)); };
|
||||
|
||||
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
|
||||
main (int, char**)
|
||||
|
|
|
|||
|
|
@ -51,6 +51,11 @@
|
|||
#include <boost/type_traits/remove_const.hpp>
|
||||
|
||||
|
||||
namespace std {
|
||||
template <typename _Tp, typename _Dp>
|
||||
class unique_ptr;
|
||||
}
|
||||
|
||||
namespace lib {
|
||||
|
||||
namespace {
|
||||
|
|
@ -72,6 +77,9 @@ namespace lib {
|
|||
template<typename T>
|
||||
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; };
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -26,8 +26,11 @@
|
|||
** collections and sequences (given by iterator). Mostly, these are tiny bits of
|
||||
** existing functionality, just packaged in a more fluent and readable way.
|
||||
** - accessors
|
||||
** - \c first() to get the first element
|
||||
** - \c last() to access the last element
|
||||
** - util::first() to get the first 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
|
||||
**
|
||||
|
|
@ -43,6 +46,8 @@
|
|||
#include "lib/util.hpp"
|
||||
#include "lib/meta/trait.hpp"
|
||||
|
||||
#include <limits>
|
||||
|
||||
|
||||
|
||||
namespace util {
|
||||
|
|
@ -163,15 +168,54 @@ namespace util {
|
|||
|
||||
/* === generic container helpers === */
|
||||
|
||||
struct WeakPtrComparator
|
||||
{
|
||||
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();
|
||||
}
|
||||
};
|
||||
template<class IT>
|
||||
inline auto
|
||||
max (IT&& elms)
|
||||
{
|
||||
using Val = typename std::remove_reference_t<IT>::value_type;
|
||||
Val res = std::numeric_limits<Val>::min();
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@
|
|||
|
||||
/** @file timeline-panel-obsolete.hpp
|
||||
** This file contains the definition of the timeline panel
|
||||
** @deprecated rework of timeline widget iminent
|
||||
** @deprecated rework of timeline widget imminent
|
||||
*/
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -92,6 +92,7 @@
|
|||
#include "stage/timeline/track-body.hpp"
|
||||
#include "lib/iter-adapter-ptr-deref.hpp"
|
||||
#include "lib/iter-adapter-stl.hpp"
|
||||
#include "lib/util-coll.hpp"
|
||||
#include "lib/itertools.hpp"
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////TICKET #1201 : test/code... remove this
|
||||
#include "lib/format-cout.hpp"
|
||||
|
|
@ -359,39 +360,18 @@ namespace timeline {
|
|||
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>
|
||||
inline void
|
||||
DisplayFrame::establishExtension (CLPS clips, MRKS markers)
|
||||
{
|
||||
// int maxVSize = max (lib::transformIterator(clips,
|
||||
// [](ClipPresenter const& clip)
|
||||
// {
|
||||
// return clip.determineRequiredVerticalExtension();
|
||||
// }));
|
||||
int maxVSize = util::max (lib::transformIterator(clips,
|
||||
[](ClipPresenter const& clip)
|
||||
{
|
||||
return clip.determineRequiredVerticalExtension();
|
||||
}));
|
||||
int headSize = this->head_.get_height();
|
||||
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>*, ...
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -37,6 +37,18 @@
|
|||
#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 model {
|
||||
|
|
|
|||
|
|
@ -98,6 +98,9 @@ namespace test {
|
|||
|
||||
verify_accessFirstLast (container, 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
|
||||
verify_typeDetectors()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue