`ctf` plugin: add `ctf::IntRangeSet` class template
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Fri, 3 Nov 2023 15:44:35 +0000 (15:44 +0000)
committerSimon Marchi <simon.marchi@efficios.com>
Wed, 4 Sep 2024 19:05:14 +0000 (15:05 -0400)
This new class template is conceptually the same as the libbabeltrace2
equivalent (`bt2::CommonIntegerRangeSet`), but it's templated and avoids
having to make libbabeltrace2 function calls to access data.

That being said, the contains() and intersects() methods don't have
their libbabeltrace2 equivalent.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I494dd1b236463eaf7c24d1143ed21afdd5e498b0
Reviewed-on: https://review.lttng.org/c/babeltrace/+/7923
Reviewed-by: Francis Deslauriers <francis.deslauriers@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/12252

src/Makefile.am
src/plugins/ctf/common/metadata/int-range-set.hpp [new file with mode: 0644]

index 1034a7acc0571680e81be4f6419ffe3797029078..1cbcaf7f6d74b836a669816ddb495a5df301ae04 100644 (file)
@@ -677,6 +677,7 @@ endif
 # ctf plugin
 plugins_ctf_babeltrace_plugin_ctf_la_SOURCES = \
        plugins/ctf/common/metadata/int-range.hpp \
+       plugins/ctf/common/metadata/int-range-set.hpp \
        plugins/ctf/common/src/bfcr/bfcr.cpp \
        plugins/ctf/common/src/bfcr/bfcr.hpp \
        plugins/ctf/common/src/clk-cls-cfg.hpp \
diff --git a/src/plugins/ctf/common/metadata/int-range-set.hpp b/src/plugins/ctf/common/metadata/int-range-set.hpp
new file mode 100644 (file)
index 0000000..0074041
--- /dev/null
@@ -0,0 +1,129 @@
+/*
+ * SPDX-License-Identifier: MIT
+ *
+ * Copyright 2022-2024 Philippe Proulx <pproulx@efficios.com>
+ */
+
+#ifndef BABELTRACE_PLUGINS_CTF_COMMON_METADATA_INT_RANGE_SET_HPP
+#define BABELTRACE_PLUGINS_CTF_COMMON_METADATA_INT_RANGE_SET_HPP
+
+#include <set>
+#include <utility>
+
+#include "common/assert.h"
+
+#include "int-range.hpp"
+
+namespace ctf {
+
+/*
+ * An integer range set is a set of integer ranges of which the values
+ * are of type `ValT`.
+ */
+template <typename ValT>
+class IntRangeSet final
+{
+public:
+    /* Type of the lower and upper values of contained integer ranges */
+    using Val = ValT;
+
+    /* Type of contained integer ranges */
+    using Range = IntRange<ValT>;
+
+    /* Type of the set containing the ranges */
+    using Set = std::set<Range>;
+
+public:
+    /*
+     * Builds an empty integer range set.
+     */
+    explicit IntRangeSet()
+    {
+    }
+
+    /*
+     * Builds an integer range set containing the integer ranges
+     * `ranges`.
+     */
+    explicit IntRangeSet(Set ranges) : _mRanges {std::move(ranges)}
+    {
+    }
+
+    /*
+     * Ranges of this integer range set.
+     */
+    const Set& ranges() const noexcept
+    {
+        return _mRanges;
+    }
+
+    /*
+     * Range set iterator at the first range of this set.
+     */
+    typename Set::const_iterator begin() const noexcept
+    {
+        return _mRanges.begin();
+    }
+
+    /*
+     * Range set iterator \em after the last range of this set.
+     */
+    typename Set::const_iterator end() const noexcept
+    {
+        return _mRanges.end();
+    }
+
+    /*
+     * Returns whether or not this integer range set contains the
+     * value `val` in at least one of its ranges.
+     */
+    bool contains(const Val val) const noexcept
+    {
+        for (auto& range : _mRanges) {
+            if (range.contains(val)) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+    /*
+     * Returns whether or not at least one range contained in `other`
+     * intersects with at least one range contained in this integer
+     * range set.
+     */
+    bool intersects(const IntRangeSet& other) const noexcept
+    {
+        for (auto& range : _mRanges) {
+            for (auto& otherRange : other.ranges()) {
+                if (range.intersects(otherRange)) {
+                    return true;
+                }
+            }
+        }
+
+        return false;
+    }
+
+    bool operator==(const IntRangeSet& other) const
+    {
+        return other.ranges() == _mRanges;
+    }
+
+    bool operator!=(const IntRangeSet& other) const
+    {
+        return !(*this == other);
+    }
+
+private:
+    Set _mRanges;
+};
+
+/* Convenient aliases */
+using UIntRangeSet = IntRangeSet<unsigned long long>;
+using SIntRangeSet = IntRangeSet<long long>;
+
+} /* namespace ctf */
+
+#endif /* BABELTRACE_PLUGINS_CTF_COMMON_METADATA_INT_RANGE_SET_HPP */
This page took 0.025897 seconds and 4 git commands to generate.