RoseAst::iterator& RoseAst::iterator::operator++() { // check if we are already past the end if(is_past_the_end()) return *this; assert(_stack.size()>0); stack_element e=_stack.top(); _stack.pop(); SgNode* new_node=0; if(e.index==ROOT_NODE_INDEX) { if(num_children(e.node)==0) return *this; else { new_node=e.node; goto root_node_has_children; } }; // check if we are at a null node (nothing to push and nothing to skip) if(e.node==0) { return *this; } new_node=access_node_by_parent_and_index(e.node,e.index); root_node_has_children: if(new_node==0) { return *this; } if(!_skipChildrenOnForward) { int numChildren=num_children(new_node); // we need to push children in reverse order on the stack (if they are != null) for(int new_index=numChildren-1;new_index>=0;new_index--) { stack_element new_e; new_e.node=new_node; new_e.index=new_index; if(new_e.node!=0) { if(_withNullValues) { // if we visit null-nodes we can push anything _stack.push(new_e); } else { if(access_node_by_parent_and_index(new_e.node,new_e.index)!=0) { _stack.push(new_e); } else { // we are not visiting null nodes therefore we do not push null nodes on the stack } } } else { // a null node has no children: nothing to do. } } } else { /* we have skipped children (because we do not put them on the stack) since we do this only once, we set the flag back to false */ _skipChildrenOnForward=false; } return *this; }
float BVHNode::computeSubtreeSAHCost(const BVHParams& p, float probability) const { float SAH = probability * p.cost(num_children(), num_triangles()); for(int i = 0; i < num_children(); i++) { BVHNode *child = get_child(i); SAH += child->computeSubtreeSAHCost(p, probability * child->bounds.safe_area()/bounds.safe_area()); } return SAH; }
bool internal_node::equal(const node& other) const { if (other.is_leaf() || category() != other.category()) return false; const auto& internal = other.as<internal_node>(); if (num_children() != internal.num_children()) return false; bool ret = true; for (size_t i = 0; i < num_children(); ++i) ret &= children_[i]->equal(*internal.children_[i]); return ret; }
// If we want to have different word and transition priors, this // will have to change void Synset::Finalize() { finalized_ = true; double trans_count = 0.0; if (num_children() > 0) { trans_count = lib_prob::normalize(transition_prior_.get()); if (trans_count <= 0) gsl_vector_print(transition_prior_.get()); assert(trans_count > 0.0); } double word_count = 0.0; for (int ll = 0; ll < (int)words_.size(); ++ll) { if (words_[ll].size() > 0) { word_count += lib_prob::normalize(emission_prior_[ll].get()); assert(word_count > 0.0); } } // Take care of the choice prior gsl_vector_set(choice_prior_.get(), kTransitionChoice, trans_count); gsl_vector_set(choice_prior_.get(), kEmissionChoice, word_count); // TODO(jbg): This should actually be strictly greater than 0.0, but // we have empty synsets, which wastes memory but isn't (hopefully) // a problem. assert(lib_prob::normalize(choice_prior_.get()) >= 0); }
bool build(inner_node *parent, const size_t depth) { const size_t this_id(num_nodes); if(num_nodes == size) return false; ++num_nodes; if(depth == 0) node[this_id] = new inner_node(this, parent, 0, this_id); else { auto my_node(new inner_node(this, parent, radix, this_id)); node[this_id] = my_node; size_t num_children(0); for(size_t i(0); i < radix; ++i) { if(build(my_node, depth - 1)) num_children++; } if(num_children != radix) my_node->set_count(num_children); } return true; }
void BVHNode::deleteSubtree() { for(int i = 0; i < num_children(); i++) if(get_child(i)) get_child(i)->deleteSubtree(); delete this; }
Node* Node::get_last_child() { if (has_children()) { return child(num_children() - 1)->get_last_child(); } return this; }
expr_ptr insert_child( expr_ptr node_to_insert, Lexer & lex, Lexer::token_type & token ) { auto curr_ptr = node_to_insert; if( curr_ptr ){ for( size_t i = 0; i != curr_ptr->num_children(); ++i ){ update_current_token( lex, token ); node_to_insert = convert_token_to_expression( token ); curr_ptr->set_children( i, insert_child( node_to_insert, lex, token ) ); } } return curr_ptr; }
void Synset::SetPrior(int index, double count) { if (transition_prior_ == NULL) { transition_prior_.reset(gsl_vector_alloc(num_children()), gsl_vector_free); gsl_vector_set_all(transition_prior_.get(), 0.0); } // We can't have a zero count. Such words should be pruned // beforehand. assert(count > 0.0 && !isnan(count)); gsl_vector_set(transition_prior_.get(), index, count); }
SgNode* RoseAst::iterator::access_node_by_parent_and_index(SgNode* p, int index) const { int numChildren=num_children(p); if(index>=numChildren) { std::stringstream ss; ss << "Ast::iterator internal error: memorized index out of bounds (violation: "; ss << index << "<" <<numChildren; ss << ")"; throw std::out_of_range(ss.str()); } SgNode* node=p->get_traversalSuccessorByIndex(index); return node; }
void inspect() { std::cout << num_children() << std::endl; for (auto &e : get_flat_list_of_descendants<Entity3D>()) std::cout << " - " << e << " " << e->get("name") << " " << e->get_id() << std::endl; }
bool Node::has_children() const { return num_children() > 0; }
Node::~Node() { for (int i = 0; i < num_children(); ++i) delete children_[i]; }
bool RoseAst::iterator::is_at_last_child() const { stack_element e=_stack.top(); return e.index==num_children(e.node)-1 && e.index!=ROOT_NODE_INDEX; }
CCL_NAMESPACE_BEGIN /* BVH Node */ int BVHNode::getSubtreeSize(BVH_STAT stat) const { int cnt = 0; switch(stat) { case BVH_STAT_NODE_COUNT: cnt = 1; break; case BVH_STAT_LEAF_COUNT: cnt = is_leaf() ? 1 : 0; break; case BVH_STAT_INNER_COUNT: cnt = is_leaf() ? 0 : 1; break; case BVH_STAT_TRIANGLE_COUNT: cnt = is_leaf() ? reinterpret_cast<const LeafNode*>(this)->num_triangles() : 0; break; case BVH_STAT_CHILDNODE_COUNT: cnt = num_children(); break; case BVH_STAT_QNODE_COUNT: cnt = 1; for(int i = 0; i < num_children(); i++) { BVHNode *node = get_child(i); if(node->is_leaf()) { cnt += 1; } else { for(int j = 0; j < node->num_children(); j++) { cnt += node->get_child(j)->getSubtreeSize(stat); } } } return cnt; case BVH_STAT_ALIGNED_COUNT: if(!is_unaligned) { cnt = 1; } break; case BVH_STAT_UNALIGNED_COUNT: if(is_unaligned) { cnt = 1; } break; case BVH_STAT_ALIGNED_INNER_COUNT: if(!is_leaf()) { bool has_unaligned = false; for(int j = 0; j < num_children(); j++) { has_unaligned |= get_child(j)->is_unaligned; } cnt += has_unaligned? 0: 1; } break; case BVH_STAT_UNALIGNED_INNER_COUNT: if(!is_leaf()) { bool has_unaligned = false; for(int j = 0; j < num_children(); j++) { has_unaligned |= get_child(j)->is_unaligned; } cnt += has_unaligned? 1: 0; } break; case BVH_STAT_ALIGNED_INNER_QNODE_COUNT: { bool has_unaligned = false; for(int i = 0; i < num_children(); i++) { BVHNode *node = get_child(i); if(node->is_leaf()) { has_unaligned |= node->is_unaligned; } else { for(int j = 0; j < node->num_children(); j++) { cnt += node->get_child(j)->getSubtreeSize(stat); has_unaligned |= node->get_child(j)->is_unaligned; } } } cnt += has_unaligned? 0: 1; } return cnt; case BVH_STAT_UNALIGNED_INNER_QNODE_COUNT: { bool has_unaligned = false; for(int i = 0; i < num_children(); i++) { BVHNode *node = get_child(i); if(node->is_leaf()) { has_unaligned |= node->is_unaligned; } else { for(int j = 0; j < node->num_children(); j++) { cnt += node->get_child(j)->getSubtreeSize(stat); has_unaligned |= node->get_child(j)->is_unaligned; } } } cnt += has_unaligned? 1: 0; } return cnt; case BVH_STAT_ALIGNED_LEAF_COUNT: cnt = (is_leaf() && !is_unaligned) ? 1 : 0; break; case BVH_STAT_UNALIGNED_LEAF_COUNT: cnt = (is_leaf() && is_unaligned) ? 1 : 0; break; case BVH_STAT_DEPTH: if(is_leaf()) { cnt = 1; } else { for(int i = 0; i < num_children(); i++) { cnt = max(cnt, get_child(i)->getSubtreeSize(stat)); } cnt += 1; } return cnt; default: assert(0); /* unknown mode */ } if(!is_leaf()) for(int i = 0; i < num_children(); i++) cnt += get_child(i)->getSubtreeSize(stat); return cnt; }
const Dimension& Schema::getDimension(std::string const& t, std::string const& ns) const { schema::index_by_name const& name_index = m_index.get<schema::name>(); schema::index_by_name::const_iterator it = name_index.find(t); schema::index_by_name::size_type count = name_index.count(t); std::ostringstream oss; oss << "Dimension with name '" << t << "' not found, unable to Schema::getDimension"; // FIXME: If there are two dimensions with the same name here, we're // scrwed // std::cout << "finding dimension with name" << t << " and ns: " << ns << std::endl; if (it != name_index.end()) { if (ns.size()) { while (it != name_index.end()) { if (boost::equals(ns, it->getNamespace())) return *it; ++it; } } if (count > 1) { std::pair<schema::index_by_name::const_iterator, schema::index_by_name::const_iterator> ret = name_index.equal_range(t); boost::uint32_t num_parents(0); boost::uint32_t num_children(0); std::map<dimension::id, dimension::id> relationships; // Test to make sure that the number of parent dimensions all with // the same name is equal to only 1. If there are multiple // dimensions with the same name, but no relationships defined, // we are in an error condition for (schema::index_by_name::const_iterator o = ret.first; o != ret.second; ++o) { // Put a map together that maps parents to children that // we are going to walk to find the very last child in the // graph. std::pair<dimension::id, dimension::id> p( o->getParent(), o->getUUID()); relationships.insert(p); // The parent dimension should have a nil parent of its own. // nil_uuid is the default parent of all dimensions as the y // are created if (o->getParent().is_nil()) { num_parents++; } else { num_children++; } } if (num_parents != 1) { std::ostringstream oss; oss << "Schema has multiple dimensions with name '" << t << "', but " "their parent/child relationships are not coherent. Multiple " "parents are present."; throw multiple_parent_dimensions(oss.str()); } dimension::id parent = boost::uuids::nil_uuid(); // Starting at the parent (nil uuid), walk the child/parent graph down to the // end. When we're done finding dimensions, what's left is the child // at the end of the graph. std::map<dimension::id, dimension::id>::const_iterator p = relationships.find(parent); dimension::id child; while (p != relationships.end()) { child = p->second; p = relationships.find(p->second); } schema::index_by_uid::const_iterator pi = m_index.get<schema::uid>().find(child); if (pi != m_index.get<schema::uid>().end()) { return *pi; } else { std::ostringstream errmsg; errmsg << "Unable to fetch subjugate dimension with id '" << child << "' in schema"; throw dimension_not_found(errmsg.str()); } } return *it; } else { boost::uuids::uuid ps1; try { boost::uuids::string_generator gen; ps1 = gen(t); } catch (std::runtime_error&) { // invalid string for uuid throw dimension_not_found(oss.str()); } schema::index_by_uid::const_iterator i = m_index.get<schema::uid>().find(ps1); if (i != m_index.get<schema::uid>().end()) { if (ns.size()) { while (i != m_index.get<schema::uid>().end()) { if (boost::equals(ns, i->getNamespace())) return *i; ++i; } } return *i; } else { oss.str(""); oss << "Dimension with name '" << t << "' not found, unable to Schema::getDimension"; throw dimension_not_found(oss.str()); } } }
std::unique_ptr<node> binarizer::operator()(const internal_node& in) { auto res = make_unique<internal_node>(in.category()); if (in.num_children() <= 2) { in.each_child([&](const node* child) { res->add_child(child->accept(*this)); if (child == in.head_constituent()) res->head(res->child(res->num_children() - 1)); }); return std::move(res); } auto bin_lbl = class_label{static_cast<std::string>(in.category()) + "*"}; // locate head node auto head = in.head_constituent(); if (!head) throw exception{"Head constituent not labeled"}; uint64_t head_idx = 0; for (uint64_t idx = 0; idx < in.num_children(); ++idx) { if (in.child(idx) == in.head_constituent()) head_idx = idx; } // eat left nodes auto curr = res.get(); for (uint64_t idx = 0; idx < head_idx; ++idx) { curr->add_child(in.child(idx)->accept(*this)); // special case: if the head is the very last node, just add the // remaining child (the head) to the current node if (idx + 1 == head_idx && head_idx == in.num_children() - 1) { curr->add_child(in.child(idx + 1)->accept(*this)); curr->head(curr->child(1)); break; } else { auto bin = make_unique<internal_node>(bin_lbl); auto next = bin.get(); curr->add_child(std::move(bin)); curr->head(curr->child(1)); curr = next; } } // eat right nodes for (uint64_t ridx = in.num_children() - 1; ridx > head_idx; --ridx) { // if the head is the next node, just add it and the current child if (head_idx + 1 == ridx) { curr->add_child(in.child(ridx - 1)->accept(*this)); curr->head(curr->child(0)); curr->add_child(in.child(ridx)->accept(*this)); break; } else { auto bin = make_unique<internal_node>(bin_lbl); auto next = bin.get(); curr->add_child(std::move(bin)); curr->head(curr->child(0)); curr->add_child(in.child(ridx)->accept(*this)); curr = next; } } lexicon_populator pop; res->accept(pop); return std::move(res); }
// Const access; returns the __const iterator* associated with // the current leaf. inline const_reference operator*() const { _GLIBCXX_DEBUG_ASSERT(num_children() == 0); return Const_Iterator(m_p_nd); }