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>{};
366 std::unique_copy(indexValues.begin(),
368 std::back_inserter(indexValuesUnique),
369 [](
const auto& indexValue1,
const auto& indexValue2) noexcept ->
bool
370 { return indexValue1.first == indexValue2.first; });
376 std::for_each(indexValuesUnique.begin(),
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());
387 std::transform(indexValuesUnique.begin(),
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())
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())
463 std::transform(indices.begin(),
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()));
494 stl_algorithms::sort(indexValues.begin(),
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>{};
499 std::unique_copy(indexValues.begin(),
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);
509 std::for_each(indexValuesUnique.begin(),
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();