code: def _calculate(
      trials: list[optuna.trial.FrozenTrial],
      include_pruned: bool = False,
      search_space: Dict[str, BaseDistribution] | None = None,
      cached_trial_number: int = -1,
  ) -> Tuple[Dict[str, BaseDistribution] | None, int]:
      states_of_interest = [
          optuna.trial.TrialState.COMPLETE,
          optuna.trial.TrialState.WAITING,
          optuna.trial.TrialState.RUNNING,
      ]
  
      if include_pruned:
          states_of_interest.append(optuna.trial.TrialState.PRUNED)
  
      trials_of_interest = [
          trial for trial in trials if trial.state in states_of_interest
      ]
  
      next_cached_trial_number = (
          trials_of_interest[-1].number + 1
          if len(trials_of_interest) > 0
          else -1
      )
      for trial in reversed(trials_of_interest):
          if cached_trial_number > trial.number:
              break
  
          if not trial.state.is_finished():
              next_cached_trial_number = trial.number
              continue
  
          if search_space is None:
              search_space = copy.copy(trial.distributions)
              continue
  
          search_space = {
              name: distribution
              for name, distribution in search_space.items()
              if trial.distributions.get(name) == distribution
          }
  
      return search_space, next_cached_trial_number
  
file_path: optuna/optuna/search_space/intersection.py
imports: from __future__ import annotations
  import copy
  from typing import Dict
  from typing import Tuple
  import optuna
  from optuna.distributions import BaseDistribution
  from optuna.study import Study
label_desc: Use image node_id 1 for calling a global function with example usage: _calculate(trials, include_pruned, search_space, cached_trial_number) and returns: search_space,  next_cached_trial_number