package std.util import java.util.* /** Returns the size of the collection */ val Collection<*>.size : Int get() = size() /** Returns true if this collection is empty */ val Collection<*>.empty : Boolean get() = isEmpty() /** Returns a new ArrayList with a variable number of initial elements */ inline fun arrayList(vararg values: T) : ArrayList = values.to(ArrayList(values.size)) /** Returns a new LinkedList with a variable number of initial elements */ inline fun linkedList(vararg values: T) : LinkedList = values.to(LinkedList()) /** Returns a new HashSet with a variable number of initial elements */ inline fun hashSet(vararg values: T) : HashSet = values.to(HashSet(values.size)) inline fun hashMap(): HashMap = HashMap() inline fun sortedMap(): SortedMap = TreeMap() val Collection<*>.indices : IntRange get() = 0..size-1 inline fun java.util.Collection.toArray() : Array { val answer = Array(this.size) var idx = 0 for (elem in this) answer[idx++] = elem return answer as Array } /** TODO these functions don't work when they generate the Array versions when they are in JavaIterables */ inline fun > java.lang.Iterable.toSortedList() : List = toList().sort() inline fun > java.lang.Iterable.toSortedList(comparator: java.util.Comparator) : List = toList().sort(comparator) // List APIs inline fun > List.sort() : List { Collections.sort(this) return this } inline fun > List.sort(comparator: java.util.Comparator) : List { Collections.sort(this, comparator) return this } /** TODO figure out necessary variance/generics ninja stuff... :) inline fun List.sort(transform: fun(T) : java.lang.Comparable<*>) : List { val comparator = java.util.Comparator() { fun compare(o1: T, o2: T): Int { val v1 = transform(o1) val v2 = transform(o2) if (v1 == v2) { return 0 } else { return v1.compareTo(v2) } } } answer.sort(comparator) } */ val List.head : T? get() = this.get(0) val List.first : T? get() = this.head val List.tail : T? get() { val s = this.size return if (s > 0) this.get(s - 1) else null } val List.last : T? get() = this.tail