mirror of
https://github.com/taglib/taglib.git
synced 2025-05-27 21:20:26 -04:00
List: Add Sort Functions (#1160)
This commit is contained in:
parent
b40b834b1b
commit
1d213b8b98
@ -276,6 +276,19 @@ namespace TagLib {
|
||||
*/
|
||||
bool operator!=(const List<T> &l) const;
|
||||
|
||||
/*!
|
||||
* Sorts this list in ascending order using operator< of T.
|
||||
*/
|
||||
void sort();
|
||||
|
||||
/*!
|
||||
* Sorts this list in ascending order using the comparison
|
||||
* function object \a comp which returns true if the first argument is
|
||||
* less than the second.
|
||||
*/
|
||||
template<class Compare>
|
||||
void sort(Compare&& comp);
|
||||
|
||||
protected:
|
||||
/*
|
||||
* If this List is being shared via implicit sharing, do a deep copy of the
|
||||
|
@ -330,6 +330,21 @@ bool List<T>::operator!=(const List<T> &l) const
|
||||
return d->list != l.d->list;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void List<T>::sort()
|
||||
{
|
||||
detach();
|
||||
d->list.sort();
|
||||
}
|
||||
|
||||
template <class T>
|
||||
template <class Compare>
|
||||
void List<T>::sort(Compare&& comp)
|
||||
{
|
||||
detach();
|
||||
d->list.sort(std::forward<Compare>(comp));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// protected members
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -35,6 +35,7 @@ class TestList : public CppUnit::TestFixture
|
||||
CPPUNIT_TEST(testAppend);
|
||||
CPPUNIT_TEST(testDetach);
|
||||
CPPUNIT_TEST(bracedInit);
|
||||
CPPUNIT_TEST(testSort);
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
|
||||
public:
|
||||
@ -122,6 +123,29 @@ public:
|
||||
CPPUNIT_ASSERT_EQUAL(6, *l4[2]);
|
||||
}
|
||||
|
||||
void testSort()
|
||||
{
|
||||
List<int> list1 {
|
||||
3,
|
||||
2,
|
||||
1
|
||||
};
|
||||
list1.sort();
|
||||
CPPUNIT_ASSERT_EQUAL(list1[0], 1);
|
||||
CPPUNIT_ASSERT_EQUAL(list1[1], 2);
|
||||
CPPUNIT_ASSERT_EQUAL(list1[2], 3);
|
||||
|
||||
List<int> list2 {
|
||||
1,
|
||||
2,
|
||||
3
|
||||
};
|
||||
list2.sort([](const auto &a, const auto &b) { return a > b; });
|
||||
CPPUNIT_ASSERT_EQUAL(list2[0], 3);
|
||||
CPPUNIT_ASSERT_EQUAL(list2[1], 2);
|
||||
CPPUNIT_ASSERT_EQUAL(list2[2], 1);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
CPPUNIT_TEST_SUITE_REGISTRATION(TestList);
|
||||
|
Loading…
Reference in New Issue
Block a user