sparrow 0.3.0
Loading...
Searching...
No Matches
builder_example.cpp
Go to the documentation of this file.
1
4
5
6#include <array>
7#include <cassert>
8#include <list>
9#include <string>
10#include <tuple>
11#include <vector>
12
14
16{
18 // using initializer_list
19 auto arr = sparrow::build({1, 2, 3, 4, 5});
21 // using vector
22 std::vector<int> v{1, 2, 3, 4, 5};
23 auto arr2 = sparrow::build(v);
25 // using list
26 std::list<int> l{1, 2, 3, 4, 5};
27 auto arr3 = sparrow::build(l);
29 // using any range
30 auto iota = std::views::iota(1, 6)
31 | std::views::transform(
32 [](int i)
33 {
34 return static_cast<int>(i);
35 }
36 );
37 auto arr4 = sparrow::build(iota);
39 // all of the arrays above are equivalent to the manually built array
40 auto arr5 = sparrow::primitive_array<int>({1, 2, 3, 4, 5});
41 assert(arr == arr2);
42 assert(arr == arr3);
43 assert(arr == arr4);
44 assert(arr == arr5);
46}
47
49{
51 // using initializer_list (here we have to explicitly specify the type when using an
52 // initializer list with nulls)
55 // using vector
56 std::vector<sparrow::nullable<int>> v{1, 2, sparrow::nullval, 4, 5};
57 auto arr2 = sparrow::build(v);
59 // using list
60 std::list<sparrow::nullable<int>> l{1, 2, sparrow::nullval, 4, 5};
61 auto arr3 = sparrow::build(l);
63 // using any range
64 auto iota = std::views::iota(1, 6)
65 | std::views::transform(
66 [](int i) -> sparrow::nullable<int>
67 {
69 }
70 );
71 auto arr4 = sparrow::build(iota);
72 // all of the arrays above are equivalent to the manually built array
73 std::vector<std::size_t> where_nulls{2};
74 sparrow::u8_buffer<int> values{1, 2, 3, 4, 5};
75 auto arr5 = sparrow::primitive_array<int>(std::move(values), where_nulls);
76 assert(arr == arr2);
77 assert(arr == arr3);
78 assert(arr == arr4);
80}
81
83{
85 // [["hello", "world","!"], ["Another", "sentence"]]
86 std::vector<std::vector<std::string>> v{{"hello", "world", "!"}, {"Another", "sentence"}};
87 auto arr = sparrow::build(v);
89}
90
92{
94 // [["hello", "world","!"],NULL , ["Another", "sentence"]]
95 using string_vector = std::vector<std::string>;
96 using nullable_string_vector = sparrow::nullable<string_vector>;
97 std::vector<nullable_string_vector> v{
98 nullable_string_vector{string_vector{"hello", "world", "!"}},
99 nullable_string_vector{},
100 nullable_string_vector{string_vector{"Another", "sentence"}}
101 };
102 auto arr = sparrow::build(v);
104}
105
107{
109 /*
110 [
111 [
112 (1, 2.5),
113 (2, 3.5)
114 ],
115 [
116 (3, 5.5),
117 (5, 6.5),
118 (6, 7.5)
119 ],
120 [
121 (7, 8.5)
122 ]
123 ]
124 */
125 std::vector<std::vector<std::tuple<int, float>>> v{
126 {std::tuple<int, float>{1, 2.5f}, std::tuple<int, float>{2, 3.5f}},
127 {std::tuple<int, float>{3, 5.5f}, std::tuple<int, float>{5, 6.5f}, std::tuple<int, float>{6, 7.5f}},
128 {std::tuple<int, float>{7, 8.5f}}
129 };
130 auto arr = sparrow::build(v);
132}
133
135{
137 std::vector<std::array<std::string, 2>> v{{"hello", "world"}, {"Another", "sentence"}, {"This", "is"}};
138 auto arr = sparrow::build(v);
140}
141
143{
145 using variant_type = std::variant<int, float>;
146 using array_type = std::array<variant_type, 2>;
147 std::vector<array_type> v{{1, 2.5f}, {2, 3.5f}, {3, 4.5f}};
148 auto arr = sparrow::build(v);
150}
151
153{
155 sparrow::dict_encode<std::vector<std::string>> v{std::vector<std::string>{
156 "hello",
157 "world",
158 "hello",
159 "world",
160 "hello",
161 }};
162 auto arr = sparrow::build(v);
164}
165
167{
169 sparrow::run_end_encode<std::vector<std::string>> v{std::vector<std::string>{
170 "hello",
171 "hello",
172 "hello",
173 "world",
174 "world",
175 }};
176 auto arr = sparrow::build(v);
178}
179
181{
183 using tuple_type = std::tuple<int, std::array<std::string, 2>, sparrow::nullable<float>>;
184 std::vector<tuple_type> v{
185 {1, {"hello", "world"}, 2.5f},
186 {2, {"Another", "sentence"}, sparrow::nullval},
187 {3, {"This", "is"}, 3.5f}
188 };
189 auto arr = sparrow::build(v);
191}
192
194{
196 using variant_type = std::variant<int, std::array<std::string, 2>, sparrow::nullable<float>>;
197 std::vector<variant_type> v{
198 int{1},
199 std::array<std::string, 2>{{"A", "sentence"}},
202 };
203 auto arr = sparrow::build(v);
205}
206
void dict_encoded_variable_sized_binary()
void run_end_encoded_variable_sized_binary()
void sparse_union_array()
void list_of_strings_with_nulls()
void struct_array()
void list_of_strings()
void fixed_sized_list_strings()
void primitve_array_with_nulls()
void list_of_struct()
void primitve_array()
void fixed_sized_list_of_union()
int main()
The nullable class models a value or a reference that can be "null", or missing, like values traditio...
Definition nullable.hpp:280
constexpr nullval_t nullval(0)
auto build(T &&t, OPTION_FLAGS &&...)
function to create a sparrow array from arbitrary nested combinations of ranges, tuples,...
Definition builder.hpp:76
array_trivial_copyable< T > primitive_array
Array of values of whose type has fixed binary size.