I need to implement additional container for my library. I need it for holding profile points in my profiler module. Everything is ready to begin coding, however, I am not sure what is the proper name for this kind of container.
What I need, is data structure described as following:
It's a 'multi-level' container (entries can have children).
There is no root node.
At the 'top level' it holds list of elements.
Elements are stored in order they were added.
Each node can have any number of child nodes.
Example usage:
Container c;
c.push(5);
c.push(7);
c.push_level(); //now, new elements will be added as children of '7'.
c.push(3);
c.push(14);
c.pop_level();
c.push(6); //this entry will be added to 'root list'.
Any ideas?
It is a forest. A forest is essentially a set of trees, each with their own root. Your first level of your container is the set of root nodes in the forest, and then all children are described in lower levels.
Everything except the "Elements are stored in order they were added" corresponds with that of an R-tree: http://en.wikipedia.org/wiki/R-tree
Related
I would like to go through the whole xml document that I have, without depending in the actual id value, node name or attributes.
I use the msxml3 lib.
I would like to get a list of the main nodes in the xml, that are descendants of the main node.
<mainNode>
<firstNodeInList></firstNodeInList>
<secondNodeInList></secondNodeInList>
<thirdNodeInList></thirdNodeInList>
</mainNode>
I would like to get a list of the inside nodes, i.e. :
firstNodeInList->secondNodeInList->thirdNodeInList.
Thank you
Since no one responded, I had to find out the answer, which apperently is very simple.
The first line will get the document element, or the root element. The second will get the list of children of the root.
MSXML2::IXMLDOMElementPtr docElem = m_newFileDoc->documentElement;
MSXML2::IXMLDOMNodeListPtr nodes = docElem->childNodes;
Short Description
I need to build a non-binary tree (language doesn't matter for now, but preferably in C++) from a list of items that do have dependencies to each other, but non-recurring and not cyclic.
The data for the nodes are read from a file and incrementally inserted into the tree.
The troubling part is how to handle those nodes which do not have parent-nodes yet that fulfill the dependency of the inserted node.
Detailed Description
Rough Outline
The assignment is easy: represent a bunch of Tasks and Subtasks in a non-binary tree.
This assignment would be quite easy to understand and implement, if not for a tiny condition: the list of Tasks has to be generated incrementally, so do the nodes in the tree.
Scenario
The Tasks are generated asynchronously and have to be added into the tree once the data to a certain Task is received.
This is "simulated" by reading a csv-file which has a certain Task in each line with some data, the most important ones being the PID and PPID attributes.
After a line is read and parsed, a Task is being created and inserted into the tree.
The tree should automatically resolve the dependencies following two simple rules:
Only show the node when the dependency is met (namely when a parent-node has been inserted before), but memorize the (now orphaned) node.
Whenever a Task(node) is added, check if it's a parentnode of one of the above meantioned orphaned ones and reconcile the nodes if rule #1 isn't infringed while doing so.
Please disregard the faulty logic behind this scenario: Normally, there can't be any SubTask without a ParentTask existing (at least in monolithic kernel designs).
And while the List of Tasks certainly do contain the ParentTasks needed to model the tree, it is unknown when the ParentNode-Data is read and inserted into the tree.
Desired outcome
Below is a figure showing the "raw data", a list of (unsorted) Tasks which has been created incrementally while adding one Task after another to the list.
The tree represents the subset of Tasks which has been inserted so far:
Please keep in mind that the tree is completely "naked" until the Tasks with the PIDs 1, 2 and 3 are inserted, because the other nodes are dependent of them.
What I did so far
I've written a Qt-C++ Code with three rough components:
TaskTree which holds a Root-Node (a node without any task-data)
TaskNode which has a field to hold the task-data and a QList<TaskNode> which is, in simple terms, a vector of TaskNodes to reference childnodes
Task has the related attributes (like pid and ppid)
It is no problem to insert a TaskNode if the parentnode already exists.
This only works though in a perfect world, in which the Tasks are sorted upon their respective dependencies AND there's a determined amount of Tasks to be added.
I don't have to tell you that such a scenario is highly unlikely though, so the tree creation has to memorize any orphaned node (which is a node that doesn't have a parent yet, duh).
I've tackled this "memorization" in different ways, but failed alltogether because I couldn't wrap my head around the algorithms behind it.
The two most promising thoughts I had were these:
Insert every orphaned node into a vector. Upon inserting a parentnode, check if it has children in the Orphan-Vector and reconcile. Do this recursively for the newly created subtree to match all possiblities.
Assign the PPID to the tree's RootNode, being 0 for the most top one. When an orphaned node appears, create a new TaskTree, assign the PPID of the orphan to the newly created tree and add the orphan to it.
This creates subtrees which can be quit intricate themselfs if several orphans match one of the trees. After each inserted Node, try to reconcile the subtrees to the root-tree.
Unfortunately I had to give up continuing those two concepts due to several spontaneous SIGSEGV's and other problems occuring because of the recursions etc.
So in the end I'm here trying to find a way to actually make this work without cutting down the complexicity of the problem through assumptions and other cheats...
Do you guys and gals have an idea which algorithm I could use for this problem or what category of problem this even is?
Approach 2 is the right one to take. The pieces that you are missing are that you need an unordered_map called node_needed that maps as yet unseen parent nodes to a vector of child trees that are waiting for it. You need a similar one mapping node_seen to the associated tree for nodes that have been seen.
Then when you see a node you perform the following:
Create TaskTree with only this node.
Add this TaskTree to the node_seen map
If this node's ID is in the parent_needed map:
Add each tree in the parent_needed map to this tree
Remove this node's ID from the parent_needed map
If this node has no parent:
Add this node's tree to the root tree
Else if this node's parent ID is in the node_seen map:
Add this node's tree to the parent tree
Else if this node's parent_ID is in the parent_needed map:
Append this node's tree to the parent_needed vector
Else:
Create a vector containing this node's tree
Add a mapping from this node's parent ID to that vector in the parent_needed map
Assuming no bugs (HAH! Bugs are part of life...), this should work.
After some deliberate design changes, I've come up with - what I think - the easiest way to implement this:
InsertTask(Task newTask)
{
Task parentTask = searchTreeForParent(newTask->ppid)
If (parentTask not found)
{
parentTask = treeRootNode;
}
If (treeRootNode has children)
{
For (every children in treeRootNode: child)
{
If (child->ppid != treeRootNode->pid AND child->ppid == newTask->pid)
{
newTask->addChild(child)
treeRootNode->remove(child)
}
}
}
parentTask->addChild(newTask)
}
The algorithm behind it is pretty easy: You add the new Tasks to the root node if there is no parent node yet and at the same time check if the newly added Task has potential children in the root node (because those orphaned ones were added to the root node before).
So if you actually insert all the Tasks to fulfill the dependencies, you end up with a complete and valid tree.
If you don't supply all the parent nodes, you end up with some of the branches being complete and valid and a bunch of orphaned ones in the root node.
But that's no problem because there is an easy trick to differenciate between a complete branch and orphans: just check if the ppid equals the root node's pid and voila, you output only those branches that are complete.
We heavily use the tree doctrine extension in our zf2 project - with some big tree data structures. We know that inserts and updates in a nested set are expensive. We also know that the tree plugin uses the "root" column to find out which tree shall be updated.
Yesterday I read the tree documentation again and found:
"Support for multiple roots in nested-set"
What does it mean and how does it work? I couldn't find any documentation for this feature.
Our hope would be that we could define a second root item of a lower branch of a big tree so that inserts and updates into this lower branch will not affect the whole tree but only this branch. Is it possible?
yes it is possible, tree root branches will be separated by level 0 nodes, see mapping example of TreeRoot column there should be examples for all mapping types to map treeRoot column. The column must be of the same type as ID, it does not support a ManyToOne relation for now, but there is a plan someday to support it.
root1
child
root2
child
child2
When updating or inserting any child on root2 or root1 branch, it will affect only that certain branch. Also note that, tree is still not concurrently safe, you have to manage locking yourself, see documentation reference here.
The doc directory contains most of the information given here.
I have a given model in .ive and the problem is that I'm trying to rotate a single node out of the whole tree model (existing in the .ive file). Is that possible? How can I do that?
Sure, it can be done.
You first have to find the nodes that you want to manipulate. You should be able to do this by creating a subclass of osg::NodeVisitor, and using it to traverse the graph until you find the node you want to manipulate. If you've given the node a name, it can be simple to find the one you are looking for. If not, you'll have to figure out some other unique characteristics of the node, or just pull all nodes of a certain type and try them one at a time.
Then, once you have that, you can just save a pointer and manipulate it directly.
See tutorials:
https://www.movesinstitute.org/Sullivan/OSGTutorials/osgDOF.html
and
https://www.movesinstitute.org/Sullivan/OSGTutorials/osgUpdate.htm
You will have to find the node that corresponds with that subsection of the model. To do this you will need to identify something that is unique about that node (hopefully it has a name or something).
Once you have that node, you may need to break it off of the main tree, add a Transform of some kind (a PositionAttitudeTransform, probably) and then re-add your node as a child of the PAT.
Then you can modify the rotations in the PAT.
You might use a visitor like the find named node(s) visitor to locate the node you want.
Consider converting the .ive file to .osgt or .osg in order to see the structure (and hopefully node names) in the file.
I've got a spark list that gets dynamically filled.
So far the new items appear at the bottom of the list. What I'd like to do is to add them at the top.
The items in the list have a unique ID so some kind of sorting mechanism would probably do the trick as the new items have a greater ID than the old ones.
What I'd like to avoid is some complex method behind this as I'm working on a mobile platform and the list can get quite big so I need this to be as efficient as possible.
The list's data provider is an ArrayList that gets updated using binding.
How can that be done?
Thanks in advance!
u can Add the items at the starting index of the datagrid. Flex datagrid automatically renew all the indexes and add 1 to all existing element indexes. So
YourDataGridId.dataprovider.addItemAt(item,0) will do.