Skip to content

Commit b941015

Browse files
committed
Merge pull request #19 from kaajo/templated
Templated munkres
2 parents 43fa85c + 52ae562 commit b941015

26 files changed

+986
-966
lines changed

CMakeLists.txt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -O0 -ggdb3 -DDEBUG")
1010
set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -O3")
1111

1212
include_directories (${PROJECT_SOURCE_DIR}/src)
13+
include_directories (${PROJECT_SOURCE_DIR}/src/adapters)
1314

1415
# Sources.
1516
set (
@@ -23,16 +24,19 @@ set (
2324
${PROJECT_SOURCE_DIR}/src/matrix.h
2425
${PROJECT_SOURCE_DIR}/src/matrix.cpp
2526
${PROJECT_SOURCE_DIR}/src/munkres.h
27+
2628
)
2729

30+
add_subdirectory(${PROJECT_SOURCE_DIR}/src/adapters)
31+
2832
# Library.
2933
add_library (
3034
munkres STATIC
3135
${MunkresCppLib_SOURCES}
3236
)
3337

3438
install (TARGETS munkres DESTINATION lib PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
35-
install (FILES ${MunkresCppLib_HEADERS} DESTINATION include PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
39+
install (FILES ${MunkresCppLib_HEADERS} DESTINATION include/munkres PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
3640

3741

3842
# Binary example
@@ -42,8 +46,7 @@ target_link_libraries (munkres.bin munkres)
4246
add_custom_target (example)
4347
add_dependencies (example munkres.bin)
4448

45-
46-
option (MUNKRESCPP_DEVEL_MODE "Configure project in development mode." OFF)
49+
option(MUNKRESCPP_DEVEL_MODE "Configure project in development mode." OFF)
4750
if (MUNKRESCPP_DEVEL_MODE)
4851
# Enable the ExternalProject_Add directive.
4952
# Which used for getting external tools for the Project.
@@ -68,6 +71,9 @@ if (MUNKRESCPP_DEVEL_MODE)
6871
benchmarks
6972
)
7073

74+
# celero needs curses library
75+
#find_package(Curses)
76+
#include_directories(${CURSES_INCLUDE_DIR})
7177

7278
# Static code analyse.
7379
set (CppCheck_REPORT ${PROJECT_BINARY_DIR}/cppcheck.report)
@@ -77,4 +83,4 @@ if (MUNKRESCPP_DEVEL_MODE)
7783
)
7884
add_custom_target (cppcheck DEPENDS ${CppCheck_REPORT})
7985
set_directory_properties (PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES ${CppCheck_REPORT})
80-
endif ()
86+
endif (MUNKRESCPP_DEVEL_MODE)

examples/main.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <ctime>
2727

2828
#include "munkres.h"
29+
#include "adapters/boostmatrixadapter.h"
2930

3031
int
3132
main(int argc, char *argv[]) {
@@ -35,8 +36,8 @@ main(int argc, char *argv[]) {
3536
if ( argc == 3 ) {
3637
nrows = atoi(argv[1]);
3738
ncols = atoi(argv[2]);
38-
}
39-
39+
}
40+
4041
Matrix<double> matrix(nrows, ncols);
4142

4243
srandom(time(nullptr)); // Seed random number generator.
@@ -59,7 +60,7 @@ main(int argc, char *argv[]) {
5960
std::cout << std::endl;
6061

6162
// Apply Munkres algorithm to matrix.
62-
Munkres m;
63+
Munkres<double> m;
6364
m.solve(matrix);
6465

6566
// Display solved matrix.

src/adapters/CMakeLists.txt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
set (
2+
MunkresAdapters_HEADERS
3+
${PROJECT_SOURCE_DIR}/src/adapters/adapter.h
4+
)
5+
6+
set (
7+
MunkresCppLib_SOURCES
8+
${MunkresCppLib_SOURCES}
9+
${PROJECT_SOURCE_DIR}/src/adapters/adapter.cpp
10+
)
11+
12+
option(STD_ADAPTERS "Build 2D array, std::array and std::vector adapters" OFF)
13+
if(STD_ADAPTERS)
14+
set (MunkresCppLib_HEADERS
15+
${MunkresCppLib_HEADERS}
16+
${PROJECT_SOURCE_DIR}/src/adapters/std2dvectoradapter.h
17+
${PROJECT_SOURCE_DIR}/src/adapters/std2darrayadapter.h
18+
)
19+
20+
set (
21+
MunkresCppLib_SOURCES
22+
${MunkresCppLib_SOURCES}
23+
${PROJECT_SOURCE_DIR}/src/adapters/std2dvectordapter.cpp
24+
${PROJECT_SOURCE_DIR}/src/adapters/std2darrayadapter.cpp
25+
)
26+
endif(STD_ADAPTERS)
27+
28+
option(BOOST_MATRIX_ADAPTER "Build boost::numeric::ublas::matrix adapter" OFF)
29+
if(BOOST_MATRIX_ADAPTER)
30+
find_package (Boost REQUIRED)
31+
set (MunkresCppLib_HEADERS ${MunkresCppLib_HEADERS}
32+
${PROJECT_SOURCE_DIR}/src/adapters/boostmatrixadapter.h
33+
)
34+
35+
set (
36+
MunkresCppLib_SOURCES
37+
${MunkresCppLib_SOURCES}
38+
${PROJECT_SOURCE_DIR}/src/adapters/boostmatrixadapter.cpp
39+
)
40+
endif(BOOST_MATRIX_ADAPTER)
41+
42+
#propagate upward edited sources
43+
set(MunkresCppLib_SOURCES ${MunkresCppLib_SOURCES} PARENT_SCOPE)
44+
45+
#install all selected adapters
46+
install (FILES ${MunkresAdapters_HEADERS} DESTINATION include/munkres/adapters PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
47+

src/adapters/adapter.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright (c) 2015 Miroslav Krajicek
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17+
*/
18+
19+
#include "adapter.h"
Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007 John Weaver
2+
* Copyright (c) 2015 Miroslav Krajicek
33
*
44
* This program is free software; you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by
@@ -16,25 +16,25 @@
1616
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1717
*/
1818

19-
#if !defined(_MUNKRES_ADAPTERS_STD_2D_VECTOR_H_)
20-
#define _MUNKRES_ADAPTERS_STD_2D_VECTOR_H_
19+
#ifndef _ADAPTER_H_
20+
#define _ADAPTER_H_
2121

2222
#include "matrix.h"
23-
#include <vector>
24-
25-
26-
// Set of functions for two-dimensional std::vector.
27-
template <typename T>
28-
Matrix <T> convert_std_2d_vector_to_munkres_matrix (const std::vector <std::vector <T> > & vector);
29-
30-
template <typename T>
31-
void fill_std_2d_vector_from_munkres_matrix (std::vector <std::vector <T> > & vector, const Matrix <T> & matrix);
32-
33-
void solve(std::vector <std::vector <double> > &m);
34-
35-
#ifndef USE_EXPORT_KEYWORD
36-
#include "std_2d_vector.cpp"
37-
//#define export /*export*/
38-
#endif
39-
40-
#endif /* !defined(_MUNKRES_ADAPTERS_STD_2D_VECTOR_H_) */
23+
#include "munkres.h"
24+
25+
template<typename Data, class Container > class Adapter
26+
{
27+
public:
28+
virtual Matrix<Data> convertToMatrix(const Container &con) const = 0;
29+
virtual void convertFromMatrix(Container &con, const Matrix<Data> &matrix) const = 0;
30+
virtual void solve(Container &con)
31+
{
32+
auto matrix = convertToMatrix(con);
33+
m_munkres.solve(matrix);
34+
convertFromMatrix(con, matrix);
35+
}
36+
protected:
37+
Munkres<Data> m_munkres;
38+
};
39+
40+
#endif /* _ADAPTER_H_ */

src/adapters/boost_matrix.cpp

Lines changed: 0 additions & 53 deletions
This file was deleted.
Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007 John Weaver
2+
* Copyright (c) 2015 Miroslav Krajicek
33
*
44
* This program is free software; you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by
@@ -16,25 +16,8 @@
1616
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1717
*/
1818

19-
#if !defined(_MUNKRES_ADAPTERS_BOOST_MATRIX_H_)
20-
#define _MUNKRES_ADAPTERS_BOOST_MATRIX_H_
19+
#include "boostmatrixadapter.h"
2120

22-
#include "matrix.h"
23-
#include <boost/numeric/ublas/matrix.hpp>
24-
25-
26-
// Set of functions for boost matrix.
27-
template <typename T>
28-
Matrix <T> convert_boost_matrix_to_munkres_matrix (const boost::numeric::ublas::matrix <T> &);
29-
30-
template <typename T>
31-
void fill_boost_matrix_from_munkres_matrix (boost::numeric::ublas::matrix <T> & boost_matrix, const Matrix <T> &);
32-
33-
void solve(boost::numeric::ublas::matrix <double> &);
34-
35-
#ifndef USE_EXPORT_KEYWORD
36-
#include "boost_matrix.cpp"
37-
//#define export /*export*/
38-
#endif
39-
40-
#endif /* !defined(_MUNKRES_ADAPTERS_BOOST_MATRIX_H_) */
21+
template class BoostMatrixAdapter<double>;
22+
template class BoostMatrixAdapter<float>;
23+
template class BoostMatrixAdapter<int>;

src/adapters/boostmatrixadapter.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) 2015 Miroslav Krajicek
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17+
*/
18+
19+
#ifndef _BOOSTMATRIXADAPTER_H_
20+
#define _BOOSTMATRIXADAPTER_H_
21+
22+
#include "adapter.h"
23+
#include <boost/numeric/ublas/matrix.hpp>
24+
25+
template<typename Data> class BoostMatrixAdapter : public Adapter<Data,boost::numeric::ublas::matrix<Data> >
26+
{
27+
public:
28+
virtual Matrix<Data> convertToMatrix(const boost::numeric::ublas::matrix<Data> &boost_matrix) const override
29+
{
30+
const auto rows = boost_matrix.size1 ();
31+
const auto columns = boost_matrix.size2 ();
32+
Matrix <Data> matrix (rows, columns);
33+
for (int i = 0; i < rows; ++i) {
34+
for (int j = 0; j < columns; ++j) {
35+
matrix (i, j) = boost_matrix (i, j);
36+
}
37+
}
38+
return matrix;
39+
}
40+
41+
virtual void convertFromMatrix(boost::numeric::ublas::matrix<Data> &boost_matrix,const Matrix<Data> &matrix) const override
42+
{
43+
const auto rows = matrix.rows();
44+
const auto columns = matrix.columns();
45+
for (int i = 0; i < rows; ++i) {
46+
for (int j = 0; j < columns; ++j) {
47+
boost_matrix (i, j) = matrix (i, j);
48+
}
49+
}
50+
}
51+
};
52+
53+
#endif /* _BOOSTMATRIXADAPTER_H_ */

0 commit comments

Comments
 (0)