JS stdlib: added LinkedHashMap abd LinkedHashSet.

This commit is contained in:
Zalim Bashorov
2014-06-18 22:26:42 +04:00
parent a2584dc6d7
commit 6f5c88c21f
4 changed files with 106 additions and 0 deletions
+3
View File
@@ -98,6 +98,9 @@ public open class HashMap<K, V>(capacity: Int = 0) : MutableMap<K, V> {
public override fun entrySet(): MutableSet<MutableMap.MutableEntry<K, V>> = js.noImpl
}
library
public open class LinkedHashMap<K, V>(capacity: Int = 0) : HashMap<K, V>(capacity)
library
public class NoSuchElementException(message: String? = null) : Exception() {}
+93
View File
@@ -590,6 +590,99 @@
return this.map;
}
});
function LinkedHashMap() {
Kotlin.ComplexHashMap.call(this);
this.orderedKeys = [];
this.super_put_wn2jw4$ = this.put_wn2jw4$;
this.put_wn2jw4$ = function(key, value) {
if (!this.containsKey_za3rmp$(key)) {
this.orderedKeys.push(key);
}
return this.super_put_wn2jw4$(key, value);
};
this.super_remove_za3rmp$ = this.remove_za3rmp$;
this.remove_za3rmp$ = function(key) {
var i = this.orderedKeys.indexOf(key);
if (i != -1) {
this.orderedKeys.splice(i, 1);
}
return this.super_remove_za3rmp$(key);
};
this.super_clear = this.clear;
this.clear = function() {
this.super_clear();
this.orderedKeys = [];
};
this.keySet = function() {
// TODO return special Set which unsupported adding
var set = new Kotlin.LinkedHashSet();
set.map = this;
return set;
};
this.values = function() {
var set = new Kotlin.LinkedHashSet();
for (var i = 0, c = this.orderedKeys, l = c.length; i < l; i++) {
set.add_za3rmp$(this.get_za3rmp$(c[i]));
}
return set;
};
this.entrySet = function() {
var set = new Kotlin.LinkedHashSet();
for (var i = 0, c = this.orderedKeys, l = c.length; i < l; i++) {
set.add_za3rmp$(new Entry(c[i], this.get_za3rmp$(c[i])));
}
return set;
};
}
LinkedHashMap.prototype = Object.create(Kotlin.ComplexHashMap);
Kotlin.LinkedHashMap = LinkedHashMap;
Kotlin.LinkedHashSet = Kotlin.createClassNow(Kotlin.AbstractCollection,
/** @constructs */
function () {
this.map = new Kotlin.LinkedHashMap();
},
/** @lends {Kotlin.LinkedHashSet.prototype} */
{
size: function () {
return this.map.size()
},
contains_za3rmp$: function (element) {
return this.map.containsKey_za3rmp$(element);
},
iterator: function () {
return new SetIterator(this);
},
add_za3rmp$: function (element) {
return this.map.put_wn2jw4$(element, true) == null;
},
remove_za3rmp$: function (element) {
return this.map.remove_za3rmp$(element) != null;
},
clear: function () {
this.map.clear();
},
toArray: function () {
return this.map.orderedKeys.slice();
}
});
}());
/**
+5
View File
@@ -16,6 +16,11 @@ class PrimitiveMapJsTest : MapJsTest() {
override fun emptyMutableMap(): MutableMap<String, Int> = HashMap()
}
class LinkedHashMapTest : MapJsTest() {
override fun <T : kotlin.Comparable<T>> Collection<T>.toNormalizedList(): List<T> = this.toList()
override fun emptyMutableMap(): MutableMap<String, Int> = LinkedHashMap()
}
abstract class MapJsTest {
//TODO: replace `array(...).toList()` to `listOf(...)`
val KEYS = array("zero", "one", "two", "three").toList()
+5
View File
@@ -3,6 +3,7 @@ package test.collections
import kotlin.test.*
import org.junit.Test
import java.util.HashSet
import java.util.LinkedHashSet
class ComplexSetJsTest : SetJsTest() {
// hashSetOf returns ComlpexHashSet because it is Generic
@@ -13,6 +14,10 @@ class PrimitiveSetJsTest : SetJsTest() {
override fun createEmptyMutableSet(): MutableSet<String> = HashSet<String>()
}
class LinkedHashSetTest : SetJsTest() {
override fun createEmptyMutableSet(): MutableSet<String> = LinkedHashSet<String>()
}
abstract class SetJsTest {
val data: Set<String> = createTestMutableSet()
val empty: Set<String> = createEmptyMutableSet()