1 #ifndef SIG_UTIL_SET_HPP
2 #define SIG_UTIL_SET_HPP
13 template <
class T,
class CMP,
template <
class T,
class = std::allocator<T>>
class Container >
14 maybe<std::tuple<T, uint>> SearchIndex(Container<T>
const& src, CMP comp)
16 if (src.empty())
return nothing;
21 for (uint i = 0, size = src.size(); i < size; ++i){
22 if (comp(src[i], val)){
28 return std::make_tuple(val, index);
34 inline void Print(std::string
const& text,
char const*
const delimiter =
"\n")
36 std::cout << text << delimiter;
39 inline void Print(std::wstring
const& text,
wchar_t const*
const delimiter = L
"\n")
41 std::wcout << text << delimiter;
44 template <
class T,
template <
class...>
class Container,
typename std::enable_if<!std::is_same<T, std::wstring>::value>::type*& =
enabler>
45 inline void Print(Container<T>
const& container,
char const*
const delimiter =
"\n")
47 std::copy(container.begin(), container.end(), std::ostream_iterator<T>(std::cout, delimiter));
50 template<
template<
class...>
class Container>
51 inline void Print(Container<std::wstring>
const& container,
wchar_t const*
const delimiter = L
"\n")
53 std::copy(container.begin(), container.end(), std::ostream_iterator<std::wstring>(std::wcout, delimiter));
57 template <
class CC1,
class CC2>
60 assert(men_vec.size() != women_vec.size());
62 uint total = men_vec.size();
63 std::vector<uint> keeping(total, total+1);
64 std::vector<std::tuple<bool, uint>> single_men(total, std::make_tuple(
true, 0));
65 std::queue<uint> rest;
68 auto propose = [&](
const uint man,
const uint woman) ->
bool{
69 if(keeping[woman] > total){
72 std::get<0>(single_men[man]) =
true;
76 for(uint i=0; i<total; ++i){
77 if(man == women_vec[woman][i]){
78 if(i < keeping[woman]){
80 uint now_man = women_vec[woman][keeping[woman]];
82 std::get<0>(single_men[man]) =
true;
84 std::get<0>(single_men[now_man]) =
false;
96 for(uint i=0; i<total; ++i) rest.push(i);
98 while (!rest.empty()){
99 const uint man = rest.front();
100 const uint propose_woman = men_vec[man][std::get<1>(single_men[man])];
101 ++std::get<1>(single_men[man]);
102 if (propose(man, propose_woman)) rest.pop();
void Print(std::string const &text, char const *const delimiter="\n")
auto GaleShapley(CC1 const &men_vec, CC2 const &women_vec)
auto copy(C &&src) -> RC
別の種類のコンテナに要素をコピーする