Containers.hpp
Go to the documentation of this file.
1 // @formatter:off
2 //
3 // Balau core C++ library
4 //
5 // Copyright (C) 2008 Bora Software (contact@borasoftware.com)
6 //
7 // Licensed under the Boost Software License - Version 1.0 - August 17th, 2003.
8 // See the LICENSE file for the full license text.
9 //
10 
16 
17 #ifndef COM_BORA_SOFTWARE__BALAU_UTIL__CONTAINERS
18 #define COM_BORA_SOFTWARE__BALAU_UTIL__CONTAINERS
19 
20 #include <Balau/Type/ToString.hpp>
21 
22 #include <cstring>
23 #include <memory>
24 #include <vector>
25 
26 namespace Balau::Util {
27 
31 struct Containers final {
33 
37  template <template <typename ...> class DstT, typename ... D, template <typename ...> class SrcT, typename ... S, template <typename ...> class ... SrcMT, typename ... SM>
38  static void append(DstT<D ...> & dst, const SrcT<S ...> & src, const SrcMT<SM ...> & ... srcMore) {
39  dst.insert(std::end(dst), std::begin(src), std::end(src));
40  append(dst, srcMore ...);
41  }
42 
46  template <template <typename ...> class DstT, typename ... D, template <typename ...> class SrcT, typename ... S>
47  static void append(DstT<D ...> & dst, const SrcT<S ...> & src) {
48  dst.insert(std::end(dst), std::begin(src), std::end(src));
49  }
50 
54  template <template <typename ...> class ContainerT, typename ... C, template <typename ...> class ... ContainerMT, typename ... M>
55  static ContainerT<C ...> concatenate(const ContainerT<C ...> & c, const ContainerMT<M ...> & ... cMore) {
56  ContainerT<C ...> dst;
57  append(dst, c, cMore ...);
58  return dst;
59  }
60 
64  template <typename D, typename S, template <typename ...> class ContainerT, typename ... DC, typename ... SC>
65  static ContainerT<D, DC ...> map(const ContainerT<S, SC ...> & input, std::function<D (const S &)> f) {
66  ContainerT<D, DC ...> ret;
67  std::transform(input.begin(), input.end(), std::back_inserter(ret), f);
68  return ret;
69  }
70 
74  template <template <typename ...> class C, typename ... T, typename U>
75  static bool contains(const C<T ...> & container, const U & value) {
76  return std::find(container.begin(), container.end(), value) != container.end();
77  }
78 
82  template <template <typename ...> class C, typename ... T, template <typename ...> class V, typename ... U>
83  static bool contains(const C<T ...> & container, const V<U ...> & values) {
84  for (const auto & value : values) {
85  if (std::find(container.begin(), container.end(), value) != container.end()) {
86  return true;
87  }
88  }
89 
90  return false;
91  }
92 
94 
95  Containers() = delete;
96  Containers(const Containers &) = delete;
97  Containers & operator = (const Containers &) = delete;
98 };
99 
100 } // namespace Balau::Util
101 
102 #endif // COM_BORA_SOFTWARE__BALAU_UTIL__CONTAINERS
static void append(DstT< D ... > &dst, const SrcT< S ... > &src, const SrcMT< SM ... > &... srcMore)
Appends the source containers to the destination container.
Definition: Containers.hpp:38
Utilities for containers.
Definition: Containers.hpp:31
Pre-defined universal to-string functions.
Utility functions.
static bool contains(const C< T ... > &container, const U &value)
Does the container contain the supplied value?
Definition: Containers.hpp:75
static ContainerT< D, DC ... > map(const ContainerT< S, SC ... > &input, std::function< D(const S &)> f)
Perform a map operator (transform the input elements to a container of different elements).
Definition: Containers.hpp:65
static bool contains(const C< T ... > &container, const V< U ... > &values)
Does the container contain one of the supplied values?
Definition: Containers.hpp:83
static ContainerT< C ... > concatenate(const ContainerT< C ... > &c, const ContainerMT< M ... > &... cMore)
Concatenates the containers to form a new container.
Definition: Containers.hpp:55
static void append(DstT< D ... > &dst, const SrcT< S ... > &src)
Appends the source container to the destination container.
Definition: Containers.hpp:47