Skip to content

Commit 5065a62

Browse files
committed
rework readme, improve examples, update license
Issue: saebyn#15 Signed-off-by: Gluttton <gluttton@ukr.net>
1 parent b0e13e8 commit 5065a62

22 files changed

+909
-368
lines changed

README.md

Lines changed: 330 additions & 54 deletions
Large diffs are not rendered by default.

examples/CMakeLists.txt

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@ target_link_libraries (example_02
1212
PRIVATE lp::munkres-cpp
1313
)
1414

15-
find_package (Boost COMPONENTS system)
1615
add_executable (example_03
1716
EXCLUDE_FROM_ALL ${PROJECT_SOURCE_DIR}/examples/example_03.cpp
1817
)
1918
target_link_libraries (example_03
2019
PRIVATE lp::munkres-cpp
21-
PRIVATE Boost::system
2220
)
2321

22+
find_package (Boost COMPONENTS system)
2423
add_executable (example_04
2524
EXCLUDE_FROM_ALL ${PROJECT_SOURCE_DIR}/examples/example_04.cpp
2625
)
@@ -37,13 +36,40 @@ target_link_libraries (example_05
3736
PRIVATE Boost::system
3837
)
3938

39+
find_package (OpenCV COMPONENTS core)
4040
add_executable (example_06
4141
EXCLUDE_FROM_ALL ${PROJECT_SOURCE_DIR}/examples/example_06.cpp
4242
)
43+
target_include_directories (example_06
44+
PRIVATE ${OPENCV_INCLUDE_DIRS}
45+
)
4346
target_link_libraries (example_06
4447
PRIVATE lp::munkres-cpp
48+
PRIVATE ${OpenCV_LIBS}
4549
)
4650

