Doxygen style.

This commit is contained in:
Mathew Sutcliffe
2016-07-02 01:40:00 +01:00
parent 8d5020d78e
commit e20c8bbcbd
6 changed files with 292 additions and 572 deletions

View File

@@ -26,7 +26,7 @@ namespace BlackMisc
{
/*!
* \brief Generic type-erased sequential container with value semantics.
* Generic type-erased sequential container with value semantics.
* \tparam T the type of elements contained.
*
* Can take any suitable container class as its implementation at runtime.
@@ -37,7 +37,7 @@ namespace BlackMisc
public Mixin::Icon<CSequence<T>>
{
public:
//! \brief STL compatibility
//! STL compatibility
//! @{
typedef T key_type;
typedef T value_type;
@@ -51,307 +51,201 @@ namespace BlackMisc
typedef intptr_t size_type;
//! @}
/*!
* \brief Default constructor.
*/
//! Default constructor.
CSequence() : m_pimpl(new Pimpl<QList<T>>(QList<T>())) {}
/*!
* \brief Initializer list constructor.
*/
//! Initializer list constructor.
CSequence(std::initializer_list<T> il) : m_pimpl(new Pimpl<QList<T>>(QList<T>(il))) {}
/*!
* \brief By QList of type T.
*/
//! By QList of type T.
CSequence(const QList<T> &list) : m_pimpl(new Pimpl<QList<T>>(QList<T>(list))) {}
/*!
* \brief Copy constructor.
*/
//! Copy constructor.
CSequence(const CSequence &other) : m_pimpl(other.pimpl() ? other.pimpl()->clone() : nullptr) {}
/*!
* \brief Move constructor.
*/
//! Move constructor.
CSequence(CSequence &&other) noexcept(std::is_nothrow_move_constructible<T>::value) : m_pimpl(other.m_pimpl.take()) {}
/*!
* \brief Copy assignment.
*/
//! Copy assignment.
CSequence &operator =(const CSequence &other) { m_pimpl.reset(other.pimpl() ? other.pimpl()->clone() : nullptr); return *this; }
/*!
* \brief Move assignment.
*/
//! Move assignment.
CSequence &operator =(CSequence && other) noexcept(std::is_nothrow_move_assignable<T>::value) { m_pimpl.reset(other.m_pimpl.take()); return *this; }
/*!
* \brief Create a new sequence with a specific implementation type.
* \tparam C Becomes the sequence's implementation type.
* \param c Initial value for the sequence; default is empty, but it could contain elements if desired. The value is copied.
*/
//! Create a new sequence with a specific implementation type.
//! \tparam C Becomes the sequence's implementation type.
//! \param c Initial value for the sequence; default is empty, but it could contain elements if desired. The value is copied.
template <class C> static CSequence fromImpl(C c = C()) { return CSequence(new Pimpl<C>(std::move(c))); }
/*!
* \brief Change the implementation type but keep all the same elements, by moving them into the new implementation.
* \tparam C Becomes the sequence's new implementation type.
*/
//! Change the implementation type but keep all the same elements, by moving them into the new implementation.
//! \tparam C Becomes the sequence's new implementation type.
template <class C> void changeImpl(C = C()) { auto c = fromImpl(C()); std::move(begin(), end(), std::inserter(c, c.begin())); *this = std::move(c); }
/*!
* \brief Like changeImpl, but uses the implementation type of another sequence.
* \pre The other sequence must be initialized.
*/
//! Like changeImpl, but uses the implementation type of another sequence.
//! \pre The other sequence must be initialized.
void useImplOf(const CSequence &other) { CSequence c(other.pimpl()->cloneEmpty()); std::move(begin(), end(), std::inserter(c, c.begin())); *this = std::move(c); }
/*!
* \brief Returns iterator at the beginning of the sequence.
*/
//! Returns iterator at the beginning of the sequence.
iterator begin() { return pimpl() ? pimpl()->begin() : iterator(); }
/*!
* \brief Returns const iterator at the beginning of the sequence.
*/
//! Returns const iterator at the beginning of the sequence.
const_iterator begin() const { return pimpl() ? pimpl()->begin() : const_iterator(); }
/*!
* \brief Returns const iterator at the beginning of the sequence.
*/
//! Returns const iterator at the beginning of the sequence.
const_iterator cbegin() const { return pimpl() ? pimpl()->cbegin() : const_iterator(); }
/*!
* \brief Returns iterator one past the end of the sequence.
*/
//! Returns iterator one past the end of the sequence.
iterator end() { return pimpl() ? pimpl()->end() : iterator(); }
/*!
* \brief Returns const iterator one past the end of the sequence.
*/
//! Returns const iterator one past the end of the sequence.
const_iterator end() const { return pimpl() ? pimpl()->end() : const_iterator(); }
/*!
* \brief Returns const iterator one past the end of the sequence.
*/
//! Returns const iterator one past the end of the sequence.
const_iterator cend() const { return pimpl() ? pimpl()->cend() : const_iterator(); }
/*!
* \brief Swap this sequence with another.
*/
//! Swap this sequence with another.
void swap(CSequence &other) noexcept { m_pimpl.swap(other.m_pimpl); }
/*!
* \brief Access an element by its index.
* \pre The sequence must be initialized and the index in bounds.
*/
//! Access an element by its index.
//! \pre The sequence must be initialized and the index in bounds.
reference operator [](size_type index) { Q_ASSERT(pimpl()); return pimpl()->operator [](index); }
/*!
* \brief Access an element by its index.
* \pre The sequence must be initialized and the index in bounds.
*/
//! Access an element by its index.
//! \pre The sequence must be initialized and the index in bounds.
const_reference operator [](size_type index) const { Q_ASSERT(pimpl()); return pimpl()->operator [](index); }
/*!
* \brief Access the first element.
* \pre The sequence must not be empty.
*/
//! Access the first element.
//! \pre The sequence must not be empty.
reference front() { Q_ASSERT(!empty()); return pimpl()->front(); }
/*!
* \brief Access the first element.
* \pre The sequence must not be empty.
*/
//! Access the first element.
//! \pre The sequence must not be empty.
const_reference front() const { Q_ASSERT(!empty()); return pimpl()->front(); }
/*!
* \brief Access the first element, or a default-initialized value if the sequence is empty.
*/
//! Access the first element, or a default-initialized value if the sequence is empty.
const_reference frontOrDefault() const { static const value_type def {}; return empty() ? def : front(); }
/*!
* \brief Access the first element, or a default-initialized value if the sequence is empty.
*/
//! Access the first element, or a default-initialized value if the sequence is empty.
value_type frontOrDefault(value_type def) const { return empty() ? def : front(); }
/*!
* \brief Access the last element.
* \pre The sequence must not be empty.
*/
//! Access the last element.
//! \pre The sequence must not be empty.
reference back() { Q_ASSERT(!empty()); return pimpl()->back(); }
/*!
* \brief Access the last element.
* \pre The sequence must not be empty.
*/
//! Access the last element.
//! \pre The sequence must not be empty.
const_reference back() const { Q_ASSERT(!empty()); return pimpl()->back(); }
/*!
* \brief Access the last element, or a default value if the sequence is empty.
*/
//! Access the last element, or a default value if the sequence is empty.
const_reference backOrDefault() const { static const value_type def {}; return empty() ? def : back(); }
/*!
* \brief Access the last element, or a default value if the sequence is empty.
*/
//! Access the last element, or a default value if the sequence is empty.
value_type backOrDefault(value_type def) const { return empty() ? def : back(); }
/*!
* \brief Returns number of elements in the sequence.
*/
//! Returns number of elements in the sequence.
size_type size() const { return pimpl() ? pimpl()->size() : 0; }
/*!
* \brief Returns true if the sequence is empty.
*/
//! Returns true if the sequence is empty.
bool empty() const { return pimpl() ? pimpl()->empty() : true; }
/*!
* \brief Synonym for empty.
*/
//! Synonym for empty.
bool isEmpty() const { return empty(); }
/*!
* \brief Removes all elements in the sequence.
*/
//! Removes all elements in the sequence.
void clear() { if (pimpl()) pimpl()->clear(); }
/*!
* \brief Changes the size of the sequence, if it is bigger than the given size.
*/
//! Changes the size of the sequence, if it is bigger than the given size.
void truncate(size_type maxSize) { if (size() > maxSize) { erase(begin() + maxSize, end()); } }
/*!
* \brief Inserts an element into the sequence.
* \return An iterator to the position where value was inserted.
* \pre The sequence must be initialized.
*/
//! Inserts an element into the sequence.
//! \return An iterator to the position where value was inserted.
//! \pre The sequence must be initialized.
iterator insert(iterator before, const T &value) { Q_ASSERT(pimpl()); return pimpl()->insert(before, value); }
/*!
* \brief Moves an element into the sequence.
* \return An iterator to the position where value was inserted.
* \pre The sequence must be initialized.
*/
//! Moves an element into the sequence.
//! \return An iterator to the position where value was inserted.
//! \pre The sequence must be initialized.
iterator insert(iterator before, T &&value) { Q_ASSERT(pimpl()); return pimpl()->insert(before, std::move(value)); }
/*!
* \brief Appends an element at the end of the sequence.
* \pre The sequence must be initialized.
*/
//! Appends an element at the end of the sequence.
//! \pre The sequence must be initialized.
void push_back(const T &value) { Q_ASSERT(pimpl()); pimpl()->push_back(value); }
/*!
* \brief Insert as first element.
* \pre The sequence must be initialized.
*/
//! Insert as first element.
//! \pre The sequence must be initialized.
void push_front(const T &value) { insert(begin(), value); }
/*!
* \brief Move-appends an element at the end of the sequence.
* \pre The sequence must be initialized.
*/
//! Move-appends an element at the end of the sequence.
//! \pre The sequence must be initialized.
void push_back(T &&value) { Q_ASSERT(pimpl()); pimpl()->push_back(std::move(value)); }
/*!
* \brief Appends all elements from another sequence at the end of this sequence.
* \pre This sequence must be initialized.
*/
//! Appends all elements from another sequence at the end of this sequence.
//! \pre This sequence must be initialized.
void push_back(const CSequence &other) { std::copy(other.begin(), other.end(), std::back_inserter(*this)); }
/*!
* \brief Appends all elements from another sequence at the end of this sequence.
* This version moves elements instead of copying.
* \pre This sequence must be initialized.
*/
//! Appends all elements from another sequence at the end of this sequence.
//! This version moves elements instead of copying.
//! \pre This sequence must be initialized.
void push_back(CSequence &&other) { std::move(other.begin(), other.end(), std::back_inserter(*this)); }
/*!
* \brief Appends all elements from a range at the end of this sequence.
* \pre This sequence must be initialized.
*/
//! Appends all elements from a range at the end of this sequence.
//! \pre This sequence must be initialized.
template <typename I>
void push_back(const CRange<I> &range) { std::copy(range.begin(), range.end(), std::back_inserter(*this)); }
/*!
* \brief Synonym for push_back.
* \pre The sequence must be initialized.
*/
//! Synonym for push_back.
//! \pre The sequence must be initialized.
void insert(const T &value) { push_back(value); }
/*!
* \brief Synonym for push_back.
* \pre The sequence must be initialized.
*/
//! Synonym for push_back.
//! \pre The sequence must be initialized.
void insert(T &&value) { push_back(std::move(value)); }
/*!
* \brief Synonym for push_back.
* \pre The sequence must be initialized.
*/
//! Synonym for push_back.
//! \pre The sequence must be initialized.
void insert(const CSequence &other) { push_back(other); }
/*!
* \brief Synonym for push_back.
* \pre The sequence must be initialized.
*/
//! Synonym for push_back.
//! \pre The sequence must be initialized.
void insert(CSequence &&other) { push_back(std::move(other)); }
/*!
* \brief Synonym for push_back.
* \pre This sequence must be initialized.
*/
//! Synonym for push_back.
//! \pre This sequence must be initialized.
template <typename I>
void insert(const CRange<I> &range) { std::copy(range.begin(), range.end(), std::back_inserter(*this)); }
/*!
* \brief Concatenates two sequences and returns the result.
* \pre This sequence must be initialized.
*/
//! Concatenates two sequences and returns the result.
//! \pre This sequence must be initialized.
CSequence join(const CSequence &other) const { CSequence copy(*this); copy.push_back(other); return copy; }
/*!
* \brief Concatenates a sequence and a range and returns the result.
* \pre This sequence must be initialized.
*/
//! Concatenates a sequence and a range and returns the result.
//! \pre This sequence must be initialized.
template <typename I>
CSequence join(const CRange<I> &range) const { CSequence copy(*this); copy.push_back(range); return copy; }
/*!
* \brief Removes an element at the end of the sequence.
* \pre The sequence must contain at least one element.
*/
//! Removes an element at the end of the sequence.
//! \pre The sequence must contain at least one element.
void pop_back() { Q_ASSERT(!empty()); pimpl()->pop_back(); }
/*!
* \brief Remove the element pointed to by the given iterator.
* \return An iterator to the position of the next element after the one removed.
* \pre The sequence must be initialized.
*/
//! Remove the element pointed to by the given iterator.
//! \return An iterator to the position of the next element after the one removed.
//! \pre The sequence must be initialized.
iterator erase(iterator pos) { Q_ASSERT(pimpl()); return pimpl()->erase(pos); }
/*!
* \brief Remove the range of elements between two iterators.
* \return An iterator to the position of the next element after the one removed.
* \pre The sequence must be initialized.
*/
//! Remove the range of elements between two iterators.
//! \return An iterator to the position of the next element after the one removed.
//! \pre The sequence must be initialized.
iterator erase(iterator it1, iterator it2) { Q_ASSERT(pimpl()); return pimpl()->erase(it1, it2); }
/*!
* \brief Return an iterator to the first element equal to the given object, or the end iterator if not found. O(n).
* \warning Take care that the returned non-const iterator is not compared with a const iterator.
*/
//! Return an iterator to the first element equal to the given object, or the end iterator if not found. O(n).
//! \warning Take care that the returned non-const iterator is not compared with a const iterator.
iterator find(const T &object) { return std::find(begin(), end(), object); }
/*!
* \brief Return an iterator to the first element equal to the given object, or the end iterator if not found. O(n).
*/
//! Return an iterator to the first element equal to the given object, or the end iterator if not found. O(n).
const_iterator find(const T &object) const { return std::find(cbegin(), cend(), object); }
/*!
* \brief Modify by applying a value map to each element for which a given predicate returns true.
* \return The number of elements modified.
*/
//! Modify by applying a value map to each element for which a given predicate returns true.
//! \return The number of elements modified.
template <class Predicate, class VariantMap>
int applyIf(Predicate p, const VariantMap &newValues, bool skipEqualValues = false)
{
@@ -363,25 +257,21 @@ namespace BlackMisc
return count;
}
/*!
* \brief Modify by applying a value map to each element matching a particular key/value pair.
* \param key1 A pointer to a member function of T.
* \param value1 Will be compared to the return value of key1.
* \param newValues Values from this map will be put into each matching element.
* \param skipEqualValues Equal values will not be updated
* \return The number of elements modified.
*/
//! Modify by applying a value map to each element matching a particular key/value pair.
//! \param key1 A pointer to a member function of T.
//! \param value1 Will be compared to the return value of key1.
//! \param newValues Values from this map will be put into each matching element.
//! \param skipEqualValues Equal values will not be updated
//! \return The number of elements modified.
template <class K1, class V1, class VariantMap>
int applyIf(K1 key1, V1 value1, const VariantMap &newValues, bool skipEqualValues = false)
{
return applyIf(BlackMisc::Predicates::MemberEqual(key1, value1), newValues, skipEqualValues);
}
/*!
* \brief Remove all elements equal to the given object, if it is contained.
* \pre The sequence must be initialized.
* \return The number of elements removed.
*/
//! Remove all elements equal to the given object, if it is contained.
//! \pre The sequence must be initialized.
//! \return The number of elements removed.
int remove(const T &object)
{
const auto newEnd = std::remove(begin(), end(), object);
@@ -390,11 +280,9 @@ namespace BlackMisc
return count;
}
/*!
* \brief Remove elements for which a given predicate returns true.
* \pre The sequence must be initialized.
* \return The number of elements removed.
*/
//! Remove elements for which a given predicate returns true.
//! \pre The sequence must be initialized.
//! \return The number of elements removed.
template <class Predicate>
int removeIf(Predicate p)
{
@@ -412,20 +300,16 @@ namespace BlackMisc
return CSequence::CContainerBase::removeIf(k0, v0, keysValues...);
}
/*!
* \brief Remove all elements if they are in other
* \pre The sequence must be initialized.
* \return The number of elements removed.
*/
//! Remove all elements if they are in other
//! \pre The sequence must be initialized.
//! \return The number of elements removed.
int removeIfIn(const CSequence &other)
{
return removeIf([&other](const T &v) { return other.contains(v); });
}
/*!
* \brief Replace elements matching the given element with a replacement.
* \return The number of elements replaced.
*/
//! Replace elements matching the given element with a replacement.
//! \return The number of elements replaced.
int replace(const T &original, const T &replacement)
{
int count = 0;
@@ -436,10 +320,8 @@ namespace BlackMisc
return count;
}
/*!
* \brief Replace elements for which a given predicate returns true.
* \return The number of elements replaced.
*/
//! Replace elements for which a given predicate returns true.
//! \return The number of elements replaced.
template <class Predicate>
int replaceIf(Predicate p, const T &replacement)
{
@@ -451,23 +333,19 @@ namespace BlackMisc
return count;
}
/*!
* \brief Replace elements matching a particular key/value pair.
* \param key1 A pointer to a member function of T.
* \param value1 Will be compared to the return value of key1.
* \param replacement All matching elements will be replaced by copies of this one.
* \return The number of elements replaced.
*/
//! Replace elements matching a particular key/value pair.
//! \param key1 A pointer to a member function of T.
//! \param value1 Will be compared to the return value of key1.
//! \param replacement All matching elements will be replaced by copies of this one.
//! \return The number of elements replaced.
template <class K1, class V1>
int replaceIf(K1 key1, V1 value1, const T &replacement)
{
return replaceIf(BlackMisc::Predicates::MemberEqual(key1, value1), replacement);
}
/*!
* \brief Replace elements for which a given predicate returns true. If there is no match, push the new element on the end.
* \pre The sequence must be initialized.
*/
//! Replace elements for which a given predicate returns true. If there is no match, push the new element on the end.
//! \pre The sequence must be initialized.
template <class Predicate>
void replaceOrAdd(Predicate p, const T &replacement)
{
@@ -475,23 +353,19 @@ namespace BlackMisc
else { push_back(replacement); }
}
/*!
* \brief Replace elements matching the given element. If there is no match, push the new element on the end.
* \pre The sequence must be initialized.
*/
//! Replace elements matching the given element. If there is no match, push the new element on the end.
//! \pre The sequence must be initialized.
void replaceOrAdd(const T &original, const T &replacement)
{
if (this->contains(original)) { replace(original, replacement); }
else { push_back(replacement); }
}
/*!
* \brief Replace elements matching a particular key/value pair. If there is no match, push the new element on the end.
* \param key1 A pointer to a member function of T.
* \param value1 Will be compared to the return value of key1.
* \param replacement All matching elements will be replaced by copies of this one, or a copy will be added.
* \pre The sequence must be initialized.
*/
//! Replace elements matching a particular key/value pair. If there is no match, push the new element on the end.
//! \param key1 A pointer to a member function of T.
//! \param value1 Will be compared to the return value of key1.
//! \param replacement All matching elements will be replaced by copies of this one, or a copy will be added.
//! \pre The sequence must be initialized.
template <class K1, class V1>
void replaceOrAdd(K1 key1, V1 value1, const T &replacement)
{
@@ -499,27 +373,21 @@ namespace BlackMisc
else { push_back(replacement); }
}
/*!
* \brief In-place sort by a given comparator predicate.
*/
//! In-place sort by a given comparator predicate.
template <class Predicate> void sort(Predicate p)
{
std::sort(begin(), end(), p);
}
/*!
* \brief In-place sort by some particular key(s).
* \param key1 A pointer to a member function of T.
* \param keys Zero or more additional pointers to member functions of T.
*/
//! In-place sort by some particular key(s).
//! \param key1 A pointer to a member function of T.
//! \param keys Zero or more additional pointers to member functions of T.
template <class K1, class... Keys> void sortBy(K1 key1, Keys... keys)
{
sort(BlackMisc::Predicates::MemberLess(key1, keys...));
}
/*!
* \brief Return a copy sorted by a given comparator predicate.
*/
//! Return a copy sorted by a given comparator predicate.
template <class Predicate>
CSequence sorted(Predicate p) const
{
@@ -528,39 +396,31 @@ namespace BlackMisc
return result;
}
/*!
* \brief Return a copy sorted by some particular key(s).
* \param key1 A pointer to a member function of T.
* \param keys Zero or more additional pointers to member functions of T.
*/
//! Return a copy sorted by some particular key(s).
//! \param key1 A pointer to a member function of T.
//! \param keys Zero or more additional pointers to member functions of T.
template <class K1, class... Keys>
CSequence sortedBy(K1 key1, Keys... keys) const
{
return sorted(BlackMisc::Predicates::MemberLess(key1, keys...));
}
/*!
* \brief In-place move the smallest n elements to the beginning and sort them.
*/
//! In-place move the smallest n elements to the beginning and sort them.
template <class Predicate> void partiallySort(size_type n, Predicate p)
{
std::partial_sort(begin(), begin() + n, end(), p);
}
/*!
* \brief In-place partially sort by some particular key(s).
* \param n size.
* \param key1 A pointer to a member function of T.
* \param keys Zero or more additional pointers to member functions of T.
*/
//! In-place partially sort by some particular key(s).
//! \param n size.
//! \param key1 A pointer to a member function of T.
//! \param keys Zero or more additional pointers to member functions of T.
template <class K1, class... Keys> void partiallySortBy(size_type n, K1 key1, Keys... keys)
{
partiallySort(n, BlackMisc::Predicates::MemberLess(key1, keys...));
}
/*!
* \brief Return a copy with the smallest n elements at the beginning and sorted.
*/
//! Return a copy with the smallest n elements at the beginning and sorted.
template <class Predicate>
CSequence partiallySorted(size_type n, Predicate p) const
{
@@ -569,21 +429,17 @@ namespace BlackMisc
return result;
}
/*!
* \brief Return a copy partially sorted by some particular key(s).
* \param n size
* \param key1 A pointer to a member function of T.
* \param keys Zero or more additional pointers to member functions of T.
*/
//! Return a copy partially sorted by some particular key(s).
//! \param n size
//! \param key1 A pointer to a member function of T.
//! \param keys Zero or more additional pointers to member functions of T.
template <class K1, class... Keys>
CSequence partiallySortedBy(size_type n, K1 key1, Keys... keys) const
{
return partiallySorted(n, BlackMisc::Predicates::MemberLess(key1, keys...));
}
/*!
* Split up the sequence into subsequences for which the given predicate returns the same value.
*/
//! Split up the sequence into subsequences for which the given predicate returns the same value.
template <class Predicate>
auto separate(Predicate p) const -> QMap<decltype(p(std::declval<T>())), CSequence>
{
@@ -599,9 +455,7 @@ namespace BlackMisc
return result;
}
/*!
* Split up the sequence into subsequences of elements having the same value for the given key.
*/
//! Split up the sequence into subsequences of elements having the same value for the given key.
template <class Key>
auto separateBy(Key k) const -> QMap<decltype(std::declval<T>().*k), CSequence>
{
@@ -633,11 +487,9 @@ namespace BlackMisc
//! Greater or equal operator.
friend bool operator >=(const CSequence &a, const CSequence &b) { return !(a < b); }
/*!
* \brief Return an opaque pointer to the implementation container.
* \details Can be useful in unusual debugging situations.
* \warning Not for general use.
*/
//! Return an opaque pointer to the implementation container.
//! \details Can be useful in unusual debugging situations.
//! \warning Not for general use.
void *getImpl() { return pimpl() ? pimpl()->impl() : nullptr; }
private: