[boost] Sorting list shared_ptr foo using std::list::sort()

2003-04-24 Thread HEATH DAVIS
I have experienced some difficulty with sorting lists of type:

list  shared_ptr  foo  

Here is the expected (sorted) program output:

424238335
596516649
719885386
846930886
1189641421
1649760492
1681692777
1714636915
1804289383
1957747793
Instead, I end up with:

1804289383
846930886
1681692777
1714636915
1957747793
424238335
719885386
1649760492
596516649
1189641421
Below is the code in question:



#include iostream
#include list
using namespace std;
#include boost/shared_ptr.hpp
using namespace boost;


class my_class
{
public:
int m_val;
public:
my_class();
bool operator(const my_class);
void Display();
};


my_class::my_class()
{
m_val = rand();
}
bool my_class::operator(const my_class arg)
{
return m_val  arg.m_val;
}
void my_class::Display()
{
cout  m_val  \n;
}


int main( int argc, char **argv )
{
// list  my_class  my_list;
// list  my_class ::iterator my_iter;
list  shared_ptr  my_class   my_list;
list  shared_ptr  my_class  ::iterator my_iter;
//populate my_list
for (int x=0; x10; x++)
{
// my_class tmp_obj;
shared_ptr  my_class  tmp_obj;
tmp_obj.reset(new my_class);
my_list.push_back(tmp_obj);
}
//sort the list
my_list.sort();
//display contents of my_list
my_iter = my_list.begin();
while (my_iter != my_list.end())
{
// (*my_iter).Display();
(*my_iter)-Display();

++my_iter;
}
return 0;
}


Thanks in advance for any help you may render...

_
Help STOP SPAM with the new MSN 8 and get 2 months FREE*  
http://join.msn.com/?page=features/junkmail

___
Unsubscribe  other changes: http://lists.boost.org/mailman/listinfo.cgi/boost


[boost] Re: Sorting list shared_ptr foo usingstd::list::sort()

2003-04-24 Thread Heath Davis
Bravo!!! This is a truly novel approach!  I agree that the 
shared_ptr::operator is meaningless.  If boost wishes to claim STL 
compatibility, container functions should be allowed to operate directly 
on the base level objects, rendering smart pointers completely 
transparent.  Proper functionality would then be defined as:

The order of any given STL container will be predictable and definite 
following any given STL container operation. The resulting order of an 
operation against any std::some_container  object  would then be 
identical to the resulting order of the same operation against 
std::some_container  boost::some_smart_pointer  object  .  No 
special preparation will be required on behalf of object beyond that of 
being directly containable by STL.  Otherwise stated, if object works 
correctly in STL containers, nothing intrusive must be done to object to 
make it compatible with boost, nor will any exteral helper functions 
be required to make boost::some_smart_pointer transparent.

If your sortable_ptr class brings boost in that direction, then I thank 
you for addressing this issue.

I have experienced some difficulty with sorting lists of type:

list  shared_ptr  foo  

For a while I have been toying with the idea of a smart pointer called 
sortable_ptr, which would behave in the way that [I believe that] the 
original poster erroneously presumed that boost::shared_ptr already did. 
 I was originally planning to do some more practical testing of the 
concept before possibly submitting it as a proposal to Boost, but since 
the topic has now been raised, I will take the opportunity to get some 
feedback as to whether the idea is sound, or if I have missed something 
crucial.

The following is an extract from the draft documentation I have been 
writing.

- - - -

The class sortable_ptr is publicly derived from boost::shared_ptr, but 
defines the comparison operator  (and its siblings , =, and = ) to 
make a comparison of the actual objects.  This means that if the class 
myclass has operator defined, we can write

sortable_ptrmyclass aptr(new myclass(/* Something */));
sortable_ptrmyclass bptr(new myclass(/* Something else*/));
assert((aptr  bptr) == (*aptr  *bptr));
This is different from boost::shared_ptr, which does only define the  
operator to do a direct comparison of the pointers themselves (or 
something to that effect).

The == and != operators retain the semantics that they inherited from 
boost::shared_ptr, which is to test the pointers themselves for 
identity.  This means that even if neither aptr  bptr nor aptr  bptr 
is true for two sortable_ptr pointers, the expression aptr == bptr may 
or may not be true.  If it is true this will mean that both aptr and 
bptr refer to the same myclass object, which of course is neither less 
than or greater than itself.  If the expression aptr == bptr is false, 
it will mean that aptr and bptr point at different objects that are 
equivalent as to their sort order.

What is the advantage of this?

Before answering that question, we note that we have at leas not done 
anything illegal by introducing these definitions.  Neither 
boost::shared_ptr nor sortable_ptr support the notion of pointer 
arithmetic, so we do not need the  operator to be defined in any 
particular way for that purpose.

We can still store sortable_ptr pointers in any of the sequence 
containers in the Standard Template Library, since these only demand 
that the == operator tests for proper identity between the objects that 
are being stored in the list or deque or vector, which it does.
The sort algorithms in STL use the  operator of the objects in the 
container by default, so if we sort a container of sortable_ptr 
pointers, they will appear in the same order as the objects themselves 
would have been sorted.  For vectors and deques it can be considerably 
faster to sort pointers rather than the objects themselves, since the 
generic sort algorithms work by swapping the items in the container, and 
it can be much faster to swap two small pointers than to swap two big 
and complex objects.

For the associative containers set, multiset, map, and multimap, the 
default is to use the  operator of the objects that are stored in the 
container to define equivalence and sort order, which is exactly what we 
want.  When we store sortable_ptr pointers in an associative container, 
the pointers will be sorted in the same order as the actual objects 
would have been if we had stored them directly in a corresponding 
associative container for objects.  The definition of operator ensures 
that any two sortable_ptr pointers are equivalent if, and only if, the 
two underlying objects are equivalent, so for sets and maps, which 
cannot hold more than one object with equivalent keys, it will be the 
same objects that are represented in the container whether the objects 
are stored directly or via sortable_ptr pointers.

- - - -

The draft implementation looks like this at the moment: