code: def _indexes_grouped_by_classes(
      sequence: Sequence[SupportsInt],
      search_elements: Union[None, Sequence[int]],
      sort_indexes: bool = True,
      sort_classes: bool = True,
  ) -> Union[List[int], None]:
      result_per_class: Dict[int, List[int]] = OrderedDict()
      result: List[int] = []
  
      # tensor_as_list() handles the situation in which sequence and
      # search_elements are a torch.Tensor
      #
      # Without the tensor_as_list conversion:
      # result_per_class[element].append(idx) -> error
      # because result_per_class[0] won't exist (result_per_class[tensor(0)] will)
  
      sequence_list: List[int] = tensor_as_list(sequence)
      if search_elements is not None:
          search_elements_list = tensor_as_list(search_elements)
      else:
          search_elements_list = torch.unique(
              torch.as_tensor(sequence_list)
          ).tolist()
  
      if sort_classes:
          # Consider that result_per_class is an OrderedDict
          # This means that, if sort_classes is True, the next for statement
          # will initialize the "result_per_class" in sorted order ->
          # -> patterns will be ordered by ascending class ID
          search_elements_list = sorted(search_elements_list)
  
      for search_element in search_elements_list:
          result_per_class[search_element] = []
  
      # Set based "in" operator is **much** faster that its list counterpart!
      search_elements_set = set()
      if search_elements_list is not None:
          search_elements_set = set(search_elements_list)
  
      # Stores each pattern index in the appropriate class list
      for idx, element in enumerate(sequence_list):
          if element in search_elements_set:
              result_per_class[element].append(idx)
  
      # Concatenate all the pattern indexes
      for search_element in search_elements_list:
          if sort_indexes:
              result_per_class[search_element].sort()
          result.extend(result_per_class[search_element])
  
      if result == sequence_list:
          # The resulting index order is the same as the input one
          # Return None to flag that the whole sequence can be
          # taken as it already is
          return None
  
      return result
  
file_path: avalanche/avalanche/benchmarks/scenarios/deprecated/new_classes/nc_utils.py
imports: from collections import OrderedDict
  from typing import List, Sequence, Dict, Any, Union, SupportsInt
  import torch
  from avalanche.benchmarks.utils import _taskaware_classification_subset, SupportedDataset
  from avalanche.benchmarks.utils import tensor_as_list
  from avalanche.benchmarks.utils.classification_dataset import TaskAwareClassificationDataset
label_desc: Use image node_id 1 for calling a global function with example usage: _indexes_grouped_by_classes(sequence, search_elements, sort_indexes, sort_classes) and returns: result, None