ENH: cluster: rewrite and optimize hierarchy in Cython