Last active
August 29, 2015 14:06
-
-
Save ldm5180/7854a666606c871121cd to your computer and use it in GitHub Desktop.
Testing containers for depth-first tree traversal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <typename Container> void tester(int iterations) { | |
srandom(1337); // Seed PRNG to a known value for reproducibility. | |
Container nodesToProcess(1); // Start with the first node to process. | |
for (auto i = 0; i < iterations; ++i) { | |
// Nodes are provided. Isolate and commonize generation. | |
auto rand = random() % 128; // Determine number of nodes at this level. | |
std::vector<typename Container::value_type> nodes(rand); | |
// Process a node. | |
if (!nodesToProcess.empty()) { | |
nodesToProcess.pop_back(); | |
} | |
// Queue up all the children nodes to process. | |
std::for_each(nodes.rbegin(), nodes.rend(), | |
[&nodesToProcess](typename Container::value_type &v) { | |
nodesToProcess.push_back(v); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A more thorough and compile-able example is available at: https://github.com/ldm5180/cppcon14-tdp