public static AvlTree<CharacterData> Parse(string filePath) { AvlTree<CharacterData> charDictionary = new AvlTree<CharacterData>(); string[] lines = File.ReadAllLines(filePath); for (int i = HeaderSize; i < lines.Length; i += 1) { string firstLine = lines[i]; string[] typesAndValues = firstLine.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); // All the data comes in a certain order, // used to make the parser shorter CharacterData charData = new CharacterData { Id = GetValue(typesAndValues[1]), X = GetValue(typesAndValues[2]), Y = GetValue(typesAndValues[3]), Width = GetValue(typesAndValues[4]), Height = GetValue(typesAndValues[5]), XOffset = GetValue(typesAndValues[6]), YOffset = GetValue(typesAndValues[7]), XAdvance = GetValue(typesAndValues[8]) }; charDictionary.Add(((char)charData.Id).ToString(), charData); } return charDictionary; }
public void CompareSearchTime() { var itemsCount = 10000; var list = new List<int>(); for (int i = 0; i < itemsCount; i++) { list.Add(i); } var startTime = DateTime.Now; list.Contains(5000); var listContainsTime = DateTime.Now - startTime; var avlTree = new AvlTree<int>(); for (int i = 0; i < itemsCount; i++) { avlTree.Add(i); } startTime = DateTime.Now; avlTree.Contains(5000); var avlContainsTime = DateTime.Now - startTime; }
public static void Main() { var treeElements = Console.ReadLine().Split().Select(int.Parse); var tree = new AvlTree<int>(); foreach (int element in treeElements) { tree.Add(element); } // Problem 2 - Range var range = Console.ReadLine().Split().Select(int.Parse).ToArray(); var elementsInRange = tree.Range(range[0], range[1]); Console.WriteLine(string.Join(" ", elementsInRange)); // Problem 3 - Indexing //int index; //bool isIndex = int.TryParse(Console.ReadLine(), out index); //while (isIndex) //{ // try // { // Console.WriteLine(tree[index]); // } // catch (IndexOutOfRangeException) // { // Console.WriteLine("Invalid index"); // } // isIndex = int.TryParse(Console.ReadLine(), out index); //} }
public void CompareRemoveTime() { var itemsCount = 10000; var list = new List<int>(); for (int i = 0; i < itemsCount; i++) { list.Add(i); } var startTime = DateTime.Now; list.Remove(5000); var listRemoveTime = DateTime.Now - startTime; var avlTree = new AvlTree<int>(); for (int i = 0; i < itemsCount; i++) { avlTree.Add(i); } startTime = DateTime.Now; avlTree.Remove(5000); var avlRemoveTime = DateTime.Now - startTime; }
public void ClearTest() { AvlTree<int> actual = new AvlTree<int> { 1,3,5,6,9,7 }; Assert.AreEqual(6, actual.Count); actual.Clear(); Assert.AreEqual(0, actual.Count); }
public static void Main() { AvlTree<int> tree1 = new AvlTree<int>(); tree1.AddElement(10); tree1.AddElement(5); tree1.AddElement(15); tree1.AddElement(6); tree1.AddElement(4); tree1.AddElement(16); tree1.AddElement(14); tree1.AddElement(19); Console.WriteLine(tree1); foreach (var item in tree1) { //same as item.Value Console.WriteLine(item); } var copy = tree1.Clone(); Console.WriteLine(copy); Console.WriteLine(tree1.FindElement(17)); Console.WriteLine(tree1.FindElement(16)); tree1.RemoveElement(19); Console.WriteLine(tree1); }
public void ClearPUT([PexAssumeUnderTest]List<int> values) { AvlTree<int> actual = new AvlTree<int>(values); PexAssert.AreEqual(values.Count, actual.Count); actual.Clear(); PexAssert.AreEqual(0, actual.Count); }
static void Main() { var elements = Console.ReadLine() .Split(' ') .Select(int.Parse); var avlTree = new AvlTree<int>(); foreach (var element in elements) { avlTree.Add(element); } var rangeTokens = Console.ReadLine() .Split(' ') .Select(int.Parse) .ToList(); var from = rangeTokens[0]; var to = rangeTokens[1]; var range = avlTree.Range(from, to); if (range.Count == 0) { Console.WriteLine("(empty)"); } else { foreach (var element in range) { Console.WriteLine(element); } } }
static void Index() { var nums = new int[] { -2, 1, 0, 4, 5, 6, 10, 13, 14, 15, 17, 18, 20}; var tree = new AvlTree<int>(); foreach (var num in nums) { tree.Add(num); } string index; Console.WriteLine("Which index? 'q' for end:"); while(true) { index = Console.ReadLine(); if("q" == index) { break; } try { Console.WriteLine(tree[int.Parse(index)]); } catch (IndexOutOfRangeException e) { Console.WriteLine(e.Message); } } }
public void Delete_Unsorted_Check() { int[] data = { 5, 2, 6, 3, 1, 4 }; int[] expected_sorted = {1, 2, 3, 4, 5, 6}; int[] expected_delete3 = {1, 2, 4, 5, 6}; int[] expected_delete2 = {1, 4, 5, 6}; int[] expected_delete6 = {1, 4, 5}; int[] expected_delete1 = {4, 5}; IBinarySearchTree<int> tree = new AvlTree<int>(); foreach (int value in data) tree.Insert(value); int[] actualArray = tree.ToArray(); CollectionAssert.AreEqual(expected_sorted, actualArray); Assert.IsTrue(tree.Delete(3)); CollectionAssert.AreEqual(expected_delete3, tree.ToArray()); Assert.IsTrue(tree.Delete(2)); CollectionAssert.AreEqual(expected_delete2, tree.ToArray()); Assert.IsTrue(tree.Delete(6)); CollectionAssert.AreEqual(expected_delete6, tree.ToArray()); Assert.IsTrue(tree.Delete(1)); CollectionAssert.AreEqual(expected_delete1, tree.ToArray()); }
public void TestAddCase2() { var tree = new AvlTree<TestNode> (); var t3 = new TestNode (3); var t24 = new TestNode (24); var t26 = new TestNode (26); tree.Add (t3); Assert.AreEqual (1, tree.Count); tree.Remove (t3); tree.Add (new TestNode (37)); tree.Add (new TestNode (70)); tree.Add (new TestNode (12)); Assert.AreEqual (3, tree.Count); tree.Add (new TestNode (90)); tree.Add (new TestNode (25)); tree.Add (new TestNode (99)); tree.Add (new TestNode (91)); tree.Add (t24); tree.Add (new TestNode (28)); tree.Add (t26); // Should do a single left rotation on node with key 12 tree.Remove (t24); Assert.IsTrue (tree.Root.Left == t26, "was:" + tree.Root.Left); }
public void Delete_EntireArray() { int[] data = { 5, 2, 6, 3, 1, 4 }; int[] expected = { 1, 2, 3, 4, 5, 6 }; IBinarySearchTree<int> tree = new AvlTree<int>(); foreach (int value in data) tree.Insert(value); int[] actualArray = tree.ToArray(); CollectionAssert.AreEqual(expected, actualArray); Assert.AreEqual(expected.Length, tree.Size); Assert.IsTrue(tree.Delete(1)); Assert.IsFalse(tree.Find(1)); Assert.IsTrue(tree.Delete(2)); Assert.IsFalse(tree.Find(2)); Assert.IsTrue(tree.Delete(3)); Assert.IsFalse(tree.Find(3)); Assert.IsTrue(tree.Delete(4)); Assert.IsFalse(tree.Find(4)); Assert.IsTrue(tree.Delete(5)); Assert.IsFalse(tree.Find(5)); Assert.IsTrue(tree.Delete(6)); Assert.IsFalse(tree.Find(6)); Assert.AreEqual(0, tree.Size); }
static void Main(string[] args) { var tree = new AvlTree<int>(); //tree.Add(50); //tree.Add(25); //tree.Add(75); //tree.Add(65); //tree.Add(70); //tree.Add(234); //tree.Add(116); //tree.Add(6); //tree.Add(18); //tree.Add(71); //tree.Add(90); //tree.Add(120); //tree.Add(300); int[] nodes = Console.ReadLine().Split().Select(int.Parse).ToArray(); foreach (var node in nodes) { tree.Add(node); } int[] rangeParams = Console.ReadLine().Split().Select(int.Parse).ToArray(); int start = rangeParams[0]; int end = rangeParams[1]; Console.WriteLine(string.Join(" ", tree.Range(start, end))); Console.WriteLine(tree.getRangeCounter); // tree.Add(65); //tree.Add(70); Console.WriteLine(); }
public void ShouldBalanceTreeWhenItIsLeftRightOverweighWithEmptyConstructor() { var avlTree = new AvlTree<int>(new int[] { 40, 15, 25 }); Assert.AreEqual(25, avlTree.Root.Value); Assert.AreEqual(15, avlTree.Root.LeftChild.Value); Assert.AreEqual(40, avlTree.Root.RightChild.Value); }
public void ShouldBalanceTreeWhenItIsRightRightOverweight() { var avlTree = new AvlTree<int>(new int[] { 20, 30, 40 }); Assert.AreEqual(30, avlTree.Root.Value); Assert.AreEqual(20, avlTree.Root.LeftChild.Value); Assert.AreEqual(40, avlTree.Root.RightChild.Value); }
public void DoubleLeftRightRotationTest() { AvlTree<int> actual = new AvlTree<int> { 10, 5, 7 }; Assert.AreEqual(7, actual.Root.Value); Assert.AreEqual(5, actual.Root.Left.Value); Assert.AreEqual(10, actual.Root.Right.Value); }
/// <summary>Creates a static model out of the parameters.</summary> /// <param name="staticModelId">The id of this model for look up purposes.</param> /// <param name="meshes">A list of mesh ids, textures, and buffer references that make up this model.</param> internal StaticModel(string staticModelId, AvlTree<StaticMesh, string> meshes) { _id = staticModelId; _shaderOverride = null; _meshes = meshes; _position = new Vector(0, 0, 0); _scale = new Vector(1, 1, 1); _orientation = Quaternion.FactoryIdentity; }
/// <summary>Creates a blank template for a static model (you will have to construct the model yourself).</summary> public StaticModel(string id) { _id = id; _shaderOverride = null; _meshes = new AvlTreeLinked<StaticMesh, string>(StaticMesh.CompareTo, StaticMesh.CompareTo); _position = new Vector(0, 0, 0); _scale = new Vector(1, 1, 1); _orientation = Quaternion.FactoryIdentity; }
public void TestAddInOrder() { var tree = new AvlTree<TestNode> (); tree.Add (new TestNode (1)); tree.Add (new TestNode (2)); tree.Add (new TestNode (3)); Assert.AreEqual (3, tree.Count); Assert.AreEqual ("1,2,3,", GetContent (tree)); }
public void ShouldCreateCorrectlyAvlTreeWithConstructorWithValue() { var avlTree = new AvlTree<int>(40); Assert.AreEqual(40, avlTree.Root.Value); //check not to add some child nodes Assert.IsFalse(avlTree.Root.HasLeftChild); Assert.IsFalse(avlTree.Root.HasRightChild); }
public void CopyConstructorPUT(List<int> values) { AvlTree<int> avlTree = new AvlTree<int>(values); PexAssume.AreDistinctValues<int>(values.ToArray()); List<int> actual = new List<int>(values.Count); foreach(int i in avlTree) { actual.Add(i); } CollectionAssert.AreEquivalent(values.ToArray(), actual.ToArray()); }
public void Delete_Single() { const int insertValue = 1; IBinarySearchTree<int> tree = new AvlTree<int>(); tree.Insert(insertValue); Assert.IsTrue(tree.Find(insertValue)); Assert.IsTrue(tree.Delete(insertValue)); Assert.IsFalse(tree.Find(insertValue)); }
public void BalanceFactorTest() { AvlTree<int> actual = new AvlTree<int> { 10, 20 }; AvlTreeNode<int> root = actual.FindNode(10); AvlTreeNode<int> leaf = actual.FindNode(20); AvlTreeNode<int> emptyNode = default(AvlTreeNode<int>); Assert.AreEqual(actual.GetBalanceFactor(root), -1); Assert.AreEqual(actual.GetBalanceFactor(leaf), 0); Assert.AreEqual(actual.GetBalanceFactor(emptyNode), 0); }
public void ShouldBalanceTreeWhenItIsLeftRightOverweight() { var avlTree = new AvlTree<int>(40); avlTree.Add(15); avlTree.Add(25); Assert.AreEqual(25, avlTree.Root.Value); Assert.AreEqual(15, avlTree.Root.LeftChild.Value); Assert.AreEqual(40, avlTree.Root.RightChild.Value); }
public void Add_RepeatingElements_ShouldNotAddDuplicates() { var nums = TestUtils.ToIntArrayUnique("1 1 1"); var tree = new AvlTree<int>(); foreach (int num in nums) { tree.Add(num); } Assert.AreEqual(1, tree.Count); }
public void AddSeveralElements_EmptyTree_ShouldIncreaseCount() { var nums = TestUtils.ToIntArrayUnique("1 2 3"); var tree = new AvlTree<int>(); foreach (int num in nums) { tree.Add(num); } Assert.AreEqual(nums.Length, tree.Count); }
/// <summary>Creates a static model from the ids provided.</summary> /// <param name="staticModelId">The id to represent this model as.</param> /// <param name="textures">An array of the texture ids for each sub-mesh of this model.</param> /// <param name="meshes">An array of each mesh id for this model.</param> /// <param name="meshNames">An array of mesh names for this specific instanc3e of a static model.</param> internal StaticModel(string staticModelId, string[] meshNames, string[] meshes, string[] textures) { if (textures.Length != meshes.Length && textures.Length != meshNames.Length) throw new Exception("Attempting to create a static model with non-matching number of components."); _id = staticModelId; _meshes = new AvlTreeLinked<StaticMesh, string>(StaticMesh.CompareTo, StaticMesh.CompareTo); for (int i = 0; i < textures.Length; i++) _meshes.Add(new StaticMesh(meshNames[i], TextureManager.Get(textures[i]), StaticModelManager.GetMesh(meshes[i]).StaticMeshInstance)); _shaderOverride = null; _position = new Vector(0, 0, 0); _scale = new Vector(1, 1, 1); _orientation = Quaternion.FactoryIdentity; }
public void ShouldGenerateAvlTreeFromEnumerableConstructor() { var avlTree = new AvlTree<int>(new int[] { 40, 15, 25, 14, 17, 22, 44, 88 }); Assert.AreEqual(17, avlTree.Root.Value); Assert.AreEqual(15, avlTree.Root.LeftChild.Value); Assert.AreEqual(14, avlTree.Root.LeftChild.LeftChild.Value); Assert.AreEqual(25, avlTree.Root.RightChild.Value); Assert.AreEqual(22, avlTree.Root.RightChild.LeftChild.Value); Assert.AreEqual(44, avlTree.Root.RightChild.RightChild.Value); Assert.AreEqual(40, avlTree.Root.RightChild.RightChild.LeftChild.Value); Assert.AreEqual(88, avlTree.Root.RightChild.RightChild.RightChild.Value); }
private static void Main() { var tree = new AvlTree<int>(); tree.Add(11); tree.Add(10); tree.Add(1); tree.Add(15); tree.Add(13); tree.Add(12); var vals = tree.ToArray(); Console.ReadKey(); }
public void BalanceFactorPUT(int val1, int val2) { PexAssume.AreNotEqual(val1, val2); int minVal = Math.Min(val1, val2); int other = Math.Max(val1, val2); AvlTree<int> actual = new AvlTree<int> {minVal,other}; AvlTreeNode<int> root = actual.FindNode(minVal); AvlTreeNode<int> leaf = actual.FindNode(other); AvlTreeNode<int> emptyNode = default(AvlTreeNode<int>); PexAssert.AreEqual(-1, actual.GetBalanceFactor(root)); PexAssert.AreEqual(0, actual.GetBalanceFactor(leaf)); PexAssert.AreEqual(0, actual.GetBalanceFactor(emptyNode)); }
internal void CacheExpression(int key, Expression expression) => this.expressionCache = this.expressionCache.AddOrUpdate(key, expression);
public static bool Insert <TKey>(this AvlTree <TKey, TKey> source, TKey key) { return(source.Insert(key, key)); }
internal void SetCircularDependencyBarrier(int key, bool value) => this.circularDependencyBarrier = this.circularDependencyBarrier.AddOrUpdate(key, value, true);
public MemoryManager() { m_avail = new AvlTree <MemorySegment>(CompareMemorySegments); m_availAddresses = new BTree <long, long>(); }
private ResolutionContext(IResolutionScope scope, AvlTree <bool> circularDependencyBarrier, AvlTreeKeyValue <Type, Expression> expressionOverrides, AvlTreeKeyValue <Type, Type> currentlyDecoratingTypes, ArrayStore <ArrayStoreKeyed <bool, ParameterExpression> > parameterExpressions, ISet <object> scopeNames, IContainerContext childContext, bool nullResultAllowed, ParameterExpression currentScope, ArrayStoreKeyed <object, ParameterExpression> knownVariables, AvlTree <Expression> expressionCache, bool shouldCacheFactoryDelegate) { this.DefinedVariables = ArrayStoreKeyed <object, ParameterExpression> .Empty; this.SingleInstructions = ArrayStore <Expression> .Empty; this.expressionOverrides = expressionOverrides; this.currentlyDecoratingTypes = currentlyDecoratingTypes; this.NullResultAllowed = nullResultAllowed; this.ResolutionScope = scope; this.CurrentScopeParameter = currentScope; this.ParameterExpressions = parameterExpressions; this.ChildContext = childContext; this.ScopeNames = scopeNames; this.knownVariables = knownVariables; this.circularDependencyBarrier = circularDependencyBarrier; this.ShouldCacheFactoryDelegate = shouldCacheFactoryDelegate; this.expressionCache = expressionCache; }
public TableLocationsCache(ISystemClock systemClock) { _systemClock = systemClock; _cache = new AvlTree(); _lock = new ReaderWriterLockSlim(); }
public void EmptyTree() { var tree = new AvlTree(); Assert.AreEqual(null, tree.Root); }
public void AvlInit() { this.avlTree = new AvlTree(); }
public MainWindowModel() { countRows = 0; countElements = 0; tree = new AvlTree <int, BlockButton>(); }
public static void PrintTree <K, V>(this AvlTree <K, V> self) where K : IComparable <K> { RenderTree(self._root); }
public static IEnumerable <Double2[]> PartitionToMonotone(Double2[][] contours) { // Sort all vertices using their default comparison var vertexSet = new SortedSet <Vertex>(); // Add the vertices to the list foreach (var poly in contours) { // Skip "line" contours if (poly.Length < 3) { continue; } // To make the circular list Vertex first = null, last = null; for (int i = 0; i < poly.Length; i++) { var prev = poly[(i == 0 ? poly.Length : i) - 1]; var cur = poly[i]; var next = poly[i == poly.Length - 1 ? 0 : i + 1]; var vtx = new Vertex(prev, cur, next); // Build the circular list if (last != null) { vtx.PreviousEdge = last.NextEdge; vtx.IncomingEdges.Insert(vtx.PreviousEdge, vtx.PreviousEdge); vtx.NextEdge.Previous = vtx.PreviousEdge; vtx.PreviousEdge.Next = vtx.NextEdge; } // Add the vertex vertexSet.Add(vtx); last = vtx; if (first == null) { first = last; } } // Close the loop first.PreviousEdge = last.NextEdge; first.IncomingEdges.Insert(first.PreviousEdge, first.PreviousEdge); first.NextEdge.Previous = first.PreviousEdge; first.PreviousEdge.Next = first.NextEdge; } // Put all vertices into an array, and swipe from up to down var vertices = vertexSet.ToArray(); Array.Reverse(vertices); // Put the edges here var edges = new AvlTree <Edge, Edge>(); void SplitDiagonal(Vertex v1, Vertex v2) { var e12 = new Edge(v1.Current, v2.Current); var e21 = new Edge(v2.Current, v1.Current); v1.SearchOutgoingEdges(e12, out var e1lo, out var e1ro); v1.SearchIncomingEdges(e21, out var e1li, out var e1ri); v2.SearchOutgoingEdges(e21, out var e2lo, out var e2ro); v2.SearchIncomingEdges(e12, out var e2li, out var e2ri); e1ri.Next = e12; e2lo.Previous = e12; e2ri.Next = e21; e1lo.Previous = e21; e12.Next = e2lo; e12.Previous = e1ri; e21.Next = e1lo; e21.Previous = e2ri; v1.OutgoingEdges.Insert(e12, e12); v1.IncomingEdges.Insert(e21, e21); v2.OutgoingEdges.Insert(e21, e21); v2.IncomingEdges.Insert(e12, e12); // Sanity check var eds = new HashSet <Edge>(ReferenceEqualityComparer.Default); foreach (var e in e12.CyclicalSequence) { if (!eds.Add(e)) { throw new Exception("Problematic edge sequence!"); } } eds.Clear(); foreach (var e in e21.CyclicalSequence) { if (!eds.Add(e)) { throw new Exception("Problematic edge sequence!"); } } } Edge SearchEdge(Vertex v) { edges.SearchLeftRight(new Edge(v.Current, v.Current), out var eleft, out var eright); return(eleft); } // Act according with the type of the vertex foreach (var v in vertices) { switch (v.Type) { case VertexType.Start: case VertexType.Split: if (v.Type == VertexType.Split) { var edgeLeft = SearchEdge(v); SplitDiagonal(v, edgeLeft.Helper); edgeLeft.Helper = v; } v.NextEdge.Helper = v; edges.Insert(v.NextEdge, v.NextEdge); break; case VertexType.End: case VertexType.Merge: if (v.PreviousEdge.Helper.Type == VertexType.Merge) { SplitDiagonal(v, v.PreviousEdge.Helper); } edges.Delete(v.PreviousEdge); if (v.Type == VertexType.Merge) { var edgeLeft = SearchEdge(v); if (edgeLeft.Helper.Type == VertexType.Merge) { SplitDiagonal(v, edgeLeft.Helper); } edgeLeft.Helper = v; } break; case VertexType.RegularLeft: if (v.PreviousEdge.Helper.Type == VertexType.Merge) { SplitDiagonal(v, v.PreviousEdge.Helper); } edges.Delete(v.PreviousEdge); v.NextEdge.Helper = v; edges.Insert(v.NextEdge, v.NextEdge); break; case VertexType.RegularRight: var eleft = SearchEdge(v); if (eleft.Helper.Type == VertexType.Merge) { SplitDiagonal(v, eleft.Helper); } eleft.Helper = v; break; default: break; } } // Collect all edges so we can pick the Y-monotone polygons var allEdges = new HashSet <Edge>(ReferenceEqualityComparer.Default); var primaryEdges = new List <Edge>(); foreach (var v in vertices) { var e = v.NextEdge; var eds = new HashSet <Edge>(ReferenceEqualityComparer.Default); int i = 0; foreach (var u in e.CyclicalSequence) { if (!eds.Add(u)) { throw new Exception("Problematic edge sequence! " + i); } i++; } if (allEdges.Contains(e)) { continue; } primaryEdges.Add(e); foreach (var u in e.CyclicalSequence) { allEdges.Add(u); } } // Now, collect the formed polygons return(primaryEdges.Select(edge => edge.CyclicalSequence.Select(e => e.A).ToArray())); }