473,408 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

everytime I read this it make me mad

This is from SGI's FAQ, its the justification for why list<T>::size() is
linear time in their library (and in gcc library too since their code is
based on SGI)

<quote>
Why is list<>::size() linear time?

The size() member function, for list and slist, takes time proportional
to the number of elements in the list. This was a deliberate tradeoff.
The only way to get a constant-time size() for linked lists would be to
maintain an extra member variable containing the list's size. This would
require taking extra time to update that variable (it would make
splice() a linear time operation, for example), and it would also make
the list larger. Many list algorithms don't require that extra word
(algorithms that do require it might do better with vectors than with
lists), and, when it is necessary to maintain an explicit size count,
it's something that users can do themselves.
</quote>

Let's take this apart point by point

'It would make the list larger' - Only by a single word for an entire
list! What planet are these guys on? Four bytes for a list that might
contain hundreds or thousands of items. This is not a valid concern.

'This would require taking extra time to update that variable' - True
but only by a constant amount, a constant time operation would still be
a constant time operation. Whereas the downside is that their decision
means that a potentially constant time operation, size(), has benn
transformed into a linear time operation.

'it would make splice() a linear time operation' - Not the whole list
splice operation, or the single item splice operation, only the very
rarely used partial list splice operation. So we have a commonly used
operation, size(), being sacrificed for the benefit of an operation that
most C++ programmers have probably never used in their entire lives.

'Many list algorithms don't require that extra word' - This is the real
kicker. I often write generic algorithms, ones that operate on any type
of container. It's one of the cool things you can do with C++. But if
those algorithms use size() then I have to think 'what if someone uses
this code with SGI's list. Sunddenly my nice linear time algorithm has
been transformed into a quadratic one. To avoid this I have to add a
size variable to my code, even though every other container, and every
other list implementation already has one. So I end up adding an extra
word, to code that most of the time would not need it, in order to patch
the deficiences of SGI's implmentation.

No question here, I just felt I had to get that off my chest.
Jan 24 '07 #1
7 1611
John Harrison wrote:
This is from SGI's FAQ, its the justification for why list<T>::size() is
linear time in their library (and in gcc library too since their code is
based on SGI)
....reasoning snipped...
>
No question here, I just felt I had to get that off my chest.
Still nothing stopping you from creating your own list container or even
a specialization which is platform dependant that provides a fast size()
method.

I tend to agree with the SGI philosophy of keeping things as simple as
possible and paying for more function when you need it.

It does not always give you the right answer (like in this case) but I
find it the best alternative most of the time - hence the tradeoff SGI made.
Jan 24 '07 #2
"Gianni Mariani" <gi*******@mariani.wswrote in message
news:45**********************@per-qv1-newsreader-01.iinet.net.au
John Harrison wrote:
>This is from SGI's FAQ, its the justification for why
list<T>::size() is linear time in their library (and in gcc library
too since their code is based on SGI)
...reasoning snipped...
>>
No question here, I just felt I had to get that off my chest.

Still nothing stopping you from creating your own list container or
even a specialization which is platform dependant that provides a fast
size() method.

I think you missed the part where he says he writes generic algorithms
intended for use (by other people) with any container.

--
John Carson

Jan 24 '07 #3
John Carson wrote:
"Gianni Mariani" <gi*******@mariani.wswrote in message
news:45**********************@per-qv1-newsreader-01.iinet.net.au
>>John Harrison wrote:
>>>This is from SGI's FAQ, its the justification for why
list<T>::size() is linear time in their library (and in gcc library
too since their code is based on SGI)

...reasoning snipped...
>>>No question here, I just felt I had to get that off my chest.

Still nothing stopping you from creating your own list container or
even a specialization which is platform dependant that provides a fast
size() method.

I think you missed the part where he says he writes generic algorithms
intended for use (by other people) with any container.
I think you missed the point altogether.

Jan 24 '07 #4
John Harrison <jo*************@hotmail.comwrote:
This is from SGI's FAQ, its the justification for why list<T>::size() is
linear time in their library (and in gcc library too since their code is
based on SGI)
They forgot an answer:

A: Because the standard only requires size() to be linear time.
'Many list algorithms don't require that extra word' - This is the real
kicker. I often write generic algorithms, ones that operate on any type
of container. It's one of the cool things you can do with C++. But if
those algorithms use size() then I have to think 'what if someone uses
this code with SGI's list. Sunddenly my nice linear time algorithm has
been transformed into a quadratic one. To avoid this I have to add a
size variable to my code, even though every other container, and every
other list implementation already has one. So I end up adding an extra
word, to code that most of the time would not need it, in order to patch
the deficiences of SGI's implmentation.
If you write generic algorithms that assume size() is constant time,
then you are depending on implementation details of the containers
passed to your algorithms.

Between empty() and begin(), end(), I'm having trouble imagining a
generic algorithm that requires the use of size(). Could you help me out
on that with an example? (I'm sure there are some, I just can't think of
any off hand.)
Jan 24 '07 #5


On 24 Jan, 14:28, "Daniel T." <danie...@earthlink.netwrote:
John Harrison <john_androni...@hotmail.comwrote:
This is from SGI's FAQ, its the justification for why list<T>::size() is
linear time in their library (and in gcc library too since their code is
based on SGI)
They forgot an answer:

