Merge pull request #88 from zolotov/fix-reverse-and-sort-for-lists

Fix reverse and sort functions for lists
This commit is contained in:
James Strachan
2012-07-02 03:42:05 -07:00
16 changed files with 71 additions and 39 deletions
@@ -245,7 +245,8 @@ public inline fun <in T, C: Collection<in T>> Array<T>.toCollection(result: C) :
*/
public inline fun <T> Array<T>.reverse() : List<T> {
val list = toList()
return list.reverse()
Collections.reverse(list)
return list
}
/** Copies all elements into a [[LinkedList]] */
@@ -245,7 +245,8 @@ public inline fun <C: Collection<Boolean>> BooleanArray.toCollection(result: C)
*/
public inline fun BooleanArray.reverse() : List<Boolean> {
val list = toList()
return list.reverse()
Collections.reverse(list)
return list
}
/** Copies all elements into a [[LinkedList]] */
@@ -245,7 +245,8 @@ public inline fun <C: Collection<Byte>> ByteArray.toCollection(result: C) : C {
*/
public inline fun ByteArray.reverse() : List<Byte> {
val list = toList()
return list.reverse()
Collections.reverse(list)
return list
}
/** Copies all elements into a [[LinkedList]] */
@@ -245,7 +245,8 @@ public inline fun <C: Collection<Char>> CharArray.toCollection(result: C) : C {
*/
public inline fun CharArray.reverse() : List<Char> {
val list = toList()
return list.reverse()
Collections.reverse(list)
return list
}
/** Copies all elements into a [[LinkedList]] */
@@ -245,7 +245,8 @@ public inline fun <C: Collection<Double>> DoubleArray.toCollection(result: C) :
*/
public inline fun DoubleArray.reverse() : List<Double> {
val list = toList()
return list.reverse()
Collections.reverse(list)
return list
}
/** Copies all elements into a [[LinkedList]] */
@@ -245,7 +245,8 @@ public inline fun <C: Collection<Float>> FloatArray.toCollection(result: C) : C
*/
public inline fun FloatArray.reverse() : List<Float> {
val list = toList()
return list.reverse()
Collections.reverse(list)
return list
}
/** Copies all elements into a [[LinkedList]] */
@@ -245,7 +245,8 @@ public inline fun <C: Collection<Int>> IntArray.toCollection(result: C) : C {
*/
public inline fun IntArray.reverse() : List<Int> {
val list = toList()
return list.reverse()
Collections.reverse(list)
return list
}
/** Copies all elements into a [[LinkedList]] */
@@ -243,7 +243,8 @@ public inline fun <in T, C: Collection<in T>> java.util.Iterator<T>.toCollection
*/
public inline fun <T> java.util.Iterator<T>.reverse() : List<T> {
val list = toList()
return list.reverse()
Collections.reverse(list)
return list
}
/** Copies all elements into a [[LinkedList]] */
@@ -245,7 +245,8 @@ public inline fun <C: Collection<Long>> LongArray.toCollection(result: C) : C {
*/
public inline fun LongArray.reverse() : List<Long> {
val list = toList()
return list.reverse()
Collections.reverse(list)
return list
}
/** Copies all elements into a [[LinkedList]] */
@@ -245,7 +245,8 @@ public inline fun <C: Collection<Short>> ShortArray.toCollection(result: C) : C
*/
public inline fun ShortArray.reverse() : List<Short> {
val list = toList()
return list.reverse()
Collections.reverse(list)
return list
}
/** Copies all elements into a [[LinkedList]] */
@@ -245,7 +245,8 @@ public inline fun <in T, C: Collection<in T>> Iterable<T>.toCollection(result: C
*/
public inline fun <T> Iterable<T>.reverse() : List<T> {
val list = toList()
return list.reverse()
Collections.reverse(list)
return list
}
/** Copies all elements into a [[LinkedList]] */
@@ -229,7 +229,8 @@ public inline fun <in T, C: Collection<in T>> java.lang.Iterable<T>.toCollection
*/
public inline fun <T> java.lang.Iterable<T>.reverse() : List<T> {
val list = toList()
return list.reverse()
Collections.reverse(list)
return list
}
/** Copies all elements into a [[LinkedList]] */
@@ -106,6 +106,18 @@ public fun <T> java.lang.Iterable<T>.withIndices() : java.lang.Iterable<#(Int, T
}
}
public inline fun <in T: java.lang.Comparable<T>> java.lang.Iterable<T>.sort() : List<T> {
val list = toList()
java.util.Collections.sort(list)
return list
}
public inline fun <in T> java.lang.Iterable<T>.sort(comparator: java.util.Comparator<T>) : List<T> {
val list = toList()
java.util.Collections.sort(list, comparator)
return list
}
private class NumberedIterator<TT>(private val sourceIterator : java.util.Iterator<TT>) : java.util.Iterator<#(Int, TT)> {
private var nextIndex = 0
-20
View File
@@ -40,26 +40,6 @@ public inline fun <in T: java.lang.Comparable<T>> java.lang.Iterable<T>.toSorted
// List APIs
/**
* Reverses the order the elements into a list
*
* @includeFunctionBody ../../test/CollectionTest.kt reverse
*/
public inline fun <T> List<T>.reverse() : List<T> {
Collections.reverse(this)
return this
}
public inline fun <in T: java.lang.Comparable<T>> List<T>.sort() : List<T> {
Collections.sort(this)
return this
}
public inline fun <in T> List<T>.sort(comparator: java.util.Comparator<T>) : List<T> {
Collections.sort(this, comparator)
return this
}
/** Returns the List if its not null otherwise returns the empty list */
public inline fun <T> java.util.List<T>?.orEmpty() : java.util.List<T>
= if (this != null) this else Collections.emptyList<T>() as java.util.List<T>
+11
View File
@@ -88,4 +88,15 @@ class ArraysTest {
}
}
test fun reverse() {
expect(arrayList(3, 2, 1)) { intArray(1, 2, 3).reverse() }
expect(arrayList<Byte>(3, 2, 1)) { byteArray(1, 2, 3).reverse() }
expect(arrayList<Short>(3, 2, 1)) { shortArray(1, 2, 3).reverse() }
expect(arrayList<Long>(3, 2, 1)) { longArray(1, 2, 3).reverse() }
expect(arrayList(3.toFloat(), 2.toFloat(), 1.toFloat())) { floatArray(1.toFloat(), 2.toFloat(), 3.toFloat()).reverse() }
expect(arrayList(3.0, 2.0, 1.0)) { doubleArray(1.0, 2.0, 3.0).reverse() }
expect(arrayList('3', '2', '1')) { charArray('1', '2', '3').reverse() }
expect(arrayList(false, false, true)) { booleanArray(true, false, false).reverse() }
}
}
+24 -7
View File
@@ -325,16 +325,33 @@ class CollectionTest {
assertEquals(arrayList("bar", "foo"), rev)
}
test fun sort() {
val coll: List<String> = arrayList("foo", "bar", "abc")
test fun reverseFunctionShouldReturnReversedCopyForList() {
val list : List<Int> = arrayList(2, 3, 1)
expect(arrayList(1, 3, 2)) { list.reverse() }
expect(arrayList(2, 3, 1)) { list }
}
// TODO fixme
// Some sort of in/out variance thing - or an issue with Java interop?
//coll.sort()
test fun reverseFunctionShouldReturnReversedCopyForIterable() {
val iterable : java.lang.Iterable<Int> = arrayList(2, 3, 1)
expect(arrayList(1, 3, 2)) { iterable.reverse() }
expect(arrayList(2, 3, 1)) { iterable }
}
test fun sortFunctionShouldReturnSortedCopyForList() {
// TODO fixme Some sort of in/out variance thing - or an issue with Java interop?
todo {
assertEquals(3, coll.size)
assertEquals(arrayList("abc", "bar", "foo"), coll)
// val list : List<Int> = arrayList<Int>(2, 3, 1)
// expect(arrayList(1, 2, 3)) { list.sort() }
// expect(arrayList(2, 3, 1)) { list }
}
}
test fun sortFunctionShouldReturnSortedCopyForIterable() {
// TODO fixme Some sort of in/out variance thing - or an issue with Java interop?
todo {
// val list : java.lang.Iterable<Int> = arrayList(2, 3, 1)
// expect(arrayList(1, 2, 3)) { list.sort() }
// expect(arrayList(2, 3, 1)) { list }
}
}