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
// Copyright (c) 2022-2023 Yegor Bugayenko
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use crate::Sodg;
use anyhow::{anyhow, Result};
use log::debug;
use std::collections::{HashMap, HashSet};

impl Sodg {
    /// Merge another graph into the current one.
    ///
    /// It is expected that both graphs are trees! If they are not, the result is unpredictable.
    ///
    /// The `right` vertex is mapped to the `left` vertex. The decisions about
    /// their kids are made recursively.
    ///
    /// The `left` vertex is expected
    /// to be the root of the current graph, while the `right` vertex is the root
    /// of the graph being merged into the current one.
    ///
    /// # Errors
    ///
    /// If it's impossible to merge, an error will be returned.
    pub fn merge(&mut self, g: &Self, left: u32, right: u32) -> Result<()> {
        let mut mapped = HashMap::new();
        let before = self.vertices.len();
        self.merge_rec(g, left, right, &mut mapped)?;
        let merged = mapped.len();
        let scope = g.vertices.len();
        if merged != scope {
            let must: Vec<u32> = g.vertices.keys().copied().collect();
            let seen: Vec<u32> = mapped.keys().copied().collect();
            let missed: HashSet<u32> = &HashSet::from_iter(must) - &HashSet::from_iter(seen);
            let mut ordered: Vec<u32> = missed.into_iter().collect();
            ordered.sort_unstable();
            return Err(anyhow!(
                "Just {merged} vertices merged, out of {scope}; maybe the right graph was not a tree? {} missed: {}",
                ordered.len(), ordered.iter().map(|v| format!("ν{v}")).collect::<Vec<String>>().join(", ")
            ));
        }
        debug!(
            "Merged all {merged} vertices into SODG of {}, making it have {} after the merge",
            before,
            self.vertices.len()
        );
        Ok(())
    }

    /// Merge two trees recursively, ignoring the nodes already `mapped`.
    ///
    /// The `right` vertex is mapped to the `left` vertex. The decisions about
    /// their kids are made recursively.
    ///
    /// The `mapped` is a key-value map, where the key is a vertex from the right
    /// graph, which is mapped to a vertex from the left graph.
    ///
    /// # Errors
    ///
    /// If it's impossible to merge, an error will be returned.
    fn merge_rec(
        &mut self,
        g: &Self,
        left: u32,
        right: u32,
        mapped: &mut HashMap<u32, u32>,
    ) -> Result<()> {
        if mapped.contains_key(&right) {
            return Ok(());
        }
        mapped.insert(right, left);
        let d = g
            .vertices
            .get(&right)
            .ok_or_else(|| anyhow!("Can't find ν{right} in the right graph"))?
            .data
            .clone();
        if !d.is_empty() {
            self.put(left, &d)?;
        }
        for (a, to) in g.kids(right)? {
            let matched = if let Some(t) = self.kid(left, &a) {
                t
            } else if let Some(t) = mapped.get(&to) {
                self.bind(left, *t, &a)?;
                *t
            } else {
                let id = self.next_id();
                self.add(id)?;
                self.bind(left, id, &a)?;
                id
            };
            self.merge_rec(g, matched, to, mapped)?;
        }
        for (a, to) in g.kids(right)? {
            if let Some(first) = self.kid(left, &a) {
                if let Some(second) = mapped.get(&to) {
                    if first != *second {
                        self.join(first, *second)?;
                    }
                }
            }
        }
        Ok(())
    }

    fn join(&mut self, left: u32, right: u32) -> Result<()> {
        for vtx in &mut self.vertices {
            for e in &mut vtx.1.edges {
                if e.to == right {
                    e.to = left;
                }
            }
        }
        for e in self.kids(right)? {
            if self.kid(left, &e.0).is_some() {
                return Err(anyhow!(
                    "Can't merge ν{right} into ν{left}, due to conflict in '{}'",
                    e.0
                ));
            }
            self.bind(left, e.1, &e.0)?;
        }
        self.vertices.remove(&right);
        Ok(())
    }
}

