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
471
472
473
474
475
476
use std::collections::BTreeSet;

use visible::StructFields;

use super::*;
/// The transaction to modify a [`Graph`].
/// It is a operation recorder which have independent lifetime than the graph and does not hold reference to the graph.
/// The transaction and the graph should have been created from the same [`Context`] to ensure correctness.
#[StructFields(pub(crate))]
pub struct Transaction<'a, NodeT: NodeEnum> {
  ctx_id: Uuid,
  alloc_nodes: BTreeSet<NodeIndex>,
  inc_nodes: Arena<NodeIndex, NodeT>,
  dec_nodes: Vec<NodeIndex>,
  mut_nodes: Vec<(NodeIndex, MutFunc<'a, NodeT>)>,
  update_nodes: Vec<(NodeIndex, UpdateFunc<'a, NodeT>)>,
  redirect_all_links_vec: Vec<(NodeIndex, NodeIndex)>,
  redirect_links_vec: Vec<(NodeIndex, NodeIndex)>,
}

impl<'a, NodeT: NodeEnum> Transaction<'a, NodeT> {
  /// Make a empty transaction
  /// Please ensure the [`Graph`] and the [`Transaction`] use the same Context!
  /// # Example
  /// ```
  /// use ttgraph::*;
  /// #[derive(TypedNode)]
  /// struct NodeA{
  ///   data: usize,
  /// }
  /// node_enum!{
  ///   enum Node{
  ///     A(NodeA)
  ///   }
  /// }
  ///
  /// # fn main() {
  /// let ctx = Context::new();
  /// let mut graph = Graph::<Node>::new(&ctx);
  /// // In fact Transaction::<Node>::new(), but <Node> can be inferenced when commit
  /// let mut trans = Transaction::new(&ctx);
  /// trans.insert(Node::A(NodeA{data: 1}));
  /// graph.commit(trans);
  /// # }
  /// ```
  pub fn new(context: &Context) -> Self {
    let node_dist = Arc::clone(&context.node_dist);
    Transaction {
      ctx_id: context.id,
      alloc_nodes: BTreeSet::new(),
      inc_nodes: Arena::new(node_dist),
      dec_nodes: Vec::new(),
      mut_nodes: Vec::new(),
      update_nodes: Vec::new(),
      redirect_all_links_vec: Vec::new(),
      redirect_links_vec: Vec::new(),
    }
  }

  /// Allocate a new [`NodeIndex`] for a new node, use together with [`fill_back`](Transaction::fill_back)
  /// Useful when there is a cycle.
  /// # Panic
  /// If there is a node that is allocaed, but is not filled back, it is detected and panic to warn the user.
  /// # Example
  /// ```
  /// use ttgraph::*;
  /// #[derive(TypedNode)]
  /// struct NodeA{
  ///   last: NodeIndex,
  ///   next: NodeIndex,
  /// }
  /// node_enum!{
  ///   enum Node{
  ///     A(NodeA)
  ///   }
  /// }
  ///
  /// # fn main() {
  /// let ctx = Context::new();
  /// let mut graph = Graph::<Node>::new(&ctx);
  /// let mut trans = Transaction::new(&ctx);
  /// // Alloc some nodes
  /// let n1 = trans.alloc();
  /// let n2 = trans.alloc();
  /// let n3 = trans.alloc();
  /// // Build a circle
  /// trans.fill_back(n1, Node::A(NodeA{ last: n3, next: n2 }));
  /// trans.fill_back(n2, Node::A(NodeA{ last: n1, next: n3 }));
  /// trans.fill_back(n3, Node::A(NodeA{ last: n2, next: n1 }));
  /// graph.commit(trans);
  /// # }
  /// ```
  pub fn alloc(&mut self) -> NodeIndex {
    let idx = self.inc_nodes.alloc();
    self.alloc_nodes.insert(idx);
    idx
  }

  /// Fill back the data to a [`NodeIndex`] created by [`alloc`](Transaction::alloc)
  pub fn fill_back(&mut self, idx: NodeIndex, data: NodeT) {
    self.inc_nodes.fill_back(idx, data);
    self.alloc_nodes.remove(&idx);
  }

  /// Insert a new node into the graph, returns a [`NodeIndex`] pointing to the new node
  /// # Example
  /// ```
  /// use ttgraph::*;
  /// #[derive(TypedNode)]
  /// struct NodeA{
  ///   data: usize,
  /// }
  /// node_enum!{
  ///   enum Node{
  ///     A(NodeA)
  ///   }
  /// }
  ///
  /// # fn main() {
  /// let ctx = Context::new();
  /// let mut graph = Graph::<Node>::new(&ctx);
  /// let mut trans = Transaction::new(&ctx);
  /// let idx = trans.insert(Node::A(NodeA{data: 1}));
  /// graph.commit(trans);
  /// // Get the node back by the returned NodeIndex idx
  /// assert_eq!(get_node!(graph, Node::A, idx).unwrap().data, 1);
  /// # }
  /// ```
  pub fn insert(&mut self, data: NodeT) -> NodeIndex {
    self.inc_nodes.insert(data)
  }

  /// Remove an existing node
  /// Note: nodes created by [`insert`](Transaction::insert) and [`alloc`](Transaction::alloc) in this uncommitted transaction can also be removed.
  /// Example:
  /// ```
  /// use ttgraph::*;
  /// #[derive(TypedNode)]
  /// struct NodeA{
  ///   data: usize,
  /// }
  /// node_enum!{
  ///   enum Node{
  ///     A(NodeA)
  ///   }
  /// }
  ///
  /// # fn main() {
  /// let ctx = Context::new();
  /// let mut graph = Graph::<Node>::new(&ctx);
  /// let mut trans = Transaction::new(&ctx);
  /// let idx = trans.insert(Node::A(NodeA{data: 1}));
  /// graph.commit(trans);
  /// assert!(graph.get(idx).is_some());
  ///
  /// trans = Transaction::new(&ctx);
  /// trans.remove(idx);
  /// graph.commit(trans);
  /// // Now the node is removed
  /// assert!(graph.get(idx).is_none());
  /// # }
  /// ```
  pub fn remove(&mut self, node: NodeIndex) {
    if self.inc_nodes.remove(node).is_none() && !self.alloc_nodes.remove(&node) {
      self.dec_nodes.push(node);
    }
  }

  /// Mutate a node as with a closure `FnOnce(&mut NodeT)`.
  /// If the type of the node is previously known, use [`mut_node!`](crate::mut_node!) instead.
  /// # Example
  /// ```
  /// use ttgraph::*;
  /// #[derive(TypedNode)]
  /// struct NodeA{
  ///   data: usize,
  /// }
  /// node_enum!{
  ///   enum Node{
  ///     A(NodeA),
  ///     B(NodeA),
  ///   }
  /// }
  ///
  /// # fn main() {
  /// let ctx = Context::new();
  /// let mut graph = Graph::<Node>::new(&ctx);
  /// let mut trans = Transaction::new(&ctx);
  /// let idx = trans.insert(Node::A(NodeA{data: 1}));
  /// graph.commit(trans);
  /// assert_eq!(get_node!(graph, Node::A, idx).unwrap().data, 1);
  ///
  /// trans = Transaction::new(&ctx);
  /// trans.mutate(idx, |node|{
  ///   if let Node::A(x) = node{
  ///     x.data = 2
  ///   }
  /// });
  /// graph.commit(trans);
  /// // Now the data field of the node is 2
  /// assert_eq!(get_node!(graph, Node::A, idx).unwrap().data, 2);
  /// }
  /// ```
  /// # Performance warning
  /// TTGraph does not know which link the user modified, so it always assumes all old links are removed and new links are added.
  /// Try not to create a node with a very large connectivity, or merge multiple operations into once.
  pub fn mutate<F>(&mut self, node: NodeIndex, func: F)
  where
    F: FnOnce(&mut NodeT) + 'a,
  {
    if self.inc_nodes.contains(node) {
      func(self.inc_nodes.get_mut(node).unwrap());
    } else {
      self.mut_nodes.push((node, Box::new(func)));
    }
  }

  /// Update a node with a closure `FnOnce(NodeT) -> NodeT`.
  /// If the type of the node is previously known, use [`update_node!`](crate::update_node!) instead.
  /// # Example
  /// ```
  /// use ttgraph::*;
  /// #[derive(TypedNode)]
  /// struct NodeA{
  ///   data: usize,
  /// }
  /// node_enum!{
  ///   enum Node{
  ///     A(NodeA),
  ///     B(NodeA),
  ///   }
  /// }
  ///
  /// # fn main() {
  /// let ctx = Context::new();
  /// let mut graph = Graph::<Node>::new(&ctx);
  /// let mut trans = Transaction::new(&ctx);
  /// let idx = trans.insert(Node::A(NodeA{data: 1}));
  /// graph.commit(trans);
  /// assert_eq!(get_node!(graph, Node::A, idx).unwrap().data, 1);
  ///
  /// trans = Transaction::new(&ctx);
  /// trans.update(idx, |node| {
  ///   if let Node::A(x) = node{
  ///     Node::A(NodeA{ data: x.data + 1 })
  ///   } else {
  ///     panic!()
  ///   }
  /// });
  /// graph.commit(trans);
  /// assert_eq!(get_node!(graph, Node::A, idx).unwrap().data, 2);
  /// # }
  /// ```
  /// # Performance warning
  /// TTGraph does not know which link the user modified, so it always assumes all old links are removed and new links are added.
  /// Try not to create a node with a very large connectivity, or merge multiple operations into once.
  pub fn update<F>(&mut self, node: NodeIndex, func: F)
  where
    F: FnOnce(NodeT) -> NodeT + 'a,
  {
    if self.inc_nodes.contains(node) {
      self.inc_nodes.update_with(node, func);
    } else {
      self.update_nodes.push((node, Box::new(func)));
    }
  }

  /// Redirect the connections from old_node to new_node
  /// Nodes in the [`Graph`] and new nodes in the [`Transaction`] are both redirected
  /// See [`redirect_links`](Transaction::redirect_links) for counter-example
  /// # Example
  /// ```
  /// use ttgraph::*;
  /// use std::collections::BTreeSet;
  /// #[derive(TypedNode)]
  /// struct NodeA {
  ///   tos: BTreeSet<NodeIndex>,
  /// }
  ///
  /// node_enum! {
  ///   enum Node {
  ///     A(NodeA),
  ///   }
  /// }
  ///
  /// # fn main() {
  /// let context = Context::new();
  /// let mut graph = Graph::<Node>::new(&context);
  /// let mut trans = Transaction::new(&context);
  /// let a = trans.alloc();
  /// let b = trans.alloc();
  /// let c = trans.alloc();
  /// let d = trans.insert(Node::A(NodeA { tos: BTreeSet::new() }));
  /// trans.fill_back(c, Node::A(NodeA { tos: BTreeSet::from_iter([d]) }));
  /// trans.fill_back(b, Node::A(NodeA { tos: BTreeSet::from_iter([c, d]) }));
  /// trans.fill_back(a, Node::A(NodeA { tos: BTreeSet::from_iter([b, c, d]) }));
  /// // Though these new nodes are not commited, they can still be redirected
  /// trans.redirect_all_links(c, b);
  /// trans.redirect_all_links(b, a);
  /// trans.redirect_all_links(d, c);
  /// graph.commit(trans);
  ///
  /// // Graph after redirect
  /// // a -> {a, a, a} = {a}
  /// // b -> {a, a}    = {a}
  /// // c -> {a}       = {a}
  /// // d -> {}
  /// assert_eq!(get_node!(graph, Node::A, a).unwrap().tos, BTreeSet::from([a]));
  /// assert_eq!(get_node!(graph, Node::A, b).unwrap().tos, BTreeSet::from([a]));
  /// assert_eq!(get_node!(graph, Node::A, c).unwrap().tos, BTreeSet::from([a]));
  /// assert_eq!(get_node!(graph, Node::A, d).unwrap().tos, BTreeSet::new());
  /// # }
  /// ```
  pub fn redirect_all_links(&mut self, old_node: NodeIndex, new_node: NodeIndex) {
    self.redirect_all_links_vec.push((old_node, new_node));
  }

  /// Redirect the connections from old_node to new_node
  /// Only nodes in the [`Graph`] is redirected, new nodes in the [`Transaction`] is not redirected
  /// See [`redirect_all_links`](Transaction::redirect_all_links) for counter-example
  /// # Example
  /// ```
  /// use ttgraph::*;
  /// use std::collections::BTreeSet;
  /// #[derive(TypedNode)]
  /// struct NodeA {
  ///   tos: BTreeSet<NodeIndex>,
  /// }
  ///
  /// node_enum! {
  ///   enum Node {
  ///     A(NodeA),
  ///   }
  /// }
  ///
  /// # fn main() {
  /// let context = Context::new();
  /// let mut graph = Graph::<Node>::new(&context);
  /// let mut trans = Transaction::new(&context);
  /// let a = trans.alloc();
  /// let b = trans.alloc();
  /// let c = trans.alloc();
  /// let d = trans.insert(Node::A(NodeA { tos: BTreeSet::new() }));
  /// trans.fill_back(c, Node::A(NodeA { tos: BTreeSet::from_iter([d]) }));
  /// trans.fill_back(b, Node::A(NodeA { tos: BTreeSet::from_iter([c, d]) }));
  /// trans.fill_back(a, Node::A(NodeA { tos: BTreeSet::from_iter([b, c, d]) }));
  /// graph.commit(trans);
  ///
  /// // Graph before redirect
  /// // a -> {b, c, d}
  /// // b -> {c, d}
  /// // c -> {d}
  /// // d -> {}
  ///
  /// trans = Transaction::new(&context);
  /// // Redirect d -> c -> b -> a
  /// // As result, all nodes will be redirected to a
  /// trans.redirect_links(c, b);
  /// trans.redirect_links(b, a);
  /// trans.redirect_links(d, c);
  /// graph.commit(trans);
  ///
  /// // Graph after redirect
  /// // a -> {a, a, a} = {a}
  /// // b -> {a, a}    = {a}
  /// // c -> {a}       = {a}
  /// // d -> {}
  /// assert_eq!(get_node!(graph, Node::A, a).unwrap().tos, BTreeSet::from([a]));
  /// assert_eq!(get_node!(graph, Node::A, b).unwrap().tos, BTreeSet::from([a]));
  /// assert_eq!(get_node!(graph, Node::A, c).unwrap().tos, BTreeSet::from([a]));
  /// assert_eq!(get_node!(graph, Node::A, d).unwrap().tos, BTreeSet::new());
  /// # }
  /// ```
  pub fn redirect_links(&mut self, old_node: NodeIndex, new_node: NodeIndex) {
    self.redirect_links_vec.push((old_node, new_node));
  }

  /// Merge a graph and all its nodes
  /// The merged graph and this transaction should have the same context, otherwise use [`switch_context`](Graph::switch_context) first.
  /// ```
  /// use ttgraph::*;
  /// #[derive(TypedNode)]
  /// struct NodeA{
  ///   data: usize,
  /// }
  /// node_enum!{
  ///   enum Node{
  ///     A(NodeA),
  ///   }
  /// }
  ///
  /// # fn main() {
  /// let ctx1 = Context::new();
  /// let mut graph1 = Graph::<Node>::new(&ctx1);
  /// let mut trans1 = Transaction::new(&ctx1);
  /// let n1 = trans1.insert(Node::A(NodeA{data: 1}));
  /// graph1.commit(trans1);
  ///
  /// let mut graph2 = Graph::<Node>::new(&ctx1);
  /// let mut trans2 = Transaction::new(&ctx1);
  /// let n2 = trans2.insert(Node::A(NodeA{data: 1}));
  /// // graph1 and graph2 have the same context
  /// trans2.merge(graph1);
  /// graph2.commit(trans2);
  /// // Now graph2 have all the nodes in graph1
  /// assert!(graph2.get(n1).is_some());
  ///
  /// let ctx2 = Context::new();
  /// let mut graph3 = Graph::<Node>::new(&ctx2);
  /// let mut trans3 = Transaction::new(&ctx2);
  /// // graph1 and graph2 have different context
  /// trans3.merge(graph2.switch_context(&ctx2));
  /// graph3.commit(trans3);
  /// // Now graph3 have all the nodes in graph2
  /// assert!(graph3.get(n1).is_some());
  /// assert!(graph3.get(n2).is_some());
  /// # }
  /// ```
  pub fn merge(&mut self, graph: Graph<NodeT>) {
    assert!(self.ctx_id == graph.ctx_id);
    for (i, n) in graph.into_iter() {
      self.fill_back(i, n);
    }
  }

  /// Give up the transaction. Currently if a transaction is dropped without commit, it does not give a warning or panic. This issue may be fixed in the future.
  /// Currently this method does nothing.
  /// # Example
  /// ```
  /// use ttgraph::*;
  /// #[derive(TypedNode)]
  /// struct NodeA{
  ///   data: usize,
  /// }
  /// node_enum!{
  ///   enum Node{
  ///     A(NodeA)
  ///   }
  /// }
  ///
  /// # fn main() {
  /// let ctx = Context::new();
  /// let mut graph = Graph::<Node>::new(&ctx);
  /// let mut trans = Transaction::<Node>::new(&ctx);
  /// let idx = trans.insert(Node::A(NodeA{data: 1}));
  ///
  /// trans.give_up();
  /// # }
  /// ```
  pub fn give_up(self) {
    drop(self);
  }
}

impl<'a, NodeT: NodeEnum + Debug> Debug for Transaction<'a, NodeT> {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct(&format!("Transaction<{}>", std::any::type_name::<NodeT>()))
      .field("ctx_id", &self.ctx_id)
      .field("alloc_nodes", &self.alloc_nodes)
      .field("inc_nodes", &self.inc_nodes)
      .field("dec_nodes", &self.dec_nodes)
      .field("mut_nodes", &Vec::from_iter(self.mut_nodes.iter().map(|(x, _)| *x)))
      .field("update_nodes", &Vec::from_iter(self.update_nodes.iter().map(|(x, _)| *x)))
      .field("redirect_all_links", &self.redirect_all_links_vec)
      .field("redirect_links", &self.redirect_links_vec)
      .finish()
  }
}

impl<'a, NodeT: NodeEnum> Extend<NodeT> for Transaction<'a, NodeT> {
  fn extend<T: IntoIterator<Item = NodeT>>(&mut self, iter: T) {
    for x in iter {
      self.insert(x);
    }
  }
}