<commit_before>//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Algorithms open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

extension Sequence {
  /// Implementation for min(count:areInIncreasingOrder:)
  @inlinable
  internal func _minImplementation(
    count: Int,
    sortedBy areInIncreasingOrder: (Element, Element) throws -> Bool
  ) rethrows -> [Element] {
    var iterator = makeIterator()
    
    var result: [Element] = []
    result.reserveCapacity(count)
    while result.count < count, let e = iterator.next() {
      result.append(e)
    }
    try result.sort(by: areInIncreasingOrder)
    
    while let e = iterator.next() {
      // To be part of `result`, `e` must be strictly less than `result.last`.
      guard try areInIncreasingOrder(e, result.last!) else { continue }
      let insertionIndex =
        try result.partitioningIndex { try areInIncreasingOrder(e, $0) }
      
      assert(insertionIndex != result.endIndex)
      result.removeLast()
      result.insert(e, at: insertionIndex)
    }

    return result
  }
  
  /// Implementation for max(count:areInIncreasingOrder:)
  @inlinable
  internal func _maxImplementation(
    count: Int,
    sortedBy areInIncreasingOrder: (Element, Element) throws -> Bool
  ) rethrows -> [Element] {
    var iterator = makeIterator()
    
    var result: [Element] = []
    result.reserveCapacity(count)
    while result.count < count, let e = iterator.next() {
      result.append(e)
    }<commit_msg>Remove the last element before determining the insertion index (#173)<commit_after>//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Algorithms open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

extension Sequence {
  /// Implementation for min(count:areInIncreasingOrder:)
  @inlinable
  internal func _minImplementation(
    count: Int,
    sortedBy areInIncreasingOrder: (Element, Element) throws -> Bool
  ) rethrows -> [Element] {
    var iterator = makeIterator()
    
    var result: [Element] = []
    result.reserveCapacity(count)
    while result.count < count, let e = iterator.next() {
      result.append(e)
    }
    try result.sort(by: areInIncreasingOrder)
    
    while let e = iterator.next() {
      // To be part of `result`, `e` must be strictly less than `result.last`.
      guard try areInIncreasingOrder(e, result.last!) else { continue }
      result.removeLast()
      let insertionIndex =
        try result.partitioningIndex { try areInIncreasingOrder(e, $0) }
      result.insert(e, at: insertionIndex)
    }

    return result
  }
  
  /// Implementation for max(count:areInIncreasingOrder:)
  @inlinable
  internal func _maxImplementation(
    count: Int,
    sortedBy areInIncreasingOrder: (Element, Element) throws -> Bool
  ) rethrows -> [Element] {
    var iterator = makeIterator()
    
    var result: [Element] = []
    result.reserveCapacity(count)
    while result.count < count, let e = iterator.next() {
      result.append(e)
    }
{'fraction_non_alphanumeric': 0.18464097588023287, 'fraction_numerical': 0.0047130579428888274, 'mean_word_length': 2.253381424706943, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4308780', 'n_tokens_mistral': 1011, 'n_tokens_neox': 960, 'n_words': 289}