From 1b69b2484c5edc03dd996790a061ae42e700c497 Mon Sep 17 00:00:00 2001 From: Anton Tarasenko Date: Mon, 4 Jul 2022 01:31:40 +0700 Subject: [PATCH] Add API methods for creating `HashTable`s --- sources/Data/Collections/CollectionsAPI.uc | 37 ++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/sources/Data/Collections/CollectionsAPI.uc b/sources/Data/Collections/CollectionsAPI.uc index 070426d..8aa141e 100644 --- a/sources/Data/Collections/CollectionsAPI.uc +++ b/sources/Data/Collections/CollectionsAPI.uc @@ -54,6 +54,43 @@ public final function ArrayList EmptyArrayList() return ArrayList(_.memory.Allocate(class'ArrayList')); } +/** + * Creates a new `HashTable`, optionally filling it with entries + * (key/value pairs) from a given native array. + * + * @param entriesArray Entries (key/value pairs) to place inside created + * `HashTable`; if empty (by default) - new, + * empty `HashTable` will be returned. + * @param managed Flag that indicates whether values from + * `entriesArray` argument should be added as managed. + * By default `false` - they would not be managed. + * @return New `HashTable`, optionally filled with contents of + * `entriesArray`. Guaranteed to be not `none` and to not contain any items + * outside of `entriesArray`. + */ +public final function HashTable NewHashTable( + array entriesArray) +{ + local int i; + local HashTable result; + + result = HashTable(_.memory.Allocate(class'HashTable')); + for (i = 0; i < entriesArray.length; i += 1) { + result.SetItem(entriesArray[i].key, entriesArray[i].value); + } + return result; +} + +/** + * Creates a new empty `HashTable`. + * + * @return New empty instance of `HashTable`. + */ +public final function HashTable EmptyHashTable() +{ + return HashTable(_.memory.Allocate(class'HashTable')); +} + /** * Creates a new `DynamicArray`, optionally filling it with objects from * a given native array.