forked from man-group/sparrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_value.cpp
85 lines (72 loc) · 2.34 KB
/
list_value.cpp
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
79
80
81
82
83
84
85
// Copyright 2024 Man Group Operations Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or mplied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "sparrow/layout/list_layout/list_value.hpp"
#include <iostream>
#include "sparrow/layout/dispatch.hpp"
namespace sparrow
{
list_value::list_value(const array_wrapper* flat_array, size_type index_begin, size_type index_end)
: p_flat_array(flat_array)
, m_index_begin(index_begin)
, m_index_end(index_end)
{
SPARROW_ASSERT_TRUE(index_begin <= index_end);
}
auto list_value::size() const -> size_type
{
return m_index_end - m_index_begin;
}
bool list_value::empty() const
{
return size() == 0;
}
auto list_value::operator[](size_type i) const -> const_reference
{
return array_element(*p_flat_array, m_index_begin + i);
}
auto list_value::front() const -> const_reference
{
return (*this)[0];
}
auto list_value::back() const -> const_reference
{
return (*this)[size() - 1];
}
bool operator==(const list_value& lhs, const list_value& rhs)
{
bool res = lhs.size() == rhs.size();
for (std::size_t i = 0; res && i < lhs.size(); ++i)
{
res = lhs[i] == rhs[i];
}
return res;
// TODO: refactor with the following when list_value is a range
// return std::ranges::equal(lhs, rhs);
}
}
#if defined(__cpp_lib_format)
auto std::formatter<sparrow::list_value>::format(const sparrow::list_value& list_value, std::format_context& ctx) const
-> decltype(ctx.out())
{
std::format_to(ctx.out(), "<");
if (!list_value.empty())
{
for (std::size_t i = 0; i < list_value.size() - 1; ++i)
{
std::format_to(ctx.out(), "{}, ", list_value[i]);
}
}
return std::format_to(ctx.out(), "{}>", list_value.back());
}
#endif