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>
283 template<
typename dtype,
typename Indices, type_traits::ndarray_
int_concept<Indices> = 0>
287 const auto isScalarValue = values.
isscalar();
293 if (!isScalarValue && indices.size() != values.
size())
298 const auto arrSize =
static_cast<int32>(arr.
size());
300 std::vector<std::pair<int32, dtype>> indexValues(indices.size());
303 const auto value = values.
front();
307 [arrSize, value](
auto index) -> std::pair<int32, dtype>
309 if constexpr (type_traits::is_ndarray_signed_int_v<Indices>)
318 THROW_INVALID_ARGUMENT_ERROR(
"index out of range");
321 if (
static_cast<int32>(index) > arrSize)
326 return std::make_pair(
static_cast<int32>(index), value);
335 [arrSize](
auto index,
const auto& value) -> std::pair<int32, dtype>
337 if constexpr (type_traits::is_ndarray_signed_int_v<Indices>)
346 THROW_INVALID_ARGUMENT_ERROR(
"index out of range");
349 if (
static_cast<int32>(index) > arrSize)
354 return std::make_pair(
static_cast<int32>(index), value);
360 [](
const auto& indexValue1,
const auto& indexValue2) noexcept ->
bool
361 { return indexValue1.first < indexValue2.first; });
362 auto indexValuesUnique = std::vector<
typename decltype(indexValues)::value_type>{};
365 std::back_inserter(indexValuesUnique),
366 [](
const auto& indexValue1,
const auto& indexValue2) noexcept ->
bool
367 { return indexValue1.first == indexValue2.first; });
371 auto mask = ones_like<bool>(result);
374 indexValuesUnique.end(),
375 [&counter, &mask](
auto& indexValue) noexcept ->
void
376 { mask[indexValue.first + counter++] = false; });
378 result.putMask(mask, arr);
380 auto valuesSorted = [&indexValuesUnique]
382 std::vector<dtype> values_;
383 values_.reserve(indexValuesUnique.size());
385 indexValuesUnique.end(),
386 std::back_inserter(values_),
387 [](
const auto& indexValue) { return indexValue.second; });
391 result.putMask(!mask,
NdArray<dtype>(valuesSorted.data(), valuesSorted.size(),
false));
397 const auto arrNumRows =
static_cast<int32>(arr.numRows());
399 std::vector<std::pair<int32, NdArray<dtype>>> indexValues(indices.size());
400 if (values.isscalar())
402 const auto value = values.front();
403 auto valueRow = NdArray<dtype>(1, arr.numCols());
404 valueRow.fill(value);
408 [arrNumRows, &valueRow](
auto index) -> std::pair<
int32, NdArray<dtype>>
410 if constexpr (type_traits::is_ndarray_signed_int_v<Indices>)
419 THROW_INVALID_ARGUMENT_ERROR(
"index out of range");
422 if (
static_cast<int32>(index) > arrNumRows)
427 return std::make_pair(
static_cast<int32>(index), valueRow);
430 else if (values.size() == arr.numCols())
435 [arrNumRows, &values](
auto index) -> std::pair<
int32, NdArray<dtype>>
437 if constexpr (type_traits::is_ndarray_signed_int_v<Indices>)
446 THROW_INVALID_ARGUMENT_ERROR(
"index out of range");
449 if (
static_cast<int32>(index) > arrNumRows)
454 return std::make_pair(
static_cast<int32>(index), values);
457 else if (values.numCols() == arr.numCols() && values.numRows() == indices.size())
463 [arrNumRows, &values, &counter](
auto index) -> std::pair<
int32, NdArray<dtype>>
465 if constexpr (type_traits::is_ndarray_signed_int_v<Indices>)
474 THROW_INVALID_ARGUMENT_ERROR(
"index out of range");
477 if (
static_cast<int32>(index) > arrNumRows)
482 return std::make_pair(
static_cast<int32>(index),
483 values(counter++, values.cSlice()));
493 [](
const auto& indexValue1,
const auto& indexValue2) noexcept ->
bool
494 { return indexValue1.first < indexValue2.first; });
495 auto indexValuesUnique = std::vector<
typename decltype(indexValues)::value_type>{};
498 std::back_inserter(indexValuesUnique),
499 [](
const auto& indexValue1,
const auto& indexValue2) noexcept ->
bool
500 { return indexValue1.first == indexValue2.first; });
502 auto result = NdArray<dtype>(arrNumRows + indexValuesUnique.size(), arr.numCols());
504 auto mask = ones_like<bool>(result);
507 indexValuesUnique.end(),
508 [&counter, &mask](
auto& indexValue) noexcept ->
void
509 { mask.put(indexValue.first + counter++, mask.cSlice(), false); });
511 result.putMask(mask, arr);
514 for (
const auto& [index, values_] : indexValuesUnique)
516 result.put(index + counter++, result.cSlice(), values_);
523 return insert(arr.transpose(), indices, values.transpose(), Axis::ROW).transpose();
545 template<
typename dtype>
#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:138
size_type dimSize(Axis inAxis) const noexcept
Definition: NdArrayCore.hpp:2637
self_type & fill(value_type inFillValue) noexcept
Definition: NdArrayCore.hpp:2761
size_type size() const noexcept
Definition: NdArrayCore.hpp:4477
self_type & put(index_type inIndex, const value_type &inValue)
Definition: NdArrayCore.hpp:3646
size_type numCols() const noexcept
Definition: NdArrayCore.hpp:3418
self_type flatten() const
Definition: NdArrayCore.hpp:2800
const_reference front() const noexcept
Definition: NdArrayCore.hpp:2813
size_type numRows() const noexcept
Definition: NdArrayCore.hpp:3430
iterator begin() noexcept
Definition: NdArrayCore.hpp:1268
bool isscalar() const noexcept
Definition: NdArrayCore.hpp:2909
self_type & putMask(const NdArray< bool > &inMask, const value_type &inValue)
Definition: NdArrayCore.hpp:4088
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
Axis
Enum To describe an axis.
Definition: Types.hpp:47
NdArray< dtype > insert(const NdArray< dtype > &arr, int32 index, const dtype &value)
Definition: insert.hpp:55
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:546