public inline fun < T , R , C : MutableCollection < in R > > Iterable < T > . mapIndexedTo ( destination : C , transform : ( index : Int , T ) -> R ) : C

{ <EOL> var index = <NUM_LIT:0> <EOL> for ( item in this ) <EOL> destination . add ( transform ( checkIndexOverflow ( index ++ ) , item ) ) <EOL> return destination <EOL> }

/**
 * Applies the given [transform] function to each element and its index in the original collection
 * and appends the results to the given [destination].
 * @param [transform] function that takes the index of an element and the element itself
 * and returns the result of the transform applied to the element.
 */


public inline fun < T , R : Any > Iterable < T > . mapNotNull ( transform : ( T ) -> R ? ) : List < R >

{ <EOL> return mapNotNullTo ( ArrayList < R > ( ) , transform ) <EOL> }

/**
 * Returns a list containing only the non-null results of applying the given [transform] function
 * to each element in the original collection.
 *
 * @sample samples.collections.Collections.Transformations.mapNotNull
 */


public inline fun < T , R : Any , C : MutableCollection < in R > > Iterable < T > . mapNotNullTo ( destination : C , transform : ( T ) -> R ? ) : C

{ <EOL> forEach { element -> transform ( element ) ? . let { destination . add ( it ) } } <EOL> return destination <EOL> }

/**
 * Applies the given [transform] function to each element in the original collection
 * and appends only the non-null results to the given [destination].
 */


public inline fun < T , R , C : MutableCollection < in R > > Iterable < T > . mapTo ( destination : C , transform : ( T ) -> R ) : C

{ <EOL> for ( item in this ) <EOL> destination . add ( transform ( item ) ) <EOL> return destination <EOL> }

/**
 * Applies the given [transform] function to each element of the original collection
 * and appends the results to the given [destination].
 */


public fun < T > Iterable < T > . withIndex ( ) : Iterable < IndexedValue < T > >

{ <EOL> return IndexingIterable { iterator ( ) } <EOL> }

/**
 * Returns a lazy [Iterable] that wraps each element of the original collection
 * into an [IndexedValue] containing the index of that element and the element itself.
 */


public fun < T > Iterable < T > . distinct ( ) : List < T >

{ <EOL> return this . toMutableSet ( ) . toList ( ) <EOL> }

/**
 * Returns a list containing only distinct elements from the given collection.
 *
 * Among equal elements of the given collection, only the first one will be present in the resulting list.
 * The elements in the resulting list are in the same order as they were in the source collection.
 *
 * @sample samples.collections.Collections.Transformations.distinctAndDistinctBy
 */


public inline fun < T , K > Iterable < T > . distinctBy ( selector : ( T ) -> K ) : List < T >

{ <EOL> val set = HashSet < K > ( ) <EOL> val list = ArrayList < T > ( ) <EOL> for ( e in this ) { <EOL> val key = selector ( e ) <EOL> if ( set . add ( key ) ) <EOL> list . add ( e ) <EOL> } <EOL> return list <EOL> }

/**
 * Returns a list containing only elements from the given collection
 * having distinct keys returned by the given [selector] function.
 *
 * Among elements of the given collection with equal keys, only the first one will be present in the resulting list.
 * The elements in the resulting list are in the same order as they were in the source collection.
 *
 * @sample samples.collections.Collections.Transformations.distinctAndDistinctBy
 */


public infix fun < T > Iterable < T > . intersect ( other : Iterable < T > ) : Set < T >

{ <EOL> val set = this . toMutableSet ( ) <EOL> set . retainAll ( other ) <EOL> return set <EOL> }

/**
 * Returns a set containing all elements that are contained by both this collection and the specified collection.
 *
 * The returned set preserves the element iteration order of the original collection.
 *
 * To get a set containing all elements that are contained at least in one of these collections use [union].
 */


public infix fun < T > Iterable < T > . subtract ( other : Iterable < T > ) : Set < T >

{ <EOL> val set = this . toMutableSet ( ) <EOL> set . removeAll ( other ) <EOL> return set <EOL> }

/**
 * Returns a set containing all elements that are contained by this collection and not contained by the specified collection.
 *
 * The returned set preserves the element iteration order of the original collection.
 */


public fun < T > Iterable < T > . toMutableSet ( ) : MutableSet < T >

{ <EOL> return when ( this ) { <EOL> is Collection < T > -> LinkedHashSet ( this ) <EOL> else -> toCollection ( LinkedHashSet < T > ( ) ) <EOL> } <EOL> }

/**
 * Returns a new [MutableSet] containing all distinct elements from the given collection.
 *
 * The returned set preserves the element iteration order of the original collection.
 */


public infix fun < T > Iterable < T > . union ( other : Iterable < T > ) : Set < T >

{ <EOL> val set = this . toMutableSet ( ) <EOL> set . addAll ( other ) <EOL> return set <EOL> }

/**
 * Returns a set containing all distinct elements from both collections.
 *
 * The returned set preserves the element iteration order of the original collection.
 * Those elements of the [other] collection that are unique are iterated in the end
 * in the order of the [other] collection.
 *
 * To get a set containing all elements that are contained in both collections use [intersect].
 */


public inline fun < T > Iterable < T > . all ( predicate : ( T ) -> Boolean ) : Boolean

{ <EOL> if ( this is Collection && isEmpty ( ) ) return true <EOL> for ( element in this ) if ( ! predicate ( element ) ) return false <EOL> return true <EOL> }

/**
 * Returns `true` if all elements match the given [predicate].
 *
 * Note that if the collection contains no elements, the function returns `true`
 * because there are no elements in it that _do not_ match the predicate.
 * See a more detailed explanation of this logic concept in ["Vacuous truth"](https://en.wikipedia.org/wiki/Vacuous_truth) article.
 *
 * @sample samples.collections.Collections.Aggregates.all
 */


public fun < T > Iterable < T > . any ( ) : Boolean

{ <EOL> if ( this is Collection ) return ! isEmpty ( ) <EOL> return iterator ( ) . hasNext ( ) <EOL> }

/**
 * Returns `true` if collection has at least one element.
 *
 * @sample samples.collections.Collections.Aggregates.any
 */


public inline fun < T > Iterable < T > . any ( predicate : ( T ) -> Boolean ) : Boolean

{ <EOL> if ( this is Collection && isEmpty ( ) ) return false <EOL> for ( element in this ) if ( predicate ( element ) ) return true <EOL> return false <EOL> }

/**
 * Returns `true` if at least one element matches the given [predicate].
 *
 * @sample samples.collections.Collections.Aggregates.anyWithPredicate
 */


public fun < T > Iterable < T > . count ( ) : Int

{ <EOL> if ( this is Collection ) return size <EOL> var count = <NUM_LIT:0> <EOL> for ( element in this ) checkCountOverflow ( ++ count ) <EOL> return count <EOL> }

/**
 * Returns the number of elements in this collection.
 */


@ kotlin . internal . InlineOnly <EOL> public inline fun < T > Collection < T > . count ( ) : Int

{ <EOL> return size <EOL> }


public inline fun < T > Iterable < T > . count ( predicate : ( T ) -> Boolean ) : Int

{ <EOL> if ( this is Collection && isEmpty ( ) ) return <NUM_LIT:0> <EOL> var count = <NUM_LIT:0> <EOL> for ( element in this ) if ( predicate ( element ) ) checkCountOverflow ( ++ count ) <EOL> return count <EOL> }

/**
 * Returns the number of elements matching the given [predicate].
 */


public inline fun < T , R > Iterable < T > . fold ( initial : R , operation : ( acc : R , T ) -> R ) : R

{ <EOL> var accumulator = initial <EOL> for ( element in this ) accumulator = operation ( accumulator , element ) <EOL> return accumulator <EOL> }

/**
 * Accumulates value starting with [initial] value and applying [operation] from left to right
 * to current accumulator value and each element.
 *
 * Returns the specified [initial] value if the collection is empty.
 *
 * @param [operation] function that takes current accumulator value and an element, and calculates the next accumulator value.
 */


public inline fun < T , R > Iterable < T > . foldIndexed ( initial : R , operation : ( index : Int , acc : R , T ) -> R ) : R

{ <EOL> var index = <NUM_LIT:0> <EOL> var accumulator = initial <EOL> for ( element in this ) accumulator = operation ( checkIndexOverflow ( index ++ ) , accumulator , element ) <EOL> return accumulator <EOL> }

/**
 * Accumulates value starting with [initial] value and applying [operation] from left to right
 * to current accumulator value and each element with its index in the original collection.
 *
 * Returns the specified [initial] value if the collection is empty.
 *
 * @param [operation] function that takes the index of an element, current accumulator value
 * and the element itself, and calculates the next accumulator value.
 */


public inline fun < T , R > List < T > . foldRight ( initial : R , operation : ( T , acc : R ) -> R ) : R

{ <EOL> var accumulator = initial <EOL> if ( ! isEmpty ( ) ) { <EOL> val iterator = listIterator ( size ) <EOL> while ( iterator . hasPrevious ( ) ) { <EOL> accumulator = operation ( iterator . previous ( ) , accumulator ) <EOL> } <EOL> } <EOL> return accumulator <EOL> }

/**
 * Accumulates value starting with [initial] value and applying [operation] from right to left
 * to each element and current accumulator value.
 *
 * Returns the specified [initial] value if the list is empty.
 *
 * @param [operation] function that takes an element and current accumulator value, and calculates the next accumulator value.
 */


public inline fun < T , R > List < T > . foldRightIndexed ( initial : R , operation : ( index : Int , T , acc : R ) -> R ) : R

{ <EOL> var accumulator = initial <EOL> if ( ! isEmpty ( ) ) { <EOL> val iterator = listIterator ( size ) <EOL> while ( iterator . hasPrevious ( ) ) { <EOL> val index = iterator . previousIndex ( ) <EOL> accumulator = operation ( index , iterator . previous ( ) , accumulator ) <EOL> } <EOL> } <EOL> return accumulator <EOL> }

/**
 * Accumulates value starting with [initial] value and applying [operation] from right to left
 * to each element with its index in the original list and current accumulator value.
 *
 * Returns the specified [initial] value if the list is empty.
 *
 * @param [operation] function that takes the index of an element, the element itself
 * and current accumulator value, and calculates the next accumulator value.
 */


@ kotlin . internal . HidesMembers <EOL> public inline fun < T > Iterable < T > . forEach ( action : ( T ) -> Unit ) : Unit

{ <EOL> for ( element in this ) action ( element ) <EOL> }

/**
 * Performs the given [action] on each element.
 */


public inline fun < T > Iterable < T > . forEachIndexed ( action : ( index : Int , T ) -> Unit ) : Unit

{ <EOL> var index = <NUM_LIT:0> <EOL> for ( item in this ) action ( checkIndexOverflow ( index ++ ) , item ) <EOL> }

/**
 * Performs the given [action] on each element, providing sequential index with the element.
 * @param [action] function that takes the index of an element and the element itself
 * and performs the action on the element.
 */


@ SinceKotlin ( "<STR_LIT:1.7>" ) <EOL> @ kotlin . jvm . JvmName ( "<STR_LIT>" ) <EOL> @ Suppress ( "<STR_LIT:CONFLICTING_OVERLOADS>" ) <EOL> public fun Iterable < Double > . max ( ) : Double

{ <EOL> val iterator = iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) throw NoSuchElementException ( ) <EOL> var max = iterator . next ( ) <EOL> while ( iterator . hasNext ( ) ) { <EOL> val e = iterator . next ( ) <EOL> max = maxOf ( max , e ) <EOL> } <EOL> return max <EOL> }

/**
 * Returns the largest element.
 *
 * If any of elements is `NaN` returns `NaN`.
 *
 * @throws NoSuchElementException if the collection is empty.
 */


@ SinceKotlin ( "<STR_LIT:1.7>" ) <EOL> @ kotlin . jvm . JvmName ( "<STR_LIT>" ) <EOL> @ Suppress ( "<STR_LIT:CONFLICTING_OVERLOADS>" ) <EOL> public fun Iterable < Float > . max ( ) : Float


@ SinceKotlin ( "<STR_LIT:1.7>" ) <EOL> @ kotlin . jvm . JvmName ( "<STR_LIT>" ) <EOL> @ Suppress ( "<STR_LIT:CONFLICTING_OVERLOADS>" ) <EOL> public fun < T : Comparable < T > > Iterable < T > . max ( ) : T

{ <EOL> val iterator = iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) throw NoSuchElementException ( ) <EOL> var max = iterator . next ( ) <EOL> while ( iterator . hasNext ( ) ) { <EOL> val e = iterator . next ( ) <EOL> if ( max < e ) max = e <EOL> } <EOL> return max <EOL> }

/**
 * Returns the largest element.
 *
 * @throws NoSuchElementException if the collection is empty.
 */


@ SinceKotlin ( "<STR_LIT:1.7>" ) <EOL> @ kotlin . jvm . JvmName ( "<STR_LIT>" ) <EOL> @ Suppress ( "<STR_LIT:CONFLICTING_OVERLOADS>" ) <EOL> public inline fun < T , R : Comparable < R > > Iterable < T > . maxBy ( selector : ( T ) -> R ) : T

