|
From: Hossein H. <ult...@em...> - 2003-07-14 12:19:53
|
Dear Ioannis,
> > > Eh? Why? Both have the same speed, actually empty is usually
> > >
> > > // inside the class definition
> > > bool empty() const { return size()==0; }
> >
> > This can be the case, but that isn't always true.
> >
> > I know that holds for many of STL platforms, but they do not have to
> > follow that way.
>
>
> Yes. Any implementor can follow his way, the standard defines only the
> template interface but the above is a reasonable implementation. And I got
> it straight from TC++PL 3. :-)
But that even doesn't mean that there aren't any other reasonable
implementations.
> It is not required by the standard but it would be prudent to not calculate
> size() every time it is called. Also if I would have a choice between
> storing information on whether it is empty or the size I would have chosen
> the size since the empty can be estimated by the size and would require less
> checks in the instance of the code where I would have placed a lot of checks
> if the empty value is true or false and change it.
Had you read "Effective STL," this has't been your answer. Sometimes,
there are policies such as willing to have a constant-time operation
on the map that have higher proiority for the implementer (for any
reason they know). And achieving that runtime needs you to cut down
the use of size update after any operation of the map.
I think the existance of nasty C++ implementations such as VC++6,
reveals that this is not just a fiction, and there even are wellknown
implementations who may have chosen that way.
> Anyway as I said only the interface is required by the standard but in C++
> world all strive for maximum performance.
You'r right. But max-performance of what? I mean, if there is an
operation, say O, that has higher priority from your point of view (as
the implementer, or disigner, etc.), and you want O to be done in a
constant time, and that forces you to forget updating size after O,
will you still think that max-performance obliges you to have size as
an internal member? Haven't you ever encountered nasty
implementations such as VC++6 that often have such higher priority
operations that don't seem to be reasonable at all? I have just
realised that the compiler we were talking about was GCC, and I
hartapologise for the suspection I have had to his majesty. But
remember that I didn't know that's GCC. Knowing that, don't you still
preserve me the right of suspecting the implementation?
> > (If you want me to address the citation precisely, it is in Section
> > 9.1.1, i.e., Container. And the complete bibliography is:
> >
> > Matthew H. Austern, Generic Programming and the STL: Using and
> > Extending the C++ Standard Template Library, Addisson Wesley, 1999,
> > ISBN: 0-201-30956-4.)
> >
> > These lines make me to become more sure that there isn't any guarantee
> > in the Standard for size() and empty() to be equal in the sense of
> > runtime.
>
>
>
> Of course there is not. Nor there is any guarantee on which is faster nor
> whether it takes milliseconds, seconds and weeks. However as I said what I
> provided is a reasonable implementation.
But, I want to mention one time more that, that doesn't mean the
shortage of any other reasonable one.
> Yes. It may also calculate the 1000 first digits of pi (3.14..) in the
> meantime, but we are discussing reasonable implementations and having to
> calculate the size() of every container that provides size() each time it is
> called (and it may have millions) doesn't sound too reaaonable to me.
Come on! I'm not joking! Even if that's not reasonable at all,
remember that there are largely-used implementations such as VC++6
that often make those mistakes.
> Yes, it may has decided manyy things. However we mau assume that all member
> functions are maximally efficient, else throw the platform away!
I agree with you in the sense of efficiency, but remember the problem
with (sometimes unwanted) priorities.
Furthermore, sometimes, the dicision of throwing a platform away is
not whithin our duty. Nor can we complain on it. E.g., the project may
be in a platform that is used for years and years, and changing it
imposes more cost to the wendors. As an other example, you may be one
of the ordinary programmers in more than 1000, in a software-house
that wants its products to have good GUIs, and so have chosen VC++6 as
they don't want to waste time for reinvnting the wheel! They may well
know about the vast erea of VC++6's bugs, but they have to tollerate
against it. So they are not going to change the platform. What would
you do then?
> > You may still think that there empty() and size() have the same
> > runtime. But that doesn't mean there are equal in any case. If you
> > have any doubt on this issue, consider Item 4 of Effective STL.
>
>
> Read TC++PL 3. Straight from the creator of C++.
Oh! That's a great book. The best "General Purpose" book in C++. I
read TC++PL:Spec, couple of years ago. (I myself, was teaching C++ in
university at those days, but unfortunately, I hadn't the opportunity
of reading that great book before. It was great and I found tones of
new fine points in it.) From that time, it's the first reference of
my C++ courses in university, and I'm very proud of it.
But that doesn't mean that the other books do not have any stuff to
mention. "Effective STL," for example, is one of the best books in the
history of C++, and is the most expert book in STL, indeed.
Regards,
--Hossein
--
__________________________________________________________
Sign-up for your own FREE Personalized E-mail at Mail.com
http://www.mail.com/?sr=signup
CareerBuilder.com has over 400,000 jobs. Be smarter about your job search
http://corp.mail.com/careers
|