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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
use super::{Multiset, Objects};
use crate::{
	object::{InvalidExpandedJson, TryFromJson, TryFromJsonObject},
	Id, IndexedObject, StrippedIndexedObject,
};
use derivative::Derivative;
use hashbrown::HashMap;
use locspan::{BorrowStripped, Meta, Stripped};
use locspan_derive::{StrippedEq, StrippedHash, StrippedPartialEq};
use rdf_types::VocabularyMut;
use std::{
	hash::{Hash, Hasher},
	ops,
};

/// Property set entry.
#[derive(Derivative, Clone, PartialEq, Eq, Hash, StrippedPartialEq, StrippedEq, StrippedHash)]
#[locspan(ignore(M))]
pub struct Entry<T, M> {
	/// Property key metadata.
	#[locspan(ignore)]
	pub key_metadata: M,

	/// Property value.
	pub value: T,
}

impl<T, M> Entry<T, M> {
	pub fn new(key_metadata: M, value: T) -> Self {
		Self {
			key_metadata,
			value,
		}
	}

	pub fn borrow(&self) -> Entry<&T, &M> {
		Entry {
			key_metadata: &self.key_metadata,
			value: &self.value,
		}
	}

	pub fn map<U>(self, f: impl FnOnce(T) -> U) -> Entry<U, M> {
		Entry {
			key_metadata: self.key_metadata,
			value: f(self.value),
		}
	}
}

impl<T, M> ops::Deref for Entry<T, M> {
	type Target = T;

	fn deref(&self) -> &Self::Target {
		&self.value
	}
}

impl<T, M> ops::DerefMut for Entry<T, M> {
	fn deref_mut(&mut self) -> &mut Self::Target {
		&mut self.value
	}
}

pub type PropertyObjects<T, B, M> = Multiset<StrippedIndexedObject<T, B, M>>;

/// Properties of a node object, and their associated objects.
#[derive(Derivative, Clone)]
#[derivative(
	PartialEq(bound = "T: Eq + Hash, B: Eq + Hash, M: PartialEq"),
	Eq(bound = "T: Eq + Hash, B: Eq + Hash, M: Eq")
)]
pub struct Properties<T, B, M = ()>(HashMap<Id<T, B>, PropertyEntry<T, B, M>>);

impl<T, B, M> Properties<T, B, M> {
	/// Creates an empty map.
	pub(crate) fn new() -> Self {
		Self(HashMap::new())
	}

	/// Returns the number of properties.
	#[inline(always)]
	pub fn len(&self) -> usize {
		self.0.len()
	}

	/// Checks if there are no defined properties.
	#[inline(always)]
	pub fn is_empty(&self) -> bool {
		self.0.is_empty()
	}

	/// Returns an iterator over the properties and their associated objects.
	#[inline(always)]
	pub fn iter(&self) -> Iter<'_, T, B, M> {
		Iter {
			inner: self.0.iter(),
		}
	}

	/// Returns an iterator over the properties with a mutable reference to their associated objects.
	#[inline(always)]
	pub fn iter_mut(&mut self) -> IterMut<'_, T, B, M> {
		IterMut {
			inner: self.0.iter_mut(),
		}
	}

	/// Removes all properties.
	#[inline(always)]
	pub fn clear(&mut self) {
		self.0.clear()
	}
}

impl<T: Eq + Hash, B: Eq + Hash, M> Properties<T, B, M> {
	/// Checks if the given property is associated to any object.
	#[inline(always)]
	pub fn contains<Q: ?Sized + Hash + hashbrown::Equivalent<Id<T, B>>>(&self, prop: &Q) -> bool {
		self.0.get(prop).is_some()
	}

	/// Returns an iterator over all the objects associated to the given property.
	#[inline(always)]
	pub fn get<Q: ?Sized + Hash + hashbrown::Equivalent<Id<T, B>>>(
		&self,
		prop: &Q,
	) -> Objects<T, B, M> {
		match self.0.get(prop) {
			Some(values) => Objects::new(Some(values.iter())),
			None => Objects::new(None),
		}
	}

