1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #ifndef INCLUDED_O3TL_SORTED_VECTOR_HXX
11 #define INCLUDED_O3TL_SORTED_VECTOR_HXX
19 #include <type_traits>
24 /** the elements are totally ordered by Compare,
25 for no 2 elements !Compare(a,b) && !Compare(b,a) is true
27 template <class Compare
> struct find_unique
29 template <typename Iterator
, typename Comparable
>
30 auto operator()(Iterator first
, Iterator last
, Comparable
const& v
)
32 auto const it
= std::lower_bound(first
, last
, v
, Compare());
33 return std::make_pair(it
, (it
!= last
&& !Compare()(v
, *it
)));
37 /** Represents a sorted vector of values.
39 @tpl Value class of item to be stored in container
40 @tpl Compare comparison method
41 @tpl Find look up index of a Value in the array
45 typename Compare
= std::less
<Value
>,
46 template<typename
> class Find
= find_unique
>
50 typedef Find
<Compare
> Find_t
;
51 typedef typename
std::vector
<Value
> vector_t
;
52 typedef typename
std::vector
<Value
>::iterator iterator
;
54 typedef typename
std::vector
<Value
>::const_iterator const_iterator
;
55 typedef typename
std::vector
<Value
>::const_reverse_iterator const_reverse_iterator
;
56 typedef typename
std::vector
<Value
>::difference_type difference_type
;
57 typedef typename
std::vector
<Value
>::size_type size_type
;
58 typedef Value value_type
;
60 constexpr sorted_vector( std::initializer_list
<Value
> init
)
63 std::sort(m_vector
.begin(), m_vector
.end(), Compare());
65 sorted_vector() = default;
66 sorted_vector(sorted_vector
const&) requires
std::is_copy_constructible_v
<Value
> = default;
67 sorted_vector(sorted_vector
&&) = default;
69 sorted_vector
& operator=(sorted_vector
const&) requires
std::is_copy_constructible_v
<Value
> = default;
70 sorted_vector
& operator=(sorted_vector
&&) = default;
74 std::pair
<const_iterator
,bool> insert( Value
&& x
)
76 std::pair
<const_iterator
, bool> const ret(Find_t()(m_vector
.begin(), m_vector
.end(), x
));
79 const_iterator
const it
= m_vector
.insert(m_vector
.begin() + (ret
.first
- m_vector
.begin()), std::move(x
));
80 return std::make_pair(it
, true);
82 return std::make_pair(ret
.first
, false);
85 std::pair
<const_iterator
,bool> insert( const Value
& x
)
87 std::pair
<const_iterator
, bool> const ret(Find_t()(m_vector
.begin(), m_vector
.end(), x
));
90 const_iterator
const it
= m_vector
.insert(m_vector
.begin() + (ret
.first
- m_vector
.begin()), x
);
91 return std::make_pair(it
, true);
93 return std::make_pair(ret
.first
, false);
96 size_type
erase( const Value
& x
)
98 std::pair
<const_iterator
, bool> const ret(Find_t()(m_vector
.begin(), m_vector
.end(), x
));
101 m_vector
.erase(m_vector
.begin() + (ret
.first
- m_vector
.begin()));
107 void erase_at(size_t index
)
109 m_vector
.erase(m_vector
.begin() + index
);
112 // like C++ 2011: erase with const_iterator (doesn't change sort order)
113 const_iterator
erase(const_iterator
const& position
)
114 { // C++98 has vector::erase(iterator), so call that
115 return m_vector
.erase(m_vector
.begin() + (position
- m_vector
.begin()));
118 void erase(const_iterator
const& first
, const_iterator
const& last
)
120 m_vector
.erase(m_vector
.begin() + (first
- m_vector
.begin()),
121 m_vector
.begin() + (last
- m_vector
.begin()));
125 * make erase return the removed element, otherwise there is no useful way of extracting a std::unique_ptr
128 Value
erase_extract( size_t index
)
130 Value val
= std::move(m_vector
[index
]);
131 m_vector
.erase(m_vector
.begin() + index
);
140 void swap(sorted_vector
& other
)
142 m_vector
.swap(other
.m_vector
);
145 void reserve(size_type amount
)
147 m_vector
.reserve(amount
);
152 size_type
size() const
154 return m_vector
.size();
159 return m_vector
.empty();
162 // Only return a const iterator, so that the vector cannot be directly updated.
163 const_iterator
begin() const
165 return m_vector
.begin();
168 // Only return a const iterator, so that the vector cannot be directly updated.
169 const_iterator
end() const
171 return m_vector
.end();
174 // Only return a const iterator, so that the vector cannot be directly updated.
175 const_reverse_iterator
rbegin() const
177 return m_vector
.rbegin();
180 // Only return a const iterator, so that the vector cannot be directly updated.
181 const_reverse_iterator
rend() const
183 return m_vector
.rend();
186 const Value
& front() const
188 return m_vector
.front();
191 const Value
& back() const
193 return m_vector
.back();
196 const Value
& operator[]( size_t index
) const
198 return m_vector
.operator[]( index
);
203 template <typename Comparable
> const_iterator
lower_bound(const Comparable
& x
) const
205 return std::lower_bound( m_vector
.begin(), m_vector
.end(), x
, Compare() );
208 template <typename Comparable
> const_iterator
upper_bound(const Comparable
& x
) const
210 return std::upper_bound( m_vector
.begin(), m_vector
.end(), x
, Compare() );
213 /* Searches the container for an element with a value of x
214 * and returns an iterator to it if found, otherwise it returns an
215 * iterator to sorted_vector::end (the element past the end of the container).
217 * Only return a const iterator, so that the vector cannot be directly updated.
219 template <typename Comparable
> const_iterator
find(const Comparable
& x
) const
221 std::pair
<const_iterator
, bool> const ret(Find_t()(m_vector
.begin(), m_vector
.end(), x
));
222 return (ret
.second
) ? ret
.first
: m_vector
.end();
225 size_type
count(const Value
& v
) const
227 return find(v
) != end() ? 1 : 0;
230 bool operator==(const sorted_vector
& other
) const
232 return m_vector
== other
.m_vector
;
235 bool operator!=(const sorted_vector
& other
) const
237 return m_vector
!= other
.m_vector
;
240 void insert(const sorted_vector
& rOther
)
242 // optimization for the rather common case that we are overwriting this with the contents
243 // of another sorted vector
245 m_vector
.insert(m_vector
.begin(), rOther
.m_vector
.begin(), rOther
.m_vector
.end());
247 insert_internal( rOther
.m_vector
);
250 void insert_sorted_unique_vector(const std::vector
<Value
>& rOther
)
252 assert( std::is_sorted(rOther
.begin(), rOther
.end(), Compare()));
253 assert( std::unique(rOther
.begin(), rOther
.end(), compare_equal
) == rOther
.end());
255 m_vector
.insert(m_vector
.begin(), rOther
.m_vector
.begin(), rOther
.m_vector
.end());
257 insert_internal( rOther
);
260 void insert_sorted_unique_vector(std::vector
<Value
>&& rOther
)
262 assert( std::is_sorted(rOther
.begin(), rOther
.end(), Compare()));
263 assert( std::unique(rOther
.begin(), rOther
.end(), compare_equal
) == rOther
.end());
265 m_vector
.swap( rOther
);
267 insert_internal( rOther
);
270 /* Clear() elements in the vector, and free them one by one. */
271 void DeleteAndDestroyAll()
273 for (const_iterator it
= m_vector
.begin(); it
!= m_vector
.end(); ++it
)
281 // fdo#58793: some existing code in Writer (SwpHintsArray)
282 // routinely modifies the members of the vector in a way that
283 // violates the sort order, and then re-sorts the array.
284 // This is a kludge to enable that code to work.
285 // If you are calling this function, you are Doing It Wrong!
288 std::stable_sort(m_vector
.begin(), m_vector
.end(), Compare());
292 static bool compare_equal( const Value
& v1
, const Value
& v2
)
293 { // Synthetize == check from < check for std::unique asserts above.
294 return !Compare()( v1
, v2
) && !Compare()( v2
, v1
);
297 void insert_internal( const std::vector
<Value
>& rOther
)
299 // Do a union in one pass rather than repeated insert() that could repeatedly
300 // move large amounts of data.
302 tmp
.reserve( m_vector
.size() + rOther
.size());
303 std::set_union( m_vector
.begin(), m_vector
.end(),
304 rOther
.begin(), rOther
.end(),
305 std::back_inserter( tmp
), Compare());
306 m_vector
.swap( tmp
);
313 /** Implements an ordering function over a pointer, where the comparison uses the < operator on the pointed-to types.
314 Very useful for the cases where we put pointers to objects inside a sorted_vector.
318 template <class T1
, class T2
> bool operator()(const T1
& lhs
, const T2
& rhs
) const
320 return (*lhs
) < (*rhs
);
324 /** the elements are partially ordered by Compare,
325 2 elements are allowed if they are not the same element (pointer equal)
327 template <class Compare
> struct find_partialorder_ptrequals
329 template <typename Iterator
, typename Comparable
>
330 auto operator()(Iterator first
, Iterator last
, Comparable
const& v
)
332 auto const& [begin
, end
] = std::equal_range(first
, last
, v
, Compare());
333 for (auto it
= begin
; it
!= end
; ++it
)
337 return std::make_pair(it
, true);
340 return std::make_pair(begin
, false);
344 template <class Ref
, class Referenced
>
345 concept is_reference_to
= std::is_convertible_v
<decltype(*std::declval
<Ref
>()), Referenced
>;
350 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */