'Wrap *func* into another compiled function inserting a runtime-created
mergestate as the first function argument.'

def wrap_with_mergestate(self, timsort, func, _cache={}):

    key = (timsort, func)
    if (key in _cache):
        return _cache[key]
    merge_init = timsort.merge_init
    @timsort.compile
    def wrapper(keys, values, *args):
        ms = merge_init(keys)
        res = func(ms, keys, values, *args)
        return res
    _cache[key] = wrapper
    return wrapper
