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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
use crate::{
	flattening::NodeMap, object, ExpandedDocument, FlattenedDocument, Id, Indexed, IndexedNode,
	IndexedObject, Node, Object, StrippedIndexedNode, StrippedIndexedObject,
};
use locspan::Meta;
use smallvec::SmallVec;
use std::hash::Hash;

/// JSON-LD Quad.
///
/// This is different from an RDF Quad since the object (last element) is a JSON-LD object.
/// A JSON-LD Quad can correspond to multiple RDF Quads.
pub struct QuadRef<'a, T, B, M>(
	pub Option<&'a Id<T, B>>,
	pub &'a Id<T, B>,
	pub PropertyRef<'a, T, B>,
	pub ObjectRef<'a, T, B, M>,
);

pub enum PropertyRef<'a, T, B> {
	Type,
	Ref(&'a Id<T, B>),
}

pub enum ObjectRef<'a, T, B, M> {
	Object(&'a Object<T, B, M>),
	Node(&'a Node<T, B, M>),
	Ref(&'a Id<T, B>),
}

pub trait LdQuads<T, B, M> {
	fn quads(&self) -> Quads<T, B, M>;
}

impl<T, B, M> LdQuads<T, B, M> for ExpandedDocument<T, B, M> {
	fn quads(&self) -> Quads<T, B, M> {
		let mut stack = SmallVec::new();
		stack.push(QuadsFrame::IndexedObjectSet(None, self.iter()));
		Quads { stack }
	}
}

impl<T, B, M> LdQuads<T, B, M> for FlattenedDocument<T, B, M> {
	fn quads(&self) -> Quads<T, B, M> {
		let mut stack = SmallVec::new();
		stack.push(QuadsFrame::IndexedNodeSlice(None, self.iter()));
		Quads { stack }
	}
}

impl<T: Eq + Hash, B: Eq + Hash, M> LdQuads<T, B, M> for NodeMap<T, B, M> {
	fn quads(&self) -> Quads<T, B, M> {
		let mut stack = SmallVec::new();

		for (id, graph) in self {
			stack.push(QuadsFrame::NodeMapGraph(id, graph.nodes()));
		}

		Quads { stack }
	}
}

const STACK_LEN: usize = 6;

pub struct Quads<'a, T, B, M> {
	stack: SmallVec<[QuadsFrame<'a, T, B, M>; STACK_LEN]>,
}

enum QuadsFrame<'a, T, B, M> {
	NodeMapGraph(
		Option<Meta<&'a Id<T, B>, &'a M>>,
		crate::flattening::NodeMapGraphNodes<'a, T, B, M>,
	),
	IndexedObjectSet(
		Option<Meta<&'a Id<T, B>, &'a M>>,
		std::collections::hash_set::Iter<'a, StrippedIndexedObject<T, B, M>>,
	),
	IndexedNodeSet(
		Option<Meta<&'a Id<T, B>, &'a M>>,
		std::collections::hash_set::Iter<'a, StrippedIndexedNode<T, B, M>>,
	),
	IndexedObjectSlice(
		Option<Meta<&'a Id<T, B>, &'a M>>,
		std::slice::Iter<'a, IndexedObject<T, B, M>>,
	),
	IndexedNodeSlice(
		Option<Meta<&'a Id<T, B>, &'a M>>,
		std::slice::Iter<'a, IndexedNode<T, B, M>>,
	),
	NodeTypes(
		Option<Meta<&'a Id<T, B>, &'a M>>,
		&'a Id<T, B>,
		std::slice::Iter<'a, Meta<Id<T, B>, M>>,
	),
	NodeProperties(
		Option<Meta<&'a Id<T, B>, &'a M>>,
		&'a Id<T, B>,
		object::node::properties::Iter<'a, T, B, M>,
	),
	NodeReverseProperties(
		Option<Meta<&'a Id<T, B>, &'a M>>,
		&'a Node<T, B, M>,
		object::node::reverse_properties::Iter<'a, T, B, M>,
	),
	NodePropertyObjects(
		Option<Meta<&'a Id<T, B>, &'a M>>,
		&'a Id<T, B>,
		&'a Id<T, B>,
		std::slice::Iter<'a, StrippedIndexedObject<T, B, M>>,
	),
	NodeReversePropertySubjects(
		Option<Meta<&'a Id<T, B>, &'a M>>,
		&'a Node<T, B, M>,
		&'a Id<T, B>,
		std::slice::Iter<'a, StrippedIndexedNode<T, B, M>>,
	),
}

impl<'a, T, B, M> Quads<'a, T, B, M> {
	fn push_object(
		&mut self,
		graph: Option<Meta<&'a Id<T, B>, &'a M>>,
		object: &'a Indexed<Object<T, B, M>, M>,
	) {
		match object.inner() {
			Object::Node(node) => self.push_node(graph, node),
			Object::List(objects) => self
				.stack
				.push(QuadsFrame::IndexedObjectSlice(graph, objects.iter())),
			Object::Value(_) => (),
		}
	}

	fn push_node(&mut self, graph: Option<Meta<&'a Id<T, B>, &'a M>>, node: &'a Node<T, B, M>) {
		if let Some(id) = node.id_entry() {
			if let Some(graph) = node.graph_entry() {
				self.stack.push(QuadsFrame::IndexedObjectSet(
					Some(id.value.borrow()),
					graph.iter(),
				))
			}

			if let Some(included) = node.included_entry() {
				self.stack
					.push(QuadsFrame::IndexedNodeSet(graph, included.iter()))
			}

			if let Some(reverse_properties) = node.reverse_properties_entry() {
				self.stack.push(QuadsFrame::NodeReverseProperties(
					graph,
					node,
					reverse_properties.iter(),
				));
			}

			self.stack.push(QuadsFrame::NodeProperties(
				graph,
				id,
				node.properties().iter(),
			));

			if let Some(types) = node.type_entry() {
				self.stack
					.push(QuadsFrame::NodeTypes(graph, id, types.iter()));
			}
		}
	}
}

impl<'a, T, B, M> Iterator for Quads<'a, T, B, M> {
	type Item = QuadRef<'a, T, B, M>;

	fn next(&mut self) -> Option<Self::Item> {
		while let Some(last) = self.stack.last_mut() {
			match last {
				QuadsFrame::NodeMapGraph(graph, nodes) => {
					let graph = *graph;
					match nodes.next() {
						Some(node) => self.push_node(graph, node),
						None => {
							self.stack.pop();
						}
					}
				}
				QuadsFrame::IndexedObjectSet(graph, objects) => {
					let graph = *graph;
					match objects.next() {
						Some(object) => self.push_object(graph, object),
						None => {
							self.stack.pop();
						}
					}
				}
				QuadsFrame::IndexedNodeSet(graph, nodes) => {
					let graph = *graph;
					match nodes.next() {
						Some(node) => self.push_node(graph, node),
						None => {
							self.stack.pop();
						}
					}
				}
				QuadsFrame::IndexedObjectSlice(graph, objects) => {
					let graph = *graph;
					match objects.next() {
						Some(object) => self.push_object(graph, object),
						None => {
							self.stack.pop();
						}
					}
				}
				QuadsFrame::IndexedNodeSlice(graph, nodes) => {
					let graph = *graph;
					match nodes.next() {
						Some(node) => self.push_node(graph, node),
						None => {
							self.stack.pop();
						}
					}
				}
				QuadsFrame::NodeTypes(graph, subject, types) => {
					let (graph, subject) = (*graph, *subject);
					match types.next() {
						Some(ty) => {
							return Some(QuadRef(
								graph.map(Meta::into_value),
								subject,
								PropertyRef::Type,
								ObjectRef::<T, B, M>::Ref(ty),
							))
						}
						None => {
							self.stack.pop();
						}
					}
				}
				QuadsFrame::NodeProperties(graph, subject, properties) => {
					let (graph, subject) = (*graph, *subject);
					match properties.next() {
						Some((property, objects)) => {
							self.stack.push(QuadsFrame::NodePropertyObjects(
								graph,
								subject,
								property.0,
								objects.iter(),
							))
						}
						None => {
							self.stack.pop();
						}
					}
				}
				QuadsFrame::NodeReverseProperties(graph, object, reverse_properties) => {
					let (graph, object) = (*graph, *object);
					match reverse_properties.next() {
						Some((property, subjects)) => {
							self.stack.push(QuadsFrame::NodeReversePropertySubjects(
								graph,
								object,
								property.0,
								subjects.iter(),
							))
						}
						None => {
							self.stack.pop();
						}
					}
				}
				QuadsFrame::NodePropertyObjects(graph, subject, property, objects) => {
					let (graph, subject, property) = (*graph, *subject, *property);
					match objects.next() {
						Some(object) => {
							self.push_object(graph, object);
							return Some(QuadRef(
								graph.map(Meta::into_value),
								subject,
								PropertyRef::Ref(property),
								ObjectRef::Object(object),
							));
						}
						None => {
							self.stack.pop();
						}
					}
				}
				QuadsFrame::NodeReversePropertySubjects(graph, object, property, subjects) => {
					let (graph, object, property) = (*graph, *object, *property);
					match subjects.next() {
						Some(subject) => {
							self.push_node(graph, subject.inner());
							if let Some(id) = subject.id_entry() {
								return Some(QuadRef(
									graph.map(Meta::into_value),
									id,
									PropertyRef::Ref(property),
									ObjectRef::Node(object),
								));
							}
						}
						None => {
							self.stack.pop();
						}
					}
				}
			}
		}

		None
	}
}