DOLFIN
DOLFIN C++ interface
RangedIndexSet.h
1 // Copyright (C) 2012 Joachim B Haga
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: 2012-03-02
19 // Last changed: 2012-03-02
20 
21 #ifndef __RANGED_INDEX_SET_H
22 #define __RANGED_INDEX_SET_H
23 
24 #include <cstddef>
25 #include <vector>
26 #include <dolfin/log/log.h>
27 
28 namespace dolfin
29 {
30 
36 
38  {
39 
40  public:
41 
43  RangedIndexSet(std::pair<std::size_t, std::size_t> range)
44  : _range(range), _is_set(range.second - range.first)
45  {
46  clear();
47  }
48 
50  RangedIndexSet(std::size_t upper_range)
51  : _range(std::pair<std::size_t, std::size_t>(0, upper_range)), _is_set(upper_range)
52  {
53  clear();
54  }
55 
57  bool in_range(std::size_t i) const
58  {
59  return (i >= _range.first && i < _range.second);
60  }
61 
63  bool has_index(std::size_t i) const
64  {
65  dolfin_assert(in_range(i));
66  return _is_set[i - _range.first];
67  }
68 
71  bool insert(std::size_t i)
72  {
73  dolfin_assert(in_range(i));
74  std::vector<bool>::reference entry = _is_set[i - _range.first];
75  if (entry)
76  {
77  return false;
78  }
79  else
80  {
81  entry = true;
82  return true;
83  }
84  }
85 
87  void erase(std::size_t i)
88  {
89  dolfin_assert(in_range(i));
90  _is_set[i - _range.first] = false;
91  }
92 
94  void clear()
95  {
96  std::fill(_is_set.begin(), _is_set.end(), false);
97  }
98 
99  private:
100 
101  const std::pair<std::size_t, std::size_t> _range;
102  std::vector<bool> _is_set;
103 
104  };
105 
106 }
107 
108 #endif
bool has_index(std::size_t i) const
Check is the set contains the given index.
Definition: RangedIndexSet.h:63
Definition: adapt.h:29
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
Definition: RangedIndexSet.h:37
void clear()
Erase all indices from the set.
Definition: RangedIndexSet.h:94
bool insert(std::size_t i)
Definition: RangedIndexSet.h:71
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
void erase(std::size_t i)
Erase an index from the set.
Definition: RangedIndexSet.h:87
RangedIndexSet(std::size_t upper_range)
Create a ranged set with 0 as lower range.
Definition: RangedIndexSet.h:50