	/// Get one of the objects associated to the given property.
	///
	/// If multiple objects are found, there are no guaranties on which object will be returned.
	#[inline(always)]
	pub fn get_any<Q: ?Sized + Hash + hashbrown::Equivalent<Id<T, B>>>(
		&self,
		prop: &Q,
	) -> Option<&IndexedObject<T, B, M>> {
		match self.0.get(prop) {
			Some(values) => values.iter().next().map(|o| &o.0),
			None => None,
		}
	}

	/// Associate the given object to the node through the given property.
	#[inline(always)]
	pub fn insert(&mut self, Meta(prop, meta): Meta<Id<T, B>, M>, value: IndexedObject<T, B, M>) {
		if let Some(node_values) = self.0.get_mut(&prop) {
			node_values.insert(Stripped(value));
		} else {
			self.0
				.insert(prop, Entry::new(meta, Multiset::singleton(Stripped(value))));
		}
	}

	/// Associate the given object to the node through the given property, unless it is already.
	#[inline(always)]
	pub fn insert_unique(
		&mut self,
		Meta(prop, meta): Meta<Id<T, B>, M>,
		value: IndexedObject<T, B, M>,
	) {
		if let Some(node_values) = self.0.get_mut(&prop) {
			if node_values.iter().all(|v| !v.equivalent(&value)) {
				node_values.insert(Stripped(value))
			}
		} else {
			self.0
				.insert(prop, Entry::new(meta, Multiset::singleton(Stripped(value))));
		}
	}

	/// Associate all the given objects to the node through the given property.
	#[inline(always)]
	pub fn insert_all<Objects: IntoIterator<Item = IndexedObject<T, B, M>>>(
		&mut self,
		Meta(prop, meta): Meta<Id<T, B>, M>,
		values: Objects,
	) {
		if let Some(node_values) = self.0.get_mut(&prop) {
			node_values.extend(values.into_iter().map(Stripped));
		} else {
			self.0.insert(
				prop,
				Entry::new(meta, values.into_iter().map(Stripped).collect()),
			);
		}
	}

	/// Associate all the given objects to the node through the given property, unless it is already.
	///
	/// The [equivalence operator](crate::Object::equivalent) is used to remove equivalent objects.
	#[inline(always)]
	pub fn insert_all_unique_stripped<
		Objects: IntoIterator<Item = StrippedIndexedObject<T, B, M>>,
	>(
		&mut self,
		Meta(prop, meta): Meta<Id<T, B>, M>,
		values: Objects,
	) {
		if let Some(node_values) = self.0.get_mut(&prop) {
			for value in values {
				if node_values.iter().all(|v| !v.equivalent(&value)) {
					node_values.insert(value)
				}
			}
		} else {
			let values = values.into_iter();
			let mut node_values: PropertyObjects<T, B, M> =
				Multiset::with_capacity(values.size_hint().0);
			for value in values {
				if node_values.iter().all(|v| !v.equivalent(&value)) {
					node_values.insert(value)
				}
			}

			self.0.insert(prop, Entry::new(meta, node_values));
		}
	}

	/// Associate all the given objects to the node through the given property, unless it is already.
	///
	/// The [equivalence operator](crate::Object::equivalent) is used to remove equivalent objects.
	#[inline(always)]
	pub fn insert_all_unique<Objects: IntoIterator<Item = IndexedObject<T, B, M>>>(
		&mut self,
		prop: Meta<Id<T, B>, M>,
		values: Objects,
	) {
		self.insert_all_unique_stripped(prop, values.into_iter().map(Stripped))
	}

	pub fn extend_unique<I, O>(&mut self, iter: I)
	where
		I: IntoIterator<Item = (Meta<Id<T, B>, M>, O)>,
		O: IntoIterator<Item = IndexedObject<T, B, M>>,
	{
		for (prop, values) in iter {
			self.insert_all_unique(prop, values)
		}
	}

