DOLFIN
DOLFIN C++ interface
IndexSet.h
1 // Copyright (C) 2011 Anders Logg
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: 2011-02-07
19 // Last changed: 2011-08-28
20 
21 #ifndef __INDEX_SET_H
22 #define __INDEX_SET_H
23 
24 #include <cstddef>
25 #include <vector>
26 
27 namespace dolfin
28 {
29 
33 
34  class IndexSet
35  {
36  public:
37 
39  IndexSet(std::size_t size) : _size(size), _has_index(size),
40  _positions(size)
41  {
42  _indices.reserve(size);
43  clear();
44  }
45 
47  ~IndexSet() {}
48 
50  bool empty() const
51  { return _indices.empty(); }
52 
54  std::size_t size() const
55  { return _indices.size(); }
56 
58  bool has_index(std::size_t index) const
59  {
60  dolfin_assert(index < _size);
61  return _has_index[index];
62  }
63 
65  std::size_t find(std::size_t index) const
66  {
67  dolfin_assert(index < _size);
68  if (!_has_index[index])
69  dolfin_error("IndexSet.h",
70  "locate position of index",
71  "Index %d is not in index set", index);
72  return _positions[index];
73  }
74 
76  std::size_t& operator[] (std::size_t i)
77  {
78  dolfin_assert(i < _indices.size());
79  return _indices[i];
80  }
81 
83  const std::size_t& operator[] (std::size_t i) const
84  {
85  dolfin_assert(i < _indices.size());
86  return _indices[i];
87  }
88 
90  void insert(std::size_t index)
91  {
92  dolfin_assert(index < _size);
93  if (_has_index[index])
94  return;
95  _indices.push_back(index);
96  _has_index[index] = true;
97  _positions[index] = _indices.size() - 1;
98  }
99 
101  void fill()
102  {
103  _indices.clear();
104  for (std::size_t i = 0; i < _size; i++)
105  _indices.push_back(i);
106  std::fill(_has_index.begin(), _has_index.end(), true);
107  }
108 
110  void clear()
111  {
112  _indices.clear();
113  std::fill(_has_index.begin(), _has_index.end(), false);
114  std::fill(_positions.begin(), _positions.end(), 0);
115  }
116 
117  private:
118 
119  // Size (maximum index + 1)
120  std::size_t _size;
121 
122  // Vector of indices
123  std::vector<std::size_t> _indices;
124 
125  // Indicators for which indices are in the set
126  std::vector<std::size_t> _has_index;
127 
128  // Mapping from indices to positions
129  std::vector<std::size_t> _positions;
130 
131  };
132 
133 }
134 
135 #endif
Definition: adapt.h:29
IndexSet(std::size_t size)
Create index set of given size.
Definition: IndexSet.h:39
void clear()
Clear set.
Definition: IndexSet.h:110
Definition: IndexSet.h:34
std::size_t size() const
Return size of set.
Definition: IndexSet.h:54
std::size_t find(std::size_t index) const
Return position (if any) for given index.
Definition: IndexSet.h:65
void fill()
Fill index set with indices 0, 1, 2, ..., size - 1.
Definition: IndexSet.h:101
void insert(std::size_t index)
Insert index into set.
Definition: IndexSet.h:90
std::size_t & operator[](std::size_t i)
Return given index.
Definition: IndexSet.h:76
bool has_index(std::size_t index) const
Check whether index is in set.
Definition: IndexSet.h:58
bool empty() const
Return true if set is empty.
Definition: IndexSet.h:50
void dolfin_error(std::string location, std::string task, std::string reason,...)
Definition: log.cpp:129
~IndexSet()
Destructor.
Definition: IndexSet.h:47