/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/** the elements are totally ordered by Compare, for no 2 elements !Compare(a,b) && !Compare(b,a) is true
*/ template <class Compare> struct find_unique
{ template <typename Iterator, typename Comparable> autooperator()(Iterator first, Iterator last, Comparable const& v)
{ autoconst it = std::lower_bound(first, last, v, Compare()); return std::make_pair(it, (it != last && !Compare()(v, *it)));
}
};
/** Represents a sorted vector of values.
@tpl Value class of item to be stored in container @tpl Compare comparison method @tpl Find look up index of a Value in the array
*/ template< typename Value, typename Compare = std::less<Value>, template<typename> class Find = find_unique > class sorted_vector
{ private: typedef Find<Compare> Find_t; typedeftypename std::vector<Value> vector_t; typedeftypename std::vector<Value>::iterator iterator; public: typedeftypename std::vector<Value>::const_iterator const_iterator; typedeftypename std::vector<Value>::const_reverse_iterator const_reverse_iterator; typedeftypename std::vector<Value>::difference_type difference_type; typedeftypename std::vector<Value>::size_type size_type; typedef Value value_type;
// like C++ 2011: erase with const_iterator (doesn't change sort order)
const_iterator erase(const_iterator const& position)
{ // C++98 has vector::erase(iterator), so call that return m_vector.erase(m_vector.begin() + (position - m_vector.begin()));
}
/** * make erase return the removed element, otherwise there is no useful way of extracting a std::unique_ptr * from this.
*/
Value erase_extract( size_t index )
{
Value val = std::move(m_vector[index]);
m_vector.erase(m_vector.begin() + index); return val;
}
/* Searches the container for an element with a value of x * and returns an iterator to it if found, otherwise it returns an * iterator to sorted_vector::end (the element past the end of the container). * * Only return a const iterator, so that the vector cannot be directly updated.
*/ template <typename Comparable> const_iterator find(const Comparable& x) const
{
std::pair<const_iterator, bool> const ret(Find_t()(m_vector.begin(), m_vector.end(), x)); return (ret.second) ? ret.first : m_vector.end();
}
void insert(const sorted_vector& rOther)
{ // optimization for the rather common case that we are overwriting this with the contents // of another sorted vector if ( empty() )
m_vector.insert(m_vector.begin(), rOther.m_vector.begin(), rOther.m_vector.end()); else
insert_internal( rOther.m_vector );
}
/* Clear() elements in the vector, and free them one by one. */ void DeleteAndDestroyAll()
{ for (const_iterator it = m_vector.begin(); it != m_vector.end(); ++it)
{ delete *it;
}
clear();
}
// fdo#58793: some existing code in Writer (SwpHintsArray) // routinely modifies the members of the vector in a way that // violates the sort order, and then re-sorts the array. // This is a kludge to enable that code to work. // If you are calling this function, you are Doing It Wrong! void Resort()
{
std::stable_sort(m_vector.begin(), m_vector.end(), Compare());
}
void insert_internal( const std::vector<Value>& rOther )
{ // Do a union in one pass rather than repeated insert() that could repeatedly // move large amounts of data.
vector_t tmp;
tmp.reserve( m_vector.size() + rOther.size());
std::set_union( m_vector.begin(), m_vector.end(),
rOther.begin(), rOther.end(),
std::back_inserter( tmp ), Compare());
m_vector.swap( tmp );
}
vector_t m_vector;
};
/** Implements an ordering function over a pointer, where the comparison uses the < operator on the pointed-to types. Very useful for the cases where we put pointers to objects inside a sorted_vector.
*/ struct less_ptr_to
{ template <class T1, class T2> booloperator()(const T1& lhs, const T2& rhs) const
{ return (*lhs) < (*rhs);
}
};
/** the elements are partially ordered by Compare, 2 elements are allowed if they are not the same element (pointer equal)
*/ template <class Compare> struct find_partialorder_ptrequals
{ template <typename Iterator, typename Comparable> autooperator()(Iterator first, Iterator last, Comparable const& v)
{ autoconst& [begin, end] = std::equal_range(first, last, v, Compare()); for (auto it = begin; it != end; ++it)
{ if (&*v == &**it)
{ return std::make_pair(it, true);
}
} return std::make_pair(begin, false);
}
};
template <class Ref, class Referenced>
concept is_reference_to = std::is_convertible_v<decltype(*std::declval<Ref>()), Referenced>;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.