Andrei Alexandrescu:
> Keeping the length cached in a singly-linked list is a costly mistake.
> It works against splicing (an important list primitive) and most of the
> time you don't need it so why waste time updating it.
... LinkedList(T, bool WithLength=True) {
static if(WithLength) {
// defines @attribute length here etc
}
}
Inside the methods like the add there is another static if(WithLength) that
enables/disables the code that updates the length.
This allows to keep the length by default, but makes it easy to remove it when
desired.
Bye,
bearophile