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
use crate::object::{FragmentRef, InvalidExpandedJson, Traverse};
use crate::{id, Id, Indexed, Relabel, StrippedIndexedObject};
use crate::{IndexedObject, TryFromJson};
use hashbrown::HashMap;
use locspan::{Location, Meta, StrippedEq, StrippedPartialEq};
use rdf_types::vocabulary::{Index, VocabularyMut};
use rdf_types::Vocabulary;
use std::collections::HashSet;
use std::hash::Hash;
pub struct ExpandedDocument<T = Index, B = Index, M = Location<T>>(
HashSet<StrippedIndexedObject<T, B, M>>,
);
impl<T, B, M> Default for ExpandedDocument<T, B, M> {
#[inline(always)]
fn default() -> Self {
Self(HashSet::new())
}
}
impl<T, B, M> ExpandedDocument<T, B, M> {
#[inline(always)]
pub fn new() -> Self {
Self::default()
}
#[inline(always)]
pub fn len(&self) -> usize {
self.0.len()
}
#[inline(always)]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[inline(always)]
pub fn objects(&self) -> &HashSet<StrippedIndexedObject<T, B, M>> {
&self.0
}
#[inline(always)]
pub fn into_objects(self) -> HashSet<StrippedIndexedObject<T, B, M>> {
self.0
}
#[inline(always)]
pub fn iter(&self) -> std::collections::hash_set::Iter<'_, StrippedIndexedObject<T, B, M>> {
self.0.iter()
}
#[inline(always)]
pub fn traverse(&self) -> Traverse<T, B, M> {
Traverse::new(self.iter().map(|o| FragmentRef::IndexedObject(o)))
}
#[inline(always)]
pub fn count(&self, f: impl FnMut(&FragmentRef<T, B, M>) -> bool) -> usize {
self.traverse().filter(f).count()
}
#[inline(always)]
pub fn identify_all_with<V: Vocabulary<Iri = T, BlankId = B>, G: id::Generator<V, M>>(
&mut self,
vocabulary: &mut V,
generator: &mut G,
) where
M: Clone,
T: Eq + Hash,
B: Eq + Hash,
{
let mut objects = HashSet::new();
std::mem::swap(&mut self.0, &mut objects);
for mut object in objects {
object.identify_all_with(vocabulary, generator);
self.0.insert(object);
}
}
#[inline(always)]
pub fn identify_all<G: id::Generator<(), M>>(&mut self, generator: &mut G)
where
M: Clone,
T: Eq + Hash,
B: Eq + Hash,
(): Vocabulary<Iri = T, BlankId = B>,
{
self.identify_all_with(&mut (), generator)
}
#[inline(always)]
pub fn relabel_and_canonicalize_with<
V: Vocabulary<Iri = T, BlankId = B>,
G: id::Generator<V, M>,
>(
&mut self,
vocabulary: &mut V,
generator: &mut G,
) where
M: Clone,
T: Clone + Eq + Hash,
B: Clone + Eq + Hash,
{
let mut objects = HashSet::new();
std::mem::swap(&mut self.0, &mut objects);
let mut relabeling = HashMap::new();
let mut buffer = ryu_js::Buffer::new();
for mut object in objects {
object.relabel_with(vocabulary, generator, &mut relabeling);
object.canonicalize_with(&mut buffer);
self.0.insert(object);
}
}
#[inline(always)]
pub fn relabel_and_canonicalize<G: id::Generator<(), M>>(&mut self, generator: &mut G)
where
M: Clone,
T: Clone + Eq + Hash,
B: Clone + Eq + Hash,
(): Vocabulary<Iri = T, BlankId = B>,
{
self.relabel_and_canonicalize_with(&mut (), generator)
}
#[inline(always)]
pub fn relabel_with<V: Vocabulary<Iri = T, BlankId = B>, G: id::Generator<V, M>>(
&mut self,
vocabulary: &mut V,
generator: &mut G,
) where
M: Clone,
T: Clone + Eq + Hash,
B: Clone + Eq + Hash,
{
let mut objects = HashSet::new();
std::mem::swap(&mut self.0, &mut objects);
let mut relabeling = HashMap::new();
for mut object in objects {
object.relabel_with(vocabulary, generator, &mut relabeling);
self.0.insert(object);
}
}
#[inline(always)]
pub fn relabel<G: id::Generator<(), M>>(&mut self, generator: &mut G)
where
M: Clone,
T: Clone + Eq + Hash,
B: Clone + Eq + Hash,
(): Vocabulary<Iri = T, BlankId = B>,
{
self.relabel_with(&mut (), generator)
}
pub fn canonicalize_with(&mut self, buffer: &mut ryu_js::Buffer)
where
T: Eq + Hash,
B: Eq + Hash,
{
let mut objects = HashSet::new();
std::mem::swap(&mut self.0, &mut objects);
for mut object in objects {
object.canonicalize_with(buffer);
self.0.insert(object);
}
}
pub fn canonicalize(&mut self)
where
T: Eq + Hash,
B: Eq + Hash,
{
let mut buffer = ryu_js::Buffer::new();
self.canonicalize_with(&mut buffer)
}
pub fn blank_ids(&self) -> HashSet<&B>
where
B: Eq + Hash,
{
self.traverse()
.filter_map(|f| f.into_id().and_then(Id::into_blank))
.collect()
}
}
impl<T: Hash + Eq, B: Hash + Eq, M> ExpandedDocument<T, B, M> {
#[inline(always)]
pub fn insert(&mut self, object: IndexedObject<T, B, M>) -> bool {
self.0.insert(locspan::Stripped(object))
}
}
impl<T: Eq + Hash, B: Eq + Hash, M> TryFromJson<T, B, M> for ExpandedDocument<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::Array(items) => {
let mut result = Self::new();
for item in items {
result.insert(Indexed::try_from_json_in(vocabulary, item)?);
}
Ok(Meta(result, meta))
}
other => Err(Meta(
InvalidExpandedJson::Unexpected(other.kind(), json_syntax::Kind::Array),
meta,
)),
}
}
}
impl<T: Eq + Hash, B: Eq + Hash, M> PartialEq for ExpandedDocument<T, B, M> {
fn eq(&self, other: &Self) -> bool {
self.0.eq(&other.0)
}
}
impl<T: Eq + Hash, B: Eq + Hash, M> Eq for ExpandedDocument<T, B, M> {}
impl<T: Eq + Hash, B: Eq + Hash, M> StrippedPartialEq for ExpandedDocument<T, B, M> {
fn stripped_eq(&self, other: &Self) -> bool {
self.0.eq(&other.0)
}
}
impl<T: Eq + Hash, B: Eq + Hash, M> StrippedEq for ExpandedDocument<T, B, M> {}
impl<T, B, M> IntoIterator for ExpandedDocument<T, B, M> {
type IntoIter = IntoIter<T, B, M>;
type Item = IndexedObject<T, B, M>;
#[inline(always)]
fn into_iter(self) -> Self::IntoIter {
IntoIter(self.0.into_iter())
}
}
impl<'a, T, B, M> IntoIterator for &'a ExpandedDocument<T, B, M> {
type IntoIter = std::collections::hash_set::Iter<'a, StrippedIndexedObject<T, B, M>>;
type Item = &'a StrippedIndexedObject<T, B, M>;
#[inline(always)]
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
pub struct IntoIter<T, B, M>(std::collections::hash_set::IntoIter<StrippedIndexedObject<T, B, M>>);
impl<T, B, M> Iterator for IntoIter<T, B, M> {
type Item = IndexedObject<T, B, M>;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|s| s.0)
}
}
impl<T: Hash + Eq, B: Hash + Eq, M> FromIterator<IndexedObject<T, B, M>>
for ExpandedDocument<T, B, M>
{
fn from_iter<I: IntoIterator<Item = IndexedObject<T, B, M>>>(iter: I) -> Self {
Self(iter.into_iter().map(locspan::Stripped).collect())
}
}
impl<T: Hash + Eq, B: Hash + Eq, M> Extend<IndexedObject<T, B, M>> for ExpandedDocument<T, B, M> {
fn extend<I: IntoIterator<Item = IndexedObject<T, B, M>>>(&mut self, iter: I) {
self.0.extend(iter.into_iter().map(locspan::Stripped))
}
}
impl<T, B, M> From<HashSet<StrippedIndexedObject<T, B, M>>> for ExpandedDocument<T, B, M> {
fn from(set: HashSet<StrippedIndexedObject<T, B, M>>) -> Self {
Self(set)
}
}