/* * Search down the tree to find the leaf that points to the segment * containing search_key. The internal node containing the leaf pointer * is returned. */ struct tree_node *veb_tree_find(struct veb *veb, key_t search_key) { int i; int cmp; struct tree_node *root = veb->elements; struct tree_node *node = root; int bfs_num = 1; for (i=1; i < veb->height; i++) { int lefti = bfs_left(bfs_num); int righti = bfs_right(bfs_num); struct tree_node *left = node_at(veb, lefti); struct tree_node *right = node_at(veb, righti); cmp = search_key - node->key; if (cmp < 0) { node = left; bfs_num = lefti; } else { node = right; bfs_num = righti; } } return node; }
// // state_of_nodes() // boost::shared_ptr<std::vector< std::map<unsigned int, bool> > > state_of_nodes(const HypothesesGraph& g) { LOG(logDEBUG) << "detections(): entered"; shared_ptr<vector< map<unsigned int, bool> > > ret(new vector< map<unsigned int, bool> >); // required node properties: timestep, traxel, active typedef property_map<node_timestep, HypothesesGraph::base_graph>::type node_timestep_map_t; node_timestep_map_t& node_timestep_map = g.get(node_timestep()); typedef property_map<node_traxel, HypothesesGraph::base_graph>::type node_traxel_map_t; node_traxel_map_t& node_traxel_map = g.get(node_traxel()); typedef property_map<node_active, HypothesesGraph::base_graph>::type node_active_map_t; node_active_map_t& node_active_map = g.get(node_active()); // for every timestep for(int t = g.earliest_timestep(); t <= g.latest_timestep(); ++t) { ret->push_back(map<unsigned int, bool>()); for(node_timestep_map_t::ItemIt node_at(node_timestep_map, t); node_at!=lemon::INVALID; ++node_at) { assert(node_traxel_map[node_at].Timestep == t); unsigned int id = node_traxel_map[node_at].Id; bool active = node_active_map[node_at]; (*ret)[t-g.earliest_timestep()][id] = active; } } return ret; }
void base_node_state::on_save(clsm::ev_cmd< save_cmd > const& cmd) { auto const& editor = context< paramtree_editor >(); auto acc = editor.acc; qualified_path path; if(cmd.nav) { path = nav_cmd_path(*cmd.nav); if(!path) { editor.output("invalid path specified"); return;// false; } } else { path = acc.where(); } // fs::path filepath{ cmd.filename }; std::ofstream file{ cmd.filename }; if(!file.is_open()) { editor.output("invalid filename"); return;// false; } auto subtree_root = acc.node_at(path); auto as_yaml = editor.ptree.convert_to_yaml(subtree_root); file << YAML::Dump(as_yaml); editor.output("subtree saved"); }
void veb_tree_print(struct veb *veb) { int i; for (i=0; i < (1 << veb->height) - 1; i++) { if (is_power_of_two(i+1)) printf("\n"); printf("%04d ", node_at(veb, i+1)->key); } printf("\n"); }
/* * Update this node so that it contains the maximum of the * left subtree and the left-most node of the right subtree. * This ensures that every node to the right is at least greater * than or equal to this node. */ void veb_tree_recompute_index(struct veb *veb, int bfs_index) { int i, nexti; int lefti = bfs_left(bfs_index); int righti = bfs_right(bfs_index); struct tree_node *left = node_at(veb, lefti); struct tree_node *right = node_at(veb, righti); int leftval = left->key; int rightval = right->key; /* explore to the left of right */ nexti = bfs_left(righti); for (i=ilog2(righti) + 1; i < veb->height; i++) { struct tree_node *next = node_at(veb, nexti); nexti = bfs_left(nexti); rightval = next->key; } node_at(veb, bfs_index)->key = max(leftval, rightval); }
void base_node_state::on_load(clsm::ev_cmd< load_cmd > const& cmd) { auto& editor = context< paramtree_editor >(); auto acc = editor.acc; qualified_path path; if(cmd.nav) { path = nav_cmd_path(*cmd.nav); if(!path) { editor.output("invalid path specified"); return;// false; } } else { path = acc.where(); } auto as_yaml = YAML::LoadFile(cmd.filename); if(as_yaml.IsNull()) { editor.output("invalid filename/file"); return;// false; } auto subtree_root = acc.node_at(path); auto& tree = editor.ptree.tree(); tree.clear_children(subtree_root); editor.ptree.generate_from_yaml(as_yaml, subtree_root); /* TODO: generate from yaml does not validate loaded params against schema... bool ok = schema::validate_param(cmd.val, sch); if(!ok) { os << "invalid value" << std::endl; return false; } */ // TODO: Overkill editor.reload_pt_schema(); // TODO: if load into node which is above current location, current location may cease to exist, // in which case need to move the accessor, and transition to node_type_choice editor.output("subtree loaded"); }
void veb_tree_link_leaf(struct veb *veb, int bfs_index, struct leaf *leaf) { node_at(veb,bfs_index)->leaf = leaf; }
void veb_tree_set_node_key(struct veb *veb, int bfs_index, key_t key) { node_at(veb,bfs_index)->key = key; }
boost::shared_ptr<std::vector< std::vector<Event> > > events(const HypothesesGraph& g) { LOG(logDEBUG) << "events(): entered"; shared_ptr<std::vector< std::vector<Event> > > ret(new vector< vector<Event> >); typedef property_map<node_timestep, HypothesesGraph::base_graph>::type node_timestep_map_t; node_timestep_map_t& node_timestep_map = g.get(node_timestep()); typedef property_map<node_traxel, HypothesesGraph::base_graph>::type node_traxel_map_t; node_traxel_map_t& node_traxel_map = g.get(node_traxel()); // for every timestep LOG(logDEBUG1) << "events(): earliest_timestep: " << g.earliest_timestep(); LOG(logDEBUG1) << "events(): latest_timestep: " << g.latest_timestep(); for(int t = g.earliest_timestep(); t < g.latest_timestep(); ++t) { LOG(logDEBUG2) << "events(): processing timestep: " << t; ret->push_back(vector<Event>()); // for every node: destiny LOG(logDEBUG2) << "events(): for every node: destiny"; for(node_timestep_map_t::ItemIt node_at(node_timestep_map, t); node_at!=lemon::INVALID; ++node_at) { assert(node_traxel_map[node_at].Timestep == t); // count ougoing arcs int count = 0; for(HypothesesGraph::base_graph::OutArcIt a(g, node_at); a!=lemon::INVALID; ++a) ++count; LOG(logDEBUG3) << "events(): counted outgoing arcs: " << count; // construct suitable Event object switch(count) { // Disappearance case 0: { Event e; e.type = Event::Disappearance; e.traxel_ids.push_back(node_traxel_map[node_at].Id); (*ret)[t-g.earliest_timestep()].push_back(e); LOG(logDEBUG3) << e; break; } // Move case 1: { Event e; e.type = Event::Move; e.traxel_ids.push_back(node_traxel_map[node_at].Id); HypothesesGraph::base_graph::OutArcIt a(g, node_at); e.traxel_ids.push_back(node_traxel_map[g.target(a)].Id); (*ret)[t-g.earliest_timestep()].push_back(e); LOG(logDEBUG3) << e; break; } // Division case 2: { Event e; e.type = Event::Division; e.traxel_ids.push_back(node_traxel_map[node_at].Id); HypothesesGraph::base_graph::OutArcIt a(g, node_at); e.traxel_ids.push_back(node_traxel_map[g.target(a)].Id); ++a; e.traxel_ids.push_back(node_traxel_map[g.target(a)].Id); (*ret)[t-g.earliest_timestep()].push_back(e); LOG(logDEBUG3) << e; break; } default: throw runtime_error("events(): encountered node dividing in three or more nodes in graph"); break; } } // appearances in next timestep LOG(logDEBUG2) << "events(): appearances in next timestep"; for(node_timestep_map_t::ItemIt node_at(node_timestep_map, t+1); node_at!=lemon::INVALID; ++node_at) { // count incoming arcs int count = 0; for(HypothesesGraph::base_graph::InArcIt a(g, node_at); a!=lemon::INVALID; ++a) ++count; LOG(logDEBUG3) << "events(): counted incoming arcs in next timestep: " << count; // no incoming arcs => appearance if(count == 0) { Event e; e.type = Event::Appearance; e.traxel_ids.push_back(node_traxel_map[node_at].Id); (*ret)[t-g.earliest_timestep()].push_back(e); LOG(logDEBUG3) << e; } } } return ret; }
int main() { t_node *head; t_node *n1; t_node *n2; int *i1; int *i2; int *i3; int i; int j; int k; char *c1; char *c2; char *c3; char a; char b; char c; head = (t_node*)xmalloc(10*sizeof(t_node)); /*new_node*/ n1 = new_node("one\n", NULL); my_str(n1->elem);/*prints one*/ n1->elem = NULL; free(n1); n1 = new_node(NULL, NULL); if(!n1->elem) my_str("create NULL node ok\n"); else my_str("create NULL node FAIL!\n"); free(n1); n1 = new_node("a", NULL); n2 = new_node("b", n1); my_str(n2->elem); my_str((n2->next)->elem);/*prints ba*/ my_char('\n'); my_str("---------------------------------------------------------------------\n"); /*add_node*/ add_node(n1, &head); my_str((*head).elem);/*prints a*/ my_char('\n'); add_node(n2, &head); my_str((*head).elem);/*prints b*/ my_char('\n'); add_node(NULL, &head); if(strcmp((*head).elem, n2->elem) == 0) my_str("add NULL ok\n"); else my_str("add NULL FAIL!\n"); add_node(new_node(NULL, NULL), &head); if(strcmp((*head).elem, n2->elem) == 0) my_str("add NULL node ok\n"); else my_str("add NULL node FAIL!\n"); add_node(new_node("something", NULL), NULL);/*if this line doesn't segfault then we're good!*/ my_str("---------------------------------------------------------------------\n"); /*traversals*/ empty_list(&head); i = 3; i1 = &i; add_node(new_node(i1, NULL), &head); j = 2; i2 = &j; add_node(new_node(i2, NULL), &head); k = 1; i3 = &k; add_node(new_node(i3, NULL), &head); traverse_int(head);/*prints 1 2 3 */ my_char('\n'); head->next->elem = NULL; traverse_int(head);/*prints 1 NULL 3*/ my_char('\n'); traverse_int(NULL);/*prints The list is empty!*/ empty_list(&head); c = 'c'; c1 = &c; add_node(new_node(c1, NULL), &head); b = 'b'; c2 = &b; add_node(new_node(c2, NULL), &head); a = 'a'; c3 = &a; add_node(new_node(c3, NULL), &head); traverse_char(head);/*prints a b c */ my_char('\n'); head->elem = NULL; traverse_char(head);/*prints NULL b c*/ my_char('\n'); traverse_char(NULL);/*prints The list is empty!*/ empty_list(&head); add_node(new_node("third", NULL), &head); add_node(new_node("second", NULL), &head); add_node(new_node("first", NULL), &head); traverse_string(head);/*prints first second third */ my_char('\n'); head->next->next->elem = NULL; traverse_string(head);/*prints first second NULL*/ my_char('\n'); traverse_string(NULL);/*prints The list is empty!*/ empty_list(&head); my_str("---------------------------------------------------------------------\n"); /*add_elem*/ add_elem("a", &head); add_elem("b", &head); add_elem("c", &head); my_str(head->elem);/*prints c*/ my_char('\n'); add_elem(NULL, &head); if(strcmp(head->elem, "c") == 0) my_str("add NULL elem ok\n"); else my_str("add NULL elem FAIL!\n"); my_str("---------------------------------------------------------------------\n"); /*append*/ append(new_node("z", NULL), &head); traverse_string(head);/*prints c b a z*/ my_char('\n'); append(NULL, &head); traverse_string(head);/*prints c b a z*/ my_char('\n'); append(new_node("stuff", NULL), NULL);/*if this line doesn't segfault then we're good!*/ my_str("---------------------------------------------------------------------\n"); /*add_node_at*/ add_node_at(new_node("d", NULL), &head, 0); traverse_string(head);/*prints d c b a z*/ my_char('\n'); add_node_at(new_node("y", NULL), &head, 42); traverse_string(head);/*prints d c b a z y*/ my_char('\n'); add_node_at(new_node("0", NULL), &head, 4); traverse_string(head);/*prints d c b a 0 z y*/ my_char('\n'); add_node_at(NULL, &head, 2); traverse_string(head);/*prints d c b a 0 z y*/ my_char('\n'); add_node_at(new_node(NULL, NULL), &head, 1); traverse_string(head);/*prints d c b a 0 z y*/ my_char('\n'); add_node_at(new_node("something", NULL), NULL, 7);/*if this line doesn't segfault then we're good!*/ my_str("---------------------------------------------------------------------\n"); /*remove_node*/ my_str(remove_node(&head));/*prints d*/ my_str(": "); traverse_string(head); my_char('\n'); if(!remove_node(NULL)) my_str("remove node from NULL ok\n"); else my_str("remove node from NULL FAIL!\n"); my_str("---------------------------------------------------------------------\n"); /*remove_node_at*/ my_str(remove_node_at(&head, 0));/*prints c*/ my_str(": "); traverse_string(head); my_char('\n'); my_str(remove_node_at(&head, 42));/*prints y*/ my_str(": "); traverse_string(head); my_char('\n'); my_str(remove_node_at(&head, 2));/*prints 0*/ my_str(": "); traverse_string(head); my_char('\n'); if(!remove_node_at(NULL, 100)) my_str("remove node from NULL ok\n"); else my_str("remove node from NULL FAIL!\n"); my_str("---------------------------------------------------------------------\n"); /*remove_last*/ my_str(remove_last(&head));/*prints z*/ my_str(": "); traverse_string(head); my_char('\n'); if(!remove_last(NULL)) my_str("remove last from NULL ok\n"); else my_str("remove last from NULL FAIL!\n"); my_str("---------------------------------------------------------------------\n"); /*count_nodes*/ my_int(count_nodes(head));/*prints 2*/ my_char('\n'); my_int(count_nodes(NULL));/*prints 0*/ my_char('\n'); my_str("---------------------------------------------------------------------\n"); /*node_at*/ my_str((node_at(head, 1))->elem);/*prints a*/ my_char('\n'); my_str((node_at(head, 0))->elem);/*prints b*/ my_char('\n'); my_str((node_at(head, 42))->elem);/*prints a*/ my_char('\n'); if(!node_at(NULL, 12)) my_str("node at with NULL ok\n"); else my_str("node at with NULL FAIL!\n"); my_str("---------------------------------------------------------------------\n"); /*elem_at*/ my_str(elem_at(head, 0));/*prints b*/ my_char('\n'); my_str(elem_at(head, 1));/*prints a*/ my_char('\n'); my_str(elem_at(head, 42));/*prints a*/ my_char('\n'); if(!elem_at(NULL, 3)) my_str("elem at with NULL ok\n"); else my_str("elem at with NULL FAIL!\n"); my_str("---------------------------------------------------------------------\n"); /*empty_list*/ my_int(count_nodes(head));/*prints 2*/ my_char('\n'); empty_list(&head); my_int(count_nodes(head));/*prints 0*/ my_char('\n'); empty_list(NULL);/*if this doesn't segfault then we're good!*/ my_str("---------------------------------------------------------------------\n"); free(head); return 0; }
void base_node_state::on_list(clsm::ev_cmd< list_cmd > const& cmd) { auto const& editor = context< paramtree_editor >(); auto acc = editor.acc; qualified_path path; param_tree::param_data pd; if(cmd.nav) { path = nav_cmd_path(*cmd.nav); if(!path) { return;// todo: false; } pd = acc.find_param(path); } else { path = acc.where(); pd = acc.param_here(); } auto node = acc.node_at(path); unsigned int depth = 0; unsigned int max_depth = cmd.depth ? cmd.depth->value : 1u; unsigned int indent = 0; auto pre_op = [&](param_tree::tree_t::node_descriptor n) { if(depth <= max_depth) { auto& pt = editor.ptree; auto& tree = pt.tree(); std::stringstream output; for(unsigned int i = 0; i < indent; ++i) { output << " "; } auto sibling_idx = tree.position_in_siblings(n); if(depth == 1) { // Index immediate children output << (sibling_idx + 1) << ". "; indent += 3; } output << tree[n].name; auto in = tree.in_edge(n).first; if(tree[in].repeat_idx) { output << " /" << (sibling_idx + 1); } output << ": "; if(is_leaf_type(tree[n].type)) { output << format_value(*tree[n].value); } if(cmd.type_info) { output << " "; output << format_type_info(tree[n]); output << " "; auto path = pt.node_qpath(n); auto acc_copy = acc; acc_copy.move_to(path); output << format_constraints((*editor.provider)[path.unindexed()](acc_copy)); } editor.output(output.str()); } ++depth; indent += 2; }; auto post_op = [&](param_tree::tree_t::node_descriptor n) { --depth; indent -= 2; if(depth == 1) { indent -= 3; } }; wb::gtree::depth_first_traversal( editor.ptree.tree(), node, pre_op, wb::gtree::null_op{}, post_op ); }
T& LinkedListIterator<T>::operator[](size_t pos) { return node_at(pos)->data; }
LinkedListIterator<T>& LinkedListIterator<T>::operator-=(size_t pos) { node = node_at(-pos); return *this; }
LinkedListIterator<T> LinkedListIterator<T>::operator-(size_t pos) const { return LinkedListIterator(node_at(-pos)); }