{ <EOL> val iterator = iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) throw NoSuchElementException ( ) <EOL> var maxElem = iterator . next ( ) <EOL> if ( ! iterator . hasNext ( ) ) return maxElem <EOL> var maxValue = selector ( maxElem ) <EOL> do { <EOL> val e = iterator . next ( ) <EOL> val v = selector ( e ) <EOL> if ( maxValue < v ) { <EOL> maxElem = e <EOL> maxValue = v <EOL> } <EOL> } while ( iterator . hasNext ( ) ) <EOL> return maxElem <EOL> }

/**
 * Returns the first element yielding the largest value of the given function.
 *
 * @throws NoSuchElementException if the collection is empty.
 *
 * @sample samples.collections.Collections.Aggregates.maxBy
 */


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> public inline fun < T , R : Comparable < R > > Iterable < T > . maxByOrNull ( selector : ( T ) -> R ) : T ?

{ <EOL> val iterator = iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) return null <EOL> var maxElem = iterator . next ( ) <EOL> if ( ! iterator . hasNext ( ) ) return maxElem <EOL> var maxValue = selector ( maxElem ) <EOL> do { <EOL> val e = iterator . next ( ) <EOL> val v = selector ( e ) <EOL> if ( maxValue < v ) { <EOL> maxElem = e <EOL> maxValue = v <EOL> } <EOL> } while ( iterator . hasNext ( ) ) <EOL> return maxElem <EOL> }

/**
 * Returns the first element yielding the largest value of the given function or `null` if there are no elements.
 *
 * @sample samples.collections.Collections.Aggregates.maxByOrNull
 */


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> @ OptIn ( kotlin . experimental . ExperimentalTypeInference :: class ) <EOL> @ OverloadResolutionByLambdaReturnType <EOL> @ kotlin . internal . InlineOnly <EOL> public inline fun < T > Iterable < T > . maxOf ( selector : ( T ) -> Double ) : Double

{ <EOL> val iterator = iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) throw NoSuchElementException ( ) <EOL> var maxValue = selector ( iterator . next ( ) ) <EOL> while ( iterator . hasNext ( ) ) { <EOL> val v = selector ( iterator . next ( ) ) <EOL> maxValue = maxOf ( maxValue , v ) <EOL> } <EOL> return maxValue <EOL> }

/**
 * Returns the largest value among all values produced by [selector] function
 * applied to each element in the collection.
 *
 * If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
 *
 * @throws NoSuchElementException if the collection is empty.
 */


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> @ OptIn ( kotlin . experimental . ExperimentalTypeInference :: class ) <EOL> @ OverloadResolutionByLambdaReturnType <EOL> @ kotlin . internal . InlineOnly <EOL> public inline fun < T > Iterable < T > . maxOf ( selector : ( T ) -> Float ) : Float


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> @ OptIn ( kotlin . experimental . ExperimentalTypeInference :: class ) <EOL> @ OverloadResolutionByLambdaReturnType <EOL> @ kotlin . internal . InlineOnly <EOL> public inline fun < T , R : Comparable < R > > Iterable < T > . maxOf ( selector : ( T ) -> R ) : R

{ <EOL> val iterator = iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) throw NoSuchElementException ( ) <EOL> var maxValue = selector ( iterator . next ( ) ) <EOL> while ( iterator . hasNext ( ) ) { <EOL> val v = selector ( iterator . next ( ) ) <EOL> if ( maxValue < v ) { <EOL> maxValue = v <EOL> } <EOL> } <EOL> return maxValue <EOL> }

/**
 * Returns the largest value among all values produced by [selector] function
 * applied to each element in the collection.
 *
 * @throws NoSuchElementException if the collection is empty.
 */


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> @ OptIn ( kotlin . experimental . ExperimentalTypeInference :: class ) <EOL> @ OverloadResolutionByLambdaReturnType <EOL> @ kotlin . internal . InlineOnly <EOL> public inline fun < T > Iterable < T > . maxOfOrNull ( selector : ( T ) -> Double ) : Double ?

{ <EOL> val iterator = iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) return null <EOL> var maxValue = selector ( iterator . next ( ) ) <EOL> while ( iterator . hasNext ( ) ) { <EOL> val v = selector ( iterator . next ( ) ) <EOL> maxValue = maxOf ( maxValue , v ) <EOL> } <EOL> return maxValue <EOL> }

/**
 * Returns the largest value among all values produced by [selector] function
 * applied to each element in the collection or `null` if there are no elements.
 *
 * If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
 */


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> @ OptIn ( kotlin . experimental . ExperimentalTypeInference :: class ) <EOL> @ OverloadResolutionByLambdaReturnType <EOL> @ kotlin . internal . InlineOnly <EOL> public inline fun < T > Iterable < T > . maxOfOrNull ( selector : ( T ) -> Float ) : Float ?


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> @ OptIn ( kotlin . experimental . ExperimentalTypeInference :: class ) <EOL> @ OverloadResolutionByLambdaReturnType <EOL> @ kotlin . internal . InlineOnly <EOL> public inline fun < T , R : Comparable < R > > Iterable < T > . maxOfOrNull ( selector : ( T ) -> R ) : R ?

{ <EOL> val iterator = iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) return null <EOL> var maxValue = selector ( iterator . next ( ) ) <EOL> while ( iterator . hasNext ( ) ) { <EOL> val v = selector ( iterator . next ( ) ) <EOL> if ( maxValue < v ) { <EOL> maxValue = v <EOL> } <EOL> } <EOL> return maxValue <EOL> }

/**
 * Returns the largest value among all values produced by [selector] function
 * applied to each element in the collection or `null` if there are no elements.
 */


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> @ OptIn ( kotlin . experimental . ExperimentalTypeInference :: class ) <EOL> @ OverloadResolutionByLambdaReturnType <EOL> @ kotlin . internal . InlineOnly <EOL> public inline fun < T , R > Iterable < T > . maxOfWith ( comparator : Comparator < in R > , selector : ( T ) -> R ) : R

{ <EOL> val iterator = iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) throw NoSuchElementException ( ) <EOL> var maxValue = selector ( iterator . next ( ) ) <EOL> while ( iterator . hasNext ( ) ) { <EOL> val v = selector ( iterator . next ( ) ) <EOL> if ( comparator . compare ( maxValue , v ) < <NUM_LIT:0> ) { <EOL> maxValue = v <EOL> } <EOL> } <EOL> return maxValue <EOL> }

/**
 * Returns the largest value according to the provided [comparator]
 * among all values produced by [selector] function applied to each element in the collection.
 *
 * @throws NoSuchElementException if the collection is empty.
 */


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> @ OptIn ( kotlin . experimental . ExperimentalTypeInference :: class ) <EOL> @ OverloadResolutionByLambdaReturnType <EOL> @ kotlin . internal . InlineOnly <EOL> public inline fun < T , R > Iterable < T > . maxOfWithOrNull ( comparator : Comparator < in R > , selector : ( T ) -> R ) : R ?

{ <EOL> val iterator = iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) return null <EOL> var maxValue = selector ( iterator . next ( ) ) <EOL> while ( iterator . hasNext ( ) ) { <EOL> val v = selector ( iterator . next ( ) ) <EOL> if ( comparator . compare ( maxValue , v ) < <NUM_LIT:0> ) { <EOL> maxValue = v <EOL> } <EOL> } <EOL> return maxValue <EOL> }

/**
 * Returns the largest value according to the provided [comparator]
 * among all values produced by [selector] function applied to each element in the collection or `null` if there are no elements.
 */


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> public fun Iterable < Double > . maxOrNull ( ) : Double ?

{ <EOL> val iterator = iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) return null <EOL> var max = iterator . next ( ) <EOL> while ( iterator . hasNext ( ) ) { <EOL> val e = iterator . next ( ) <EOL> max = maxOf ( max , e ) <EOL> } <EOL> return max <EOL> }

/**
 * Returns the largest element or `null` if there are no elements.
 *
 * If any of elements is `NaN` returns `NaN`.
 */


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> public fun Iterable < Float > . maxOrNull ( ) : Float ?


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> public fun < T : Comparable < T > > Iterable < T > . maxOrNull ( ) : T ?

{ <EOL> val iterator = iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) return null <EOL> var max = iterator . next ( ) <EOL> while ( iterator . hasNext ( ) ) { <EOL> val e = iterator . next ( ) <EOL> if ( max < e ) max = e <EOL> } <EOL> return max <EOL> }

/**
 * Returns the largest element or `null` if there are no elements.
 */


@ SinceKotlin ( "<STR_LIT:1.7>" ) <EOL> @ kotlin . jvm . JvmName ( "<STR_LIT>" ) <EOL> @ Suppress ( "<STR_LIT:CONFLICTING_OVERLOADS>" ) <EOL> public fun < T > Iterable < T > . maxWith ( comparator : Comparator < in T > ) : T

{ <EOL> val iterator = iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) throw NoSuchElementException ( ) <EOL> var max = iterator . next ( ) <EOL> while ( iterator . hasNext ( ) ) { <EOL> val e = iterator . next ( ) <EOL> if ( comparator . compare ( max , e ) < <NUM_LIT:0> ) max = e <EOL> } <EOL> return max <EOL> }

/**
 * Returns the first element having the largest value according to the provided [comparator].
 *
 * @throws NoSuchElementException if the collection is empty.
 */


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> public fun < T > Iterable < T > . maxWithOrNull ( comparator : Comparator < in T > ) : T ?

{ <EOL> val iterator = iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) return null <EOL> var max = iterator . next ( ) <EOL> while ( iterator . hasNext ( ) ) { <EOL> val e = iterator . next ( ) <EOL> if ( comparator . compare ( max , e ) < <NUM_LIT:0> ) max = e <EOL> } <EOL> return max <EOL> }

/**
 * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements.
 */


@ SinceKotlin ( "<STR_LIT:1.7>" ) <EOL> @ kotlin . jvm . JvmName ( "<STR_LIT>" ) <EOL> @ Suppress ( "<STR_LIT:CONFLICTING_OVERLOADS>" ) <EOL> public fun Iterable < Double > . min ( ) : Double

{ <EOL> val iterator = iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) throw NoSuchElementException ( ) <EOL> var min = iterator . next ( ) <EOL> while ( iterator . hasNext ( ) ) { <EOL> val e = iterator . next ( ) <EOL> min = minOf ( min , e ) <EOL> } <EOL> return min <EOL> }

/**
 * Returns the smallest element.
 *
 * If any of elements is `NaN` returns `NaN`.
 *
 * @throws NoSuchElementException if the collection is empty.
 */


@ SinceKotlin ( "<STR_LIT:1.7>" ) <EOL> @ kotlin . jvm . JvmName ( "<STR_LIT>" ) <EOL> @ Suppress ( "<STR_LIT:CONFLICTING_OVERLOADS>" ) <EOL> public fun Iterable < Float > . min ( ) : Float


@ SinceKotlin ( "<STR_LIT:1.7>" ) <EOL> @ kotlin . jvm . JvmName ( "<STR_LIT>" ) <EOL> @ Suppress ( "<STR_LIT:CONFLICTING_OVERLOADS>" ) <EOL> public fun < T : Comparable < T > > Iterable < T > . min ( ) : T

{ <EOL> val iterator = iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) throw NoSuchElementException ( ) <EOL> var min = iterator . next ( ) <EOL> while ( iterator . hasNext ( ) ) { <EOL> val e = iterator . next ( ) <EOL> if ( min > e ) min = e <EOL> } <EOL> return min <EOL> }

/**
 * Returns the smallest element.
 *
 * @throws NoSuchElementException if the collection is empty.
 */


@ SinceKotlin ( "<STR_LIT:1.7>" ) <EOL> @ kotlin . jvm . JvmName ( "<STR_LIT>" ) <EOL> @ Suppress ( "<STR_LIT:CONFLICTING_OVERLOADS>" ) <EOL> public inline fun < T , R : Comparable < R > > Iterable < T > . minBy ( selector : ( T ) -> R ) : T

{ <EOL> val iterator = iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) throw NoSuchElementException ( ) <EOL> var minElem = iterator . next ( ) <EOL> if ( ! iterator . hasNext ( ) ) return minElem <EOL> var minValue = selector ( minElem ) <EOL> do { <EOL> val e = iterator . next ( ) <EOL> val v = selector ( e ) <EOL> if ( minValue > v ) { <EOL> minElem = e <EOL> minValue = v <EOL> } <EOL> } while ( iterator . hasNext ( ) ) <EOL> return minElem <EOL> }

/**
 * Returns the first element yielding the smallest value of the given function.
 *
 * @throws NoSuchElementException if the collection is empty.
 *
 * @sample samples.collections.Collections.Aggregates.minBy
 */


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> public inline fun < T , R : Comparable < R > > Iterable < T > . minByOrNull ( selector : ( T ) -> R ) : T ?

{ <EOL> val iterator = iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) return null <EOL> var minElem = iterator . next ( ) <EOL> if ( ! iterator . hasNext ( ) ) return minElem <EOL> var minValue = selector ( minElem ) <EOL> do { <EOL> val e = iterator . next ( ) <EOL> val v = selector ( e ) <EOL> if ( minValue > v ) { <EOL> minElem = e <EOL> minValue = v <EOL> } <EOL> } while ( iterator . hasNext ( ) ) <EOL> return minElem <EOL> }

