13 #ifndef INCLUDE_MOLASSEMBLER_TEMPLE_FUNCTIONAL_H
14 #define INCLUDE_MOLASSEMBLER_TEMPLE_FUNCTIONAL_H
22 namespace Molassembler {
27 std::enable_if_t<Traits::hasSize<Container>::value,
int> = 0
29 return container.size();
34 std::enable_if_t<!Traits::hasSize<Container>::value,
int> = 0
37 std::begin(container),
71 template<
typename,
typename...>
class Container,
72 template<
typename>
class ... Dependents
74 const Container<T, Dependents<T>...>& container,
75 UnaryFunction&&
function
84 for(
const auto&
element : container) {
91 return returnContainer;
97 template<
typename ...>
class Tuplelike,
102 constexpr
auto mapTupleHelper(Tuple&& tup, Unary&& f, std::index_sequence<Inds ...> ) {
103 using ReturnType = Tuplelike<
105 Temple::invoke(f, std::declval<std::tuple_element_t<Inds, Tuple>>())
114 template<
typename ...>
class Tuplelike,
117 std::enable_if_t<!Traits::isPairlike<Tuplelike<Ts...>>::value && Traits::isTuplelike<Tuplelike<Ts...>>::value>* =
nullptr
118 > constexpr
auto map(Tuplelike<Ts...>&& tup, Unary&& f) {
119 return Detail::mapTupleHelper<Tuplelike>(
120 std::forward<Tuplelike<Ts...>>(tup),
121 std::forward<Unary>(f),
122 std::make_index_sequence<std::tuple_size<Tuplelike<Ts...>>::value> {}
130 std::enable_if_t<Traits::isPairlike<Pairlike>::value && !Traits::isTuplelike<Pairlike>::value>* =
nullptr
131 > constexpr
auto map(Pairlike&& pair, Unary&& f) {
132 return std::make_pair(
142 template<class Container, class UnaryFunction, std::enable_if_t<!Traits::isPairlike<Container>::value && !Traits::isTuplelike<Container>::value>* =
nullptr>
144 using U = decltype(
Temple::invoke(
function, *std::begin(container)));
146 std::vector<U> returnContainer;
149 for(
auto&& value : container) {
150 returnContainer.push_back(
Temple::invoke(
function, std::move(value)));
153 return returnContainer;
157 template<
class Container,
class Callable>
159 for(
const auto& value : container) {
179 typename ComparisonFunction,
180 typename MappingFunction
182 typename Container::const_iterator
select(
184 ComparisonFunction&& comparator,
185 MappingFunction&& mapFunction
187 auto selection = std::begin(container);
188 auto selectionValue = mapFunction(*selection);
190 auto iter = std::begin(container);
192 while(iter != std::end(container)) {
193 auto currentValue = mapFunction(*iter);
196 if(comparator(currentValue, selectionValue)) {
198 selectionValue = currentValue;
219 BinaryFunction&& reductionFunction
221 for(
const auto& value: container) {
233 template<
class Container,
class UnaryPredicate = Functor::Identity>
234 bool all_of(
const Container& container, UnaryPredicate&& predicate = UnaryPredicate {}) {
235 for(
const auto&
element : container) {
248 template<
class Container,
class UnaryPredicate = Functor::Identity>
249 bool any_of(
const Container& container, UnaryPredicate&& predicate = UnaryPredicate {}) {
250 for(
const auto&
element : container) {
260 template<
class Container>
263 std::begin(container),
269 template<
class Container,
typename Comparator>
272 std::begin(container),
274 std::forward<Comparator>(comparator)
279 template<
class Container>
282 std::begin(container),
288 template<
class Container,
typename T>
295 std::begin(container),
304 template<
class Container,
class UnaryFunction>
307 UnaryFunction&& predicate
311 std::begin(container),
313 std::forward<UnaryFunction>(predicate)
319 template<
class Container,
class Predicate>
323 for(
const auto& value : container) {
329 return returnContainer;
332 template<
class Container>
338 template<
class Container,
typename Comparator>
340 sort(container, std::forward<Comparator>(comparator));
345 template<
class Container,
typename T>
348 std::begin(container),
355 template<
class Container,
typename UnaryPredicate>
358 std::begin(container),
360 std::forward<UnaryPredicate>(predicate)
364 template<
class Container,
typename T>
365 std::size_t index(
const Container& container,
const T& needle) {
366 const auto iter =
find(container, needle);
367 if(iter == std::end(container)) {
368 throw std::out_of_range(
"Needle not in the haystack");
370 return iter - std::begin(container);
373 template<
class Container,
typename UnaryPredicate>
374 std::size_t index_if(
const Container& container, UnaryPredicate&& predicate) {
375 const auto iter =
find_if(container, predicate);
376 if(iter == std::end(container)) {
377 throw std::out_of_range(
"Needle not in the haystack");
379 return iter - std::begin(container);
384 std::vector<T>
iota(
unsigned upperBound) {
385 std::vector<T> values(upperBound);
397 template<
class Container>
399 using T = Traits::getValueType<Container>;
401 return [&container](
const T&
element) ->
bool {
403 std::begin(container),
406 ) != std::end(container);
412 template<
typename Transformation>
414 Map(Transformation&& f) : t(f) {}
416 template<
typename Container>
424 template<
typename Transformation>
425 auto map(Transformation&& t) {
void sort(Container &container)
Calls std::sort on a container.
Definition: Functional.h:261
PURITY_STRONG constexpr std::enable_if_t< std::is_arithmetic< T >::value, ArrayType< T, size >> iota()
Iota for any array type.
Definition: Containers.h:146
Uniform callable invoke from arguments or tuple of arguments.
void remove_if(Container &container, UnaryFunction &&predicate)
Applies erase-remove idiom in-place to container with a predicate function.
Definition: Functional.h:305
auto find_if(const Container &container, UnaryPredicate &&predicate)
std::find_if shorthand
Definition: Functional.h:356
double element(const PositionCollection &normalizedPositions, const Elements::Rotation &rotation)
Returns the CSM for a Rotation symmetry element along the rotation axis without optimizing the coordi...
unsigned size(Shape shape)
Fetch the number of vertices of a shape.
auto map_stl(const Container< T, Dependents< T >...> &container, UnaryFunction &&function)
Definition: Functional.h:73
void reserveIfPossible(TargetContainer &target, const SourceContainer &source, SizeModifierUnary &&sourceModifierUnary=SizeModifierUnary{})
Rerves the space required in the target container if size can be determined from the source container...
Definition: AddToContainer.h:201
Definition: Functional.h:413
Central interface to add elements to many types of containers.
void remove(Container &container, const T &value)
Applies erase-remove idiom in-place to container.
Definition: Functional.h:289
T accumulate(const Container &container, T init, BinaryFunction &&reductionFunction)
Accumulate shorthand.
Definition: Functional.h:216
void forEach(const Container &container, Callable &&callable)
Apply a callable for all values of a container.
Definition: Functional.h:158
void reverse(Container &container)
In-place reversal.
Definition: Functional.h:280
bool any_of(const Container &container, UnaryPredicate &&predicate=UnaryPredicate{})
any_of shorthand
Definition: Functional.h:249
constexpr auto map(const ArrayType< T, size > &array, UnaryFunction &&function)
Maps all elements of any array-like container with a unary function.
Definition: Containers.h:63
auto makeContainsPredicate(const Container &container)
Creates a predicate that uses std::find for the container's value type.
Definition: Functional.h:398
Container::const_iterator select(const Container &container, ComparisonFunction &&comparator, MappingFunction &&mapFunction)
Select a value from a container.
Definition: Functional.h:182
void addToContainer(Container &container, const T &value)
Adds an lvalue element to a container by calling the container's insert or push_back member functions...
Definition: AddToContainer.h:179
auto find(const Container &container, const T &needle)
std::find shorthand
Definition: Functional.h:346
auto invoke(Function &&function, const boost::tuples::cons< HT, TT > &tuple)
Invokes a function with all values in a given boost tuple.
Definition: Invoke.h:44
bool all_of(const Container &container, UnaryPredicate &&predicate=UnaryPredicate{})
all_of shorthand
Definition: Functional.h:234