A: Because the standard only requires size() to be linear time.
Actually I cut that part.
If you write generic algorithms that assume size() is constant time,
then you are depending on implementation details of the containers
passed to your algorithms.
I think it's very likely that the standard was written that way because
they didn't want to break existing implementations not because it was
thought to be a good idea to make an exception of std::list. In other
words mine is a 'in perfect world' argument.
>
Between empty() and begin(), end(), I'm having trouble imagining a
generic algorithm that requires the use of size(). Could you help me out
on that with an example? (I'm sure there are some, I just can't think of
any off hand.)
This situation has only occured for me once, and I can't recall the
details now, sorry.

Jan 24 '07 #6


On Jan 24, 2:45 am, John Harrison <john_androni...@hotmail.comwrote:
This is from SGI's FAQ, its the justification for why list<T>::size() is
linear time in their library (and in gcc library too since their code is
based on SGI)

<quote>
Why is list<>::size() linear time?

The size() member function, for list and slist, takes time proportional
to the number of elements in the list. This was a deliberate tradeoff.
The only way to get a constant-time size() for linked lists would be to
maintain an extra member variable containing the list's size. This would
require taking extra time to update that variable (it would make
splice() a linear time operation, for example), and it would also make
the list larger. Many list algorithms don't require that extra word
(algorithms that do require it might do better with vectors than with
lists), and, when it is necessary to maintain an explicit size count,
it's something that users can do themselves.
</quote>

Let's take this apart point by point

'It would make the list larger' - Only by a single word for an entire
list! What planet are these guys on? Four bytes for a list that might
contain hundreds or thousands of items. This is not a valid concern.

'This would require taking extra time to update that variable' - True
but only by a constant amount, a constant time operation would still be
a constant time operation. Whereas the downside is that their decision
means that a potentially constant time operation, size(), has benn
transformed into a linear time operation.

'it would make splice() a linear time operation' - Not the whole list
splice operation, or the single item splice operation, only the very
rarely used partial list splice operation. So we have a commonly used
operation, size(), being sacrificed for the benefit of an operation that
most C++ programmers have probably never used in their entire lives.

'Many list algorithms don't require that extra word' - This is the real
kicker. I often write generic algorithms, ones that operate on any type
of container. It's one of the cool things you can do with C++. But if
those algorithms use size() then I have to think 'what if someone uses
this code with SGI's list. Sunddenly my nice linear time algorithm has
been transformed into a quadratic one. To avoid this I have to add a
size variable to my code, even though every other container, and every
other list implementation already has one. So I end up adding an extra
word, to code that most of the time would not need it, in order to patch
the deficiences of SGI's implmentation.

No question here, I just felt I had to get that off my chest.
If this makes you mad, you must live a fairly sheltered life,
programming-wise. I reserve my moments of anger/despair for code that
seems to have no rationale whatsoever for its design.

Seems like you could write a class template that
inherits from any STL container and keeps track
of the size (plus partial specilizations like:

template <typename T>
class Counted<std::vector<T
: public std::vector<T>
{ };

for STL containers that already have a size()
member. Or maybe the boost template
meta-programming library has some trick
in it to generally handle the case where
the base container has a size() member.)

If you decide to do this, please make the
result open source, and post us a link.

Jan 24 '07 #7
W Karas wrote:
>
On Jan 24, 2:45 am, John Harrison <john_androni...@hotmail.comwrote:
>>This is from SGI's FAQ, its the justification for why list<T>::size() is
linear time in their library (and in gcc library too since their code is
based on SGI)

If this makes you mad, you must live a fairly sheltered life,
programming-wise. I reserve my moments of anger/despair for code that
seems to have no rationale whatsoever for its design.
No, I think I just get mad fairly easily!
Jan 24 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Richard Hollenbeck | last post by:
Is there JavaScript code I could insert into a page to automatically notfy me by email everytime I get a hit on a certain webpage?
3
by: aaagent | last post by:
I have a test WinApp that I use to test middle-tier methods why am I having to re-GAC everytime I re-compile in order to debug into my middle-tier projects?
8
by: Abhishek | last post by:
Hi! I need to create a unique password everytime i click a button . what technique/Algo should i follow. Abhishek
2
by: Harry Whitehouse | last post by:
I'm coming from an ISAPI background and migrating code to Webservices. In the ISAPI world, there are sections of code that are invoked only once when the DLL is loaded by IIS. It's a place...
6
by: andrea azzini | last post by:
hi to all I use this query SELECT SQL_CALC_FOUND_ROWS * FROM prods LIMIT 100,10; I'd like to know how many rows this query returns without limit, but using this select FOUND_ROWS(); the...
1
by: hhyong99 | last post by:
I need to populate a textarea with different id in a form everytime a button is clicked. Is there any way to do this? thanks
3
by: CptDondo | last post by:
I am working on an embedded system. The entire configuration for the system is stored in an XML file, which is pretty long. It takes about 3 seconds to open the file using domxml_open_file. ...
9
by: Achintya | last post by:
Is it good to assert the pointer *each* time after a new() is called? or it should be a normal if condition check. which of below is good practice: (I know assert works only in debug) 1) .........
11
by: Bocah Sableng | last post by:
Hi, I'm new member of this group. I had added new virtual host at my intranet server. The new virtual host configuration on httpd.conf is similar with the old one. At the new virtual host, the...
6
by: crack.the.hack | last post by:
Hi All, If I am changing the database machine, what should I do not to prep bind the sqc files everytime. Because I need to build my application everytime the database is changed? Is there...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.