/**
 * Returns the first element yielding the smallest value of the given function or `null` if there are no elements.
 *
 * @sample samples.collections.Collections.Aggregates.minByOrNull
 */


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> @ OptIn ( kotlin . experimental . ExperimentalTypeInference :: class ) <EOL> @ OverloadResolutionByLambdaReturnType <EOL> @ kotlin . internal . InlineOnly <EOL> public inline fun < T > Iterable < T > . minOf ( selector : ( T ) -> Double ) : Double

{ <EOL> val iterator = iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) throw NoSuchElementException ( ) <EOL> var minValue = selector ( iterator . next ( ) ) <EOL> while ( iterator . hasNext ( ) ) { <EOL> val v = selector ( iterator . next ( ) ) <EOL> minValue = minOf ( minValue , v ) <EOL> } <EOL> return minValue <EOL> }

/**
 * Returns the smallest value among all values produced by [selector] function
 * applied to each element in the collection.
 *
 * If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
 *
 * @throws NoSuchElementException if the collection is empty.
 */


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> @ OptIn ( kotlin . experimental . ExperimentalTypeInference :: class ) <EOL> @ OverloadResolutionByLambdaReturnType <EOL> @ kotlin . internal . InlineOnly <EOL> public inline fun < T > Iterable < T > . minOf ( selector : ( T ) -> Float ) : Float


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> @ OptIn ( kotlin . experimental . ExperimentalTypeInference :: class ) <EOL> @ OverloadResolutionByLambdaReturnType <EOL> @ kotlin . internal . InlineOnly <EOL> public inline fun < T , R : Comparable < R > > Iterable < T > . minOf ( selector : ( T ) -> R ) : R

{ <EOL> val iterator = iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) throw NoSuchElementException ( ) <EOL> var minValue = selector ( iterator . next ( ) ) <EOL> while ( iterator . hasNext ( ) ) { <EOL> val v = selector ( iterator . next ( ) ) <EOL> if ( minValue > v ) { <EOL> minValue = v <EOL> } <EOL> } <EOL> return minValue <EOL> }

/**
 * Returns the smallest value among all values produced by [selector] function
 * applied to each element in the collection.
 *
 * @throws NoSuchElementException if the collection is empty.
 */


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> @ OptIn ( kotlin . experimental . ExperimentalTypeInference :: class ) <EOL> @ OverloadResolutionByLambdaReturnType <EOL> @ kotlin . internal . InlineOnly <EOL> public inline fun < T > Iterable < T > . minOfOrNull ( selector : ( T ) -> Double ) : Double ?

{ <EOL> val iterator = iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) return null <EOL> var minValue = selector ( iterator . next ( ) ) <EOL> while ( iterator . hasNext ( ) ) { <EOL> val v = selector ( iterator . next ( ) ) <EOL> minValue = minOf ( minValue , v ) <EOL> } <EOL> return minValue <EOL> }

/**
 * Returns the smallest value among all values produced by [selector] function
 * applied to each element in the collection or `null` if there are no elements.
 *
 * If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
 */


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> @ OptIn ( kotlin . experimental . ExperimentalTypeInference :: class ) <EOL> @ OverloadResolutionByLambdaReturnType <EOL> @ kotlin . internal . InlineOnly <EOL> public inline fun < T > Iterable < T > . minOfOrNull ( selector : ( T ) -> Float ) : Float ?


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> @ OptIn ( kotlin . experimental . ExperimentalTypeInference :: class ) <EOL> @ OverloadResolutionByLambdaReturnType <EOL> @ kotlin . internal . InlineOnly <EOL> public inline fun < T , R : Comparable < R > > Iterable < T > . minOfOrNull ( selector : ( T ) -> R ) : R ?

{ <EOL> val iterator = iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) return null <EOL> var minValue = selector ( iterator . next ( ) ) <EOL> while ( iterator . hasNext ( ) ) { <EOL> val v = selector ( iterator . next ( ) ) <EOL> if ( minValue > v ) { <EOL> minValue = v <EOL> } <EOL> } <EOL> return minValue <EOL> }

/**
 * Returns the smallest value among all values produced by [selector] function
 * applied to each element in the collection or `null` if there are no elements.
 */


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> @ OptIn ( kotlin . experimental . ExperimentalTypeInference :: class ) <EOL> @ OverloadResolutionByLambdaReturnType <EOL> @ kotlin . internal . InlineOnly <EOL> public inline fun < T , R > Iterable < T > . minOfWith ( comparator : Comparator < in R > , selector : ( T ) -> R ) : R

{ <EOL> val iterator = iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) throw NoSuchElementException ( ) <EOL> var minValue = selector ( iterator . next ( ) ) <EOL> while ( iterator . hasNext ( ) ) { <EOL> val v = selector ( iterator . next ( ) ) <EOL> if ( comparator . compare ( minValue , v ) > <NUM_LIT:0> ) { <EOL> minValue = v <EOL> } <EOL> } <EOL> return minValue <EOL> }

/**
 * Returns the smallest value according to the provided [comparator]
 * among all values produced by [selector] function applied to each element in the collection.
 *
 * @throws NoSuchElementException if the collection is empty.
 */


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> @ OptIn ( kotlin . experimental . ExperimentalTypeInference :: class ) <EOL> @ OverloadResolutionByLambdaReturnType <EOL> @ kotlin . internal . InlineOnly <EOL> public inline fun < T , R > Iterable < T > . minOfWithOrNull ( comparator : Comparator < in R > , selector : ( T ) -> R ) : R ?

{ <EOL> val iterator = iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) return null <EOL> var minValue = selector ( iterator . next ( ) ) <EOL> while ( iterator . hasNext ( ) ) { <EOL> val v = selector ( iterator . next ( ) ) <EOL> if ( comparator . compare ( minValue , v ) > <NUM_LIT:0> ) { <EOL> minValue = v <EOL> } <EOL> } <EOL> return minValue <EOL> }

/**
 * Returns the smallest value according to the provided [comparator]
 * among all values produced by [selector] function applied to each element in the collection or `null` if there are no elements.
 */


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> public fun Iterable < Double > . minOrNull ( ) : Double ?

{ <EOL> val iterator = iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) return null <EOL> var min = iterator . next ( ) <EOL> while ( iterator . hasNext ( ) ) { <EOL> val e = iterator . next ( ) <EOL> min = minOf ( min , e ) <EOL> } <EOL> return min <EOL> }

/**
 * Returns the smallest element or `null` if there are no elements.
 *
 * If any of elements is `NaN` returns `NaN`.
 */


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> public fun Iterable < Float > . minOrNull ( ) : Float ?


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> public fun < T : Comparable < T > > Iterable < T > . minOrNull ( ) : T ?

{ <EOL> val iterator = iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) return null <EOL> var min = iterator . next ( ) <EOL> while ( iterator . hasNext ( ) ) { <EOL> val e = iterator . next ( ) <EOL> if ( min > e ) min = e <EOL> } <EOL> return min <EOL> }

/**
 * Returns the smallest element or `null` if there are no elements.
 */


@ SinceKotlin ( "<STR_LIT:1.7>" ) <EOL> @ kotlin . jvm . JvmName ( "<STR_LIT>" ) <EOL> @ Suppress ( "<STR_LIT:CONFLICTING_OVERLOADS>" ) <EOL> public fun < T > Iterable < T > . minWith ( comparator : Comparator < in T > ) : T

{ <EOL> val iterator = iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) throw NoSuchElementException ( ) <EOL> var min = iterator . next ( ) <EOL> while ( iterator . hasNext ( ) ) { <EOL> val e = iterator . next ( ) <EOL> if ( comparator . compare ( min , e ) > <NUM_LIT:0> ) min = e <EOL> } <EOL> return min <EOL> }

/**
 * Returns the first element having the smallest value according to the provided [comparator].
 *
 * @throws NoSuchElementException if the collection is empty.
 */


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> public fun < T > Iterable < T > . minWithOrNull ( comparator : Comparator < in T > ) : T ?

{ <EOL> val iterator = iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) return null <EOL> var min = iterator . next ( ) <EOL> while ( iterator . hasNext ( ) ) { <EOL> val e = iterator . next ( ) <EOL> if ( comparator . compare ( min , e ) > <NUM_LIT:0> ) min = e <EOL> } <EOL> return min <EOL> }

/**
 * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements.
 */


public fun < T > Iterable < T > . none ( ) : Boolean

{ <EOL> if ( this is Collection ) return isEmpty ( ) <EOL> return ! iterator ( ) . hasNext ( ) <EOL> }

/**
 * Returns `true` if the collection has no elements.
 *
 * @sample samples.collections.Collections.Aggregates.none
 */


public inline fun < T > Iterable < T > . none ( predicate : ( T ) -> Boolean ) : Boolean

{ <EOL> if ( this is Collection && isEmpty ( ) ) return true <EOL> for ( element in this ) if ( predicate ( element ) ) return false <EOL> return true <EOL> }

/**
 * Returns `true` if no elements match the given [predicate].
 *
 * @sample samples.collections.Collections.Aggregates.noneWithPredicate
 */


@ SinceKotlin ( "<STR_LIT:1.1>" ) <EOL> public inline fun < T , C : Iterable < T > > C . onEach ( action : ( T ) -> Unit ) : C

{ <EOL> return apply { for ( element in this ) action ( element ) } <EOL> }

/**
 * Performs the given [action] on each element and returns the collection itself afterwards.
 */


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> public inline fun < T , C : Iterable < T > > C . onEachIndexed ( action : ( index : Int , T ) -> Unit ) : C

{ <EOL> return apply { forEachIndexed ( action ) } <EOL> }

/**
 * Performs the given [action] on each element, providing sequential index with the element,
 * and returns the collection itself afterwards.
 * @param [action] function that takes the index of an element and the element itself
 * and performs the action on the element.
 */


public inline fun < S , T : S > Iterable < T > . reduce ( operation : ( acc : S , T ) -> S ) : S

{ <EOL> val iterator = this . iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) throw UnsupportedOperationException ( "<STR_LIT>" ) <EOL> var accumulator : S = iterator . next ( ) <EOL> while ( iterator . hasNext ( ) ) { <EOL> accumulator = operation ( accumulator , iterator . next ( ) ) <EOL> } <EOL> return accumulator <EOL> }

/**
 * Accumulates value starting with the first element and applying [operation] from left to right
 * to current accumulator value and each element.
 *
 * Throws an exception if this collection is empty. If the collection can be empty in an expected way,
 * please use [reduceOrNull] instead. It returns `null` when its receiver is empty.
 *
 * @param [operation] function that takes current accumulator value and an element,
 * and calculates the next accumulator value.
 *
 * @sample samples.collections.Collections.Aggregates.reduce
 */


public inline fun < S , T : S > Iterable < T > . reduceIndexed ( operation : ( index : Int , acc : S , T ) -> S ) : S

{ <EOL> val iterator = this . iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) throw UnsupportedOperationException ( "<STR_LIT>" ) <EOL> var index = <NUM_LIT:1> <EOL> var accumulator : S = iterator . next ( ) <EOL> while ( iterator . hasNext ( ) ) { <EOL> accumulator = operation ( checkIndexOverflow ( index ++ ) , accumulator , iterator . next ( ) ) <EOL> } <EOL> return accumulator <EOL> }

/**
 * Accumulates value starting with the first element and applying [operation] from left to right
 * to current accumulator value and each element with its index in the original collection.
 *
 * Throws an exception if this collection is empty. If the collection can be empty in an expected way,
 * please use [reduceIndexedOrNull] instead. It returns `null` when its receiver is empty.
 *
 * @param [operation] function that takes the index of an element, current accumulator value and the element itself,
 * and calculates the next accumulator value.
 *
 * @sample samples.collections.Collections.Aggregates.reduce
 */


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> public inline fun < S , T : S > Iterable < T > . reduceIndexedOrNull ( operation : ( index : Int , acc : S , T ) -> S ) : S ?

{ <EOL> val iterator = this . iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) return null <EOL> var index = <NUM_LIT:1> <EOL> var accumulator : S = iterator . next ( ) <EOL> while ( iterator . hasNext ( ) ) { <EOL> accumulator = operation ( checkIndexOverflow ( index ++ ) , accumulator , iterator . next ( ) ) <EOL> } <EOL> return accumulator <EOL> }

/**
 * Accumulates value starting with the first element and applying [operation] from left to right
 * to current accumulator value and each element with its index in the original collection.
 *
 * Returns `null` if the collection is empty.
 *
 * @param [operation] function that takes the index of an element, current accumulator value and the element itself,
 * and calculates the next accumulator value.
 *
 * @sample samples.collections.Collections.Aggregates.reduceOrNull
 */


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> public inline fun < S , T : S > Iterable < T > . reduceOrNull ( operation : ( acc : S , T ) -> S ) : S ?

{ <EOL> val iterator = this . iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) return null <EOL> var accumulator : S = iterator . next ( ) <EOL> while ( iterator . hasNext ( ) ) { <EOL> accumulator = operation ( accumulator , iterator . next ( ) ) <EOL> } <EOL> return accumulator <EOL> }

/**
 * Accumulates value starting with the first element and applying [operation] from left to right
 * to current accumulator value and each element.
 *
 * Returns `null` if the collection is empty.
 *
 * @param [operation] function that takes current accumulator value and an element,
 * and calculates the next accumulator value.
 *
 * @sample samples.collections.Collections.Aggregates.reduceOrNull
 */