	pub fn extend_unique_stripped<I, O>(&mut self, iter: I)
	where
		I: IntoIterator<Item = (Meta<Id<T, B>, M>, O)>,
		O: IntoIterator<Item = StrippedIndexedObject<T, B, M>>,
	{
		for (prop, values) in iter {
			self.insert_all_unique_stripped(prop, values)
		}
	}

	/// Removes and returns all the values associated to the given property.
	#[inline(always)]
	pub fn remove(&mut self, prop: &Id<T, B>) -> Option<PropertyEntry<T, B, M>> {
		self.0.remove(prop)
	}
}

impl<T: Eq + Hash, B: Eq + Hash, M> TryFromJson<T, B, M> for Properties<T, B, M> {
	fn try_from_json_in(
		vocabulary: &mut impl VocabularyMut<Iri = T, BlankId = B>,
		Meta(value, meta): Meta<json_syntax::Value<M>, M>,
	) -> Result<Meta<Self, M>, Meta<InvalidExpandedJson<M>, M>> {
		match value {
			json_syntax::Value::Object(object) => {
				Self::try_from_json_object_in(vocabulary, Meta(object, meta))
			}
			_ => Err(Meta(InvalidExpandedJson::InvalidObject, meta)),
		}
	}
}

impl<T: Eq + Hash, B: Eq + Hash, M> TryFromJsonObject<T, B, M> for Properties<T, B, M> {
	fn try_from_json_object_in(
		vocabulary: &mut impl VocabularyMut<Iri = T, BlankId = B>,
		Meta(object, meta): Meta<json_syntax::Object<M>, M>,
	) -> Result<Meta<Self, M>, Meta<InvalidExpandedJson<M>, M>> {
		let mut result = Self::new();

		for entry in object {
			let Meta(key, key_meta) = entry.key;
			let prop = Id::from_string_in(vocabulary, key.to_string());
			let objects: Vec<IndexedObject<T, B, M>> =
				Vec::try_from_json_in(vocabulary, entry.value)?.into_value();
			result.insert_all(Meta(prop, key_meta), objects)
		}

		Ok(Meta(result, meta))
	}
}

impl<T: Eq + Hash, B: Eq + Hash, M> locspan::StrippedPartialEq for Properties<T, B, M> {
	#[inline(always)]
	fn stripped_eq(&self, other: &Self) -> bool {
		self.0.stripped() == other.0.stripped()
	}
}

impl<T: Eq + Hash, B: Eq + Hash, M> locspan::StrippedEq for Properties<T, B, M> {}

impl<T: Hash, B: Hash, M> locspan::StrippedHash for Properties<T, B, M> {
	#[inline(always)]
	fn stripped_hash<H: Hasher>(&self, h: &mut H) {
		crate::utils::hash_map_stripped(&self.0, h)
	}
}

impl<T: Hash, B: Hash, M: Hash> Hash for Properties<T, B, M> {
	#[inline(always)]
	fn hash<H: Hasher>(&self, h: &mut H) {
		crate::utils::hash_map(&self.0, h)
	}
}

impl<T: Eq + Hash, B: Eq + Hash, M> Extend<(Meta<Id<T, B>, M>, Vec<IndexedObject<T, B, M>>)>
	for Properties<T, B, M>
{
	fn extend<I>(&mut self, iter: I)
	where
		I: IntoIterator<Item = (Meta<Id<T, B>, M>, Vec<IndexedObject<T, B, M>>)>,
	{
		for (prop, values) in iter {
			self.insert_all(prop, values)
		}
	}
}

/// Tuple type representing a binding in a node object,
/// associating a property to some objects.
pub type Binding<T, B, M> = (Meta<Id<T, B>, M>, PropertyObjects<T, B, M>);

/// Tuple type representing a reference to a binding in a node object,
/// associating a property to some objects.
pub type BindingRef<'a, T, B, M> = (
	Meta<&'a Id<T, B>, &'a M>,
	&'a [StrippedIndexedObject<T, B, M>],
);

