开发者

PowerCollections - MultiDictionary 'Remove' removing all values when value not found

I am using the powercollections library from http://powercollections.codeplex.com/

The first test passes, however the second test fails. I wouldn't expect Remove to remove all items from the dictionary if a value was not found.

Is this an issue or am I misunderstanding how Remove functions ?

[Test]
public void passing_test()
{
    var classes = new MultiDictionary<string, object>(false);
    classes.Add("class", "class_name1");
    classes.Add("class", "class_name2");
    classes.Remove("class", "class_name2");开发者_StackOverflow社区

    Assert.AreEqual(1, classes.Count);
}

[Test]
public void failing_test()
{
    var classes = new MultiDictionary<string, object>(false);
    classes.Add("class", "class_name1");
    classes.Remove("class", "class_name2");

    Assert.AreEqual(1, classes.Count);
}


It actually looks like a bug in the code:

        /// <summary>
        /// Removes a given value from the values associated with a key. If the
        /// last value is removed from a key, the key is removed also.
        /// </summary>
        /// <param name="key">A key to remove a value from.</param>
        /// <param name="value">The value to remove.</param>
        /// <returns>True if <paramref name="value"/> was associated with <paramref name="key"/> (and was
        /// therefore removed). False if <paramref name="value"/> was not associated with <paramref name="key"/>.</returns>
        public sealed override bool Remove(TKey key, TValue value)
        {
            KeyAndValues keyValues = new KeyAndValues(key);
            KeyAndValues existing;

            if (hash.Find(keyValues, false, out existing)) {
                // There is an item in the hash table equal to this key. Find the value.
                int existingCount = existing.Count;
                int valueHash = Util.GetHashCode(value, valueEqualityComparer);
                int indexFound = -1;
                for (int i = 0; i < existingCount; ++i) {
                    if (Util.GetHashCode(existing.Values[i], valueEqualityComparer) == valueHash &&
                        valueEqualityComparer.Equals(existing.Values[i], value)) 
                    {
                        // Found an equal existing value
                        indexFound = i;
                    }
                }

                if (existingCount == 1) {
                    // Removing the last value. Remove the key.
                    hash.Delete(existing, out keyValues);
                    return true;
                }

Notice how they are checking for existingCount == 1? Although the values you entered differ, there is no code to check whether the index was found in the search prior to the hash.Delete call. The correct way to fix this would be to do something like:

            if (existingCount == 1 && indexFound != -1) {
                // Removing the last value. Remove the key.
                hash.Delete(existing, out keyValues);
                return true;
            }

Here's the full source: http://powercollections.codeplex.com/SourceControl/changeset/view/6259#71508

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