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

Feature request: Hashtbl.adjust #5220

Closed
vicuna opened this issue Feb 4, 2011 · 4 comments
Closed

Feature request: Hashtbl.adjust #5220

vicuna opened this issue Feb 4, 2011 · 4 comments

Comments

@vicuna
Copy link

vicuna commented Feb 4, 2011

Original bug ID: 5220
Reporter: Christoph Bauer
Status: acknowledged (set by @damiendoligez on 2011-05-17T14:17:55Z)
Resolution: open
Priority: normal
Severity: feature
Version: 3.11.2
Category: standard library
Related to: #7309
Monitored by: "Pascal Cuoq" "Julien Signoles" @hcarty

Bug description

If you have a Hashtable of counters and you want to increment one, you have to do it that way:

Hashtbl.replace t key (1+Hashtbl.find t key)

There could be a more efficient way to do this with a helper function

Hashtbl.adjust t key f

with the type

Hashtbl.adjust : ('a, 'b) Hashtbl.t -> 'a -> ('b -> 'b)

Hashtbl.adjust looks up the key in t and replaces it associated value b with (f b). It might raise Not_found.

I guess it's not possible to implement this function in BatHashtbl.

@vicuna
Copy link
Author

vicuna commented Feb 4, 2011

Comment author: Christoph Bauer

The type is

Hashtbl.adjust : ('a, 'b) Hashtbl.t -> 'a -> ('b -> 'b) -> unit

@vicuna
Copy link
Author

vicuna commented Feb 4, 2011

Comment author: Christoph Bauer

Here an implementation:

let adjust h key f =
let rec replace_bucket = function
Empty ->
raise Not_found
| Cons(k, i, next) ->
if compare k key = 0
then Cons(k, f i, next)
else Cons(k, i, replace_bucket next) in
let i = (hash key) mod (Array.length h.data) in
let l = h.data.(i) in
h.data.(i) <- replace_bucket l

@github-actions
Copy link

This issue has been open one year with no activity. Consequently, it is being marked with the "stale" label. What this means is that the issue will be automatically closed in 30 days unless more comments are added or the "stale" label is removed. Comments that provide new information on the issue are especially welcome: is it still reproducible? did it appear in other contexts? how critical is it? etc.

@gasche
Copy link
Member

gasche commented Jun 24, 2020

This is a sensible feature request, but several libraries out there call it modify, so maybe there is some naming study to do. In any case, someone interested should consider just opening a Pull Request with a proposed implementation.

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