FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Adding .flat(i) · xtensor-stack/xtensor@50e3d42 · GitHub

Repository navigation

Commit 50e3d42

Browse files
committed
Adding .flat(i)
1 parent 78aaac3 commit 50e3d42

15 files changed

Lines changed: 204 additions & 10 deletions

‎docs/source/numpy.rst‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ See :any:`numpy indexing <numpy:arrays.indexing>` page.
136136
+====================================================================+====================================================================+
137137
| ``a[3, 2]`` | ``a(3, 2)`` |
138138
+--------------------------------------------------------------------+--------------------------------------------------------------------+
139-
| :any:`a.flat[4] <numpy.ndarray.flat>` || ``a[4]`` |
140-
| || ``a(4)`` |
139+
| :any:`a.flat[4] <numpy.ndarray.flat>` | ``a.flat(4)`` |
141140
+--------------------------------------------------------------------+--------------------------------------------------------------------+
142141
| ``a[3]`` || ``xt::view(a, 3, xt::all())`` |
143142
| || ``xt::row(a, 3)`` |

‎include/xtensor/xaccessible.hpp‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ namespace xt
125125
/************************************
126126
* xconst_accessible implementation *
127127
************************************/
128-
128+
129129
/**
130130
* Returns the size of the expression.
131131
*/
@@ -211,7 +211,7 @@ namespace xt
211211
normalize_periodic(derived_cast().shape(), args...);
212212
return derived_cast()(static_cast<size_type>(args)...);
213213
}
214-
214+
215215
/**
216216
* Returns ``true`` only if the the specified position is a valid entry in the expression.
217217
* @param args a list of indices specifying the position in the expression.
@@ -246,7 +246,7 @@ namespace xt
246246
template <class D>
247247
template <class... Args>
248248
inline auto xaccessible<D>::at(Args... args) -> reference
249-
{
249+
{
250250
check_access(derived_cast().shape(), static_cast<size_type>(args)...);
251251
return derived_cast().operator()(args...);
252252
}
@@ -293,7 +293,6 @@ namespace xt
293293
return derived_cast()(static_cast<size_type>(args)...);
294294
}
295295

296-
297296
template <class D>
298297
inline auto xaccessible<D>::derived_cast() noexcept -> derived_type&
299298
{

‎include/xtensor/xcontainer.hpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ namespace xt
174174
reference data_element(size_type i);
175175
const_reference data_element(size_type i) const;
176176

177+
reference flat(size_type i);
178+
const_reference flat(size_type i) const;
179+
177180
template <class requested_type>
178181
using simd_return_type = xt_simd::simd_return_type<value_type, requested_type>;
179182

@@ -643,6 +646,18 @@ namespace xt
643646
return storage()[i];
644647
}
645648

649+
template <class D>
650+
inline auto xcontainer<D>::flat(size_type i) -> reference
651+
{
652+
return storage()[i];
653+
}
654+
655+
template <class D>
656+
inline auto xcontainer<D>::flat(size_type i) const -> const_reference
657+
{
658+
return storage()[i];
659+
}
660+
646661
/***************
647662
* stepper api *
648663
***************/

‎include/xtensor/xdynamic_view.hpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ namespace xt
181181
template <class... Args>
182182
const_reference unchecked(Args... args) const;
183183

184+
reference flat(size_type index);
185+
const_reference flat(size_type index) const;
186+
184187
using base_type::operator[];
185188
using base_type::at;
186189
using base_type::periodic;
@@ -450,6 +453,18 @@ namespace xt
450453
return base_type::storage()[static_cast<size_type>(offset)];
451454
}
452455

456+
template <class CT, class S, layout_type L, class FST>
457+
inline auto xdynamic_view<CT, S, L, FST>::flat(size_type i) -> reference
458+
{
459+
return base_type::storage()[data_offset() + i];
460+
}
461+
462+
template <class CT, class S, layout_type L, class FST>
463+
inline auto xdynamic_view<CT, S, L, FST>::flat(size_type i) const -> const_reference
464+
{
465+
return base_type::storage()[data_offset() + i];
466+
}
467+
453468
template <class CT, class S, layout_type L, class FST>
454469
template <class It>
455470
inline auto xdynamic_view<CT, S, L, FST>::element(It first, It last) -> reference

‎include/xtensor/xfunction.hpp‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,8 @@ namespace xt
307307

308308
const_reference data_element(size_type i) const;
309309

310+
const_reference flat(size_type i) const;
311+
310312
template <class UT = self_type, class = typename std::enable_if<UT::only_scalar::value>::type>
311313
operator value_type() const;
312314

@@ -583,6 +585,20 @@ namespace xt
583585
return access_impl(std::make_index_sequence<sizeof...(CT)>(), static_cast<size_type>(args)...);
584586
}
585587

