File indexing completed on 2024-04-28 15:40:25

0001 /* SPDX-FileCopyrightText: 2013-2019 The KPhotoAlbum Development Team
0002 
0003    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 */
0005 
0006 #ifndef ALGORITHMHELPER_H
0007 #define ALGORITHMHELPER_H
0008 
0009 #include <algorithm>
0010 
0011 // The algotihms of std all takes two iterators, which makes it more verbose to use for the common case where we want the algortihm to work
0012 // on the whole container. Below is a number of utility functions which makes it easier to use those algorithms.
0013 // See http://en.cppreference.com/w/cpp/algorithm for a description of the C++ algorithmns.
0014 
0015 namespace Utilities
0016 {
0017 
0018 template <class Container, class UnaryPredicate>
0019 static bool any_of(const Container &container, UnaryPredicate p)
0020 {
0021     return std::any_of(container.begin(), container.end(), p);
0022 }
0023 
0024 template <class Container, class UnaryPredicate>
0025 static bool all_of(const Container &container, UnaryPredicate p)
0026 {
0027     return std::all_of(container.begin(), container.end(), p);
0028 }
0029 
0030 /**
0031     Sum up the elements of the container. The value of each element is extracted using fn
0032  */
0033 template <class Container, class ExtractFunction>
0034 int sum(const Container &container, ExtractFunction fn)
0035 {
0036     int res = 0;
0037     for (auto item : container) {
0038         res += fn(item);
0039     }
0040     return res;
0041 }
0042 
0043 }
0044 #endif // ALGORITHMHELPER_H