public inline fun < S , T : S > List < T > . reduceRight ( operation : ( T , acc : S ) -> S ) : S

{ <EOL> val iterator = listIterator ( size ) <EOL> if ( ! iterator . hasPrevious ( ) ) <EOL> throw UnsupportedOperationException ( "<STR_LIT>" ) <EOL> var accumulator : S = iterator . previous ( ) <EOL> while ( iterator . hasPrevious ( ) ) { <EOL> accumulator = operation ( iterator . previous ( ) , accumulator ) <EOL> } <EOL> return accumulator <EOL> }

/**
 * Accumulates value starting with the last element and applying [operation] from right to left
 * to each element and current accumulator value.
 *
 * Throws an exception if this list is empty. If the list can be empty in an expected way,
 * please use [reduceRightOrNull] instead. It returns `null` when its receiver is empty.
 *
 * @param [operation] function that takes an element and current accumulator value,
 * and calculates the next accumulator value.
 *
 * @sample samples.collections.Collections.Aggregates.reduceRight
 */


public inline fun < S , T : S > List < T > . reduceRightIndexed ( operation : ( index : Int , T , acc : S ) -> S ) : S

{ <EOL> val iterator = listIterator ( size ) <EOL> if ( ! iterator . hasPrevious ( ) ) <EOL> throw UnsupportedOperationException ( "<STR_LIT>" ) <EOL> var accumulator : S = iterator . previous ( ) <EOL> while ( iterator . hasPrevious ( ) ) { <EOL> val index = iterator . previousIndex ( ) <EOL> accumulator = operation ( index , iterator . previous ( ) , accumulator ) <EOL> } <EOL> return accumulator <EOL> }

/**
 * Accumulates value starting with the last element and applying [operation] from right to left
 * to each element with its index in the original list and current accumulator value.
 *
 * Throws an exception if this list is empty. If the list can be empty in an expected way,
 * please use [reduceRightIndexedOrNull] instead. It returns `null` when its receiver is empty.
 *
 * @param [operation] function that takes the index of an element, the element itself and current accumulator value,
 * and calculates the next accumulator value.
 *
 * @sample samples.collections.Collections.Aggregates.reduceRight
 */


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> public inline fun < S , T : S > List < T > . reduceRightIndexedOrNull ( operation : ( index : Int , T , acc : S ) -> S ) : S ?

{ <EOL> val iterator = listIterator ( size ) <EOL> if ( ! iterator . hasPrevious ( ) ) <EOL> return null <EOL> var accumulator : S = iterator . previous ( ) <EOL> while ( iterator . hasPrevious ( ) ) { <EOL> val index = iterator . previousIndex ( ) <EOL> accumulator = operation ( index , iterator . previous ( ) , accumulator ) <EOL> } <EOL> return accumulator <EOL> }

/**
 * Accumulates value starting with the last element and applying [operation] from right to left
 * to each element with its index in the original list and current accumulator value.
 *
 * Returns `null` if the list is empty.
 *
 * @param [operation] function that takes the index of an element, the element itself and current accumulator value,
 * and calculates the next accumulator value.
 *
 * @sample samples.collections.Collections.Aggregates.reduceRightOrNull
 */


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> public inline fun < S , T : S > List < T > . reduceRightOrNull ( operation : ( T , acc : S ) -> S ) : S ?

{ <EOL> val iterator = listIterator ( size ) <EOL> if ( ! iterator . hasPrevious ( ) ) <EOL> return null <EOL> var accumulator : S = iterator . previous ( ) <EOL> while ( iterator . hasPrevious ( ) ) { <EOL> accumulator = operation ( iterator . previous ( ) , accumulator ) <EOL> } <EOL> return accumulator <EOL> }

/**
 * Accumulates value starting with the last element and applying [operation] from right to left
 * to each element and current accumulator value.
 *
 * Returns `null` if the list is empty.
 *
 * @param [operation] function that takes an element and current accumulator value,
 * and calculates the next accumulator value.
 *
 * @sample samples.collections.Collections.Aggregates.reduceRightOrNull
 */


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> public inline fun < T , R > Iterable < T > . runningFold ( initial : R , operation : ( acc : R , T ) -> R ) : List < R >

{ <EOL> val estimatedSize = collectionSizeOrDefault ( <NUM_LIT:9> ) <EOL> if ( estimatedSize == <NUM_LIT:0> ) return listOf ( initial ) <EOL> val result = ArrayList < R > ( estimatedSize + <NUM_LIT:1> ) . apply { add ( initial ) } <EOL> var accumulator = initial <EOL> for ( element in this ) { <EOL> accumulator = operation ( accumulator , element ) <EOL> result . add ( accumulator ) <EOL> } <EOL> return result <EOL> }

/**
 * Returns a list containing successive accumulation values generated by applying [operation] from left to right
 * to each element and current accumulator value that starts with [initial] value.
 *
 * Note that `acc` value passed to [operation] function should not be mutated;
 * otherwise it would affect the previous value in resulting list.
 *
 * @param [operation] function that takes current accumulator value and an element, and calculates the next accumulator value.
 *
 * @sample samples.collections.Collections.Aggregates.runningFold
 */


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> public inline fun < T , R > Iterable < T > . runningFoldIndexed ( initial : R , operation : ( index : Int , acc : R , T ) -> R ) : List < R >

{ <EOL> val estimatedSize = collectionSizeOrDefault ( <NUM_LIT:9> ) <EOL> if ( estimatedSize == <NUM_LIT:0> ) return listOf ( initial ) <EOL> val result = ArrayList < R > ( estimatedSize + <NUM_LIT:1> ) . apply { add ( initial ) } <EOL> var index = <NUM_LIT:0> <EOL> var accumulator = initial <EOL> for ( element in this ) { <EOL> accumulator = operation ( index ++ , accumulator , element ) <EOL> result . add ( accumulator ) <EOL> } <EOL> return result <EOL> }

/**
 * Returns a list containing successive accumulation values generated by applying [operation] from left to right
 * to each element, its index in the original collection and current accumulator value that starts with [initial] value.
 *
 * Note that `acc` value passed to [operation] function should not be mutated;
 * otherwise it would affect the previous value in resulting list.
 *
 * @param [operation] function that takes the index of an element, current accumulator value
 * and the element itself, and calculates the next accumulator value.
 *
 * @sample samples.collections.Collections.Aggregates.runningFold
 */


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> public inline fun < S , T : S > Iterable < T > . runningReduce ( operation : ( acc : S , T ) -> S ) : List < S >

{ <EOL> val iterator = this . iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) return emptyList ( ) <EOL> var accumulator : S = iterator . next ( ) <EOL> val result = ArrayList < S > ( collectionSizeOrDefault ( <NUM_LIT:10> ) ) . apply { add ( accumulator ) } <EOL> while ( iterator . hasNext ( ) ) { <EOL> accumulator = operation ( accumulator , iterator . next ( ) ) <EOL> result . add ( accumulator ) <EOL> } <EOL> return result <EOL> }

/**
 * Returns a list containing successive accumulation values generated by applying [operation] from left to right
 * to each element and current accumulator value that starts with the first element of this collection.
 *
 * Note that `acc` value passed to [operation] function should not be mutated;
 * otherwise it would affect the previous value in resulting list.
 *
 * @param [operation] function that takes current accumulator value and the element, and calculates the next accumulator value.
 *
 * @sample samples.collections.Collections.Aggregates.runningReduce
 */


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> public inline fun < S , T : S > Iterable < T > . runningReduceIndexed ( operation : ( index : Int , acc : S , T ) -> S ) : List < S >

{ <EOL> val iterator = this . iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) return emptyList ( ) <EOL> var accumulator : S = iterator . next ( ) <EOL> val result = ArrayList < S > ( collectionSizeOrDefault ( <NUM_LIT:10> ) ) . apply { add ( accumulator ) } <EOL> var index = <NUM_LIT:1> <EOL> while ( iterator . hasNext ( ) ) { <EOL> accumulator = operation ( index ++ , accumulator , iterator . next ( ) ) <EOL> result . add ( accumulator ) <EOL> } <EOL> return result <EOL> }

/**
 * Returns a list containing successive accumulation values generated by applying [operation] from left to right
 * to each element, its index in the original collection and current accumulator value that starts with the first element of this collection.
 *
 * Note that `acc` value passed to [operation] function should not be mutated;
 * otherwise it would affect the previous value in resulting list.
 *
 * @param [operation] function that takes the index of an element, current accumulator value
 * and the element itself, and calculates the next accumulator value.
 *
 * @sample samples.collections.Collections.Aggregates.runningReduce
 */


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> public inline fun < T , R > Iterable < T > . scan ( initial : R , operation : ( acc : R , T ) -> R ) : List < R >

{ <EOL> return runningFold ( initial , operation ) <EOL> }

/**
 * Returns a list containing successive accumulation values generated by applying [operation] from left to right
 * to each element and current accumulator value that starts with [initial] value.
 *
 * Note that `acc` value passed to [operation] function should not be mutated;
 * otherwise it would affect the previous value in resulting list.
 *
 * @param [operation] function that takes current accumulator value and an element, and calculates the next accumulator value.
 *
 * @sample samples.collections.Collections.Aggregates.scan
 */


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> public inline fun < T , R > Iterable < T > . scanIndexed ( initial : R , operation : ( index : Int , acc : R , T ) -> R ) : List < R >

{ <EOL> return runningFoldIndexed ( initial , operation ) <EOL> }

/**
 * Returns a list containing successive accumulation values generated by applying [operation] from left to right
 * to each element, its index in the original collection and current accumulator value that starts with [initial] value.
 *
 * Note that `acc` value passed to [operation] function should not be mutated;
 * otherwise it would affect the previous value in resulting list.
 *
 * @param [operation] function that takes the index of an element, current accumulator value
 * and the element itself, and calculates the next accumulator value.
 *
 * @sample samples.collections.Collections.Aggregates.scan
 */


@ Deprecated ( "<STR_LIT>" , ReplaceWith ( "<STR_LIT>" ) ) <EOL> @ DeprecatedSinceKotlin ( warningSince = "<STR_LIT:1.5>" ) <EOL> public inline fun < T > Iterable < T > . sumBy ( selector : ( T ) -> Int ) : Int

{ <EOL> var sum : Int = <NUM_LIT:0> <EOL> for ( element in this ) { <EOL> sum += selector ( element ) <EOL> } <EOL> return sum <EOL> }

/**
 * Returns the sum of all values produced by [selector] function applied to each element in the collection.
 */


@ Deprecated ( "<STR_LIT>" , ReplaceWith ( "<STR_LIT>" ) ) <EOL> @ DeprecatedSinceKotlin ( warningSince = "<STR_LIT:1.5>" ) <EOL> public inline fun < T > Iterable < T > . sumByDouble ( selector : ( T ) -> Double ) : Double

{ <EOL> var sum : Double = <NUM_LIT:0.0> <EOL> for ( element in this ) { <EOL> sum += selector ( element ) <EOL> } <EOL> return sum <EOL> }


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> @ OptIn ( kotlin . experimental . ExperimentalTypeInference :: class ) <EOL> @ OverloadResolutionByLambdaReturnType <EOL> @ kotlin . jvm . JvmName ( "<STR_LIT>" ) <EOL> @ kotlin . internal . InlineOnly <EOL> public inline fun < T > Iterable < T > . sumOf ( selector : ( T ) -> Double ) : Double

{ <EOL> var sum : Double = <NUM_LIT:0> . toDouble ( ) <EOL> for ( element in this ) { <EOL> sum += selector ( element ) <EOL> } <EOL> return sum <EOL> }


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> @ OptIn ( kotlin . experimental . ExperimentalTypeInference :: class ) <EOL> @ OverloadResolutionByLambdaReturnType <EOL> @ kotlin . jvm . JvmName ( "<STR_LIT>" ) <EOL> @ kotlin . internal . InlineOnly <EOL> public inline fun < T > Iterable < T > . sumOf ( selector : ( T ) -> Int ) : Int

{ <EOL> var sum : Int = <NUM_LIT:0> . toInt ( ) <EOL> for ( element in this ) { <EOL> sum += selector ( element ) <EOL> } <EOL> return sum <EOL> }


@ SinceKotlin ( "<STR_LIT:1.4>" ) <EOL> @ OptIn ( kotlin . experimental . ExperimentalTypeInference :: class ) <EOL> @ OverloadResolutionByLambdaReturnType <EOL> @ kotlin . jvm . JvmName ( "<STR_LIT>" ) <EOL> @ kotlin . internal . InlineOnly <EOL> public inline fun < T > Iterable < T > . sumOf ( selector : ( T ) -> Long ) : Long

{ <EOL> var sum : Long = <NUM_LIT:0> . toLong ( ) <EOL> for ( element in this ) { <EOL> sum += selector ( element ) <EOL> } <EOL> return sum <EOL> }


@ SinceKotlin ( "<STR_LIT:1.5>" ) <EOL> @ OptIn ( kotlin . experimental . ExperimentalTypeInference :: class ) <EOL> @ OverloadResolutionByLambdaReturnType <EOL> @ kotlin . jvm . JvmName ( "<STR_LIT>" ) <EOL> @ WasExperimental ( ExperimentalUnsignedTypes :: class ) <EOL> @ kotlin . internal . InlineOnly <EOL> public inline fun < T > Iterable < T > . sumOf ( selector : ( T ) -> UInt ) : UInt

