1 /*
2 This file is part of the kcalcore library.
3
4 SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8
9 #ifndef KCALCORE_RECURRENCEHELPER_P_H
10 #define KCALCORE_RECURRENCEHELPER_P_H
11
12 #include <algorithm>
13
14 namespace KCalendarCore
15 {
16 template<typename T>
sortAndRemoveDuplicates(T & container)17 inline void sortAndRemoveDuplicates(T &container)
18 {
19 std::sort(container.begin(), container.end());
20 container.erase(std::unique(container.begin(), container.end()), container.end());
21 }
22
23 template<typename T>
inplaceSetDifference(T & set1,const T & set2)24 inline void inplaceSetDifference(T &set1, const T &set2)
25 {
26 auto beginIt = set1.begin();
27 for (const auto &elem : set2) {
28 const auto it = std::lower_bound(beginIt, set1.end(), elem);
29 if (it != set1.end() && *it == elem) {
30 beginIt = set1.erase(it);
31 }
32 }
33 }
34
35 template<typename Container, typename Value>
setInsert(Container & c,const Value & v)36 inline void setInsert(Container &c, const Value &v)
37 {
38 const auto it = std::lower_bound(c.begin(), c.end(), v);
39 if (it == c.end() || *it != v) {
40 c.insert(it, v);
41 }
42 }
43
44 template<typename It, typename Value>
strictLowerBound(It begin,It end,const Value & v)45 inline It strictLowerBound(It begin, It end, const Value &v)
46 {
47 const auto it = std::lower_bound(begin, end, v);
48 if (it == end || (*it) >= v) {
49 return it == begin ? end : (it - 1);
50 }
51 return it;
52 }
53
54 }
55
56 #endif
57