forked from boostorg/range
trac 9515 - fix for uniqued adaptor consistent with unique algorithm.
This commit is contained in:
@ -62,54 +62,44 @@ namespace boost
|
|||||||
, pred_t(pred)
|
, pred_t(pred)
|
||||||
, m_last(last)
|
, m_last(last)
|
||||||
{
|
{
|
||||||
move_to_next_valid();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class OtherIter>
|
template<class OtherIter>
|
||||||
skip_iterator( const skip_iterator<OtherIter, pred_t, default_pass>& other )
|
skip_iterator( const skip_iterator<OtherIter, pred_t, default_pass>& other )
|
||||||
: base_t(other.base())
|
: base_t(other.base())
|
||||||
, pred_t(other)
|
, pred_t(other)
|
||||||
, m_last(other.m_last) {}
|
, m_last(other.m_last)
|
||||||
|
|
||||||
void move_to_next_valid()
|
|
||||||
{
|
{
|
||||||
iter_t& it = this->base_reference();
|
|
||||||
pred_t& bi_pred = *this;
|
|
||||||
if (it != m_last)
|
|
||||||
{
|
|
||||||
if (default_pass)
|
|
||||||
{
|
|
||||||
iter_t nxt = ::boost::next(it);
|
|
||||||
while (nxt != m_last && !bi_pred(*it, *nxt))
|
|
||||||
{
|
|
||||||
++it;
|
|
||||||
++nxt;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
iter_t nxt = ::boost::next(it);
|
|
||||||
for(; nxt != m_last; ++it, ++nxt)
|
|
||||||
{
|
|
||||||
if (bi_pred(*it, *nxt))
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (nxt == m_last)
|
|
||||||
{
|
|
||||||
it = m_last;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void increment()
|
void increment()
|
||||||
{
|
{
|
||||||
iter_t& it = this->base_reference();
|
iter_t& it = this->base_reference();
|
||||||
BOOST_ASSERT( it != m_last );
|
BOOST_ASSERT( it != m_last );
|
||||||
|
pred_t& bi_pred = *this;
|
||||||
|
iter_t prev = it;
|
||||||
++it;
|
++it;
|
||||||
move_to_next_valid();
|
if (it != m_last)
|
||||||
|
{
|
||||||
|
if (default_pass)
|
||||||
|
{
|
||||||
|
while (it != m_last && !bi_pred(*prev, *it))
|
||||||
|
{
|
||||||
|
++it;
|
||||||
|
++prev;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (; it != m_last; ++it, ++prev)
|
||||||
|
{
|
||||||
|
if (bi_pred(*prev, *it))
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
iter_t m_last;
|
iter_t m_last;
|
||||||
|
@ -9,6 +9,10 @@
|
|||||||
// For more information, see http://www.boost.org/libs/range/
|
// For more information, see http://www.boost.org/libs/range/
|
||||||
//
|
//
|
||||||
#include <boost/range/adaptor/uniqued.hpp>
|
#include <boost/range/adaptor/uniqued.hpp>
|
||||||
|
#include <boost/range/adaptor/transformed.hpp>
|
||||||
|
#include <boost/range/algorithm/unique_copy.hpp>
|
||||||
|
#include <boost/range/algorithm_ext/push_back.hpp>
|
||||||
|
#include <boost/algorithm/string/predicate.hpp>
|
||||||
|
|
||||||
#include <boost/test/test_tools.hpp>
|
#include <boost/test/test_tools.hpp>
|
||||||
#include <boost/test/unit_test.hpp>
|
#include <boost/test/unit_test.hpp>
|
||||||
@ -74,9 +78,82 @@ namespace boost
|
|||||||
uniqued_test_impl< std::set< int > >();
|
uniqued_test_impl< std::set< int > >();
|
||||||
uniqued_test_impl< std::multiset< int > >();
|
uniqued_test_impl< std::multiset< int > >();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class istring
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
istring()
|
||||||
|
: m_value("")
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
explicit istring(const char* value)
|
||||||
|
: m_value(value)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool operator==(istring r) const
|
||||||
|
{
|
||||||
|
return boost::iequals(m_value, r.m_value);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator!=(istring r) const
|
||||||
|
{
|
||||||
|
return !operator==(r);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline friend std::ostream& operator<<(std::ostream& out, istring o)
|
||||||
|
{
|
||||||
|
return out << o.m_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* get() const { return m_value; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
const char* m_value;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct istring_to_string
|
||||||
|
{
|
||||||
|
typedef std::string result_type;
|
||||||
|
|
||||||
|
std::string operator()(istring s) const
|
||||||
|
{
|
||||||
|
return s.get();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// This is based on a test-case provided by Eric Neibler.
|
||||||
|
void uniqued_return_first()
|
||||||
|
{
|
||||||
|
using namespace boost::adaptors;
|
||||||
|
|
||||||
|
std::vector<istring> strs;
|
||||||
|
strs.push_back(istring("hello"));
|
||||||
|
strs.push_back(istring("hElLo"));
|
||||||
|
strs.push_back(istring("HELLO"));
|
||||||
|
strs.push_back(istring("ZZZZ"));
|
||||||
|
|
||||||
|
std::vector<istring> output1;
|
||||||
|
|
||||||
|
boost::unique_copy(strs, std::back_inserter(output1));
|
||||||
|
|
||||||
|
std::vector<istring> output2;
|
||||||
|
boost::push_back(output2, strs | uniqued);
|
||||||
|
|
||||||
|
std::vector<std::string> test1;
|
||||||
|
boost::push_back(test1, output1 | transformed(istring_to_string()));
|
||||||
|
|
||||||
|
std::vector<std::string> test2;
|
||||||
|
boost::push_back(test2, output2 | transformed(istring_to_string()));
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL_COLLECTIONS(test1.begin(), test1.end(),
|
||||||
|
test2.begin(), test2.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
} // anonymous namespace
|
||||||
|
} // namespace boost
|
||||||
|
|
||||||
boost::unit_test::test_suite*
|
boost::unit_test::test_suite*
|
||||||
init_unit_test_suite(int argc, char* argv[])
|
init_unit_test_suite(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
@ -85,5 +162,7 @@ init_unit_test_suite(int argc, char* argv[])
|
|||||||
|
|
||||||
test->add( BOOST_TEST_CASE( &boost::uniqued_test ) );
|
test->add( BOOST_TEST_CASE( &boost::uniqued_test ) );
|
||||||
|
|
||||||
|
test->add(BOOST_TEST_CASE(&boost::uniqued_return_first));
|
||||||
|
|
||||||
return test;
|
return test;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user