array idioms

Jonathan M Davis jmdavisProg at gmx.com
Fri Feb 25 12:39:10 PST 2011


On Friday 25 February 2011 12:09:44 Jonathan M Davis wrote:
> On Friday, February 25, 2011 11:30:28 spir wrote:
> > * delete an element in a dyn array
> 
> I don't think that there is one right now. There should probably be a
> function in std.array which does what remove in std.container would do,
> but I don't believe that there's a function for it right now. So, the
> simplest way at the moment (albeit not the most efficient) would probably
> do to something like
> 
> auto found = findSplit(arr, value);
> auto newArr = array(chain(found[0], found[2]));
> 
> The efficient way to do it, however, would involve shifting all of the
> elements in the array, which is more complicated and not the kind of code
> that you really want to be rewriting every time that you need to remove an
> element. But you _do_ need to remember that removing an arbitrary element
> from an array is _not_ cheap, because even in the most efficient case,
> that means moving a potentially large number of elements in the array -
> unlike a linked list where removal is cheap.
> 
> Of course, popFront and popBack would be the best way to remove from the
> ends of an array, and _that_ is efficient.

Actually, on reflection, we probably _don't_ want a remove like std.container 
uses. What we probably want is something like remove and removeInPlace. The fact 
that dynamic arrays are really ranges rather than containers makes removing in 
place _far_ less desirable in many cases, and it's not necessarily a good idea 
to treat an array like a container in std.container.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list