{ <EOL> var sum : UInt = <NUM_LIT:0> . toUInt ( ) <EOL> for ( element in this ) { <EOL> sum += selector ( element ) <EOL> } <EOL> return sum <EOL> }


@ SinceKotlin ( "<STR_LIT:1.5>" ) <EOL> @ OptIn ( kotlin . experimental . ExperimentalTypeInference :: class ) <EOL> @ OverloadResolutionByLambdaReturnType <EOL> @ kotlin . jvm . JvmName ( "<STR_LIT>" ) <EOL> @ WasExperimental ( ExperimentalUnsignedTypes :: class ) <EOL> @ kotlin . internal . InlineOnly <EOL> public inline fun < T > Iterable < T > . sumOf ( selector : ( T ) -> ULong ) : ULong

{ <EOL> var sum : ULong = <NUM_LIT:0> . toULong ( ) <EOL> for ( element in this ) { <EOL> sum += selector ( element ) <EOL> } <EOL> return sum <EOL> }


public fun < T : Any > Iterable < T ? > . requireNoNulls ( ) : Iterable < T >

{ <EOL> for ( element in this ) { <EOL> if ( element == null ) { <EOL> throw IllegalArgumentException ( "<STR_LIT>" ) <EOL> } <EOL> } <EOL> @ Suppress ( "<STR_LIT:UNCHECKED_CAST>" ) <EOL> return this as Iterable < T > <EOL> }

/**
 * Returns an original collection containing all the non-`null` elements, throwing an [IllegalArgumentException] if there are any `null` elements.
 */


public fun < T : Any > List < T ? > . requireNoNulls ( ) : List < T >

{ <EOL> for ( element in this ) { <EOL> if ( element == null ) { <EOL> throw IllegalArgumentException ( "<STR_LIT>" ) <EOL> } <EOL> } <EOL> @ Suppress ( "<STR_LIT:UNCHECKED_CAST>" ) <EOL> return this as List < T > <EOL> }


@ SinceKotlin ( "<STR_LIT:1.2>" ) <EOL> public fun < T > Iterable < T > . chunked ( size : Int ) : List < List < T > >

{ <EOL> return windowed ( size , size , partialWindows = true ) <EOL> }

/**
 * Splits this collection into a list of lists each not exceeding the given [size].
 *
 * The last list in the resulting list may have fewer elements than the given [size].
 *
 * @param size the number of elements to take in each list, must be positive and can be greater than the number of elements in this collection.
 *
 * @sample samples.collections.Collections.Transformations.chunked
 */


@ SinceKotlin ( "<STR_LIT:1.2>" ) <EOL> public fun < T , R > Iterable < T > . chunked ( size : Int , transform : ( List < T > ) -> R ) : List < R >

{ <EOL> return windowed ( size , size , partialWindows = true , transform = transform ) <EOL> }

/**
 * Splits this collection into several lists each not exceeding the given [size]
 * and applies the given [transform] function to an each.
 *
 * @return list of results of the [transform] applied to an each list.
 *
 * Note that the list passed to the [transform] function is ephemeral and is valid only inside that function.
 * You should not store it or allow it to escape in some way, unless you made a snapshot of it.
 * The last list may have fewer elements than the given [size].
 *
 * @param size the number of elements to take in each list, must be positive and can be greater than the number of elements in this collection.
 *
 * @sample samples.text.Strings.chunkedTransform
 */


public operator fun < T > Iterable < T > . minus ( element : T ) : List < T >

{ <EOL> val result = ArrayList < T > ( collectionSizeOrDefault ( <NUM_LIT:10> ) ) <EOL> var removed = false <EOL> return this . filterTo ( result ) { if ( ! removed && it == element ) { removed = true ; false } else true } <EOL> }

/**
 * Returns a list containing all elements of the original collection without the first occurrence of the given [element].
 */


public operator fun < T > Iterable < T > . minus ( elements : Array < out T > ) : List < T >

{ <EOL> if ( elements . isEmpty ( ) ) return this . toList ( ) <EOL> return this . filterNot { it in elements } <EOL> }

/**
 * Returns a list containing all elements of the original collection except the elements contained in the given [elements] array.
 */


public operator fun < T > Iterable < T > . minus ( elements : Iterable < T > ) : List < T >

{ <EOL> val other = elements . convertToListIfNotCollection ( ) <EOL> if ( other . isEmpty ( ) ) <EOL> return this . toList ( ) <EOL> return this . filterNot { it in other } <EOL> }

/**
 * Returns a list containing all elements of the original collection except the elements contained in the given [elements] collection.
 */


public operator fun < T > Iterable < T > . minus ( elements : Sequence < T > ) : List < T >

{ <EOL> val other = elements . toList ( ) <EOL> if ( other . isEmpty ( ) ) <EOL> return this . toList ( ) <EOL> return this . filterNot { it in other } <EOL> }

/**
 * Returns a list containing all elements of the original collection except the elements contained in the given [elements] sequence.
 */


@ kotlin . internal . InlineOnly <EOL> public inline fun < T > Iterable < T > . minusElement ( element : T ) : List < T >

{ <EOL> return minus ( element ) <EOL> }


public inline fun < T > Iterable < T > . partition ( predicate : ( T ) -> Boolean ) : Pair < List < T > , List < T > >

{ <EOL> val first = ArrayList < T > ( ) <EOL> val second = ArrayList < T > ( ) <EOL> for ( element in this ) { <EOL> if ( predicate ( element ) ) { <EOL> first . add ( element ) <EOL> } else { <EOL> second . add ( element ) <EOL> } <EOL> } <EOL> return Pair ( first , second ) <EOL> }

/**
 * Splits the original collection into pair of lists,
 * where *first* list contains elements for which [predicate] yielded `true`,
 * while *second* list contains elements for which [predicate] yielded `false`.
 *
 * @sample samples.collections.Iterables.Operations.partition
 */


public operator fun < T > Iterable < T > . plus ( element : T ) : List < T >

{ <EOL> if ( this is Collection ) return this . plus ( element ) <EOL> val result = ArrayList < T > ( ) <EOL> result . addAll ( this ) <EOL> result . add ( element ) <EOL> return result <EOL> }

/**
 * Returns a list containing all elements of the original collection and then the given [element].
 */


public operator fun < T > Collection < T > . plus ( element : T ) : List < T >

{ <EOL> val result = ArrayList < T > ( size + <NUM_LIT:1> ) <EOL> result . addAll ( this ) <EOL> result . add ( element ) <EOL> return result <EOL> }


public operator fun < T > Iterable < T > . plus ( elements : Array < out T > ) : List < T >

{ <EOL> if ( this is Collection ) return this . plus ( elements ) <EOL> val result = ArrayList < T > ( ) <EOL> result . addAll ( this ) <EOL> result . addAll ( elements ) <EOL> return result <EOL> }

/**
 * Returns a list containing all elements of the original collection and then all elements of the given [elements] array.
 */


public operator fun < T > Collection < T > . plus ( elements : Array < out T > ) : List < T >

{ <EOL> val result = ArrayList < T > ( this . size + elements . size ) <EOL> result . addAll ( this ) <EOL> result . addAll ( elements ) <EOL> return result <EOL> }


public operator fun < T > Iterable < T > . plus ( elements : Iterable < T > ) : List < T >

/**
 * Returns a list containing all elements of the original collection and then all elements of the given [elements] collection.
 */


public operator fun < T > Collection < T > . plus ( elements : Iterable < T > ) : List < T >

{ <EOL> if ( elements is Collection ) { <EOL> val result = ArrayList < T > ( this . size + elements . size ) <EOL> result . addAll ( this ) <EOL> result . addAll ( elements ) <EOL> return result <EOL> } else { <EOL> val result = ArrayList < T > ( this ) <EOL> result . addAll ( elements ) <EOL> return result <EOL> } <EOL> }


public operator fun < T > Iterable < T > . plus ( elements : Sequence < T > ) : List < T >

{ <EOL> val result = ArrayList < T > ( ) <EOL> result . addAll ( this ) <EOL> result . addAll ( elements ) <EOL> return result <EOL> }

/**
 * Returns a list containing all elements of the original collection and then all elements of the given [elements] sequence.
 */


public operator fun < T > Collection < T > . plus ( elements : Sequence < T > ) : List < T >

{ <EOL> val result = ArrayList < T > ( this . size + <NUM_LIT:10> ) <EOL> result . addAll ( this ) <EOL> result . addAll ( elements ) <EOL> return result <EOL> }


@ kotlin . internal . InlineOnly <EOL> public inline fun < T > Iterable < T > . plusElement ( element : T ) : List < T >

{ <EOL> return plus ( element ) <EOL> }


@ kotlin . internal . InlineOnly <EOL> public inline fun < T > Collection < T > . plusElement ( element : T ) : List < T >

{ <EOL> return plus ( element ) <EOL> }


@ SinceKotlin ( "<STR_LIT:1.2>" ) <EOL> public fun < T > Iterable < T > . windowed ( size : Int , step : Int = <NUM_LIT:1> , partialWindows : Boolean = false ) : List < List < T > >

{ <EOL> checkWindowSizeStep ( size , step ) <EOL> if ( this is RandomAccess && this is List ) { <EOL> val thisSize = this . size <EOL> val resultCapacity = thisSize / step + if ( thisSize % step == <NUM_LIT:0> ) <NUM_LIT:0> else <NUM_LIT:1> <EOL> val result = ArrayList < List < T > > ( resultCapacity ) <EOL> var index = <NUM_LIT:0> <EOL> while ( index in <NUM_LIT:0> until thisSize ) { <EOL> val windowSize = size . coerceAtMost ( thisSize - index ) <EOL> if ( windowSize < size && ! partialWindows ) break <EOL> result . add ( List ( windowSize ) { this [ it + index ] } ) <EOL> index += step <EOL> } <EOL> return result <EOL> } <EOL> val result = ArrayList < List < T > > ( ) <EOL> windowedIterator ( iterator ( ) , size , step , partialWindows , reuseBuffer = false ) . forEach { <EOL> result . add ( it ) <EOL> } <EOL> return result <EOL> }

/**
 * Returns a list of snapshots of the window of the given [size]
 * sliding along this collection with the given [step], where each
 * snapshot is a list.
 *
 * Several last lists may have fewer elements than the given [size].
 *
 * Both [size] and [step] must be positive and can be greater than the number of elements in this collection.
 * @param size the number of elements to take in each window
 * @param step the number of elements to move the window forward by on an each step, by default 1
 * @param partialWindows controls whether or not to keep partial windows in the end if any,
 * by default `false` which means partial windows won't be preserved
 *
 * @sample samples.collections.Sequences.Transformations.takeWindows
 */


@ SinceKotlin ( "<STR_LIT:1.2>" ) <EOL> public fun < T , R > Iterable < T > . windowed ( size : Int , step : Int = <NUM_LIT:1> , partialWindows : Boolean = false , transform : ( List < T > ) -> R ) : List < R >

{ <EOL> checkWindowSizeStep ( size , step ) <EOL> if ( this is RandomAccess && this is List ) { <EOL> val thisSize = this . size <EOL> val resultCapacity = thisSize / step + if ( thisSize % step == <NUM_LIT:0> ) <NUM_LIT:0> else <NUM_LIT:1> <EOL> val result = ArrayList < R > ( resultCapacity ) <EOL> val window = MovingSubList ( this ) <EOL> var index = <NUM_LIT:0> <EOL> while ( index in <NUM_LIT:0> until thisSize ) { <EOL> val windowSize = size . coerceAtMost ( thisSize - index ) <EOL> if ( ! partialWindows && windowSize < size ) break <EOL> window . move ( index , index + windowSize ) <EOL> result . add ( transform ( window ) ) <EOL> index += step <EOL> } <EOL> return result <EOL> } <EOL> val result = ArrayList < R > ( ) <EOL> windowedIterator ( iterator ( ) , size , step , partialWindows , reuseBuffer = true ) . forEach { <EOL> result . add ( transform ( it ) ) <EOL> } <EOL> return result <EOL> }

/**
 * Returns a list of results of applying the given [transform] function to
 * an each list representing a view over the window of the given [size]
 * sliding along this collection with the given [step].
 *
 * Note that the list passed to the [transform] function is ephemeral and is valid only inside that function.
 * You should not store it or allow it to escape in some way, unless you made a snapshot of it.
 * Several last lists may have fewer elements than the given [size].
 *
 * Both [size] and [step] must be positive and can be greater than the number of elements in this collection.
 * @param size the number of elements to take in each window
 * @param step the number of elements to move the window forward by on an each step, by default 1
 * @param partialWindows controls whether or not to keep partial windows in the end if any,
 * by default `false` which means partial windows won't be preserved
 *
 * @sample samples.collections.Sequences.Transformations.averageWindows
 */


public infix fun < T , R > Iterable < T > . zip ( other : Array < out R > ) : List < Pair < T , R > >

{ <EOL> return zip ( other ) { t1 , t2 -> t1 to t2 } <EOL> }

/**
 * Returns a list of pairs built from the elements of `this` collection and the [other] array with the same index.
 * The returned list has length of the shortest collection.
 *
 * @sample samples.collections.Iterables.Operations.zipIterable
 */


