From ec3a751162d3f1943fa4a869f126004e05a5b3f4 Mon Sep 17 00:00:00 2001 From: Anton Tarasenko Date: Tue, 3 Aug 2021 04:24:58 +0700 Subject: [PATCH] Add `TakeItem()` an ability to deallocate key --- sources/Data/Collections/AssociativeArray.uc | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/sources/Data/Collections/AssociativeArray.uc b/sources/Data/Collections/AssociativeArray.uc index d01ecad..a2983a6 100644 --- a/sources/Data/Collections/AssociativeArray.uc +++ b/sources/Data/Collections/AssociativeArray.uc @@ -319,13 +319,23 @@ public final function Entry TakeEntry(AcediaObject key) * Returned value is no longer managed by the `AssociativeArray` (if it was) * and must be deallocated once you do not need it anymore. * - * @param key Key for which to return value. + * @param key Key for which to return value. + * @param freeKey Setting this to `true` will also free the key item was + * stored with. Passed argument `key` will not be deallocated, unless it is + * the exact same object as item's key inside caller collection. * @return Value, stored with given key `key`. If there is no value with * such a key method will return `none`. */ -public final function AcediaObject TakeItem(AcediaObject key) +public final function AcediaObject TakeItem( + AcediaObject key, + optional bool freeKey) { - return TakeEntry(key).value; + local Entry entry; + entry = TakeEntry(key); + if (freeKey) { + _.memory.Free(entry.key); + } + return entry.value; } /**