From e045f9785c400152ed11cf65f9e492fa4080acc7 Mon Sep 17 00:00:00 2001 From: Anton Tarasenko Date: Tue, 5 Jul 2022 21:02:12 +0700 Subject: [PATCH] Add `Vector` support to new Acedia collections --- sources/Data/Collections/ArrayList.uc | 83 ++++++++++++++++++++++++++ sources/Data/Collections/Collection.uc | 75 +++++++++++++++++++++++ sources/Data/Collections/HashTable.uc | 68 +++++++++++++++++++++ 3 files changed, 226 insertions(+) diff --git a/sources/Data/Collections/ArrayList.uc b/sources/Data/Collections/ArrayList.uc index 5342059..5c42634 100644 --- a/sources/Data/Collections/ArrayList.uc +++ b/sources/Data/Collections/ArrayList.uc @@ -730,6 +730,74 @@ public final function ArrayList SetFloat( return self; } +/** + * Returns `Vector` item at `index`. If index is invalid or + * stores a non-`int` value, returns `defaultValue`. + * + * Referred value must be stored as `VectorBox` or `VectorRef` + * (or one of their sub-classes) for this method to work. + * + * @param index Index of a `Vector` item that `ArrayList` + * has to return. + * @param defaultValue Value to return if there is either no item recorded + * at `index` or it has a wrong type. + * @return `Vector` value at `index` in the caller `ArrayList`. + * `defaultValue` if passed `index` is invalid or non-`Vector` value + * is stored there. + */ +public final function Vector GetVector(int index, optional Vector defaultValue) +{ + local AcediaObject result; + local VectorBox asBox; + local VectorRef asRef; + + result = BorrowItem(index); + if (result == none) { + return defaultValue; + } + asBox = VectorBox(result); + if (asBox != none) { + return asBox.Get(); + } + asRef = VectorRef(result); + if (asRef != none) { + return asRef.Get(); + } + return defaultValue; +} + +/** + * Changes `ArrayList`'s value at `index` to `value` that will be recorded + * as either `VectorBox` or `VectorRef`, depending of `asRef` optional + * parameter. + * + * @param index Index, at which to change the value. If `ArrayList` is + * not long enough to hold it, it will be automatically expanded. + * If passed index is negative - method will do nothing. + * @param value Value to be set at a given index. + * @param asRef Given `Vector` value will be recorded as immutable + * `VectorBox` by default (`asRef == false`). Setting this parameter to + * `true` will make this method record it as a mutable `VectorRef`. + * @return Reference to the caller `ArrayList` to allow for method chaining. + */ +public final function ArrayList SetVector( + int index, + Vector value, + optional bool asRef) +{ + local AcediaObject newValue; + + if (asRef) { + newValue = _.ref.Vec(value); + } + else { + newValue = _.box.Vec(value); + } + SetItem(index, newValue); + newValue.FreeSelf(); + return self; +} + /** * Returns plain string item at `index`. If index is invalid or * stores a non-`BaseText` value, returns `defaultValue`. @@ -918,6 +986,21 @@ public final function ArrayList AddFloat(float value, optional bool asRef) return SetFloat(storedObjects.length, value, asRef); } +/** + * Adds given `Vector` at the end of the `ArrayList`, expanding it by + * one item. + * + * @param value `Vector` value to be added at the end of the `ArrayList`. + * @param asRef Given `Vector` value will be recorded as immutable + * `VectorBox` by default (`asRef == false`). Setting this parameter to + * `true` will make this method record it as a mutable `VectorRef`. + * @return Reference to the caller `ArrayList` to allow for method chaining. + */ +public final function ArrayList AddVector(Vector value, optional bool asRef) +{ + return SetVector(storedObjects.length, value, asRef); +} + /** * Adds given plain string at the end of the `ArrayList`, expanding it by * one item. diff --git a/sources/Data/Collections/Collection.uc b/sources/Data/Collections/Collection.uc index 8d18b5e..8b409c6 100644 --- a/sources/Data/Collections/Collection.uc +++ b/sources/Data/Collections/Collection.uc @@ -331,6 +331,44 @@ public final function float GetFloatBy( return defaultValue; } +/** + * Returns a `Vector` value stored (in the caller `Collection` or + * one of it's sub-collections) pointed by + * [JSON pointer](https://tools.ietf.org/html/rfc6901). + * See `GetItemBy()` for more information. + * + * Referred value must be stored as `VectorBox` or `VectorRef` + * (or one of their sub-classes) for this method to work. + * + * @param jsonPointerAsText Description of a path to the `Vector` value. + * @param defaultValue Value to return in case `jsonPointerAsText` + * does not point at any existing value or if that value does not have + * appropriate type. + * @return `Vector` value, stored at `jsonPointerAsText` or `defaultValue` if it + * is missing or has a different type. + */ +public final function Vector GetVectorBy( + BaseText jsonPointerAsText, + optional Vector defaultValue) +{ + local AcediaObject result; + local VectorBox asBox; + local VectorRef asRef; + result = GetItemBy(jsonPointerAsText); + if (result == none) { + return defaultValue; + } + asBox = VectorBox(result); + if (asBox != none) { + return asBox.Get(); + } + asRef = VectorRef(result); + if (asRef != none) { + return asRef.Get(); + } + return defaultValue; +} + /** * Returns a plain string value stored (in the caller `Collection` or * one of it's sub-collections) pointed by @@ -639,6 +677,43 @@ public final function float GetFloatByJSON( return defaultValue; } +/** + * Returns a `Vector` value stored (in the caller `Collection` or + * one of it's sub-collections) pointed by JSON pointer. + * See `GetItemByJSON()` for more information. + * + * Referred value must be stored as `VectorBox` or `VectorRef` + * (or one of their sub-classes) for this method to work. + * + * @param jsonPointer JSON path to the `Vector` value. + * @param defaultValue Value to return in case `jsonPointer` + * does not point at any existing value or if that value does not have + * appropriate type. + * @return `Vector` value, stored at `jsonPointerAsText` or `defaultValue` if it + * is missing or has a different type. + */ +public final function Vector GetVectorByJSON( + JSONPointer jsonPointer, + optional Vector defaultValue) +{ + local AcediaObject result; + local VectorBox asBox; + local VectorRef asRef; + result = GetItemByJSON(jsonPointer); + if (result == none) { + return defaultValue; + } + asBox = VectorBox(result); + if (asBox != none) { + return asBox.Get(); + } + asRef = VectorRef(result); + if (asRef != none) { + return asRef.Get(); + } + return defaultValue; +} + /** * Returns a plain string value stored (in the caller `Collection` or * one of it's sub-collections) pointed by JSON pointer. diff --git a/sources/Data/Collections/HashTable.uc b/sources/Data/Collections/HashTable.uc index 13be517..1e56de1 100644 --- a/sources/Data/Collections/HashTable.uc +++ b/sources/Data/Collections/HashTable.uc @@ -934,6 +934,74 @@ public final function HashTable SetFloat( return self; } +/** + * Returns `Vector` item at key `key`. If key is invalid or + * stores a non-`Vector` value, returns `defaultValue`. + * + * Referred value must be stored as `VectorBox` or `VectorRef` + * (or one of their sub-classes) for this method to work. + * + * @param key Key of a `Vector` item that `HashTable` + * has to return. + * @param defaultValue Value to return if there is either no item recorded + * at `key` or it has a wrong type. + * @return `Vector` value at `key` in the caller `HashTable`. + * `defaultValue` if passed `key` is invalid or non-`Vector` value + * is stored with it. + */ +public final function Vector GetVector( + AcediaObject key, + optional Vector defaultValue) +{ + local AcediaObject result; + local VectorBox asBox; + local VectorRef asRef; + + result = BorrowItem(key); + if (result == none) { + return defaultValue; + } + asBox = VectorBox(result); + if (asBox != none) { + return asBox.Get(); + } + asRef = VectorRef(result); + if (asRef != none) { + return asRef.Get(); + } + return defaultValue; +} + +/** + * Changes `HashTable`'s value at key `key` to `value` that will be + * recorded as either `VectorBox` or `VectorRef`, depending of `asRef` + * optional parameter. + * + * @param key Key, at which to change the value. + * @param value Value to be set at a given key. + * @param asRef Given `Vector` value will be recorded as immutable + * `VectorBox` by default (`asRef == false`). Setting this parameter to + * `true` will make this method record it as a mutable `VectorRef`. + * @return Reference to the caller `HashTable` to allow for method chaining. + */ +public final function HashTable SetVector( + AcediaObject key, + Vector value, + optional bool asRef) +{ + local AcediaObject newValue; + + if (asRef) { + newValue = _.ref.Vec(value); + } + else { + newValue = _.box.Vec(value); + } + SetItem(key, newValue); + newValue.FreeSelf(); + return self; +} + /** * Returns plain string item at key `key`. If key is invalid or stores * a non-`BaseText` value, returns `defaultValue`.