flex dictionary bug?

懵懂的女人 提交于 2019-12-24 10:22:37

问题


I was testing a HashMap implementation in AS3 .

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:

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. 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:

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

Does anyone know what's the reason the y key doesn't get deleted?


回答1:


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; in the remove function.

I.E. 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.

What you can do is remove the map[key] = undefined; from the remove function and it should work. 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

You will see that a key is iterate twice when you assign a value to the dictionary.



来源:https://stackoverflow.com/questions/8929128/flex-dictionary-bug

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!