From 6f5c88c21fae86c7777f4afdfeb16edba586bc12 Mon Sep 17 00:00:00 2001 From: Zalim Bashorov Date: Wed, 18 Jun 2014 22:26:42 +0400 Subject: [PATCH] JS stdlib: added LinkedHashMap abd LinkedHashSet. --- js/js.libraries/src/core/javautil.kt | 3 + js/js.translator/testData/maps.js | 93 +++++++++++++++++++++++++++ libraries/stdlib/test/js/MapJsTest.kt | 5 ++ libraries/stdlib/test/js/SetJsTest.kt | 5 ++ 4 files changed, 106 insertions(+) diff --git a/js/js.libraries/src/core/javautil.kt b/js/js.libraries/src/core/javautil.kt index fc34253bb10..9113fb56c59 100644 --- a/js/js.libraries/src/core/javautil.kt +++ b/js/js.libraries/src/core/javautil.kt @@ -98,6 +98,9 @@ public open class HashMap(capacity: Int = 0) : MutableMap { public override fun entrySet(): MutableSet> = js.noImpl } +library +public open class LinkedHashMap(capacity: Int = 0) : HashMap(capacity) + library public class NoSuchElementException(message: String? = null) : Exception() {} diff --git a/js/js.translator/testData/maps.js b/js/js.translator/testData/maps.js index ffed41daa11..bc222f7f629 100644 --- a/js/js.translator/testData/maps.js +++ b/js/js.translator/testData/maps.js @@ -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(); + } + }); + }()); /** diff --git a/libraries/stdlib/test/js/MapJsTest.kt b/libraries/stdlib/test/js/MapJsTest.kt index 88c76b9db22..4e288d97673 100644 --- a/libraries/stdlib/test/js/MapJsTest.kt +++ b/libraries/stdlib/test/js/MapJsTest.kt @@ -16,6 +16,11 @@ class PrimitiveMapJsTest : MapJsTest() { override fun emptyMutableMap(): MutableMap = HashMap() } +class LinkedHashMapTest : MapJsTest() { + override fun > Collection.toNormalizedList(): List = this.toList() + override fun emptyMutableMap(): MutableMap = LinkedHashMap() +} + abstract class MapJsTest { //TODO: replace `array(...).toList()` to `listOf(...)` val KEYS = array("zero", "one", "two", "three").toList() diff --git a/libraries/stdlib/test/js/SetJsTest.kt b/libraries/stdlib/test/js/SetJsTest.kt index bb6b366e5d8..eb9e846d253 100644 --- a/libraries/stdlib/test/js/SetJsTest.kt +++ b/libraries/stdlib/test/js/SetJsTest.kt @@ -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 = HashSet() } +class LinkedHashSetTest : SetJsTest() { + override fun createEmptyMutableSet(): MutableSet = LinkedHashSet() +} + abstract class SetJsTest { val data: Set = createTestMutableSet() val empty: Set = createEmptyMutableSet()