How to concatenate one vector in another elegantly in ADVPL?

2

I have two vectors of numbers, aBuffer and aCandidato . Depending on a condition (external to the vector), I need to have aBuffer receive the contents of aCandidato to work on it later. aBuffer will be used by a function that is already ready to receive a vector of numbers, so I would not like to change it.

In Java, I would do something like this:

ArrayList<Integer> aBuffer = ...;
ArrayList<Integer> aCandidato = ...;

...

if (condicaoMisteriosa()) {
  aBuffer.addAll(aCandidato);
}

However, in ADVPL, I know only aadd , which adds an element to the end of the array. The code I can do is:

local aBuffer := {}
local aCandidato := {}
local lCondicaoMisteriosa := ...
local i

...

If lCondicaoMisteriosa
  For i := 1 len(aCandidato)
    aadd(aBuffer, aCandidato[i])
  Next i
EndIf

The Java equivalent of this code would be:

ArrayList<Integer> aBuffer = ...;
ArrayList<Integer> aCandidato = ...;

...

if (condicaoMisteriosa()) {
  for (int i = 0; i < aCandidato.size(); i++) {
    aBuffer.add(aCandidato.get(i));
  }
}

The alternative of concatenating the vectors in an intermediate vector, then doing the flatten (throwing the result in aBuffer ) similar to this answer in Python, but I do not see this as being elegant.

    
asked by anonymous 07.12.2018 / 02:35

1 answer

2

I think this does what you want, elegant is relative, but it seems simpler and probably faster (in ADVPL without testing I never doubt it might not be):

tamanhoAtual = len(aBuffer)
asize(aBuffer, tamanhoAtual + len(aCandidato))
acopy(aCandidato, aBuffer, 1, len(aCandidato), tamanhoAtual + 1)

I have increased the size of the target to fit the elements of the array source, invoked the font copy from element 1 to the last one and added from the next to the last element before being raised. If you do not increase the array it will give you an error, unless you already have enough space, but you are running the risk of overlapping something that should remain there.     

07.12.2018 / 02:49