项目作者: dstein64

项目描述 :
:door: A single-file C++ library for visiting revolving door combinations.
高级语言: C++
项目地址: git://github.com/dstein64/revdoor.git
创建时间: 2020-02-09T22:42:16Z
项目社区:https://github.com/dstein64/revdoor

开源协议:

下载


Build Status

revdoor

revdoor is a single-file C++ library for visiting revolving door
combinations.

The combinations without replacement generator implements Algorithm R from
TAOCP 7.2.1.3 [1]. The combinations with replacement generator
implements the same algorithm, modified to support replacement.

The algorithms visit combinations by indicating in and out swap values for
one or two items on each iteration.

Quick Start

  1. Include the revdoor.hpp header.
  2. For combinations without replacement, initialize a generator object with
    revdoor::combinations. For combinations with replacement, initialize a
    generator object with revdoor::combinations_with_replacement.
  3. Use the state() method to get the initial state of the generator.
  4. Call the step() method to proceed to the next combination.
    • For combinations without replacement, this takes two int pointers:
      out‘s dereference is set to the element that is removed, and in‘s
      dereference is set to the element that is added.
    • For combinations without replacement, this takes four int pointers:
      out1‘s and out2‘s dereferences are set to elements that are removed;
      in1‘s and in2‘s dereferences are set to elements that are added.
      In cases where only a single item is swapped out, *out1 == *in1.

Example 1: combinations without replacement

  1. #include <revdoor.hpp>
  2. #include <vector>
  3. void process_init_state(const std::vector<int> init_state) { /* ... */ }
  4. void process_change(int out, int in) { /* ... */ }
  5. int main(int argc, char* argv[]) {
  6. int n = 5;
  7. int t = 3;
  8. revdoor::combinations combs(n, t);
  9. std::vector<int> init_state = combs.state();
  10. process_init_state(init_state);
  11. int out, in;
  12. while (combs.step(&out, &in)) {
  13. process_change(out, in);
  14. }
  15. return 0;
  16. }

Example 2: combinations with replacement

  1. #include <revdoor.hpp>
  2. #include <vector>
  3. void process_init_state(const std::vector<int> init_state) { /* ... */ }
  4. void process_change(int out, int in) { /* ... */ }
  5. int main(int argc, char* argv[]) {
  6. int n = 5;
  7. int t = 3;
  8. revdoor::combinations_with_replacement combs_rep(n, t);
  9. std::vector<int> init_state = combs_rep.state();
  10. process_init_state(init_state);
  11. int out1, in1, out2, in2;
  12. while (combs_rep.step(&out1, &in1, &out2, &in2)) {
  13. if (out1 != in1)
  14. process_change(out1, in1);
  15. process_change(out2, in2);
  16. }
  17. return 0;
  18. }

Documentation

Refer to the comments in revdoor.hpp for detailed information.

License

The source code has an MIT License.

See revdoor.hpp.

References

[1] Donald Knuth, The Art of Computer Programming, Volume 4, Fascicle 3: Generating
All Combinations and Partitions (Addison-Wesley Professional, 2005).