🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

4 total messages Started by Tony Cook Thu, 21 Sep 1995 05:44
Re: Making STL user-friendly?? [Re: STL find errorprone]
#2003
Author: Tony Cook
Date: Thu, 21 Sep 1995 05:44
69 lines
2938 bytes
Mumit Khan (khan@xraylith.wisc.edu) wrote:
: This is a rather interesting thread started by Tony Cook in c.std.c++
: on whether STL is error-prone or not.

Thanks <grin>.

: 1. What are the various things that you have find to be error-prone?

Take the find example again:
  vector<int> myContainer;
  vector<int>::iterator found = 
	find(myContainer.begin(), myContainer.end());
  if (found == myContainer.end())
	....

Here I must supply the container name 3 times in a to perform a
fairly simple (and common) operation - this is what I find
error-prone.

: 2. What kind of changes would you like to see in the interface to the
:    STL algorithms? The thread started with Tony Cook using the begin/
:    end iterator in find algorithm (which I consider essential since I 
:    do partial searches all the time) as an example if I remember
:    correctly. Cook suggests find() return a bool and modify the iterator 
:    argument ... 

I don't see any need to _remove_ the current begin/end idiom - as
you say it's useful - but to simply supply extra interfaces to
perform the most common functions in a more compact way -
specifically, operating on the whole container, and possibly
operating from the start of the container to a specific point and
from a specific point to the end of the container.

Note that even with a subset of a container, we still have multiple
redundant references, eg:

  vector<int>::iterator searchStart, searchEnd;
  // ... searchStart, searchEnd set somehow
  vector<int>::iterator searchResult = 
	find(searchStart, searchEnd);
  // we use searchEnd twice
  if (searchResult == searchEnd)
	...

or, possibly more realisticly:
  vector<int>::iterator searchStart, searchEnd;
  // ... searchStart, searchEnd set somehow
  vector<int>::iterator searchPos = searchStart;
  while (searchPos = find(searchPos, searchEnd)) != searchEnd)
  {
    //...process it
    ++searchPos; // prepare for next one
  }

The test here should be 'did we find it' - not 'are we at this point
yet' - find 'knows' where the end of the range is - why can't it
report whether or not it was successful without us having to supply
one of it's parameters again?

-- 
        Tony Cook - tony@online.tmx.com.au
                    100237.3425@compuserve.com


---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Re: Making STL user-friendly?? [Re: STL find errorprone]
#2017
Author: Alex Martelli
Date: Sat, 23 Sep 1995 20:57
62 lines
2940 bytes
Tony Cook <tony@online.tmx.com.au> writes:
	...
>I don't see any need to _remove_ the current begin/end idiom - as
>you say it's useful - but to simply supply extra interfaces to
>perform the most common functions in a more compact way -
>specifically, operating on the whole container, and possibly

I will agree on this one -- it IS going to be frequent, and:
	find(container)
IS a distinct enhancement over:
	find(container.begin(), container.end())

>operating from the start of the container to a specific point and
>from a specific point to the end of the container.

But how could you do better here than
	find(container.begin(), specificPoint)
or
	find(specificPoint, container.end())
???


>  vector<int>::iterator searchStart, searchEnd;
>  // ... searchStart, searchEnd set somehow
>  vector<int>::iterator searchPos = searchStart;
>  while (searchPos = find(searchPos, searchEnd)) != searchEnd)
>  {
>    //...process it
>    ++searchPos; // prepare for next one
>  }

>The test here should be 'did we find it' - not 'are we at this point
>yet' - find 'knows' where the end of the range is - why can't it
>report whether or not it was successful without us having to supply
>one of it's parameters again?

Because, I guess, there is no "null" value for every sort of
iterator -- one that could only be used for automatic conversion
to bool returning false, while every other iterator would convert
to true.  This is a distinct convenience with pointers and one
of the most common C idioms, of course.  If there was a "null",
then it would seem to me to be the natural value to return from
an iterator-returning function that "fails" (in a "soft" sense,
not in an exception-raising one).  "Null" would also be a natural
for initialization of iterators, etc, again in analogy with
pointers.  I assume there were solid reasons not to guarantee
the existence of "null iterators" for every kind...?


