Reputation: 123
I am using a hash table as a multi-mapping and I want to remove any of the previous bindings. The Hashtbl.remove just removes the current binding. I am thinking of something like remove hash x y which removes the binding of y to x. If there are more than one bindings x y it should remove one of them. I have some working code for this but it is too complicated and slow (includes for loops etc.)
I want it to work like this :
Hashtbl.find_all hash 1 ;; returns [1;2;3;3;4]
remove hash 1 3 ;;
Hashtbl.find_all hash 1 ;; returns [1;2;3;4]
Upvotes: 2
Views: 247
Reputation: 66823
I'd say this isn't really how Hashtbl
is designed to be used, so it's not going to be particularly convenient. There is no function in Hashtbl
for removing or adding multiple bindings of a key, so you'll have to add and remove them one at a time. Your iterative solution is probably about as good as you can expect.
It might be better to handle multiple bindings explicitly, i.e., to bind each key to an explicit (multi-)set of values. This will give better than linear behavior when making changes to the bindings.
Upvotes: 2