Re: Effective STL Item 4 (size() vs. empty())
Howard Hinnant wrote:[color=blue]
> <snip>
> Now on to the question: What does O(1) list::size benefit?
>
> Answer: Both client code, and the implementation of list.
>[/color]
One quick question. It would seem to me that if you wanted to allow
splice, and O(1) list::size, then each list node would have to contain a
pointer to some kind of central information bank about the list, which
stores the size. if you didn't have this then you would have no way of
accessing the size information to update it after a splice out of the
list. Also you wouldn't even be able to check if a splice had occured.
You could make the first/last node of the list a different type to every
other node and then whenever you splice out of a list cycle around to
it, but that would be expensive.
Basically, I don't see how you can make a list with O(1) size without
adding an extra item to every single node, which could potentially make
a list take 25% more space (for list<int> and sinilar things). That
isn't to be sniffed at?
Chris
Howard Hinnant wrote:[color=blue]
> <snip>
> Now on to the question: What does O(1) list::size benefit?
>
> Answer: Both client code, and the implementation of list.
>[/color]
One quick question. It would seem to me that if you wanted to allow
splice, and O(1) list::size, then each list node would have to contain a
pointer to some kind of central information bank about the list, which
stores the size. if you didn't have this then you would have no way of
accessing the size information to update it after a splice out of the
list. Also you wouldn't even be able to check if a splice had occured.
You could make the first/last node of the list a different type to every
other node and then whenever you splice out of a list cycle around to
it, but that would be expensive.
Basically, I don't see how you can make a list with O(1) size without
adding an extra item to every single node, which could potentially make
a list take 25% more space (for list<int> and sinilar things). That
isn't to be sniffed at?
Chris
Comment