Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r50674 - in trunk: boost/filesystem libs/filesystem/test
From: bdawes_at_[hidden]
Date: 2009-01-19 13:38:29


Author: bemandawes
Date: 2009-01-19 13:38:28 EST (Mon, 19 Jan 2009)
New Revision: 50674
URL: http://svn.boost.org/trac/boost/changeset/50674

Log:
Filesystem: path operator==, !=; use string comparison rather than !(lhs < rhs) && !(rhs < lhs) because the result is the same yet the direct string compare is much more efficient than operator<, which uses lexicographical_compare.
Text files modified:
   trunk/boost/filesystem/path.hpp | 39 ++++++++++++++++++++++-----------------
   trunk/libs/filesystem/test/path_test.cpp | 15 +++++++++++++++
   2 files changed, 37 insertions(+), 17 deletions(-)

Modified: trunk/boost/filesystem/path.hpp
==============================================================================
--- trunk/boost/filesystem/path.hpp (original)
+++ trunk/boost/filesystem/path.hpp 2009-01-19 13:38:28 EST (Mon, 19 Jan 2009)
@@ -390,64 +390,69 @@
         lhs.begin(), lhs.end(), tmp.begin(), tmp.end() );
     }
 
+ // operator == uses string compare rather than !(lhs < rhs) && !(rhs < lhs) because
+ // the result is the same yet the direct string compare is much more efficient that
+ // lexicographical_compare, and lexicographical_compare used twice at that.
+
     template< class String, class Traits >
     inline bool operator==( const basic_path<String, Traits> & lhs, const basic_path<String, Traits> & rhs )
     {
- return !(lhs < rhs) && !(rhs < lhs);
+ return lhs.string() == rhs.string();
     }
 
     template< class String, class Traits >
     inline bool operator==( const typename basic_path<String, Traits>::string_type::value_type * lhs,
                     const basic_path<String, Traits> & rhs )
     {
- basic_path<String, Traits> tmp( lhs );
- return !(tmp < rhs) && !(rhs < tmp);
+ return lhs == rhs.string();
     }
 
     template< class String, class Traits >
     inline bool operator==( const typename basic_path<String, Traits>::string_type & lhs,
                     const basic_path<String, Traits> & rhs )
     {
- basic_path<String, Traits> tmp( lhs );
- return !(tmp < rhs) && !(rhs < tmp);
+ return lhs == rhs.string();
     }
 
     template< class String, class Traits >
     inline bool operator==( const basic_path<String, Traits> & lhs,
                     const typename basic_path<String, Traits>::string_type::value_type * rhs )
     {
- basic_path<String, Traits> tmp( rhs );
- return !(lhs < tmp) && !(tmp < lhs);
+ return lhs.string() == rhs;
     }
 
     template< class String, class Traits >
     inline bool operator==( const basic_path<String, Traits> & lhs,
                     const typename basic_path<String, Traits>::string_type & rhs )
     {
- basic_path<String, Traits> tmp( rhs );
- return !(lhs < tmp) && !(tmp < lhs);
+ return lhs.string() == rhs;
     }
 
     template< class String, class Traits >
- inline bool operator!=( const basic_path<String, Traits> & lhs, const basic_path<String, Traits> & rhs ) { return !(lhs == rhs); }
+ inline bool operator!=( const basic_path<String, Traits> & lhs,
+ const basic_path<String, Traits> & rhs )
+ { return !(lhs == rhs); }
     
     template< class String, class Traits >
- inline bool operator!=( const typename basic_path<String, Traits>::string_type::value_type * lhs,
- const basic_path<String, Traits> & rhs ) { return !(basic_path<String, Traits>(lhs) == rhs); }
+ inline bool operator!=( const typename basic_path<String,
+ Traits>::string_type::value_type * lhs,
+ const basic_path<String, Traits> & rhs )
+ { return !(lhs == rhs); }
 
     template< class String, class Traits >
     inline bool operator!=( const typename basic_path<String, Traits>::string_type & lhs,
- const basic_path<String, Traits> & rhs ) { return !(basic_path<String, Traits>(lhs) == rhs); }
+ const basic_path<String, Traits> & rhs )
+ { return !(lhs == rhs); }
 
     template< class String, class Traits >
     inline bool operator!=( const basic_path<String, Traits> & lhs,
- const typename basic_path<String, Traits>::string_type::value_type * rhs )
- { return !(lhs == basic_path<String, Traits>(rhs)); }
+ const typename basic_path<String, Traits>::string_type::value_type * rhs )
+ { return !(lhs == rhs); }
 
     template< class String, class Traits >
     inline bool operator!=( const basic_path<String, Traits> & lhs,
- const typename basic_path<String, Traits>::string_type & rhs )
- { return !(lhs == basic_path<String, Traits>(rhs)); }
+ const typename basic_path<String, Traits>::string_type & rhs )
+ { return !(lhs == rhs); }
 
     template< class String, class Traits >
     inline bool operator>( const basic_path<String, Traits> & lhs, const basic_path<String, Traits> & rhs ) { return rhs < lhs; }

Modified: trunk/libs/filesystem/test/path_test.cpp
==============================================================================
--- trunk/libs/filesystem/test/path_test.cpp (original)
+++ trunk/libs/filesystem/test/path_test.cpp 2009-01-19 13:38:28 EST (Mon, 19 Jan 2009)
@@ -206,9 +206,24 @@
   path p2( p1 );
   path p3;
   BOOST_CHECK( p1.string() != p3.string() );
+
+ // check each overload
+ BOOST_CHECK( p1 != p3 );
+ BOOST_CHECK( p1 != p3.string() );
+ BOOST_CHECK( p1 != p3.string().c_str() );
+ BOOST_CHECK( p1.string() != p3 );
+ BOOST_CHECK( p1.string().c_str() != p3 );
+
   p3 = p2;
   BOOST_CHECK( p1.string() == p3.string() );
 
+ // check each overload
+ BOOST_CHECK( p1 == p3 );
+ BOOST_CHECK( p1 == p3.string() );
+ BOOST_CHECK( p1 == p3.string().c_str() );
+ BOOST_CHECK( p1.string() == p3 );
+ BOOST_CHECK( p1.string().c_str() == p3 );
+
   path p4( "foobar" );
   BOOST_CHECK( p4.string() == "foobar" );
   p4 = p4; // self-assignment


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk