-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathspan_basic_usage.cpp
More file actions
78 lines (68 loc) · 2.73 KB
/
Copy pathspan_basic_usage.cpp
File metadata and controls
78 lines (68 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// Demonstrates basic beman::span usage: construction from C-arrays, std::array,
// std::vector, element access, and iteration.
#include <beman/span/span.hpp>
#include <array>
#include <iostream>
#include <numeric>
#include <vector>
// Print every element of a span — accepts any element type and extent.
template <class T, std::size_t E>
void print_span(beman::span::span<T, E> s) {
std::cout << "[";
for (std::size_t i = 0; i < s.size(); ++i) {
if (i)
std::cout << ", ";
std::cout << s[i];
}
std::cout << "] (size=" << s.size() << ")\n";
}
// Returns the sum of all elements in a span.
template <class T, std::size_t E>
T sum(beman::span::span<T, E> s) {
T total{};
for (const auto& v : s)
total += v;
return total;
}
int main() {
// ----------------------------------------------------------------
// 1. Construct from a C-array (fixed extent deduced)
// ----------------------------------------------------------------
int c_arr[] = {10, 20, 30, 40, 50};
beman::span::span s1(c_arr); // span<int, 5>
std::cout << "From C-array: ";
print_span(s1);
// ----------------------------------------------------------------
// 2. Construct from std::array
// ----------------------------------------------------------------
std::array<int, 4> std_arr = {1, 2, 3, 4};
beman::span::span s2(std_arr); // span<int, 4>
std::cout << "From std::array: ";
print_span(s2);
// ----------------------------------------------------------------
// 3. Construct from std::vector (dynamic extent)
// ----------------------------------------------------------------
std::vector<int> vec(6);
std::iota(vec.begin(), vec.end(), 1); // {1,2,3,4,5,6}
beman::span::span<int> s3(vec);
std::cout << "From vector: ";
print_span(s3);
// ----------------------------------------------------------------
// 4. Element access
// ----------------------------------------------------------------
std::cout << "front=" << s3.front() << " back=" << s3.back() << "\n";
// ----------------------------------------------------------------
// 5. Modify through span
// ----------------------------------------------------------------
for (auto& v : s3)
v *= 2;
std::cout << "After *2: ";
print_span(s3);
std::cout << "Sum: " << sum(s3) << "\n";
// ----------------------------------------------------------------
// 6. size_bytes
// ----------------------------------------------------------------
std::cout << "size_bytes=" << s3.size_bytes() << " (=" << s3.size() << " * " << sizeof(int) << ")\n";
}