home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!pmafire!mica.inel.gov!ux1!news.byu.edu!eff!sol.ctr.columbia.edu!usc!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!cbnews!cbnewsm!cbnewsl!cbnewsk!pegasus!hansen
- From: hansen@pegasus.att.com (Tony L. Hansen)
- Subject: Re: Making qsort type-safe
- Organization: AT&T
- Date: Mon, 16 Nov 1992 15:45:33 GMT
- Message-ID: <1992Nov16.154533.17360@cbnewsk.cb.att.com>
- Summary: examples of unsafeness
- Keywords: sorting, templates, qsort
- References: <1992Nov4.162131.10289@cs.brown.edu> <1992Nov12.194207.19040@cbnewsk.cb.att.com> <1992Nov13.215612.7145@netcom.com>
- Sender: hansen@cbnewsk.cb.att.com (tony.l.hansen)
- Lines: 42
-
- < From: mdr@netcom.com (Marc D. Rossner)
- < In article <1992Nov12.194207.19040@cbnewsk.cb.att.com>, hansen@pegasus.att.com (Tony L. Hansen) writes:
- << [ .. describes a safe qsort]
- << Is that the end of the story, though? No, of course not, because just
- << swapping the bits around may not be sufficient to swap a[0] and a[1]. They
- << may truly have to be swapped using assignments, and qsort provides no way to
- << do that.
-
- < Can you think of a good example of this? I realize that COPYING an
- < objects bits may be insufficient for making a conceptually independent
- < copy, but I can't think why swapping the bits of two objects would not
- < leave the two objects physically and conceptually swapped.
-
- Two examples come to mind, both related:
-
- o An object consists of an outer object with a pointer to an inner
- object. The inner object also contains a pointer back to the outer
- object. Swapping the bits of the outer object won't get the inner
- objects updated properly.
-
- o An external registry of a pointer and some information about the
- object is maintained about for each object, and doing an assignment
- will update that external registery.
-
- Defining a safe and efficient sort function is hard, as there are tradeoffs
- which must be made. Some tradeoffs are appropriate in some cases and not in
- others. The example of swapping bits is one tradeoff. There may be times
- that swapping bits is just fine; other times it's disastrous. Some times a
- third algorithm is more appropriate. The example in the first bullet item
- above could probably be handled by swapping the bits and then updating the
- inner object's pointer, but only a swap<T>() function could know that.
- qsort<T>() cannot know apriori how to handle any given type. On a related
- note, the comparison function doesn't necessarily have to provide a total
- ordering of two elements, and using operator<() would be sufficient.
- Similarly, just using the less-than relationship, provided by some other
- function, would often be sufficient. This probably argues for a set of
- related functions which provide various options for the different tradeoffs.
-
- Tony Hansen
- hansen@pegasus.att.com, tony@attmail.com
- att!pegasus!hansen, attmail!tony
-
-