Move RangeSet from Externals to Common

This is a very small libary, and as I understand it, it was more or less
developed for Dolphin.

This moves the two relevant files from Externals to Common, changes the
namespace to Common, reformats the code, and adds Dolphin copyright
notices. The change in copyright notice and license was approved by
AdmiralCurtiss.
This commit is contained in:
JosJuice 2026-02-18 20:02:54 +01:00
parent ffa03fec78
commit 36f45dce44
12 changed files with 323 additions and 314 deletions

View File

@ -761,8 +761,6 @@ include_directories(Externals/picojson)
add_subdirectory(Externals/expr)
add_subdirectory(Externals/rangeset)
add_subdirectory(Externals/FatFs)
if (USE_RETRO_ACHIEVEMENTS)

View File

@ -64,8 +64,6 @@ Dolphin includes or links code of the following third-party software projects:
[LGPLv2.1+](http://cgit.freedesktop.org/pulseaudio/pulseaudio/tree/LICENSE)
- [Qt5](http://qt-project.org/):
[LGPLv3 and other licenses](http://doc.qt.io/qt-5/licensing.html)
- [rangeset](https://github.com/AdmiralCurtiss/rangeset)
[zlib license](https://github.com/AdmiralCurtiss/rangeset/blob/master/LICENSE)
- [SDL](https://www.libsdl.org/):
[zlib license](http://hg.libsdl.org/SDL/file/tip/COPYING.txt)
- [SFML](http://www.sfml-dev.org/):

View File

@ -1,4 +0,0 @@
add_library(RangeSet::RangeSet INTERFACE IMPORTED GLOBAL)
set_target_properties(RangeSet::RangeSet PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_LIST_DIR}/include
)

View File

@ -1,17 +0,0 @@
Copyright (c) 2020 Admiral H. Curtiss
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.

View File

@ -127,6 +127,8 @@ add_library(common
QoSSession.h
Random.cpp
Random.h
RangeSet.h
RangeSizeSet.h
ScopeGuard.h
SDCardUtil.cpp
SDCardUtil.h

View File

@ -1,3 +1,6 @@
// Copyright 2020 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <cassert>
@ -5,51 +8,51 @@
#include <map>
#include <utility>
namespace HyoutaUtilities {
template <typename T> class RangeSet {
namespace Common
{
template <typename T>
class RangeSet
{
private:
using MapT = std::map<T, T>;
public:
struct const_iterator {
struct const_iterator
{
public:
const T& from() const {
return It->first;
}
const T& from() const { return It->first; }
const T& to() const {
return It->second;
}
const T& to() const { return It->second; }
const_iterator& operator++() {
const_iterator& operator++()
{
++It;
return *this;
}
const_iterator operator++(int) {
const_iterator operator++(int)
{
const_iterator old = *this;
++It;
return old;
}
const_iterator& operator--() {
const_iterator& operator--()
{
--It;
return *this;
}
const_iterator operator--(int) {
const_iterator operator--(int)
{
const_iterator old = *this;
--It;
return old;
}
bool operator==(const const_iterator& rhs) const {
return this->It == rhs.It;
}
bool operator==(const const_iterator& rhs) const { return this->It == rhs.It; }
bool operator!=(const const_iterator& rhs) const {
return !operator==(rhs);
}
bool operator!=(const const_iterator& rhs) const { return !operator==(rhs); }
private:
typename MapT::const_iterator It;
@ -57,7 +60,8 @@ public:
friend class RangeSet;
};
void insert(T from, T to) {
void insert(T from, T to)
{
if (from >= to)
return;
@ -65,18 +69,21 @@ public:
// upper_bound() returns the closest range whose starting position
// is greater than 'from'.
auto bound = Map.upper_bound(from);
if (bound == Map.end()) {
if (bound == Map.end())
{
// There is no range that starts greater than the given one.
// This means we have three options:
// - 1. No range exists yet, this is the first range.
if (Map.empty()) {
if (Map.empty())
{
insert_range(from, to);
return;
}
// - 2. The given range does not overlap the last range.
--bound;
if (from > get_to(bound)) {
if (from > get_to(bound))
{
insert_range(from, to);
return;
}
@ -86,7 +93,8 @@ public:
return;
}
if (bound == Map.begin()) {
if (bound == Map.begin())
{
// The given range starts before any of the existing ones.
// We must insert this as a new range even if we potentially overlap
// an existing one as we can't modify a key in a std::map.
@ -101,7 +109,8 @@ public:
// could possibly be affected.
// If 'bound' overlaps with given range, update bounds object.
if (get_to(bound) >= from) {
if (get_to(bound) >= from)
{
maybe_expand_to(bound, to);
auto inserted = bound;
++bound;
@ -112,7 +121,8 @@ public:
// 'bound' *doesn't* overlap with given range, check next range.
// If this range overlaps with given range,
if (get_from(abound) <= to) {
if (get_from(abound) <= to)
{
// insert new range
auto inserted = insert_range(from, to >= get_to(abound) ? to : get_to(abound));
// and delete overlaps
@ -126,49 +136,61 @@ public:
insert_range(from, to);
}
void erase(T from, T to) {
void erase(T from, T to)
{
if (from >= to)
return;
// Like insert(), we use upper_bound to find the closest range.
auto bound = Map.upper_bound(from);
if (bound == Map.end()) {
if (bound == Map.end())
{
// There is no range that starts greater than the given one.
if (Map.empty()) {
if (Map.empty())
{
// nothing to do
return;
}
--bound;
// 'bound' now points at the last range.
if (from >= get_to(bound)) {
if (from >= get_to(bound))
{
// Given range is larger than any range that exists, nothing to do.
return;
}
if (to >= get_to(bound)) {
if (from == get_from(bound)) {
if (to >= get_to(bound))
{
if (from == get_from(bound))
{
// Given range fully overlaps last range, erase it.
erase_range(bound);
return;
} else {
}
else
{
// Given range overlaps end of last range, reduce it.
reduce_to(bound, from);
return;
}
}
if (from == get_from(bound)) {
if (from == get_from(bound))
{
// Given range overlaps begin of last range, reduce it.
reduce_from(bound, to);
return;
} else {
}
else
{
// Given range overlaps middle of last range, bisect it.
bisect_range(bound, from, to);
return;
}
}
if (bound == Map.begin()) {
if (bound == Map.begin())
{
// If we found the first range that means 'from' is before any stored range.
// This means we can just erase from start until 'to' and be done with it.
erase_from_iterator_to_value(bound, to);
@ -178,7 +200,8 @@ public:
// check previous range
auto abound = bound--;
if (from == get_from(bound)) {
if (from == get_from(bound))
{
// Similarly, if the previous range starts with the given one, just erase until 'to'.
erase_from_iterator_to_value(bound, to);
return;
@ -187,12 +210,16 @@ public:
// If we come here, the given range may or may not overlap part of the current 'bound'
// (but never the full range), which means we may need to update the end position of it,
// or possibly even split it into two.
if (from < get_to(bound)) {
if (to < get_to(bound)) {
if (from < get_to(bound))
{
if (to < get_to(bound))
{
// need to split in two
bisect_range(bound, from, to);
return;
} else {
}
else
{
// just update end
reduce_to(bound, from);
}
@ -203,15 +230,12 @@ public:
return;
}
const_iterator erase(const_iterator it) {
return const_iterator(erase_range(it.It));
}
const_iterator erase(const_iterator it) { return const_iterator(erase_range(it.It)); }
void clear() {
Map.clear();
}
void clear() { Map.clear(); }
bool contains(T value) const {
bool contains(T value) const
{
auto it = Map.upper_bound(value);
if (it == Map.begin())
return false;
@ -219,49 +243,33 @@ public:
return get_from(it) <= value && value < get_to(it);
}
std::size_t size() const {
return Map.size();
}
std::size_t size() const { return Map.size(); }
bool empty() const {
return Map.empty();
}
bool empty() const { return Map.empty(); }
void swap(RangeSet<T>& other) {
Map.swap(other.Map);
}
void swap(RangeSet<T>& other) { Map.swap(other.Map); }
const_iterator begin() const {
return const_iterator(Map.begin());
}
const_iterator begin() const { return const_iterator(Map.begin()); }
const_iterator end() const {
return const_iterator(Map.end());
}
const_iterator end() const { return const_iterator(Map.end()); }
const_iterator cbegin() const {
return const_iterator(Map.cbegin());
}
const_iterator cbegin() const { return const_iterator(Map.cbegin()); }
const_iterator cend() const {
return const_iterator(Map.cend());
}
const_iterator cend() const { return const_iterator(Map.cend()); }
bool operator==(const RangeSet<T>& other) const {
return this->Map == other.Map;
}
bool operator==(const RangeSet<T>& other) const { return this->Map == other.Map; }
bool operator!=(const RangeSet<T>& other) const {
return !(*this == other);
}
bool operator!=(const RangeSet<T>& other) const { return !(*this == other); }
// Get free size and fragmentation ratio
std::pair<std::size_t, double> get_stats() const {
std::pair<std::size_t, double> get_stats() const
{
std::size_t free_total = 0;
if (begin() == end())
return {free_total, 1.0};
std::size_t largest_size = 0;
for (auto iter = begin(); iter != end(); ++iter) {
for (auto iter = begin(); iter != end(); ++iter)
{
const std::size_t size = calc_size(iter.from(), iter.to());
if (size > largest_size)
largest_size = size;
@ -271,11 +279,15 @@ public:
}
private:
static std::size_t calc_size(T from, T to) {
if constexpr (std::is_pointer_v<T>) {
static std::size_t calc_size(T from, T to)
{
if constexpr (std::is_pointer_v<T>)
{
// For pointers we don't want pointer arithmetic here, else void* breaks.
return reinterpret_cast<std::size_t>(to) - reinterpret_cast<std::size_t>(from);
} else {
}
else
{
return static_cast<std::size_t>(to - from);
}
}
@ -289,35 +301,25 @@ private:
// - Stored ranges never overlap.
MapT Map;
T get_from(typename MapT::iterator it) const {
return it->first;
}
T get_from(typename MapT::iterator it) const { return it->first; }
T get_to(typename MapT::iterator it) const {
return it->second;
}
T get_to(typename MapT::iterator it) const { return it->second; }
T get_from(typename MapT::const_iterator it) const {
return it->first;
}
T get_from(typename MapT::const_iterator it) const { return it->first; }
T get_to(typename MapT::const_iterator it) const {
return it->second;
}
T get_to(typename MapT::const_iterator it) const { return it->second; }
typename MapT::iterator insert_range(T from, T to) {
return Map.emplace(from, to).first;
}
typename MapT::iterator insert_range(T from, T to) { return Map.emplace(from, to).first; }
typename MapT::iterator erase_range(typename MapT::iterator it) {
typename MapT::iterator erase_range(typename MapT::iterator it) { return Map.erase(it); }
typename MapT::const_iterator erase_range(typename MapT::const_iterator it)
{
return Map.erase(it);
}
typename MapT::const_iterator erase_range(typename MapT::const_iterator it) {
return Map.erase(it);
}
void bisect_range(typename MapT::iterator it, T from, T to) {
void bisect_range(typename MapT::iterator it, T from, T to)
{
assert(get_from(it) < from);
assert(get_from(it) < to);
assert(get_to(it) > from);
@ -328,54 +330,66 @@ private:
insert_range(to, itto);
}
typename MapT::iterator reduce_from(typename MapT::iterator it, T from) {
typename MapT::iterator reduce_from(typename MapT::iterator it, T from)
{
assert(get_from(it) < from);
T itto = get_to(it);
erase_range(it);
return insert_range(from, itto);
}
void maybe_expand_to(typename MapT::iterator it, T to) {
void maybe_expand_to(typename MapT::iterator it, T to)
{
if (to <= get_to(it))
return;
expand_to(it, to);
}
void expand_to(typename MapT::iterator it, T to) {
void expand_to(typename MapT::iterator it, T to)
{
assert(get_to(it) < to);
it->second = to;
}
void reduce_to(typename MapT::iterator it, T to) {
void reduce_to(typename MapT::iterator it, T to)
{
assert(get_to(it) > to);
it->second = to;
}
void merge_from_iterator_to_value(typename MapT::iterator inserted, typename MapT::iterator bound, T to) {
void merge_from_iterator_to_value(typename MapT::iterator inserted, typename MapT::iterator bound,
T to)
{
// Erase all ranges that overlap the inserted while updating the upper end.
while (bound != Map.end() && get_from(bound) <= to) {
while (bound != Map.end() && get_from(bound) <= to)
{
maybe_expand_to(inserted, get_to(bound));
bound = erase_range(bound);
}
}
void erase_from_iterator_to_value(typename MapT::iterator bound, T to) {
void erase_from_iterator_to_value(typename MapT::iterator bound, T to)
{
// Assumption: Given bound starts at or after the 'from' value of the range to erase.
while (true) {
while (true)
{
// Given range starts before stored range.
if (to <= get_from(bound)) {
if (to <= get_from(bound))
{
// Range ends before this range too, nothing to do.
return;
}
if (to < get_to(bound)) {
if (to < get_to(bound))
{
// Range ends in the middle of current range, reduce current.
reduce_from(bound, to);
return;
}
if (to == get_to(bound)) {
if (to == get_to(bound))
{
// Range ends exactly with current range, erase current.
erase_range(bound);
return;
@ -384,11 +398,12 @@ private:
// Range ends later than current range.
// First erase current, then loop to check the range(s) after this one too.
bound = erase_range(bound);
if (bound == Map.end()) {
if (bound == Map.end())
{
// Unless that was the last range, in which case there's nothing else to do.
return;
}
}
}
};
} // namespace HyoutaUtilities
} // namespace Common

View File

@ -1,3 +1,6 @@
// Copyright 2020 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <cassert>
@ -6,42 +9,51 @@
#include <type_traits>
#include <utility>
namespace HyoutaUtilities {
// Like RangeSet, but additionally stores a map of the ranges sorted by their size, for quickly finding the largest or
// smallest range.
template <typename T> class RangeSizeSet {
namespace Common
{
// Like RangeSet, but additionally stores a map of the ranges sorted by their size, for quickly
// finding the largest or smallest range.
template <typename T>
class RangeSizeSet
{
private:
// Key type used in the by-size multimap. Should be a type big enough to hold all possible distances between
// possible 'from' and 'to'.
// Key type used in the by-size multimap. Should be a type big enough to hold all possible
// distances between possible 'from' and 'to'.
// I'd actually love to just do
// using SizeT = typename std::conditional<std::is_pointer_v<T>,
// std::size_t, typename std::make_unsigned<T>::type>::type;
// but that's apparently not possible due to the std::make_unsigned<T>::type not existing for pointer types
// so we'll work around this...
template <typename U, bool IsPointer> struct GetSizeType { using S = typename std::make_unsigned<U>::type; };
template <typename U> struct GetSizeType<U, true> { using S = std::size_t; };
// but that's apparently not possible due to the std::make_unsigned<T>::type not existing for
// pointer types so we'll work around this...
template <typename U, bool IsPointer>
struct GetSizeType
{
using S = typename std::make_unsigned<U>::type;
};
template <typename U>
struct GetSizeType<U, true>
{
using S = std::size_t;
};
public:
using SizeT = typename GetSizeType<T, std::is_pointer_v<T>>::S;
private:
// Value type stored in the regular range map.
struct Value {
struct Value
{
// End point of the range.
T To;
// Pointer to the same range in the by-size multimap.
typename std::multimap<SizeT, typename std::map<T, Value>::iterator, std::greater<SizeT>>::iterator SizeIt;
typename std::multimap<SizeT, typename std::map<T, Value>::iterator,
std::greater<SizeT>>::iterator SizeIt;
Value(T to) : To(to) {}
bool operator==(const Value& other) const {
return this->To == other.To;
}
bool operator==(const Value& other) const { return this->To == other.To; }
bool operator!=(const Value& other) const {
return !operator==(other);
}
bool operator!=(const Value& other) const { return !operator==(other); }
};
using MapT = std::map<T, Value>;
@ -50,49 +62,44 @@ private:
public:
struct by_size_const_iterator;
struct const_iterator {
struct const_iterator
{
public:
const T& from() const {
return It->first;
}
const T& from() const { return It->first; }
const T& to() const {
return It->second.To;
}
const T& to() const { return It->second.To; }
const_iterator& operator++() {
const_iterator& operator++()
{
++It;
return *this;
}
const_iterator operator++(int) {
const_iterator operator++(int)
{
const_iterator old = *this;
++It;
return old;
}
const_iterator& operator--() {
const_iterator& operator--()
{
--It;
return *this;
}
const_iterator operator--(int) {
const_iterator operator--(int)
{
const_iterator old = *this;
--It;
return old;
}
bool operator==(const const_iterator& rhs) const {
return this->It == rhs.It;
}
bool operator==(const const_iterator& rhs) const { return this->It == rhs.It; }
bool operator!=(const const_iterator& rhs) const {
return !operator==(rhs);
}
bool operator!=(const const_iterator& rhs) const { return !operator==(rhs); }
by_size_const_iterator to_size_iterator() {
return by_size_const_iterator(It->second.SizeIt);
}
by_size_const_iterator to_size_iterator() { return by_size_const_iterator(It->second.SizeIt); }
private:
typename MapT::const_iterator It;
@ -100,49 +107,44 @@ public:
friend class RangeSizeSet;
};
struct by_size_const_iterator {
struct by_size_const_iterator
{
public:
const T& from() const {
return It->second->first;
}
const T& from() const { return It->second->first; }
const T& to() const {
return It->second->second.To;
}
const T& to() const { return It->second->second.To; }
by_size_const_iterator& operator++() {
by_size_const_iterator& operator++()
{
++It;
return *this;
}
by_size_const_iterator operator++(int) {
by_size_const_iterator operator++(int)
{
by_size_const_iterator old = *this;
++It;
return old;
}
by_size_const_iterator& operator--() {
by_size_const_iterator& operator--()
{
--It;
return *this;
}
by_size_const_iterator operator--(int) {
by_size_const_iterator operator--(int)
{
by_size_const_iterator old = *this;
--It;
return old;
}
bool operator==(const by_size_const_iterator& rhs) const {
return this->It == rhs.It;
}
bool operator==(const by_size_const_iterator& rhs) const { return this->It == rhs.It; }
bool operator!=(const by_size_const_iterator& rhs) const {
return !operator==(rhs);
}
bool operator!=(const by_size_const_iterator& rhs) const { return !operator==(rhs); }
const_iterator to_range_iterator() {
return const_iterator(It->second);
}
const_iterator to_range_iterator() { return const_iterator(It->second); }
private:
typename SizeMapT::const_iterator It;
@ -157,7 +159,8 @@ public:
RangeSizeSet<T>& operator=(const RangeSizeSet<T>&) = delete;
RangeSizeSet<T>& operator=(RangeSizeSet<T>&&) = default;
void insert(T from, T to) {
void insert(T from, T to)
{
if (from >= to)
return;
@ -165,18 +168,21 @@ public:
// upper_bound() returns the closest range whose starting position
// is greater than 'from'.
auto bound = Map.upper_bound(from);
if (bound == Map.end()) {
if (bound == Map.end())
{
// There is no range that starts greater than the given one.
// This means we have three options:
// - 1. No range exists yet, this is the first range.
if (Map.empty()) {
if (Map.empty())
{
insert_range(from, to);
return;
}
// - 2. The given range does not overlap the last range.
--bound;
if (from > get_to(bound)) {
if (from > get_to(bound))
{
insert_range(from, to);
return;
}
@ -186,7 +192,8 @@ public:
return;
}
if (bound == Map.begin()) {
if (bound == Map.begin())
{
// The given range starts before any of the existing ones.
// We must insert this as a new range even if we potentially overlap
// an existing one as we can't modify a key in a std::map.
@ -201,7 +208,8 @@ public:
// could possibly be affected.
// If 'bound' overlaps with given range, update bounds object.
if (get_to(bound) >= from) {
if (get_to(bound) >= from)
{
maybe_expand_to(bound, to);
auto inserted = bound;
++bound;
@ -212,7 +220,8 @@ public:
// 'bound' *doesn't* overlap with given range, check next range.
// If this range overlaps with given range,
if (get_from(abound) <= to) {
if (get_from(abound) <= to)
{
// insert new range
auto inserted = insert_range(from, to >= get_to(abound) ? to : get_to(abound));
// and delete overlaps
@ -226,49 +235,61 @@ public:
insert_range(from, to);
}
void erase(T from, T to) {
void erase(T from, T to)
{
if (from >= to)
return;
// Like insert(), we use upper_bound to find the closest range.
auto bound = Map.upper_bound(from);
if (bound == Map.end()) {
if (bound == Map.end())
{
// There is no range that starts greater than the given one.
if (Map.empty()) {
if (Map.empty())
{
// nothing to do
return;
}
--bound;
// 'bound' now points at the last range.
if (from >= get_to(bound)) {
if (from >= get_to(bound))
{
// Given range is larger than any range that exists, nothing to do.
return;
}
if (to >= get_to(bound)) {
if (from == get_from(bound)) {
if (to >= get_to(bound))
{
if (from == get_from(bound))
{
// Given range fully overlaps last range, erase it.
erase_range(bound);
return;
} else {
}
else
{
// Given range overlaps end of last range, reduce it.
reduce_to(bound, from);
return;
}
}
if (from == get_from(bound)) {
if (from == get_from(bound))
{
// Given range overlaps begin of last range, reduce it.
reduce_from(bound, to);
return;
} else {
}
else
{
// Given range overlaps middle of last range, bisect it.
bisect_range(bound, from, to);
return;
}
}
if (bound == Map.begin()) {
if (bound == Map.begin())
{
// If we found the first range that means 'from' is before any stored range.
// This means we can just erase from start until 'to' and be done with it.
erase_from_iterator_to_value(bound, to);
@ -278,7 +299,8 @@ public:
// check previous range
auto abound = bound--;
if (from == get_from(bound)) {
if (from == get_from(bound))
{
// Similarly, if the previous range starts with the given one, just erase until 'to'.
erase_from_iterator_to_value(bound, to);
return;
@ -287,12 +309,16 @@ public:
// If we come here, the given range may or may not overlap part of the current 'bound'
// (but never the full range), which means we may need to update the end position of it,
// or possibly even split it into two.
if (from < get_to(bound)) {
if (to < get_to(bound)) {
if (from < get_to(bound))
{
if (to < get_to(bound))
{
// need to split in two
bisect_range(bound, from, to);
return;
} else {
}
else
{
// just update end
reduce_to(bound, from);
}
@ -303,20 +329,21 @@ public:
return;
}
const_iterator erase(const_iterator it) {
return const_iterator(erase_range(it.It));
}
const_iterator erase(const_iterator it) { return const_iterator(erase_range(it.It)); }
by_size_const_iterator erase(by_size_const_iterator it) {
by_size_const_iterator erase(by_size_const_iterator it)
{
return by_size_const_iterator(erase_range_by_size(it.It));
}
void clear() {
void clear()
{
Map.clear();
Sizes.clear();
}
bool contains(T value) const {
bool contains(T value) const
{
auto it = Map.upper_bound(value);
if (it == Map.begin())
return false;
@ -324,83 +351,61 @@ public:
return get_from(it) <= value && value < get_to(it);
}
std::size_t size() const {
return Map.size();
}
std::size_t size() const { return Map.size(); }
bool empty() const {
return Map.empty();
}
bool empty() const { return Map.empty(); }
std::size_t by_size_count(const SizeT& key) const {
return Sizes.count(key);
}
std::size_t by_size_count(const SizeT& key) const { return Sizes.count(key); }
by_size_const_iterator by_size_find(const SizeT& key) const {
return Sizes.find(key);
}
by_size_const_iterator by_size_find(const SizeT& key) const { return Sizes.find(key); }
std::pair<by_size_const_iterator, by_size_const_iterator> by_size_equal_range(const SizeT& key) const {
std::pair<by_size_const_iterator, by_size_const_iterator>
by_size_equal_range(const SizeT& key) const
{
auto p = Sizes.equal_range(key);
return std::pair<by_size_const_iterator, by_size_const_iterator>(by_size_const_iterator(p.first),
by_size_const_iterator(p.second));
return std::pair<by_size_const_iterator, by_size_const_iterator>(
by_size_const_iterator(p.first), by_size_const_iterator(p.second));
}
by_size_const_iterator by_size_lower_bound(const SizeT& key) const {
by_size_const_iterator by_size_lower_bound(const SizeT& key) const
{
return Sizes.lower_bound(key);
}
by_size_const_iterator by_size_upper_bound(const SizeT& key) const {
by_size_const_iterator by_size_upper_bound(const SizeT& key) const
{
return Sizes.upper_bound(key);
}
void swap(RangeSizeSet<T>& other) {
void swap(RangeSizeSet<T>& other)
{
Map.swap(other.Map);
Sizes.swap(other.Sizes);
}
const_iterator begin() const {
return const_iterator(Map.begin());
}
const_iterator begin() const { return const_iterator(Map.begin()); }
const_iterator end() const {
return const_iterator(Map.end());
}
const_iterator end() const { return const_iterator(Map.end()); }
const_iterator cbegin() const {
return const_iterator(Map.cbegin());
}
const_iterator cbegin() const { return const_iterator(Map.cbegin()); }
const_iterator cend() const {
return const_iterator(Map.cend());
}
const_iterator cend() const { return const_iterator(Map.cend()); }
by_size_const_iterator by_size_begin() const {
return by_size_const_iterator(Sizes.begin());
}
by_size_const_iterator by_size_begin() const { return by_size_const_iterator(Sizes.begin()); }
by_size_const_iterator by_size_end() const {
return by_size_const_iterator(Sizes.end());
}
by_size_const_iterator by_size_end() const { return by_size_const_iterator(Sizes.end()); }
by_size_const_iterator by_size_cbegin() const {
return by_size_const_iterator(Sizes.cbegin());
}
by_size_const_iterator by_size_cbegin() const { return by_size_const_iterator(Sizes.cbegin()); }
by_size_const_iterator by_size_cend() const {
return by_size_const_iterator(Sizes.cend());
}
by_size_const_iterator by_size_cend() const { return by_size_const_iterator(Sizes.cend()); }
bool operator==(const RangeSizeSet<T>& other) const {
return this->Map == other.Map;
}
bool operator==(const RangeSizeSet<T>& other) const { return this->Map == other.Map; }
bool operator!=(const RangeSizeSet<T>& other) const {
return !(*this == other);
}
bool operator!=(const RangeSizeSet<T>& other) const { return !(*this == other); }
// Get free size and fragmentation ratio
std::pair<std::size_t, double> get_stats() const {
std::pair<std::size_t, double> get_stats() const
{
std::size_t free_total = 0;
if (begin() == end())
return {free_total, 1.0};
@ -410,12 +415,16 @@ public:
}
private:
static SizeT calc_size(T from, T to) {
if constexpr (std::is_pointer_v<T>) {
static SizeT calc_size(T from, T to)
{
if constexpr (std::is_pointer_v<T>)
{
// For pointers we don't want pointer arithmetic here, else void* breaks.
static_assert(sizeof(T) <= sizeof(SizeT));
return reinterpret_cast<SizeT>(to) - reinterpret_cast<SizeT>(from);
} else {
}
else
{
return static_cast<SizeT>(to - from);
}
}
@ -435,44 +444,41 @@ private:
// We use std::greater so that Sizes.begin() gives us the largest range.
SizeMapT Sizes;
T get_from(typename MapT::iterator it) const {
return it->first;
}
T get_from(typename MapT::iterator it) const { return it->first; }
T get_to(typename MapT::iterator it) const {
return it->second.To;
}
T get_to(typename MapT::iterator it) const { return it->second.To; }
T get_from(typename MapT::const_iterator it) const {
return it->first;
}
T get_from(typename MapT::const_iterator it) const { return it->first; }
T get_to(typename MapT::const_iterator it) const {
return it->second.To;
}
T get_to(typename MapT::const_iterator it) const { return it->second.To; }
typename MapT::iterator insert_range(T from, T to) {
typename MapT::iterator insert_range(T from, T to)
{
auto m = Map.emplace(from, to).first;
m->second.SizeIt = Sizes.emplace(calc_size(from, to), m);
return m;
}
typename MapT::iterator erase_range(typename MapT::iterator it) {
typename MapT::iterator erase_range(typename MapT::iterator it)
{
Sizes.erase(it->second.SizeIt);
return Map.erase(it);
}
typename MapT::const_iterator erase_range(typename MapT::const_iterator it) {
typename MapT::const_iterator erase_range(typename MapT::const_iterator it)
{
Sizes.erase(it->second.SizeIt);
return Map.erase(it);
}
typename SizeMapT::const_iterator erase_range_by_size(typename SizeMapT::const_iterator it) {
typename SizeMapT::const_iterator erase_range_by_size(typename SizeMapT::const_iterator it)
{
Map.erase(it->second);
return Sizes.erase(it);
}
void bisect_range(typename MapT::iterator it, T from, T to) {
void bisect_range(typename MapT::iterator it, T from, T to)
{
assert(get_from(it) < from);
assert(get_from(it) < to);
assert(get_to(it) > from);
@ -483,58 +489,70 @@ private:
insert_range(to, itto);
}
typename MapT::iterator reduce_from(typename MapT::iterator it, T from) {
typename MapT::iterator reduce_from(typename MapT::iterator it, T from)
{
assert(get_from(it) < from);
T itto = get_to(it);
erase_range(it);
return insert_range(from, itto);
}
void maybe_expand_to(typename MapT::iterator it, T to) {
void maybe_expand_to(typename MapT::iterator it, T to)
{
if (to <= get_to(it))
return;
expand_to(it, to);
}
void expand_to(typename MapT::iterator it, T to) {
void expand_to(typename MapT::iterator it, T to)
{
assert(get_to(it) < to);
it->second.To = to;
Sizes.erase(it->second.SizeIt);
it->second.SizeIt = Sizes.emplace(calc_size(get_from(it), to), it);
}
void reduce_to(typename MapT::iterator it, T to) {
void reduce_to(typename MapT::iterator it, T to)
{
assert(get_to(it) > to);
it->second.To = to;
Sizes.erase(it->second.SizeIt);
it->second.SizeIt = Sizes.emplace(calc_size(get_from(it), to), it);
}
void merge_from_iterator_to_value(typename MapT::iterator inserted, typename MapT::iterator bound, T to) {
void merge_from_iterator_to_value(typename MapT::iterator inserted, typename MapT::iterator bound,
T to)
{
// Erase all ranges that overlap the inserted while updating the upper end.
while (bound != Map.end() && get_from(bound) <= to) {
while (bound != Map.end() && get_from(bound) <= to)
{
maybe_expand_to(inserted, get_to(bound));
bound = erase_range(bound);
}
}
void erase_from_iterator_to_value(typename MapT::iterator bound, T to) {
void erase_from_iterator_to_value(typename MapT::iterator bound, T to)
{
// Assumption: Given bound starts at or after the 'from' value of the range to erase.
while (true) {
while (true)
{
// Given range starts before stored range.
if (to <= get_from(bound)) {
if (to <= get_from(bound))
{
// Range ends before this range too, nothing to do.
return;
}
if (to < get_to(bound)) {
if (to < get_to(bound))
{
// Range ends in the middle of current range, reduce current.
reduce_from(bound, to);
return;
}
if (to == get_to(bound)) {
if (to == get_to(bound))
{
// Range ends exactly with current range, erase current.
erase_range(bound);
return;
@ -543,11 +561,12 @@ private:
// Range ends later than current range.
// First erase current, then loop to check the range(s) after this one too.
bound = erase_range(bound);
if (bound == Map.end()) {
if (bound == Map.end())
{
// Unless that was the last range, in which case there's nothing else to do.
return;
}
}
}
};
} // namespace HyoutaUtilities
} // namespace Common

View File

@ -660,7 +660,6 @@ PUBLIC
inputcommon
MbedTLS::mbedtls
pugixml
RangeSet::RangeSet
sfml-network
videonull
videoogl

View File

@ -5,9 +5,8 @@
#include <cstddef>
#include <rangeset/rangesizeset.h>
#include "Common/CommonTypes.h"
#include "Common/RangeSizeSet.h"
#include "Core/PowerPC/CachedInterpreter/CachedInterpreterBlockCache.h"
#include "Core/PowerPC/CachedInterpreter/CachedInterpreterEmitter.h"
#include "Core/PowerPC/JitCommon/JitBase.h"
@ -112,7 +111,7 @@ private:
static s32 CheckIdle(PowerPC::PowerPCState& ppc_state, const CheckIdleOperands& operands);
static s32 CheckIdle(std::ostream& stream, const CheckIdleOperands& operands);
HyoutaUtilities::RangeSizeSet<u8*> m_free_ranges;
Common::RangeSizeSet<u8*> m_free_ranges;
CachedInterpreterBlockCache m_block_cache;
};

View File

@ -19,9 +19,8 @@
#include <optional>
#include <rangeset/rangesizeset.h>
#include "Common/CommonTypes.h"
#include "Common/RangeSizeSet.h"
#include "Common/x64Emitter.h"
#include "Core/PowerPC/Jit64/JitAsm.h"
#include "Core/PowerPC/Jit64/RegCache/FPURegCache.h"
@ -295,8 +294,8 @@ private:
Jit64AsmRoutineManager asm_routines{*this};
HyoutaUtilities::RangeSizeSet<u8*> m_free_ranges_near;
HyoutaUtilities::RangeSizeSet<u8*> m_free_ranges_far;
Common::RangeSizeSet<u8*> m_free_ranges_near;
Common::RangeSizeSet<u8*> m_free_ranges_far;
const bool m_im_here_debug = false;
const bool m_im_here_log = false;

View File

@ -154,6 +154,8 @@
<ClInclude Include="Common\Projection.h" />
<ClInclude Include="Common\QoSSession.h" />
<ClInclude Include="Common\Random.h" />
<ClInclude Include="Common\RangeSet.h" />
<ClInclude Include="Common\RangeSizeSet.h" />
<ClInclude Include="Common\scmrev.h" />
<ClInclude Include="Common\ScopeGuard.h" />
<ClInclude Include="Common\SDCardUtil.h" />

View File

@ -14,7 +14,6 @@
<AdditionalIncludeDirectories>$(ExternalsDir)FFmpeg-bin\$(Platform)\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(ExternalsDir)OpenAL\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(ExternalsDir)expr\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(ExternalsDir)rangeset\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(ExternalsDir)Vulkan-Headers\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(ExternalsDir)VulkanMemoryAllocator\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(ExternalsDir)watcher\watcher\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>