from typing import Dict
from typing import Any
from typing import Type


def to_nested_dict(flatten_dict: Dict[str, Any],
                   generator: Type = dict) -> Dict[str, Any]:
  """Convert a flat dict to a hierarchical dict.

  The input dict's hierarchy is denoted by ``.``.

  Example:
    ::

      >>> to_nested_dict({"a.b": 2333, "a.c": 666})
      {"a": {"b": 2333, "c": 666}}

  Args:
    flatten_dict: a dict whose keys list contains ``.`` for hierarchical
      representation.
    generator: a type of mapping. Default to ``dict``.
  """
  ret: Dict[str, Any] = generator()
  for k, v in flatten_dict.items():
    segments = k.split(".")
    ptr = ret
    for s in segments[:-1]:
      if s not in ptr:
        ptr[s] = generator()
      ptr = ptr[s]
    ptr[segments[-1]] = v
  return ret