From f661d13d24b34f2412d8874e53a7112398390899 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Sun, 4 Jul 2021 04:00:41 +0300 Subject: [PATCH] Include a few other extra methods. --- module/Library/Utils/Dictionary.cpp | 5 ++- module/Library/Utils/Dictionary.hpp | 63 ++++++++++++++++++++++++++++- 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/module/Library/Utils/Dictionary.cpp b/module/Library/Utils/Dictionary.cpp index 70b7a945..6adbccca 100644 --- a/module/Library/Utils/Dictionary.cpp +++ b/module/Library/Utils/Dictionary.cpp @@ -22,8 +22,11 @@ void Register_Dictionary(HSQUIRRELVM vm, Table & ns) .Prop(_SC("Size"), &SqDictionary::Size) // Member Methods .Func(_SC("Get"), &SqDictionary::Get) - .Func(_SC("Set"), &SqDictionary::Set) .Func(_SC("GetOr"), &SqDictionary::GetOr) + .Func(_SC("Set"), &SqDictionary::Set) + .Func(_SC("SetOr"), &SqDictionary::SetOr) + .Func(_SC("Update"), &SqDictionary::Update) + .Func(_SC("Create"), &SqDictionary::Create) .Func(_SC("Clear"), &SqDictionary::Clear) .Func(_SC("Erase"), &SqDictionary::Erase) .Func(_SC("Contains"), &SqDictionary::Contains) diff --git a/module/Library/Utils/Dictionary.hpp b/module/Library/Utils/Dictionary.hpp index 06d8ba8d..d17a8169 100644 --- a/module/Library/Utils/Dictionary.hpp +++ b/module/Library/Utils/Dictionary.hpp @@ -166,18 +166,77 @@ struct SqDictionary /* -------------------------------------------------------------------------------------------- * Modify a value from the container. */ - void Set(SqKeyHash k, LightObj & v) + bool Set(SqKeyHash k, LightObj & v) { for (auto & e : mC) { if (e.first == k.mH) { e.second = std::move(v); - return; // We updated existing element + // We updated the element + return true; } } // Create the element now mC.emplace_back(k.mH, std::move(v)); + // We created the element + return false; + } + + /* -------------------------------------------------------------------------------------------- + * Modify a value from the container. Use different values for update and create. + */ + bool SetOr(SqKeyHash k, LightObj & a, LightObj & b) + { + for (auto & e : mC) + { + if (e.first == k.mH) + { + e.second = std::move(a); + // We updated the element + return true; + } + } + // Create the element now + mC.emplace_back(k.mH, std::move(b)); + // We created the element + return false; + } + + /* -------------------------------------------------------------------------------------------- + * Update a value from the container. Does not create one if it doesn't exist. + */ + bool Update(SqKeyHash k, LightObj & v) + { + for (auto & e : mC) + { + if (e.first == k.mH) + { + e.second = std::move(v); + // We updated the element + return true; + } + } + // No element was updated + return false; + } + + /* -------------------------------------------------------------------------------------------- + * Create a value in the container. Does not update value from existing element. + */ + bool Create(SqKeyHash k, LightObj & v) + { + for (auto & e : mC) + { + if (e.first == k.mH) + { + return false; // No element was created + } + } + // Create the element now + mC.emplace_back(k.mH, std::move(v)); + // We created the element + return true; } /* --------------------------------------------------------------------------------------------