fmatvec  0.0.0
indices.h
1 /* Copyright (C) 2022 Martin Förg
2 
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16  *
17  * Contact:
18  * martin.o.foerg@googlemail.com
19  *
20  */
21 
22 #ifndef indices_h
23 #define indices_h
24 
25 #include <cassert>
26 #include <utility>
27 #include <vector>
28 
29 namespace fmatvec {
30 
31  class Indices {
32  public:
33  Indices() = default;
34  Indices(int size) { ind.resize(size); }
35  Indices(std::vector<int> ind_) : ind(std::move(ind_)) {
36  assert(min()>=0);
37  }
38  Indices(std::initializer_list<int> il) : ind(il) {
39  assert(min()>=0);
40  }
41  int size() const { return ind.size(); }
42  int max() const {
43  assert(ind.size());
44  int m = ind[0];
45  for(size_t i=1; i<ind.size(); i++) {
46  if(ind[i]>m)
47  m = ind[i];
48  }
49  return m;
50  }
51  int min() const {
52  assert(ind.size());
53  int m = ind[0];
54  for(size_t i=1; i<ind.size(); i++) {
55  if(ind[i]<m)
56  m = ind[i];
57  }
58  return m;
59  }
60  void add(int ind_) {
61  assert(ind_>=0);
62  ind.push_back(ind_);
63  }
64  void set(int i, int ind_) {
65  assert(ind_>=0);
66  ind[i] = ind_;
67  }
68  const int& operator[](int i) const { return ind[i]; }
69  private:
70  std::vector<int> ind;
71  };
72 
73 }
74 
75 #endif
Definition: indices.h:31
Namespace fmatvec.
Definition: _memory.cc:28