def _patternrootsanddirs(kindpats):
    """Returns roots and directories corresponding to each pattern.

    This calculates the roots and directories exactly matching the patterns and
    returns a tuple of (roots, dirs) for each. It does not return other
    directories which may also need to be considered, like the parent
    directories.
    """
    r = []
    d = []
    for kind, pat, source in kindpats:
        if kind in (b'glob', b'rootglob'):
            root = []
            for p in pat.split(b'/'):
                if b'[' in p or b'{' in p or b'*' in p or b'?' in p:
                    break
                root.append(p)
            r.append(b'/'.join(root))
        elif kind in (b'relpath', b'path'):
            if pat == b'.':
                pat = b''
            r.append(pat)
        elif kind in (b'rootfilesin',):
            if pat == b'.':
                pat = b''
            d.append(pat)
        else:
            r.append(b'')
    return r, d
