Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
devinamatthews committed Dec 25, 2020
2 parents ba1c12c + f77c1c8 commit 5a7ad89
Show file tree
Hide file tree
Showing 10 changed files with 377 additions and 64 deletions.
5 changes: 5 additions & 0 deletions src/external/marray/include/marray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,11 @@ class marray : public marray_base<Type, NDim, marray<Type, NDim, Allocator>, tru
void resize(const detail::array_1d<len_type>& len,
const Type& val=Type())
{
std::array<len_type, NDim> new_len;
len.slurp(new_len);

if (new_len == len_) return;

marray a(std::move(*this));
reset(len, val, layout_);
marray_view<Type, NDim> b(*this);
Expand Down
76 changes: 69 additions & 7 deletions src/external/marray/include/range.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ struct underlying_type_if<T, detail::enable_if_t<std::is_enum<T>::value>>
typedef typename std::underlying_type<T>::type type;
};

template <typename... Ts> struct are_numeric;

template <> struct are_numeric<>
: std::integral_constant<bool,true> {};

template <typename T, typename... Ts> struct are_numeric<T, Ts...>
: std::integral_constant<bool, (std::is_integral<T>::value ||
std::is_enum<T>::value) &&
are_numeric<Ts...>::value> {};

template <typename... Ts> using enable_if_numeric =
std::enable_if_t<are_numeric<Ts...>::value>;

}

template <typename T>
Expand Down Expand Up @@ -228,24 +241,69 @@ class range_t
{
return U(begin(), end());
}

range_t& operator+=(T shift)
{
from_ += shift;
to_ += shift;
return *this;
}

range_t& operator-=(T shift)
{
from_ -= shift;
to_ -= shift;
return *this;
}

range_t operator+(T shift)
{
range_t shifted(*this);
shifted += shift;
return shifted;
}

range_t operator-(T shift)
{
range_t shifted(*this);
shifted -= shift;
return shifted;
}

friend range_t operator+(T shift, const range_t& other)
{
return other + shift;
}

friend range_t operator-(T shift, const range_t& other)
{
range_t shifted(other);
shifted.from_ = shift - shifted.from_;
shifted.to_ -= shift - shifted.to_;
shifted.delta_ = -shifted.delta_;
return shifted;
}
};

template <typename T>
template <typename T, typename=
detail::enable_if_numeric<T>>
auto range(T to)
{
typedef typename detail::underlying_type_if<T>::type U;
return range_t<U>{U(to)};
}

template <typename T, typename U>
template <typename T, typename U, typename=
detail::enable_if_numeric<T,U>>
auto rangeN(T from, U N)
{
typedef decltype(std::declval<T>() + std::declval<U>()) V0;
typedef typename detail::underlying_type_if<V0>::type V;
return range_t<V>{V(from), V(from+N)};
}

template <typename T, typename U>
template <typename T, typename U, typename=
detail::enable_if_numeric<T,U>>
auto range(T from, U to)
{
typedef decltype(std::declval<T>() + std::declval<U>()) V0;
Expand All @@ -255,7 +313,8 @@ auto range(T from, U to)
return range_t<V>{(V)from, (V)to};
}

template <typename T, typename U, typename V>
template <typename T, typename U, typename V, typename=
detail::enable_if_numeric<T,U,V>>
auto range(T from, U to, V delta)
{
typedef decltype(std::declval<T>() + std::declval<U>() + std::declval<V>()) W0;
Expand All @@ -266,19 +325,22 @@ auto range(T from, U to, V delta)
return range_t<W>{(W)from, (W)to, (W)delta};
}

template <typename T>
template <typename T, typename=
detail::enable_if_numeric<T>>
auto reversed_range(T to)
{
return range(to-1, -1, -1);
}

template <typename T, typename U>
template <typename T, typename U, typename=
detail::enable_if_numeric<T,U>>
auto reversed_rangeN(T from, U N)
{
return range(from-1, from-N-1, -1);
}

template <typename T, typename U>
template <typename T, typename U, typename=
detail::enable_if_numeric<T,U>>
auto reversed_range(T from, U to)
{
return range(to-1, from-1, -1);
Expand Down
5 changes: 5 additions & 0 deletions src/external/marray/include/varray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@ class varray : public varray_base<Type, varray<Type, Allocator>, true>
{
MARRAY_ASSERT(len.size() == dimension());

len_vector new_len;
len.slurp(new_len);

if (new_len == len_) return;

varray a(std::move(*this));
reset(len, val, layout_);
auto b = view();
Expand Down
85 changes: 85 additions & 0 deletions src/external/stl_ext/include/algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,50 @@ typename T::value_type min(const T& t)
return v;
}

template <typename T>
size_t max_pos(const T& t)
{
typedef typename T::value_type V;

if (t.empty()) return 0;

size_t pos = 0;
typename T::const_iterator i = t.begin();
V v = *i;
for (size_t j = 0;i != t.end();++i,++j)
{
if (v < *i)
{
v = *i;
pos = j;
}
}

return pos;
}

template <typename T>
size_t min_pos(const T& t)
{
typedef typename T::value_type V;

if (t.empty()) return 0;

size_t pos = 0;
typename T::const_iterator i = t.begin();
V v = *i;
for (size_t j = 0;i != t.end();++i,++j)
{
if (*i < v)
{
v = *i;
pos = j;
}
}

return pos;
}

template <typename T, typename Functor>
enable_if_not_same_t<typename T::value_type,Functor,T&>
erase(T& v, const Functor& f)
Expand All @@ -99,6 +143,18 @@ T& erase(T& v, const typename T::value_type& e)
return v;
}

