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 #include <climits>
29
30 namespace fmatvec {
31
32 class Indices {
33 public:
34 Indices() = default;
35 Indices(int size) { ind.resize(size); }
36 Indices(std::vector<int> ind_) : ind(std::move(ind_)) {
37 assert(min()>=0);
38 }
39 Indices(const std::initializer_list<int> &il) : ind(il) {
40 assert(min()>=0);
41 }
42 size_t size() const { return ind.size(); }
43 int max() const {
44 if(not ind.size())
45 return -1;
46 int m = ind[0];
47 for(size_t i=1; i<ind.size(); i++) {
48 if(ind[i]>m)
49 m = ind[i];
50 }
51 return m;
52 }
53 int min() const {
54 if(not ind.size())
55 return INT_MAX;
56 int m = ind[0];
57 for(size_t i=1; i<ind.size(); i++) {
58 if(ind[i]<m)
59 m = ind[i];
60 }
61 return m;
62 }
63 void add(int ind_) {
64 assert(ind_>=0);
65 ind.push_back(ind_);
66 }
67 void set(int i, int ind_) {
68 assert(ind_>=0);
69 ind[i] = ind_;
70 }
71 const int& operator[](int i) const { return ind[i]; }
72 private:
73 std::vector<int> ind;
74 };
75
76 }
77
78 #endif
fmatvec::Indices
Definition: indices.h:32
fmatvec
Namespace fmatvec.
Definition: _memory.cc:28