如何增加HashTable中的值?

时间:2009-03-06 14:33:54

标签: c#

我有一个HashTable,我跟踪颜色(这是键)和颜色的数量,这是关键。

我试图弄清楚当HashTable已经包含颜色时如何增加键。这是一段代码:

Hashtable htColors = new Hashtable();

if (htColors.Contains(color))
{
    // Want to increase the "value" of the key here.       
}
else
{
    htColors.Add(color, 1); //Found color for first time
}

2 个答案:

答案 0 :(得分:5)

我发布这个是迂腐的。我不喜欢与Dictionary的接口,因为这种非常常见的访问需要花费 - 如果你最常见的情况是触摸已经存在的元素,你必须哈希并查找你的值3次。不相信我?我在这里写了DK的解决方案:

static void AddInc(Dictionary<string, int> dict, string s)
{
    if (dict.ContainsKey(s))
    {
        dict[s]++;
    }
    else
    {
        dict.Add(s, 1);
    }
}

当放入IL时 - 你得到这个:

L_0000: nop 
L_0001: ldarg.0 
L_0002: ldarg.1 
L_0003: callvirt instance bool [mscorlib]System.Collections.Generic.Dictionary`2<string, int32>::ContainsKey(!0)
L_0008: ldc.i4.0 
L_0009: ceq 
L_000b: stloc.0 
L_000c: ldloc.0 
L_000d: brtrue.s L_0028
L_000f: nop 
L_0010: ldarg.0 
L_0011: dup 
L_0012: stloc.1 
L_0013: ldarg.1 
L_0014: dup 
L_0015: stloc.2 
L_0016: ldloc.1 
L_0017: ldloc.2 
L_0018: callvirt instance !1 [mscorlib]System.Collections.Generic.Dictionary`2<string, int32>::get_Item(!0)
L_001d: ldc.i4.1 
L_001e: add 
L_001f: callvirt instance void [mscorlib]System.Collections.Generic.Dictionary`2<string, int32>::set_Item(!0, !1)
L_0024: nop 
L_0025: nop 
L_0026: br.s L_0033
L_0028: nop 
L_0029: ldarg.0 
L_002a: ldarg.1 
L_002b: ldc.i4.1 
L_002c: callvirt instance void [mscorlib]System.Collections.Generic.Dictionary`2<string, int32>::Add(!0, !1)
L_0031: nop 
L_0032: nop 
L_0033: ret

调用ContainsKey,get_item和set_item,所有这些都散列并查找。

我写了一些不太漂亮的东西,它使用了一个包含int的类,并且该类允许你对它产生副作用(由于struct复制语义,你不能真正使用结构而不会产生同样的惩罚)。

class IntegerHolder {
    public IntegerHolder(int x) { i = x; }
    public int i;
}
static void AddInc2(Dictionary<string, IntegerHolder> dict, string s)
{
    IntegerHolder holder = dict[s];
    if (holder != null)
    {
        holder.i++;
    }
    else
    {
        dict.Add(s, new IntegerHolder(1));
    }
}

这为您提供以下IL:

L_0000: nop 
L_0001: ldarg.0 
L_0002: ldarg.1 
L_0003: callvirt instance !1 [mscorlib]System.Collections.Generic.Dictionary`2<string, class AddableDictionary.IntegerHolder>::get_Item(!0)
L_0008: stloc.0 
L_0009: ldloc.0 
L_000a: ldnull 
L_000b: ceq 
L_000d: stloc.1 
L_000e: ldloc.1 
L_000f: brtrue.s L_0023
L_0011: nop 
L_0012: ldloc.0 
L_0013: dup 
L_0014: ldfld int32 AddableDictionary.IntegerHolder::i
L_0019: ldc.i4.1 
L_001a: add 
L_001b: stfld int32 AddableDictionary.IntegerHolder::i
L_0020: nop 
L_0021: br.s L_0033
L_0023: nop 
L_0024: ldarg.0 
L_0025: ldarg.1 
L_0026: ldc.i4.1 
L_0027: newobj instance void AddableDictionary.IntegerHolder::.ctor(int32)
L_002c: callvirt instance void [mscorlib]System.Collections.Generic.Dictionary`2<string, class AddableDictionary.IntegerHolder>::Add(!0, !1)
L_0031: nop 
L_0032: nop 
L_0033: ret 

调用get_item一次 - 在对象存在的情况下没有额外的散列。我有点肮脏,并将该字段公开,以避免方法调用属性访问。

如果是我,我会将这个整体功能包装到自己的类中,并将IntegerHolder类隐藏在公共视图中 - 这是一个限制版本:

public class CountableItem<T>
{
    private class IntegerHolder
    {
        public int i;
        public IntegerHolder() { i = 1; }
    }
    Dictionary<T, IntegerHolder> dict = new Dictionary<T, IntegerHolder>();

    public void Add(T key)
    {
        IntegerHolder val = dict[key];
        if (val != null)
            val.i++;
        else
            dict.Add(key, new IntegerHolder());
    }

    public void Clear()
    {
        dict.Clear();
    }

    public int Count(T key)
    {
        IntegerHolder val = dict[key];
        if (val != null)
            return val.i;
        return 0;
    }

    // TODO - write the IEnumerable accessor.
}

答案 1 :(得分:4)

尝试以下

if (htColors.Contains(color))
{
   int old = (int)htColors[color];
   htColor[color] = old + 1;
}

编辑对评论的回复

恕我直言,字典方法要好得多,因为它是1)类型安全和2)消除了此解决方案中涉及的拳击。

让行成为以下内容不会影响密钥,只会影响值

htColor[color] = (int)htColor[color] + 1;