54 template<
typename dtype>
58 return insert(arr, index, values);
72 template<
typename dtype>
84 else if (index >
static_cast<int32>(arr.
size()))
89 const auto valuesSlice =
Slice(index, index + values.
size());
91 result.put(valuesSlice, values);
95 mask.
put(valuesSlice,
false);
113 template<
typename dtype>
117 return insert(arr, index, values, axis);
132 template<
typename dtype>
139 return insert(arr, index, values);
175 auto mask = ones_like<bool>(result);
176 mask.put(
Slice(index, index + valuesSize), mask.cSlice(),
false);
178 result.putMask(mask, arr);
179 result.putMask(!mask, values);
217 auto mask = ones_like<bool>(result);
218 mask.put(mask.rSlice(),
Slice(index, index + valuesSize),
false);
220 result.putMask(mask, arr);
221 result.putMask(!mask, values);
245 template<
typename dtype,
typename Indices, type_traits::ndarray_
int_concept<Indices> = 0>
249 return insert(arr, indices, values, axis);
264 template<
typename dtype>
286 template<
typename dtype,
typename Indices, type_traits::ndarray_
int_concept<Indices> = 0>
290 const auto isScalarValue = values.
isscalar();
296 if (!isScalarValue && indices.size() != values.
size())
301 const auto arrSize =
static_cast<int32>(arr.
size());
303 std::vector<std::pair<int32, dtype>> indexValues(indices.size());
306 const auto value = values.
front();
310 [arrSize, value](
auto index) -> std::pair<int32, dtype>
312 if constexpr (type_traits::is_ndarray_signed_int_v<Indices>)
321 THROW_INVALID_ARGUMENT_ERROR(
"index out of range");
324 if (
static_cast<int32>(index) > arrSize)
329 return std::make_pair(
static_cast<int32>(index), value);
338 [arrSize](
auto index,
const auto& value) -> std::pair<int32, dtype>
340 if constexpr (type_traits::is_ndarray_signed_int_v<Indices>)
349 THROW_INVALID_ARGUMENT_ERROR(
"index out of range");
352 if (
static_cast<int32>(index) > arrSize)
357 return std::make_pair(
static_cast<int32>(index), value);
363 [](
const auto& indexValue1,
const auto& indexValue2) noexcept ->
bool
364 { return indexValue1.first < indexValue2.first; });
365 auto indexValuesUnique = std::vector<
typename decltype(indexValues)::value_type>{};
368 std::back_inserter(indexValuesUnique),
369 [](
const auto& indexValue1,
const auto& indexValue2) noexcept ->
bool
370 { return indexValue1.first == indexValue2.first; });
374 auto mask = ones_like<bool>(result);
377 indexValuesUnique.end(),
378 [&counter, &mask](
auto& indexValue) noexcept ->
void
379 { mask[indexValue.first + counter++] = false; });
381 result.putMask(mask, arr);
383 auto valuesSorted = [&indexValuesUnique]
385 std::vector<dtype> values_;
386 values_.reserve(indexValuesUnique.size());
388 indexValuesUnique.end(),
389 std::back_inserter(values_),
390 [](
const auto& indexValue) { return indexValue.second; });
400 const auto arrNumRows =
static_cast<int32>(arr.numRows());
402 std::vector<std::pair<int32, NdArray<dtype>>> indexValues(indices.size());
403 if (values.isscalar())
405 const auto value = values.front();
406 auto valueRow = NdArray<dtype>(1, arr.numCols());
407 valueRow.fill(value);
411 [arrNumRows, &valueRow](
auto index) -> std::pair<
int32, NdArray<dtype>>
413 if constexpr (type_traits::is_ndarray_signed_int_v<Indices>)
422 THROW_INVALID_ARGUMENT_ERROR(
"index out of range");
425 if (
static_cast<int32>(index) > arrNumRows)
430 return std::make_pair(
static_cast<int32>(index), valueRow);
433 else if (values.size() == arr.numCols())
438 [arrNumRows, &values](
auto index) -> std::pair<
int32, NdArray<dtype>>
440 if constexpr (type_traits::is_ndarray_signed_int_v<Indices>)
449 THROW_INVALID_ARGUMENT_ERROR(
"index out of range");
452 if (
static_cast<int32>(index) > arrNumRows)
457 return std::make_pair(
static_cast<int32>(index), values);
460 else if (values.numCols() == arr.numCols() && values.numRows() == indices.size())
466 [arrNumRows, &values, &counter](
auto index) -> std::pair<
int32, NdArray<dtype>>
468 if constexpr (type_traits::is_ndarray_signed_int_v<Indices>)
477 THROW_INVALID_ARGUMENT_ERROR(
"index out of range");
480 if (
static_cast<int32>(index) > arrNumRows)
485 return std::make_pair(
static_cast<int32>(index),
486 values(counter++, values.cSlice()));
496 [](
const auto& indexValue1,
const auto& indexValue2) noexcept ->
bool
497 { return indexValue1.first < indexValue2.first; });
498 auto indexValuesUnique = std::vector<
typename decltype(indexValues)::value_type>{};
501 std::back_inserter(indexValuesUnique),
502 [](
const auto& indexValue1,
const auto& indexValue2) noexcept ->
bool
503 { return indexValue1.first == indexValue2.first; });
505 auto result = NdArray<dtype>(arrNumRows + indexValuesUnique.size(), arr.numCols());
507 auto mask = ones_like<bool>(result);
510 indexValuesUnique.end(),
511 [&counter, &mask](
auto& indexValue) noexcept ->
void
512 { mask.put(indexValue.first + counter++, mask.cSlice(), false); });
514 result.putMask(mask, arr);
517 for (
const auto& [index, values_] : indexValuesUnique)
519 result.put(index + counter++, result.cSlice(), values_);
526 return insert(arr.transpose(), indices, values.transpose(), Axis::ROW).transpose();
548 template<
typename dtype>
553 NdArray<uint32>(sliceIndices.data(), sliceIndices.size(), PointerPolicy::SHELL),
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:139
size_type dimSize(Axis inAxis) const noexcept
Definition: NdArrayCore.hpp:2684
size_type size() const noexcept
Definition: NdArrayCore.hpp:4524
size_type numCols() const noexcept
Definition: NdArrayCore.hpp:3465
self_type flatten() const
Definition: NdArrayCore.hpp:2847
self_type & fill(value_type inFillValue) noexcept
Definition: NdArrayCore.hpp:2808
const_reference front() const noexcept
Definition: NdArrayCore.hpp:2860
self_type & putMask(const NdArray< bool > &inMask, const value_type &inValue)
Definition: NdArrayCore.hpp:4135
size_type numRows() const noexcept
Definition: NdArrayCore.hpp:3477
iterator begin() noexcept
Definition: NdArrayCore.hpp:1315
bool isscalar() const noexcept
Definition: NdArrayCore.hpp:2956
self_type & put(index_type inIndex, const value_type &inValue)
Definition: NdArrayCore.hpp:3693
A Class for slicing into NdArrays.
Definition: Slice.hpp:45
std::vector< uint32 > toIndices(uint32 inArrayDimSize)
Definition: Slice.hpp:214
void sort(RandomIt first, RandomIt last) noexcept
Definition: StlAlgorithms.hpp:696
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
Definition: StlAlgorithms.hpp:775
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:225
constexpr OutputIt unique_copy(InputIt first, InputIt last, OutputIt destination) noexcept
Definition: StlAlgorithms.hpp:823
Definition: Cartesian.hpp:40
NdArray< dtype > insert(const NdArray< dtype > &arr, int32 index, const dtype &value)
Definition: insert.hpp:55
Axis
Enum To describe an axis.
Definition: Enums.hpp:36
std::int32_t int32
Definition: Types.hpp:36
NdArray< dtype > insert(const NdArray< dtype > &arr, Slice slice, const NdArray< dtype > &values, Axis axis=Axis::NONE)
Definition: insert.hpp:549