JS backend: added entrySet to HashMap implementations and fixed iteration over maps.

This commit is contained in:
Zalim Bashorov
2014-06-17 15:51:22 +04:00
parent 516af6c908
commit a2584dc6d7
2 changed files with 108 additions and 18 deletions
+55 -13
View File
@@ -17,6 +17,27 @@
(function () { (function () {
"use strict"; "use strict";
/**
* @class
* @constructor
* @param {K} key
* @param {V} value
* @template K, V
*/
function Entry(key, value) {
this.key = key;
this.value = value;
}
Entry.prototype.getKey = function () {
return this.key;
};
Entry.prototype.getValue = function () {
return this.value;
};
/** @const */ /** @const */
var FUNCTION = "function"; var FUNCTION = "function";
var arrayRemoveAt = (typeof Array.prototype.splice == FUNCTION) ? var arrayRemoveAt = (typeof Array.prototype.splice == FUNCTION) ?
@@ -400,26 +421,36 @@
} }
return res; return res;
}; };
this.entrySet = function () {
var result = new Kotlin.ComplexHashSet();
var entries = this._entries();
var i = entries.length;
while (i--) {
var entry = entries[i];
result.add_za3rmp$(new Entry(entry[0], entry[1]));
}
return result;
}
}; };
Kotlin.HashTable = Hashtable; Kotlin.HashTable = Hashtable;
})();
/** /**
* @interface * @interface
* @template Key, Value * @template Key, Value
*/ */
Kotlin.Map = Kotlin.createClassNow(); Kotlin.Map = Kotlin.createClassNow();
Kotlin.HashMap = Kotlin.createClassNow(Kotlin.Map, Kotlin.HashMap = Kotlin.createClassNow(Kotlin.Map,
function () { function () {
Kotlin.HashTable.call(this); Kotlin.HashTable.call(this);
} }
); );
Kotlin.ComplexHashMap = Kotlin.HashMap; Kotlin.ComplexHashMap = Kotlin.HashMap;
(function () {
/** /**
* @class * @class
* @implements Kotlin.Iterator.<Value> * @implements Kotlin.Iterator.<Value>
@@ -530,6 +561,17 @@ Kotlin.ComplexHashMap = Kotlin.HashMap;
} }
} }
}, },
entrySet: function () {
var result = new Kotlin.ComplexHashSet();
var map = this.map;
for (var key in map) {
if (map.hasOwnProperty(key)) {
result.add_za3rmp$(new Entry(key, map[key]));
}
}
return result;
},
keySet: function () { keySet: function () {
var result = new Kotlin.PrimitiveHashSet(); var result = new Kotlin.PrimitiveHashSet();
var map = this.map; var map = this.map;
+53 -5
View File
@@ -6,12 +6,14 @@ import java.util.*
import org.junit.Test as test import org.junit.Test as test
class ComplexMapJsTest : MapJsTest() { class ComplexMapJsTest : MapJsTest() {
override fun <T : kotlin.Comparable<T>> Collection<T>.toNormalizedList(): List<T> = this.toSortedList()
// hashMapOf returns ComlpexHashMap because it is Generic // hashMapOf returns ComlpexHashMap because it is Generic
override fun emptyMutableMap(): MutableMap<String, Int> = hashMapOf<String, Int>() override fun emptyMutableMap(): MutableMap<String, Int> = hashMapOf()
} }
class PrimitiveMapJsTest : MapJsTest() { class PrimitiveMapJsTest : MapJsTest() {
override fun emptyMutableMap(): MutableMap<String, Int> = HashMap<String, Int>() override fun <T : kotlin.Comparable<T>> Collection<T>.toNormalizedList(): List<T> = this.toSortedList()
override fun emptyMutableMap(): MutableMap<String, Int> = HashMap()
} }
abstract class MapJsTest { abstract class MapJsTest {
@@ -102,12 +104,26 @@ abstract class MapJsTest {
test fun mapValues() { test fun mapValues() {
val map = createTestMap() val map = createTestMap()
assertEquals(VALUES, map.values().toSortedList()) assertEquals(VALUES.toNormalizedList(), map.values().toNormalizedList())
} }
test fun mapKeySet() { test fun mapKeySet() {
val map = createTestMap() val map = createTestMap()
assertEquals(KEYS.toSortedList(), map.keySet().toSortedList()) assertEquals(KEYS.toNormalizedList(), map.keySet().toNormalizedList())
}
test fun mapEntrySet() {
val map = createTestMap()
val actualKeys = ArrayList<String>()
val actualValues = ArrayList<Int>()
for (e in map.entrySet()) {
actualKeys.add(e.getKey())
actualValues.add(e.getValue())
}
assertEquals(KEYS.toNormalizedList(), actualKeys.toNormalizedList())
assertEquals(VALUES.toNormalizedList(), actualValues.toNormalizedList())
} }
test fun mapContainsKey() { test fun mapContainsKey() {
@@ -196,6 +212,35 @@ abstract class MapJsTest {
assertEquals(2, map.get("b")) assertEquals(2, map.get("b"))
} }
test fun mapIteratorImplicitly() {
val map = createTestMap()
val actualKeys = ArrayList<String>()
val actualValues = ArrayList<Int>()
for (e in map) {
actualKeys.add(e.getKey())
actualValues.add(e.getValue())
}
assertEquals(KEYS.toNormalizedList(), actualKeys.toNormalizedList())
assertEquals(VALUES.toNormalizedList(), actualValues.toNormalizedList())
}
test fun mapIteratorExplicitly() {
val map = createTestMap()
val actualKeys = ArrayList<String>()
val actualValues = ArrayList<Int>()
val iterator = map.iterator()
for (e in iterator) {
actualKeys.add(e.getKey())
actualValues.add(e.getValue())
}
assertEquals(KEYS.toNormalizedList(), actualKeys.toNormalizedList())
assertEquals(VALUES.toNormalizedList(), actualValues.toNormalizedList())
}
/* /*
test fun createLinkedMap() { test fun createLinkedMap() {
val map = linkedMapOf("c" to 3, "b" to 2, "a" to 1) val map = linkedMapOf("c" to 3, "b" to 2, "a" to 1)
@@ -312,7 +357,10 @@ abstract class MapJsTest {
// Helpers // Helpers
fun emptyMap(): Map<String, Int> = HashMap<String, Int>() abstract fun <T : kotlin.Comparable<T>> Collection<T>.toNormalizedList(): List<T>
fun emptyMap(): Map<String, Int> = emptyMutableMap()
abstract fun emptyMutableMap(): MutableMap<String, Int> abstract fun emptyMutableMap(): MutableMap<String, Int>
fun createTestMap(): Map<String, Int> = createTestMutableMap() fun createTestMap(): Map<String, Int> = createTestMutableMap()