289 const auto isScalarValue = values.
isscalar();
295 if (!isScalarValue && indices.size() != values.
size())
300 const auto arrSize =
static_cast<int32>(arr.
size());
302 std::vector<std::pair<int32, dtype>> indexValues(indices.size());
305 const auto value = values.
front();
309 [arrSize, value](
auto index) -> std::pair<int32, dtype>
311 if constexpr (type_traits::is_ndarray_signed_int_v<Indices>)
320 THROW_INVALID_ARGUMENT_ERROR(
"index out of range");
323 if (
static_cast<int32>(index) > arrSize)
328 return std::make_pair(
static_cast<int32>(index), value);
337 [arrSize](
auto index,
const auto& value) -> std::pair<int32, dtype>
339 if constexpr (type_traits::is_ndarray_signed_int_v<Indices>)
348 THROW_INVALID_ARGUMENT_ERROR(
"index out of range");
351 if (
static_cast<int32>(index) > arrSize)
356 return std::make_pair(
static_cast<int32>(index), value);
362 [](
const auto& indexValue1,
const auto& indexValue2) noexcept ->
bool
363 { return indexValue1.first < indexValue2.first; });
364 auto indexValuesUnique = std::vector<
typename decltype(indexValues)::value_type>{};
365 std::unique_copy(indexValues.begin(),
367 std::back_inserter(indexValuesUnique),
368 [](
const auto& indexValue1,
const auto& indexValue2) noexcept ->
bool
369 { return indexValue1.first == indexValue2.first; });
375 std::for_each(indexValuesUnique.begin(),
376 indexValuesUnique.end(),
377 [&counter, &mask](
auto& indexValue) noexcept ->
void
378 { mask[indexValue.first + counter++] = false; });
380 result.putMask(mask, arr);
382 auto valuesSorted = [&indexValuesUnique]
384 std::vector<dtype> values_;
385 values_.reserve(indexValuesUnique.size());
386 std::transform(indexValuesUnique.begin(),
387 indexValuesUnique.end(),
388 std::back_inserter(values_),
389 [](
const auto& indexValue) { return indexValue.second; });
399 const auto arrNumRows =
static_cast<int32>(arr.numRows());
401 std::vector<std::pair<int32, NdArray<dtype>>> indexValues(indices.size());
402 if (values.isscalar())
404 const auto value = values.front();
405 auto valueRow = NdArray<dtype>(1, arr.numCols());
406 valueRow.fill(value);
410 [arrNumRows, &valueRow](
auto index) -> std::pair<
int32, NdArray<dtype>>
412 if constexpr (type_traits::is_ndarray_signed_int_v<Indices>)
421 THROW_INVALID_ARGUMENT_ERROR(
"index out of range");
424 if (
static_cast<int32>(index) > arrNumRows)
429 return std::make_pair(
static_cast<int32>(index), valueRow);
432 else if (values.size() == arr.numCols())
439 if constexpr (type_traits::is_ndarray_signed_int_v<Indices>)
448 THROW_INVALID_ARGUMENT_ERROR(
"index out of range");
451 if (
static_cast<int32>(index) > arrNumRows)
456 return std::make_pair(
static_cast<int32>(index), values);
459 else if (values.numCols() == arr.numCols() && values.numRows() == indices.size())
462 std::transform(indices.begin(),
465 [arrNumRows, &values, &counter](
auto index) -> std::pair<int32, NdArray<dtype>>
467 if constexpr (type_traits::is_ndarray_signed_int_v<Indices>)
476 THROW_INVALID_ARGUMENT_ERROR(
"index out of range");
479 if (
static_cast<int32>(index) > arrNumRows)
484 return std::make_pair(
static_cast<int32>(index),
485 values(counter++, values.cSlice()));
493 stl_algorithms::sort(indexValues.begin(),
495 [](
const auto& indexValue1,
const auto& indexValue2) noexcept ->
bool
496 { return indexValue1.first < indexValue2.first; });
497 auto indexValuesUnique = std::vector<
typename decltype(indexValues)::value_type>{};
498 std::unique_copy(indexValues.begin(),
500 std::back_inserter(indexValuesUnique),
501 [](
const auto& indexValue1,
const auto& indexValue2) noexcept ->
bool
502 { return indexValue1.first == indexValue2.first; });
504 auto result = NdArray<dtype>(arrNumRows + indexValuesUnique.size(), arr.numCols());
506 auto mask = ones_like<bool>(result);
508 std::for_each(indexValuesUnique.begin(),
509 indexValuesUnique.end(),
510 [&counter, &mask](
auto& indexValue) noexcept ->
void
511 { mask.put(indexValue.first + counter++, mask.cSlice(), false); });
513 result.putMask(mask, arr);
516 for (
const auto& [index, values_] : indexValuesUnique)
518 result.put(index + counter++, result.cSlice(), values_);
525 return insert(arr.transpose(), indices, values.transpose(), Axis::ROW).transpose();