Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'udpor-phase3' into 'master'
[simgrid.git] / src / xbt / utils / iter / LazyKSubsets.hpp
1 /* Copyright (c) 2004-2023 The SimGrid Team. All rights reserved.           */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef XBT_UTILS_LAZY_KSUBSETS_HPP
7 #define XBT_UTILS_LAZY_KSUBSETS_HPP
8
9 #include "src/xbt/utils/iter/subsets.hpp"
10
11 namespace simgrid::xbt {
12
13 template <class Iterable> class LazyKSubsets;
14 template <class Iterable> LazyKSubsets<Iterable> make_k_subsets_iter(unsigned k, const Iterable& container);
15
16 /**
17  * @brief A container which "contains" all subsets of
18  * size `k` of some other container `WrapperContainer`
19  *
20  * @note: You should not store instances of this class directly,
21  * as it acts more like a simply wrapping around another iterable
22  * type to make it more convenient to iterate over subsets of
23  * some iterable type.
24  *
25  * @class Iterable: The container from which
26  * the subsets "contained" by this container are derived
27  */
28 template <class Iterable> class LazyKSubsets final {
29 public:
30   auto begin() const
31   {
32     return subsets_iterator<typename Iterable::const_iterator>(k, iterable.begin(), iterable.end());
33   }
34   auto end() const { return subsets_iterator<typename Iterable::const_iterator>(k); }
35
36 private:
37   const unsigned k;
38   const Iterable& iterable;
39   LazyKSubsets(unsigned k, const Iterable& iterable) : k(k), iterable(iterable) {}
40
41   template <class IterableType>
42   friend LazyKSubsets<IterableType> make_k_subsets_iter(unsigned k, const IterableType& iterable);
43 };
44
45 template <class Iterable> LazyKSubsets<Iterable> make_k_subsets_iter(unsigned k, const Iterable& container)
46 {
47   return LazyKSubsets<Iterable>(k, container);
48 }
49
50 } // namespace simgrid::xbt
51
52 #endif