/// Tuple type representing a mutable reference to a binding in a node object,
/// associating a property to some objects, with a mutable access to the objects.
pub type BindingMut<'a, T, B, M> = (
	Meta<&'a Id<T, B>, &'a mut M>,
	&'a mut PropertyObjects<T, B, M>,
);

impl<T, B, M> IntoIterator for Properties<T, B, M> {
	type Item = Binding<T, B, M>;
	type IntoIter = IntoIter<T, B, M>;

	#[inline(always)]
	fn into_iter(self) -> Self::IntoIter {
		IntoIter {
			inner: self.0.into_iter(),
		}
	}
}

impl<'a, T, B, M> IntoIterator for &'a Properties<T, B, M> {
	type Item = BindingRef<'a, T, B, M>;
	type IntoIter = Iter<'a, T, B, M>;

	#[inline(always)]
	fn into_iter(self) -> Self::IntoIter {
		self.iter()
	}
}

impl<'a, T, B, M> IntoIterator for &'a mut Properties<T, B, M> {
	type Item = BindingMut<'a, T, B, M>;
	type IntoIter = IterMut<'a, T, B, M>;

	#[inline(always)]
	fn into_iter(self) -> Self::IntoIter {
		self.iter_mut()
	}
}

/// Iterator over the properties of a node.
///
/// It is created by the [`Properties::into_iter`] function.
pub struct IntoIter<T, B, M> {
	inner: hashbrown::hash_map::IntoIter<Id<T, B>, PropertyEntry<T, B, M>>,
}

impl<T, B, M> Iterator for IntoIter<T, B, M> {
	type Item = Binding<T, B, M>;

	#[inline(always)]
	fn size_hint(&self) -> (usize, Option<usize>) {
		self.inner.size_hint()
	}

	#[inline(always)]
	fn next(&mut self) -> Option<Self::Item> {
		self.inner
			.next()
			.map(|(k, v)| (Meta(k, v.key_metadata), v.value))
	}
}

impl<T, B, M> ExactSizeIterator for IntoIter<T, B, M> {}

impl<T, B, M> std::iter::FusedIterator for IntoIter<T, B, M> {}

/// Iterator over the properties of a node.
///
/// It is created by the [`Properties::iter`] function.
#[derive(Derivative)]
#[derivative(Clone(bound = ""))]
pub struct Iter<'a, T, B, M> {
	inner: hashbrown::hash_map::Iter<'a, Id<T, B>, PropertyEntry<T, B, M>>,
}

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

	#[inline(always)]
	fn size_hint(&self) -> (usize, Option<usize>) {
		self.inner.size_hint()
	}

	#[inline(always)]
	fn next(&mut self) -> Option<Self::Item> {
		self.inner.next().map(|(property, objects)| {
			(
				Meta(property, &objects.key_metadata),
				objects.value.as_slice(),
			)
		})
	}
}

impl<'a, T, B, M> ExactSizeIterator for Iter<'a, T, B, M> {}

impl<'a, T, B, M> std::iter::FusedIterator for Iter<'a, T, B, M> {}

pub type PropertyEntry<T, B, M> = Entry<PropertyObjects<T, B, M>, M>;

/// Iterator over the properties of a node, giving a mutable reference
/// to the associated objects.
///
/// It is created by the [`Properties::iter_mut`] function.
pub struct IterMut<'a, T, B, M> {
	inner: hashbrown::hash_map::IterMut<'a, Id<T, B>, PropertyEntry<T, B, M>>,
}

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

	#[inline(always)]
	fn size_hint(&self) -> (usize, Option<usize>) {
		self.inner.size_hint()
	}

	#[inline(always)]
	fn next(&mut self) -> Option<Self::Item> {
		self.inner
			.next()
			.map(|(k, v)| (Meta(k, &mut v.key_metadata), &mut v.value))
	}
}

impl<'a, T, B, M> ExactSizeIterator for IterMut<'a, T, B, M> {}

impl<'a, T, B, M> std::iter::FusedIterator for IterMut<'a, T, B, M> {}