Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0005220OCamlstandard librarypublic2011-02-04 11:452016-12-09 09:23
ReporterChristoph Bauer 
Assigned To 
PrioritynormalSeverityfeatureReproducibilityhave not tried
PlatformOSOS Version
Product Version3.11.2 
Target VersionFixed in Version 
Summary0005220: Feature request: Hashtbl.adjust
DescriptionIf 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.
TagsNo tags attached.
Attached Files

- Relationships
related to 0007309resolved provides a `update` function for map 

-  Notes
Christoph Bauer (reporter)
2011-02-04 11:56

The type is

 Hashtbl.adjust : ('a, 'b) Hashtbl.t -> 'a -> ('b -> 'b) -> unit
Christoph Bauer (reporter)
2011-02-04 11:57

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 in
  let l = in <- replace_bucket l

- Issue History
Date Modified Username Field Change
2011-02-04 11:45 Christoph Bauer New Issue
2011-02-04 11:56 Christoph Bauer Note Added: 0005794
2011-02-04 11:57 Christoph Bauer Note Added: 0005795
2011-05-17 16:17 doligez Status new => acknowledged
2016-12-07 14:14 shinwell Category OCaml general => OCaml standard library
2016-12-09 09:23 frisch Relationship added related to 0007309
2017-02-23 16:43 doligez Category OCaml standard library => standard library

Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker