From: Philippe Proulx Date: Fri, 3 Nov 2023 15:44:35 +0000 (+0000) Subject: `ctf` plugin: add `ctf::IntRangeSet` class template X-Git-Url: http://drtracing.org/?a=commitdiff_plain;h=ced0b7fde9652d643711c3e9641775b76a73f280;p=babeltrace.git `ctf` plugin: add `ctf::IntRangeSet` class template 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 Change-Id: I494dd1b236463eaf7c24d1143ed21afdd5e498b0 Reviewed-on: https://review.lttng.org/c/babeltrace/+/7923 Reviewed-by: Francis Deslauriers Reviewed-on: https://review.lttng.org/c/babeltrace/+/12252 --- diff --git a/src/Makefile.am b/src/Makefile.am index 1034a7ac..1cbcaf7f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 index 00000000..0074041a --- /dev/null +++ b/src/plugins/ctf/common/metadata/int-range-set.hpp @@ -0,0 +1,129 @@ +/* + * SPDX-License-Identifier: MIT + * + * Copyright 2022-2024 Philippe Proulx + */ + +#ifndef BABELTRACE_PLUGINS_CTF_COMMON_METADATA_INT_RANGE_SET_HPP +#define BABELTRACE_PLUGINS_CTF_COMMON_METADATA_INT_RANGE_SET_HPP + +#include +#include + +#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 +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; + + /* Type of the set containing the ranges */ + using Set = std::set; + +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; +using SIntRangeSet = IntRangeSet; + +} /* namespace ctf */ + +#endif /* BABELTRACE_PLUGINS_CTF_COMMON_METADATA_INT_RANGE_SET_HPP */