#[test]
fn merges_two_graphs() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(0)?;
    g.add(1)?;
    g.bind(0, 1, "foo")?;
    let mut extra = Sodg::empty();
    extra.add(0)?;
    extra.add(1)?;
    extra.bind(0, 1, "bar")?;
    g.merge(&extra, 0, 0)?;
    assert_eq!(3, g.vertices.len());
    assert_eq!(1, g.kid(0, "foo").unwrap());
    assert_eq!(2, g.kid(0, "bar").unwrap());
    Ok(())
}

#[test]
fn merges_two_non_trees() -> Result<()> {
    let mut g = Sodg::empty();
    let mut extra = Sodg::empty();
    extra.add(0)?;
    extra.add(42)?;
    extra.add(2)?;
    extra.add(13)?;
    let r = g.merge(&extra, 0, 0);
    assert!(r.is_err());
    let msg = r.err().unwrap().to_string();
    assert!(msg.contains("ν2, ν13, ν42"), "{}", msg);
    Ok(())
}

#[test]
fn merges_a_loop() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(0)?;
    g.add(1)?;
    g.bind(0, 1, "a")?;
    g.add(2)?;
    g.bind(1, 2, "b")?;
    let mut extra = Sodg::empty();
    extra.add(0)?;
    extra.add(4)?;
    extra.bind(0, 4, "c")?;
    extra.add(3)?;
    extra.bind(0, 3, "a")?;
    extra.bind(4, 3, "d")?;
    extra.add(5)?;
    extra.bind(3, 5, "e")?;
    g.merge(&extra, 0, 0)?;
    assert_eq!(5, g.vertices.len());
    assert_eq!(1, g.kid(0, "a").unwrap());
    assert_eq!(2, g.kid(1, "b").unwrap());
    assert_eq!(3, g.kid(0, "c").unwrap());
    assert_eq!(1, g.kid(3, "d").unwrap());
    assert_eq!(5, g.kid(1, "e").unwrap());
    Ok(())
}

#[test]
fn avoids_simple_duplicates() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(0)?;
    g.add(5)?;
    g.bind(0, 5, "foo")?;
    let mut extra = Sodg::empty();
    extra.add(0)?;
    extra.add(1)?;
    extra.bind(0, 1, "foo")?;
    extra.add(2)?;
    extra.bind(1, 2, "bar")?;
    g.merge(&extra, 0, 0)?;
    assert_eq!(3, g.vertices.len());
    assert_eq!(5, g.kid(0, "foo").unwrap());
    assert_eq!(6, g.kid(5, "bar").unwrap());
    Ok(())
}

#[test]
fn keeps_existing_vertices_intact() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(0)?;
    g.add(1)?;
    g.bind(0, 1, "foo")?;
    g.add(2)?;
    g.bind(1, 2, "bar")?;
    g.add(3)?;
    g.bind(2, 3, "zzz")?;
    let mut extra = Sodg::empty();
    extra.add(0)?;
    extra.add(5)?;
    extra.bind(0, 5, "foo")?;
    g.merge(&extra, 0, 0)?;
    assert_eq!(4, g.vertices.len());
    assert_eq!(1, g.kid(0, "foo").unwrap());
    assert_eq!(2, g.kid(1, "bar").unwrap());
    assert_eq!(3, g.kid(2, "zzz").unwrap());
    Ok(())
}

#[test]
fn merges_singletons() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(13)?;
    let mut extra = Sodg::empty();
    extra.add(13)?;
    g.merge(&extra, 13, 13)?;
    assert_eq!(1, g.vertices.len());
    Ok(())
}

#[test]
fn merges_simple_loop() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(1)?;
    g.add(2)?;
    g.bind(1, 2, "foo")?;
    g.bind(2, 1, "bar")?;
    let extra = g.clone();
    g.merge(&extra, 1, 1)?;
    assert_eq!(extra.vertices.len(), g.vertices.len());
    Ok(())
}

