Skip to content

Commit

Permalink
Tree generation
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorNicollet committed Jun 13, 2014
1 parent 17b906d commit 718e031
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions main.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
open Explore
open Tree

let () = ()
39 changes: 39 additions & 0 deletions tree.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
(* © 2014 RunOrg *)

module Map = BatMap
open Read

(** Generates a tree representation for a list of parsed files, based on their
[parent] property. *)

(** A tree, includes data structures for the relevant queries. *)
type t = {

(** Key is path, value is the file at that path. *)
all : (string, Read.t) Map.t ;

(** Key is path, value is the files that have that path as a parent. *)
children: (string option, Read.t list) Map.t ;

}

(** Create a tree from a source list. *)
let make files = {
all = List.fold_left (fun map file -> Map.add file.path file map) Map.empty files ;
children = List.fold_left (fun map file ->
let current = try Map.find file.parent map with Not_found -> [] in
Map.add file.parent (file :: current) map) Map.empty files ;
}

(** Grab the direct children of a file. *)
let children file t =
try Map.find (Some file.path) t.children with Not_found -> []

(** Grab all the ascendants of a file all up to a root, in reverse order,
excluding the file itself. *)
let rec ascendants file t =
match file.parent with None -> [] | Some id ->
let parent = try Map.find id t.all with Not_found ->
failwith (Printf.sprintf ("File '%s' references parent '%s' which does not exist") file.path id) in
parent :: ascendants parent t

0 comments on commit 718e031

Please sign in to comment.