Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add LazyPowerSet and LazyKSubsets
authorMaxwell Pirtle <maxwellpirtle@gmail.com>
Tue, 28 Feb 2023 10:30:28 +0000 (11:30 +0100)
committerMaxwell Pirtle <maxwellpirtle@gmail.com>
Tue, 28 Feb 2023 10:30:28 +0000 (11:30 +0100)
Two new classes were added that act effectively
as convenience methods around creating subset
iterators. The classes themselves act mostly
like iterators but are instead light-weight
"containers" which can be iterated over. Effectively,
they are contains which "contain" the entire
collection of subsets (of size k respectively) of
some collection without actually storing that collection
in memory.

MANIFEST.in
src/xbt/utils/iter/LazyKSubsets.hpp [new file with mode: 0644]
src/xbt/utils/iter/LazyPowerset.hpp [new file with mode: 0644]
tools/cmake/DefinePackages.cmake

index 3d886a8..3f30851 100644 (file)
@@ -2520,6 +2520,9 @@ include src/xbt/snprintf.c
 include src/xbt/string.cpp
 include src/xbt/unit-tests_main.cpp
 include src/xbt/utils/iter/subsets.hpp
+include src/xbt/utils/iter/powerset.hpp
+include src/xbt/utils/iter/LazyKSubsets.hpp
+include src/xbt/utils/iter/LazyPowerset.hpp
 include src/xbt/xbt_log_appender_file.cpp
 include src/xbt/xbt_log_layout_format.cpp
 include src/xbt/xbt_log_layout_simple.cpp
diff --git a/src/xbt/utils/iter/LazyKSubsets.hpp b/src/xbt/utils/iter/LazyKSubsets.hpp
new file mode 100644 (file)
index 0000000..0164e0d
--- /dev/null
@@ -0,0 +1,47 @@
+/* Copyright (c) 2004-2023 The SimGrid Team. All rights reserved.           */
+
+/* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. */
+
+#ifndef XBT_UTILS_LAZY_KSUBSETS_HPP
+#define XBT_UTILS_LAZY_KSUBSETS_HPP
+
+#include "src/xbt/utils/iter/subsets.hpp"
+
+namespace simgrid::xbt {
+
+template <class Iterable> class LazyKSubsets<Iterable>;
+template <class Iterable> LazyKSubsets<Iterable> make_k_subsets_iter(unsigned k, const Iterable& container);
+
+/**
+ * @brief A container which "contains" all subsets of
+ * size `k` of some other container `WrapperContainer`
+ *
+ * @note: You should not store instances of this class directly,
+ * as it acts more like a simply wrapping around another iterable
+ * type to make it more convenient to iterate over subsets of
+ * some iterable type.
+ *
+ * @class Iterable: The container from which
+ * the subsets "contained" by this container are derived
+ */
+template <class Iterable> class LazyKSubsets final {
+public:
+  auto begin() const { return subsets_iterator<Iterable::iterator>(k, iterable.begin(), iterable.end()); }
+  auto end() const { return subsets_iterator<Iterable::iterator>(k); }
+
+private:
+  const unsigned k;
+  const Iterable& iterable;
+  LazyKSubsets(unsigned k, const Iterable& iterable) : k(k), iterable(iterable) {}
+  friend LazyKSubsets<Iterable> make_k_subsets_iter(unsigned k, const Iterable& iterable);
+};
+
+template <class Iterable> LazyKSubsets<Iterable> make_k_subsets_iter(unsigned k, const Iterable& container)
+{
+  return LazyKSubsets<Iterable>(k, container);
+}
+
+} // namespace simgrid::xbt
+
+#endif
diff --git a/src/xbt/utils/iter/LazyPowerset.hpp b/src/xbt/utils/iter/LazyPowerset.hpp
new file mode 100644 (file)
index 0000000..cb74844
--- /dev/null
@@ -0,0 +1,46 @@
+/* Copyright (c) 2004-2023 The SimGrid Team. All rights reserved.           */
+
+/* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. */
+
+#ifndef XBT_UTILS_LAZY_POWER_SET_HPP
+#define XBT_UTILS_LAZY_POWER_SET_HPP
+
+#include "src/xbt/utils/iter/powerset.hpp"
+
+namespace simgrid::xbt {
+
+template <class Iterable> class LazyPowerset<Iterable>;
+template <class Iterable> LazyPowerset<Iterable> make_powerset_iter(const Iterable& container);
+
+/**
+ * @brief A container which "contains" all subsets of
+ * size `k` of some other container `WrapperContainer`
+ *
+ * @note: You should not store instances of this class directly,
+ * as it acts more like a simply wrapping around another iterable
+ * type to make it more convenient to iterate over subsets of
+ * some iterable type.
+ *
+ * @class Iterable: The container from which
+ * the subsets "contained" by this container are derived
+ */
+template <class Iterable> class LazyPowerset final {
+public:
+  auto begin() const { return powerset_iterator<Iterable::iterator>(iterable.begin(), iterable.end()); }
+  auto end() const { return powerset_iterator<Iterable::iterator>(); }
+
+private:
+  const Iterable& iterable;
+  LazyPowerset(const Iterable& iterable) : iterable(iterable) {}
+  friend LazyPowerset<Iterable> make_powerset_iter(const Iterable& iterable);
+};
+
+template <class Iterable> LazyPowerset<Iterable> make_powerset_iter(const Iterable& container)
+{
+  return LazyPowerset<Iterable>(container);
+}
+
+} // namespace simgrid::xbt
+
+#endif
index 6fb642c..60745e1 100644 (file)
@@ -283,6 +283,10 @@ set(XBT_SRC
   src/xbt/xbt_parse_units.cpp
   src/xbt/xbt_replay.cpp
   src/xbt/xbt_str.cpp
+  src/xbt/utils/iter/subsets.hpp
+  src/xbt/utils/iter/powerset.hpp
+  src/xbt/utils/iter/LazyKSubsets.hpp
+  src/xbt/utils/iter/LazyPowerset.hpp
   )
 
 if(HAVE_MMALLOC)