KSS Utility
C++ general utilities
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
containerutil.hpp
Go to the documentation of this file.
1 //
2 // containerutil.hpp
3 // kssutil
4 //
5 // Created by Steven W. Klassen on 2013-05-18.
6 // Copyright (c) 2013 Klassen Software Solutions. All rights reserved.
7 // Licensing follows the MIT License.
8 //
9 
15 #ifndef kssutil_containerutil_hpp
16 #define kssutil_containerutil_hpp
17 
18 #include <algorithm>
19 #include <functional>
20 
21 namespace kss { namespace util { namespace containers {
22 
28  template <class Container>
29  inline bool contains(const Container& c, const typename Container::key_type& a) {
30  return (c.find(a) != c.end());
31  }
32 
38  template <class Container>
39  inline bool hasAtLeast(const Container& c, typename Container::size_type sz) noexcept {
40  return (c.size() >= sz);
41  }
42 
43 
49  template <class Container>
50  inline bool isFull(const Container& c) noexcept {
51  return (c.size() == c.capacity());
52  }
53 
58  template <class Container, class UnaryPredicate>
59  void eraseIf(Container& c, UnaryPredicate pred) {
60  c.erase(std::remove_if(c.begin(), c.end(), pred), c.end());
61  }
62 
70  template <class Container, class UnaryPredicate>
71  typename Container::iterator findIf(Container& c, UnaryPredicate pred) {
72  return std::find_if(c.begin(), c.end(), pred);
73  }
74 
75  template <class Container, class UnaryPredicate>
76  typename Container::const_iterator findIf(const Container& c, UnaryPredicate pred) {
77  return std::find_if(c.begin(), c.end(), pred);
78  }
79 
85  template <class Container, class UnaryPredicate>
86  inline bool containsIf(const Container& c, UnaryPredicate pred) {
87  return (kss::util::containers::findIf(c, pred) != c.end());
88  }
89 
96  template <class Vector>
97  void apply(Vector& vec,
98  const std::function<typename Vector::value_type(size_t, const typename Vector::value_type&)>& fn)
99  {
100  const size_t len = vec.size();
101  for (size_t i(0); i < len; ++i) {
102  vec[i] = fn(i, vec[i]);
103  }
104  }
105 }}}
106 
107 #endif
void eraseIf(Container &c, UnaryPredicate pred)
void apply(Vector &vec, const std::function< typename Vector::value_type(size_t, const typename Vector::value_type &)> &fn)
bool containsIf(const Container &c, UnaryPredicate pred)
bool hasAtLeast(const Container &c, typename Container::size_type sz) noexcept
bool contains(const Container &c, const typename Container::key_type &a)
Container::iterator findIf(Container &c, UnaryPredicate pred)
bool isFull(const Container &c) noexcept