Loading...
Searching...
No Matches
Set.h
// Copyright (C) 2009-2011 Garth N. Wells
//
// This file is part of DOLFIN.
//
// DOLFIN is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// DOLFIN is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
//
// First added: 2009-08-09
// Last changed: 2011-01-05
#ifndef __DOLFIN_SET_H
#define __DOLFIN_SET_H
#include <algorithm>
#include <cstddef>
#include <vector>
namespace dolfin
{
// and std::unordered_set in some cases.
template<typename T>
class Set
{
public:
typedef typename std::vector<T>::iterator iterator;
typedef typename std::vector<T>::const_iterator const_iterator;
Set() {}
Set(std::vector<T>& x) : _x(x)
{ _x.clear(); }
Set(const dolfin::Set<T>& x) : _x(x._x) {}
~Set() {}
iterator find(const T& x)
{ return std::find(_x.begin(), _x.end(), x); }
const_iterator find(const T& x) const
{ return std::find(_x.begin(), _x.end(), x); }
bool insert(const T& x)
{
if( find(x) == this->end() )
{
_x.push_back(x);
return true;
}
else
return false;
}
template <typename InputIt>
void insert(const InputIt first, const InputIt last)
{
for (InputIt position = first; position != last; ++position)
{
if (std::find(_x.begin(), _x.end(), *position) == _x.end())
_x.push_back(*position);
}
}
const_iterator begin() const
{ return _x.begin(); }
const_iterator end() const
{ return _x.end(); }
std::size_t size() const
{ return _x.size(); }
void erase(const T& x)
{
iterator p = find(x);
if (p != _x.end())
_x.erase(p);
}
void sort()
{ std::sort(_x.begin(), _x.end()); }
void clear()
{ _x.clear(); }
T operator[](std::size_t n) const
{ return _x[n]; }
const std::vector<T>& set() const
{ return _x; }
std::vector<T>& set()
{ return _x; }
private:
std::vector<T> _x;
};
}
#endif
dolfin::Set
Definition Set.h:37
dolfin::Set::clear
void clear()
Clear set.
Definition Set.h:114
dolfin::Set::erase
void erase(const T &x)
Erase an entry.
Definition Set.h:102
dolfin::Set::~Set
~Set()
Destructor.
Definition Set.h:56
dolfin::Set::size
std::size_t size() const
Set size.
Definition Set.h:98
dolfin::Set::find
const_iterator find(const T &x) const
Find entry in set and return an iterator to the entry (const).
Definition Set.h:63
dolfin::Set::end
const_iterator end() const
Iterator to beyond end of Set.
Definition Set.h:94
dolfin::Set::const_iterator
std::vector< T >::const_iterator const_iterator
Const iterator.
Definition Set.h:43
dolfin::Set::Set
Set(const dolfin::Set< T > &x)
Copy constructor.
Definition Set.h:53
dolfin::Set::Set
Set()
Create empty set.
Definition Set.h:46
dolfin::Set::Set
Set(std::vector< T > &x)
Wrap std::vector as a set. Contents will be erased.
Definition Set.h:49
dolfin::Set::set
std::vector< T > & set()
Return the vector that stores the data in the Set.
Definition Set.h:126
dolfin::Set::iterator
std::vector< T >::iterator iterator
Iterator.
Definition Set.h:41
dolfin::Set::set
const std::vector< T > & set() const
Return the vector that stores the data in the Set.
Definition Set.h:122
dolfin::Set::begin
const_iterator begin() const
Iterator to start of Set.
Definition Set.h:90
dolfin::Set::sort
void sort()
Sort set.
Definition Set.h:110
dolfin::Set::operator[]
T operator[](std::size_t n) const
Index the nth entry in the set.
Definition Set.h:118
dolfin::Set::find
iterator find(const T &x)
Find entry in set and return an iterator to the entry.
Definition Set.h:59
dolfin::Set::insert
void insert(const InputIt first, const InputIt last)
Insert entries.
Definition Set.h:80
dolfin::Set::insert
bool insert(const T &x)
Insert entry.
Definition Set.h:67
dolfin
Definition adapt.h:30