|
| 1 | +// Copyright (c) 2023 The Dash Core developers |
| 2 | +// Distributed under the MIT software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#include <util/ranges_set.h> |
| 6 | + |
| 7 | +CRangesSet::Range::Range() : CRangesSet::Range::Range(0, 0) {} |
| 8 | + |
| 9 | +CRangesSet::Range::Range(uint64_t begin, uint64_t end) : |
| 10 | + begin(begin), |
| 11 | + end(end) |
| 12 | +{ |
| 13 | +} |
| 14 | + |
| 15 | +bool CRangesSet::Add(uint64_t value) |
| 16 | +{ |
| 17 | + if (Contains(value)) return false; |
| 18 | + |
| 19 | + // If element is not in CRangesSet, we need to add a new range [value, value + 1) |
| 20 | + // But this operation can cause 2 operation of merge (total 3 cases): |
| 21 | + // - if there're exist ranges [x, value) and [value + 1, y) than |
| 22 | + // all 3 of them should be merged in one range [x, y) |
| 23 | + // - if there's exist a range [x, value) - we need to replace it to new range [x, value + 1) |
| 24 | + // - if there's exist a range [value + 1, y) - we need to replace it to new range [value, y) |
| 25 | + Range new_range{value, value + 1}; |
| 26 | + auto it = ranges.lower_bound({value, value}); |
| 27 | + if (it != ranges.begin()) { |
| 28 | + auto prev = it; |
| 29 | + --prev; |
| 30 | + |
| 31 | + if (prev->end == value) { |
| 32 | + new_range.begin = prev->begin; |
| 33 | + it = ranges.erase(prev); |
| 34 | + } |
| 35 | + } |
| 36 | + const auto next = it; |
| 37 | + if (next != ranges.end()) { |
| 38 | + if (next->begin == value + 1) { |
| 39 | + new_range.end = next->end; |
| 40 | + it = ranges.erase(next); |
| 41 | + } |
| 42 | + } |
| 43 | + const auto ret = ranges.insert(new_range); |
| 44 | + assert(ret.second); |
| 45 | + return true; |
| 46 | +} |
| 47 | + |
| 48 | +bool CRangesSet::Remove(uint64_t value) |
| 49 | +{ |
| 50 | + if (!Contains(value)) return false; |
| 51 | + |
| 52 | + // If element is in CRangesSet, there's possible 2 cases: |
| 53 | + // - value in the range that is lower-bound (if begin of that range equals to `value`) |
| 54 | + // - value in the previous range (otherwise) |
| 55 | + auto it = ranges.lower_bound({value, value}); |
| 56 | + |
| 57 | + if (it == ranges.end() || it->begin != value) { |
| 58 | + assert(it != ranges.begin()); |
| 59 | + --it; |
| 60 | + } |
| 61 | + |
| 62 | + const Range current_range = *it; |
| 63 | + ranges.erase(it); |
| 64 | + if (current_range.begin != value) { |
| 65 | + const auto ret = ranges.insert({current_range.begin, value}); |
| 66 | + assert(ret.second); |
| 67 | + } |
| 68 | + if (value + 1 != current_range.end) { |
| 69 | + const auto ret = ranges.insert({value + 1, current_range.end}); |
| 70 | + assert(ret.second); |
| 71 | + } |
| 72 | + return true; |
| 73 | +} |
| 74 | + |
| 75 | +bool CRangesSet::IsEmpty() const noexcept |
| 76 | +{ |
| 77 | + return ranges.empty(); |
| 78 | +} |
| 79 | + |
| 80 | +size_t CRangesSet::Size() const noexcept |
| 81 | +{ |
| 82 | + size_t result{0}; |
| 83 | + for (auto i : ranges) { |
| 84 | + result += i.end - i.begin; |
| 85 | + } |
| 86 | + return result; |
| 87 | +} |
| 88 | + |
| 89 | +bool CRangesSet::Contains(uint64_t value) const noexcept |
| 90 | +{ |
| 91 | + const auto it = ranges.lower_bound({value, value}); |
| 92 | + if (it != ranges.end() && it->begin == value) return true; |
| 93 | + if (it == ranges.begin()) return false; |
| 94 | + auto prev = it; |
| 95 | + --prev; |
| 96 | + return prev->begin <= value && prev->end > value; |
| 97 | +} |
| 98 | + |
0 commit comments