51+
add_executable (example_07
52+
EXCLUDE_FROM_ALL ${PROJECT_SOURCE_DIR}/examples/example_07.cpp
53+
)
54+
target_link_libraries (example_07
55+
PRIVATE lp::munkres-cpp
56+
)
57+
58+
add_executable (example_08
59+
EXCLUDE_FROM_ALL ${PROJECT_SOURCE_DIR}/examples/example_08.cpp
60+
)
61+
target_link_libraries (example_08
62+
PRIVATE lp::munkres-cpp
63+
)
64+
65+
add_executable (example_09
66+
EXCLUDE_FROM_ALL ${PROJECT_SOURCE_DIR}/examples/example_09.cpp
67+
)
68+
target_link_libraries (example_09
69+
PRIVATE lp::munkres-cpp
70+
)
71+
72+
4773
add_custom_target (examples)
4874
add_dependencies (examples
4975
example_01
@@ -52,4 +78,7 @@ add_dependencies (examples
5278
example_04
5379
example_05
5480
example_06
81+
example_07
82+
example_08
83+
example_09
5584
)

examples/example_01.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007 John Weaver
2+
* Copyright (c) 2016 Gluttton <gluttton@ukr.net>
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
@@ -18,33 +18,34 @@
1818

1919

2020

21-
// The easiest way to start with munkres-cpp.
21+
// Trivial example with obvious solution.
22+
23+
// Include header with the solver class.
2224
#include <munkres-cpp/munkres.h>
25+
// Include header with the built-in matrix class.
2326
#include <munkres-cpp/matrix.h>
2427
#include <cstdlib>
2528

26-
int main (int /*argc*/, char * /*argv*/[])
29+
int main (int, char **)
2730
{
28-
// Set input data (cost matrix) into matrix of generic type
29-
// munkres_cpp::Matrix, which is provided by the library.
30-
munkres_cpp::Matrix<int> data {
31+
// Create an instance of matrix container and fill it with
32+
// input (cost) data.
33+
munkres_cpp::Matrix<unsigned> data {
3134
// Task I Task II
3235
{1, 3} // Worker I
3336
,{5, 9} // Worker II
3437
};
35-
// You are totally responsible for correctness of the input data.
36-
// Input data must be positive and well defined (no NaN or infinity).
3738

38-
// Next you need create the problem solver and pass data to it.
39-
munkres_cpp::Munkres<int, munkres_cpp::Matrix> solver (data);
39+
// Next, create a problem solver and pass data to it.
40+
munkres_cpp::Munkres<unsigned, munkres_cpp::Matrix> solver (data);
4041

4142
// Now the matrix contains the solution of the problem.
4243
// Zero value represents selected values.
4344
// For input above data the result will be:
4445
// Task I Task II
4546
// 1, 0 // Worker I
4647
// 0, 1 // Worker II
47-
// Which means that sum of the costs 3 and 5 is equal 8 and
48+
// That means that sum of the costs 3 and 5 is equal 8 and
4849
// is minimum cost among the matrix.
4950

5051
return EXIT_SUCCESS;

examples/example_02.cpp

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007 John Weaver
2+
* Copyright (c) 2021 Gluttton <gluttton@ukr.net>
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
@@ -18,40 +18,37 @@
1818

1919

2020

21-
// Easy way to start with munkres-cpp.
21+
// Trivial example with less obvious solution.
22+
23+
// Include header with the solver class.
2224
#include <munkres-cpp/munkres.h>
25+
// Include header with the built-in matrix class.
2326
#include <munkres-cpp/matrix.h>
24-
#include <munkres-cpp/utils.h>
2527
#include <cstdlib>
2628

27-
int main (int /*argc*/, char * /*argv*/[])
29+
int main (int, char **)
2830
{
29-
// Set input data (cost matrix) into matrix of generic type
30-
// munkres_cpp::Matrix, which is provided by the library.
31-
munkres_cpp::Matrix<int> data {
32-
{1, 3}
33-
,{5, 9}
31+
// Create an instance of matrix container and fill it with
32+
// input (cost) data.
33+
munkres_cpp::Matrix<unsigned> data {
34+
// Task I Task II Task III
35+
{1, 3, 4} // Worker I
36+
,{5, 9, 6} // Worker II
37+
,{5, 8, 2} // Worker III
3438
};
35-
// You are totally responsible for correctness of the input data.
36-
// Input data must be positive and well defined (no NaN or infinity).
37-
38-
// The library provide generic function for checking is input data
39-
// correct and ready for processing. If you not sure in correctness
40-
// of the input data you should use it.
41-
if (munkres_cpp::is_data_valid (data) ) {
42-
// Next you need to create the problem solver and pass data to it.
43-
munkres_cpp::Munkres<int, munkres_cpp::Matrix> solver (data);
44-
45-
// Now the matrix contains the solution.
46-
// Zero value represents selected values.
47-
// For input above data the result will be:
48-
// 1, 0
49-
// 0, 1
50-
51-
return EXIT_SUCCESS;
52-
}
53-
else {
54-
return EXIT_FAILURE;
55-
}
56-
}
5739

40+
// Next, create a problem solver and pass data to it.
41+
munkres_cpp::Munkres<unsigned, munkres_cpp::Matrix> solver (data);
42+
43+
// Now the matrix contains the solution of the problem.
44+
// Zero value represents selected values.
45+
// For input above data the result will be:
46+
// Task I Task II Task III
47+
// 1, 0, 1 // Worker I
48+
// 0, 1, 1 // Worker II
49+
// 1, 1, 0 // Worker III
50+
// That means that sum of the costs 3, 5 and 2 is equal 10 and
51+
// is minimum cost among the matrix.
52+
53+
return EXIT_SUCCESS;
54+
}

examples/example_03.cpp

Lines changed: 36 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) 2016 Gluttton <gluttton@ukr.net>
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
@@ -18,30 +18,44 @@
1818

1919

2020

21-
// Easy way to start with munkres-cpp.
21+
// Example with input data validation.
22+
23+
// Include header with the solver class.
2224
#include <munkres-cpp/munkres.h>
23-
// The library provides set of adapters for the most popular containers
24-
// (for more info explore "adapters" folder).
25-
// If you are lucky then adapter for your container is already implemented.
26-
#include <munkres-cpp/adapters/matrix_boost.h>
25+
// Include header with the built-in matrix class.
26+
#include <munkres-cpp/matrix.h>
27+
// Include header with validation functions.
28+
#include <munkres-cpp/utils.h>
2729
#include <cstdlib>
2830

29-
int main (int /*argc*/, char * /*argv*/[])
31+
int main (int, char **)
3032
{
31-
// In such case you just need to create matrix of corresponding type,
32-
// set input data (cost) and pass it to the solver.
33-
34-
// Set input data (cost matrix).
35-
munkres_cpp::matrix_boost<double> data (2, 2);
36-
data (0, 0) = 1.0; data (0, 1) = 3.0;
37-
data (1, 0) = 5.0; data (1, 1) = 9.0;
38-
// Don't forget! You are responsible for correctness of the input data.
39-
40-
// Create the solver and pass data to it.
41-
munkres_cpp::Munkres<double, munkres_cpp::matrix_boost> solver (data);
42-
43-
// Now the matrix contains the solution.
44-
45-
return EXIT_SUCCESS;
33+
// Create an instance of matrix container and fill it with
34+
// input (cost) data.
35+
munkres_cpp::Matrix<int> data {
36+
{1, 3}
37+
,{5, 9}
38+
};
39+
// You are totally responsible for correctness of the input data.
40+
// Input data must be positive and well defined (no NaN or infinity).
41+
42+
// The library provide generic function for checking is input data
43+
// correct and ready for processing. If you not sure in correctness
44+
// of the input data you should use it.
45+
if (munkres_cpp::is_data_valid (data) ) {
46+
// Next you need to create the problem solver and pass data to it.
47+
munkres_cpp::Munkres<int, munkres_cpp::Matrix> solver (data);
48+
49+
// Now the matrix contains the solution.
50+
// Zero value represents selected values.
51+
// For input above data the result will be:
52+
// 1, 0
53+
// 0, 1
54+
55+
return EXIT_SUCCESS;
56+
}
57+
else {
58+
return EXIT_FAILURE;
59+
}
4660
}
4761

examples/example_04.cpp

Lines changed: 15 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007 John Weaver
2+
* Copyright (c) 2016 Gluttton <gluttton@ukr.net>
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
@@ -17,92 +17,33 @@
1717
*/
1818

1919

20+
// Trivial example of using of adapter class.
2021

21-
// More complex example.
22+
// Include header with the solver class.
2223
#include <munkres-cpp/munkres.h>
23-
// The library provides set of adapters for the most popular containers.
24-
// But if you need you can create adapter for any type of container by deriving
25-
// from base matrix class and implement the basic functions which allows to navigate
26-
// on container and access to its data.
27-
#include <munkres-cpp/matrix_base.h>
28-
#include <boost/numeric/ublas/matrix.hpp>
29-
#include <cstdlib>
30-
31-
// In general there are several approaches how to implement adapters:
32-
// - use inheritance (inherited from both your container and
33-
// munkres_cpp::matrix_base);
34-
// - use composition (inherited from munkres_cpp::matrix_base and use your
35-
// container as member);
36-
template<class T>
37-
class matrix_boost_adapter : public munkres_cpp::matrix_base<T>
38-
{
39-
public:
40-
matrix_boost_adapter (boost::numeric::ublas::matrix<T> * data = nullptr)
41-
: data {data}
42-
, is_own_data {false}
43-
{
44-
if (!data) {
45-
data = new boost::numeric::ublas::matrix<T>;
46-
is_own_data = true;
47-
}
48-
}
49-
50-
matrix_boost_adapter (size_t rows, size_t columns)
51-
: data {new boost::numeric::ublas::matrix<T> (rows, columns, 0)}
52-
, is_own_data {true}
53-
{
54-
}
55-
56-
~matrix_boost_adapter () override
57-
{
58-
if (is_own_data) {
59-
delete data;
60-
}
61-
}
62-
63-
const T & operator () (const size_t row, const size_t column) const override
64-
{
65-
return data->operator () (row, column);
66-
};
6724

68-
T & operator () (const size_t row, const size_t column) override
69-
{
70-
return data->operator () (row, column);
71-
}
25+
// The library provides set of adapters for the most popular containers
26+
// (for more info explore "adapters" folder).
27+
// If you are lucky then adapter for your container is already implemented.
7228

73-
size_t columns () const override
74-
{
75-
return data->size2 ();
76-
}
77-
78-
size_t rows () const override
79-
{
80-
return data->size1 ();
81-
}
82-
83-
void resize (size_t, size_t, T/* value = T (0) */) override
84-
{
85-
}
86-
87-
private:
88-
boost::numeric::ublas::matrix<T> * data;
89-
bool is_own_data;
90-
};
29+
// Include header with the adapter for Boost matrix class.
30+
#include <munkres-cpp/adapters/matrix_boost.h>
31+
#include <cstdlib>
9132

9233
int main (int /*argc*/, char * /*argv*/[])
9334
{
94-
// Create (or use already existed) matrix of your type.
95-
boost::numeric::ublas::matrix<double> data (2, 2);
35+
// In such case you just need to create matrix of corresponding type,
36+
// set input data (cost) and pass it to the solver.
37+
9638
// Set input data (cost matrix).
39+
munkres_cpp::matrix_boost<double> data (2, 2);
9740
data (0, 0) = 1.0; data (0, 1) = 3.0;
9841
data (1, 0) = 5.0; data (1, 1) = 9.0;
9942
// Don't forget! You are responsible for correctness of the input data.
100-
101-
// Create data adapter and pass your container to it.
102-
matrix_boost_adapter<double> adapter (& data);
43+
// NaN, infinities and negative values are prohibited!
10344

10445
// Create the solver and pass data to it.
105-
munkres_cpp::Munkres<double, matrix_boost_adapter> solver (adapter);
46+
munkres_cpp::Munkres<double, munkres_cpp::matrix_boost> solver (data);
10647

10748
// Now the matrix contains the solution.
10849

0 commit comments

Comments
 (0)