public inline fun < T , R , V > Iterable < T > . zip ( other : Array < out R > , transform : ( a : T , b : R ) -> V ) : List < V >

{ <EOL> val arraySize = other . size <EOL> val list = ArrayList < V > ( minOf ( collectionSizeOrDefault ( <NUM_LIT:10> ) , arraySize ) ) <EOL> var i = <NUM_LIT:0> <EOL> for ( element in this ) { <EOL> if ( i >= arraySize ) break <EOL> list . add ( transform ( element , other [ i ++ ] ) ) <EOL> } <EOL> return list <EOL> }

/**
 * Returns a list of values built from the elements of `this` collection and the [other] array with the same index
 * using the provided [transform] function applied to each pair of elements.
 * The returned list has length of the shortest collection.
 *
 * @sample samples.collections.Iterables.Operations.zipIterableWithTransform
 */


public infix fun < T , R > Iterable < T > . zip ( other : Iterable < R > ) : List < Pair < T , R > >

/**
 * Returns a list of pairs built from the elements of `this` collection and [other] collection with the same index.
 * The returned list has length of the shortest collection.
 *
 * @sample samples.collections.Iterables.Operations.zipIterable
 */


public inline fun < T , R , V > Iterable < T > . zip ( other : Iterable < R > , transform : ( a : T , b : R ) -> V ) : List < V >

{ <EOL> val first = iterator ( ) <EOL> val second = other . iterator ( ) <EOL> val list = ArrayList < V > ( minOf ( collectionSizeOrDefault ( <NUM_LIT:10> ) , other . collectionSizeOrDefault ( <NUM_LIT:10> ) ) ) <EOL> while ( first . hasNext ( ) && second . hasNext ( ) ) { <EOL> list . add ( transform ( first . next ( ) , second . next ( ) ) ) <EOL> } <EOL> return list <EOL> }

/**
 * Returns a list of values built from the elements of `this` collection and the [other] collection with the same index
 * using the provided [transform] function applied to each pair of elements.
 * The returned list has length of the shortest collection.
 *
 * @sample samples.collections.Iterables.Operations.zipIterableWithTransform
 */


@ SinceKotlin ( "<STR_LIT:1.2>" ) <EOL> public fun < T > Iterable < T > . zipWithNext ( ) : List < Pair < T , T > >

{ <EOL> return zipWithNext { a , b -> a to b } <EOL> }

/**
 * Returns a list of pairs of each two adjacent elements in this collection.
 *
 * The returned list is empty if this collection contains less than two elements.
 *
 * @sample samples.collections.Collections.Transformations.zipWithNext
 */


@ SinceKotlin ( "<STR_LIT:1.2>" ) <EOL> public inline fun < T , R > Iterable < T > . zipWithNext ( transform : ( a : T , b : T ) -> R ) : List < R >

{ <EOL> val iterator = iterator ( ) <EOL> if ( ! iterator . hasNext ( ) ) return emptyList ( ) <EOL> val result = mutableListOf < R > ( ) <EOL> var current = iterator . next ( ) <EOL> while ( iterator . hasNext ( ) ) { <EOL> val next = iterator . next ( ) <EOL> result . add ( transform ( current , next ) ) <EOL> current = next <EOL> } <EOL> return result <EOL> }

/**
 * Returns a list containing the results of applying the given [transform] function
 * to an each pair of two adjacent elements in this collection.
 *
 * The returned list is empty if this collection contains less than two elements.
 *
 * @sample samples.collections.Collections.Transformations.zipWithNextToFindDeltas
 */


public fun < T , A : Appendable > Iterable < T > . joinTo ( buffer : A , separator : CharSequence = "<STR_LIT:U+002CU+0020>" , prefix : CharSequence = "<STR_LIT>" , postfix : CharSequence = "<STR_LIT>" , limit : Int = - <NUM_LIT:1> , truncated : CharSequence = "<STR_LIT:...>" , transform : ( ( T ) -> CharSequence ) ? = null ) : A

{ <EOL> buffer . append ( prefix ) <EOL> var count = <NUM_LIT:0> <EOL> for ( element in this ) { <EOL> if ( ++ count > <NUM_LIT:1> ) buffer . append ( separator ) <EOL> if ( limit < <NUM_LIT:0> || count <= limit ) { <EOL> buffer . appendElement ( element , transform ) <EOL> } else break <EOL> } <EOL> if ( limit >= <NUM_LIT:0> && count > limit ) buffer . append ( truncated ) <EOL> buffer . append ( postfix ) <EOL> return buffer <EOL> }

/**
 * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied.
 *
 * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit]
 * elements will be appended, followed by the [truncated] string (which defaults to "...").
 *
 * @sample samples.collections.Collections.Transformations.joinTo
 */


public fun < T > Iterable < T > . joinToString ( separator : CharSequence = "<STR_LIT:U+002CU+0020>" , prefix : CharSequence = "<STR_LIT>" , postfix : CharSequence = "<STR_LIT>" , limit : Int = - <NUM_LIT:1> , truncated : CharSequence = "<STR_LIT:...>" , transform : ( ( T ) -> CharSequence ) ? = null ) : String

{ <EOL> return joinTo ( StringBuilder ( ) , separator , prefix , postfix , limit , truncated , transform ) . toString ( ) <EOL> }

/**
 * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied.
 *
 * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit]
 * elements will be appended, followed by the [truncated] string (which defaults to "...").
 *
 * @sample samples.collections.Collections.Transformations.joinToString
 */


@ kotlin . internal . InlineOnly <EOL> public inline fun < T > Iterable < T > . asIterable ( ) : Iterable < T >

{ <EOL> return this <EOL> }

/**
 * Returns this collection as an [Iterable].
 */


public fun < T > Iterable < T > . asSequence ( ) : Sequence < T >

{ <EOL> return Sequence { this . iterator ( ) } <EOL> }

/**
 * Creates a [Sequence] instance that wraps the original collection returning its elements when being iterated.
 *
 * @sample samples.collections.Sequences.Building.sequenceFromCollection
 */


@ kotlin . jvm . JvmName ( "<STR_LIT>" ) <EOL> public fun Iterable < Byte > . average ( ) : Double

{ <EOL> var sum : Double = <NUM_LIT:0.0> <EOL> var count : Int = <NUM_LIT:0> <EOL> for ( element in this ) { <EOL> sum += element <EOL> checkCountOverflow ( ++ count ) <EOL> } <EOL> return if ( count == <NUM_LIT:0> ) Double . NaN else sum / count <EOL> }

/**
 * Returns an average value of elements in the collection.
 */


@ kotlin . jvm . JvmName ( "<STR_LIT>" ) <EOL> public fun Iterable < Short > . average ( ) : Double


@ kotlin . jvm . JvmName ( "<STR_LIT>" ) <EOL> public fun Iterable < Int > . average ( ) : Double


@ kotlin . jvm . JvmName ( "<STR_LIT>" ) <EOL> public fun Iterable < Long > . average ( ) : Double


@ kotlin . jvm . JvmName ( "<STR_LIT>" ) <EOL> public fun Iterable < Float > . average ( ) : Double


@ kotlin . jvm . JvmName ( "<STR_LIT>" ) <EOL> public fun Iterable < Double > . average ( ) : Double


@ kotlin . jvm . JvmName ( "<STR_LIT>" ) <EOL> public fun Iterable < Byte > . sum ( ) : Int

{ <EOL> var sum : Int = <NUM_LIT:0> <EOL> for ( element in this ) { <EOL> sum += element <EOL> } <EOL> return sum <EOL> }

/**
 * Returns the sum of all elements in the collection.
 */


@ kotlin . jvm . JvmName ( "<STR_LIT>" ) <EOL> public fun Iterable < Short > . sum ( ) : Int


@ kotlin . jvm . JvmName ( "<STR_LIT>" ) <EOL> public fun Iterable < Int > . sum ( ) : Int


@ kotlin . jvm . JvmName ( "<STR_LIT>" ) <EOL> public fun Iterable < Long > . sum ( ) : Long

{ <EOL> var sum : Long = <NUM_LIT:0L> <EOL> for ( element in this ) { <EOL> sum += element <EOL> } <EOL> return sum <EOL> }


@ kotlin . jvm . JvmName ( "<STR_LIT>" ) <EOL> public fun Iterable < Float > . sum ( ) : Float

{ <EOL> var sum : Float = <NUM_LIT:0.0f> <EOL> for ( element in this ) { <EOL> sum += element <EOL> } <EOL> return sum <EOL> }


@ kotlin . jvm . JvmName ( "<STR_LIT>" ) <EOL> public fun Iterable < Double > . sum ( ) : Double

{ <EOL> var sum : Double = <NUM_LIT:0.0> <EOL> for ( element in this ) { <EOL> sum += element <EOL> } <EOL> return sum <EOL> }


internal open suspend fun sendBroadcast ( element : E ) : Boolean

= suspendCancellableCoroutine { cont -> <EOL> check ( onUndeliveredElement == null ) { <EOL> "<STR_LIT>" <EOL> } <EOL> sendImpl ( element = element , waiter = SendBroadcast ( cont ) , onRendezvousOrBuffered = { cont . resume ( true ) } , onSuspend = { _ , _ -> } , onClosed = { cont . resume ( false ) } ) <EOL> }

/**
 * This is a special `send(e)` implementation that returns `true` if the element
 * has been successfully sent, and `false` if the channel is closed.
 *
 * In case of coroutine cancellation, the element may be undelivered --
 * the [onUndeliveredElement] feature is unsupported in this implementation.
 *
 */


private inline fun < R > sendImpl ( element : E , waiter : Any ? , onRendezvousOrBuffered : ( ) -> R , onSuspend : ( segm : ChannelSegment < E > , i : Int ) -> R , onClosed : ( ) -> R , onNoWaiterSuspend : ( segm : ChannelSegment < E > , i : Int , element : E , s : Long ) -> R = { _ , _ , _ , _ -> error ( "<STR_LIT>" ) } ) : R

{ <EOL> var segment = sendSegment . value <EOL> while ( true ) { <EOL> val sendersAndCloseStatusCur = sendersAndCloseStatus . getAndIncrement ( ) <EOL> val s = sendersAndCloseStatusCur . sendersCounter <EOL> val closed = sendersAndCloseStatusCur . isClosedForSend0 <EOL> val id = s / SEGMENT_SIZE <EOL> val i = ( s % SEGMENT_SIZE ) . toInt ( ) <EOL> if ( segment . id != id ) { <EOL> segment = findSegmentSend ( id , segment ) ? : <EOL> if ( closed ) { <EOL> return onClosed ( ) <EOL> } else { <EOL> continue <EOL> } <EOL> } <EOL> when ( updateCellSend ( segment , i , element , s , waiter , closed ) ) { <EOL> RESULT_RENDEZVOUS -> { <EOL> segment . cleanPrev ( ) <EOL> return onRendezvousOrBuffered ( ) <EOL> } <EOL> RESULT_BUFFERED -> { <EOL> return onRendezvousOrBuffered ( ) <EOL> } <EOL> RESULT_SUSPEND -> { <EOL> if ( closed ) { <EOL> segment . onSlotCleaned ( ) <EOL> return onClosed ( ) <EOL> } <EOL> ( waiter as? Waiter ) ? . prepareSenderForSuspension ( segment , i ) <EOL> return onSuspend ( segment , i ) <EOL> } <EOL> RESULT_CLOSED -> { <EOL> if ( s < receiversCounter ) segment . cleanPrev ( ) <EOL> return onClosed ( ) <EOL> } <EOL> RESULT_FAILED -> { <EOL> segment . cleanPrev ( ) <EOL> continue <EOL> } <EOL> RESULT_SUSPEND_NO_WAITER -> { <EOL> return onNoWaiterSuspend ( segment , i , element , s ) <EOL> } <EOL> } <EOL> } <EOL> }

/**
 * Abstract send implementation.
 */


private fun updateCellSendSlow ( segment : ChannelSegment < E > , index : Int , element : E , s : Long , waiter : Any ? , closed : Boolean ) : Int

{ <EOL> while ( true ) { <EOL> val state = segment . getState ( index ) <EOL> when { <EOL> state === null -> { <EOL> if ( bufferOrRendezvousSend ( s ) && ! closed ) { <EOL> if ( segment . casState ( index , null , BUFFERED ) ) { <EOL> return RESULT_BUFFERED <EOL> } <EOL> } else { <EOL> when { <EOL> closed -> if ( segment . casState ( index , null , INTERRUPTED_SEND ) ) { <EOL> segment . onCancelledRequest ( index , false ) <EOL> return RESULT_CLOSED <EOL> } <EOL> waiter == null -> return RESULT_SUSPEND_NO_WAITER <EOL> else -> if ( segment . casState ( index , null , waiter ) ) return RESULT_SUSPEND <EOL> } <EOL> } <EOL> } <EOL> state === IN_BUFFER -> { <EOL> if ( segment . casState ( index , state , BUFFERED ) ) { <EOL> return RESULT_BUFFERED <EOL> } <EOL> } <EOL> state === INTERRUPTED_RCV -> { <EOL> segment . cleanElement ( index ) <EOL> return RESULT_FAILED <EOL> } <EOL> state === POISONED -> { <EOL> segment . cleanElement ( index ) <EOL> return RESULT_FAILED <EOL> } <EOL> state === CHANNEL_CLOSED -> { <EOL> segment . cleanElement ( index ) <EOL> completeCloseOrCancel ( ) <EOL> return RESULT_CLOSED <EOL> } <EOL> else -> { <EOL> assert { state is Waiter || state is WaiterEB } <EOL> segment . cleanElement ( index ) <EOL> val receiver = if ( state is WaiterEB ) state . waiter else state <EOL> return if ( receiver . tryResumeReceiver ( element ) ) { <EOL> segment . setState ( index , DONE_RCV ) <EOL> onReceiveDequeued ( ) <EOL> RESULT_RENDEZVOUS <EOL> } else { <EOL> if ( segment . getAndSetState ( index , INTERRUPTED_RCV ) !== INTERRUPTED_RCV ) { <EOL> segment . onCancelledRequest ( index , true ) <EOL> } <EOL> RESULT_FAILED <EOL> } <EOL> } <EOL> } <EOL> } <EOL> }

