123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231open!Import(** The key is used for the hashtable of queue elements. *)moduletypeKey=Hashtbl.Key_plainmoduletypeS1=sigtype'keycreate_argtype'keycreate_key(** A hash-queue, where the values are of type ['data]. *)type('key,'data)t[@@derivingsexp_of]includeContainer.S1_phantom_invariantwithtype('data,'key)t:=('key,'data)t(** [invariant t] checks the invariants of the queue. *)valinvariant:('key,'data)t->unit(** [create ()] returns an empty queue. The arguments [growth_allowed] and [size] are
referring to the underlying hashtable.
@param growth_allowed defaults to true
@param size initial size -- default to 16
*)valcreate:?growth_allowed:bool->?size:int->'keycreate_arg->('keycreate_key,'data)t(** Clears the queue. *)valclear:('key,'data)t->unit(** Makes a fresh copy of the queue with identical contents to the original. *)valcopy:('key,'data)t->('key,'data)t(** {2 Finding elements} *)(** [mem q k] returns true iff there is some (k, v) in the queue. *)valmem:('key,'data)t->'key->bool(** [lookup t k] returns the value of the key-value pair in the queue with
key k, if there is one. *)vallookup:('key,'data)t->'key->'dataoptionvallookup_exn:('key,'data)t->'key->'data(** {2 Adding, removing, and replacing elements}
Note that even the non-[*_exn] versions can raise, but only if there is an ongoing
iteration. *)(** [enqueue t back_or_front k v] adds the key-value pair (k, v) to the front or back of
the queue, returning [`Ok] if the pair was added, or [`Key_already_present] if there
is already a (k, v') in the queue.
*)valenqueue:('key,'data)t->[`back|`front]->'key->'data->[`Ok|`Key_already_present](** Like {!enqueue}, but it raises in the [`Key_already_present] case *)valenqueue_exn:('key,'data)t->[`back|`front]->'key->'data->unit(** See {!enqueue}. [enqueue_back t k v] is the same as [enqueue t `back k v] *)valenqueue_back:('key,'data)t->'key->'data->[`Ok|`Key_already_present](** See {!enqueue_exn}. [enqueue_back_exn t k v] is the same as [enqueue_exn t `back k v] *)valenqueue_back_exn:('key,'data)t->'key->'data->unit(** See {!enqueue}. [enqueue_front t k v] is the same as [enqueue t `front k v] *)valenqueue_front:('key,'data)t->'key->'data->[`Ok|`Key_already_present](** See {!enqueue_exn}. [enqueue_front_exn t k v] is the same as [enqueue_exn t `front k
v] *)valenqueue_front_exn:('key,'data)t->'key->'data->unit(** [lookup_and_move_to_back] finds the key-value pair (k, v) and moves it to the
back of the queue if it exists, otherwise returning [None].
The [_exn] versions of these functions raise if key-value pair does not exist.
*)vallookup_and_move_to_back:('key,'data)t->'key->'dataoption(** Like {!lookup_and_move_to_back}, but raises instead of returning an option *)vallookup_and_move_to_back_exn:('key,'data)t->'key->'data(** Like {!lookup_and_move_to_back}, but moves element to the front of the queue *)vallookup_and_move_to_front:('key,'data)t->'key->'dataoption(** Like {!lookup_and_move_to_front}, but raises instead of returning an option *)vallookup_and_move_to_front_exn:('key,'data)t->'key->'data(** [last t] returns the last element of the queue, without removing it. *)vallast:('key,'data)t->'dataoption(** [last_with_key t] returns the last element of the queue and its key, without
removing it. *)vallast_with_key:('key,'data)t->('key*'data)option(** [first t] returns the front element of the queue, without removing it. *)valfirst:('key,'data)t->'dataoption(** [first_with_key t] returns the front element of the queue and its key, without
removing it. *)valfirst_with_key:('key,'data)t->('key*'data)option(** [keys t] returns the keys in the order of the queue. *)valkeys:('key,'data)t->'keylist(** [dequeue t front_or_back] returns the front or back element of the queue. *)valdequeue:('key,'data)t->[`back|`front]->'dataoption(** Like {!dequeue}, but it raises if the queue is empty. *)valdequeue_exn:('key,'data)t->[`back|`front]->'data(** [dequeue_back t] returns the back element of the queue. *)valdequeue_back:('key,'data)t->'dataoption(** Like {!dequeue_back}, but it raises if the queue is empty. *)valdequeue_back_exn:('key,'data)t->'data(** [dequeue_front t] returns the front element of the queue. *)valdequeue_front:('key,'data)t->'dataoption(** Like {!dequeue_front}, but it raises if the queue is empty. *)valdequeue_front_exn:('key,'data)t->'data(** [dequeue_with_key t] returns the front or back element of the queue and its key. *)valdequeue_with_key:('key,'data)t->[`back|`front]->('key*'data)option(** Like {!dequeue_with_key}, but it raises if the queue is empty. *)valdequeue_with_key_exn:('key,'data)t->[`back|`front]->'key*'data(** [dequeue_back_with_key t] returns the back element of the queue and its key. *)valdequeue_back_with_key:('key,'data)t->('key*'data)option(** Like {!dequeue_back_with_key}, but it raises if the queue is empty. *)valdequeue_back_with_key_exn:('key,'data)t->'key*'data(** [dequeue_front_with_key t] returns the front element of the queue and its key. *)valdequeue_front_with_key:('key,'data)t->('key*'data)option(** Like {!dequeue_front_with_key}, but it raises if the queue is empty. *)valdequeue_front_with_key_exn:('key,'data)t->'key*'data(** [dequeue_all t ~f] dequeues every element of the queue and applies [f] to each one.
The dequeue order is from front to back. *)valdequeue_all:('key,'data)t->f:('data->unit)->unit(** [remove q k] removes the key-value pair with key [k] from the queue. *)valremove:('key,'data)t->'key->[`Ok|`No_such_key]valremove_exn:('key,'data)t->'key->unit(** like {!remove}, but returns the removed element *)vallookup_and_remove:('key,'data)t->'key->'dataoption(** [replace q k v] changes the value of key [k] in the queue to [v]. *)valreplace:('key,'data)t->'key->'data->[`Ok|`No_such_key]valreplace_exn:('key,'data)t->'key->'data->unit(** [drop ?n q back_or_front] drops [n] elements (default 1) from the back or front of
the queue. If the queue has fewer than [n] elements then it is cleared. *)valdrop:?n:int->('key,'data)t->[`back|`front]->unit(** Equivalent to [drop ?n q `front]. *)valdrop_front:?n:int->('key,'data)t->unit(** Equivalent to [drop ?n q `back]. *)valdrop_back:?n:int->('key,'data)t->unit(** {2 Iterating over elements} *)(** [iter t ~f] applies [f] to each key and element of the queue. *)valiteri:('key,'data)t->f:(key:'key->data:'data->unit)->unitvalfoldi:('key,'data)t->init:'b->f:('b->key:'key->data:'data->'b)->'bendmoduletypeS0=sigtype('key,'data)hash_queuetypekeyincludeS1withtype'keycreate_key:=keywithtype'keycreate_arg:=unitwithtype('key,'data)t:=('key,'data)hash_queuetype'datat=(key,'data)hash_queue[@@derivingsexp_of]endmoduletypeS_backend=sigincludeS1withtype'keycreate_arg:='keyHashtbl.Hashable.twithtype'keycreate_key:='keymoduletypeS=S0withtype('key,'data)hash_queue:=('key,'data)tmoduleMake(Key:Key):Swithtypekey=Key.tmoduleMake_with_hashable(T:sigmoduleKey:Keyvalhashable:Key.tHashtbl.Hashable.tend):Swithtypekey=T.Key.tend(** A hash-queue is a combination of a queue and a hashtable that
supports constant-time lookup and removal of queue elements in addition to
the usual queue operations (enqueue, dequeue). The queue elements are
key-value pairs. The hashtable has one entry for each element of the queue.
Calls to functions that would modify a hash-queue (e.g. [enqueue], [dequeue],
[remove], [replace]) detect if a client is in the middle of iterating over the
queue (e.g., [iter], [fold], [for_all], [exists]) and if so, raise an exception.
*)moduletypeHash_queue=sigmoduletypeKey=KeymoduletypeS_backend=S_backendmoduleMake_backend(Table:Hashtbl_intf.Hashtbl):S_backend(** equivalent to [Make_backend (Hashtbl)] *)includeS_backendend