From dab81019b9e3398dc9d14dea693585f1419eaa96 Mon Sep 17 00:00:00 2001 From: "Martin R. Smith" <1695515+ms609@users.noreply.github.com> Date: Wed, 19 Apr 2023 09:41:54 +0100 Subject: [PATCH 1/2] Handle zero-length input --- src/hungarian.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/hungarian.cpp b/src/hungarian.cpp index d5dbe28..10d1228 100644 --- a/src/hungarian.cpp +++ b/src/hungarian.cpp @@ -27,6 +27,13 @@ List HungarianSolver(NumericMatrix costMatrix) { int nr = costMatrix.nrow(); int nc = costMatrix.ncol(); + if (nr == 0 || nc == 0) { + List out(2); + out[0] = 0; + out.names() = CharacterVector::create("cost", "pairs"); + return out; + } + vector c(nc); vector> cm(nr, c); for (int i=0; i < nr; i++){ From 2497a987c50ddb76bc155f219b2f9985fc70d73a Mon Sep 17 00:00:00 2001 From: "Martin R. Smith" <1695515+ms609@users.noreply.github.com> Date: Thu, 23 Nov 2023 08:56:35 +0000 Subject: [PATCH 2/2] Return value on zero-length input The second element of the returned list should be 2 column 0 row matrix --- src/hungarian.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/hungarian.cpp b/src/hungarian.cpp index 10d1228..8f0364a 100644 --- a/src/hungarian.cpp +++ b/src/hungarian.cpp @@ -30,6 +30,7 @@ List HungarianSolver(NumericMatrix costMatrix) { if (nr == 0 || nc == 0) { List out(2); out[0] = 0; + out[1] = NumericMatrix::create(0, 2); out.names() = CharacterVector::create("cost", "pairs"); return out; }