From 058cfac2909e3d15779452ce420e0c7078151486 Mon Sep 17 00:00:00 2001 From: Leo Date: Sat, 31 Oct 2026 10:48:56 +0100 Subject: [PATCH 01/15] Add array manipulation functions for rapidjson values --- .../Nebulite/Data/Document/RjDirectAccess.hpp | 40 +++++++++++++++++++ .../Nebulite/Data/Document/RjDirectAccess.tpp | 20 ++++++++++ src/Nebulite/Data/Document/RjDirectAccess.cpp | 22 ++++++++++ 3 files changed, 82 insertions(+) diff --git a/include/Nebulite/Data/Document/RjDirectAccess.hpp b/include/Nebulite/Data/Document/RjDirectAccess.hpp index d9ad1819..bba98b9b 100644 --- a/include/Nebulite/Data/Document/RjDirectAccess.hpp +++ b/include/Nebulite/Data/Document/RjDirectAccess.hpp @@ -7,6 +7,7 @@ // Standard library #include // NOLINT #include +#include #include #include #include @@ -110,6 +111,45 @@ namespace Nebulite::Data::RjDirectAccess { template requires (!std::is_trivially_copyable_v) bool set(std::string_view key, T const& value, rapidjson::Value& val, rapidjson::Document::AllocatorType& allocator); + //------------------------------------------ + // Array + + /** + * @brief Ensures that a rapidjson value is an array. + * @param val The rapidjson value to modify. + * @param allocator The allocator to use when moving the value into the array. + */ + void ensureArray(rapidjson::Value* val, rapidjson::Document::AllocatorType& allocator); + + /** + * @brief Replaces a rapidjson value with an empty array. + * @param val The rapidjson value to modify. + */ + void setEmptyArray(rapidjson::Value* val); + + /** + * @brief Replaces a value with an array containing the supplied range. + * @details Any existing value is discarded, including an existing array. + * @tparam R The type of the range to insert. + * @param val The rapidjson value to replace. + * @param values The values to insert into the new array. + * @param allocator The allocator to use when adding values to the array. + */ + template + void insertArray(rapidjson::Value* val, R values, rapidjson::Document::AllocatorType& allocator); + + /** + * @brief Appends a range of values to a rapidjson array. + * @details A non-array value is first converted to an array with ensureArray, + * preserving it as the first element. A null value becomes an empty array. + * @tparam R The type of the range to append. + * @param val The rapidjson value to append to. + * @param values The values to append to the array. + * @param allocator The allocator to use when adding values to the array. + */ + template + void appendArray(rapidjson::Value* val, R values, rapidjson::Document::AllocatorType& allocator); + //------------------------------------------ // Conversion diff --git a/include/Nebulite/Data/Document/RjDirectAccess.tpp b/include/Nebulite/Data/Document/RjDirectAccess.tpp index da0b8171..95cc7bbf 100644 --- a/include/Nebulite/Data/Document/RjDirectAccess.tpp +++ b/include/Nebulite/Data/Document/RjDirectAccess.tpp @@ -7,6 +7,7 @@ // Standard library #include // NOLINT #include +#include #include #include #include @@ -154,6 +155,25 @@ bool set(std::string_view const key, T const& value, rapidjson::Value& val, rapi return false; } +//------------------------------------------ +// Array + +template +void insertArray(rapidjson::Value* val, R values, rapidjson::Document::AllocatorType& allocator) { + setEmptyArray(val); + for (auto const& v : values) { + val->PushBack(v, allocator); + } +} + +template +void appendArray(rapidjson::Value* val, R values, rapidjson::Document::AllocatorType& allocator) { + ensureArray(val, allocator); + for (auto const& v : values) { + val->PushBack(v, allocator); + } +} + //------------------------------------------ // All conversion variants from/to rapidjson values diff --git a/src/Nebulite/Data/Document/RjDirectAccess.cpp b/src/Nebulite/Data/Document/RjDirectAccess.cpp index 2ec3f1d6..e185cd94 100644 --- a/src/Nebulite/Data/Document/RjDirectAccess.cpp +++ b/src/Nebulite/Data/Document/RjDirectAccess.cpp @@ -49,6 +49,28 @@ std::optional getSimpleValue(rapidjson::Value const* val) { return std::nullopt; } +//------------------------------------------ +// Array + +void ensureArray(rapidjson::Value* val, rapidjson::Document::AllocatorType& allocator) { + if (val->IsArray()) { + return; + } + + if (!val->IsNull()) { + rapidjson::Value existingValue(std::move(*val)); + val->SetArray(); + val->PushBack(existingValue, allocator); + return; + } + + val->SetArray(); +} + +void setEmptyArray(rapidjson::Value* val) { + val->SetArray(); +} + //------------------------------------------ // Document traversal From b8c7caff9c8a560386dcb3a4ff27b779339ece55 Mon Sep 17 00:00:00 2001 From: Leo Date: Sat, 31 Oct 2026 11:05:47 +0100 Subject: [PATCH 02/15] Add insertArray and appendArray methods for range-based array manipulation --- include/Nebulite/Data/Document/Json.hpp | 22 ++++++++++++++ include/Nebulite/Data/Document/Json.tpp | 30 ++++++++++++++++++++ include/Nebulite/Data/Document/JsonScope.hpp | 10 +++++++ include/Nebulite/Data/Document/JsonScope.tpp | 10 +++++++ src/Nebulite/Data/Document/Json.cpp | 8 +++++- 5 files changed, 79 insertions(+), 1 deletion(-) diff --git a/include/Nebulite/Data/Document/Json.hpp b/include/Nebulite/Data/Document/Json.hpp index 92d9e61c..8e07429a 100644 --- a/include/Nebulite/Data/Document/Json.hpp +++ b/include/Nebulite/Data/Document/Json.hpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -276,6 +277,27 @@ class Json { template void set(std::string_view key, T const& val); + /** + * @brief Replaces the value at a key with an array containing the supplied range. + * @details Existing values and cached child entries at the key are discarded. + * @tparam R The type of the range to insert. + * @param key The key of the array to replace. + * @param values The values to insert into the new array. + */ + template + void insertArray(std::string_view key, R values); + + /** + * @brief Appends a range of values to the array at a key. + * @details Existing cached values are flushed before appending, and cached child + * entries are synchronized afterward. + * @tparam R The type of the range to append. + * @param key The key of the array to append to. + * @param values The values to append. + */ + template + void appendArray(std::string_view key, R values); + /** * @brief Sets a variant value of supported simple values in the JSON document. * Stable double pointer of the value is updated as well. Child keys are invalidated. diff --git a/include/Nebulite/Data/Document/Json.tpp b/include/Nebulite/Data/Document/Json.tpp index bce0ceff..be7a3646 100644 --- a/include/Nebulite/Data/Document/Json.tpp +++ b/include/Nebulite/Data/Document/Json.tpp @@ -7,6 +7,8 @@ // Standard library #include #include +#include +#include #include #include #include @@ -54,6 +56,34 @@ void Json::set(std::string_view const key, T const& val){ } } +template +void Json::insertArray(std::string_view const key, R values) { + std::scoped_lock const lockGuard(mtx); + helperNonConstVar++; + flush(key); + auto* const value = RjDirectAccess::ensurePath(key, doc, doc.GetAllocator()); + if (value == nullptr) { + throw std::runtime_error("Failed to create or access path: " + std::string(key)); + } + RjDirectAccess::insertArray(value, values, doc.GetAllocator()); + cache.deleteEntry(key); + synchronizeChildren(key); +} + +template +void Json::appendArray(std::string_view const key, R values) { + std::scoped_lock const lockGuard(mtx); + helperNonConstVar++; + flush(key); + auto* const value = RjDirectAccess::ensurePath(key, doc, doc.GetAllocator()); + if (value == nullptr) { + throw std::runtime_error("Failed to create or access path: " + std::string(key)); + } + RjDirectAccess::appendArray(value, values, doc.GetAllocator()); + cache.deleteEntry(key); + synchronizeChildren(key); +} + template std::expected Json::get(std::string_view const key) const { std::scoped_lock const lockGuard(mtx); diff --git a/include/Nebulite/Data/Document/JsonScope.hpp b/include/Nebulite/Data/Document/JsonScope.hpp index d13e7b8c..15ce11b8 100644 --- a/include/Nebulite/Data/Document/JsonScope.hpp +++ b/include/Nebulite/Data/Document/JsonScope.hpp @@ -204,6 +204,16 @@ class JsonScope final { template void set(ScopedKeyView const& key, T const& value); template void set(ScopedKey const& key, T const& value){set(key.view(), value);} + template + void insertArray(ScopedKeyView const& key, R values); + template + void insertArray(ScopedKey const& key, R values){insertArray(key.view(), values);} + + template + void appendArray(ScopedKeyView const& key, R values); + template + void appendArray(ScopedKey const& key, R values){appendArray(key.view(), values);} + void setVariant(ScopedKeyView const& key, RjDirectAccess::SimpleValue const& value); void setVariant(ScopedKey const& key, RjDirectAccess::SimpleValue const& value); diff --git a/include/Nebulite/Data/Document/JsonScope.tpp b/include/Nebulite/Data/Document/JsonScope.tpp index 2db388ba..776ae979 100644 --- a/include/Nebulite/Data/Document/JsonScope.tpp +++ b/include/Nebulite/Data/Document/JsonScope.tpp @@ -83,6 +83,16 @@ void JsonScope::setArray(ScopedKeyView const& key, R const& range) { } } +template +void JsonScope::insertArray(ScopedKeyView const& key, R values) { + baseDocument->insertArray(key.full(*this), values); +} + +template +void JsonScope::appendArray(ScopedKeyView const& key, R values) { + baseDocument->appendArray(key.full(*this), values); +} + template requires std::same_as>,ScopedKeyView> double** JsonScope::ensureOrderedCacheList(std::uint64_t uniqueId, R const& keys) { thread_local std::size_t const threadIndex = assignCacheLookupIndex(); diff --git a/src/Nebulite/Data/Document/Json.cpp b/src/Nebulite/Data/Document/Json.cpp index 3482d337..2f95a276 100644 --- a/src/Nebulite/Data/Document/Json.cpp +++ b/src/Nebulite/Data/Document/Json.cpp @@ -433,7 +433,10 @@ void Json::setEmptyArray(std::string_view const key) { helperNonConstVar++; // Signal non-const operation flush(key); rapidjson::Value* val = RjDirectAccess::ensurePath(key, doc, doc.GetAllocator()); - val->SetArray(); + if (val == nullptr) { + throw std::runtime_error("Failed to create or access path: " + std::string(key)); + } + RjDirectAccess::setEmptyArray(val); } void Json::setEmptyObject(std::string_view const key) { @@ -441,6 +444,9 @@ void Json::setEmptyObject(std::string_view const key) { helperNonConstVar++; // Signal non-const operation flush(key); rapidjson::Value* val = RjDirectAccess::ensurePath(key, doc, doc.GetAllocator()); + if (val == nullptr) { + throw std::runtime_error("Failed to create or access path: " + std::string(key)); + } val->SetObject(); } From 19903190a9184662b8e3caed9cf82530cf6f6098 Mon Sep 17 00:00:00 2001 From: Leo Date: Sat, 31 Oct 2026 11:24:42 +0100 Subject: [PATCH 03/15] Refactor insertArray and appendArray methods to accept const references for improved performance, add range-type-specific insertion functionality. --- include/Nebulite/Data/Document/Json.hpp | 4 +-- include/Nebulite/Data/Document/Json.tpp | 4 +-- include/Nebulite/Data/Document/JsonScope.hpp | 8 +++--- include/Nebulite/Data/Document/JsonScope.tpp | 4 +-- .../Nebulite/Data/Document/RjDirectAccess.hpp | 4 +-- .../Nebulite/Data/Document/RjDirectAccess.tpp | 26 ++++++++++++++++--- 6 files changed, 34 insertions(+), 16 deletions(-) diff --git a/include/Nebulite/Data/Document/Json.hpp b/include/Nebulite/Data/Document/Json.hpp index 8e07429a..2c778d49 100644 --- a/include/Nebulite/Data/Document/Json.hpp +++ b/include/Nebulite/Data/Document/Json.hpp @@ -285,7 +285,7 @@ class Json { * @param values The values to insert into the new array. */ template - void insertArray(std::string_view key, R values); + void insertArray(std::string_view key, R const& values); /** * @brief Appends a range of values to the array at a key. @@ -296,7 +296,7 @@ class Json { * @param values The values to append. */ template - void appendArray(std::string_view key, R values); + void appendArray(std::string_view key, R const& values); /** * @brief Sets a variant value of supported simple values in the JSON document. diff --git a/include/Nebulite/Data/Document/Json.tpp b/include/Nebulite/Data/Document/Json.tpp index be7a3646..d9511c97 100644 --- a/include/Nebulite/Data/Document/Json.tpp +++ b/include/Nebulite/Data/Document/Json.tpp @@ -57,7 +57,7 @@ void Json::set(std::string_view const key, T const& val){ } template -void Json::insertArray(std::string_view const key, R values) { +void Json::insertArray(std::string_view const key, R const& values) { std::scoped_lock const lockGuard(mtx); helperNonConstVar++; flush(key); @@ -71,7 +71,7 @@ void Json::insertArray(std::string_view const key, R values) { } template -void Json::appendArray(std::string_view const key, R values) { +void Json::appendArray(std::string_view const key, R const& values) { std::scoped_lock const lockGuard(mtx); helperNonConstVar++; flush(key); diff --git a/include/Nebulite/Data/Document/JsonScope.hpp b/include/Nebulite/Data/Document/JsonScope.hpp index 15ce11b8..d7356abc 100644 --- a/include/Nebulite/Data/Document/JsonScope.hpp +++ b/include/Nebulite/Data/Document/JsonScope.hpp @@ -205,14 +205,14 @@ class JsonScope final { template void set(ScopedKey const& key, T const& value){set(key.view(), value);} template - void insertArray(ScopedKeyView const& key, R values); + void insertArray(ScopedKeyView const& key, R const& values); template - void insertArray(ScopedKey const& key, R values){insertArray(key.view(), values);} + void insertArray(ScopedKey const& key, R const& values){insertArray(key.view(), values);} template - void appendArray(ScopedKeyView const& key, R values); + void appendArray(ScopedKeyView const& key, R const& values); template - void appendArray(ScopedKey const& key, R values){appendArray(key.view(), values);} + void appendArray(ScopedKey const& key, R const& values){appendArray(key.view(), values);} void setVariant(ScopedKeyView const& key, RjDirectAccess::SimpleValue const& value); void setVariant(ScopedKey const& key, RjDirectAccess::SimpleValue const& value); diff --git a/include/Nebulite/Data/Document/JsonScope.tpp b/include/Nebulite/Data/Document/JsonScope.tpp index 776ae979..bd034b0c 100644 --- a/include/Nebulite/Data/Document/JsonScope.tpp +++ b/include/Nebulite/Data/Document/JsonScope.tpp @@ -84,12 +84,12 @@ void JsonScope::setArray(ScopedKeyView const& key, R const& range) { } template -void JsonScope::insertArray(ScopedKeyView const& key, R values) { +void JsonScope::insertArray(ScopedKeyView const& key, R const& values) { baseDocument->insertArray(key.full(*this), values); } template -void JsonScope::appendArray(ScopedKeyView const& key, R values) { +void JsonScope::appendArray(ScopedKeyView const& key, R const& values) { baseDocument->appendArray(key.full(*this), values); } diff --git a/include/Nebulite/Data/Document/RjDirectAccess.hpp b/include/Nebulite/Data/Document/RjDirectAccess.hpp index bba98b9b..70816b12 100644 --- a/include/Nebulite/Data/Document/RjDirectAccess.hpp +++ b/include/Nebulite/Data/Document/RjDirectAccess.hpp @@ -136,7 +136,7 @@ namespace Nebulite::Data::RjDirectAccess { * @param allocator The allocator to use when adding values to the array. */ template - void insertArray(rapidjson::Value* val, R values, rapidjson::Document::AllocatorType& allocator); + void insertArray(rapidjson::Value* val, R const& values, rapidjson::Document::AllocatorType& allocator); /** * @brief Appends a range of values to a rapidjson array. @@ -148,7 +148,7 @@ namespace Nebulite::Data::RjDirectAccess { * @param allocator The allocator to use when adding values to the array. */ template - void appendArray(rapidjson::Value* val, R values, rapidjson::Document::AllocatorType& allocator); + void appendArray(rapidjson::Value* val, R const& values, rapidjson::Document::AllocatorType& allocator); //------------------------------------------ // Conversion diff --git a/include/Nebulite/Data/Document/RjDirectAccess.tpp b/include/Nebulite/Data/Document/RjDirectAccess.tpp index 95cc7bbf..bd16e7df 100644 --- a/include/Nebulite/Data/Document/RjDirectAccess.tpp +++ b/include/Nebulite/Data/Document/RjDirectAccess.tpp @@ -158,19 +158,37 @@ bool set(std::string_view const key, T const& value, rapidjson::Value& val, rapi //------------------------------------------ // Array +namespace { +template +void appendArrayValue(rapidjson::Value* val, T const& value, rapidjson::Document::AllocatorType& allocator) { + using ValueT = std::remove_cvref_t; + if constexpr (std::is_same_v || std::is_same_v) { + rapidjson::Value stringValue( + value.data(), + static_cast(value.size()), + allocator + ); + val->PushBack(stringValue, allocator); + } + else { + val->PushBack(value, allocator); + } +} +} // anonymous namespace + template -void insertArray(rapidjson::Value* val, R values, rapidjson::Document::AllocatorType& allocator) { +void insertArray(rapidjson::Value* val, R const& values, rapidjson::Document::AllocatorType& allocator) { setEmptyArray(val); for (auto const& v : values) { - val->PushBack(v, allocator); + appendArrayValue(val, v, allocator); } } template -void appendArray(rapidjson::Value* val, R values, rapidjson::Document::AllocatorType& allocator) { +void appendArray(rapidjson::Value* val, R const& values, rapidjson::Document::AllocatorType& allocator) { ensureArray(val, allocator); for (auto const& v : values) { - val->PushBack(v, allocator); + appendArrayValue(val, v, allocator); } } From 10c436aaa427d913a378017ea2f5610e4b7abbcc Mon Sep 17 00:00:00 2001 From: Leo Date: Sat, 31 Oct 2026 11:34:56 +0100 Subject: [PATCH 04/15] Simplify listMembers method by replacing manual member listing with insertArray for streamlined array insertion --- src/Nebulite/Module/Transformation/Collection.cpp | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/Nebulite/Module/Transformation/Collection.cpp b/src/Nebulite/Module/Transformation/Collection.cpp index fcfedb0f..925d042f 100644 --- a/src/Nebulite/Module/Transformation/Collection.cpp +++ b/src/Nebulite/Module/Transformation/Collection.cpp @@ -67,16 +67,9 @@ bool Collection::get(Utility::Args::SegmentedStringView const& args, Data::JsonS } bool Collection::listMembers(Data::JsonScope& jsonDoc){ - auto const membersAndKeys = jsonDoc.listAvailableMembersAndKeys(rootKey); - jsonDoc.removeMember(rootKey); - jsonDoc.setEmptyArray(rootKey); - std::ranges::for_each( - membersAndKeys | Utility::Ranges::enumerate, - [&](auto const& enumeratedMemberAndKey) { - auto const& [index, memberAndKey] = enumeratedMemberAndKey; - auto const& [member, _] = memberAndKey; - jsonDoc.set(rootKey.addIndex(index),member); - } + jsonDoc.insertArray( + rootKey, + jsonDoc.listAvailableMembers(rootKey) ); return true; } From 374cdc38ac879ba86cd91207f2ec0b68810526c7 Mon Sep 17 00:00:00 2001 From: Leo Date: Sat, 31 Oct 2026 11:53:00 +0100 Subject: [PATCH 05/15] Add support for boolean and integral types in rapidjson value insertion --- .../Nebulite/Data/Document/RjDirectAccess.tpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/include/Nebulite/Data/Document/RjDirectAccess.tpp b/include/Nebulite/Data/Document/RjDirectAccess.tpp index bd16e7df..11680bde 100644 --- a/include/Nebulite/Data/Document/RjDirectAccess.tpp +++ b/include/Nebulite/Data/Document/RjDirectAccess.tpp @@ -170,6 +170,26 @@ void appendArrayValue(rapidjson::Value* val, T const& value, rapidjson::Document ); val->PushBack(stringValue, allocator); } + else if constexpr (std::is_same_v) { + rapidjson::Value jsonValue; + jsonValue.SetBool(value); + val->PushBack(jsonValue, allocator); + } + else if constexpr (std::is_integral_v && std::is_signed_v) { + rapidjson::Value jsonValue; + jsonValue.SetInt64(static_cast(value)); + val->PushBack(jsonValue, allocator); + } + else if constexpr (std::is_integral_v && std::is_unsigned_v) { + rapidjson::Value jsonValue; + jsonValue.SetUint64(static_cast(value)); + val->PushBack(jsonValue, allocator); + } + else if constexpr (std::is_floating_point_v) { + rapidjson::Value jsonValue; + jsonValue.SetDouble(static_cast(value)); + val->PushBack(jsonValue, allocator); + } else { val->PushBack(value, allocator); } From 730ee4d43daa8f847f631fd59cea5ba2471138f3 Mon Sep 17 00:00:00 2001 From: Leo Date: Sat, 31 Oct 2026 12:26:06 +0100 Subject: [PATCH 06/15] Add support for complex number representation in rapidjson values --- .../Nebulite/Data/Document/RjDirectAccess.hpp | 14 ++++- .../Nebulite/Data/Document/RjDirectAccess.tpp | 62 ++++++++++--------- src/Nebulite/Data/Document/RjDirectAccess.cpp | 10 +++ 3 files changed, 57 insertions(+), 29 deletions(-) diff --git a/include/Nebulite/Data/Document/RjDirectAccess.hpp b/include/Nebulite/Data/Document/RjDirectAccess.hpp index 70816b12..7d9dd019 100644 --- a/include/Nebulite/Data/Document/RjDirectAccess.hpp +++ b/include/Nebulite/Data/Document/RjDirectAccess.hpp @@ -6,6 +6,7 @@ // Standard library #include // NOLINT +#include #include #include #include @@ -23,6 +24,9 @@ * @brief Provides direct access and manipulation of RapidJSON values. */ namespace Nebulite::Data::RjDirectAccess { + static auto constexpr complexRealMember = "re"; + static auto constexpr complexImaginaryMember = "im"; + /** * @brief Used flags for rapidjson parsing, allowing comments and trailing commas in JSON. */ @@ -63,7 +67,7 @@ namespace Nebulite::Data::RjDirectAccess { std::optional getSimpleValue(std::string_view key, RjValType& doc); //------------------------------------------ - // Templated Getter, Setter + // Getter, Setter /** * @brief Fallback to direct rapidjson access for getting values. @@ -111,6 +115,14 @@ namespace Nebulite::Data::RjDirectAccess { template requires (!std::is_trivially_copyable_v) bool set(std::string_view key, T const& value, rapidjson::Value& val, rapidjson::Document::AllocatorType& allocator); + /** + * @brief Sets a rapidjson value to the object representation of a complex number. + * @param val The rapidjson value to modify. + * @param value The complex number to store. + * @param allocator The allocator to use for creating object members. + */ + void setComplex(rapidjson::Value* val, std::complex const& value, rapidjson::Document::AllocatorType& allocator); + //------------------------------------------ // Array diff --git a/include/Nebulite/Data/Document/RjDirectAccess.tpp b/include/Nebulite/Data/Document/RjDirectAccess.tpp index 11680bde..ac5c95e3 100644 --- a/include/Nebulite/Data/Document/RjDirectAccess.tpp +++ b/include/Nebulite/Data/Document/RjDirectAccess.tpp @@ -5,6 +5,7 @@ // Includes // Standard library +#include #include // NOLINT #include #include @@ -91,19 +92,19 @@ std::optional convertSimpleValue(SimpleValue const& simpleValue) { template std::optional getSimpleValue(std::string_view key, RjValType& doc) { - // The given RjValType should be a Document. - // If we pass a rapidjson value, we risk not starting at the top of the document, where we should apply the key traversal. - // This fixes implicit conversion worries. - static_assert( - std::is_same_v, - "The given Rapidjson Value type should be a document to ensure the key traversal happens at the top! " - "Passing, for example, a rapidjson::Value would risk starting the traversal at the wrong point in the document, which could lead to incorrect retrieval or failure to find the value." - ); - - if (auto const rjVal = traversePath(key, doc); rjVal != nullptr) { - return getSimpleValue(rjVal); - } - return std::nullopt; + // The given RjValType should be a Document. + // If we pass a rapidjson value, we risk not starting at the top of the document, where we should apply the key traversal. + // This fixes implicit conversion worries. + static_assert( + std::is_same_v, + "The given Rapidjson Value type should be a document to ensure the key traversal happens at the top! " + "Passing, for example, a rapidjson::Value would risk starting the traversal at the wrong point in the document, which could lead to incorrect retrieval or failure to find the value." + ); + + if (auto const rjVal = traversePath(key, doc); rjVal != nullptr) { + return getSimpleValue(rjVal); + } + return std::nullopt; } //------------------------------------------ @@ -111,15 +112,15 @@ std::optional getSimpleValue(std::string_view key, RjValType& doc) template requires (std::is_trivially_copyable_v) T get(std::string_view const key, T const defaultValue, rapidjson::Value& val) { - rapidjson::Value const* keyVal = traversePath(key, val); - if (keyVal == nullptr) { - // Value doesn't exist in doc, return default - return defaultValue; - } - // Base case: convert currentVal to T using JSONHandler - T tmp; - convertFromJsonValue(*keyVal, tmp, defaultValue); - return tmp; + rapidjson::Value const* keyVal = traversePath(key, val); + if (keyVal == nullptr) { + // Value doesn't exist in doc, return default + return defaultValue; + } + // Base case: convert currentVal to T using JSONHandler + T tmp; + convertFromJsonValue(*keyVal, tmp, defaultValue); + return tmp; } template requires (!std::is_trivially_copyable_v) @@ -137,12 +138,12 @@ T get(std::string_view const key, T const& defaultValue, rapidjson::Value& val) template requires (std::is_trivially_copyable_v) bool set(std::string_view const key, T const value, rapidjson::Value& val, rapidjson::Document::AllocatorType& allocator) { - // Ensure key path exists - if (rapidjson::Value* keyVal = ensurePath(key, val, allocator); keyVal != nullptr) { - convertToJsonValue(value, *keyVal, allocator); - return true; - } - return false; + // Ensure key path exists + if (rapidjson::Value* keyVal = ensurePath(key, val, allocator); keyVal != nullptr) { + convertToJsonValue(value, *keyVal, allocator); + return true; + } + return false; } template requires (!std::is_trivially_copyable_v) @@ -190,6 +191,11 @@ void appendArrayValue(rapidjson::Value* val, T const& value, rapidjson::Document jsonValue.SetDouble(static_cast(value)); val->PushBack(jsonValue, allocator); } + else if constexpr (std::is_same_v>) { + rapidjson::Value jsonValue; + setComplex(&jsonValue, value, allocator); + val->PushBack(jsonValue, allocator); + } else { val->PushBack(value, allocator); } diff --git a/src/Nebulite/Data/Document/RjDirectAccess.cpp b/src/Nebulite/Data/Document/RjDirectAccess.cpp index e185cd94..16ca52f4 100644 --- a/src/Nebulite/Data/Document/RjDirectAccess.cpp +++ b/src/Nebulite/Data/Document/RjDirectAccess.cpp @@ -4,6 +4,7 @@ // Standard library #include #include +#include #include #include #include @@ -49,6 +50,15 @@ std::optional getSimpleValue(rapidjson::Value const* val) { return std::nullopt; } +//------------------------------------------ +// Getter, Setter + +void setComplex(rapidjson::Value* val, std::complex const& value, rapidjson::Document::AllocatorType& allocator) { + val->SetObject(); + val->AddMember(rapidjson::StringRef(complexRealMember), value.real(), allocator); + val->AddMember(rapidjson::StringRef(complexImaginaryMember), value.imag(), allocator); +} + //------------------------------------------ // Array From 120b1a1719ef09c5137a46f7b7957293ff9ec106 Mon Sep 17 00:00:00 2001 From: Leo Date: Sat, 31 Oct 2026 12:27:41 +0100 Subject: [PATCH 07/15] Add support for complex number retrieval and insertion in JSON --- include/Nebulite/Data/Document/Json.hpp | 18 +++++++++++++++ src/Nebulite/Data/Document/Json.cpp | 29 +++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/include/Nebulite/Data/Document/Json.hpp b/include/Nebulite/Data/Document/Json.hpp index 2c778d49..3a6fe14f 100644 --- a/include/Nebulite/Data/Document/Json.hpp +++ b/include/Nebulite/Data/Document/Json.hpp @@ -5,11 +5,13 @@ // Includes // Standard library +#include #include #include // NOLINT #include #include #include +#include #include #include #include @@ -250,6 +252,14 @@ class Json { */ Json getSubDoc(std::string_view key) const ; + /** + * @brief Gets a complex value from the JSON document. + * @details The value is read from an object with `re` and `im` members. + * @param key The key of the complex value to retrieve. + * @return The complex value, or std::nullopt if either component is unavailable. + */ + std::optional> getComplex(std::string_view key) const; + /** * @brief Gets a pointer to a double value pointer in the JSON document. * @return A pointer to the double value associated with the key. @@ -306,6 +316,14 @@ class Json { */ void setVariant(std::string_view key, RjDirectAccess::SimpleValue const& val); + /** + * @brief Sets a complex value in the JSON document. + * @details The value is stored as an object with `re` and `im` members. + * @param key The key of the complex value to set. + * @param value The complex value to set. + */ + void setComplex(std::string_view key, std::complex const& value); + /** * @brief Sets a sub-document in the JSON document. * @details If the key already exists, the sub-document is updated. diff --git a/src/Nebulite/Data/Document/Json.cpp b/src/Nebulite/Data/Document/Json.cpp index 2f95a276..241cea6e 100644 --- a/src/Nebulite/Data/Document/Json.cpp +++ b/src/Nebulite/Data/Document/Json.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include // NOLINT #include @@ -281,6 +282,22 @@ Json Json::getSubDoc(std::string_view const key) const { return Json{}; } +std::optional> Json::getComplex(std::string_view const key) const { + auto const memberKey = [key](std::string_view const member) { + return key.empty() + ? std::string(member) + : std::string(key) + "." + std::string(member); + }; + auto const realKey = memberKey(RjDirectAccess::complexRealMember); + auto const imaginaryKey = memberKey(RjDirectAccess::complexImaginaryMember); + auto const realPart = get(realKey); + auto const imaginaryPart = get(imaginaryKey); + if (realPart.has_value() && imaginaryPart.has_value()) { + return std::complex(realPart.value(), imaginaryPart.value()); + } + return std::nullopt; +} + double* Json::getStableDoublePointer(std::string_view const key) const { std::scoped_lock const lockGuard(mtx); @@ -439,6 +456,18 @@ void Json::setEmptyArray(std::string_view const key) { RjDirectAccess::setEmptyArray(val); } +void Json::setComplex(std::string_view const key, std::complex const& value) { + std::scoped_lock const lockGuard(mtx); + helperNonConstVar++; // Signal non-const operation + flush(key); + rapidjson::Value* val = RjDirectAccess::ensurePath(key, doc, doc.GetAllocator()); + if (val == nullptr) { + throw std::runtime_error("Failed to create or access path: " + std::string(key)); + } + RjDirectAccess::setComplex(val, value, doc.GetAllocator()); + synchronizeChildren(key); +} + void Json::setEmptyObject(std::string_view const key) { std::scoped_lock const lockGuard(mtx); helperNonConstVar++; // Signal non-const operation From 9bbac350e2c0850d4e63a4beadc79639ba47d99a Mon Sep 17 00:00:00 2001 From: Leo Date: Sat, 31 Oct 2026 12:28:32 +0100 Subject: [PATCH 08/15] Refactor complex number handling in JsonScope for improved clarity and performance, remove old setArray function. --- include/Nebulite/Data/Document/JsonScope.hpp | 17 +++------------ include/Nebulite/Data/Document/JsonScope.tpp | 17 --------------- src/Nebulite/Data/Document/JsonScope.cpp | 23 ++++++++------------ 3 files changed, 12 insertions(+), 45 deletions(-) diff --git a/include/Nebulite/Data/Document/JsonScope.hpp b/include/Nebulite/Data/Document/JsonScope.hpp index d7356abc..7c6c3ec1 100644 --- a/include/Nebulite/Data/Document/JsonScope.hpp +++ b/include/Nebulite/Data/Document/JsonScope.hpp @@ -79,13 +79,6 @@ class JsonScope final { cacheLookupThreadCount> odpCache; - //------------------------------------------ - // Complex number prefixes - - static auto constexpr complexRe = "re"; - static auto constexpr complexIm = "im"; - - //------------------------------------------ // Valid prefix check and generation /** @@ -223,19 +216,15 @@ class JsonScope final { void setSubDoc(ScopedKeyView const& key, JsonScope const& subDoc); void setSubDoc(ScopedKey const& key, JsonScope const& subDoc); - // Later on, we should use C++26 reflection once widely available, see branch feature/jsonscope/reflection - template - void setArray(ScopedKeyView const& key, R const& range); - void setEmptyArray(ScopedKeyView const& key); void setEmptyArray(ScopedKey const& key); - void setComplex(ScopedKeyView const& key, std::complex const& value); - void setComplex(ScopedKey const& key, std::complex const& value); - void setEmptyObject(ScopedKeyView const& key); void setEmptyObject(ScopedKey const& key); + void setComplex(ScopedKeyView const& key, std::complex const& value); + void setComplex(ScopedKey const& key, std::complex const& value); + //------------------------------------------ // Special sets for threadsafe maths operations diff --git a/include/Nebulite/Data/Document/JsonScope.tpp b/include/Nebulite/Data/Document/JsonScope.tpp index bd034b0c..27bfc299 100644 --- a/include/Nebulite/Data/Document/JsonScope.tpp +++ b/include/Nebulite/Data/Document/JsonScope.tpp @@ -21,7 +21,6 @@ #include "Nebulite/Data/Document/Json.hpp" #include "Nebulite/Data/Document/ScopedKeyView.hpp" #include "Nebulite/Data/Document/SimpleValueError.hpp" -#include "Nebulite/Utility/Ranges.hpp" //------------------------------------------ // Conditional includes @@ -67,22 +66,6 @@ void JsonScope::set(ScopedKeyView const& key, T const& value) { baseDocument->set(key.full(*this), value); } -template -void JsonScope::setArray(ScopedKeyView const& key, R const& range) { - setEmptyArray(key); - for (auto const [index, indexKey] : getArrayKeys(key, range.size()) | Utility::Ranges::enumerate) { - if constexpr (std::is_same_v>) { - setComplex(indexKey, range[index]); - } - else if constexpr (std::is_same_v || std::is_same_v) { - setSubDoc(indexKey, range[index]); - } - else { - set(indexKey, range[index]); - } - } -} - template void JsonScope::insertArray(ScopedKeyView const& key, R const& values) { baseDocument->insertArray(key.full(*this), values); diff --git a/src/Nebulite/Data/Document/JsonScope.cpp b/src/Nebulite/Data/Document/JsonScope.cpp index d1c1c8c5..dc58f659 100644 --- a/src/Nebulite/Data/Document/JsonScope.cpp +++ b/src/Nebulite/Data/Document/JsonScope.cpp @@ -128,10 +128,7 @@ double* JsonScope::getStableDoublePointer(ScopedKey const& key) const { } std::optional> JsonScope::getComplex(ScopedKeyView const& key) const { - if (auto const [realPart, imagPart] = getMultiple(key.addMember(complexRe).view(), key.addMember(complexIm).view()); realPart.has_value() && imagPart.has_value()) { - return {std::complex(realPart.value(), imagPart.value())}; - } - return std::nullopt; + return baseDocument->getComplex(key.full(*this)); } std::optional> JsonScope::getComplex(ScopedKey const& key) const { @@ -176,16 +173,6 @@ void JsonScope::setEmptyArray(ScopedKey const& key) { setEmptyArray(key.view()); } -void JsonScope::setComplex(ScopedKeyView const& key, std::complex const& value){ - doc().removeMember(key.full(*this)); // Remove any existing member to avoid type conflicts - doc().set(key.addMember(complexRe).view().full(*this), value.real()); - doc().set(key.addMember(complexIm).view().full(*this), value.imag()); -} - -void JsonScope::setComplex(ScopedKey const& key, std::complex const& value) { - setComplex(key.view(), value); -} - void JsonScope::setEmptyObject(ScopedKeyView const& key){ doc().setEmptyObject(key.full(*this)); } @@ -194,6 +181,14 @@ void JsonScope::setEmptyObject(ScopedKey const& key) { setEmptyObject(key.view()); } +void JsonScope::setComplex(ScopedKeyView const& key, std::complex const& value){ + doc().setComplex(key.full(*this), value); +} + +void JsonScope::setComplex(ScopedKey const& key, std::complex const& value) { + setComplex(key.view(), value); +} + //------------------------------------------ // Special sets for threadsafe maths operations From 7e09c2f2e7ee1f27a915dd686da2527daef82665 Mon Sep 17 00:00:00 2001 From: Leo Date: Sat, 31 Oct 2026 12:29:41 +0100 Subject: [PATCH 09/15] Replace setArray with insertArray for FFT results to streamline JSON array insertion --- src/Nebulite/Module/Transformation/FFT.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Nebulite/Module/Transformation/FFT.cpp b/src/Nebulite/Module/Transformation/FFT.cpp index cd0477ef..1fcf4078 100644 --- a/src/Nebulite/Module/Transformation/FFT.cpp +++ b/src/Nebulite/Module/Transformation/FFT.cpp @@ -44,7 +44,7 @@ bool Fft::applyFft(Data::JsonScope& jsonDoc) { return true; } auto const result = Math::Fft::fft(samples.value()); - jsonDoc.setArray(rootKey, result); + jsonDoc.insertArray(rootKey, result); return true; } @@ -63,7 +63,7 @@ bool Fft::applyIfft(Data::JsonScope& jsonDoc) { return false; } auto const result = Math::Fft::fftInverse(samples.value()); - jsonDoc.setArray(rootKey, result); + jsonDoc.insertArray(rootKey, result); return true; } @@ -102,7 +102,7 @@ bool Fft::applyTransferFunctionFrequencyDomain(Utility::Args::SegmentedStringVie } auto const result = Math::Fft::applyTransferFunctionFrequencyDomain(samples.value(), num.value(), den.value()); - jsonDoc.setArray(rootKey, result); + jsonDoc.insertArray(rootKey, result); return true; } From 8d2fb0f855010613ad97db238d9b72fca1f2c6bb Mon Sep 17 00:00:00 2001 From: Leo Date: Sat, 31 Oct 2026 12:30:47 +0100 Subject: [PATCH 10/15] Use insertArray for Array::iota generation --- src/Nebulite/Module/Transformation/Array.cpp | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/Nebulite/Module/Transformation/Array.cpp b/src/Nebulite/Module/Transformation/Array.cpp index 4cce197d..c342015b 100644 --- a/src/Nebulite/Module/Transformation/Array.cpp +++ b/src/Nebulite/Module/Transformation/Array.cpp @@ -440,14 +440,7 @@ bool Array::iota(Utility::Args::SegmentedStringView const& args, Data::JsonScope jsonDoc.setEmptyArray(rootKey); return true; } - std::ranges::for_each( - std::views::iota(start, end), - [&](auto const& i) { - auto index = i - start; - auto const key = rootKey.addIndex(static_cast(index)); - jsonDoc.set(key, i); - } - ); + jsonDoc.insertArray(rootKey, std::views::iota(start, end)); return true; } From 6b95a16874a29a4fc6222b1bc98e12765e0ccad2 Mon Sep 17 00:00:00 2001 From: Leo Date: Sat, 31 Oct 2026 12:53:39 +0100 Subject: [PATCH 11/15] Add TODOs for scientific notation support and large number formatting in Formatter --- include/Nebulite/Interaction/Logic/Formatter.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/Nebulite/Interaction/Logic/Formatter.hpp b/include/Nebulite/Interaction/Logic/Formatter.hpp index 16d17704..f030dfb4 100644 --- a/include/Nebulite/Interaction/Logic/Formatter.hpp +++ b/include/Nebulite/Interaction/Logic/Formatter.hpp @@ -23,6 +23,7 @@ struct Formatter { /** * @enum Nebulite::Interaction::Logic::Formatter::CastType * @brief Represents the type of cast to apply to an expression component. + * @todo Add support for scientific notation */ enum class CastType : std::uint8_t { none, // No formatting @@ -39,6 +40,7 @@ struct Formatter { */ static Formatter readFormatter(std::string_view formatter); + // TODO: Cuts off large numbers if formatter size is too short [[nodiscard]] std::string format(double value) const ; }; } // namespace Nebulite::Interaction::Logic From abafc60e8ce9a64edda8450f513a9d4dcfbf0728 Mon Sep 17 00:00:00 2001 From: Leo Date: Sat, 31 Oct 2026 12:55:16 +0100 Subject: [PATCH 12/15] Add benchmarks for FFT implementation using feature-test and transformation pipeline --- TaskFiles/Benchmarks/fft.nebs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/TaskFiles/Benchmarks/fft.nebs b/TaskFiles/Benchmarks/fft.nebs index 4e6d46ee..823dfb3e 100644 --- a/TaskFiles/Benchmarks/fft.nebs +++ b/TaskFiles/Benchmarks/fft.nebs @@ -1,5 +1,28 @@ # Testing benchmark for FFT (Fast Fourier Transform) implementation -# Using the featuretest module so we don' have to benchmark with the slower transformation fft. +# Setup +set-fps 5000 +wait 1 + +# Normal FFT benchmark using the optimized feature-test implementation. +echo "" +echo "" +echo "------------------------------------" feature-test large-fft 10000000 +feature-test large-fft 10000 + +# FFT benchmark using the transformation pipeline. The input is generated with +# iota and transformed element-by-element before running the FFT transformation. +echo "" +echo "" +echo "------------------------------------" +wait 1 +eval set pipeline.start {global:time.runtime.t} +assign global:result = {|iota 0 10000|map mod 100|fft} +wait 1 +eval set pipeline.end {global:time.runtime.t} +eval echo "Pipeline FFT took $6.4f({global:pipeline.end} - {global:pipeline.start}) seconds for N=10000." +eval echo "First 10 results of {global:result|length} total:" +eval echo {global:result|head 10|map complexToString 10.2f|serialize} + exit \ No newline at end of file From 202a09f68a25018fe6532dc6a8f5f5366fe11ce9 Mon Sep 17 00:00:00 2001 From: Leo Date: Sat, 31 Oct 2026 13:31:26 +0100 Subject: [PATCH 13/15] Add ensureArray method to Json and JsonScope for array key management --- include/Nebulite/Data/Document/Json.hpp | 8 ++++++++ include/Nebulite/Data/Document/JsonScope.hpp | 3 +++ src/Nebulite/Data/Document/Json.cpp | 13 +++++++++++++ src/Nebulite/Data/Document/JsonScope.cpp | 9 +++++++++ 4 files changed, 33 insertions(+) diff --git a/include/Nebulite/Data/Document/Json.hpp b/include/Nebulite/Data/Document/Json.hpp index 3a6fe14f..6c54619e 100644 --- a/include/Nebulite/Data/Document/Json.hpp +++ b/include/Nebulite/Data/Document/Json.hpp @@ -442,6 +442,14 @@ class Json { */ void copyMember(std::string_view fromKey, std::string_view toKey); + /** + * @brief Ensures that a key exists and is an array. + * @details If the key does not exist, it will be created as an empty array. + * If the key exists but is not an array, the value is inserted into an array. + * @param key The key to ensure is an array. + */ + void ensureArray(std::string_view key); + /** * @brief Lists all available member keys in a rapidjson object. diff --git a/include/Nebulite/Data/Document/JsonScope.hpp b/include/Nebulite/Data/Document/JsonScope.hpp index 7c6c3ec1..07e2ed0c 100644 --- a/include/Nebulite/Data/Document/JsonScope.hpp +++ b/include/Nebulite/Data/Document/JsonScope.hpp @@ -316,6 +316,9 @@ class JsonScope final { void copyMember(ScopedKeyView const& fromKey, ScopedKey const& toKey); void copyMember(ScopedKey const& fromKey, ScopedKeyView const& toKey); + void ensureArray(ScopedKeyView const& key); + void ensureArray(ScopedKey const& key); + //------------------------------------------ // Member listing diff --git a/src/Nebulite/Data/Document/Json.cpp b/src/Nebulite/Data/Document/Json.cpp index 241cea6e..773695e6 100644 --- a/src/Nebulite/Data/Document/Json.cpp +++ b/src/Nebulite/Data/Document/Json.cpp @@ -763,6 +763,19 @@ void Json::copyMember(std::string_view const fromKey, std::string_view const toK setSubDoc(toKey, *this, fromKey); } +void Json::ensureArray(std::string_view key){ + // TODO: use key for flush and synchronizeChildren? + std::scoped_lock const lockGuard(mtx); + helperNonConstVar++; // Signal non-const operation + + // Ensure cache is flushed before copying key + flush(""); + + auto* val = RjDirectAccess::ensurePath(key, doc, doc.GetAllocator()); + RjDirectAccess::ensureArray(val, doc.GetAllocator()); + synchronizeChildren(""); +} + std::vector Json::listAvailableMembers(std::string_view const key) const { std::scoped_lock const lockGuard(mtx); diff --git a/src/Nebulite/Data/Document/JsonScope.cpp b/src/Nebulite/Data/Document/JsonScope.cpp index dc58f659..7c3cf53d 100644 --- a/src/Nebulite/Data/Document/JsonScope.cpp +++ b/src/Nebulite/Data/Document/JsonScope.cpp @@ -318,6 +318,15 @@ void JsonScope::copyMember(ScopedKey const& fromKey, ScopedKeyView const& toKey) copyMember(fromKey.view(), toKey); } +void JsonScope::ensureArray(ScopedKeyView const& key){ + doc().ensureArray(key.full(*this)); +} + +void JsonScope::ensureArray(ScopedKey const& key){ + ensureArray(key.view()); +} + + //------------------------------------------ // Member listing From d42464ac2ed142566c6ca80ff2b89c2076e51b4b Mon Sep 17 00:00:00 2001 From: Leo Date: Sat, 31 Oct 2026 13:31:55 +0100 Subject: [PATCH 14/15] Refactor array handling in Array class to use jsonDoc.ensureArray for consistency --- .../Nebulite/Module/Transformation/Array.hpp | 2 +- src/Nebulite/Module/Transformation/Array.cpp | 54 +++++-------------- 2 files changed, 13 insertions(+), 43 deletions(-) diff --git a/include/Nebulite/Module/Transformation/Array.hpp b/include/Nebulite/Module/Transformation/Array.hpp index 63c8665b..04195e8e 100644 --- a/include/Nebulite/Module/Transformation/Array.hpp +++ b/include/Nebulite/Module/Transformation/Array.hpp @@ -127,7 +127,7 @@ class Array final : public Base::TransformationModule { static bool iota(Utility::Args::SegmentedStringView const& args, Data::JsonScope& jsonDoc); static auto constexpr iotaName = "iota"; - static auto constexpr iotaDesc = "Generates an array of values.\n" + static auto constexpr iotaDesc = "Generates a sequence of integer values.\n" "Usage: |iota -> {array}\n" "Generates an array containing the values from start (inclusive) to end (exclusive).\n" "If end is not greater than start, the result is an empty array.\n"; diff --git a/src/Nebulite/Module/Transformation/Array.cpp b/src/Nebulite/Module/Transformation/Array.cpp index c342015b..6f8f78ea 100644 --- a/src/Nebulite/Module/Transformation/Array.cpp +++ b/src/Nebulite/Module/Transformation/Array.cpp @@ -60,9 +60,7 @@ bool Array::at(Utility::Args::SegmentedStringView const& args, Data::JsonScope& return false; } try { - if (!ensureArray(jsonDoc)) { - return false; - } + jsonDoc.ensureArray(rootKey); auto const index = args.tryGetAs(1); if (!index) { return false; // Failed to convert index @@ -79,9 +77,7 @@ bool Array::at(Utility::Args::SegmentedStringView const& args, Data::JsonScope& } bool Array::first(Data::JsonScope& jsonDoc) { - if (!ensureArray(jsonDoc)) { - return false; - } + jsonDoc.ensureArray(rootKey); if (jsonDoc.memberSize(rootKey) == 0) { return false; // Empty array } @@ -91,9 +87,7 @@ bool Array::first(Data::JsonScope& jsonDoc) { } bool Array::last(Data::JsonScope& jsonDoc) { - if (!ensureArray(jsonDoc)) { - return false; - } + jsonDoc.ensureArray(rootKey); auto const arraySize = jsonDoc.memberSize(rootKey); if (arraySize == 0) { return false; // Empty array @@ -104,9 +98,7 @@ bool Array::last(Data::JsonScope& jsonDoc) { } bool Array::length(Data::JsonScope& jsonDoc) { - if (!ensureArray(jsonDoc)) { - return false; - } + jsonDoc.ensureArray(rootKey); auto const len = jsonDoc.memberSize(rootKey); jsonDoc.set(rootKey, static_cast(len)); return true; @@ -160,9 +152,10 @@ bool takeElements( return false; } auto const count = Utility::Convert::Cast::String::to(args.at(1)); - if (!count.has_value() || !Array::ensureArray(jsonDoc)) { + if (!count.has_value()) { return false; } + jsonDoc.ensureArray(rootKey); auto const arraySize = jsonDoc.memberSize(rootKey); auto const resultSize = std::min(count.value(), arraySize); @@ -334,9 +327,7 @@ bool Array::flatten(Data::JsonScope& jsonDoc){ } bool Array::reverse(Data::JsonScope& jsonDoc) { - if (!ensureArray(jsonDoc)) { - return false; - } + jsonDoc.ensureArray(rootKey); auto const arraySize = jsonDoc.memberSize(rootKey); Data::Json const tmp = jsonDoc.getSubDoc(rootKey); for (std::size_t i = 0; i < arraySize; ++i) { @@ -447,34 +438,15 @@ bool Array::iota(Utility::Args::SegmentedStringView const& args, Data::JsonScope // Other bool Array::ensureArray(Data::JsonScope& jsonDoc) { - auto const type = jsonDoc.memberType(rootKey); - - // Already array, nothing to do - if (type == Data::KeyType::array) { - return true; - } - - // No value stored, set to empty array - if (type == Data::KeyType::null) { - jsonDoc.setEmptyArray(rootKey); - return true; - } - - // Single value, wrap into an array - auto const key = rootKey.addIndex(0); - jsonDoc.moveMember(rootKey, key); // Move the original value to the new array index - - // Return whether wrapping succeeded - return jsonDoc.memberType(rootKey) == Data::KeyType::array; + jsonDoc.ensureArray(rootKey); + return true; } bool Array::push(Utility::Args::SegmentedStringView const& args, Data::JsonScope& jsonDoc) { if (args.size() < 2) { return false; } - if (!ensureArray(jsonDoc)) { - return false; - } + jsonDoc.ensureArray(rootKey); auto const arraySize = jsonDoc.memberSize(rootKey); auto const key = rootKey.addIndex(arraySize); jsonDoc.set(key, args.recombineSubspan(1)); @@ -487,9 +459,7 @@ bool Array::pushNumber(Utility::Args::SegmentedStringView const& args, Data::Jso } try { double const number = std::stod(std::string(args.at(1))); - if (!ensureArray(jsonDoc)) { - return false; - } + jsonDoc.ensureArray(rootKey); auto const arraySize = jsonDoc.memberSize(rootKey); auto const key = rootKey.addIndex(arraySize); jsonDoc.set(key, number); @@ -503,7 +473,7 @@ bool Array::pad(Utility::Args::SegmentedStringView const& args, Data::JsonScope& if (args.size() < 2) return false; auto const size = Utility::Convert::Cast::String::to(args.at(1)); if (!size.has_value()) return false; - if (!ensureArray(jsonDoc)) return false; + jsonDoc.ensureArray(rootKey); while (jsonDoc.memberSize(rootKey) < size.value()) { auto const newIndex = jsonDoc.memberSize(rootKey); auto const newKey = rootKey.addIndex(newIndex); From 3070b4762ba7e1f21bcd23a58c6877222fda77b2 Mon Sep 17 00:00:00 2001 From: Leo Date: Sat, 31 Oct 2026 13:32:39 +0100 Subject: [PATCH 15/15] Update documentation --- .../syntaxes/keywords.json | 20 ++ .../syntaxes/keywords.txt | 20 ++ .../syntaxes/nebs.tmLanguage.json | 2 +- doc/Commands.md | 244 +++++++++++++++++- 4 files changed, 280 insertions(+), 6 deletions(-) diff --git a/Languages/nebs/nebulite-script-vscode/syntaxes/keywords.json b/Languages/nebs/nebulite-script-vscode/syntaxes/keywords.json index bd83fa80..3e207678 100644 --- a/Languages/nebs/nebulite-script-vscode/syntaxes/keywords.json +++ b/Languages/nebs/nebulite-script-vscode/syntaxes/keywords.json @@ -17,7 +17,9 @@ "batch", "batchPadded", "beep", + "benchmark", "bind", + "booleanToSigned", "broadcast", "bundle", "cam", @@ -33,6 +35,7 @@ "clear", "close", "collision-detect", + "compare", "complexAbs", "complexArg", "complexToString", @@ -41,9 +44,11 @@ "contains", "context-evaluation", "copy", + "copyMember", "crash", "custom", "debug", + "dedupe", "default", "delete", "deload", @@ -92,6 +97,7 @@ "formatComplexNumberString", "formatNumber", "forward", + "framebuffer-scale", "function", "functree", "geq", @@ -100,10 +106,12 @@ "grid", "gt", "halt-once", + "head", "help", "if", "ifft", "imgui-view", + "imguidb", "injectScript", "input-mapping", "input-wait", @@ -135,13 +143,17 @@ "master-unlock", "match", "max", + "mean", "median", "member", "members", "min", "mirror", "mod", + "mode", + "mouse-scale", "move", + "moveMember", "mul", "must-throw", "nand", @@ -179,12 +191,16 @@ "rPadNonNumeric", "rPadNumeric", "rStrip", + "range", + "recombine", + "recombineLines", "recursive", "regex", "reload", "remove", "removeMember", "render-object", + "render-scale", "reparse", "replace", "require", @@ -202,6 +218,8 @@ "ruleset", "save", "save-standards", + "scale", + "segmented-string-view", "selected-object", "serialize", "set", @@ -222,6 +240,7 @@ "sort", "spawn", "split", + "splitNewlines", "sqrt", "square", "standard-file", @@ -239,6 +258,7 @@ "subspan", "substring", "sum", + "tail", "task", "task-exec", "throw", diff --git a/Languages/nebs/nebulite-script-vscode/syntaxes/keywords.txt b/Languages/nebs/nebulite-script-vscode/syntaxes/keywords.txt index 097cba18..eaee8ecc 100644 --- a/Languages/nebs/nebulite-script-vscode/syntaxes/keywords.txt +++ b/Languages/nebs/nebulite-script-vscode/syntaxes/keywords.txt @@ -16,7 +16,9 @@ basicValue batch batchPadded beep +benchmark bind +booleanToSigned broadcast bundle cam @@ -32,6 +34,7 @@ ceiling clear close collision-detect +compare complexAbs complexArg complexToString @@ -40,9 +43,11 @@ constrain contains context-evaluation copy +copyMember crash custom debug +dedupe default delete deload @@ -91,6 +96,7 @@ for-progress formatComplexNumberString formatNumber forward +framebuffer-scale function functree geq @@ -99,10 +105,12 @@ global grid gt halt-once +head help if ifft imgui-view +imguidb injectScript input-mapping input-wait @@ -134,13 +142,17 @@ map master-unlock match max +mean median member members min mirror mod +mode +mouse-scale move +moveMember mul must-throw nand @@ -178,12 +190,16 @@ rPad rPadNonNumeric rPadNumeric rStrip +range +recombine +recombineLines recursive regex reload remove removeMember render-object +render-scale reparse replace require @@ -201,6 +217,8 @@ roundUp ruleset save save-standards +scale +segmented-string-view selected-object serialize set @@ -221,6 +239,7 @@ snapshot sort spawn split +splitNewlines sqrt square standard-file @@ -238,6 +257,7 @@ sub subspan substring sum +tail task task-exec throw diff --git a/Languages/nebs/nebulite-script-vscode/syntaxes/nebs.tmLanguage.json b/Languages/nebs/nebulite-script-vscode/syntaxes/nebs.tmLanguage.json index 2ffd1363..abc74672 100644 --- a/Languages/nebs/nebulite-script-vscode/syntaxes/nebs.tmLanguage.json +++ b/Languages/nebs/nebulite-script-vscode/syntaxes/nebs.tmLanguage.json @@ -23,7 +23,7 @@ }, { "name": "keyword.control.nebs", - "match": "\\b(formatComplexNumberString|numericOrNumericString|listMembersAndValues|context-evaluation|strCountAppearance|typeAsSimpleString|case-insensitive|collision-detect|filterRegexValue|complexToString|enumerateInline|expression-help|fetch-container|filterGlobValue|find-parent-key|key-combination|numberToComplex|selected-object|applyTfDomainF|case-sensitive|categoryLevel1|categoryLevel2|lPadNonNumeric|rPadNonNumeric|save-standards|input-mapping|master-unlock|render-object|standard-file|always-clear|ensure-array|feature-test|filterCustom|for-progress|formatNumber|injectScript|removeMember|rng2ArgInt16|rng3ArgInt16|set-fixed-dt|toBoolString|typeAsNumber|typeAsString|batchPadded|deserialize|ensureArray|filterNulls|filterRegex|lPadNumeric|listMembers|numerically|rPadNumeric|set-integer|unreachable|--headless|basicValue|capitalize|complexAbs|complexArg|fetch-name|filterGlob|imgui-view|input-wait|must-throw|play-sound|push-front|pushNumber|set-string|startsWith|strCompare|--recover|add-clock|broadcast|constrain|dump-view|enumerate|error-log|halt-once|keyDelete|large-fft|pop-front|push-back|recursive|roundDown|serialize|setDouble|setString|substring|task-exec|tile-info|toBipolar|category|contains|document|drawcall|endsWith|evaluate|fetch-id|function|functree|nonEmpty|pop-back|print-id|settings|show-fps|snapshot|toDouble|toString|triangle|variable|average|capture|ceiling|console|default|flatten|forward|members|numeric|product|reparse|replace|require|reverse|rng2Arg|rng3Arg|roundUp|ruleset|set-fps|set-res|setBool|subspan|toLower|toUpper|always|assert|assign|bundle|custom|delete|deload|equals|exists|global|invoke|lStrip|length|listen|median|member|mirror|object|rStrip|reload|remove|setInt|square|stddev|strLen|stride|string|toBool|unlock|update|array|batch|clear|close|crash|debug|draft|empty|error|false|fetch|first|floor|match|other|parse|print|query|regex|rmlui|round|slide|spawn|split|state|store|strip|throw|toInt|beep|bind|copy|echo|eval|exit|grid|help|ifft|iota|json|lPad|last|list|load|lock|move|nand|once|open|push|rPad|root|save|sort|sqrt|task|time|true|type|view|wait|warn|xnor|add|all|and|cam|cat|div|env|fft|for|geq|get|int|key|leq|log|map|max|min|mod|mul|neq|nop|nor|not|off|pad|pow|rng|set|sgn|sub|sum|xor|at|eq|gt|if|ls|lt|on|or)\\b" + "match": "\\b(formatComplexNumberString|numericOrNumericString|segmented-string-view|listMembersAndValues|context-evaluation|strCountAppearance|typeAsSimpleString|framebuffer-scale|case-insensitive|collision-detect|filterRegexValue|booleanToSigned|complexToString|enumerateInline|expression-help|fetch-container|filterGlobValue|find-parent-key|key-combination|numberToComplex|selected-object|applyTfDomainF|case-sensitive|categoryLevel1|categoryLevel2|lPadNonNumeric|rPadNonNumeric|recombineLines|save-standards|input-mapping|master-unlock|render-object|splitNewlines|standard-file|always-clear|ensure-array|feature-test|filterCustom|for-progress|formatNumber|injectScript|removeMember|render-scale|rng2ArgInt16|rng3ArgInt16|set-fixed-dt|toBoolString|typeAsNumber|typeAsString|batchPadded|deserialize|ensureArray|filterNulls|filterRegex|lPadNumeric|listMembers|mouse-scale|numerically|rPadNumeric|set-integer|unreachable|--headless|basicValue|capitalize|complexAbs|complexArg|copyMember|fetch-name|filterGlob|imgui-view|input-wait|moveMember|must-throw|play-sound|push-front|pushNumber|set-string|startsWith|strCompare|--recover|add-clock|benchmark|broadcast|constrain|dump-view|enumerate|error-log|halt-once|keyDelete|large-fft|pop-front|push-back|recombine|recursive|roundDown|serialize|setDouble|setString|substring|task-exec|tile-info|toBipolar|category|contains|document|drawcall|endsWith|evaluate|fetch-id|function|functree|nonEmpty|pop-back|print-id|settings|show-fps|snapshot|toDouble|toString|triangle|variable|average|capture|ceiling|compare|console|default|flatten|forward|imguidb|members|numeric|product|reparse|replace|require|reverse|rng2Arg|rng3Arg|roundUp|ruleset|set-fps|set-res|setBool|subspan|toLower|toUpper|always|assert|assign|bundle|custom|dedupe|delete|deload|equals|exists|global|invoke|lStrip|length|listen|median|member|mirror|object|rStrip|reload|remove|setInt|square|stddev|strLen|stride|string|toBool|unlock|update|array|batch|clear|close|crash|debug|draft|empty|error|false|fetch|first|floor|match|other|parse|print|query|range|regex|rmlui|round|scale|slide|spawn|split|state|store|strip|throw|toInt|beep|bind|copy|echo|eval|exit|grid|head|help|ifft|iota|json|lPad|last|list|load|lock|mean|mode|move|nand|once|open|push|rPad|root|save|sort|sqrt|tail|task|time|true|type|view|wait|warn|xnor|add|all|and|cam|cat|div|env|fft|for|geq|get|int|key|leq|log|map|max|min|mod|mul|neq|nop|nor|not|off|pad|pow|rng|set|sgn|sub|sum|xor|at|eq|gt|if|ls|lt|on|or)\\b" }, { "name": "markup.underline.link", diff --git a/doc/Commands.md b/doc/Commands.md index c0d7d6bc..a1264c96 100644 --- a/doc/Commands.md +++ b/doc/Commands.md @@ -2,7 +2,7 @@ This documentation is automatically generated. -Generated on: Sun Sep 6 19:18:17 CEST 2026 +Generated on: Sat Oct 31 13:32:03 CET 2026 ## Table of Contents @@ -56,6 +56,7 @@ Available Functions | `help` | Show available commands and their descriptions | | `if` | Executes a block of code if a condition is true. | | `imgui-view` | Creates an ImGui view of the domain. | +| `imguidb` | Dear ImGui debugging utilities. | | `input-mapping` | Functions for mapping inputs to actions within the GlobalSpace. | | `input-wait` | Waits for user input before continuing. | | `json` | Functions to manipulate JSON data via read-only JSON documents | @@ -491,6 +492,7 @@ Available Functions | `help` | Show available commands and their descriptions | | `key-combination` | Tests key-combinations for the ScopedKey class. | | `large-fft` | Tests the FFT implementation with a large dataset. | +| `segmented-string-view` | Functions for testing segmented string view functionality. | ##### `feature-test context-evaluation` @@ -529,6 +531,38 @@ Tests the FFT implementation with a large dataset. Usage: feature-test large-fft ``` +##### `feature-test segmented-string-view` + +Available Functions + +| Function | Description | +|----------|-------------| +| `benchmark` | Tests benchmark for segmented string-view, using compare and contains methods. | +| `compare` | Segmented string view comparison tests. | +| `contains` | Tests the contains method of the SegmentedStringView class. | +| `help` | Show available commands and their descriptions | + +###### `feature-test segmented-string-view benchmark` + +``` +Tests benchmark for segmented string-view, using compare and contains methods. +Prints the amount of milliseconds the test took.Usage: feature-test segmented-string-view +``` + +###### `feature-test segmented-string-view compare` + +``` +Segmented string view comparison tests. +Prints the message 'SegmentedStringView test passed.' or 'SegmentedStringView test failed: 'Usage: feature-test segmented-string-view compare +``` + +###### `feature-test segmented-string-view contains` + +``` +Tests the contains method of the SegmentedStringView class. +Prints the message 'SegmentedStringView test passed.' or 'SegmentedStringView test failed: 'Usage: feature-test segmented-string-view contains +``` + #### `fetch-container` ``` @@ -642,6 +676,50 @@ Creates an ImGui view of the domain. Usage: imgui-view ``` +#### `imguidb` + +Available Functions + +| Function | Description | +|----------|-------------| +| `framebuffer-scale` | Overrides Dear ImGui's framebuffer scale for the current frame. | +| `help` | Show available commands and their descriptions | +| `mouse-scale` | Scales mouse coordinates forwarded to Dear ImGui. | +| `render-scale` | Scales Dear ImGui vertices during its SDL render pass. | +| `scale` | Scales all Dear ImGui style sizes. | + +##### `imguidb framebuffer-scale` + +``` +Overrides Dear ImGui's framebuffer scale for the current frame. + +Usage: imguidb framebuffer-scale +``` + +##### `imguidb mouse-scale` + +``` +Scales mouse coordinates forwarded to Dear ImGui. + +Usage: imguidb mouse-scale +``` + +##### `imguidb render-scale` + +``` +Scales Dear ImGui vertices during its SDL render pass. + +Usage: imguidb render-scale +``` + +##### `imguidb scale` + +``` +Scales all Dear ImGui style sizes. + +Usage: imguidb scale +``` + #### `input-mapping` Available Functions @@ -2102,20 +2180,24 @@ Available Functions | Function | Description | |----------|-------------| | `add` | Adds a numeric value to the current JSON value. | +| `and` | Logical and operation on the current JSON array values. | | `applyTfDomainF` | Stores the result of applying a transfer function on the frequency domain to a given real-number series | | `assert` | Assertion transformations that validate JSON values and throw exceptions on failure. | | `assign` | Assigns a value based on the result of an expression. | | `at` | Gets the element at the specified index from the array in the current JSON value. | -| `average` | Calculates the average of the elements of the array in the current JSON value. | +| `average` | Calculates the mean of the elements of the array in the current JSON value. | | `batch` | Batches the array in the current JSON value into subarrays of the specified size. | | `batchPadded` | Batches the array in the current JSON value into subarrays of the specified size, padding the last batch with empty objects if necessary. | | `bind` | Binds the elements of an array to the provided keys. | +| `booleanToSigned` | Converts the root boolean value to 1 (true) or -1 (false)Usage: |booleanToSigned -> {value:int} | | `bundle` | Gathers all members from the provided keys into an array. | | `capitalize` | Capitalizes the current JSON string. | | `ceiling` | Rounds the current JSON numeric value down to the nearest integer. | | `complexAbs` | Calculates the absolute value (magnitude) of a complex number. | | `complexArg` | Calculates the argument (phase angle) of a complex number in radians. | | `complexToString` | Formats the contained complex number object to a string. | +| `copyMember` | Copys a value based on the result of an expression. | +| `dedupe` | Removes duplicate elements from the array in the current JSON value. | | `default` | If the current value is null, default to a given String | | `deserialize` | Deserializes the current JSON string value stored in root. | | `div` | Divides the current JSON value by a numeric value. | @@ -2141,10 +2223,11 @@ Available Functions | `geq` | Checks if the current JSON value is greater than or equal to the specified value. | | `get` | Gets the value at the specified key from the current JSON object. | | `gt` | Checks if the current JSON value is greater than the specified value. | +| `head` | Gets the first elements of the array in the current JSON value. | | `help` | Show available commands and their descriptions | | `ifft` | Stores the inverse fft of a given complex-number or real-number series (mixable) | | `injectScript` | Injects a nebulite script to modify the json doc. | -| `iota` | Generates an array of values. | +| `iota` | Generates a sequence of integer values. | | `lPad` | Pads the current JSON string value on the left with a specified character until it reaches a specified total length. | | `lPadNonNumeric` | Pads the current JSON non-numeric string value on the left with a specified character until it reaches a specified total length. | | `lPadNumeric` | Pads the current JSON numeric string value on the left with a specified character until it reaches a specified total length. | @@ -2157,13 +2240,19 @@ Available Functions | `lt` | Checks if the current JSON value is less than the specified value. | | `map` | Applies a mapping function to each element in the array of the current JSON value. | | `max` | Finds the maximum value among the elements of the array in the current JSON value. | +| `mean` | Calculates the mean of the elements of the array in the current JSON value. | | `median` | Calculates the median of the elements of the array in the current JSON value. | | `min` | Finds the minimum value among the elements of the array in the current JSON value. | | `mod` | Calculates the modulo of the current JSON value by a numeric value. | +| `mode` | Finds the most frequent value in the array in the current JSON value. | +| `moveMember` | Moves a value based on the result of an expression. | | `mul` | Multiplies the current JSON value by a numeric value. | +| `nand` | Logical nand operation on the current JSON array values. | | `neq` | Checks if the current JSON value is not equal to the specified value. | +| `nor` | Logical nor operation on the current JSON array values. | | `not` | Logical NOT operation on the current JSON value. | | `numberToComplex` | Converts a given number to a complex number. | +| `or` | Logical or operation on the current JSON array values. | | `pad` | Pads the array in the current JSON value to the specified length with empty objects. | | `pow` | Raises the current JSON value to the power of a numeric value. | | `print` | Prints the current JSON value to the console. | @@ -2174,6 +2263,9 @@ Available Functions | `rPadNonNumeric` | Pads the current JSON non-numeric string value on the right with a specified character until it reaches a specified total length. | | `rPadNumeric` | Pads the current JSON numeric string value on the right with a specified character until it reaches a specified total length. | | `rStrip` | Strips whitespace from the right end of the current JSON string value. | +| `range` | Calculates the range of the elements of the array in the current JSON value. | +| `recombine` | Recombines the string elements of the array in the current JSON value. | +| `recombineLines` | Recombines the string elements of the array in the current JSON value with newlines. | | `removeMember` | Removes the member at the specified key from the JSON document. | | `replace` | Replaces all occurrences of a target substring with a replacement substring in the current JSON string value. | | `require` | Requirement transformations that validate JSON values and return false on failure (fallback to default value in get-call, usually 'null' or 0.0). | @@ -2190,6 +2282,7 @@ Available Functions | `slide` | Slides the array in the current JSON value by the specified size, generating overlapping windows. | | `sort` | Sorting transformation functions | | `split` | Splits the current JSON string value into an array of substrings based on a specified character delimiter. | +| `splitNewlines` | Splits the current JSON string value into an array of substrings at newline characters. | | `sqrt` | Calculates the square root of the current JSON value. | | `stddev` | Calculates the standard deviation of the elements of the array in the current JSON value. | | `store` | Stores the current JSON value as a file with the given name. | @@ -2202,6 +2295,7 @@ Available Functions | `subspan` | Gets a subarray from the array in the current JSON value. | | `substring` | Extracts a substring from the current JSON string value. | | `sum` | Sums the elements of the array in the current JSON value. | +| `tail` | Gets the last elements of the array in the current JSON value. | | `toBool` | Converts the current JSON value to a boolean. | | `toBoolString` | Converts the current JSON value to a boolean string. | | `toDouble` | Converts the current JSON value to a double. | @@ -2214,6 +2308,8 @@ Available Functions | `typeAsString` | Converts the current JSON type value to a string with metadata. | | `unreachable` | Indicates that the current code path should be unreachable. | | `warn` | Echoes the provided arguments to the console as a warning message, with newline. | +| `xnor` | Logical xnor operation on the current JSON array values. | +| `xor` | Logical xor operation on the current JSON array values. | #### `add` @@ -2223,6 +2319,14 @@ If multiple numbers are provided, the result is an array, one element for each p Usage: |add ... -> {number/array} ``` +#### `and` + +``` +Logical and operation on the current JSON array values. +Input must be an array. +Usage: |and -> {value:bool} +``` + #### `applyTfDomainF` ``` @@ -2400,7 +2504,7 @@ Usage: |at -> {value} #### `average` ``` -Calculates the average of the elements of the array in the current JSON value. +Calculates the mean of the elements of the array in the current JSON value. Input must be an array of numbers. If any element is not a number, the current value is not an array or the array is empty, the transformation fails. Usage: |average -> {number} ``` @@ -2429,6 +2533,12 @@ Inserts empty objects for missing values in the array. Usage: |bind ... -> {object} ``` +#### `booleanToSigned` + +``` +Converts the root boolean value to 1 (true) or -1 (false)Usage: |booleanToSigned -> {value:int} +``` + #### `bundle` ``` @@ -2479,6 +2589,20 @@ Example formatters: 04.2f, 5i, 06i Usage: |complexToString [formatter] -> {string} ``` +#### `copyMember` + +``` +Copys a value based on the result of an expression. +Usage: |copyMember -> {json} +``` + +#### `dedupe` + +``` +Removes duplicate elements from the array in the current JSON value. +Usage: |dedupe -> {array} +``` + #### `default` ``` @@ -2676,6 +2800,13 @@ Checks if the current JSON value is greater than the specified value. Usage: |gt -> {bool} ``` +#### `head` + +``` +Gets the first elements of the array in the current JSON value. +Usage: |head -> {array} +``` + #### `ifft` ``` @@ -2693,7 +2824,7 @@ Usage: |injectScript -> {modified-json} #### `iota` ``` -Generates an array of values. +Generates a sequence of integer values. Usage: |iota -> {array} Generates an array containing the values from start (inclusive) to end (exclusive). If end is not greater than start, the result is an empty array. @@ -2796,6 +2927,14 @@ Input must be an array of numbers. If any element is not a number, the current v Usage: |max -> {number} ``` +#### `mean` + +``` +Calculates the mean of the elements of the array in the current JSON value. +Input must be an array of numbers. If any element is not a number, the current value is not an array or the array is empty, the transformation fails. +Usage: |average -> {number} +``` + #### `median` ``` @@ -2820,6 +2959,22 @@ If multiple numbers are provided, the result is an array, one element for each p Usage: |mod ... -> {number/array} ``` +#### `mode` + +``` +Finds the most frequent value in the array in the current JSON value. +If multiple values are tied, the first value in the array is returned. +Input must be a non-empty array of numbers. +Usage: |mode -> {number} +``` + +#### `moveMember` + +``` +Moves a value based on the result of an expression. +Usage: |moveMember -> {json} +``` + #### `mul` ``` @@ -2828,6 +2983,14 @@ If multiple numbers are provided, the result is an array, one element for each p Usage: |multiply ... -> {number/array} ``` +#### `nand` + +``` +Logical nand operation on the current JSON array values. +Input must be an array. +Usage: |nand -> {value:bool} +``` + #### `neq` ``` @@ -2835,6 +2998,14 @@ Checks if the current JSON value is not equal to the specified value. Usage: |neq -> {bool} ``` +#### `nor` + +``` +Logical nor operation on the current JSON array values. +Input must be an array. +Usage: |nor -> {value:bool} +``` + #### `not` ``` @@ -2855,6 +3026,14 @@ Allowed values are: The transformation fails if the current JSON value is not convertible to a complex number. ``` +#### `or` + +``` +Logical or operation on the current JSON array values. +Input must be an array. +Usage: |or -> {value:bool} +``` + #### `pad` ``` @@ -2938,6 +3117,30 @@ Strips whitespace from the right end of the current JSON string value. Usage: |rStrip -> {string} ``` +#### `range` + +``` +Calculates the range of the elements of the array in the current JSON value. +The range is the maximum value minus the minimum value. +Input must be a non-empty array of numbers. +Usage: |range -> {number} +``` + +#### `recombine` + +``` +Recombines the string elements of the array in the current JSON value. +Uses whitespace as the delimiter if none is provided. +Usage: |recombine [delimiter] -> {string} +``` + +#### `recombineLines` + +``` +Recombines the string elements of the array in the current JSON value with newlines. +Usage: |recombineLines -> {string} +``` + #### `removeMember` ``` @@ -3247,6 +3450,14 @@ Usage: |sort numerically -> {sorted array} Splits the current JSON string value into an array of substrings based on a specified character delimiter. Usage: |split {delimiter} -> {array} {delimiter}: Delimiter to split the string by. If left empty, it splits by whitespace. +Delimiter must be a single character. +``` + +#### `splitNewlines` + +``` +Splits the current JSON string value into an array of substrings at newline characters. +Usage: |splitNewlines -> {array} ``` #### `sqrt` @@ -3389,6 +3600,13 @@ Input must be an array of numbers. If any element is not a number, the current v Usage: |sum -> {number} ``` +#### `tail` + +``` +Gets the last elements of the array in the current JSON value. +Usage: |tail -> {array} +``` + #### `toBool` ``` @@ -3481,6 +3699,22 @@ Echoes the provided arguments to the console as a warning message, with newline. Usage: |warn -> {unchanged-json} ``` +#### `xnor` + +``` +Logical xnor operation on the current JSON array values. +Input must be an array. +Usage: |xnor -> {value:bool} +``` + +#### `xor` + +``` +Logical xor operation on the current JSON array values. +Input must be an array. +Usage: |xor -> {value:bool} +``` + ## Expression Functions These functions are available in expressions (e.g. in `eval` or `if` conditions).