Rewrite gui_to_tree_index() for performance

This is now fast even for very long or very complex elements. We used
to be O(n) and is now O(1) if we ignore the fact that str.count() is
technically still O(n), but is implemented natively, so no looping in
python any more.
Rewrite tree_to_gui_index() using treeindex_to_iter()
Rewrite treeindex_to_iter() for performance

This uses a similar trick just introduced for gui_to_tree_index(). This
should improve searching performance a bit more.
Factor out anchor counting code