'Given list of groups returns dict of group -> list of state events
with matching types. `types` is a list of `(type, state_key)`, where
a `state_key` of None matches all state_keys. If `types` is None then
all events are returned.'

@defer.inlineCallbacks
def _get_state_for_groups(self, groups, types=None):

    if types:
        types = frozenset(types)
    results = {}
    missing_groups = []
    if (types is not None):
        for group in set(groups):
            (state_dict_ids, _, got_all) = self._get_some_state_from_cache(group, types)
            results[group] = state_dict_ids
            if (not got_all):
                missing_groups.append(group)
    else:
        for group in set(groups):
            (state_dict_ids, got_all) = self._get_all_state_from_cache(group)
            results[group] = state_dict_ids
            if (not got_all):
                missing_groups.append(group)
    if missing_groups:
        cache_seq_num = self._state_group_cache.sequence
        group_to_state_dict = (yield self._get_state_groups_from_groups(missing_groups, types))
        for (group, group_state_dict) in group_to_state_dict.iteritems():
            state_dict = results[group]
            state_dict.update((((intern_string(k[0]), intern_string(k[1])), to_ascii(v)) for (k, v) in group_state_dict.iteritems()))
            self._state_group_cache.update(cache_seq_num, key=group, value=state_dict, full=(types is None), known_absent=types)
    defer.returnValue(results)
