Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0005220OCamlOCaml generalpublic2011-02-04 11:452011-05-17 16:17
ReporterChristoph Bauer 
Assigned To 
PrioritynormalSeverityfeatureReproducibilityhave not tried
StatusacknowledgedResolutionopen 
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

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

The type is

 Hashtbl.adjust : ('a, 'b) Hashtbl.t -> 'a -> ('b -> 'b) -> unit
(0005795)
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 h.data) in
  let l = h.data.(i) in
  h.data.(i) <- 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


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker