mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2025-07-14 13:12:13 +02:00
util: add boundedmap
This commit is contained in:
parent
78447fc995
commit
147ea49a3a
@ -19,61 +19,6 @@
|
|||||||
namespace ams::ncm {
|
namespace ams::ncm {
|
||||||
|
|
||||||
template<class Key, class Value, size_t N>
|
template<class Key, class Value, size_t N>
|
||||||
class BoundedMap {
|
using BoundedMap = util::BoundedMap<Key, Value, N>;
|
||||||
private:
|
|
||||||
std::array<std::optional<Key>, N> keys;
|
|
||||||
std::array<Value, N> values;
|
|
||||||
public:
|
|
||||||
Value *Find(const Key &key) {
|
|
||||||
for (size_t i = 0; i < N; i++) {
|
|
||||||
if (this->keys[i] && this->keys[i].value() == key) {
|
|
||||||
return &this->values[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Remove(const Key &key) {
|
|
||||||
for (size_t i = 0; i < N; i++) {
|
|
||||||
if (this->keys[i] && this->keys[i].value() == key) {
|
|
||||||
this->keys[i].reset();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void RemoveAll() {
|
|
||||||
for (size_t i = 0; i < N; i++) {
|
|
||||||
this->keys[i].reset();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IsFull() {
|
|
||||||
for (size_t i = 0; i < N; i++) {
|
|
||||||
if (!this->keys[i]) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
Value &operator[](const Key &key) {
|
|
||||||
/* Try to find an existing value. */
|
|
||||||
if (Value *value = this->Find(key); value != nullptr) {
|
|
||||||
return *value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Reference a new value. */
|
|
||||||
for (size_t i = 0; i < N; i++) {
|
|
||||||
if (!this->keys[i]) {
|
|
||||||
this->keys[i] = key;
|
|
||||||
return this->values[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* We ran out of space in the map. */
|
|
||||||
AMS_ABORT();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -30,11 +30,11 @@ namespace ams::lr {
|
|||||||
/* No existing resolver is present, create one. */
|
/* No existing resolver is present, create one. */
|
||||||
if (!resolver) {
|
if (!resolver) {
|
||||||
if (storage_id == ncm::StorageId::Host) {
|
if (storage_id == ncm::StorageId::Host) {
|
||||||
this->location_resolvers[storage_id] = std::make_shared<RedirectOnlyLocationResolverImpl>();
|
AMS_ABORT_UNLESS(this->location_resolvers.Insert(storage_id, std::make_shared<RedirectOnlyLocationResolverImpl>()));
|
||||||
} else {
|
} else {
|
||||||
auto content_resolver = std::make_shared<ContentLocationResolverImpl>(storage_id);
|
auto content_resolver = std::make_shared<ContentLocationResolverImpl>(storage_id);
|
||||||
R_TRY(content_resolver->Refresh());
|
R_TRY(content_resolver->Refresh());
|
||||||
this->location_resolvers[storage_id] = std::move(content_resolver);
|
AMS_ABORT_UNLESS(this->location_resolvers.Insert(storage_id, std::move(content_resolver)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Acquire the newly-created resolver. */
|
/* Acquire the newly-created resolver. */
|
||||||
|
@ -32,3 +32,4 @@
|
|||||||
#include <vapours/util/util_intrusive_red_black_tree.hpp>
|
#include <vapours/util/util_intrusive_red_black_tree.hpp>
|
||||||
#include <vapours/util/util_tinymt.hpp>
|
#include <vapours/util/util_tinymt.hpp>
|
||||||
#include <vapours/util/util_uuid.hpp>
|
#include <vapours/util/util_uuid.hpp>
|
||||||
|
#include <vapours/util/util_bounded_map.hpp>
|
||||||
|
129
libraries/libvapours/include/vapours/util/util_bounded_map.hpp
Normal file
129
libraries/libvapours/include/vapours/util/util_bounded_map.hpp
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2018-2020 Atmosphère-NX
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms and conditions of the GNU General Public License,
|
||||||
|
* version 2, as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include <vapours/common.hpp>
|
||||||
|
#include <vapours/assert.hpp>
|
||||||
|
#include <vapours/util/util_typed_storage.hpp>
|
||||||
|
|
||||||
|
namespace ams::util {
|
||||||
|
|
||||||
|
template<class Key, class Value, size_t N>
|
||||||
|
class BoundedMap {
|
||||||
|
private:
|
||||||
|
std::array<std::optional<Key>, N> keys;
|
||||||
|
std::array<TYPED_STORAGE(Value), N> values;
|
||||||
|
private:
|
||||||
|
ALWAYS_INLINE void FreeEntry(size_t i) {
|
||||||
|
this->keys[i].reset();
|
||||||
|
GetReference(this->values[i]).~Value();
|
||||||
|
}
|
||||||
|
public:
|
||||||
|
Value *Find(const Key &key) {
|
||||||
|
for (size_t i = 0; i < N; i++) {
|
||||||
|
if (this->keys[i] && this->keys[i].value() == key) {
|
||||||
|
return GetPointer(this->values[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Remove(const Key &key) {
|
||||||
|
for (size_t i = 0; i < N; i++) {
|
||||||
|
if (this->keys[i] && this->keys[i].value() == key) {
|
||||||
|
this->FreeEntry(i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void RemoveAll() {
|
||||||
|
for (size_t i = 0; i < N; i++) {
|
||||||
|
this->FreeEntry(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsFull() {
|
||||||
|
for (size_t i = 0; i < N; i++) {
|
||||||
|
if (!this->keys[i]) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Insert(const Key &key, Value &&value) {
|
||||||
|
/* We can't insert if the key is used. */
|
||||||
|
if (this->Find(key) != nullptr) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Find a free value. */
|
||||||
|
for (size_t i = 0; i < N; i++) {
|
||||||
|
if (!this->keys[i]) {
|
||||||
|
this->keys[i] = key;
|
||||||
|
new (GetPointer(this->values[i])) Value(std::move(value));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool InsertOrAssign(const Key &key, Value &&value) {
|
||||||
|
/* Try to find and assign an existing value. */
|
||||||
|
for (size_t i = 0; i < N; i++) {
|
||||||
|
if (this->keys[i] && this->keys[i].value() == key) {
|
||||||
|
GetReference(this->values[i]) = std::move(value);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Find a free value. */
|
||||||
|
for (size_t i = 0; i < N; i++) {
|
||||||
|
if (!this->keys[i]) {
|
||||||
|
this->keys[i] = key;
|
||||||
|
new (GetPointer(this->values[i])) Value(std::move(value));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class... Args>
|
||||||
|
bool Emplace(const Key &key, Args&&... args) {
|
||||||
|
/* We can't emplace if the key is used. */
|
||||||
|
if (this->Find(key) != nullptr) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Find a free value. */
|
||||||
|
for (size_t i = 0; i < N; i++) {
|
||||||
|
this->keys[i] = key;
|
||||||
|
if (!this->keys[i]) {
|
||||||
|
this->keys[i] = key;
|
||||||
|
new (GetPointer(this->values[i])) Value(std::forward<Args>(args)...);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user