From a1a957f8be92613c329e12fb75b0a8dd27854788 Mon Sep 17 00:00:00 2001 From: Gavin Halliday Date: Fri, 8 Nov 2024 11:36:34 +0000 Subject: [PATCH] HPCC-32971 Add removeAndSwapLast(n) to the array classes Signed-off-by: Gavin Halliday --- system/jlib/jarray.hpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/system/jlib/jarray.hpp b/system/jlib/jarray.hpp index 9870c8d9c31..1e0d03fe9af 100644 --- a/system/jlib/jarray.hpp +++ b/system/jlib/jarray.hpp @@ -21,6 +21,7 @@ #define JARRAY_HPP #include +#include #include "platform.h" #include "jiface.hpp" @@ -281,6 +282,18 @@ class ArrayOf : public AllocatorOf if (!nodestruct) SELF::destruct(pos); SELF::_move( pos, pos + 1, ( SELF::used - pos ) ); } + //Remove the element at pos and move the last element to pos - to avoid moving all elements + void removeAndSwapLast(aindex_t pos, bool nodestruct = false) + { + assertex(pos < SELF::used); + SELF::used --; + if (!nodestruct) SELF::destruct(pos); + if (pos != SELF::used) + { + MEMBER * head = (MEMBER *)SELF::_head; + head[pos] = std::move(head[SELF::used]); + } + } void removen(aindex_t pos, aindex_t num, bool nodestruct = false) { assertex(pos + num <= SELF::used);