nchoosek.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Olga Diamanti <olga.diam@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "nchoosek.h"
  9. namespace igl {
  10. class CombinationFinder
  11. {
  12. private:
  13. std::vector<int> combinations;
  14. void add(const std::vector<int>& v,
  15. std::vector<std::vector<int>> &allCombs)
  16. {
  17. allCombs.push_back(v);
  18. }
  19. public:
  20. void doCombs(int offset,
  21. int k,
  22. int N,
  23. std::vector<std::vector<int>> &allCombs)
  24. {
  25. if (k == 0) {
  26. add(combinations,allCombs);
  27. return;
  28. }
  29. for (int i = offset; i <= N - k; ++i) {
  30. combinations.push_back(i);
  31. doCombs(i+1, k-1, N,allCombs);
  32. combinations.pop_back();
  33. }
  34. }
  35. };
  36. }
  37. IGL_INLINE void igl::nchoosek(int offset,
  38. int k,
  39. int N,
  40. std::vector<std::vector<int>> &allCombs)
  41. {
  42. CombinationFinder cmbf;
  43. allCombs.clear();
  44. cmbf.doCombs(offset,k,N,allCombs);
  45. }
  46. #ifndef IGL_HEADER_ONLY
  47. // Explicit template specialization
  48. #endif