Alex
--
DISCLAIMER: these are TOTALLY personal opinions and viewpoints, NOT connected
in any way with my employer, nor any other organization or individual!
Email: martelli@cadlab.it                            Phone: +39 (51) 597313
CAD.LAB s.p.a., v. Ronzani 7/29, Casalecchio, Italia   Fax: +39 (51) 597120


---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Re: Making STL user-friendly?? [Re: STL find errorprone]
#2027
Author: John Max Skaller
Date: Mon, 25 Sep 1995 01:59
52 lines
2717 bytes
martelli@cadlab.cadlab.it (Alex Martelli) wrote:
>
>Tony Cook <tony@online.tmx.com.au> writes:
>	...
>>I don't see any need to _remove_ the current begin/end idiom - as
>>you say it's useful - but to simply supply extra interfaces to
>>perform the most common functions in a more compact way -
>>specifically, operating on the whole container, and possibly
>
>I will agree on this one -- it IS going to be frequent, and:
>	find(container)
>IS a distinct enhancement over:
>	find(container.begin(), container.end())

    I DON'T agree. 

    It is crucial for orthogonality that the algorithm interfaces
are isolated from any notion of containers -- which is 
what the STL iterator architecture is for.

             ITERATORS
               ^    \-------------\------\---------... 
             /       \             \      \ 
            /         V             V      V
      ALGORITHMS     CONTAINERS DEVICES PROCESSES

    Providing standard algorithms taking container arguments
would destroy the essential independence of the notions
by introducing direct coupling. The _indirection_ provided
by iterators is what makes STL work so well.

    What makes STL such an excellent library is the
way iterators are used to interface containers and algorithms
by contracts on the intermediaries that service both --- iterators.
This leads to predictable orthogonal combination -- where
predictable refers not only to correctness, but also performance.

    There is nothing wrong with writing convenience
functions like "find(container, value)" --- the very ability to
do so in a few lines of code --- in several different ways ---
is exactly why this should NOT be standardised.
Put such a function in your own namespace, to avoid clashes.

-- 
John Max Skaller               voice: 61-2-566-2189
81 Glebe Point Rd              fax:   61-2-660-0850
GLEBE NSW 2037                 email: maxtal@suphys.physics.oz.au
AUSTRALIA                      email: skaller@maxtal.com.au
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Re: Making STL user-friendly?? [Re: STL find errorprone]
#2031
Author: kanze
Date: Mon, 25 Sep 1995 15:45
51 lines
3130 bytes
In article <MATT.95Sep23125702@Austern> martelli@cadlab.cadlab.it
(Alex Martelli) writes:

|> Tony Cook <tony@online.tmx.com.au> writes:

    [...]
|> >The test here should be 'did we find it' - not 'are we at this point
|> >yet' - find 'knows' where the end of the range is - why can't it
|> >report whether or not it was successful without us having to supply
|> >one of it's parameters again?

|> Because, I guess, there is no "null" value for every sort of
|> iterator -- one that could only be used for automatic conversion
|> to bool returning false, while every other iterator would convert
|> to true.  This is a distinct convenience with pointers and one
|> of the most common C idioms, of course.  If there was a "null",
|> then it would seem to me to be the natural value to return from
|> an iterator-returning function that "fails" (in a "soft" sense,
|> not in an exception-raising one).  "Null" would also be a natural
|> for initialization of iterators, etc, again in analogy with
|> pointers.  I assume there were solid reasons not to guarantee
|> the existence of "null iterators" for every kind...?

One nice solution comes from Barton and Nackman: the Fallible class
template.  It is also applicable to pointers, and IMHO, results in a
much cleaner interface than using null pointers.

The problem, of course, is that although the interface is much
cleaner, the resulting code, at least with today's compilers, will be
significantly slower, since it replaces returning a pointer with
returning a class.  For functions like find, which contain a loop, I
seriously doubt that this would make a serious difference, though.
And of course, in the cases where the iterator is already a class,
the difference is negligible.  (I would hope that such cases will be
the overwhelming majority.)

In many ways, I think it might be better if the STL algorithms used
Fallible.  But given the schedule, and the time constraints, I don't
think it would a good idea to introduce this change now.  (I wish I'd
paid more attention to library issues, and thought of this,
earlier:-(.)
--

James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils en informatique industrielle --
                              -- Beratung in industrieller Datenverarbeitung
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads