Directory: | ./ |
---|---|
File: | src/sot/flags.cpp |
Date: | 2024-11-13 12:35:17 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 47 | 81 | 58.0% |
Branches: | 32 | 96 | 33.3% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Copyright 2010, | ||
3 | * François Bleibel, | ||
4 | * Olivier Stasse, | ||
5 | * | ||
6 | * CNRS/AIST | ||
7 | * | ||
8 | */ | ||
9 | |||
10 | /* --------------------------------------------------------------------- */ | ||
11 | /* --- INCLUDE --------------------------------------------------------- */ | ||
12 | /* --------------------------------------------------------------------- */ | ||
13 | |||
14 | /*! System framework */ | ||
15 | #include <stdlib.h> | ||
16 | |||
17 | #include <list> | ||
18 | |||
19 | /*! Local Framework */ | ||
20 | #include <sot/core/debug.hh> | ||
21 | #include <sot/core/flags.hh> | ||
22 | |||
23 | using namespace std; | ||
24 | using namespace dynamicgraph::sot; | ||
25 | |||
26 | /* --------------------------------------------------------------------- */ | ||
27 | /* --- CLASS ----------------------------------------------------------- */ | ||
28 | /* --------------------------------------------------------------------- */ | ||
29 | |||
30 | 74 | Flags::Flags(const bool &b) : flags(), outOfRangeFlag(b) {} | |
31 | |||
32 | ✗ | Flags::Flags(const char *_flags) | |
33 | ✗ | : flags(strlen(_flags)), outOfRangeFlag(false) { | |
34 | ✗ | for (unsigned int i = 0; i < flags.size(); ++i) { | |
35 | ✗ | switch (_flags[i]) { | |
36 | ✗ | case '0': | |
37 | ✗ | flags[i] = false; | |
38 | ✗ | break; | |
39 | ✗ | case '1': | |
40 | ✗ | flags[i] = true; | |
41 | ✗ | break; | |
42 | ✗ | case ' ': | |
43 | ✗ | break; | |
44 | ✗ | default: | |
45 | ✗ | throw std::invalid_argument("Could not parse input string " + | |
46 | ✗ | std::string(_flags) + ". Expected 0 or 1."); | |
47 | } | ||
48 | } | ||
49 | } | ||
50 | |||
51 | ✗ | Flags::Flags(const std::vector<bool> &_flags) | |
52 | ✗ | : flags(_flags), outOfRangeFlag(false) {} | |
53 | |||
54 | 4 | Flags::operator bool(void) const { | |
55 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
|
4 | if (outOfRangeFlag) return true; |
56 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | for (unsigned int i = 0; i < flags.size(); ++i) |
57 | ✗ | if (flags[i]) return true; | |
58 | 1 | return false; | |
59 | } | ||
60 | |||
61 | /* --------------------------------------------------------------------- */ | ||
62 | |||
63 | 4800 | bool Flags::operator()(const int &i) const { | |
64 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4800 times.
|
4800 | if (i < (int)flags.size()) return flags[i]; |
65 | 4800 | return outOfRangeFlag; | |
66 | } | ||
67 | |||
68 | /* --------------------------------------------------------------------- */ | ||
69 | ✗ | void Flags::add(const bool &b) { flags.push_back(b); } | |
70 | |||
71 | /* --------------------------------------------------------------------- */ | ||
72 | ✗ | void Flags::set(const unsigned int &idx) { | |
73 | ✗ | if (idx < flags.size()) flags[idx] = true; | |
74 | } | ||
75 | |||
76 | ✗ | void Flags::unset(const unsigned int &idx) { | |
77 | ✗ | if (idx < flags.size()) flags[idx] = false; | |
78 | } | ||
79 | |||
80 | namespace dynamicgraph { | ||
81 | namespace sot { | ||
82 | |||
83 | /* --------------------------------------------------------------------- */ | ||
84 | 5 | Flags Flags::operator!(void) const { | |
85 | 5 | Flags res = *this; | |
86 | 5 | res.flags.flip(); | |
87 | 5 | res.outOfRangeFlag = !outOfRangeFlag; | |
88 | 5 | return res; | |
89 | } | ||
90 | |||
91 | 7 | Flags operator&(const Flags &f1, const Flags &f2) { | |
92 | 7 | Flags res = f1; | |
93 |
1/2✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
|
7 | res &= f2; |
94 | 7 | return res; | |
95 | } | ||
96 | |||
97 | 3 | Flags operator|(const Flags &f1, const Flags &f2) { | |
98 | 3 | Flags res = f1; | |
99 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | res |= f2; |
100 | 3 | return res; | |
101 | } | ||
102 | |||
103 | 7 | Flags &Flags::operator&=(const Flags &f2) { | |
104 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
|
7 | if (f2.flags.size() > flags.size()) |
105 | ✗ | flags.resize(f2.flags.size(), outOfRangeFlag); | |
106 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
|
7 | for (unsigned int i = 0; i < f2.flags.size(); ++i) |
107 | ✗ | flags[i] = flags[i] & f2.flags[i]; | |
108 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
|
7 | for (auto i = f2.flags.size(); i < flags.size(); ++i) |
109 | ✗ | flags[i] = flags[i] & f2.outOfRangeFlag; | |
110 |
4/4✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1 times.
|
7 | outOfRangeFlag = outOfRangeFlag && f2.outOfRangeFlag; |
111 | 7 | return *this; | |
112 | } | ||
113 | |||
114 | 3 | Flags &Flags::operator|=(const Flags &f2) { | |
115 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
|
3 | if (f2.flags.size() > flags.size()) |
116 | ✗ | flags.resize(f2.flags.size(), outOfRangeFlag); | |
117 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | for (unsigned int i = 0; i < f2.flags.size(); ++i) |
118 | ✗ | flags[i] = flags[i] | f2.flags[i]; | |
119 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
|
3 | for (auto i = f2.flags.size(); i < flags.size(); ++i) |
120 | ✗ | flags[i] = flags[i] | f2.outOfRangeFlag; | |
121 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
3 | outOfRangeFlag = outOfRangeFlag || f2.outOfRangeFlag; |
122 | 3 | return *this; | |
123 | } | ||
124 | |||
125 | /* --------------------------------------------------------------------- */ | ||
126 | 14 | std::ostream &operator<<(std::ostream &os, const Flags &fl) { | |
127 |
5/6✓ Branch 3 taken 2 times.
✓ Branch 4 taken 3 times.
✓ Branch 6 taken 5 times.
✗ Branch 7 not taken.
✓ Branch 10 taken 5 times.
✓ Branch 11 taken 14 times.
|
19 | for (auto f : fl.flags) os << (f ? '1' : '0'); |
128 | 14 | return os; | |
129 | } | ||
130 | |||
131 | 1 | std::istream &operator>>(std::istream &is, Flags &fl) { | |
132 | char c; | ||
133 | 1 | fl.flags.clear(); | |
134 |
4/6✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 5 times.
✓ Branch 7 taken 1 times.
|
6 | while (is.get(c).good()) { |
135 |
2/4✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
5 | switch (c) { |
136 | 3 | case '0': | |
137 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | fl.flags.push_back(false); |
138 | 3 | break; | |
139 | 2 | case '1': | |
140 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | fl.flags.push_back(true); |
141 | 2 | break; | |
142 | ✗ | case ' ': | |
143 | ✗ | break; | |
144 | ✗ | default: | |
145 | ✗ | throw std::invalid_argument("Could not parse input character " + | |
146 | ✗ | std::string(1, c) + ". Expected 0 or 1."); | |
147 | } | ||
148 | } | ||
149 | 1 | return is; | |
150 | } | ||
151 | |||
152 | } /* namespace sot */ | ||
153 | } /* namespace dynamicgraph */ | ||
154 |