简体   繁体   English

flex字典错误?

[英]flex dictionary bug?

I was testing a HashMap implementation in AS3 . 我正在AS3中测试HashMap实现

I have tried the following code: 我尝试了以下代码:

 var map:IMap = new HashMap();
 map.put("a", "value A");
 map.put("b", "value B");
 map.put("c", "value C");
 map.put("x", "value X");
 map.put("y", "value Y");
 map.put("z", "value Z");

Then I called the clear() method: 然后我调用了clear()方法:

map.clear();

The size of the hashmap didn't become 0, but it was 1. The problem is that when the key is "y", it doesn't get removed. 哈希图的大小没有变为0,而是为1。问题在于,当键为“ y”时,不会将其删除。 The corresponding code is as follows: 相应的代码如下:

protected var map:Dictionary = null;

public function HashMap(useWeakReferences:Boolean = true)
{
    map = new Dictionary( useWeakReferences );
}

public function put(key:*, value:*) : void
{
    map[key] = value;
}

public function remove(key:*) : void
{
    map[ key ] = undefined;
    delete map[ key ];
}

public function clear() : void
{
    for ( var key:* in map )
    {
        remove( key );
    }
}

If I call the clear() function again, the remaining key will be removed: 如果再次调用clear()函数,则剩余的键将被删除:

if (size() != 0)
{
    clear();
}

Does anyone know what's the reason the y key doesn't get deleted? 有谁知道y键没有被删除的原因是什么?

I haven't had the time to look at the Dictionary implementation in tamarin (the VM for flash), but it seems that the Dictionary is beeing rehashed when a value is reaffected to the map by the line map[ key ] = undefined; 我还没有时间在tamarin(用于闪存的VM)中查看Dictionary实现,但是当值由line map[ key ] = undefined;影响到map时,字典正在蜂拥而至map[ key ] = undefined; in the remove function. remove功能中。

IE you begin an iteration with a set of key but then a rehashed occured and the keys are not valid anymore and the VM can't find the previous key and so in this case miss the y key. 在IE中,您使用一组键开始迭代,但是随后重新哈希,并且键不再有效,并且VM无法找到先前的键,因此在这种情况下会丢失y键。

What you can do is remove the map[key] = undefined ; 您可以做的就是删除map[key] = undefined from the remove function and it should work. remove功能,它应该工作。 What it's strange is that the delete didn't produce any similar bug... 奇怪的是,删除操作没有产生任何类似的错误...

To show that the rehash has occurred see here a live example : http://wonderfl.net/c/2PZT 要显示已经进行了重新哈希处理,请参见此处的实时示例: http : //wonderfl.net/c/2PZT

You will see that a key is iterate twice when you assign a value to the dictionary. 当您为字典分配一个值时,您会看到一个键重复两次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM