public override void AddAfter(ShapeNode node, ShapeNode newNode, Direction dir) { CheckFrozen(); if (newNode.List == this) { throw new ArgumentException("newNode is already a member of this collection.", "newNode"); } if (node != null && node.List != this) { throw new ArgumentException("node is not a member of this collection.", "node"); } if (Count == 0) { newNode.Tag = 0; } else { ShapeNode curNode = node; if (dir == Direction.RightToLeft) { curNode = curNode == null ? Last : curNode.Prev; } if (curNode == null) { if (First.Tag == int.MinValue + 1) { RelabelMinimumSparseEnclosingRange(null); } } else if (curNode.Next == null) { if (curNode.Tag == int.MaxValue - 1) { RelabelMinimumSparseEnclosingRange(curNode); } } else if (curNode.Tag + 1 == curNode.Next.Tag) { RelabelMinimumSparseEnclosingRange(curNode); } if (curNode != null && curNode.Next == null) { newNode.Tag = Average(curNode.Tag, int.MaxValue); } else { newNode.Tag = Average(curNode == null ? int.MinValue : curNode.Tag, curNode == null ? First.Tag: curNode.Next.Tag); } } base.AddAfter(node, newNode, dir); _annotations.Add(newNode.Annotation); }
public Shape(Func <bool, ShapeNode> marginSelector, AnnotationList <ShapeNode> annotations) : base(EqualityComparer <ShapeNode> .Default, marginSelector) { _marginSelector = marginSelector; _annotations = annotations; Begin.Tag = int.MinValue; End.Tag = int.MaxValue; _annotations.Add(Begin.Annotation, false); _annotations.Add(End.Annotation, false); }
private void UpdateAnnotations(AnnotationList <ShapeNode> annList, ShapeNode node) { if (annList.Count == 0) { return; } Annotation <ShapeNode> startAnn; annList.Find(node, Direction.LeftToRight, out startAnn); if (startAnn == annList.Begin) { startAnn = annList.First; } Annotation <ShapeNode> endAnn; annList.Find(node, Direction.RightToLeft, out endAnn); if (endAnn == annList.End) { endAnn = annList.Last; } if (startAnn.CompareTo(endAnn) > 0) { return; } foreach (Annotation <ShapeNode> ann in annList.GetNodes(startAnn, endAnn).Where(ann => ann.Span.Contains(node)).ToArray()) { if (!ann.IsLeaf) { UpdateAnnotations(ann.Children, node); } if (ann.Span.Start == node && ann.Span.End == node) { annList.Remove(ann); } else if (ann.Span.Start == node || ann.Span.End == node) { Span <ShapeNode> span = ann.Span.Start == node?_spanFactory.Create(node.Next, ann.Span.End) : _spanFactory.Create(ann.Span.Start, node.Prev); var newAnn = new Annotation <ShapeNode>(span, ann.FeatureStruct.DeepClone()) { Optional = ann.Optional }; if (!ann.IsLeaf) { foreach (Annotation <ShapeNode> child in ann.Children.ToArray()) { newAnn.Children.Add(child, false); } } annList.Remove(ann, false); annList.Add(newAnn, false); } } }
private void CopyAnnotations(AnnotationList <ShapeNode> destList, Annotation <ShapeNode> ann, Dictionary <ShapeNode, ShapeNode> mapping) { if (ann.Span.Start.Annotation == ann) { destList.Add(mapping[ann.Span.Start].Annotation, false); } else { var newAnn = new Annotation <ShapeNode>(_spanFactory.Create(mapping[ann.Span.Start], mapping[ann.Span.End]), ann.FeatureStruct.DeepClone()); destList.Add(newAnn, false); if (!ann.IsLeaf) { foreach (Annotation <ShapeNode> child in ann.Children) { CopyAnnotations(newAnn.Children, child, mapping); } } } }
public void GetNodes() { var annList = new AnnotationList <int>(); annList.Add(1, 2, FeatureStruct.New().Value); annList.Add(9, 10, FeatureStruct.New().Value); annList.Add(24, 25, FeatureStruct.New().Value); annList.Add(49, 50, FeatureStruct.New().Value); annList.Add(99, 100, FeatureStruct.New().Value); annList.Add(new Annotation <int>(Range <int> .Create(20, 70), FeatureStruct.New().Value), false); Assert.IsFalse(annList.GetNodes(0, 1).Any()); Assert.IsFalse(annList.GetNodes(100, 101).Any()); Annotation <int>[] anns = annList.GetNodes(8, 52).ToArray(); Assert.AreEqual(3, anns.Length); Assert.AreEqual(annList.First.Next, anns[0]); Assert.AreEqual(annList.Last.Prev, anns[2]); anns = annList.GetNodes(9, 10).ToArray(); Assert.AreEqual(1, anns.Length); Assert.AreEqual(annList.First.Next, anns[0]); anns = annList.GetNodes(0, 200).ToArray(); Assert.AreEqual(6, anns.Length); }
public void FindDepthFirst() { var annList = new AnnotationList <int>(); annList.Add(1, 2, FeatureStruct.New().Value); annList.Add(9, 10, FeatureStruct.New().Value); annList.Add(49, 50, FeatureStruct.New().Value); annList.Add(69, 70, FeatureStruct.New().Value); annList.Add(99, 100, FeatureStruct.New().Value); annList.Add(1, 49, FeatureStruct.New().Value); annList.Add(51, 100, FeatureStruct.New().Value); Annotation <int> result; Assert.IsFalse(annList.FindDepthFirst(0, out result)); Assert.AreEqual(annList.Begin, result); Assert.IsFalse(annList.FindDepthFirst(100, out result)); Assert.AreEqual(annList.Last, result); Assert.IsTrue(annList.FindDepthFirst(1, out result)); Assert.AreEqual(annList.First, result); Assert.IsFalse(annList.FindDepthFirst(8, out result)); Assert.AreEqual(annList.First.Children.First, result); Assert.IsTrue(annList.FindDepthFirst(99, out result)); Assert.AreEqual(annList.Last.Children.Last, result); Assert.IsTrue(annList.FindDepthFirst(49, out result)); Assert.AreEqual(annList.First.Next, result); Assert.IsFalse(annList.FindDepthFirst(101, Direction.RightToLeft, out result)); Assert.AreEqual(annList.End, result); Assert.IsFalse(annList.FindDepthFirst(1, Direction.RightToLeft, out result)); Assert.AreEqual(annList.First, result); Assert.IsTrue(annList.FindDepthFirst(100, Direction.RightToLeft, out result)); Assert.AreEqual(annList.Last, result); Assert.IsFalse(annList.FindDepthFirst(71, Direction.RightToLeft, out result)); Assert.AreEqual(annList.Last.Children.Last, result); Assert.IsTrue(annList.FindDepthFirst(2, Direction.RightToLeft, out result)); Assert.AreEqual(annList.First.Children.First, result); Assert.IsTrue(annList.FindDepthFirst(50, Direction.RightToLeft, out result)); Assert.AreEqual(annList.Last.Prev, result); }
public void Find() { var annList = new AnnotationList <int>(); annList.Add(1, 2, FeatureStruct.New().Value); annList.Add(9, 10, FeatureStruct.New().Value); annList.Add(24, 25, FeatureStruct.New().Value); annList.Add(49, 50, FeatureStruct.New().Value); annList.Add(99, 100, FeatureStruct.New().Value); annList.Add(99, 100, FeatureStruct.New().Value); annList.Add(new Annotation <int>(Range <int> .Create(20, 70), FeatureStruct.New().Value), false); Annotation <int> result; Assert.IsFalse(annList.Find(0, out result)); Assert.AreSame(annList.Begin, result); Assert.IsTrue(annList.Find(1, out result)); Assert.AreSame(annList.First, result); Assert.IsFalse(annList.Find(100, out result)); Assert.AreSame(annList.Last, result); Assert.IsFalse(annList.Find(101, out result)); Assert.AreSame(annList.Last, result); Assert.IsFalse(annList.Find(30, out result)); Assert.AreSame(annList.ElementAt(3), result); Assert.IsTrue(annList.Find(9, out result)); Assert.AreSame(annList.First.Next, result); Assert.IsFalse(annList.Find(101, Direction.RightToLeft, out result)); Assert.AreSame(annList.End, result); Assert.IsTrue(annList.Find(100, Direction.RightToLeft, out result)); Assert.AreSame(annList.Last, result); Assert.IsFalse(annList.Find(1, Direction.RightToLeft, out result)); Assert.AreSame(annList.First, result); Assert.IsFalse(annList.Find(0, Direction.RightToLeft, out result)); Assert.AreSame(annList.First, result); Assert.IsFalse(annList.Find(15, Direction.RightToLeft, out result)); Assert.AreSame(annList.ElementAt(2), result); Assert.IsTrue(annList.Find(10, Direction.RightToLeft, out result)); Assert.AreSame(annList.First.Next, result); }
public void Add() { var annList = new AnnotationList <int>(); // add without subsumption // add to empty list var a = new Annotation <int>(Range <int> .Create(49, 50), FeatureStruct.New().Value); annList.Add(a, false); Assert.AreEqual(1, annList.Count); Assert.AreSame(a, annList.First); // add to beginning of list a = new Annotation <int>(Range <int> .Create(0, 1), FeatureStruct.New().Value); annList.Add(a, false); Assert.AreEqual(2, annList.Count); Assert.AreSame(a, annList.First); // add to end of list a = new Annotation <int>(Range <int> .Create(99, 100), FeatureStruct.New().Value); annList.Add(a, false); Assert.AreEqual(3, annList.Count); Assert.AreSame(a, annList.Last); // add to middle of list a = new Annotation <int>(Range <int> .Create(24, 25), FeatureStruct.New().Value); annList.Add(a, false); Assert.AreEqual(4, annList.Count); Assert.AreSame(a, annList.ElementAt(1)); // add containing annotation a = new Annotation <int>(Range <int> .Create(0, 100), FeatureStruct.New().Value); annList.Add(a, false); Assert.AreEqual(5, annList.Count); Assert.AreSame(a, annList.First()); // add contained annotation a = new Annotation <int>(Range <int> .Create(9, 10), FeatureStruct.New().Value); annList.Add(a, false); Assert.AreEqual(6, annList.Count); Assert.AreSame(a, annList.ElementAt(2)); annList.Clear(); // add with subsumption // add to empty list a = new Annotation <int>(Range <int> .Create(49, 50), FeatureStruct.New().Value); annList.Add(a); Assert.AreEqual(1, annList.Count); Assert.AreSame(a, annList.First); // add to beginning of list a = new Annotation <int>(Range <int> .Create(0, 1), FeatureStruct.New().Value); annList.Add(a); Assert.AreEqual(2, annList.Count); Assert.AreSame(a, annList.First); // add to end of list a = new Annotation <int>(Range <int> .Create(99, 100), FeatureStruct.New().Value); annList.Add(a); Assert.AreEqual(3, annList.Count); Assert.AreSame(a, annList.Last); // add to middle of list a = new Annotation <int>(Range <int> .Create(24, 25), FeatureStruct.New().Value); annList.Add(a); Assert.AreEqual(4, annList.Count); Assert.AreSame(a, annList.ElementAt(1)); // add containing annotation a = new Annotation <int>(Range <int> .Create(0, 100), FeatureStruct.New().Value); annList.Add(a); Assert.AreEqual(1, annList.Count); Assert.AreSame(a, annList.First()); Assert.AreEqual(4, a.Children.Count); // add contained annotation a = new Annotation <int>(Range <int> .Create(9, 10), FeatureStruct.New().Value); annList.Add(a); Assert.AreEqual(1, annList.Count); Assert.AreEqual(5, annList.First.Children.Count); Assert.AreSame(a, annList.First.Children.ElementAt(1)); annList.Clear(); annList.Add(0, 1, FeatureStruct.New().Value); annList.Add(1, 2, FeatureStruct.New().Value); annList.Add(2, 3, FeatureStruct.New().Value); annList.Add(3, 4, FeatureStruct.New().Value); annList.Add(4, 5, FeatureStruct.New().Value); annList.Add(5, 6, FeatureStruct.New().Value); Assert.AreEqual(6, annList.Count); a = new Annotation <int>(Range <int> .Create(1, 5), FeatureStruct.New().Value); a.Children.Add(1, 3, FeatureStruct.New().Value); a.Children.Add(3, 5, FeatureStruct.New().Value); Assert.AreEqual(2, a.Children.Count); annList.Add(a); Assert.AreEqual(3, annList.Count); Assert.AreSame(a, annList.ElementAt(1)); Assert.AreEqual(2, a.Children.Count); Assert.AreEqual(2, a.Children.First.Children.Count); Assert.AreEqual(2, a.Children.Last.Children.Count); }
public void Remove() { var annList = new AnnotationList <int>(); annList.Add(0, 1, FeatureStruct.New().Value); annList.Add(9, 10, FeatureStruct.New().Value); annList.Add(24, 25, FeatureStruct.New().Value); annList.Add(49, 50, FeatureStruct.New().Value); annList.Add(99, 100, FeatureStruct.New().Value); annList.Remove(annList.First); Assert.AreEqual(4, annList.Count); Assert.AreEqual(Range <int> .Create(9, 10), annList.First.Range); annList.Remove(annList.Last); Assert.AreEqual(3, annList.Count); Assert.AreEqual(Range <int> .Create(49, 50), annList.Last.Range); annList.Remove(annList.First.Next); Assert.AreEqual(2, annList.Count); annList.Remove(annList.First); Assert.AreEqual(1, annList.Count); annList.Remove(annList.First); Assert.AreEqual(0, annList.Count); annList.Add(0, 1, FeatureStruct.New().Value); annList.Add(9, 10, FeatureStruct.New().Value); annList.Add(49, 50, FeatureStruct.New().Value); annList.Add(69, 70, FeatureStruct.New().Value); annList.Add(99, 100, FeatureStruct.New().Value); annList.Add(0, 49, FeatureStruct.New().Value); annList.Add(51, 100, FeatureStruct.New().Value); annList.Remove(annList.First); Assert.AreEqual(4, annList.Count); annList.Remove(annList.Last, false); Assert.AreEqual(3, annList.Count); }