LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to move rows in an array without allocating new memory.

This is totally possible and efficient in O(n) using only swaps. I don't know what lab view is so I'll show some C that manipulates a string, and I'll use the left/right convention instead of up/down. I understand we're trying to delete a contiguous section of array elements and reinsert them at another index while preserving the order of the remainder of the array. The heart of the algorithm is tail recursive and easily converted to a loop. First, know that shifting G elements to the right by D is the same as shifting D elements to the left by G. From this we see it is possible to always work left to right, placing a final value into each index as it is encountered. Nearing the end of the array we find some rotated version of the elements having size G being moved. They are rotated by D%G. So simply recurse to unrotate this array until D%G is zero.

 

Here's the heart of it.

 

void moverecurse(char *a, int g, int d)
{
  if (d == 0) return;
  for (int i=0; i<d; i++)
    swap(a[i], a[i+g]);
  moverecurse(a+d, g-d%g, d%g);
}

Now to handle negative values of g and d, and to cover the strange case G = 0, add some setup code. The purpose is to transform forward and backward offsets into forward offsets only.

 

/* Moves group of elements size g distance d in array a */
void move(char *a, int g, int d)
{
  /* Group size < 0 means look backward in array */
  if (g < 0) {
    a += g;
    g = -g;
  }
  /* Distance < 0 means move left */
  if (d < 0) {
    a += d;
    d = -d;
    swap(d, g);
  }

  if (g)
    moverecurse(a, g, d);
}

If you dislike recursion you can replace the call to moverecurse with this.

 

      while (d) {
 	for (int i=0; i<d; i++)
	  swap(a[i], a[i+g]);
	a += d;
	d %= g;
	g -= d;
      }

I hope this is of use to someone as I did make a minor sacrafice to obtain it. On my last flight I made a fool of myself moving bits of shredded napkin around on my tray table figuring it out.

0 Kudos
Message 11 of 17
(786 Views)

"I don't know what LabVIEW is"   so why are posting in a LabVIEW forum.  Particular one that is 6 years old.

 

*****

Long list of C code

*****

 

Why do we want to read all of this?

 

What is the point of this message?

0 Kudos
Message 12 of 17
(775 Views)

@SamuD wrote:

I don't know what lab view is so I'll show some C that manipulates a string...


Which, as RF mentions, will basically make it irrelevant to probably everyone reading this, as this is a LabVIEW board. The logic for moving bytes around is simple enough, but isn't really relevant in LabVIEW, as it does all of the allocations automatically. The user never has direct access to something like malloc() or pointers.


___________________
Try to take over the world!
0 Kudos
Message 13 of 17
(766 Views)

The question is about an algorithm. Algorithms are language agnostic. I searched for this exact problem and found this discussion to most closely discribe the problem for which I needed a solution. It was a six year old question with no algorithmic solution, which is what the OP needed to avoid allocation. If you can't increment a pointer you'd split it into base+offset i.e. the signature of the recursive function becomes moverecurse(array, i, g, d) and a[whatever] becomes array[i+whatever].

0 Kudos
Message 14 of 17
(762 Views)

@SamuD wrote:

It was a six year old question with no algorithmic solution, which is what the OP needed to avoid allocation.


The algorithms of the solutions were in LabVIEW and the problem was solved! (yes, it was!).

The solution and algorithm is not language agnostic because your "solution" cannot be applied to LabVIEW.

Your post is completely irrelevant and does not belong here.

0 Kudos
Message 15 of 17
(743 Views)

any body have a idea about this code on the picture ?1.jpg

0 Kudos
Message 16 of 17
(713 Views)

What is your question?

 

What idea are you looking for?

0 Kudos
Message 17 of 17
(703 Views)