mn_sorted_vector.hpp
Go to the documentation of this file.
1 /*
2 *This file is part of the Mini Thread Library (https://github.com/RoseLeBlood/MiniThread ).
3 *Copyright (c) 2018-2021 Amber-Sophia Schroeck
4 *
5 *The Mini Thread Library is free software; you can redistribute it and/or modify
6 *it under the terms of the GNU Lesser General Public License as published by
7 *the Free Software Foundation, version 3, or (at your option) any later version.
8 
9 *The Mini Thread Library 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 Lesser General Public
15 *License along with the Mini Thread Library; if not, see
16 *<https://www.gnu.org/licenses/>.
17 */
18 
19 #ifndef _MINLIB_3b4e9cd5_8d8d_4268_bbff_2f66acdac76e_H_
20 #define _MINLIB_3b4e9cd5_8d8d_4268_bbff_2f66acdac76e_H_
21 
22 
23 #include "../mn_functional.hpp"
24 #include "mn_pair.hpp"
25 #include "../utils/mn_sort.hpp"
26 #include "mn_vector.hpp"
27 
28 namespace mn {
29  namespace container {
30  namespace internal {
31 
38  template<class TPair, class TFunctor>
39  struct compare_func {
40  bool operator()(const TPair& lhs, const TPair& rhs) const {
41  return TFunctor()(lhs.first, rhs.first);
42  }
43  bool operator()(const TPair& lhs, const typename TPair::first_type& rhs) const {
44  return TFunctor()(lhs.first, rhs);
45  }
46  bool operator()(const typename TPair::first_type& lhs, const TPair& rhs) const {
47  return TFunctor()(lhs, rhs.first);
48  }
49  };
50  }
51 
52  template<typename TKey, typename TValue, class TAllocator,
53  class TCompare = mn::less<TKey>, class TStorage = basic_vector_storage< basic_pair<TKey, TValue>, TAllocator> >
54  class basic_sorted_vector : private basic_vector<basic_pair<TKey, TValue>, TAllocator, TStorage > {
55  using base_type = basic_vector<basic_pair<TKey, TValue>, TAllocator, TStorage>;
56  public:
57  using key_type = TKey;
58  using mapped_type = TValue;
59  using size_type = typename base_type::size_type;
61  using pointer = typename base_type::pointer;
62  using reference = typename base_type::reference;
63 
64 
65  using iterator = typename base_type::iterator;
68 
71 
72  explicit basic_sorted_vector(const allocator_type& allocator = allocator_type())
73  : base_type(allocator) { }
74 
75  template <class InputIterator>
76  basic_sorted_vector(InputIterator first, InputIterator last,
77  const allocator_type& allocator = allocator_type())
78  : base_type(first, last, allocator) {
80  assert(invariant());
81  }
82 
85 
86  using base_type::begin;
87  using base_type::end;
88  using base_type::size;
89  using base_type::empty;
90  using base_type::capacity;
91  using base_type::clear;
94 
95  pair_type insert(const value_type& val) {
96  assert(invariant());
97  bool found(true);
98 
99  iterator it = lower_bound(val.first);
100  assert(it == end() || !m_compare(*it, val));
101 
102  if (it == end() || m_compare(*it, val)){
103  it = base_type::insert(it, val);
104  found = false;
105  }
106  assert(invariant());
107  return pair_type(it, !found);
108  }
109  inline pair_type insert(const key_type& k, const mapped_type& v) {
110  return insert(value_type(k, v));
111  }
112 
113  iterator find(const key_type& k) {
114  assert(invariant());
115  iterator i(lower_bound(k));
116  if (i != end() && m_compare(k, *i)) {
117  i = end();
118  }
119  return i;
120  }
121  const_iterator find(const key_type& k) const {
122  assert(invariant());
124  if (i != end() && m_compare(k, *i)) i = end();
125  return i;
126  }
127 
128  inline iterator erase(iterator it) {
129  assert(invariant());
130  return base_type::erase(it);
131  }
133  iterator i(find(k));
134  if (i == end()) return 0;
135  erase(i);
136  assert(invariant());
137  return 1;
138  }
140  return mn::lower_bound(begin(), end(), k, m_compare);
141  }
143  return mn::lower_bound(begin(), end(), k, m_compare);
144  }
146  return mn::upper_bound(begin(), end(), k, m_compare);
147  }
149  return mn::upper_bound(begin(), end(), k, m_compare);
150  }
151  private:
152  bool invariant() const {
153  const_iterator first = begin();
154  const_iterator last = end();
155 
156  assert(last >= first);
157  if (first != last) {
158  const_iterator next = first;
159  if (++next != last) {
160  assert(m_compare(*first, *next));
161  first = next;
162  }
163  }
164  return true;
165  }
166  private:
168  };
169 
170 
171  template<typename TKey, typename TValue, class TCompare = mn::less<TKey> >
173 
174  }
175 }
176 
177 #endif
Definition: mn_sorted_vector.hpp:54
bool invariant() const
Definition: mn_sorted_vector.hpp:152
TKey key_type
Definition: mn_sorted_vector.hpp:57
iterator begin()
Definition: mn_vector.hpp:173
iterator upper_bound(const key_type &k)
Definition: mn_sorted_vector.hpp:145
basic_sorted_vector & operator=(const basic_sorted_vector &)=delete
iterator end()
Definition: mn_vector.hpp:174
pair_type insert(const value_type &val)
Definition: mn_sorted_vector.hpp:95
basic_sorted_vector(const allocator_type &allocator=allocator_type())
Definition: mn_sorted_vector.hpp:72
size_type erase(const key_type &k)
Definition: mn_sorted_vector.hpp:132
iterator erase(iterator it)
Definition: mn_sorted_vector.hpp:128
iterator lower_bound(const key_type &k)
Definition: mn_sorted_vector.hpp:139
const_iterator upper_bound(const key_type &k) const
Definition: mn_sorted_vector.hpp:148
typename base_type::size_type size_type
Definition: mn_sorted_vector.hpp:59
basic_sorted_vector(const basic_sorted_vector &)=delete
iterator find(const key_type &k)
Definition: mn_sorted_vector.hpp:113
typename base_type::value_type value_type
Definition: mn_sorted_vector.hpp:60
const_iterator find(const key_type &k) const
Definition: mn_sorted_vector.hpp:121
basic_sorted_vector(InputIterator first, InputIterator last, const allocator_type &allocator=allocator_type())
Definition: mn_sorted_vector.hpp:76
basic_pair< iterator, bool > pair_type
Definition: mn_sorted_vector.hpp:70
pair_type insert(const key_type &k, const mapped_type &v)
Definition: mn_sorted_vector.hpp:109
typename base_type::const_iterator const_iterator
Definition: mn_sorted_vector.hpp:66
compare_type m_compare
Definition: mn_sorted_vector.hpp:167
const_iterator lower_bound(const key_type &k) const
Definition: mn_sorted_vector.hpp:142
typename base_type::pointer pointer
Definition: mn_sorted_vector.hpp:61
typename base_type::reference reference
Definition: mn_sorted_vector.hpp:62
typename base_type::iterator iterator
Definition: mn_sorted_vector.hpp:65
typename base_type::allocator_type allocator_type
Definition: mn_sorted_vector.hpp:67
TValue mapped_type
Definition: mn_sorted_vector.hpp:58
Definition: mn_vector.hpp:117
const pointer const_iterator
Definition: mn_vector.hpp:128
T value_type
Definition: mn_vector.hpp:121
iterator begin()
Definition: mn_vector.hpp:173
iterator end()
Definition: mn_vector.hpp:174
size_type capacity()
Definition: mn_vector.hpp:179
TAllocator allocator_type
Definition: mn_vector.hpp:129
const allocator_type & get_allocator() const
Definition: mn_vector.hpp:377
void set_allocator(const allocator_type &allocator)
Definition: mn_vector.hpp:381
size_type size() const
Definition: mn_vector.hpp:176
mn::size_t size_type
Definition: mn_vector.hpp:130
pointer iterator
Definition: mn_vector.hpp:127
void insert(size_type index, size_type n, const reference val)
Definition: mn_vector.hpp:228
T * pointer
Definition: mn_vector.hpp:122
bool empty() const
Definition: mn_vector.hpp:177
T & reference
Definition: mn_vector.hpp:123
void clear()
Definition: mn_vector.hpp:343
iterator erase(iterator it)
Definition: mn_vector.hpp:306
Basic vector container This file is part of the Mini Thread Library (https://github....
Definition: mn_allocator_typetraits.hpp:25
void quick_sort(T *begin, T *end, TPredicate pred)
Definition: mn_sort.hpp:124
constexpr TIter upper_bound(TIter first, TIter last, const TPred &value)
Definition: mn_algorithm.hpp:191
TForwardIter next(TForwardIter x, TDistance n=1)
Definition: mn_iterator.hpp:112
constexpr TIter lower_bound(TIter first, TIter last, const TPred &value)
Definition: mn_algorithm.hpp:160
Definition: mn_pair.hpp:28
Internal helper for sorting.
Definition: mn_sorted_vector.hpp:39
bool operator()(const TPair &lhs, const typename TPair::first_type &rhs) const
Definition: mn_sorted_vector.hpp:43
bool operator()(const TPair &lhs, const TPair &rhs) const
Definition: mn_sorted_vector.hpp:40
bool operator()(const typename TPair::first_type &lhs, const TPair &rhs) const
Definition: mn_sorted_vector.hpp:46
Definition: mn_utils.hpp:175