forked from andrewprock/ustl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
umultiset.h
84 lines (80 loc) · 5.34 KB
/
umultiset.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// This file is part of the uSTL library, an STL implementation.
//
// Copyright (c) 2005 by Mike Sharov <[email protected]>
// This file is free software, distributed under the MIT License.
#pragma once
#include "uvector.h"
#include "ualgo.h"
namespace ustl {
/// \class multiset umultiset.h ustl.h
/// \ingroup AssociativeContainers
///
/// \brief Multiple sorted container.
/// Unlike set, it may contain multiple copies of each element.
///
template <typename T, typename Comp = less<T> >
class multiset : public vector<T> {
public:
using rcself_t = const multiset<T,Comp>&;
using base_class = vector<T>;
using value_type = typename base_class::value_type;
using size_type = typename base_class::size_type;
using pointer = typename base_class::pointer;
using const_pointer = typename base_class::const_pointer;
using reference = typename base_class::reference;
using const_reference = typename base_class::const_reference;
using const_iterator = typename base_class::const_iterator;
using iterator = typename base_class::iterator;
using reverse_iterator = typename base_class::reverse_iterator;
using const_reverse_iterator = typename base_class::const_reverse_iterator;
using range_t = pair<iterator,iterator>;
using const_range_t = pair<const_iterator,const_iterator>;
using initlist_t = std::initializer_list<value_type>;
public:
inline multiset (void) : base_class() {}
inline explicit multiset (size_type n) : base_class (n) {}
inline explicit multiset (rcself_t v) : base_class (v) {}
inline multiset (const_iterator i1, const_iterator i2) : base_class() { insert (i1, i2); }
inline explicit multiset (multiset&& v) : base_class (move(v)) {}
inline multiset (initlist_t v) : base_class() { insert (v.begin(), v.end()); }
inline rcself_t operator= (rcself_t v) { base_class::operator= (v); return *this; }
inline multiset& operator= (multiset&& v) { base_class::operator= (move(v)); return *this; }
inline size_type size (void) const { return base_class::size(); }
inline iterator begin (void) { return base_class::begin(); }
inline const_iterator begin (void) const { return base_class::begin(); }
inline const_iterator cbegin (void) const { return base_class::cbegin(); }
inline iterator end (void) { return base_class::end(); }
inline const_iterator end (void) const { return base_class::end(); }
inline const_iterator cend (void) const { return base_class::cend(); }
inline Comp value_comp (void) const { return Comp(); }
inline Comp key_comp (void) const { return value_comp(); }
inline void assign (const_iterator i1, const_iterator i2) { clear(); insert (i1, i2); }
inline const_iterator find (const_reference v) const { const_iterator i = ::ustl::lower_bound (begin(), end(), v, Comp()); return (i != end() && *i == v) ? i : end(); }
inline iterator find (const_reference v) { return const_cast<iterator>(const_cast<rcself_t>(*this).find (v)); }
inline const_iterator lower_bound (const_reference v) const { return ::ustl::lower_bound (begin(), end(), v, Comp()); }
inline iterator lower_bound (const_reference v) { return const_cast<iterator>(const_cast<rcself_t>(*this).lower_bound (v)); }
inline const_iterator upper_bound (const_reference v) const { return ::ustl::upper_bound (begin(), end(), v, Comp()); }
inline iterator upper_bound (const_reference v) { return const_cast<iterator>(const_cast<rcself_t>(*this).upper_bound (v)); }
inline const_range_t equal_range (const_reference v) const { return ::ustl::equal_range (begin(), end(), v, Comp()); }
inline range_t equal_range (const_reference v) { return ::ustl::equal_range (begin(), end(), v, Comp()); }
inline size_type count (const_reference v) const { const_range_t r = equal_range(v); return distance(r.first,r.second); }
inline void push_back (const_reference v) { insert (v); }
template <typename... Args>
inline iterator emplace (Args&&... args) { return insert (T(forward<Args>(args)...)); }
template <typename... Args>
inline iterator emplace_hint (const_iterator h, Args&&... args) { return insert (h, T(forward<Args>(args)...)); }
template <typename... Args>
inline iterator emplace_back (Args&&... args) { return insert (T(forward<Args>(args)...)); }
inline iterator insert (T&& v) { return base_class::insert (upper_bound(v), move(v)); }
inline iterator insert (const_iterator, T&& v) { return insert (move(v)); }
inline void insert (initlist_t v) { insert (v.begin(), v.end()); }
inline iterator insert (const_reference v) { return base_class::insert (upper_bound(v), v); }
inline iterator insert (const_iterator, const_reference v) { return insert(v); }
void insert (const_iterator i1, const_iterator i2) { for (; i1 < i2; ++i1) insert (*i1); }
inline iterator erase (const_iterator ep) { return base_class::erase (ep); }
inline iterator erase (const_iterator ep1, const_iterator ep2) { return base_class::erase (ep1, ep2); }
inline size_type erase (const_reference v) { range_t epr = equal_range (v); erase (epr.first, epr.second); return distance(epr.first, epr.second); }
inline void clear (void) { base_class::clear(); }
inline void swap (multiset& v) { base_class::swap (v); }
};
} // namespace ustl