GCC Code Coverage Report


Directory: ./
File: Utils/include/Utils/HelperFunctions.hpp
Date: 2022-10-15 05:10:18
Warnings: 1 unchecked decisions!
Exec Total Coverage
Lines: 17 17 100.0%
Functions: 3 3 100.0%
Branches: 24 46 52.2%
Decisions: 2 4 50.0%

Line Branch Decision Exec Source
1 // Copyright 2019-2022 Cambridge Quantum Computing
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #pragma once
16
17 #include <boost/range/adaptor/transformed.hpp>
18 #include <cstdint>
19 #include <deque>
20 #include <map>
21 #include <vector>
22
23 namespace tket {
24
25 typedef std::vector<std::deque<bool>> GrayCode;
26 /**
27 * Construct a GrayCode over `n` bits
28 * @param n bits
29 */
30 GrayCode gen_graycode(unsigned n);
31
32 /**
33 * Check equality of each element of first and second
34 * when first and second can provide begin, end iterators
35 **/
36 template <typename T>
37 158 bool check_iterators_equality(const T& first, const T& second) {
38
1/2
✓ Branch 1 taken 158 times.
✗ Branch 2 not taken.
158 auto it1 = first.begin();
39
1/2
✓ Branch 1 taken 158 times.
✗ Branch 2 not taken.
158 auto f_end = first.end();
40
41
1/2
✓ Branch 1 taken 158 times.
✗ Branch 2 not taken.
158 auto it2 = second.begin();
42
1/2
✓ Branch 1 taken 158 times.
✗ Branch 2 not taken.
158 auto s_end = second.end();
43
44
3/4
✓ Branch 1 taken 1901 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1907 times.
✓ Branch 5 taken 152 times.
0/1
? Decision couldn't be analyzed.
2059 for (; it1 != f_end; ++it1) {
45
10/20
✓ Branch 1 taken 1907 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1907 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1907 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1907 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 6 times.
✓ Branch 13 taken 1901 times.
✓ Branch 14 taken 1907 times.
✗ Branch 15 not taken.
✓ Branch 17 taken 1907 times.
✗ Branch 18 not taken.
✓ Branch 20 taken 6 times.
✓ Branch 21 taken 1901 times.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
2/2
✓ Decision 'true' taken 6 times.
✓ Decision 'false' taken 1901 times.
1907 if (it2 == s_end || !(*it1 == *it2)) {
46 6 return false;
47 }
48
1/2
✓ Branch 1 taken 1901 times.
✗ Branch 2 not taken.
1901 ++it2;
49 }
50
51 // check there were the same number of elements
52 152 return (it2 == s_end);
53 158 }
54
55 /*
56 * Convert a map-like object (e.g. <bimap>.right/<bimap>.left) to std::map
57 *
58 * This is particularly useful for bimaps. Converting a bimap to a map is
59 * slightly annoying, see
60 * https://stackoverflow.com/questions/20667187/convert-boostbimap-to-stdmap
61 */
62 template <typename MapT>
63 static std::map<
64 std::remove_const_t<typename MapT::key_type>,
65 std::remove_const_t<typename MapT::mapped_type>>
66 29417 bimap_to_map(MapT& bm) {
67 using retT = std::map<
68 std::remove_const_t<typename MapT::key_type>,
69 std::remove_const_t<typename MapT::mapped_type>>;
70 using funcT = std::function<typename retT::value_type(
71 const typename MapT::value_type&)>;
72 29417 funcT trans = [](const typename MapT::value_type& val) {
73 205841 return std::make_pair(val.first, val.second);
74 };
75
3/6
✓ Branch 1 taken 29417 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 29417 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 29417 times.
✗ Branch 8 not taken.
58834 auto it = bm | boost::adaptors::transformed(trans);
76
3/6
✓ Branch 1 taken 29417 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 29417 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 29417 times.
✗ Branch 8 not taken.
58834 return retT(it.begin(), it.end());
77 29417 }
78
79 /**
80 * Reverse bits 0,1,...,w-1 of the number v, assuming v < 2^w and w <= 32.
81 */
82 uint32_t reverse_bits(uint32_t v, unsigned w);
83
84 } // namespace tket
85