• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openmc-dev / openmc / 14840340664

05 May 2025 03:38PM UTC coverage: 85.195% (-0.009%) from 85.204%
14840340664

Pull #3392

github

web-flow
Merge 5bc1ec35f into 1e7d8324e
Pull Request #3392: Map Compton subshell data to atomic relaxation data

14 of 14 new or added lines in 2 files covered. (100.0%)

330 existing lines in 19 files now uncovered.

52194 of 61264 relevant lines covered (85.2%)

37398320.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

61.54
/include/openmc/span.h
1
#ifndef OPENMC_SPAN_H
2
#define OPENMC_SPAN_H
3
#include <cstddef>   // for std::size_t, std::ptrdiff_t
4
#include <iterator>  // for std::begin, std::end
5
#include <stdexcept> // for std::out_of_range
6
#include <type_traits>
7

8
#include "openmc/vector.h"
9

10
namespace openmc {
11

12
template<typename T>
13
class span {
14
public:
15
  using value_type = T;
16
  using pointer = T*;
17
  using const_pointer = const T*;
18
  using reference = T&;
19
  using const_reference = const T&;
20
  using iterator = T*;
21
  using const_iterator = const T*;
22
  using size_type = std::size_t;
23
  using difference_type = std::ptrdiff_t;
24

25
  /**
26
   * @brief Default constructor for an empty span.
27
   */
28
  span() noexcept : data_(nullptr), size_(0) {}
29

30
  /**
31
   * @brief Constructs a span from a pointer and size.
32
   * @param ptr Pointer to the first element.
33
   * @param count Number of elements in the span.
34
   */
35
  span(pointer ptr, size_type count) : data_(ptr), size_(count) {}
36

37
  /**
38
   * @brief Constructs a span from two pointers marking the span range.
39
   * @param first Pointer to the first element.
40
   * @param last Pointer past the last element.
41
   * @throws std::out_of_range if last < first.
42
   */
43
  span(pointer first, pointer last) : data_(first), size_(last - first)
44
  {
45
    if (last < first) {
46
      throw std::out_of_range("span: last pointer is before first pointer");
47
    }
48
  }
49

50
  /**
51
   * @brief Constructs a span from a non-const std::vector.
52
   * @param vec Reference to the vector to create a span from.
53
   */
54
  span(std::vector<T>& vec) : data_(vec.data()), size_(vec.size()) {}
55

56
  /**
57
   * @brief Constructs a span from a const std::vector.
58
   *
59
   * This is handling the semantics that a span<const double> is used
60
   * for read-only access into a vector.
61
   * @param vec Reference to the const vector to create a span from.
62
   */
63
  template<typename U = T,
64
    typename = std::enable_if_t<std::is_same<U, const T>::value>>
UNCOV
65
  span(const std::vector<std::remove_const_t<U>>& vec)
×
UNCOV
66
    : data_(vec.data()), size_(vec.size())
×
UNCOV
67
  {}
×
68

69
  /**
70
   * @brief Constructs a read-only span<const T> from a non-const span<T>.
71
   */
72
  template<typename U = T, typename = std::enable_if_t<std::is_const<U>::value>>
73
  span(const span<std::remove_const_t<U>>& other) noexcept
74
    : data_(other.data()), size_(other.size())
75
  {}
76

77
  /**
78
   * @brief Access an element without bounds checking.
79
   * @param index Index of the element to access.
80
   * @return Reference to the accessed element.
81
   */
82
  reference operator[](size_type index) { return data_[index]; }
33,698,133✔
83

84
  /**
85
   * @brief Access an element without bounds checking (const version).
86
   * @param index Index of the element to access.
87
   * @return Const reference to the accessed element.
88
   */
89
  const_reference operator[](size_type index) const { return data_[index]; }
90

91
  /**
92
   * @brief Access an element with bounds checking.
93
   * @param index Index of the element to access.
94
   * @return Reference to the accessed element.
95
   * @throws std::out_of_range if index is out of range.
96
   */
97
  reference at(size_type index)
98
  {
99
    if (index >= size_) {
100
      throw std::out_of_range("span: index out of range");
101
    }
102
    return data_[index];
103
  }
104

105
  /**
106
   * @brief Access an element with bounds checking (const version).
107
   * @param index Index of the element to access.
108
   * @return Const reference to the accessed element.
109
   * @throws std::out_of_range if index is out of range.
110
   */
111
  const_reference at(size_type index) const
112
  {
113
    if (index >= size_) {
114
      throw std::out_of_range("span: index out of range");
115
    }
116
    return data_[index];
117
  }
118

119
  /**
120
   * @brief Get a pointer to the underlying data.
121
   * @return Pointer to the data, or nullptr if the span is empty.
122
   */
123
  pointer data() noexcept { return data_; }
124

125
  /**
126
   * @brief Get a const pointer to the underlying data.
127
   * @return Const pointer to the data, or nullptr if the span is empty.
128
   */
129
  const_pointer data() const noexcept { return data_; }
130

131
  /**
132
   * @brief Get the number of elements in the span.
133
   * @return The size of the span.
134
   */
135
  size_type size() const noexcept { return size_; }
864,245✔
136

137
  /**
138
   * @brief Check if the span is empty.
139
   * @return True if the span is empty, false otherwise.
140
   */
141
  bool empty() const noexcept { return size_ == 0; }
748✔
142

143
  /**
144
   * @brief Get an iterator to the beginning of the span.
145
   * @return Iterator pointing to the first element.
146
   */
147
  iterator begin() noexcept { return data_; }
148

149
  /**
150
   * @brief Get a const iterator to the beginning of the span.
151
   * @return Const iterator pointing to the first element.
152
   */
153
  const_iterator begin() const noexcept { return data_; }
154

155
  /**
156
   * @brief Get a const iterator to the beginning of the span.
157
   * @return Const iterator pointing to the first element.
158
   */
159
  const_iterator cbegin() const noexcept { return data_; }
142✔
160

161
  /**
162
   * @brief Get an iterator to the end of the span.
163
   * @return Iterator pointing past the last element.
164
   */
165
  iterator end() noexcept { return data_ + size_; }
×
166

167
  /**
168
   * @brief Get a const iterator to the end of the span.
169
   * @return Const iterator pointing past the last element.
170
   */
171
  const_iterator end() const noexcept { return data_ + size_; }
172

173
  /**
174
   * @brief Get a const iterator to the end of the span.
175
   * @return Const iterator pointing past the last element.
176
   */
177
  const_iterator cend() const noexcept { return data_ + size_; }
142✔
178

179
  /**
180
   * @brief Access the first element.
181
   * @return Reference to the first element.
182
   * @throws std::out_of_range if the span is empty.
183
   */
184
  reference front()
748✔
185
  {
186
    if (empty()) {
748✔
187
      throw std::out_of_range("span::front(): span is empty");
×
188
    }
189
    return data_[0];
748✔
190
  }
191

192
  /**
193
   * @brief Access the first element (const version).
194
   * @return Const reference to the first element.
195
   * @throws std::out_of_range if the span is empty.
196
   */
197
  const_reference front() const
198
  {
199
    if (empty()) {
200
      throw std::out_of_range("span::front(): span is empty");
201
    }
202
    return data_[0];
203
  }
204

205
  /**
206
   * @brief Access the last element.
207
   * @return Reference to the last element.
208
   * @throws std::out_of_range if the span is empty.
209
   */
210
  reference back()
211
  {
212
    if (empty()) {
213
      throw std::out_of_range("span::back(): span is empty");
214
    }
215
    return data_[size_ - 1];
216
  }
217

218
  /**
219
   * @brief Access the last element (const version).
220
   * @return Const reference to the last element.
221
   * @throws std::out_of_range if the span is empty.
222
   */
223
  const_reference back() const
224
  {
225
    if (empty()) {
226
      throw std::out_of_range("span::back(): span is empty");
227
    }
228
    return data_[size_ - 1];
229
  }
230

231
private:
232
  pointer data_;
233
  size_type size_;
234
};
235

236
} // namespace openmc
237
#endif // OPENMC_SPAN_H
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc