Skip to content

Instantly share code, notes, and snippets.

@ldm5180
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ldm5180/7854a666606c871121cd to your computer and use it in GitHub Desktop.
Save ldm5180/7854a666606c871121cd to your computer and use it in GitHub Desktop.
Testing containers for depth-first tree traversal
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);
});
}
}
@ldm5180
Copy link
Author

ldm5180 commented Sep 11, 2014

A more thorough and compile-able example is available at: https://github.com/ldm5180/cppcon14-tdp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment