All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kotlin.deprecated._Iterators.kt Maven / Gradle / Ivy

package kotlin

//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//

import java.util.*
import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js

/**
 * Returns *true* if all elements match the given *predicate*
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public inline fun  Iterator.all(predicate: (T) -> Boolean) : Boolean {
    for (element in this) if (!predicate(element)) return false
    return true
}

/**
 * Returns *true* if any elements match the given *predicate*
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public inline fun  Iterator.any(predicate: (T) -> Boolean) : Boolean {
    for (element in this) if (predicate(element)) return true
    return false
}

/**
 * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
 * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
 * a special *truncated* separator (which defaults to "..."
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public fun  Iterator.appendString(buffer: Appendable, separator: String = ", ", prefix: String ="", postfix: String = "", limit: Int = -1, truncated: String = "...") : Unit {
    buffer.append(prefix)
    var count = 0
    for (element in this) {
        if (++count > 1) buffer.append(separator)
        if (limit < 0 || count <= limit) {
            val text = if (element == null) "null" else element.toString()
            buffer.append(text)
        } else break
    }
    if (limit >= 0 && count > limit) buffer.append(truncated)
    buffer.append(postfix)
}

/**
 * Returns the number of elements which match the given *predicate*
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public inline fun  Iterator.count(predicate: (T) -> Boolean) : Int {
    var count = 0
    for (element in this) if (predicate(element)) count++
    return count
}

/**
 * Returns a list containing everything but the first *n* elements
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public fun  Iterator.drop(n: Int) : List {
    return dropWhile(countTo(n))
}

/**
 * Returns a list containing the everything but the first elements that satisfy the given *predicate*
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public inline fun  Iterator.dropWhile(predicate: (T) -> Boolean) : List {
    return dropWhileTo(ArrayList(), predicate)
}

/**
 * Returns a list containing the everything but the first elements that satisfy the given *predicate*
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public inline fun > Iterator.dropWhileTo(result: L, predicate: (T) -> Boolean) : L {
    var start = true
    for (element in this) {
        if (start && predicate(element)) {
            // ignore
        } else {
            start = false
            result.add(element)
        }
    }
    return result
}

/**
 * Returns an iterator over elements which match the given *predicate*
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public fun  Iterator.filter(predicate: (T) -> Boolean) : Iterator {
    return FilterIterator(this, predicate)
}

/**
 * Returns an iterator over elements which don't match the given *predicate*
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public inline fun  Iterator.filterNot(inlineOptions(InlineOption.ONLY_LOCAL_RETURN) predicate: (T) -> Boolean) : Iterator {
    return filter {!predicate(it)}
}

/**
 * Returns an iterator over non-*null* elements
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public fun  Iterator.filterNotNull() : Iterator {
    return FilterNotNullIterator(this)
}

/**
 * Filters all non-*null* elements into the given list
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public fun > Iterator.filterNotNullTo(result: C) : C {
    for (element in this) if (element != null) result.add(element)
    return result
}

/**
 * Returns a list containing all elements which do not match the given *predicate*
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public inline fun > Iterator.filterNotTo(result: C, predicate: (T) -> Boolean) : C {
    for (element in this) if (!predicate(element)) result.add(element)
    return result
}

/**
 * Filters all elements which match the given predicate into the given list
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public inline fun > Iterator.filterTo(result: C, predicate: (T) -> Boolean) : C {
    for (element in this) if (predicate(element)) result.add(element)
    return result
}

/**
 * Returns the first element which matches the given *predicate* or *null* if none matched
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public inline fun  Iterator.find(predicate: (T) -> Boolean) : T? {
    for (element in this) if (predicate(element)) return element
    return null
}

/**
 * Returns an iterator over the concatenated results of transforming each element to one or more values
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public fun  Iterator.flatMap(transform: (T) -> Iterator) : Iterator {
    return FlatMapIterator(this, transform)
}

/**
 * Returns the result of transforming each element to one or more values which are concatenated together into a single collection
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public inline fun > Iterator.flatMapTo(result: C, transform: (T) -> Iterable) : C {
    for (element in this) {
        val list = transform(element)
        for (r in list) result.add(r)
    }
    return result
}

/**
 * Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public inline fun  Iterator.fold(initial: R, operation: (R, T) -> R) : R {
    var answer = initial
    for (element in this) answer = operation(answer, element)
    return answer
}

/**
 * Performs the given *operation* on each element
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public inline fun  Iterator.forEach(operation: (T) -> Unit) : Unit {
    for (element in this) operation(element)
}

/**
 * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public inline fun  Iterator.groupBy(toKey: (T) -> K) : Map> {
    return groupByTo(HashMap>(), toKey)
}

deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public inline fun  Iterator.groupByTo(result: MutableMap>, toKey: (T) -> K) : Map> {
    for (element in this) {
        val key = toKey(element)
        val list = result.getOrPut(key) { ArrayList() }
        list.add(element)
    }
    return result
}

/**
 * Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
 * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
 * a special *truncated* separator (which defaults to "..."
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public fun  Iterator.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...") : String {
    val buffer = StringBuilder()
    appendString(buffer, separator, prefix, postfix, limit, truncated)
    return buffer.toString()
}

// -------------------------

/**
 * Returns an iterator obtained by applying *transform*, a function transforming an object of type *T* into an object of type *R*
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public fun  Iterator.map(transform : (T) -> R) : Iterator {
    return MapIterator(this, transform)
}

/**
 * Transforms each element of this collection with the given *transform* function and
 * adds each return value to the given *results* collection
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public inline fun > Iterator.mapTo(result: C, transform : (T) -> R) : C {
    for (item in this)
        result.add(transform(item))
    return result
}

/**
 * Returns the largest element or null if there are no elements
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public fun > Iterator.max() : T? {
    if (!hasNext()) return null

    var max = next()
    while (hasNext()) {
        val e = next()
        if (max < e) max = e
    }
    return max
}

/**
 * Returns the first element yielding the largest value of the given function or null if there are no elements
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public inline fun , T: Any> Iterator.maxBy(f: (T) -> R) : T? {
    if (!hasNext()) return null

    var maxElem = next()
    var maxValue = f(maxElem)
    while (hasNext()) {
        val e = next()
        val v = f(e)
        if (maxValue < v) {
           maxElem = e
           maxValue = v
        }
    }
    return maxElem
}

/**
 * Returns the smallest element or null if there are no elements
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public fun > Iterator.min() : T? {
    if (!hasNext()) return null

    var min = next()
    while (hasNext()) {
        val e = next()
        if (min > e) min = e
    }
    return min
}

/**
 * Returns the first element yielding the smallest value of the given function or null if there are no elements
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public inline fun , T: Any> Iterator.minBy(f: (T) -> R) : T? {
    if (!hasNext()) return null

    var minElem = next()
    var minValue = f(minElem)
    while (hasNext()) {
        val e = next()
        val v = f(e)
        if (minValue > v) {
           minElem = e
           minValue = v
        }
    }
    return minElem
}

/**
 * Partitions this collection into a pair of collections
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public inline fun  Iterator.partition(predicate: (T) -> Boolean) : Pair, List> {
    val first = ArrayList()
    val second = ArrayList()
    for (element in this) {
        if (predicate(element)) {
            first.add(element)
        } else {
            second.add(element)
        }
    }
    return Pair(first, second)
}

/**
 * Creates an [[Iterator]] which iterates over this iterator then the following collection
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public fun  Iterator.plus(collection: Iterable) : Iterator {
    return plus(collection.iterator())
}

/**
 * Creates an [[Iterator]] which iterates over this iterator then the given element at the end
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public fun  Iterator.plus(element: T) : Iterator {
    return CompositeIterator(this, SingleIterator(element))
}

/**
 * Creates an [[Iterator]] which iterates over this iterator then the following iterator
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public fun  Iterator.plus(iterator: Iterator) : Iterator {
    return CompositeIterator(this, iterator)
}

/**
 * Applies binary operation to all elements of iterable, going from left to right.
 * Similar to fold function, but uses the first element as initial value
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public inline fun  Iterator.reduce(operation: (T, T) -> T) : T {
    val iterator = this.iterator()
    if (!iterator.hasNext()) {
        throw UnsupportedOperationException("Empty iterable can't be reduced")
    }

    var result: T = iterator.next() //compiler doesn't understand that result will initialized anyway
    while (iterator.hasNext()) {
        result = operation(result, iterator.next())
    }

    return result
}

/**
 * Returns a original Iterable containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public fun  Iterator.requireNoNulls() : Iterator {
    return map{
        if (it == null) throw IllegalArgumentException("null element in iterator $this") else it
    }
}

/**
 * Reverses the order the elements into a list
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public fun  Iterator.reverse() : List {
    val list = toCollection(ArrayList())
    Collections.reverse(list)
    return list
}

/**
 * Copies all elements into a [[List]] and sorts it by value of compare_function(element)
 * E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public inline fun > Iterator.sortBy(inlineOptions(InlineOption.ONLY_LOCAL_RETURN) f: (T) -> R) : List {
    val sortedList = toCollection(ArrayList())
    val sortBy: Comparator = comparator {(x: T, y: T) ->
        val xr = f(x)
        val yr = f(y)
        xr.compareTo(yr)
    }
    java.util.Collections.sort(sortedList, sortBy)
    return sortedList
}

/**
 * Returns an iterator restricted to the first *n* elements
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public fun  Iterator.take(n: Int) : Iterator {
    var count = n
    return takeWhile{ --count >= 0 }
}

/**
 * Returns an iterator restricted to the first elements that match the given *predicate*
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public fun  Iterator.takeWhile(predicate: (T) -> Boolean) : Iterator {
    return TakeWhileIterator(this, predicate)
}

/**
 * Returns a list containing the first elements that satisfy the given *predicate*
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public inline fun > Iterator.takeWhileTo(result: C, predicate: (T) -> Boolean) : C {
    for (element in this) if (predicate(element)) result.add(element) else break
    return result
}

/**
 * Copies all elements into the given collection
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public fun > Iterator.toCollection(result: C) : C {
    for (element in this) result.add(element)
    return result
}

/**
 * Copies all elements into a [[LinkedList]]
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public fun  Iterator.toLinkedList() : LinkedList {
    return toCollection(LinkedList())
}

/**
 * Copies all elements into a [[List]]
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public fun  Iterator.toList() : List {
    return toCollection(ArrayList())
}

/**
 * Copies all elements into a [[ArrayList]]
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public fun  Iterator.toArrayList() : ArrayList {
    return toCollection(ArrayList())
}

/**
 * Copies all elements into a [[Set]]
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public fun  Iterator.toSet() : Set {
    return toCollection(LinkedHashSet())
}

/**
 * Copies all elements into a [[HashSet]]
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public fun  Iterator.toHashSet() : HashSet {
    return toCollection(HashSet())
}

/**
 * Copies all elements into a [[SortedSet]]
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public fun  Iterator.toSortedSet() : SortedSet {
    return toCollection(TreeSet())
}

/**
 * Returns an iterator of Pairs(index, data)
 */
deprecated("Replace Iterator with Sequence by using sequence() function instead of iterator()")
public fun  Iterator.withIndices() : Iterator> {
    return IndexIterator(iterator())
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy