fork download
  1. #include <vector>
  2. #include <algorithm>
  3.  
  4. struct for_each_unmirrored {
  5.  
  6. template<class datapoint, class function>
  7. for_each_unmirrored(const std::vector<datapoint>& data, int num_values, function&& func) {
  8. std::vector<unsigned> indexes;
  9. indexes.reserve(num_values);
  10. if (num_values)
  11. for_each_unmirrored_helper(0, data.size(), num_values, data, func, indexes);
  12. }
  13. private:
  14. template<class datapoint, class function>
  15. void for_each_unmirrored_helper(unsigned minindex, unsigned maxindex, unsigned num_values, const std::vector<datapoint>& data, function& func, std::vector<unsigned>& indexes) {
  16. if (minindex >= maxindex)
  17. return;
  18. //First is set, but not last
  19. indexes.push_back(minindex);
  20. if (num_values-1 > 0) {
  21. for_each_mirrored_helper(minindex+1, maxindex-1, num_values-1, data, func, indexes);
  22. //First and last are set
  23. if (num_values-2 > 0) {
  24. indexes.push_back(maxindex-1);
  25. for_each_unmirrored_helper(minindex+1, maxindex-1, num_values-2, data, func, indexes);
  26. indexes.pop_back();
  27. }
  28. } else
  29. for_each_call_helper(data, func, indexes);
  30. indexes.pop_back();
  31. //neither first nor last are set
  32. for_each_unmirrored_helper(minindex+1, maxindex-1, num_values, data, func, indexes);
  33. }
  34.  
  35. template<class datapoint, class function>
  36. void for_each_mirrored_helper(unsigned minindex, unsigned maxindex, unsigned num_values, const std::vector<datapoint>& data, function& func, std::vector<unsigned>& indexes) {
  37. if (minindex >= maxindex)
  38. return;
  39. indexes.push_back(minindex);
  40. for( ; indexes.back() < maxindex; ++indexes.back()) {
  41. if (num_values>1)
  42. for_each_mirrored_helper(indexes.back()+1, maxindex, num_values-1, data, func, indexes);
  43. else
  44. for_each_call_helper(data, func, indexes);
  45. }
  46. indexes.pop_back();
  47. }
  48.  
  49. template<class datapoint, class function>
  50. void for_each_call_helper(const std::vector<datapoint>& data, function& func, std::vector<unsigned>& indexes) {
  51. static std::vector<unsigned> sorted_indexes;
  52. sorted_indexes = indexes;
  53. static std::vector<datapoint> call_data;
  54. call_data.reserve(sorted_indexes.size());
  55. call_data.clear();
  56.  
  57. std::sort(sorted_indexes.begin(), sorted_indexes.end());
  58. for(unsigned i=0; i<sorted_indexes.size(); ++i)
  59. call_data.push_back(data[sorted_indexes[i]]);
  60. func(call_data);
  61. }
  62. };
  63.  
  64.  
  65.  
  66. #include <iostream>
  67. #include <string>
  68. struct printer {
  69. void operator()(const std::vector<std::string>& data) {
  70. for(unsigned i=0; i<data.size(); ++i)
  71. std::cout << data[i] << ' ';
  72. std::cout << '\n';
  73. }
  74. };
  75.  
  76. int main() {
  77. std::vector<std::string> data;
  78. data.push_back("A");
  79. data.push_back("B");
  80. data.push_back("C");
  81. data.push_back("D");
  82. data.push_back("E");
  83. data.push_back("F");
  84. for_each_unmirrored(data, 3, printer());
  85. return 0;
  86. }
  87.  
  88. /*expected output:
  89. 5 elements: A B C D E. pick 2 unique.
  90. Results:
  91. A B
  92. A C
  93. A D
  94. A E
  95. B C
  96. B D
  97.  
  98. 6 elements: A B C D E F. pick 2 unique.
  99. Results:
  100. A B
  101. A C
  102. A D
  103. A E
  104. A F
  105. B C
  106. B D
  107. B E
  108. C D
  109.  
  110.  
  111. 6 elements: A B C D E F. pick 3 unique.
  112. Results:
  113. A B C
  114. A B D
  115. A B E
  116. A C D
  117. A C E
  118. A D E
  119. A B F
  120. A C F
  121. B C D
  122. B C E
  123. */
Success #stdin #stdout 0s 3052KB
stdin
Standard input is empty
stdout
A B C 
A B D 
A B E 
A C D 
A C E 
A D E 
A B F 
A C F 
B C D 
B C E