/**
 * Updates the working cell of an abstract send operation.
 */


@ JsName ( "<STR_LIT>" ) <EOL> private fun shouldSendSuspend ( curSendersAndCloseStatus : Long ) : Boolean

{ <EOL> if ( curSendersAndCloseStatus . isClosedForSend0 ) return false <EOL> return ! bufferOrRendezvousSend ( curSendersAndCloseStatus . sendersCounter ) <EOL> }

/**
 * Checks whether a [send] invocation is bound to suspend if it is called
 * with the specified [sendersAndCloseStatus], [receivers], and [bufferEnd]
 * values. When this channel is already closed, the function returns `false`.
 *
 * Specifically, [send] suspends if the channel is not unlimited,
 * the number of receivers is greater than then index of the working cell of the
 * potential [send] invocation, and the buffer does not cover this cell
 * in case of buffered channel.
 * When the channel is already closed, [send] does not suspend.
 */


private fun bufferOrRendezvousSend ( curSenders : Long ) : Boolean

= <EOL> curSenders < bufferEndCounter || curSenders < receiversCounter + capacity

/**
 * Returns `true` when the specified [send] should place
 * its element to the working cell without suspension.
 */


internal open fun shouldSendSuspend ( ) : Boolean

= shouldSendSuspend ( sendersAndCloseStatus . value )

/**
 * Checks whether a [send] invocation is bound to suspend if it is called
 * with the current counter and close status values. See [shouldSendSuspend] for details.
 *
 * Note that this implementation is _false positive_ in case of rendezvous channels,
 * so it can return `false` when a [send] invocation is bound to suspend. Specifically,
 * the counter of `receive()` operations may indicate that there is a waiting receiver,
 * while it has already been cancelled, so the potential rendezvous is bound to fail.
 */


@ Suppress ( "<STR_LIT:UNCHECKED_CAST>" ) <EOL> private fun Any . tryResumeReceiver ( element : E ) : Boolean

= when ( this ) { <EOL> is SelectInstance < * > -> { <EOL> trySelect ( this @ BufferedChannel , element ) <EOL> } <EOL> is ReceiveCatching < * > -> { <EOL> this as ReceiveCatching < E > <EOL> cont . tryResume0 ( success ( element ) , onUndeliveredElement ? . bindCancellationFun ( element , cont . context ) ) <EOL> } <EOL> is BufferedChannel < * > . BufferedChannelIterator -> { <EOL> this as BufferedChannel < E > . BufferedChannelIterator <EOL> tryResumeHasNext ( element ) <EOL> } <EOL> is CancellableContinuation < * > -> { <EOL> this as CancellableContinuation < E > <EOL> tryResume0 ( element , onUndeliveredElement ? . bindCancellationFun ( element , context ) ) <EOL> } <EOL> else -> error ( "<STR_LIT>" ) <EOL> }

/**
 * Tries to resume this receiver with the specified [element] as a result.
 * Returns `true` on success and `false` otherwise.
 */


protected open fun onReceiveEnqueued ( )

/**
 * This function is invoked when a receiver is added as a waiter in this channel.
 */


protected open fun onReceiveDequeued ( )

/**
 * This function is invoked when a waiting receiver is no longer stored in this channel;
 * independently on whether it is caused by rendezvous, cancellation, or channel closing.
 */


protected fun dropFirstElementUntilTheSpecifiedCellIsInTheBuffer ( globalCellIndex : Long )

{ <EOL> assert { isConflatedDropOldest } <EOL> var segment = receiveSegment . value <EOL> while ( true ) { <EOL> val r = this . receivers . value <EOL> if ( globalCellIndex < max ( r + capacity , bufferEndCounter ) ) return <EOL> if ( ! this . receivers . compareAndSet ( r , r + <NUM_LIT:1> ) ) continue <EOL> val id = r / SEGMENT_SIZE <EOL> val i = ( r % SEGMENT_SIZE ) . toInt ( ) <EOL> if ( segment . id != id ) { <EOL> segment = findSegmentReceive ( id , segment ) ? : <EOL> continue <EOL> } <EOL> val updCellResult = updateCellReceive ( segment , i , r , null ) <EOL> when { <EOL> updCellResult === FAILED -> { <EOL> if ( r < sendersCounter ) segment . cleanPrev ( ) <EOL> } <EOL> else -> { <EOL> segment . cleanPrev ( ) <EOL> @ Suppress ( "<STR_LIT:UNCHECKED_CAST>" ) <EOL> onUndeliveredElement ? . callUndeliveredElementCatchingException ( updCellResult as E ) ? . let { throw it } <EOL> } <EOL> } <EOL> } <EOL> }

/**
 * Extracts the first element from this channel until the cell with the specified
 * index is moved to the logical buffer. This is a key procedure for the _conflated_
 * channel implementation, see [ConflatedBufferedChannel] with the [BufferOverflow.DROP_OLDEST]
 * strategy on buffer overflowing.
 */


private inline fun < R > receiveImpl ( waiter : Any ? , onElementRetrieved : ( element : E ) -> R , onSuspend : ( segm : ChannelSegment < E > , i : Int , r : Long ) -> R , onClosed : ( ) -> R , onNoWaiterSuspend : ( segm : ChannelSegment < E > , i : Int , r : Long ) -> R = { _ , _ , _ -> error ( "<STR_LIT>" ) } ) : R

{ <EOL> var segment = receiveSegment . value <EOL> while ( true ) { <EOL> if ( isClosedForReceive ) return onClosed ( ) <EOL> val r = this . receivers . getAndIncrement ( ) <EOL> val id = r / SEGMENT_SIZE <EOL> val i = ( r % SEGMENT_SIZE ) . toInt ( ) <EOL> if ( segment . id != id ) { <EOL> segment = findSegmentReceive ( id , segment ) ? : <EOL> continue <EOL> } <EOL> val updCellResult = updateCellReceive ( segment , i , r , waiter ) <EOL> return when { <EOL> updCellResult === SUSPEND -> { <EOL> ( waiter as? Waiter ) ? . prepareReceiverForSuspension ( segment , i ) <EOL> onSuspend ( segment , i , r ) <EOL> } <EOL> updCellResult === FAILED -> { <EOL> if ( r < sendersCounter ) segment . cleanPrev ( ) <EOL> continue <EOL> } <EOL> updCellResult === SUSPEND_NO_WAITER -> { <EOL> onNoWaiterSuspend ( segment , i , r ) <EOL> } <EOL> else -> { <EOL> segment . cleanPrev ( ) <EOL> @ Suppress ( "<STR_LIT:UNCHECKED_CAST>" ) <EOL> onElementRetrieved ( updCellResult as E ) <EOL> } <EOL> } <EOL> } <EOL> }

/**
 * Abstract receive implementation.
 */


private fun incCompletedExpandBufferAttempts ( nAttempts : Long = <NUM_LIT:1> )

{ <EOL> completedExpandBuffersAndPauseFlag . addAndGet ( nAttempts ) . also { <EOL> if ( it . ebPauseExpandBuffers ) { <EOL> @ Suppress ( "<STR_LIT>" ) <EOL> while ( completedExpandBuffersAndPauseFlag . value . ebPauseExpandBuffers ) { } <EOL> } <EOL> } <EOL> }

/**
 * Increments the counter of completed [expandBuffer] invocations.
 * To guarantee starvation-freedom for [waitExpandBufferCompletion],
 * which waits until the counters of started and completed [expandBuffer] calls
 * coincide and become greater or equal to the specified value,
 * [waitExpandBufferCompletion] may set a flag that pauses further progress.
 */


internal fun waitExpandBufferCompletion ( globalIndex : Long )

{ <EOL> if ( isRendezvousOrUnlimited ) return <EOL> @ Suppress ( "<STR_LIT>" ) <EOL> while ( bufferEndCounter <= globalIndex ) { } <EOL> repeat ( EXPAND_BUFFER_COMPLETION_WAIT_ITERATIONS ) { <EOL> val b = bufferEndCounter <EOL> val ebCompleted = completedExpandBuffersAndPauseFlag . value . ebCompletedCounter <EOL> if ( b == ebCompleted && b == bufferEndCounter ) return <EOL> } <EOL> completedExpandBuffersAndPauseFlag . update { <EOL> constructEBCompletedAndPauseFlag ( it . ebCompletedCounter , true ) <EOL> } <EOL> while ( true ) { <EOL> val b = bufferEndCounter <EOL> val ebCompletedAndBit = completedExpandBuffersAndPauseFlag . value <EOL> val ebCompleted = ebCompletedAndBit . ebCompletedCounter <EOL> val pauseExpandBuffers = ebCompletedAndBit . ebPauseExpandBuffers <EOL> if ( b == ebCompleted && b == bufferEndCounter ) { <EOL> completedExpandBuffersAndPauseFlag . update { <EOL> constructEBCompletedAndPauseFlag ( it . ebCompletedCounter , false ) <EOL> } <EOL> return <EOL> } <EOL> if ( ! pauseExpandBuffers ) { <EOL> completedExpandBuffersAndPauseFlag . compareAndSet ( ebCompletedAndBit , constructEBCompletedAndPauseFlag ( ebCompleted , true ) ) <EOL> } <EOL> } <EOL> }

/**
 * Waits in a spin-loop until the [expandBuffer] call that
 * should process the [globalIndex]-th cell is completed.
 * Essentially, it waits until the numbers of started ([bufferEnd])
 * and completed ([completedExpandBuffersAndPauseFlag]) [expandBuffer]
 * attempts coincide and become equal or greater than [globalIndex].
 * To avoid starvation, this function may set a flag
 * that pauses further progress.
 */


protected open fun onClosedIdempotent ( )

/**
 * Invoked when channel is closed as the last action of [close] invocation.
 * This method should be idempotent and can be called multiple times.
 */


protected open fun closeOrCancelImpl ( cause : Throwable ? , cancel : Boolean ) : Boolean

{ <EOL> if ( cancel ) markCancellationStarted ( ) <EOL> val closedByThisOperation = _closeCause . compareAndSet ( NO_CLOSE_CAUSE , cause ) <EOL> if ( cancel ) markCancelled ( ) else markClosed ( ) <EOL> completeCloseOrCancel ( ) <EOL> return closedByThisOperation . also { <EOL> onClosedIdempotent ( ) <EOL> if ( it ) invokeCloseHandler ( ) <EOL> } <EOL> }

/**
 * This is a common implementation for [close] and [cancel]. It first tries
 * to install the specified cause; the invocation that successfully installs
 * the cause returns `true` as a results of this function, while all further
 * [close] and [cancel] calls return `false`.
 *
 * After the closing/cancellation cause is installed, the channel should be marked
 * as closed or cancelled, which bounds further `send(e)`-s to fails.
 *
 * Then, [completeCloseOrCancel] is called, which cancels waiting `receive()`
 * requests ([cancelSuspendedReceiveRequests]) and removes unprocessed elements
 * ([removeUnprocessedElements]) in case this channel is cancelled.
 *
 * Finally, if this [closeOrCancelImpl] has installed the cause, therefore,
 * has closed the channel, [closeHandler] and [onClosedIdempotent] should be invoked.
 */


{ <EOL> val closeHandler = closeHandler . getAndUpdate { <EOL> if ( it === null ) { <EOL> CLOSE_HANDLER_CLOSED <EOL> } else { <EOL> CLOSE_HANDLER_INVOKED <EOL> } <EOL> } ? : return <EOL> @ Suppress ( "<STR_LIT:UNCHECKED_CAST>" ) <EOL> closeHandler as ( cause : Throwable ? ) -> Unit <EOL> closeHandler ( closeCause ) <EOL> }

/**
 * Invokes the installed close handler,
 * updating the [closeHandler] state correspondingly.
 */


= <EOL> sendersAndCloseStatus . update { cur -> <EOL> when ( cur . sendersCloseStatus ) { <EOL> CLOSE_STATUS_ACTIVE -> <EOL> constructSendersAndCloseStatus ( cur . sendersCounter , CLOSE_STATUS_CLOSED ) <EOL> CLOSE_STATUS_CANCELLATION_STARTED -> <EOL> constructSendersAndCloseStatus ( cur . sendersCounter , CLOSE_STATUS_CANCELLED ) <EOL> else -> return <EOL> } <EOL> }