#[test]
fn merges_large_loop() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(1)?;
    g.add(2)?;
    g.add(3)?;
    g.add(4)?;
    g.bind(1, 2, "a")?;
    g.bind(2, 3, "b")?;
    g.bind(3, 4, "c")?;
    g.bind(4, 1, "d")?;
    let extra = g.clone();
    g.merge(&extra, 1, 1)?;
    assert_eq!(extra.vertices.len(), g.vertices.len());
    Ok(())
}

#[cfg(test)]
use crate::Hex;

#[test]
fn merges_data() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(1)?;
    let mut extra = Sodg::empty();
    extra.add(1)?;
    extra.put(1, &Hex::from(42))?;
    g.merge(&extra, 1, 1)?;
    assert_eq!(42, g.data(1)?.to_i64()?);
    Ok(())
}

#[test]
fn understands_same_name_kids() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(0)?;
    g.add(1)?;
    g.bind(0, 1, "a")?;
    g.add(2)?;
    g.bind(1, 2, "x")?;
    let mut extra = Sodg::empty();
    extra.add(0)?;
    extra.add(1)?;
    extra.bind(0, 1, "b")?;
    extra.add(2)?;
    extra.bind(1, 2, "x")?;
    g.merge(&extra, 0, 0)?;
    assert_eq!(5, g.vertices.len());
    assert_eq!(1, g.kid(0, "a").unwrap());
    assert_eq!(2, g.kid(1, "x").unwrap());
    Ok(())
}

#[test]
fn merges_into_empty_graph() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(1)?;
    let mut extra = Sodg::empty();
    extra.add(1)?;
    extra.add(2)?;
    extra.add(3)?;
    extra.bind(1, 2, "a")?;
    extra.bind(2, 3, "b")?;
    extra.bind(3, 1, "c")?;
    g.merge(&extra, 1, 1)?;
    assert_eq!(3, g.vertices.len());
    assert_eq!(2, g.kid(1, "a").unwrap());
    Ok(())
}

#[test]
fn mixed_injection() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(4)?;
    let mut extra = Sodg::empty();
    extra.add(4)?;
    extra.put(4, &Hex::from(4))?;
    extra.add(5)?;
    extra.put(5, &Hex::from(5))?;
    extra.bind(4, 5, "b")?;
    g.merge(&extra, 4, 4)?;
    assert_eq!(2, g.vertices.len());
    Ok(())
}

#[test]
fn zero_to_zero() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(0)?;
    g.add(1)?;
    g.bind(0, 1, "a")?;
    g.bind(1, 0, "back")?;
    g.add(2)?;
    g.bind(0, 2, "b")?;
    let mut extra = Sodg::empty();
    extra.add(0)?;
    extra.add(1)?;
    extra.bind(0, 1, "c")?;
    extra.bind(1, 0, "back")?;
    g.merge(&extra, 0, 0)?;
    assert_eq!(4, g.vertices.len());
    Ok(())
}

#[test]
fn finds_siblings() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(0)?;
    g.add(1)?;
    g.bind(0, 1, "a")?;
    g.add(2)?;
    g.bind(0, 2, "b")?;
    let mut extra = Sodg::empty();
    extra.add(0)?;
    extra.add(1)?;
    extra.bind(0, 1, "b")?;
    g.merge(&extra, 0, 0)?;
    assert_eq!(3, g.vertices.len());
    Ok(())
}

#[cfg(test)]
use crate::Script;

#[test]
fn two_big_graphs() -> Result<()> {
    let mut g = Sodg::empty();
    Script::from_str(
        "ADD(0); ADD(1); BIND(0, 1, foo);
        ADD(2); BIND(0, 1, alpha);
        BIND(1, 0, back);",
    )
    .deploy_to(&mut g)?;
    let mut extra = Sodg::empty();
    Script::from_str("ADD(0); ADD(1); BIND(0, 1, bar); BIND(1, 0, back);").deploy_to(&mut extra)?;
    g.merge(&extra, 0, 0)?;
    assert_eq!(4, g.vertices.len());
    Ok(())
}