get number of items in DList

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jul 11 07:46:37 PDT 2014


On Fri, Jul 11, 2014 at 10:23:58AM -0300, Ary Borenszweig via Digitalmars-d-learn wrote:
> On 7/11/14, 4:46 AM, bearophile wrote:
> >pgtkda:
> >
> >>How can i get the number of items which are currently hold in a
> >>DList?
> >
> >Try (walkLength is from std.range):
> >
> >mydList[].walkLength
> >
> >Bye,
> >bearophile
> 
> So the doubly linked list doesn't know it's length? That seems a bit
> inefficient...

It should be relatively simple to write a wrapper that *does* keep track
of length.

The main problem, though, comes from list splicing: given two arbitrary
points in the list, if you splice out the section of the list in
between, there's no easy way to know how many items lie in between, so
you'll have to walk the list to recompute the length then. Which sorta
defeats the purpose of having a linked list. :)


T

-- 
Valentine's Day: an occasion for florists to reach into the wallets of nominal lovers in dire need of being reminded to profess their hypothetical love for their long-forgotten.


More information about the Digitalmars-d-learn mailing list