Loading...
Searching...
No Matches
RangedIndexSet.h
// Copyright (C) 2012 Joachim B Haga
//
// 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: 2012-03-02
// Last changed: 2012-03-02
#ifndef __RANGED_INDEX_SET_H
#define __RANGED_INDEX_SET_H
#include <cstddef>
#include <vector>
#include <dolfin/log/log.h>
namespace dolfin
{
class RangedIndexSet
{
public:
RangedIndexSet(std::pair<std::size_t, std::size_t> range)
: _range(range), _is_set(range.second - range.first)
{
clear();
}
RangedIndexSet(std::size_t upper_range)
: _range(std::pair<std::size_t, std::size_t>(0, upper_range)), _is_set(upper_range)
{
clear();
}
bool in_range(std::size_t i) const
{
return (i >= _range.first && i < _range.second);
}
bool has_index(std::size_t i) const
{
dolfin_assert(in_range(i));
return _is_set[i - _range.first];
}
bool insert(std::size_t i)
{
dolfin_assert(in_range(i));
std::vector<bool>::reference entry = _is_set[i - _range.first];
if (entry)
{
return false;
}
else
{
entry = true;
return true;
}
}
void erase(std::size_t i)
{
dolfin_assert(in_range(i));
_is_set[i - _range.first] = false;
}
void clear()
{
std::fill(_is_set.begin(), _is_set.end(), false);
}
private:
const std::pair<std::size_t, std::size_t> _range;
std::vector<bool> _is_set;
};
}
#endif
dolfin::RangedIndexSet::clear
void clear()
Erase all indices from the set.
Definition RangedIndexSet.h:94
dolfin::RangedIndexSet::has_index
bool has_index(std::size_t i) const
Check is the set contains the given index.
Definition RangedIndexSet.h:63
dolfin::RangedIndexSet::RangedIndexSet
RangedIndexSet(std::pair< std::size_t, std::size_t > range)
Create a ranged set with range given as a (lower, upper) pair.
Definition RangedIndexSet.h:43
dolfin::RangedIndexSet::erase
void erase(std::size_t i)
Erase an index from the set.
Definition RangedIndexSet.h:87
dolfin::RangedIndexSet::insert
bool insert(std::size_t i)
Definition RangedIndexSet.h:71
dolfin::RangedIndexSet::in_range
bool in_range(std::size_t i) const
Return true if a given index is within range, i.e., if it can be stored in the set.
Definition RangedIndexSet.h:57
dolfin::RangedIndexSet::RangedIndexSet
RangedIndexSet(std::size_t upper_range)
Create a ranged set with 0 as lower range.
Definition RangedIndexSet.h:50
dolfin
Definition adapt.h:30