1 |
786 |
skrzyp |
// This file is part of the uSTL library, an STL implementation.
|
2 |
|
|
//
|
3 |
|
|
// Copyright (c) 2005-2009 by Mike Sharov <msharov@users.sourceforge.net>
|
4 |
|
|
// This file is free software, distributed under the MIT License.
|
5 |
|
|
|
6 |
|
|
#ifndef USET_H_45543F516E02A87A3FCEA5024052A6F5
|
7 |
|
|
#define USET_H_45543F516E02A87A3FCEA5024052A6F5
|
8 |
|
|
|
9 |
|
|
#include "uvector.h"
|
10 |
|
|
|
11 |
|
|
namespace ustl {
|
12 |
|
|
|
13 |
|
|
/// \class set uset.h ustl.h
|
14 |
|
|
/// \ingroup Sequences
|
15 |
|
|
///
|
16 |
|
|
/// \brief Unique sorted container. Sorted vector with all values unique.
|
17 |
|
|
///
|
18 |
|
|
template <typename T>
|
19 |
|
|
class set : public vector<T> {
|
20 |
|
|
public:
|
21 |
|
|
typedef const set<T>& rcself_t;
|
22 |
|
|
typedef vector<T> base_class;
|
23 |
|
|
typedef typename base_class::value_type key_type;
|
24 |
|
|
typedef typename base_class::value_type data_type;
|
25 |
|
|
typedef typename base_class::value_type value_type;
|
26 |
|
|
typedef typename base_class::size_type size_type;
|
27 |
|
|
typedef typename base_class::pointer pointer;
|
28 |
|
|
typedef typename base_class::const_pointer const_pointer;
|
29 |
|
|
typedef typename base_class::reference reference;
|
30 |
|
|
typedef typename base_class::const_reference const_reference;
|
31 |
|
|
typedef typename base_class::const_iterator const_iterator;
|
32 |
|
|
typedef typename base_class::iterator iterator;
|
33 |
|
|
typedef typename base_class::reverse_iterator reverse_iterator;
|
34 |
|
|
typedef typename base_class::const_reverse_iterator const_reverse_iterator;
|
35 |
|
|
typedef pair<iterator,bool> insertrv_t;
|
36 |
|
|
public:
|
37 |
|
|
inline set (void) : vector<T> () { }
|
38 |
|
|
explicit inline set (size_type n) : vector<T> (n) { }
|
39 |
|
|
inline set (rcself_t v) : vector<T> (v) { }
|
40 |
|
|
inline set (const_iterator i1, const_iterator i2) : vector<T> () { insert (i1, i2); }
|
41 |
|
|
inline rcself_t operator= (rcself_t v) { base_class::operator= (v); return (*this); }
|
42 |
|
|
inline size_type size (void) const { return (base_class::size()); }
|
43 |
|
|
inline iterator begin (void) { return (base_class::begin()); }
|
44 |
|
|
inline const_iterator begin (void) const { return (base_class::begin()); }
|
45 |
|
|
inline iterator end (void) { return (base_class::end()); }
|
46 |
|
|
inline const_iterator end (void) const { return (base_class::end()); }
|
47 |
|
|
inline void assign (const_iterator i1, const_iterator i2) { clear(); insert (i1, i2); }
|
48 |
|
|
inline void push_back (const_reference v) { insert (v); }
|
49 |
|
|
inline const_iterator find (const_reference v) const { const_iterator i = lower_bound (begin(), end(), v); return ((i != end() && *i == v) ? i : end()); }
|
50 |
|
|
inline iterator find (const_reference v) { return (const_cast<iterator>(const_cast<rcself_t>(*this).find (v))); }
|
51 |
|
|
insertrv_t insert (const_reference v);
|
52 |
|
|
inline void insert (const_iterator i1, const_iterator i2);
|
53 |
|
|
inline void erase (const_reference v);
|
54 |
|
|
inline iterator erase (iterator ep) { return (base_class::erase (ep)); }
|
55 |
|
|
inline iterator erase (iterator ep1, iterator ep2) { return (base_class::erase (ep1, ep2)); }
|
56 |
|
|
inline void clear (void) { base_class::clear(); }
|
57 |
|
|
};
|
58 |
|
|
|
59 |
|
|
/// Inserts \p v into the container, maintaining the sort order.
|
60 |
|
|
template <typename T>
|
61 |
|
|
typename set<T>::insertrv_t set<T>::insert (const_reference v)
|
62 |
|
|
{
|
63 |
|
|
iterator ip = lower_bound (begin(), end(), v);
|
64 |
|
|
bool bInserted = (ip == end() || v < *ip);
|
65 |
|
|
if (bInserted)
|
66 |
|
|
ip = base_class::insert (ip, v);
|
67 |
|
|
return (make_pair (ip, bInserted));
|
68 |
|
|
}
|
69 |
|
|
|
70 |
|
|
/// Inserts the contents of range [i1,i2)
|
71 |
|
|
template <typename T>
|
72 |
|
|
void set<T>::insert (const_iterator i1, const_iterator i2)
|
73 |
|
|
{
|
74 |
|
|
assert (i1 <= i2);
|
75 |
|
|
reserve (size() + distance (i1, i2));
|
76 |
|
|
for (; i1 < i2; ++i1)
|
77 |
|
|
push_back (*i1);
|
78 |
|
|
}
|
79 |
|
|
|
80 |
|
|
/// Erases the element with value \p v.
|
81 |
|
|
template <typename T>
|
82 |
|
|
inline void set<T>::erase (const_reference v)
|
83 |
|
|
{
|
84 |
|
|
iterator ip = find (v);
|
85 |
|
|
if (ip != end())
|
86 |
|
|
erase (ip);
|
87 |
|
|
}
|
88 |
|
|
|
89 |
|
|
} // namespace ustl
|
90 |
|
|
|
91 |
|
|
#endif
|