DOLFIN
DOLFIN C++ interface
Set.h
1 // Copyright (C) 2009-2011 Garth N. Wells
2 //
3 // This file is part of DOLFIN.
4 //
5 // DOLFIN 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, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // DOLFIN is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // First added: 2009-08-09
19 // Last changed: 2011-01-05
20 
21 #ifndef __DOLFIN_SET_H
22 #define __DOLFIN_SET_H
23 
24 #include <algorithm>
25 #include <cstddef>
26 #include <vector>
27 
28 namespace dolfin
29 {
30 
33  // and std::unordered_set in some cases.
34 
35  template<typename T>
36  class Set
37  {
38  public:
39 
41  typedef typename std::vector<T>::iterator iterator;
43  typedef typename std::vector<T>::const_iterator const_iterator;
44 
46  Set() {}
47 
49  Set(std::vector<T>& x) : _x(x)
50  { _x.clear(); }
51 
53  Set(const dolfin::Set<T>& x) : _x(x._x) {}
54 
56  ~Set() {}
57 
59  iterator find(const T& x)
60  { return std::find(_x.begin(), _x.end(), x); }
61 
63  const_iterator find(const T& x) const
64  { return std::find(_x.begin(), _x.end(), x); }
65 
67  bool insert(const T& x)
68  {
69  if( find(x) == this->end() )
70  {
71  _x.push_back(x);
72  return true;
73  }
74  else
75  return false;
76  }
77 
79  template <typename InputIt>
80  void insert(const InputIt first, const InputIt last)
81  {
82  for (InputIt position = first; position != last; ++position)
83  {
84  if (std::find(_x.begin(), _x.end(), *position) == _x.end())
85  _x.push_back(*position);
86  }
87  }
88 
90  const_iterator begin() const
91  { return _x.begin(); }
92 
94  const_iterator end() const
95  { return _x.end(); }
96 
98  std::size_t size() const
99  { return _x.size(); }
100 
102  void erase(const T& x)
103  {
104  iterator p = find(x);
105  if (p != _x.end())
106  _x.erase(p);
107  }
108 
110  void sort()
111  { std::sort(_x.begin(), _x.end()); }
112 
114  void clear()
115  { _x.clear(); }
116 
118  T operator[](std::size_t n) const
119  { return _x[n]; }
120 
122  const std::vector<T>& set() const
123  { return _x; }
124 
126  std::vector<T>& set()
127  { return _x; }
128 
129  private:
130 
131  std::vector<T> _x;
132 
133  };
134 
135 }
136 
137 #endif
Set()
Create empty set.
Definition: Set.h:46
std::vector< T >::const_iterator const_iterator
Const iterator.
Definition: Set.h:43
std::vector< T >::iterator iterator
Iterator.
Definition: Set.h:41
std::size_t size() const
Set size.
Definition: Set.h:98
void insert(const InputIt first, const InputIt last)
Insert entries.
Definition: Set.h:80
Definition: adapt.h:29
void sort()
Sort set.
Definition: Set.h:110
Definition: Set.h:36
T operator[](std::size_t n) const
Index the nth entry in the set.
Definition: Set.h:118
Set(std::vector< T > &x)
Wrap std::vector as a set. Contents will be erased.
Definition: Set.h:49
const_iterator end() const
Iterator to beyond end of Set.
Definition: Set.h:94
~Set()
Destructor.
Definition: Set.h:56
Set(const dolfin::Set< T > &x)
Copy constructor.
Definition: Set.h:53
const_iterator find(const T &x) const
Find entry in set and return an iterator to the entry (const)
Definition: Set.h:63
const_iterator begin() const
Iterator to start of Set.
Definition: Set.h:90
void erase(const T &x)
Erase an entry.
Definition: Set.h:102
bool insert(const T &x)
Insert entry.
Definition: Set.h:67
void clear()
Clear set.
Definition: Set.h:114
iterator find(const T &x)
Find entry in set and return an iterator to the entry.
Definition: Set.h:59