From 59d6ca492d40ee7ec55ffbc55f14cb61f0f9dc89 Mon Sep 17 00:00:00 2001 From: Anton Tarasenko Date: Mon, 6 Mar 2023 03:27:59 +0700 Subject: [PATCH] Add `Append` method to `HashTable` --- sources/Data/Collections/HashTable.uc | 31 +++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/sources/Data/Collections/HashTable.uc b/sources/Data/Collections/HashTable.uc index 92ae16c..844f343 100644 --- a/sources/Data/Collections/HashTable.uc +++ b/sources/Data/Collections/HashTable.uc @@ -197,6 +197,37 @@ private final function ResizeHashTable(int newSize) } } +/** + * Appends objects from another `HashTable` to the caller one. + * + * @param other Array to append objects from. `none` means nothing will be + * added. + * @return Reference to the caller `HashTable` to allow for method chaining. + */ +public final function HashTable Append(HashTable other) +{ + local AcediaObject nextKey, nextValue; + local HashTableIterator iter; + + if (other == none) return self; + if (other.GetLength() <= 0) return self; + + iter = HashTableIterator(other.Iterate()); + while (!iter.HasFinished()) + { + nextKey = iter.GetKey(); + nextValue = iter.Get(); + if (!HasKey(nextKey)) { + SetItem(nextKey, nextValue); + } + _.memory.Free(nextKey); + _.memory.Free(nextValue); + iter.Next(); + } + _.memory.Free(iter); + return self; +} + /** * Returns minimal capacity of the caller associative array. *