Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0004103OCamlstandard librarypublic2006-09-07 19:192016-12-07 16:50
Reporteracone 
Assigned Tofrisch 
PrioritynormalSeverityfeatureReproducibilityN/A
StatusresolvedResolutionwon't fix 
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)
    else
      (maxkey, maxval)
  in
    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 = h.data in
  let len = Array.length d
  let rec loop i =
    if i == len then
      raise Not_found
    else
      (match d.(i) with
         Empty -> loop(i+1)
       | Cons(key, val, _) -> (key, val))
  in
    loop 0
Tagspatch
Attached Files

- Relationships

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

I accidentally labeled this as caml-light. It concerns caml genereally.
(0016732)
frisch (developer)
2016-12-07 16:50

No activity for 10 years. The requested behavior does not seem very common and can be achieved with the current API (Hashtbl.iter + an exception to stop the traversal after the first element). So marking as "won't fix".

- 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
2016-12-07 14:43 shinwell Category OCaml general => OCaml standard library
2016-12-07 16:50 frisch Note Added: 0016732
2016-12-07 16:50 frisch Status acknowledged => resolved
2016-12-07 16:50 frisch Resolution open => won't fix
2016-12-07 16:50 frisch Assigned To => frisch
2017-02-23 16:43 doligez Category OCaml standard library => standard library


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker