aboutsummaryrefslogtreecommitdiffstats
path: root/tools/ocaml/xenstored/trie.ml
blob: bc9a903582bcb3d083d5b35b729f365316eb8cf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
(*
 * Copyright (C) 2008-2009 Citrix Ltd.
 * Author Thomas Gazagnaire <thomas.gazagnaire@eu.citrix.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; version 2.1 only. with the special
 * exception on linking described in file LICENSE.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *)

module Node =
struct
	type ('a,'b) t =  {
		key: 'a;
		value: 'b option;
		children: ('a,'b) t list;
	}

	let create key value = {
		key = key;
		value = Some value;
		children = [];
	}

	let empty key = {
		key = key;
		value = None;
		children = []
	}

	let get_key node = node.key
	let get_value node = 
		match node.value with
		| None       -> raise Not_found
		| Some value -> value

	let get_children node = node.children

	let set_value node value =
		{ node with value = Some value }
	let set_children node children =
		{ node with children = children }

	let add_child node child = 
		{ node with children = child :: node.children }
end

type ('a,'b) t = ('a,'b) Node.t list

let mem_node nodes key =
	List.exists (fun n -> n.Node.key = key) nodes

let find_node nodes key =
	List.find (fun n -> n.Node.key = key) nodes

let replace_node nodes key node =
	let rec aux = function
		| []                            -> []
		| h :: tl when h.Node.key = key -> node :: tl
		| h :: tl                       -> h :: aux tl
	in
	aux nodes
			
let remove_node nodes key =
	let rec aux = function
		| []                            -> raise Not_found
		| h :: tl when h.Node.key = key -> tl
		| h :: tl                       -> h :: aux tl
	in
	aux nodes

let create () = []

let rec iter f tree = 
	let rec aux node =
		f node.Node.key node.Node.value; 
		iter f node.Node.children
	in
	List.iter aux tree

let rec map f tree =
	let rec aux node =
		let value = 
			match node.Node.value with
			| None       -> None
			| Some value -> f value
		in
		{ node with Node.value = value; Node.children = map f node.Node.children }
	in
	List.filter (fun n -> n.Node.value <> None || n.Node.children <> []) (List.map aux tree)

let rec fold f tree acc =
	let rec aux accu node =
		fold f node.Node.children (f node.Node.key node.Node.value accu)
	in
	List.fold_left aux acc tree 

(* return a sub-trie *)
let rec sub_node tree = function
	| []   -> raise Not_found
	| h::t -> 
		  if mem_node tree h
		  then begin
			  let node = find_node tree h in
			  if t = []
			  then node
			  else sub_node node.Node.children t
		  end else
			  raise Not_found

let sub tree path = 
	try (sub_node tree path).Node.children
	with Not_found -> []

let find tree path = 
	Node.get_value (sub_node tree path)

(* return false if the node doesn't exists or if it is not associated to any value *)
let rec mem tree = function
	| []   -> false
	| h::t -> 
		  mem_node tree h
		  && (let node = find_node tree h in 
			  if t = []
			  then node.Node.value <> None
			  else mem node.Node.children t)

(* Iterate over the longest valid prefix *)
let rec iter_path f tree = function
	| []   -> ()
	| h::l -> 
		  if mem_node tree h
		  then begin
			  let node = find_node tree h in
			  f node.Node.key node.Node.value;
			  iter_path f node.Node.children l
		  end

let rec set_node node path value =
	if path = [] 
	then Node.set_value node value
	else begin
		let children = set node.Node.children path value in
		Node.set_children node children
	end

and set tree path value =
	match path with
		| []   -> raise Not_found
		| h::t -> 
			  if mem_node tree h
			  then begin
				  let node = find_node tree h in
				  replace_node tree h (set_node node t value)
			  end else begin
				  let node = Node.empty h in
				  set_node node t value :: tree
			  end

let rec unset tree = function
	| []   -> tree
	| h::t -> 
		  if mem_node tree h
		  then begin
			  let node = find_node tree h in
			  let children = unset node.Node.children t in
			  let new_node =
				  if t = []
				  then Node.set_children (Node.empty h) children
				  else Node.set_children node children
			  in
			  if children = [] && new_node.Node.value = None
			  then remove_node tree h
			  else replace_node tree h new_node
		  end else
			  raise Not_found