Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0004103OCamlOCaml generalpublic2006-09-07 19:192013-09-04 17:15
Assigned To 
PlatformOSOS Version
Product Version3.09.2 
Target VersionFixed in Version 
Summary0004103: Hashtbl.arbitrary
DescriptionHashtbl.arbitrary : ('a, 'b) Hashtbl.t -> ('a * 'b)

Returns an arbitrary (i.e. quickest to access) key-value pair in a hashtable, or raises Not_found if the hashtable is empty.

This would be useful for starting a function that finds the key with the maximum value:

let hashmax gt t =
  let maxfolder key value (maxkey, maxval) =
    if gt key maxkey then
      (key, value)
      (maxkey, maxval)
    let startpair = Hashtbl.arbitrary t in
      Hashtbl.fold maxfolder t startpair

Additional InformationThe code for such a function might look like:

let arbitrary h =
  let d = in
  let len = Array.length d
  let rec loop i =
    if i == len then
      raise Not_found
      (match d.(i) with
         Empty -> loop(i+1)
       | Cons(key, val, _) -> (key, val))
    loop 0
Attached Files

- Relationships

-  Notes
acone (reporter)
2006-09-07 19:20

I accidentally labeled this as caml-light. It concerns caml genereally.

- Issue History
Date Modified Username Field Change
2006-09-07 19:19 acone New Issue
2006-09-07 19:20 acone Note Added: 0003766
2006-09-08 10:20 ertai Category Caml-light => OCaml general
2006-09-11 11:20 doligez Status new => acknowledged
2013-09-04 17:15 doligez Tag Attached: patch

Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker