I don't deserve any credit for this; @SebAaltonen did all the work to write the C++ version. I just did a line-by-line translation.