Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-PLACET committed Jan 10, 2025
1 parent 609a659 commit d741f5b
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 109 deletions.
4 changes: 2 additions & 2 deletions include/sparrow/layout/primitive_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,9 @@ namespace sparrow
friend base_type::base_type::base_type;
};

/*******************************************
/**********************************
* primitive_array implementation *
*******************************************/
**********************************/

namespace detail
{
Expand Down
78 changes: 0 additions & 78 deletions include/sparrow/utils/format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@
#pragma once

#include <algorithm>
#include <array>
#include <cstddef>
#include <format>
#include <numeric>
#include <ranges>
#include <string>
#include <type_traits>
#include <variant>
#include <vector>

Expand Down Expand Up @@ -58,82 +56,6 @@ namespace std

std::string m_format_string = "{:";
};

#if !defined(__cpp_lib_format_ranges)
template <typename T, std::size_t Extent>
struct formatter<std::array<T, Extent>>
{
private:

char delimiter = ',';
char opening = '[';
char closing = ']';
bool compact = false;

public:

constexpr auto parse(std::format_parse_context& ctx)
{
auto it = ctx.begin();
auto end = ctx.end();

// Parse format specifiers
while (it != end && *it != '}')
{
switch (*it)
{
case 'c': // compact mode
compact = true;
break;
case '(': // use parentheses
opening = '(';
closing = ')';
break;
case '{': // use curly braces
opening = '{';
closing = '}';
break;
case ';': // use semicolon as delimiter
delimiter = ';';
break;
default:
break;
}
++it;
}
return it;
}

auto format(const std::array<T, Extent>& array, std::format_context& ctx) const
{
auto out = ctx.out();

// Output opening bracket
*out++ = opening;

bool first = true;
for (const auto& elem : array)
{
if (!first)
{
*out++ = delimiter;
if (!compact)
{
*out++ = ' ';
}
}
first = false;

// Format each element using its own formatter
out = std::format_to(out, "{}", elem);
}

// Output closing bracket
*out++ = closing;
return out;
}
};
#endif
}

namespace sparrow
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ else()
test_null_array.cpp
test_nullable.cpp
test_primitive_array.cpp
test_ranges.cpp
test_record_batch.cpp
test_repeat_container.cpp
test_run_end_encoded_array.cpp
Expand Down
18 changes: 3 additions & 15 deletions test/test_fixed_width_binary_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,11 @@

namespace sparrow
{
template <typename T>
class my_class
{
std::array<T, 3> data;
T my_value;
};

template <typename T>
class my_incompatible_class
{
std::vector<T> data;
};

TEST_SUITE("fixed_width_binary_array")
{
auto make_array = [](size_t count, size_t offset = 0
) -> std::pair<fixed_width_binary_array, std::vector<std::array<byte_t, 3>>>
static const auto make_array =
[](size_t count,
size_t offset = 0) -> std::pair<fixed_width_binary_array, std::vector<std::array<byte_t, 3>>>
{
std::vector<std::array<byte_t, 3>> input_values;
input_values.reserve(count);
Expand Down
95 changes: 95 additions & 0 deletions test/test_ranges.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// 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 implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <array>
#include <vector>

#include "sparrow/utils/ranges.hpp"

#include "doctest/doctest.h"

namespace sparrow
{
TEST_SUITE("ranges")
{
TEST_CASE("range_size")
{
SUBCASE("for sized_range")
{
std::vector<int> v{1, 2, 3, 4, 5};
CHECK_EQ(range_size(v), 5);
}

SUBCASE("for non-sized_range")
{
std::vector<int> v{1, 2, 3, 4, 5};
CHECK_EQ(
range_size(
v
| std::views::filter(
[](int i)
{
return i % 2 == 0;
}
)
),
2
);
}

SUBCASE("for empty range")
{
std::vector<int> v;
CHECK_EQ(range_size(v), 0);
}
}

TEST_CASE("all_same_size")
{
SUBCASE("for std::array")
{
std::vector<std::array<int, 3>> v{
{std::array<int, 3>{1, 2, 3}, std::array<int, 3>{4, 5, 6}, std::array<int, 3>{7, 8, 9}}
};
CHECK(all_same_size(v));
}

SUBCASE("for std::vector")
{
std::vector<std::vector<int>> v{
std::vector<int>{1, 2, 3},
std::vector<int>{4, 5, 6},
std::vector<int>{7, 8, 9}
};
CHECK(all_same_size(v));
}

SUBCASE("for std::vector with different sizes")
{
std::vector<std::vector<int>> v{
std::vector<int>{1, 2, 3},
std::vector<int>{4, 5, 6, 7},
std::vector<int>{8, 9}
};
CHECK_FALSE(all_same_size(v));
}

SUBCASE("for empty range")
{
std::vector<std::vector<int>> v;
CHECK(all_same_size(v));
}
}
}
}
28 changes: 14 additions & 14 deletions test/test_traits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@
/////////////////////////////////////////////////////////////////////////////////////////
// Opt-in support for custom C++ representations of arrow data types.

// struct MyDataType
// {
// };

// template <>
// struct sparrow::arrow_traits<MyDataType>
// {
// static constexpr data_type type_id = sparrow::data_type::INT32;
// using value_type = MyDataType;
// using default_layout = sparrow::fixed_width_binary_array<MyDataType>;
// };

// static_assert(sparrow::is_arrow_traits<sparrow::arrow_traits<MyDataType>>);
// static_assert(sparrow::any_arrow_type<MyDataType>);
struct MyDataType
{
};

template <>
struct sparrow::arrow_traits<MyDataType>
{
static constexpr data_type type_id = sparrow::data_type::FIXED_WIDTH_BINARY;
using value_type = MyDataType;
using default_layout = sparrow::fixed_width_binary_array;
};

static_assert(sparrow::is_arrow_traits<sparrow::arrow_traits<MyDataType>>);
static_assert(sparrow::any_arrow_type<MyDataType>);

//////////////////////////////////////////////////////////////////////////////////////////
// Base arrow types representations support tests and concept checking.
Expand Down

0 comments on commit d741f5b

Please sign in to comment.