Skip to content

A possible implement of Deque::sort #2715

@FrozenLemonTee

Description

@FrozenLemonTee

Is your feature request related to a problem? Please describe.
Missing Deque::sort
Related issue: #2706

Describe the solution you'd like

pub fn[A : Compare] sort(self : Deque[A]) -> Unit {
  let total_len = self.length()
  
  // Case 0: Empty
  if total_len == 0 {
    return
  }
  
  let (left_view, right_view) = self.as_views()
  
 // Array provides APIs for Deque to call quick_sort
 // Case 1: Only the left view has data (already continuous)
  if right_view.length() == 0 {
    Array::quick_sort(left_view, None, get_limit(left_view.length()))
    return
  }
  
 // Case 2: Only the right view has data (already continuous)
  if left_view.length() == 0 {
    Array::quick_sort(right_view, None, get_limit(right_view.length()))
    return
  }
  
  // Case 3: Both views have data, make them contiguous through reserve_capacity
  self.reserve_capacity(total_len)
  
  let new_left_view = self.as_views().0
  
  Array::quick_sort(new_left_view, None, get_limit(new_left_view.length()))
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions