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

Add filename sets #9040

Merged
merged 2 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions otherlibs/stdune/src/filename_set.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
(* CR-someday amokhov: Switch from sets to "flat sets" backed by immutable arrays. *)
type t =
{ dir : Path.t
; filenames : Filename.Set.t
}

let equal t { dir; filenames } =
Path.equal t.dir dir && Filename.Set.equal filenames t.filenames
;;

let dir { dir; filenames = _ } = dir
let filenames { dir = _; filenames } = filenames
let empty ~dir = { dir; filenames = String.Set.empty }

let create ?filter ~dir filenames =
match filter with
| None -> { dir; filenames }
| Some f ->
{ dir
; filenames =
Filename.Set.to_list filenames
|> List.filter ~f:(fun basename -> f ~basename)
|> Filename.Set.of_list
}
;;

let to_list { dir; filenames } = Filename.Set.to_list_map filenames ~f:(Path.relative dir)
19 changes: 19 additions & 0 deletions otherlibs/stdune/src/filename_set.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(** Like [Path.Set.t] but tailored for representing sets of file names in the same parent
directory. Compared to [Path.Set.t], [Filename_set.t] statically enforces an important
invariant, and can also be processed more efficiently. *)

type t

val equal : t -> t -> bool

(** The directory of the filename set. *)
val dir : t -> Path.t

(** The set of file names, all relative to [dir]. *)
val filenames : t -> Filename.Set.t

val empty : dir:Path.t -> t

(* CR-soon amokhov: Decouple [create] from [filter]. *)
val create : ?filter:(basename:string -> bool) -> dir:Path.t -> Filename.Set.t -> t
val to_list : t -> Path.t list
Loading