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

openmc-dev / openmc / 13546000884

26 Feb 2025 02:19PM UTC coverage: 85.015% (-0.2%) from 85.186%
13546000884

Pull #3328

github

web-flow
Merge 67a173195 into e060534ff
Pull Request #3328: NCrystal becomes runtime rather than buildtime dependency

81 of 108 new or added lines in 6 files covered. (75.0%)

467 existing lines in 21 files now uncovered.

51062 of 60062 relevant lines covered (85.02%)

32488887.43 hits per line

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

58.33
/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) {}
864✔
UNCOV
36

×
37
  /**
864✔
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
   */
UNCOV
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>>
65
  span(const std::vector<std::remove_const_t<U>>& vec)
66
    : data_(vec.data()), size_(vec.size())
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]; }
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
  /**
554✔
126
   * @brief Get a const pointer to the underlying data.
374✔
127
   * @return Const pointer to the data, or nullptr if the span is empty.
180✔
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_; }
×
136

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

143
  /**
144
   * @brief Get an iterator to the beginning of the span.
145
   * @return Iterator pointing to the first element.
180✔
146
   */
UNCOV
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_; }
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_; }
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()
185
  {
186
    if (empty()) {
187
      throw std::out_of_range("span::front(): span is empty");
188
    }
189
    return data_[0];
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

© 2026 Coveralls, Inc