Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Do you mean to use <code>listBox</code> and <code>listFiles</code> to refer to the same thing?</p> <p>I'm stepping out on a limb here, because I don't have a ton of experience with JavaScript, but I'd do this the same way that I'd do it in C, C++, or Java: By copying the remaining array elements down into their new locations.</p> <p>Assuming that <code>listFiles.selectedIndices</code> is sorted (and its contents are valid indices for dataArray), the code would be something like the following:</p> <p><strong>(WARNING: untested code follows.)</strong></p> <pre><code>// Don't bother copying any elements below the first selected element. var writeIndex = listFiles.selectedIndices[0]; var readIndex = listFiles.selectedIndices[0] + 1; var selectionIndex = 1; while(writeIndex &lt; (dataArray.length - listFiles.selectedIndices.length)) { if (selectionIndex &lt; listFiles.selectedIndices.length) { // If the read pointer is currently at a selected element, // then bump it up until it's past selected range. while(selectionIndex &lt; listFiles.selectedIndices.length &amp;&amp; readIndex == listFiles.selectedIndices[selectionIndex]) { selectionIndex++; readIndex++; } } dataArray[writeIndex++] = dataArray[readIndex++]; } // Remove the tail of the dataArray if (writeIndex &lt; dataArray.length) { dataArray.splice(writeIndex, dataArray.length - writeIndex); } </code></pre> <p><strong>EDIT 2009/04/04:</strong> Your Remove algorithm still suffers from the flaw that as you remove items in <code>listFiles.selectedIndices</code>, you break the correspondence between the indices in <code>arrayQueue</code> and those in <code>listFiles.selectedIndices</code>.</p> <p>To see this, try adding 3 files, then doing "Select All" and then hit Remove. It will start by removing the 1st file in the list (index 0). Now what <strong>had been</strong> the 2nd and 3rd files in the list are at indices 0 and 1. The next value taken from <code>listFiles.selectedIndices</code> is 1 -- but now, what had been the <strong>3rd</strong> file is at index 1. So the former File #3 gets spliced out of the array, leaving the former 2nd file un-removed and at index 0. (Using more files, you'll see that this implementation only removes every other file in the array.)</p> <p>This is why my JavaScript code (above) uses a <code>readIndex</code> and a <code>writeIndex</code> to copy the entries in the array, skipping the <code>readIndex</code> over the indices that are to be deleted. This algorithm avoids the problem of losing correspondence between the array indices. (It does need to be coded carefully to guard against various edge conditions.) I tried some JavaScript code <strong>similar to</strong> what I wrote above; it worked for me.</p> <p>I suspect that the problem in your original test case (removing the 2nd file, then adding another) is analogous. Since you've only shown part of your code, I can't tell whether the array indices and the data in <code>listFiles.selectedIndices</code>, <code>arrayQueue</code>, and <code>fileRefList.fileList</code> are always going to match up appropriately. (But I suspect that the problem is that they don't.)</p> <p>BTW, even if you fix the problem with using <code>splice()</code> by adjusting the array index values appropriately, it's still an O(N<sup>2</sup>) algorithm in the general case. The array copy algorithm is O(N).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload