|
| 1 | +/* |
| 2 | + * Copyright (C) 2022 Apertium |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or |
| 5 | + * modify it under the terms of the GNU General Public License as |
| 6 | + * published by the Free Software Foundation; either version 2 of the |
| 7 | + * License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, but |
| 10 | + * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | + * General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program; if not, see <https://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +#pragma once |
| 19 | +#ifndef c6d28b7452ec699b_SORTED_VECTOR_HPP |
| 20 | +#define c6d28b7452ec699b_SORTED_VECTOR_HPP |
| 21 | +#include <set> |
| 22 | +#include <vector> |
| 23 | +#include <algorithm> |
| 24 | +#include <functional> |
| 25 | + |
| 26 | +namespace detail { |
| 27 | + template<typename ForwardIt, typename Comp> |
| 28 | + bool is_sorted(ForwardIt first, ForwardIt last, Comp comp) { |
| 29 | + if (first != last) { |
| 30 | + ForwardIt next = first; |
| 31 | + while (++next != last) { |
| 32 | + if (comp(*next, *first)) { |
| 33 | + return false; |
| 34 | + } |
| 35 | + first = next; |
| 36 | + } |
| 37 | + } |
| 38 | + return true; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +template<typename T, typename Comp = std::less<T>> |
| 43 | +class sorted_vector { |
| 44 | +public: |
| 45 | + typedef typename std::vector<T> container; |
| 46 | + typedef typename container::iterator iterator; |
| 47 | + typedef typename container::const_iterator const_iterator; |
| 48 | + typedef typename container::const_reverse_iterator const_reverse_iterator; |
| 49 | + typedef typename container::size_type size_type; |
| 50 | + typedef T value_type; |
| 51 | + typedef T key_type; |
| 52 | + |
| 53 | + sorted_vector() {} |
| 54 | + |
| 55 | + sorted_vector(const std::set<T>& o) { |
| 56 | + insert(o.begin(), o.end()); |
| 57 | + } |
| 58 | + |
| 59 | + std::pair<iterator, bool> insert(T t) { |
| 60 | + if (elements.empty()) { |
| 61 | + elements.push_back(t); |
| 62 | + return std::make_pair(elements.begin(), true); |
| 63 | + } |
| 64 | + iterator it = std::lower_bound(elements.begin(), elements.end(), t, comp); |
| 65 | + size_t at = std::distance(elements.begin(), it); |
| 66 | + if (it == elements.end() || comp(*it, t) || comp(t, *it)) { |
| 67 | + elements.insert(it, t); |
| 68 | + return std::make_pair(elements.begin() + at, true); |
| 69 | + } |
| 70 | + return std::make_pair(elements.begin() + at, false); |
| 71 | + } |
| 72 | + |
| 73 | + template<typename It> |
| 74 | + void insert(It b, It e) { |
| 75 | + size_t d = std::distance(b, e); |
| 76 | + if (d == 1) { |
| 77 | + insert(*b); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + static thread_local container merged; |
| 82 | + merged.resize(0); |
| 83 | + merged.reserve(elements.size() + d); |
| 84 | + |
| 85 | + if (detail::is_sorted(b, e, comp)) { |
| 86 | + std::merge(elements.begin(), elements.end(), b, e, std::back_inserter(merged), comp); |
| 87 | + } |
| 88 | + else { |
| 89 | + static thread_local container sorted; |
| 90 | + sorted.assign(b, e); |
| 91 | + std::sort(sorted.begin(), sorted.end(), comp); |
| 92 | + std::merge(elements.begin(), elements.end(), sorted.begin(), sorted.end(), std::back_inserter(merged), comp); |
| 93 | + } |
| 94 | + |
| 95 | + merged.swap(elements); |
| 96 | + auto it = std::unique(elements.begin(), elements.end()); |
| 97 | + elements.erase(it, elements.end()); |
| 98 | + } |
| 99 | + |
| 100 | + void push_back(T t) { |
| 101 | + insert(t); |
| 102 | + } |
| 103 | + |
| 104 | + bool erase(T t) { |
| 105 | + if (elements.empty()) { |
| 106 | + return false; |
| 107 | + } |
| 108 | + if (comp(elements.back(), t)) { |
| 109 | + return false; |
| 110 | + } |
| 111 | + if (comp(t, elements.front())) { |
| 112 | + return false; |
| 113 | + } |
| 114 | + auto it = lower_bound(t); |
| 115 | + if (it != elements.end() && !comp(*it, t) && !comp(t, *it)) { |
| 116 | + elements.erase(it); |
| 117 | + return true; |
| 118 | + } |
| 119 | + return false; |
| 120 | + } |
| 121 | + |
| 122 | + const_iterator erase(const_iterator it) { |
| 123 | + size_type o = std::distance<const_iterator>(elements.begin(), it); |
| 124 | + return elements.erase(elements.begin() + o); |
| 125 | + } |
| 126 | + |
| 127 | + template<typename It> |
| 128 | + void erase(It b, It e) { |
| 129 | + for (; b != e; ++b) { |
| 130 | + erase(*b); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + const_iterator find(T t) const { |
| 135 | + if (elements.empty()) { |
| 136 | + return elements.end(); |
| 137 | + } |
| 138 | + if (comp(elements.back(), t)) { |
| 139 | + return elements.end(); |
| 140 | + } |
| 141 | + if (comp(t, elements.front())) { |
| 142 | + return elements.end(); |
| 143 | + } |
| 144 | + auto it = lower_bound(t); |
| 145 | + if (it != elements.end() && (comp(*it, t) || comp(t, *it))) { |
| 146 | + return elements.end(); |
| 147 | + } |
| 148 | + return it; |
| 149 | + } |
| 150 | + |
| 151 | + size_t count(T t) const { |
| 152 | + return (find(t) != end()); |
| 153 | + } |
| 154 | + |
| 155 | + iterator begin() { |
| 156 | + return elements.begin(); |
| 157 | + } |
| 158 | + |
| 159 | + iterator end() { |
| 160 | + return elements.end(); |
| 161 | + } |
| 162 | + |
| 163 | + const_iterator begin() const { |
| 164 | + return elements.begin(); |
| 165 | + } |
| 166 | + |
| 167 | + const_iterator end() const { |
| 168 | + return elements.end(); |
| 169 | + } |
| 170 | + |
| 171 | + const_iterator cbegin() const { |
| 172 | + return elements.cbegin(); |
| 173 | + } |
| 174 | + |
| 175 | + const_iterator cend() const { |
| 176 | + return elements.cend(); |
| 177 | + } |
| 178 | + |
| 179 | + const_reverse_iterator rbegin() const { |
| 180 | + return elements.rbegin(); |
| 181 | + } |
| 182 | + |
| 183 | + const_reverse_iterator rend() const { |
| 184 | + return elements.rend(); |
| 185 | + } |
| 186 | + |
| 187 | + T front() const { |
| 188 | + return elements.front(); |
| 189 | + } |
| 190 | + |
| 191 | + T back() const { |
| 192 | + return elements.back(); |
| 193 | + } |
| 194 | + |
| 195 | + iterator lower_bound(T t) { |
| 196 | + return std::lower_bound(elements.begin(), elements.end(), t, comp); |
| 197 | + } |
| 198 | + |
| 199 | + const_iterator lower_bound(T t) const { |
| 200 | + return std::lower_bound(elements.begin(), elements.end(), t, comp); |
| 201 | + } |
| 202 | + |
| 203 | + const_iterator upper_bound(T t) const { |
| 204 | + return std::upper_bound(elements.begin(), elements.end(), t, comp); |
| 205 | + } |
| 206 | + |
| 207 | + bool intersects(const sorted_vector<T>& other) const { |
| 208 | + auto ti = begin(); |
| 209 | + auto oi = other.begin(); |
| 210 | + auto te = end(); |
| 211 | + auto oe = other.end(); |
| 212 | + while (ti != te && oi != oe) { |
| 213 | + if (*ti == *oi) { |
| 214 | + return true; |
| 215 | + } |
| 216 | + else if (comp(*ti, *oi)) { |
| 217 | + ++ti; |
| 218 | + } |
| 219 | + else { |
| 220 | + ++oi; |
| 221 | + } |
| 222 | + } |
| 223 | + return false; |
| 224 | + } |
| 225 | + |
| 226 | + size_type size() const { |
| 227 | + return elements.size(); |
| 228 | + } |
| 229 | + |
| 230 | + size_type capacity() const { |
| 231 | + return elements.capacity(); |
| 232 | + } |
| 233 | + |
| 234 | + bool empty() const { |
| 235 | + return elements.empty(); |
| 236 | + } |
| 237 | + |
| 238 | + template<typename It> |
| 239 | + void assign(It b, It e) { |
| 240 | + clear(); |
| 241 | + insert(b, e); |
| 242 | + } |
| 243 | + |
| 244 | + void assign(const_iterator b, const_iterator e) { |
| 245 | + elements.assign(b, e); |
| 246 | + } |
| 247 | + |
| 248 | + void swap(sorted_vector& other) { |
| 249 | + elements.swap(other.elements); |
| 250 | + } |
| 251 | + |
| 252 | + void clear() { |
| 253 | + elements.clear(); |
| 254 | + } |
| 255 | + |
| 256 | + void sort() { |
| 257 | + std::sort(elements.begin(), elements.end(), Comp()); |
| 258 | + } |
| 259 | + |
| 260 | + void pop_back() { |
| 261 | + elements.pop_back(); |
| 262 | + } |
| 263 | + |
| 264 | + container& get() { |
| 265 | + return elements; |
| 266 | + } |
| 267 | + |
| 268 | + bool operator<(const sorted_vector<T>& o) const { |
| 269 | + return elements < o.elements; |
| 270 | + } |
| 271 | + |
| 272 | +private: |
| 273 | + container elements; |
| 274 | + Comp comp; |
| 275 | +}; |
| 276 | + |
| 277 | +#endif |
0 commit comments