c++ port of the python code from https://github.com/gnebehay/CMT