On 01/08/2005 17:31, Kevin Newman wrote:
[snip]
I'll just have to figure out how to keep the indexes contiguous when I
remove an element (I'm guessing there is a way to do that using
Array.slice, instead of delete - I'll have to experiment).
The Array.prototype .splice method[1] can be used to delete (and insert)
elements at arbitrary locations within an array. Essentially, it just
copies values about. However, this is a problem that I alluded to when I
mentioned the differences between sequential and linked data structures.
With the former, insertions and deletions at any location except the end
require all elements after the point of change to be shifted which, in a
large structure, can take a long time. With linked lists, it's simply a
matter of changing the links as the new element can be added anywhere in
memory (for insertion) or just forgotten about (for deletion).
Sequential structures offer fast, random look ups, but as yours will
always be complete iteration through the collection, this is only an
advantage when inserting or deletion at specific locations. Even then,
insertion for you will always be at the end once you've checked that the
new element is not a duplicate (more complete iteration). That just
leaves deletion, which should be rare anyway.
Despite how it may seem, I'm not necessarily pushing for you to use a
list. I do, but it's up to you to decide for yourself. Theoretical
reasons don't always in scripting, particularly as ECMAScript Arrays
don't have the same drawbacks that 'real' arrays do (including
reallocation during insertion).
Mike
[1] Not available in JScript 5 (usually IE5.0) and earlier.
--
Michael Winter
Prefix subject with [News] before replying by e-mail.