inline std::string& erase(std::string& v, const std::string& e)
{
for (auto c : e) erase(v, c);
return v;
}

inline std::string& erase(std::string& v, const char* e)
{
while (*e) erase(v, *e++);
return v;
}

template <typename T, typename Functor>
enable_if_not_same_t<typename T::value_type,Functor,T>
erased(T v, const Functor& x)
Expand All @@ -114,6 +170,18 @@ T erased(T v, const typename T::value_type& e)
return v;
}

inline std::string erased(std::string v, const std::string& e)
{
erase(v, e);
return v;
}

inline std::string erased(std::string v, const char* e)
{
erase(v, e);
return v;
}

template <typename T, class Predicate>
T& filter(T& v, Predicate pred)
{
Expand Down Expand Up @@ -229,6 +297,12 @@ bool contains(const T& v, const U& e)
return find(v, e) != v.end();
}

template <typename T, typename U>
auto count(const T& v, const U& e)
{
return std::count(v.begin(), v.end(), e);
}

template <typename T, typename Predicate>
bool matches(const T& v, Predicate&& pred)
{
Expand Down Expand Up @@ -620,6 +694,17 @@ T appended(T t, U&&... u)
return t;
}

template <typename Functor, typename T>
auto map(Functor&& func, const T& v)
{
typedef std::decay_t<decltype(*v.begin())> R;
typedef std::decay_t<decltype(func(*v.begin()))> S;
typedef std::conditional_t<std::is_same<R,S>::value,T,std::vector<R>> U;
U v2; v2.reserve(v.size());
for (auto& e : v) v2.push_back(func(e));
return v2;
}

}

#endif
33 changes: 33 additions & 0 deletions src/external/stl_ext/include/array.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef _STL_EXT_ARRAY_HPP_
#define _STL_EXT_ARRAY_HPP_

#include <array>

#include "type_traits.hpp"

namespace stl_ext
{

namespace detail
{

template <typename T, size_t N, size_t... M>
struct array_helper
{
typedef std::array<typename array_helper<T,M...>::type,N> type;
};

template <typename T, size_t N>
struct array_helper<T,N>
{
typedef std::array<T,N> type;
};

}

template <typename T, size_t... N>
using array = typename detail::array_helper<T,N...>::type;

}

#endif
5 changes: 5 additions & 0 deletions src/external/stl_ext/include/iostream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <tuple>
#include <vector>
#include <stdexcept>
#include <cstring>

#include "complex.hpp"
#include "type_traits.hpp"
Expand Down Expand Up @@ -543,6 +544,8 @@ std::ostream& operator<<(std::ostream& os, const T v[N])

}

#if 0

namespace stl_ext
{

Expand Down Expand Up @@ -619,3 +622,5 @@ detail::sigfig_printer<T> printToAccuracy(const T& value, double accuracy)
}

#endif

#endif
55 changes: 55 additions & 0 deletions src/external/stl_ext/include/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <string>
#include <sstream>
#include <utility>
#include <vector>

#include <iostream>

Expand Down Expand Up @@ -102,6 +103,60 @@ inline string tolower(const string& S)
return s;
}

inline std::string trim(const std::string& s)
{
auto begin = s.find_first_not_of(" \n\r\t");
auto end = s.find_last_not_of(" \n\r\t");

if (begin == s.npos) return "";
else return s.substr(begin, end-begin+1);
}

inline std::vector<std::string> split(const std::string& s,
const std::string& sep = "",
int max_split = -1)
{
std::vector<std::string> tokens;

if (sep == "")
{
std::istringstream iss(s);
std::string token;
for (auto i = 0;(i < max_split || max_split == -1) && (iss >> token);i++)
tokens.push_back(token);

token.clear();
char c;
while (iss.get(c)) token.push_back(c);
if (!token.empty()) tokens.push_back(token);
}
else
{
auto begin = 0;
for (auto i = 0;i < max_split || max_split == -1;i++)
{
auto end = s.find(sep, begin);

if (end == s.npos)
{
tokens.push_back(s.substr(begin));
begin = end;
break;
}
else
{
tokens.push_back(s.substr(begin, end-begin+1));
begin = end+1;
}
}

if (begin != s.npos)
tokens.push_back(s.substr(begin));
}

return tokens;
}

}

#endif
Loading

0 comments on commit 5a7ad89

Please sign in to comment.