/**
 * Marks this channel as closed.
 * In case [cancelImpl] has already been invoked,
 * and this channel is marked with [CLOSE_STATUS_CANCELLATION_STARTED],
 * this function marks the channel as cancelled.
 *
 * All operation that notice this channel in the closed state,
 * must help to complete the closing via [completeCloseOrCancel].
 */


= <EOL> sendersAndCloseStatus . update { cur -> <EOL> constructSendersAndCloseStatus ( cur . sendersCounter , CLOSE_STATUS_CANCELLED ) <EOL> }

/**
 * Marks this channel as cancelled.
 *
 * All operation that notice this channel in the cancelled state,
 * must help to complete the cancellation via [completeCloseOrCancel].
 */


private fun markCancellationStarted ( ) : Unit

= <EOL> sendersAndCloseStatus . update { cur -> <EOL> if ( cur . sendersCloseStatus == CLOSE_STATUS_ACTIVE ) <EOL> constructSendersAndCloseStatus ( cur . sendersCounter , CLOSE_STATUS_CANCELLATION_STARTED ) <EOL> else return <EOL> }

/**
 * When the cancellation procedure starts, it is critical
 * to mark the closing status correspondingly. Thus, other
 * operations, which may help to complete the cancellation,
 * always correctly update the status to `CANCELLED`.
 */


{ <EOL> isClosedForSend <EOL> }

/**
 * Completes the started [close] or [cancel] procedure.
 */


private fun completeClose ( sendersCur : Long ) : ChannelSegment < E >

{ <EOL> val lastSegment = closeLinkedList ( ) <EOL> if ( isConflatedDropOldest ) { <EOL> val lastBufferedCellGlobalIndex = markAllEmptyCellsAsClosed ( lastSegment ) <EOL> if ( lastBufferedCellGlobalIndex != - <NUM_LIT:1L> ) <EOL> dropFirstElementUntilTheSpecifiedCellIsInTheBuffer ( lastBufferedCellGlobalIndex ) <EOL> } <EOL> cancelSuspendedReceiveRequests ( lastSegment , sendersCur ) <EOL> return lastSegment <EOL> }

/**
 * Completes the channel closing procedure.
 */


private fun completeCancel ( sendersCur : Long )

{ <EOL> val lastSegment = completeClose ( sendersCur ) <EOL> removeUnprocessedElements ( lastSegment ) <EOL> }

/**
 * Completes the channel cancellation procedure.
 */


private fun closeLinkedList ( ) : ChannelSegment < E >

{ <EOL> var lastSegment = bufferEndSegment . value <EOL> sendSegment . value . let { if ( it . id > lastSegment . id ) lastSegment = it } <EOL> receiveSegment . value . let { if ( it . id > lastSegment . id ) lastSegment = it } <EOL> return lastSegment . close ( ) <EOL> }

/**
 * Closes the underlying linked list of segments for further segment addition.
 */


private fun markAllEmptyCellsAsClosed ( lastSegment : ChannelSegment < E > ) : Long

{ <EOL> var segment = lastSegment <EOL> while ( true ) { <EOL> for ( index in SEGMENT_SIZE - <NUM_LIT:1> downTo <NUM_LIT:0> ) { <EOL> val globalIndex = segment . id * SEGMENT_SIZE + index <EOL> if ( globalIndex < receiversCounter ) return - <NUM_LIT:1> <EOL> cell_update @ while ( true ) { <EOL> val state = segment . getState ( index ) <EOL> when { <EOL> state === null || state === IN_BUFFER -> { <EOL> if ( segment . casState ( index , state , CHANNEL_CLOSED ) ) { <EOL> segment . onSlotCleaned ( ) <EOL> break@cell_update <EOL> } <EOL> } <EOL> state === BUFFERED -> return globalIndex <EOL> else -> break@cell_update <EOL> } <EOL> } <EOL> } <EOL> segment = segment . prev ? : return - <NUM_LIT:1> <EOL> } <EOL> }

/**
 * This function marks all empty cells, in the `null` and [IN_BUFFER] state,
 * as closed. Notably, it processes the cells from right to left, and finishes
 * immediately when the processing cell is already covered by `receive()` or
 * contains a buffered elements ([BUFFERED] state).
 *
 * This function returns the global index of the last buffered element,
 * or `-1` if this channel does not contain buffered elements.
 */


private fun removeUnprocessedElements ( lastSegment : ChannelSegment < E > )

{ <EOL> val onUndeliveredElement = onUndeliveredElement <EOL> var undeliveredElementException : UndeliveredElementException ? = null <EOL> var suspendedSenders = InlineList < Waiter > ( ) <EOL> var segment = lastSegment <EOL> process_segments @ while ( true ) { <EOL> for ( index in SEGMENT_SIZE - <NUM_LIT:1> downTo <NUM_LIT:0> ) { <EOL> val globalIndex = segment . id * SEGMENT_SIZE + index <EOL> update_cell @ while ( true ) { <EOL> val state = segment . getState ( index ) <EOL> when { <EOL> state === DONE_RCV -> break@process_segments <EOL> state === BUFFERED -> { <EOL> if ( globalIndex < receiversCounter ) break@process_segments <EOL> if ( segment . casState ( index , state , CHANNEL_CLOSED ) ) { <EOL> if ( onUndeliveredElement != null ) { <EOL> val element = segment . getElement ( index ) <EOL> undeliveredElementException = onUndeliveredElement . callUndeliveredElementCatchingException ( element , undeliveredElementException ) <EOL> } <EOL> segment . cleanElement ( index ) <EOL> segment . onSlotCleaned ( ) <EOL> break@update_cell <EOL> } <EOL> } <EOL> state === IN_BUFFER || state === null -> { <EOL> if ( segment . casState ( index , state , CHANNEL_CLOSED ) ) { <EOL> segment . onSlotCleaned ( ) <EOL> break@update_cell <EOL> } <EOL> } <EOL> state is Waiter || state is WaiterEB -> { <EOL> if ( globalIndex < receiversCounter ) break@process_segments <EOL> val sender : Waiter = if ( state is WaiterEB ) state . waiter <EOL> else state as Waiter <EOL> if ( segment . casState ( index , state , CHANNEL_CLOSED ) ) { <EOL> if ( onUndeliveredElement != null ) { <EOL> val element = segment . getElement ( index ) <EOL> undeliveredElementException = onUndeliveredElement . callUndeliveredElementCatchingException ( element , undeliveredElementException ) <EOL> } <EOL> suspendedSenders += sender <EOL> segment . cleanElement ( index ) <EOL> segment . onSlotCleaned ( ) <EOL> break@update_cell <EOL> } <EOL> } <EOL> state === RESUMING_BY_EB || state === RESUMING_BY_RCV -> break@process_segments <EOL> state === RESUMING_BY_EB -> continue@update_cell <EOL> else -> break@update_cell <EOL> } <EOL> } <EOL> } <EOL> segment = segment . prev ? : break <EOL> } <EOL> suspendedSenders . forEachReversed { it . resumeSenderOnCancelledChannel ( ) } <EOL> undeliveredElementException ? . let { throw it } <EOL> }

/**
 * Cancels suspended `send(e)` requests and removes buffered elements
 * starting from the last cell in the specified [lastSegment] (it must
 * be the physical tail of the underlying linked list) and updating
 * the cells in reverse order.
 */


private fun cancelSuspendedReceiveRequests ( lastSegment : ChannelSegment < E > , sendersCounter : Long )

{ <EOL> var suspendedReceivers = InlineList < Waiter > ( ) <EOL> var segment : ChannelSegment < E > ? = lastSegment <EOL> process_segments @ while ( segment != null ) { <EOL> for ( index in SEGMENT_SIZE - <NUM_LIT:1> downTo <NUM_LIT:0> ) { <EOL> if ( segment . id * SEGMENT_SIZE + index < sendersCounter ) break@process_segments <EOL> cell_update @ while ( true ) { <EOL> val state = segment . getState ( index ) <EOL> when { <EOL> state === null || state === IN_BUFFER -> { <EOL> if ( segment . casState ( index , state , CHANNEL_CLOSED ) ) { <EOL> segment . onSlotCleaned ( ) <EOL> break@cell_update <EOL> } <EOL> } <EOL> state is WaiterEB -> { <EOL> if ( segment . casState ( index , state , CHANNEL_CLOSED ) ) { <EOL> suspendedReceivers += state . waiter <EOL> segment . onCancelledRequest ( index = index , receiver = true ) <EOL> break@cell_update <EOL> } <EOL> } <EOL> state is Waiter -> { <EOL> if ( segment . casState ( index , state , CHANNEL_CLOSED ) ) { <EOL> suspendedReceivers += state <EOL> segment . onCancelledRequest ( index = index , receiver = true ) <EOL> break@cell_update <EOL> } <EOL> } <EOL> else -> break@cell_update <EOL> } <EOL> } <EOL> } <EOL> segment = segment . prev <EOL> } <EOL> suspendedReceivers . forEachReversed { it . resumeReceiverOnClosedChannel ( ) } <EOL> }

/**
 * Cancels suspended `receive` requests from the end to the beginning,
 * also moving empty cells to the `CHANNEL_CLOSED` state.
 */


private fun Waiter . resumeReceiverOnClosedChannel ( )

= resumeWaiterOnClosedChannel ( receiver = true )

/**
 * Resumes this receiver because this channel is closed.
 * This function does not take any effect if the operation has already been resumed or cancelled.
 */


private fun Waiter . resumeSenderOnCancelledChannel ( )

= resumeWaiterOnClosedChannel ( receiver = false )

/**
 * Resumes this sender because this channel is cancelled.
 * This function does not take any effect if the operation has already been resumed or cancelled.
 */


{ <EOL> while ( true ) { <EOL> var segment = receiveSegment . value <EOL> val r = receiversCounter <EOL> val s = sendersCounter <EOL> if ( s <= r ) return false <EOL> val id = r / SEGMENT_SIZE <EOL> if ( segment . id != id ) { <EOL> segment = findSegmentReceive ( id , segment ) ? : <EOL> if ( receiveSegment . value . id < id ) return false else continue <EOL> } <EOL> segment . cleanPrev ( ) <EOL> val i = ( r % SEGMENT_SIZE ) . toInt ( ) <EOL> if ( isCellNonEmpty ( segment , i , r ) ) return true <EOL> receivers . compareAndSet ( r , r + <NUM_LIT:1> ) <EOL> } <EOL> }

/**
 * Checks whether this channel contains elements to retrieve.
 * Unfortunately, simply comparing the counters is insufficient,
 * as some cells can be in the `INTERRUPTED` state due to cancellation.
 * This function tries to find the first "alive" element,
 * updating the `receivers` counter to skip empty cells.
 *
 * The implementation is similar to `receive()`.
 */


private fun isCellNonEmpty ( segment : ChannelSegment < E > , index : Int , globalIndex : Long ) : Boolean

{ <EOL> while ( true ) { <EOL> val state = segment . getState ( index ) <EOL> when { <EOL> state === null || state === IN_BUFFER -> { <EOL> if ( segment . casState ( index , state , POISONED ) ) { <EOL> expandBuffer ( ) <EOL> return false <EOL> } <EOL> } <EOL> state === BUFFERED -> return true <EOL> state === INTERRUPTED_SEND -> return false <EOL> state === CHANNEL_CLOSED -> return false <EOL> state === DONE_RCV -> return false <EOL> state === POISONED -> return false <EOL> state === RESUMING_BY_EB -> return true <EOL> state === RESUMING_BY_RCV -> return false <EOL> else -> return globalIndex == receiversCounter <EOL> } <EOL> } <EOL> }

/**
 * Checks whether this cell contains a buffered element or a waiting sender,
 * returning `true` in this case. Otherwise, if this cell is empty
 * (due to waiter cancellation, cell poisoning, or channel closing),
 * this function returns `false`.
 *
 * Notably, this function must be called only if the cell is covered by a sender.
 */


private fun findSegmentSend ( id : Long , startFrom : ChannelSegment < E > ) : ChannelSegment < E > ?

{ <EOL> return sendSegment . findSegmentAndMoveForward ( id , startFrom , createSegmentFunction ( ) ) . let { <EOL> if ( it . isClosed ) { <EOL> completeCloseOrCancel ( ) <EOL> if ( startFrom . id * SEGMENT_SIZE < receiversCounter ) startFrom . cleanPrev ( ) <EOL> null <EOL> } else { <EOL> val segment = it . segment <EOL> if ( segment . id > id ) { <EOL> updateSendersCounterIfLower ( segment . id * SEGMENT_SIZE ) <EOL> if ( segment . id * SEGMENT_SIZE < receiversCounter ) segment . cleanPrev ( ) <EOL> null <EOL> } else { <EOL> assert { segment . id == id } <EOL> segment <EOL> } <EOL> } <EOL> } <EOL> }

/**
 * Finds the segment with the specified [id] starting by the [startFrom]
 * segment and following the [ChannelSegment.next] references. In case
 * the required segment has not been created yet, this function attempts
 * to add it to the underlying linked list. Finally, it updates [sendSegment]
 * to the found segment if its [ChannelSegment.id] is greater than the one
 * of the already stored segment.
 *
 * In case the requested segment is already removed, or if it should be allocated
 * but the linked list structure is closed for new segments addition, this function
 * returns `null`. The implementation also efficiently skips a sequence of removed
 * segments, updating the counter value in [sendersAndCloseStatus] correspondingly.
 */