sparrow ..
Loading...
Searching...
No Matches
builder_example.cpp
Go to the documentation of this file.
1#include <array>
2#include <cassert>
3#include <list>
4#include <string>
5#include <tuple>
6#include <vector>
7
8#include <sparrow/builder.hpp>
9
11{
13 // using initializer_list
14 auto arr = sparrow::build({1, 2, 3, 4, 5});
16 // using vector
17 std::vector<int> v{1, 2, 3, 4, 5};
18 auto arr2 = sparrow::build(v);
20 // using list
21 std::list<int> l{1, 2, 3, 4, 5};
22 auto arr3 = sparrow::build(l);
24 // using any range
25 auto iota = std::views::iota(1, 6)
26 | std::views::transform(
27 [](int i)
28 {
29 return static_cast<int>(i);
30 }
31 );
32 auto arr4 = sparrow::build(iota);
34 // all of the arrays above are equivalent to the manually built array
35 auto arr5 = sparrow::primitive_array<int>({1, 2, 3, 4, 5});
36 assert(arr == arr2);
37 assert(arr == arr3);
38 assert(arr == arr4);
39 assert(arr == arr5);
41}
42
44{
46 // using initializer_list (here we have to explicitly specify the type when using an
47 // initializer list with nulls)
50 // using vector
51 std::vector<sparrow::nullable<int>> v{1, 2, sparrow::nullval, 4, 5};
52 auto arr2 = sparrow::build(v);
54 // using list
55 std::list<sparrow::nullable<int>> l{1, 2, sparrow::nullval, 4, 5};
56 auto arr3 = sparrow::build(l);
58 // using any range
59 auto iota = std::views::iota(1, 6)
60 | std::views::transform(
61 [](int i) -> sparrow::nullable<int>
62 {
64 }
65 );
66 auto arr4 = sparrow::build(iota);
67 // all of the arrays above are equivalent to the manually built array
68 std::vector<std::size_t> where_nulls{2};
69 sparrow::u8_buffer<int> values{1, 2, 3, 4, 5};
70 auto arr5 = sparrow::primitive_array<int>(std::move(values), where_nulls);
71 assert(arr == arr2);
72 assert(arr == arr3);
73 assert(arr == arr4);
75}
76
78{
80 // [["hello", "world","!"], ["Another", "sentence"]]
81 std::vector<std::vector<std::string>> v{{"hello", "world", "!"}, {"Another", "sentence"}};
82 auto arr = sparrow::build(v);
84}
85
87{
89 // [["hello", "world","!"],NULL , ["Another", "sentence"]]
90 using string_vector = std::vector<std::string>;
91 using nullable_string_vector = sparrow::nullable<string_vector>;
92 std::vector<nullable_string_vector> v{
93 nullable_string_vector{string_vector{"hello", "world", "!"}},
94 nullable_string_vector{},
95 nullable_string_vector{string_vector{"Another", "sentence"}}
96 };
97 auto arr = sparrow::build(v);
99}
100
102{
104 /*
105 [
106 [
107 (1, 2.5),
108 (2, 3.5)
109 ],
110 [
111 (3, 5.5),
112 (5, 6.5),
113 (6, 7.5)
114 ],
115 [
116 (7, 8.5)
117 ]
118 ]
119 */
120 std::vector<std::vector<std::tuple<int, float>>> v{
121 {std::tuple<int, float>{1, 2.5f}, std::tuple<int, float>{2, 3.5f}},
122 {std::tuple<int, float>{3, 5.5f}, std::tuple<int, float>{5, 6.5f}, std::tuple<int, float>{6, 7.5f}},
123 {std::tuple<int, float>{7, 8.5f}}
124 };
125 auto arr = sparrow::build(v);
127}
128
130{
132 std::vector<std::array<std::string, 2>> v{{"hello", "world"}, {"Another", "sentence"}, {"This", "is"}};
133 auto arr = sparrow::build(v);
135}
136
138{
140 using variant_type = std::variant<int, float>;
141 using array_type = std::array<variant_type, 2>;
142 std::vector<array_type> v{{1, 2.5f}, {2, 3.5f}, {3, 4.5f}};
143 auto arr = sparrow::build(v);
145}
146
148{
150 sparrow::dict_encode<std::vector<std::string>> v{std::vector<std::string>{
151 "hello",
152 "world",
153 "hello",
154 "world",
155 "hello",
156 }};
157 auto arr = sparrow::build(v);
159}
160
162{
164 sparrow::run_end_encode<std::vector<std::string>> v{std::vector<std::string>{
165 "hello",
166 "hello",
167 "hello",
168 "world",
169 "world",
170 }};
171 auto arr = sparrow::build(v);
173}
174
176{
178 using tuple_type = std::tuple<int, std::array<std::string, 2>, sparrow::nullable<float>>;
179 std::vector<tuple_type> v{
180 {1, {"hello", "world"}, 2.5f},
181 {2, {"Another", "sentence"}, sparrow::nullval},
182 {3, {"This", "is"}, 3.5f}
183 };
184 auto arr = sparrow::build(v);
186}
187
189{
191 using variant_type = std::variant<int, std::array<std::string, 2>, sparrow::nullable<float>>;
192 std::vector<variant_type> v{
193 int{1},
194 std::array<std::string, 2>{{"A", "sentence"}},
197 };
198 auto arr = sparrow::build(v);
200}
201
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()
This buffer class is used as storage buffer for all sparrow arrays.
constexpr auto build(T &&t, OPTION_FLAGS &&...)
function to create a sparrow array from arbitrary nested combinations of ranges, tuples,...
Definition builder.hpp:80
constexpr nullval_t nullval(0)
primitive_array_impl< T > primitive_array
Array of values of whose type has fixed binary size.