Skip to content

Instantly share code, notes, and snippets.

@c-cube
Last active December 18, 2015 14:19
Show Gist options
  • Save c-cube/5796351 to your computer and use it in GitHub Desktop.
Save c-cube/5796351 to your computer and use it in GitHub Desktop.
draft of ixSet for OCaml
class type ['a, 'b] index =
object
method name : string
method get : 'a -> 'b list
method add : 'a -> 'b -> ('a, 'b) index
method remove : 'a -> 'b -> ('a, 'b) index
method keys : ('a -> unit) -> unit
end
type 'a indexes = string Mixtbl.t
val idx_fun : ?cmp:('b -> 'b -> int) ->
name:string ->
('a -> 'b list) -> ('a, 'b) index
(** Index the elements by their image by the given function *)
val idx_set : ?cmp:('a -> 'a -> int) ->
name:string -> ('a, 'a) index
(** Just stores the elements in a Set *)
type 'a index_list =
| IL_cons : ('a, 'b) index * 'a index_list -> 'a index_list
| IL_nil : 'a index_list
type 'a t
(** The type for an indexed set *)
val mk_set : 'a index_list -> 'a t
(** Create a set indexed by the given list of indexes *)
val get_eq : 'a t -> ('a, 'b) index -> 'b -> 'a t
(** Select by predicate *)
val get_filter : 'a t -> ('a, 'b) index -> ('b -> bool) -> 'a t
(** Only select elements whose given index satisfies the predicate *)
val inter : 'a t -> 'a t -> 'a t
(** Set intersection *)
val union : 'a t -> 'a t -> 'a t
(** Set union *)
val add : 'a t -> 'a -> 'a t
(** Add an element to the set *)
val remove : 'a t -> 'a -> 'a t
(** Remove an element to the set *)
val group_by : 'a t -> ('a, 'b) index -> ('b * 'a list) list
(** Group by the given index *)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment