// Using standard STL to test the red-black tree in C++ // In glibc++ this uses // With the LLVM libc++ this uses // In glibc this uses eventually: // (Highly optimized in-place red-black tree using the low pointer bit to encode color information.) #include #include #include using std::for_each; typedef int nat; struct nat_lt_fn { bool operator()(nat const & n1, nat const & n2) const { return n1 < n2; } }; typedef std::map map; map mk_map(unsigned n) { map m; while (n > 0) { --n; m.insert(std::make_pair(nat(n), n%10 == 0)); } return m; } nat fold(map const & m) { nat r(0); for_each(m.begin(), m.end(), [&](std::pair const & p) { if (p.second) r = r + nat(1); }); return r; } int main(int argc, char ** argv) { unsigned n = 4200000; if (argc == 2) { n = atoi(argv[1]); } map m = mk_map(n); std::cout << fold(m) << "\n"; return 0; }