588+
/**
589+
* @name Data
590+
*/
591+
/**
592+
* Returns a constant reference to the element at the specified position of the underlying
593+
* contiguous storage of the function.
594+
* @param index index to underlying flat storage.
595+
*/
596+
template <class F, class... CT>
597+
inline auto xfunction<F, CT...>::flat(size_type index) const -> const_reference
598+
{
599+
return data_element_impl(std::make_index_sequence<sizeof...(CT)>(), index);
600+
}
601+
586602
/**
587603
* Returns a constant reference to the element at the specified position in the expression.
588604
* @param args a list of indices specifying the position in the expression. Indices

‎include/xtensor/xfunctor_view.hpp‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ namespace xt
178178
using accessible_base::at;
179179
using accessible_base::operator[];
180180
using accessible_base::periodic;
181+
181182
using accessible_base::in_bounds;
182183

183184
xexpression_type& expression() noexcept;
@@ -203,6 +204,20 @@ namespace xt
203204
return m_functor(m_e.data_element(i));
204205
}
205206

207+
template <class FCT = functor_type>
208+
auto flat(size_type i)
209+
-> decltype(std::declval<FCT>()(std::declval<undecay_expression>().flat(i)))
210+
{
211+
return m_functor(m_e.flat(i));
212+
}
213+
214+
template <class FCT = functor_type>
215+
auto flat(size_type i) const
216+
-> decltype(std::declval<FCT>()(std::declval<const undecay_expression>().flat(i)))
217+
{
218+
return m_functor(m_e.flat(i));
219+
}
220+
206221
// The following functions are defined inline because otherwise signatures
207222
// don't match on GCC.
208223
template <class align, class requested_type = typename xexpression_type::value_type,
@@ -732,15 +747,15 @@ namespace xt
732747
*
733748
* @warning This method is meant for performance, for expressions with a dynamic
734749
* number of dimensions (i.e. not known at compile time). Since it may have
735-
* undefined behavior (see parameters), operator() should be prefered whenever
750+
* undefined behavior (see parameters), operator() should be preferred whenever
736751
* it is possible.
737752
* @warning This method is NOT compatible with broadcasting, meaning the following
738753
* code has undefined behavior:
739754
* \code{.cpp}
740755
* xt::xarray<double> a = {{0, 1}, {2, 3}};
741756
* xt::xarray<double> b = {0, 1};
742757
* auto fd = a + b;
743-
* double res = fd.uncheked(0, 1);
758+
* double res = fd.unchecked(0, 1);
744759
* \endcode
745760
*/
746761
template <class D>

‎include/xtensor/xmasked_view.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace xt
3737

3838
template <class CTD, class CTM>
3939
struct xcontainer_inner_types<xmasked_view<CTD, CTM>>
40-
{
40+
{
4141
using data_type = std::decay_t<CTD>;
4242
using mask_type = std::decay_t<CTM>;
4343
using base_value_type = typename data_type::value_type;

‎include/xtensor/xoptional_assembly_base.hpp‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ namespace xt
177177
template <class... Args>
178178
const_reference periodic(Args... args) const;
179179

180+
reference flat(size_type args);
181+
const_reference flat(size_type args) const;
182+
180183
template <class It>
181184
reference element(It first, It last);
182185
template <class It>
@@ -659,6 +662,28 @@ namespace xt
659662
return const_reference(value().periodic(args...), has_value().periodic(args...));
660663
}
661664

665+
/**
666+
* Returns a reference to the element at the specified position
667+
* of the underlying storage in the optional assembly.
668+
* @param index index to underlying flat storage.
669+
*/
670+
template <class D>
671+
inline auto xoptional_assembly_base<D>::flat(size_type i) -> reference
672+
{
673+
return reference(value().flat(i), has_value().flat(i));
674+
}
675+
676+
/**
677+
* Returns a constant reference to the element at the specified position
678+
* of the underlying storage in the optional assembly.
679+
* @param index index to underlying flat storage.
680+
*/
681+
template <class D>
682+
inline auto xoptional_assembly_base<D>::flat(size_type i) const -> const_reference
683+
{
684+
return const_reference(value().flat(i), has_value().flat(i));
685+
}
686+
662687
/**
663688
* Returns a reference to the element at the specified position in the optional assembly.
664689
* @param first iterator starting the sequence of indices

‎include/xtensor/xscalar.hpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ namespace xt
281281
reference data_element(size_type i) noexcept;
282282
const_reference data_element(size_type i) const noexcept;
283283

284+
reference flat(size_type i) noexcept;
285+
const_reference flat(size_type i) const noexcept;
286+
284287
template <class align, class simd = simd_value_type>
285288
void store_simd(size_type i, const simd& e);
286289
template <class align, class requested_type = value_type,
@@ -932,6 +935,18 @@ namespace xt
932935
return m_value;
933936
}
934937

938+
template <class CT>
939+
inline auto xscalar<CT>::flat(size_type) noexcept -> reference
940+
{
941+
return m_value;
942+
}
943+
944+
template <class CT>
945+
inline auto xscalar<CT>::flat(size_type) const noexcept -> const_reference
946+
{
947+
return m_value;
948+
}
949+
935950
template <class CT>
936951
template <class align, class simd>
937952
inline void xscalar<CT>::store_simd(size_type, const simd& e)

‎include/xtensor/xstrided_view.hpp‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ namespace xt
266266
reference data_element(size_type i);
267267
const_reference data_element(size_type i) const;
268268

269+
reference flat(size_type i);
270+
const_reference flat(size_type i) const;
271+
269272
using container_iterator = std::conditional_t<is_const,
270273
typename storage_type::const_iterator,
271274
typename storage_type::iterator>;
@@ -457,6 +460,17 @@ namespace xt
457460
return storage()[i];
458461
}
459462

463+
template <class CT, class S, layout_type L, class FST>
464+
inline auto xstrided_view<CT, S, L, FST>::flat(size_type i) -> reference
465+
{
466+
return storage()[i];
467+
}
468+
469+
template <class CT, class S, layout_type L, class FST>
470+
inline auto xstrided_view<CT, S, L, FST>::flat(size_type i) const -> const_reference
471+
{
472+
return storage()[i];
473+
}
460474

461475
template <class CT, class S, layout_type L, class FST>
462476
inline auto xstrided_view<CT, S, L, FST>::storage_begin() -> storage_iterator

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL