diff options
| author | Graydon Hoare <[email protected]> | 2010-06-28 19:24:04 -0700 |
|---|---|---|
| committer | Graydon Hoare <[email protected]> | 2010-06-28 19:24:04 -0700 |
| commit | 329a65530fecdb59bbf06c6830cd766778b155b4 (patch) | |
| tree | 10a90eb2e441f6558cb375ea2c8c449f387bc1b6 /src/boot/util/common.ml | |
| parent | Whitespace. (diff) | |
| download | rust-329a65530fecdb59bbf06c6830cd766778b155b4.tar.xz rust-329a65530fecdb59bbf06c6830cd766778b155b4.zip | |
Canonicalize hashtables after running them through htab_map. Closes #77.
Diffstat (limited to 'src/boot/util/common.ml')
| -rw-r--r-- | src/boot/util/common.ml | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/src/boot/util/common.ml b/src/boot/util/common.ml index f33a6ea1..5c381b81 100644 --- a/src/boot/util/common.ml +++ b/src/boot/util/common.ml @@ -220,6 +220,21 @@ let htab_put (htab:('a,'b) Hashtbl.t) (a:'a) (b:'b) : unit = Hashtbl.add htab a b ;; +(* This is completely ridiculous, but it turns out that ocaml hashtables are + * order-of-element-addition sensitive when it comes to the built-in + * polymorphic comparison operator. So you have to canonicalize them after + * you've stopped adding things to them if you ever want to use them in a + * term that requires structural comparison to work. Sigh. + *) + +let htab_canonicalize (htab:('a,'b) Hashtbl.t) : ('a,'b) Hashtbl.t = + let n = Hashtbl.create (Hashtbl.length htab) in + Array.iter + (fun k -> Hashtbl.add n k (Hashtbl.find htab k)) + (sorted_htab_keys htab); + n +;; + let htab_map (htab:('a,'b) Hashtbl.t) (f:'a -> 'b -> ('c * 'd)) @@ -230,10 +245,9 @@ let htab_map htab_put ntab c d in Hashtbl.iter g htab; - ntab + htab_canonicalize (ntab) ;; - let htab_fold (fn:'a -> 'b -> 'c -> 'c) (init:'c) |