0

why when table.Count = 1 table iteration functions get more than 1 key-value pairs? And if this is feature how can I expected behavior?

//
            let table = new HashMultiMap<string, int> (HashIdentity.Structural)
            table.Add("a", 1)
            table.Add("a", 1)
            table.Add("a", 1)
            // now table.Count is one
            let reduced = table.Fold (fun k v acc -> (k,v) :: acc) [] 
            let acc = ref []
            table.Iterate (fun k v -> acc.contents <- (k,v) :: acc.contents)
            //  

reduced and acc.contents contains three ("a",1) pairs

RomanKovalev
  • 852
  • 3
  • 10
  • 29

1 Answers1

1

The documentation for the Count member says:

The total number of keys in the hash table

If you have multiple values associated with the same key, there is still only a single key, so the value 1 is the expected result for your example. As far as I can see, there is no property that returns the total number of values, but you can implement that easily using Fold:

let valueCount = table.Fold (fun _ _ n -> n + 1) 0
Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
  • I had hoped that one key can be associated with only one value. So, this is normal behavior when a key is actually a lot of bindings? – RomanKovalev Mar 23 '12 at 12:35
  • 2
    @psct If you're using `HashMultiMap` than one key can be associated with multiple values (that's why it's called `Multi`). If you want to associate one value with a key, then you can use normal F# immutable `Map` or mutable .NET `Dictionary`. – Tomas Petricek Mar 23 '12 at 13:06