nodes: 0, module; 1, function_definition; 2, function_name:_slice_mostly_sorted; 3, parameters; 4, block; 5, identifier:array; 6, identifier:keep; 7, identifier:rest; 8, default_parameter; 9, expression_statement; 10, if_statement; 11, expression_statement; 12, expression_statement; 13, if_statement; 14, expression_statement; 15, expression_statement; 16, for_statement; 17, if_statement; 18, expression_statement; 19, return_statement; 20, identifier:ind; 21, None; 22, comment:"""Slice dask array `array` that is almost entirely sorted already.
  
      We perform approximately `2 * len(keep)` slices on `array`.
      This is OK, since `keep` is small. Individually, each of these slices
      is entirely sorted.
  
      Parameters
      ----------
      array : dask.array.Array
      keep : ndarray[Int]
          This must be sorted.
      rest : ndarray[Bool]
      ind : ndarray[Int], optional
  
      Returns
      -------
      sliced : dask.array.Array
      """; 23, comparison_operator:ind is None; 24, block; 25, assignment; 26, assignment; 27, comparison_operator:keep[0] > 0; 28, comment:# avoid creating empty slices; 29, block; 30, call; 31, assignment; 32, pattern_list; 33, identifier:windows; 34, block; 35, comparison_operator:keep[-1] < len(array) - 1; 36, comment:# avoid creating empty slices; 37, block; 38, assignment; 39, identifier:result; 40, identifier:ind; 41, None; 42, expression_statement; 43, identifier:idx; 44, call; 45, identifier:slices; 46, list; 47, subscript; 48, integer:0; 49, expression_statement; 50, attribute; 51, argument_list; 52, identifier:windows; 53, call; 54, identifier:l; 55, identifier:r; 56, if_statement; 57, expression_statement; 58, subscript; 59, binary_operator:len(array) - 1; 60, expression_statement; 61, identifier:result; 62, call; 63, assignment; 64, attribute; 65, argument_list; 66, identifier:keep; 67, integer:0; 68, call; 69, identifier:slices; 70, identifier:append; 71, list; 72, identifier:zip; 73, argument_list; 74, comparison_operator:r > l + 1; 75, comment:# avoid creating empty slices; 76, block; 77, call; 78, identifier:keep; 79, unary_operator; 80, call; 81, integer:1; 82, call; 83, attribute; 84, argument_list; 85, identifier:ind; 86, call; 87, identifier:np; 88, identifier:argsort; 89, call; 90, attribute; 91, argument_list; 92, subscript; 93, subscript; 94, subscript; 95, identifier:r; 96, binary_operator:l + 1; 97, expression_statement; 98, attribute; 99, argument_list; 100, integer:1; 101, identifier:len; 102, argument_list; 103, attribute; 104, argument_list; 105, identifier:da; 106, identifier:concatenate; 107, list_comprehension; 108, attribute; 109, argument_list; 110, attribute; 111, argument_list; 112, identifier:slices; 113, identifier:append; 114, call; 115, identifier:keep; 116, integer:0; 117, identifier:keep; 118, slice; 119, identifier:keep; 120, slice; 121, identifier:l; 122, integer:1; 123, call; 124, identifier:slices; 125, identifier:append; 126, list; 127, identifier:array; 128, identifier:slices; 129, identifier:append; 130, call; 131, subscript; 132, for_in_clause; 133, identifier:np; 134, identifier:arange; 135, call; 136, identifier:np; 137, identifier:concatenate; 138, list; 139, identifier:slice; 140, argument_list; 141, unary_operator; 142, integer:1; 143, attribute; 144, argument_list; 145, identifier:r; 146, identifier:slice; 147, argument_list; 148, identifier:array; 149, subscript; 150, identifier:slice_; 151, identifier:slices; 152, identifier:len; 153, argument_list; 154, identifier:keep; 155, subscript; 156, None; 157, subscript; 158, integer:1; 159, identifier:slices; 160, identifier:append; 161, call; 162, binary_operator:keep[-1] + 1; 163, None; 164, identifier:idx; 165, identifier:slice_; 166, identifier:array; 167, identifier:ind; 168, identifier:rest; 169, identifier:keep; 170, integer:0; 171, identifier:slice; 172, argument_list; 173, subscript; 174, integer:1; 175, binary_operator:l + 1; 176, identifier:r; 177, identifier:keep; 178, unary_operator; 179, identifier:l; 180, integer:1; 181, integer:1
code: def _slice_mostly_sorted(array, keep, rest, ind=None):
      """Slice dask array `array` that is almost entirely sorted already.
  
      We perform approximately `2 * len(keep)` slices on `array`.
      This is OK, since `keep` is small. Individually, each of these slices
      is entirely sorted.
  
      Parameters
      ----------
      array : dask.array.Array
      keep : ndarray[Int]
          This must be sorted.
      rest : ndarray[Bool]
      ind : ndarray[Int], optional
  
      Returns
      -------
      sliced : dask.array.Array
      """
      if ind is None:
          ind = np.arange(len(array))
      idx = np.argsort(np.concatenate([keep, ind[rest]]))
  
      slices = []
      if keep[0] > 0:  # avoid creating empty slices
          slices.append(slice(None, keep[0]))
      slices.append([keep[0]])
      windows = zip(keep[:-1], keep[1:])
  
      for l, r in windows:
          if r > l + 1:  # avoid creating empty slices
              slices.append(slice(l + 1, r))
          slices.append([r])
  
      if keep[-1] < len(array) - 1:  # avoid creating empty slices
          slices.append(slice(keep[-1] + 1, None))
      result = da.concatenate([array[idx[slice_]] for slice_ in slices])
      return result