public static int SizeOf(object objectValue, TraversalMode traversalMode) { ObjectContext objectContext = new ObjectContext(); objectContext.Visit(objectValue); return(InternalSizeOf(objectValue, traversalMode, objectContext)); }
/// <summary> /// Recursively Visits All nodes in tree applying a given action to all nodes. /// By default this method traverses the tree in inorder fashion. /// </summary> public static void ForEach <T>(BSTNode <T> BinaryTreeRoot, Action <T> Action, TraversalMode Mode = TraversalMode.InOrder) where T : IComparable <T> { if (BinaryTreeRoot == null) { throw new ArgumentNullException("Tree root cannot be null."); } if (Action == null) { throw new ArgumentNullException("Action<T> Action cannot be null."); } // Traverse switch (Mode) { case TraversalMode.PreOrder: PreOrderVisitor(BinaryTreeRoot, Action); return; case TraversalMode.InOrder: InOrderVisitor(BinaryTreeRoot, Action); return; case TraversalMode.PostOrder: PostOrderVisitor(BinaryTreeRoot, Action); return; default: InOrderVisitor(BinaryTreeRoot, Action); return; } }
/// <summary> /// Search the tree for the specified value. /// By default this method traverses the tree in inorder fashion. /// </summary> public static bool BinarySearch <T>(BSTNode <T> BinaryTreeRoot, T Value, TraversalMode Mode = TraversalMode.InOrder) where T : IComparable <T> { if (BinaryTreeRoot == null) { throw new ArgumentNullException("Tree root cannot be null."); } // Traverse // Traverse switch (Mode) { case TraversalMode.PreOrder: return(PreOrderSearcher(BinaryTreeRoot, Value, true)); case TraversalMode.InOrder: return(InOrderSearcher(BinaryTreeRoot, Value, true)); case TraversalMode.PostOrder: return(PostOrderSearcher(BinaryTreeRoot, Value, true)); default: return(InOrderSearcher(BinaryTreeRoot, Value, true)); } }
public static IntersectionResolution GetIntersectionInfo(WeaklySimplePolygon lhs, WeaklySimplePolygon rhs) { IntersectionResolution res = new IntersectionResolution { intersections = new List <IntersectionPair>(), lhs = new Dictionary <int, List <int> >(), rhs = new Dictionary <int, List <int> >() }; //-1 just refers to verts and not an index into holes for (int i = -1; i < lhs.holes.Count; i++) { LineLoop lhsLoop = (i == -1) ? lhs.verts : lhs.holes[i]; for (int j = -1; j < rhs.holes.Count; j++) { LineLoop rhsLoop = (j == -1) ? rhs.verts : rhs.holes[j]; List <LineLoop.LoopLoopIntersection> theseIntersections = LineLoop.AllIntersections(lhsLoop, rhsLoop); foreach (LineLoop.LoopLoopIntersection info in theseIntersections) { gvec2 lhsDir = lhsLoop[info.lhsIndex + 1] - lhsLoop[info.lhsIndex]; gvec2 rhsDir = rhsLoop[info.rhsIndex + 1] - rhsLoop[info.rhsIndex]; TraversalMode lhsMode = gvec2.Dot(lhsDir, rhsDir.RotatedCCW90()) > 0 ? TraversalMode.entering : TraversalMode.exiting; TraversalMode rhsMode = gvec2.Dot(rhsDir, lhsDir.RotatedCCW90()) > 0 ? TraversalMode.entering : TraversalMode.exiting; res.intersections.Add(new IntersectionPair { vert = info.position, lhs = new IntersectionInfo { index = i, segment = info.lhsIndex, param = info.lhsParam, mode = lhsMode }, rhs = new IntersectionInfo { index = j, segment = info.rhsIndex, param = info.rhsParam, mode = rhsMode } }); if (!res.lhs.ContainsKey(i)) { res.lhs[i] = new List <int>(); } res.lhs[i].Add(res.intersections.Count - 1); if (!res.rhs.ContainsKey(j)) { res.rhs[j] = new List <int>(); } res.rhs[j].Add(res.intersections.Count - 1); } } } return(res); }
public QuantizedBvh() { m_bulletVersion = BulletGlobals.BT_BULLET_VERSION; m_useQuantization = false; m_traversalMode = TraversalMode.TRAVERSAL_STACKLESS_CACHE_FRIENDLY; //m_traversalMode = TraversalMode.TRAVERSAL_STACKLESS; //m_traversalMode = TraversalMode.TRAVERSAL_RECURSIVE; m_subtreeHeaderCount = 0; //PCK: add this line m_bvhAabbMin = new Vector3(-MathUtil.SIMD_INFINITY, -MathUtil.SIMD_INFINITY, -MathUtil.SIMD_INFINITY); m_bvhAabbMax = new Vector3(MathUtil.SIMD_INFINITY, MathUtil.SIMD_INFINITY, MathUtil.SIMD_INFINITY); }
public void Traverse(TraversalMode mode, TextWriter tw) { switch (mode) { case TraversalMode.PRE: PreOrder_Traverse(Root, tw); break; case TraversalMode.POST: PostOrder_Traverse(Root, tw); break; case TraversalMode.IN: InOrder_Traverse(Root, tw); break; } }
/************************************************************************************ * PUBLIC API SECTION * */ /// <summary> /// Recusrsivley walks the tree and prints the values of all nodes. /// By default this method traverses the tree in inorder fashion. /// </summary> public static void PrintAll <T>(BSTNode <T> BinaryTreeRoot, TraversalMode Mode = TraversalMode.InOrder) where T : IComparable <T> { if (BinaryTreeRoot == null) { throw new ArgumentNullException("Tree root cannot be null."); } var printAction = new Action <T>((T nodeValue) => System.Console.Write(String.Format("{0} ", nodeValue))); BinaryTreeRecursiveWalker.ForEach(BinaryTreeRoot, printAction, Mode); System.Console.WriteLine(); }
public IEnumerable <T> GetEnumerable(TraversalMode mode, TraversalDirection direction) { switch (mode) { case TraversalMode.DepthFirst: return(DepthFirstEnumerable(direction)); case TraversalMode.BreadthFirst: return(BreadthFirstEnumerable(direction)); default: return(null); } }
public static List <FieldSizeInfo> SizesOf(object objectValue, TraversalMode traversalMode) { if (objectValue == null) { return(new List <FieldSizeInfo>()); } Type type = objectValue.GetType(); if (IsSimpleType(type) || (type.FullName == "System.String") || type.IsArray) { return new List <FieldSizeInfo>() { new FieldSizeInfo(type.FullName, "<object>", type.FullName, SizeOf(objectValue, traversalMode)) } } ; List <FieldSizeInfo> result = new List <FieldSizeInfo>(); ObjectContext objectContext = new ObjectContext(); objectContext.Visit(objectValue); while (type != null) { FieldInfo[] fieldInfos = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); foreach (FieldInfo fieldInfo in fieldInfos) { result.Add ( new FieldSizeInfo ( type.FullName, fieldInfo.Name, fieldInfo.FieldType.FullName, ((traversalMode == TraversalMode.Shallow) || ((traversalMode == TraversalMode.Default) && fieldInfo.IsDefined(typeof(ReferenceAttribute), false))) ? SizeOf(fieldInfo.FieldType) : InternalSizeOf(fieldInfo.GetValue(objectValue), traversalMode, objectContext) ) ); } type = type.BaseType; } return(result); } }
static extern void btQuantizedBvh_setTraversalMode(IntPtr obj, TraversalMode traversalMode);
public void SetTraversalMode(TraversalMode traversalMode) { btQuantizedBvh_setTraversalMode(_native, traversalMode); }
static extern void btQuantizedBvh_setTraversalMode(IntPtr obj, TraversalMode traversalMode);
public void SetTraversalMode(TraversalMode traversalMode) { btQuantizedBvh_setTraversalMode(_native, traversalMode); }
public static void DoRecursively(this IDirectory directoryObject, string path, Action <string> action, TraversalMode mode = TraversalMode.DirectoryFile) { if (mode == TraversalMode.File || mode == TraversalMode.DirectoryFile) { DoRecursivelyForFile(directoryObject, path, action); } if (mode == TraversalMode.Directory || mode == TraversalMode.DirectoryFile) { DoRecursivelyForDirectory(directoryObject, path, action); } }
public ItemParser(IUriMapper uriMapper, TraversalMode mode) { _uriMapper = uriMapper; _mode = mode; }
/// <summary> /// Recursively visits all nodes of the given subtree in the specified order and applies the given action on them. Level-order traversal is iterative. /// </summary> /// <typeparam name="TKey">The data type of the key of the <see cref="BinarySearchTreeMapNode{TKey, TValue}"/>.</typeparam> /// <typeparam name="TValue">The data type of the value of the <see cref="BinarySearchTreeMapNode{TKey, TValue}"/>.</typeparam> /// <param name="root">The root of the subtree for traversal.</param> /// <param name="action">The action to perform on each node.</param> /// <param name="traversalMode">The way in which the subtree is traversed.</param> public static void ForEachRecursive <TKey, TValue>(this BinarySearchTreeMapNode <TKey, TValue> root, Action <BinarySearchTreeMapNode <TKey, TValue> > action, TraversalMode traversalMode = TraversalMode.InOrder) { if (root == null) { throw new ArgumentNullException("root"); } if (action == null) { throw new ArgumentNullException("action"); } switch (traversalMode) { case TraversalMode.InOrder: InOrderVisitor(root, action); break; case TraversalMode.InOrderRightToLeft: InOrderRightToLeftVisitor(root, action); break; case TraversalMode.PreOrder: PreOrderVisitor(root, action); break; case TraversalMode.PreOrderRightToLeft: PreOrderRightToLeftVisitor(root, action); break; case TraversalMode.PostOrder: PostOrderVisitor(root, action); break; case TraversalMode.PostOrderRightToLeft: PostOrderRightToLeftVisitor(root, action); break; case TraversalMode.LevelOrder: LevelOrderVisitor(root, action); break; case TraversalMode.LevelOrderRightToLeft: LevelOrderRightToLeftVisitor(root, action); break; default: InOrderVisitor(root, action); break; } }
/// <summary> /// Recursively visits all nodes of the given tree in the specified order and applies the given action on them. Level-order traversal is iterative. /// </summary> /// <typeparam name="T">The data type of the <see cref="BinarySearchTree{T}"/>.</typeparam> /// <param name="tree">The <see cref="BinarySearchTree{T}"/> for traversal.</param> /// <param name="action">The action to perform on each node.</param> /// <param name="traversalMode">The way in which the tree is traversed.</param> public static void ForEachRecursive <T>(this BinarySearchTree <T> tree, Action <BinarySearchTreeNode <T> > action, TraversalMode traversalMode = TraversalMode.InOrder) { if (tree == null) { throw new ArgumentNullException("tree"); } if (action == null) { throw new ArgumentNullException("action"); } if (tree.Root == null) { return; } switch (traversalMode) { case TraversalMode.InOrder: InOrderVisitor(tree.Root, action); break; case TraversalMode.InOrderRightToLeft: InOrderRightToLeftVisitor(tree.Root, action); break; case TraversalMode.PreOrder: PreOrderVisitor(tree.Root, action); break; case TraversalMode.PreOrderRightToLeft: PreOrderRightToLeftVisitor(tree.Root, action); break; case TraversalMode.PostOrder: PostOrderVisitor(tree.Root, action); break; case TraversalMode.PostOrderRightToLeft: PostOrderRightToLeftVisitor(tree.Root, action); break; case TraversalMode.LevelOrder: LevelOrderVisitor(tree.Root, action); break; case TraversalMode.LevelOrderRightToLeft: LevelOrderRightToLeftVisitor(tree.Root, action); break; default: InOrderVisitor(tree.Root, action); break; } }
///setTraversalMode let's you choose between stackless, recursive or stackless cache friendly tree traversal. Note this is only implemented for quantized trees. void SetTraversalMode(TraversalMode traversalMode) { m_traversalMode = traversalMode; }
/// <summary> /// Search the tree for all matches for a given predicate function. /// By default this method traverses the tree in inorder fashion. /// </summary> public static List <T> FindAllMatches <T>(BSTNode <T> BinaryTreeRoot, Predicate <T> Match, TraversalMode Mode = TraversalMode.InOrder) where T : IComparable <T> { throw new NotImplementedException(); }
/// <summary> /// Iteratively visits all nodes of the given tree in the specified order and applies the given action on them. /// </summary> /// <typeparam name="TKey">The data type of the key of the <see cref="BinarySearchTreeMap{TKey, TValue}"/>.</typeparam> /// <typeparam name="TValue">The data type of the value of the <see cref="BinarySearchTreeMap{TKey, TValue}"/>.</typeparam> /// <param name="tree">The <see cref="BinarySearchTreeMap{TKey, TValue}"/> for traversal.</param> /// <param name="action">The action to perform on each node.</param> /// <param name="traversalMode">The way in which the tree is traversed.</param> public static void ForEachIterative <TKey, TValue>(this BinarySearchTreeMap <TKey, TValue> tree, Action <BinarySearchTreeMapNode <TKey, TValue> > action, TraversalMode traversalMode = TraversalMode.InOrder) { if (tree == null) { throw new ArgumentNullException(nameof(tree)); } if (action == null) { throw new ArgumentNullException(nameof(action)); } if (tree.Root == null) { return; } switch (traversalMode) { case TraversalMode.InOrder: InOrderVisitor(tree.Root, action); break; case TraversalMode.InOrderRightToLeft: InOrderRightToLeftVisitor(tree.Root, action); break; case TraversalMode.PreOrder: PreOrderVisitor(tree.Root, action); break; case TraversalMode.PreOrderRightToLeft: PreOrderRightToLeftVisitor(tree.Root, action); break; case TraversalMode.PostOrder: PostOrderVisitor(tree.Root, action); break; case TraversalMode.PostOrderRightToLeft: PostOrderRightToLeftVisitor(tree.Root, action); break; case TraversalMode.LevelOrder: LevelOrderVisitor(tree.Root, action); break; case TraversalMode.LevelOrderRightToLeft: LevelOrderRightToLeftVisitor(tree.Root, action); break; default: InOrderVisitor(tree.Root, action); break; } }
private static int InternalSizeOf(object objectValue, TraversalMode traversalMode, ObjectContext objectContext) { if (objectValue == null) { return(0); } Type type = objectValue.GetType(); switch (type.FullName) { case "System.Boolean": return(1); case "System.Byte": return(1); case "System.SByte": return(1); case "System.Char": return(1); case "System.Decimal": return(16); case "System.Double": return(8); case "System.Single": return(4); case "System.Int32": return(4); case "System.UInt32": return(4); case "System.Int64": return(8); case "System.UInt64": return(8); case "System.Int16": return(2); case "System.UInt16": return(2); case "System.String": return(((string)objectValue).Length * 2 + 16); // Interned strings? } if (type.IsArray) { Array array = (Array)objectValue; if (array.Rank > 1) { throw new NotSupportedException("Multi-dimension arrays not supported"); } Type elementType = type.GetElementType(); if (IsSimpleType(elementType)) { return(array.Length * SizeOf(elementType) + 32); } int result = array.Length * SizeOf(elementType) + 32; // Size of array itself if (traversalMode != TraversalMode.Shallow) { // Traverse the array elements for (int index = 0; index < array.Length; index++) { object value = array.GetValue(index); if (value != null) { Type valueType = value.GetType(); if (valueType.IsValueType) { if (!IsSimpleType(valueType)) { result += InternalSizeOf(value, traversalMode, objectContext); } } else { if (objectContext.Visit(value)) { result += InternalSizeOf(value, traversalMode, objectContext); } } } } } return(result); } { int result = 12; // base object size while (type != null) { FieldInfo[] fieldInfos = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); foreach (FieldInfo fieldInfo in fieldInfos) { result += SizeOf(fieldInfo.FieldType); bool shouldTraverse = (traversalMode == TraversalMode.Deep) || ((traversalMode == TraversalMode.Default) && !fieldInfo.IsDefined(typeof(ReferenceAttribute), false)); object fieldValue = fieldInfo.GetValue(objectValue); if (fieldValue != null) { Type fieldValueType = fieldValue.GetType(); if (fieldValueType.IsValueType) { if (!IsSimpleType(fieldValueType) && shouldTraverse) { result += InternalSizeOf(fieldValue, traversalMode, objectContext); } } else if ((fieldValueType.IsArray || (fieldValueType.GetInterface("IEnumerable", false) != null)) && objectContext.Visit(fieldValue)) { result += InternalSizeOf(fieldValue, shouldTraverse ? traversalMode : TraversalMode.Shallow, objectContext); } else { if (shouldTraverse && objectContext.Visit(fieldValue)) { result += InternalSizeOf(fieldValue, traversalMode, objectContext); } } } } type = type.BaseType; } return(result); } }
public static WeaklySimplePolygon Union(WeaklySimplePolygon lhs, WeaklySimplePolygon rhs) { WeaklySimplePolygon res = new WeaklySimplePolygon(); IntersectionResolution ir = GetIntersectionInfo(lhs, rhs); //preprocessing for (int i = -1; i < lhs.holes.Count; i++) { if (i == -1 && (!ir.lhs.ContainsKey(-1)) && !lhs.verts[0].IsInside(rhs)) { } //if(ir.lhs.ContainsKey() } List <IntersectionPair> currentLoop = new List <IntersectionPair>(); //this loop finds the next IntersectionPair in the loop //and when a loop is complete, outputs it into res while (ir.intersections.Count != 0) { //start off a random pair if we need to if (currentLoop.Count == 0) { currentLoop.Add(ir.intersections[0]); } //find the next intersection and store it in nearestIntersection IntersectionPair nearestIntersection = null; IntersectionPair lastIntersection = currentLoop.Last(); TraversalMode mode = (lastIntersection.lhs.mode == TraversalMode.exiting) ? TraversalMode.lhs : TraversalMode.rhs; //Dictionary<int, int> nextQueue = foreach (IntersectionPair info in ir.intersections) { if (info.lhs.index == lastIntersection.lhs.index && info.rhs.index == lastIntersection.rhs.index && info != lastIntersection) { if (mode == TraversalMode.lhs) { //traversing lhs if (nearestIntersection == null) { nearestIntersection = info; } else { if (nearestIntersection.lhs.dist < lastIntersection.lhs.dist) { if (info.lhs.dist > lastIntersection.lhs.dist || info.lhs.dist < nearestIntersection.lhs.dist) { nearestIntersection = info; } } else { if (info.lhs.dist < nearestIntersection.lhs.dist && info.lhs.dist > lastIntersection.lhs.dist) { nearestIntersection = info; } } } } else { //traversing rhs if (nearestIntersection == null) { nearestIntersection = info; } else { if (nearestIntersection.rhs.dist < lastIntersection.rhs.dist) { if (info.rhs.dist > lastIntersection.rhs.dist || info.rhs.dist < nearestIntersection.rhs.dist) { nearestIntersection = info; } } else { if (info.rhs.dist < nearestIntersection.rhs.dist && info.rhs.dist > lastIntersection.rhs.dist) { nearestIntersection = info; } } } } } } //do what we must with this intersection, we might be done with the loop if (nearestIntersection == currentLoop.First()) { //loop is done, create the loop with actual segments and such LineLoop loop = new LineLoop(); //segment mode TraversalMode segMode = (currentLoop.First().lhs.mode == TraversalMode.exiting) ? TraversalMode.lhs : TraversalMode.rhs; for (int i = 0; i < currentLoop.Count; i++) { IntersectionPair info = currentLoop[i]; IntersectionPair nextInfo = currentLoop[(i + 1) % currentLoop.Count]; loop.Add(info.vert); //select the correct polygon and loop WeaklySimplePolygon opPoly = (segMode == TraversalMode.lhs) ? lhs : rhs; int loopIndex = (segMode == TraversalMode.lhs) ? info.lhs.index : info.rhs.index; LineLoop opLoop = (loopIndex == -1) ? opPoly.verts : opPoly.holes[loopIndex]; int startSegment = ((segMode == TraversalMode.lhs) ? (info.lhs.segment + 1) : info.rhs.segment + 1) % opLoop.Count; int endSegment = (segMode == TraversalMode.lhs) ? nextInfo.lhs.segment : nextInfo.rhs.segment; int endSegmentPlusOneMod = (endSegment + 1) % opLoop.Count; bool first = (segMode == TraversalMode.lhs) ? (info.lhs.dist > nextInfo.lhs.dist) : (info.rhs.dist > nextInfo.rhs.dist); for (int currentSegment = startSegment; (currentSegment != endSegmentPlusOneMod) || first; currentSegment = (currentSegment + 1) % opLoop.Count) { loop.Add(opLoop[currentSegment]); if (first) { first = false; } } if (segMode == TraversalMode.lhs) { segMode = TraversalMode.rhs; } else { segMode = TraversalMode.lhs; } } res.holes.Add(loop); foreach (IntersectionPair info in currentLoop) { ir.intersections.Remove(info); } currentLoop.Clear(); } else { currentLoop.Add(nearestIntersection); } } return(res); }