Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hashtbl.arbitrary #4103

Closed
vicuna opened this issue Sep 7, 2006 · 2 comments
Closed

Hashtbl.arbitrary #4103

vicuna opened this issue Sep 7, 2006 · 2 comments

Comments

@vicuna
Copy link

vicuna commented Sep 7, 2006

Original bug ID: 4103
Reporter: acone
Assigned to: @alainfrisch
Status: resolved (set by @alainfrisch on 2016-12-07T15:50:38Z)
Resolution: won't fix
Priority: normal
Severity: feature
Version: 3.09.2
Category: standard library
Tags: patch

Bug description

Hashtbl.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 information

The 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

@vicuna
Copy link
Author

vicuna commented Sep 7, 2006

Comment author: acone

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

@vicuna
Copy link
Author

vicuna commented Dec 7, 2016

Comment author: @alainfrisch

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".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants