Use array based storage in container objects (#3600)
Currently, collections use object based solutions for storing elements and iterating on them. If an element is deleted and re-inserted, the storage position is the same as before so the iteration order is wrong. This patch replaces the object based storage with an array based solution that helps to store and iterate elements as expected. JerryScript-DCO-1.0-Signed-off-by: Roland Takacs rtakacs@inf.u-szeged.hu
This commit is contained in:
@@ -32,13 +32,15 @@ methods.forEach(function (method) {
|
||||
}
|
||||
});
|
||||
|
||||
var m = new Map([{0: '0', 1: 0},
|
||||
var testArray = [{0: '0', 1: 0},
|
||||
{0: '1', 1: 1},
|
||||
{0: '2', 1: 2},
|
||||
{0: '3', 1: 3},
|
||||
{0: '4', 1: 4},
|
||||
{0: '5', 1: 5},
|
||||
{0: '6', 1: 6}]);
|
||||
{0: '6', 1: 6}];
|
||||
|
||||
var m = new Map(testArray);
|
||||
|
||||
methods.forEach(function(method) {
|
||||
assert(m[method]().toString() === '[object Map Iterator]');
|
||||
@@ -122,3 +124,61 @@ for (var i = 0; i < elementCount; i++) {
|
||||
}
|
||||
|
||||
assert(m.size === 0);
|
||||
|
||||
m = new Map(testArray);
|
||||
var loopCount = 0;
|
||||
var expected = [{0: '0', 1: 0},
|
||||
{0: '2', 1: 2},
|
||||
{0: '4', 1: 4},
|
||||
{0: '6', 1: 6},
|
||||
{0: '1', 1: 1},
|
||||
{0: '3', 1: 3},
|
||||
{0: '5', 1: 5}]
|
||||
|
||||
m.forEach(function(value, key) {
|
||||
if (loopCount === 0) {
|
||||
for (i = 0; i < testArray.length; i++) {
|
||||
if (i % 2) {
|
||||
m.delete(testArray[i][0]);
|
||||
m.set(testArray[i][0], testArray[i][1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assert (key === expected[loopCount][0]);
|
||||
assert (value === expected[loopCount][1]);
|
||||
|
||||
loopCount++;
|
||||
});
|
||||
|
||||
assert(loopCount === expected.length);
|
||||
|
||||
loopCount = 0;
|
||||
expected = [{0: '0', 1: 0},
|
||||
{0: '1', 1: 1}];
|
||||
|
||||
for (var [key, value] of m) {
|
||||
if (loopCount === 0) {
|
||||
m.clear();
|
||||
m.set('1', 1);
|
||||
}
|
||||
|
||||
assert(key === expected[loopCount][0]);
|
||||
assert(value === expected[loopCount][1]);
|
||||
|
||||
loopCount++;
|
||||
}
|
||||
|
||||
m = new Map(testArray);
|
||||
loopCount = 0;
|
||||
|
||||
for (var [key, value] of m) {
|
||||
if (loopCount === 0) {
|
||||
m.delete('' + testArray.length - 1);
|
||||
}
|
||||
|
||||
assert(key === '' + loopCount);
|
||||
assert(value === loopCount);
|
||||
|
||||
loopCount++;
|
||||
}
|
||||
|
||||
@@ -106,3 +106,49 @@ for (var i = 0; i < elementCount; i++) {
|
||||
}
|
||||
|
||||
assert(s.size === 0);
|
||||
|
||||
s = new Set ([0, 1]);
|
||||
var expected = [0, 1, 2, 4, 5, 6, 3];
|
||||
var loopCount = 0;
|
||||
|
||||
s.forEach(function(element) {
|
||||
if (loopCount === 0) {
|
||||
for (var i = 0; i < expected.length ; i++) {
|
||||
s.add(i);
|
||||
}
|
||||
s.delete(3);
|
||||
s.add(3);
|
||||
}
|
||||
assert(element === expected[loopCount++]);
|
||||
});
|
||||
|
||||
assert(loopCount === expected.length);
|
||||
|
||||
s = new Set([0, 1, 2, 3, 4, 5, 6]);
|
||||
expected = [0, 1];
|
||||
loopCount = 0;
|
||||
|
||||
for (var value of s) {
|
||||
if (loopCount === 0) {
|
||||
s.clear();
|
||||
s.add(1);
|
||||
}
|
||||
|
||||
assert(value === expected[loopCount++]);
|
||||
}
|
||||
|
||||
s = new Set([0])
|
||||
expected = [0, 1];
|
||||
loopCount = 0;
|
||||
|
||||
for (var value of s) {
|
||||
if (loopCount === 0) {
|
||||
s.add(2);
|
||||
s.delete(2);
|
||||
s.add(3);
|
||||
s.delete(3);
|
||||
s.add(1);
|
||||
}
|
||||
|
||||
assert(value === expected[loopCount++]);
|
||||
}
|
||||
|
||||
@@ -115,7 +115,7 @@ m1.set(k1, "str");
|
||||
m1.set(k1, "4");
|
||||
m1.set(k1, null);
|
||||
m1.set(k1, 42);
|
||||
print (m1.has (k1));
|
||||
assert (m1.has (k1));
|
||||
k1 = {};
|
||||
gc();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user