File: test_xassign.cpp

package info (click to toggle)
xtensor 0.25.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,476 kB
  • sloc: cpp: 65,302; makefile: 202; python: 171; javascript: 8
file content (170 lines) | stat: -rw-r--r-- 3,699 bytes parent folder | download
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/***************************************************************************
 * Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht          *
 * Copyright (c) QuantStack                                                 *
 *                                                                          *
 * Distributed under the terms of the BSD 3-Clause License.                 *
 *                                                                          *
 * The full license is in the file LICENSE, distributed with this software. *
 ****************************************************************************/

#include <iterator>
#include <type_traits>
#include <vector>

#include "xtensor/xarray.hpp"
#include "xtensor/xassign.hpp"
#include "xtensor/xnoalias.hpp"
#include "xtensor/xtensor.hpp"

#include "test_common.hpp"
#include "test_common_macros.hpp"

// a dummy shape *not derived* from std::vector but compatible
template <class T>
class my_vector
{
private:

    using vector_type = std::vector<T>;

public:

    using value_type = T;
    using size_type = typename vector_type::size_type;

    template <class U>
    my_vector(std::initializer_list<U> vals)
        : m_data(vals.begin(), vals.end())
    {
    }

    my_vector(const std::size_t size = 0, const T& val = T())
        : m_data(size, val)
    {
    }

    auto resize(const std::size_t size)
    {
        return m_data.resize(size);
    }

    auto size() const
    {
        return m_data.size();
    }

    auto cend() const
    {
        return m_data.cend();
    }

    auto cbegin() const
    {
        return m_data.cbegin();
    }

    auto end()
    {
        return m_data.end();
    }

    auto end() const
    {
        return m_data.end();
    }

    auto begin()
    {
        return m_data.begin();
    }

    auto begin() const
    {
        return m_data.begin();
    }

    auto rbegin() const
    {
        return std::make_reverse_iterator(end());
    }

    auto rend() const
    {
        return std::make_reverse_iterator(begin());
    }

    auto empty() const
    {
        return m_data.empty();
    }

    auto& back()
    {
        return m_data.back();
    }

    const auto& back() const
    {
        return m_data.back();
    }

    auto& front()
    {
        return m_data.front();
    }

    const auto& front() const
    {
        return m_data.front();
    }

    auto& operator[](const std::size_t i)
    {
        return m_data[i];
    }

    const auto& operator[](const std::size_t i) const
    {
        return m_data[i];
    }

private:

    std::vector<T> m_data;
};

namespace xt
{

    template <class T, class C_T>
    struct rebind_container<T, my_vector<C_T>>
    {
        using type = my_vector<T>;
    };

    TEST(xassign, mix_shape_types)
    {
        {
            // xarray like with custom shape
            using my_xarray = xt::xarray_container<std::vector<int>, xt::layout_type::row_major, my_vector<std::size_t>>;

            auto a = my_xarray::from_shape({1, 3});
            auto b = xt::xtensor<int, 2>::from_shape({2, 3});
            xt::noalias(a) += b;
            EXPECT_EQ(a.dimension(), 2);
            EXPECT_EQ(a.shape(0), 2);
            EXPECT_EQ(a.shape(1), 3);
        }
        {
            // xarray like with custom shape
            using my_xarray = xt::xarray_container<std::vector<int>, xt::layout_type::row_major, my_vector<std::size_t>>;

            auto a = my_xarray::from_shape({3});
            auto b = xt::xtensor<int, 2>::from_shape({2, 3});
            xt::noalias(a) += b;
            EXPECT_EQ(a.dimension(), 2);
            EXPECT_EQ(a.shape(0), 2);
            EXPECT_EQ(a.shape(1), 3);
        }
    }
}