sparrow 2.4.0
C++20 idiomatic APIs for the Apache Arrow Columnar Format
Loading...
Searching...
No Matches
list_value.hpp
Go to the documentation of this file.
1// Copyright 2024 Man Group Operations Limited
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <ostream>
18
24
25namespace sparrow
26{
27 class array;
28
29 class list_value;
30
31 template <class L>
32 class list_reference;
33
52 list_value_iterator,
53 const array,
54 array_traits::const_reference,
55 array_traits::const_reference,
56 std::random_access_iterator_tag>
57 {
58 public:
59
63 const array,
66 std::random_access_iterator_tag>;
67 using size_type = size_t;
68
75 list_value_iterator() noexcept = default;
76
86 list_value_iterator(const array* flat_array, size_type index);
87
88 private:
89
90 [[nodiscard]] reference dereference() const;
91
92 friend class iterator_access;
93 };
94
139 {
140 public:
141
144 using size_type = std::size_t;
145 using list_value_reverse_iterator = std::reverse_iterator<list_value_iterator>;
146
154 list_value() = default;
155
170 list_value(const array* flat_array, size_type index_begin, size_type index_end);
171
180 [[nodiscard]] size_type size() const;
181
190 [[nodiscard]] bool empty() const;
191
203
213 [[nodiscard]] const_reference front() const;
214
224 [[nodiscard]] const_reference back() const;
225
234 [[nodiscard]] list_value_iterator begin();
235
244 [[nodiscard]] list_value_iterator begin() const;
245
254 [[nodiscard]] list_value_iterator cbegin() const;
255
264 [[nodiscard]] list_value_iterator end();
265
274 [[nodiscard]] list_value_iterator end() const;
275
284 [[nodiscard]] list_value_iterator cend() const;
285
295
304 [[nodiscard]] list_value_reverse_iterator rbegin() const;
305
315
325
334 [[nodiscard]] list_value_reverse_iterator rend() const;
335
344 [[nodiscard]] list_value_reverse_iterator crend() const;
345
346 private:
347
349 [[nodiscard]] const array* flat_array() const noexcept
350 {
351 return p_flat_array;
352 }
353
359 [[nodiscard]] size_type begin_index() const noexcept
360 {
361 return m_index_begin;
362 }
363
369 [[nodiscard]] size_type end_index() const noexcept
370 {
371 return m_index_end;
372 }
373
374 const array* p_flat_array = nullptr;
375 size_type m_index_begin = 0u;
376 size_type m_index_end = 0u;
377
378
379 template <bool BIG>
380 friend class list_array_impl;
381
382 template <bool BIG>
384
386
387 template <class DERIVED>
389 };
390
391 SPARROW_API bool operator==(const list_value& lhs, const list_value& rhs);
392
393 template <class L>
395 {
396 public:
397
403
404 constexpr list_reference(L* layout, size_type index)
405 : p_layout(layout)
406 , m_index(index)
407 {
408 }
409
410 constexpr list_reference(const self_type&) noexcept = default;
411 constexpr list_reference(self_type&&) noexcept = default;
412 ~list_reference() = default;
413
414 self_type& operator=(const list_value& rhs)
415 {
416 p_layout->replace_value(m_index, rhs);
417 return *this;
418 }
419
421 {
422 return operator=(static_cast<list_value>(rhs));
423 }
424
426 {
427 return operator=(static_cast<list_value>(rhs));
428 }
429
430 operator list_value() const noexcept
431 {
432 const auto [b, e] = p_layout->offset_range(m_index);
433 return list_value(p_layout->raw_flat_array(), static_cast<size_type>(b), static_cast<size_type>(e));
434 }
435
436 [[nodiscard]] size_type size() const noexcept
437 {
438 return view().size();
439 }
440
441 [[nodiscard]] bool empty() const noexcept
442 {
443 return view().empty();
444 }
445
446 [[nodiscard]] auto operator[](size_type i) const
447 {
448 return view()[i];
449 }
450
451 [[nodiscard]] auto front() const
452 {
453 return view().front();
454 }
455
456 [[nodiscard]] auto back() const
457 {
458 return view().back();
459 }
460
461 [[nodiscard]] iterator begin() const
462 {
463 return view().begin();
464 }
465
466 [[nodiscard]] iterator end() const
467 {
468 return view().end();
469 }
470
471 [[nodiscard]] iterator cbegin() const
472 {
473 return view().cbegin();
474 }
475
476 [[nodiscard]] iterator cend() const
477 {
478 return view().cend();
479 }
480
481 [[nodiscard]] bool operator==(const list_value& rhs) const
482 {
483 return view() == rhs;
484 }
485
486 [[nodiscard]] bool operator==(const self_type& rhs) const
487 {
488 return view() == rhs.view();
489 }
490
491 private:
492
493 [[nodiscard]] list_value view() const noexcept
494 {
495 return static_cast<list_value>(*this);
496 }
497
498 L* p_layout = nullptr;
499 size_type m_index = 0;
500 };
501
517 bool operator==(const list_value& lhs, const list_value& rhs);
518}
519
520#if defined(__cpp_lib_format)
521
522template <>
523struct std::formatter<sparrow::list_value>
524{
525 constexpr auto parse(std::format_parse_context& ctx) -> decltype(ctx.begin())
526 {
527 return ctx.begin(); // Simple implementation
528 }
529
530 SPARROW_API auto format(const sparrow::list_value& list_value, std::format_context& ctx) const
531 -> decltype(ctx.out());
532};
533
534namespace sparrow
535{
536 SPARROW_API std::ostream& operator<<(std::ostream& os, const list_value& value);
537}
538
539#endif
Dynamically typed array encapsulating an Arrow layout.
Definition array_api.hpp:50
constexpr list_reference(L *layout, size_type index)
size_type size() const noexcept
iterator cend() const
self_type & operator=(const self_type &rhs)
iterator begin() const
self_type & operator=(self_type &&rhs)
auto operator[](size_type i) const
iterator cbegin() const
bool operator==(const list_value &rhs) const
constexpr list_reference(const self_type &) noexcept=default
list_value_iterator const_iterator
list_value_iterator iterator
bool operator==(const self_type &rhs) const
iterator end() const
bool empty() const noexcept
list_reference< L > self_type
self_type & operator=(const list_value &rhs)
constexpr list_reference(self_type &&) noexcept=default
list_value::size_type size_type
Iterator for traversing elements within a list_value.
pointer_index_iterator_base< list_value_iterator, const array, array_traits::const_reference, array_traits::const_reference, std::random_access_iterator_tag > base_type
list_value_iterator() noexcept=default
Default constructor creating an invalid iterator.
list_value_iterator self_type
const_reference operator[](size_type i) const
Gets element at specified position without bounds checking.
friend class list_array_crtp_base
const_reference back() const
Gets reference to the last element.
list_value_iterator cbegin() const
Gets const iterator to the beginning of the list.
list_value_reverse_iterator rend()
Gets reverse iterator to the end of reversed list.
friend class fixed_sized_list_array
array_traits::value_type value_type
bool empty() const
Checks if the list is empty.
list_value_reverse_iterator rbegin() const
Gets const reverse iterator to the beginning of reversed list.
array_traits::const_reference const_reference
friend class list_array_impl
std::reverse_iterator< list_value_iterator > list_value_reverse_iterator
list_value_reverse_iterator rbegin()
Gets reverse iterator to the beginning of reversed list.
list_value()=default
Default constructor creating an empty list view.
list_value_iterator cend() const
Gets const iterator to the end of the list.
list_value(const array *flat_array, size_type index_begin, size_type index_end)
Constructs list view over specified array range.
const_reference front() const
Gets reference to the first element.
size_type size() const
Gets the number of elements in the list.
list_value_reverse_iterator crend() const
Gets const reverse iterator to the end of reversed list.
list_value_reverse_iterator rend() const
Gets const reverse iterator to the end of reversed list.
std::size_t size_type
list_value_iterator begin() const
Gets const iterator to the beginning of the list.
list_value_iterator end() const
Gets const iterator to the end of the list.
list_value_iterator begin()
Gets iterator to the beginning of the list.
friend class list_view_array_impl
list_value_iterator end()
Gets iterator to the end of the list.
list_value_reverse_iterator crbegin() const
Gets const reverse iterator to the beginning of reversed list.
Concept for layouts.
#define SPARROW_API
Definition config.hpp:38
SPARROW_API bool operator==(const array &lhs, const array &rhs)
Compares the content of two arrays.
std::ostream & operator<<(std::ostream &os, const nullval_t &)
mpl::rename< mpl::unique< mpl::transform< detail::array_const_reference_t, all_base_types_t > >, nullable_variant > const_reference
mpl::rename< mpl::transform< detail::array_value_type_t, all_base_types_t >, nullable_variant > value_type