From 89eb7ba4ac0309aed60815f7b7a85c29e29e5aa9 Mon Sep 17 00:00:00 2001 From: James Strachan Date: Mon, 16 Apr 2012 19:48:05 +0100 Subject: [PATCH] added some missing sortedSet() methods and kept simpler naming convention of (sorted|Linked)(Map|Set)() for functions to create linked/sorted map/set --- libraries/stdlib/src/kotlin/JUtil.kt | 9 ++++++++- libraries/stdlib/src/kotlin/Standard.kt | 20 ++++++++++++++------ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/libraries/stdlib/src/kotlin/JUtil.kt b/libraries/stdlib/src/kotlin/JUtil.kt index 98ac1dabfab..1fa691766c2 100644 --- a/libraries/stdlib/src/kotlin/JUtil.kt +++ b/libraries/stdlib/src/kotlin/JUtil.kt @@ -20,9 +20,16 @@ public inline fun linkedList(vararg values: T) : LinkedList = values.to(L /** Returns a new HashSet with a variable number of initial elements */ public inline fun hashSet(vararg values: T) : HashSet = values.to(HashSet(values.size)) -/** Returns a new SortedSet with a variable number of initial elements */ +/** + * Returns a new [[SortedSet]] with the initial elements + */ public inline fun sortedSet(vararg values: T) : TreeSet = values.to(TreeSet()) +/** + * Returns a new [[SortedSet]] with the given *comparator* and the initial elements + */ +public inline fun sortedSet(comparator: Comparator, vararg values: T) : TreeSet = values.to(TreeSet(comparator)) + /** * Returns a new [[HashMap]] populated with the given tuple values where the first value in each tuple * is the key and the second value is the value diff --git a/libraries/stdlib/src/kotlin/Standard.kt b/libraries/stdlib/src/kotlin/Standard.kt index 4426c507b3c..ec63b159b77 100644 --- a/libraries/stdlib/src/kotlin/Standard.kt +++ b/libraries/stdlib/src/kotlin/Standard.kt @@ -6,6 +6,8 @@ import java.util.LinkedList import java.util.HashSet import java.util.LinkedHashSet import java.util.TreeSet +import java.util.SortedSet +import java.util.Comparator /** Helper to make jet.Iterator usable in for @@ -56,14 +58,20 @@ Add iterated elements to java.util.HashSet public inline fun Iterator.toHashSet() : HashSet = to(HashSet()) /** -Add iterated elements to java.util.LinkedHashSet -*/ -public inline fun Iterator.toLinkedHashSet() : LinkedHashSet = to(LinkedHashSet()) + * Add iterated elements to a [[LinkedHashSet]] to preserve insertion order + */ +public inline fun Iterator.toLinkedSet() : LinkedHashSet = to(LinkedHashSet()) /** -Add iterated elements to java.util.TreeSet -*/ -public inline fun Iterator.toTreeSet() : TreeSet = to(TreeSet()) + * Add iterated elements to [[SortedSet]] to ensure iteration is in the order of the default comparator + * for the type + */ +public inline fun Iterator.toSortedSet() : SortedSet = to(TreeSet()) + +/** + * Add iterated elements to [[SortedSet]] with the given *comparator* to ensure iteration is in the order of the given comparator + */ +public inline fun Iterator.toSortedSet(comparator: Comparator) : SortedSet = to(TreeSet(comparator)) /** Run function f