struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q)
{
    if (isAncestor(root->left, p) && isAncestor(root->left, q))
        return lowestCommonAncestor(root->left, p, q);
    else if (isAncestor(root->right, p) && isAncestor(root->right, q))
        return lowestCommonAncestor(root->right, p, q);
    else
        return root;
}
bool isAncestor(struct TreeNode* node, struct TreeNode* t)
{
    if (!node)
        return false;
    else if (node->val == t->val)
        return true;
    else if ((node->left && node->left->val == t->val) || (node->right && node->right->val == t->val))
        return true;
    else
        return isAncestor(node->left, t) || isAncestor(node->right, t);
}
Beispiel #3
0
bool Person::isAncestor(Person * descendant)
   {
   if (traverse > descendant->traverse)
      return false;

   if (serial == descendant->serial)
      return true;

   if (descendant->isFounder())
      return false;

   return (isAncestor(descendant->mother) ||
           isAncestor(descendant->father));
   }
Beispiel #4
0
static LOOP *nearestAncestor(LOOP *l1, LOOP *l2)
{
    if (!l1)
        return l2;
    if (!l2)
        return l1;
    if (l1 == l2 || isAncestor(l2, l1))
        return l2;
    while (!isAncestor(l1, l2))
    {
        l1 = l1->parent;
    }
    return l1;
}
Beispiel #5
0
std::vector<Node_T*> filterUniqueParents(std::vector<Node_T*>& nodes)
{
  std::vector<Node_T*> cleaned_nodes;

  ossia::sort(nodes);
  nodes.erase(ossia::unique(nodes), nodes.end());

  cleaned_nodes.reserve(nodes.size());

  // Only copy the index if it none of its parents
  // except the invisible root are in the list.
  for (auto n : nodes)
  {
    if (ossia::any_of(nodes, [&](Node_T* other) {
          if (other == n)
            return false;
          return isAncestor(*other, n);
        }))
    {
      continue;
    }
    else
    {
      cleaned_nodes.push_back(n);
    }
  }

  return cleaned_nodes;
}
void Layer::localToAncestor(const Layer* ancestor, SkMatrix* matrix) const {
    if (this == ancestor) {
        matrix->setIdentity();
        return;
    }

    getLocalTransform(matrix);

    // Fixed position layers simply use the root layer's transform.
    if (shouldInheritFromRootTransform()) {
        ASSERT(!ancestor);
        matrix->postConcat(getRootLayer()->getMatrix());
        return;
    }

    // Apply the local and child transforms for every layer between this layer
    // and ancestor.
    ASSERT(isAncestor(ancestor));
    for (const Layer* layer = this->fParent; layer != ancestor; layer = layer->fParent) {
        SkMatrix tmp;
        layer->getLocalTransform(&tmp);
        tmp.preConcat(layer->getChildrenMatrix());
        matrix->postConcat(tmp);
    }

    // If ancestor is not the root layer, apply its child transformation too.
    if (ancestor)
        matrix->postConcat(ancestor->getChildrenMatrix());
}
Beispiel #7
0
int mrca(int* & P,int i,int j){
    int c = i;
    while (!isAncestor(P,c,j)){
          c=P[c];
    }    
    return c;
}
void Item::add( ItemPtr item )
{
	if( isAncestor( item.getObject() ) )
		throw 1;
	subitems_.push_back( item );
	item->addParent( this );
}
Beispiel #9
0
int mrca(Node** nodes,list<int> taxa){

    int t=taxa.front();

    taxa.pop_front();

    bool flag = false;

    while (!flag && nodes[t]->P!=-1){

        t = nodes[t]->P;

        flag=true;

        for (list<int>::iterator ia=taxa.begin();ia!=taxa.end();ia++){

            int j=*ia;

            if (!isAncestor(nodes,t,j)) {flag=false;break;}

        }

    }

    return t;

}
Beispiel #10
0
int mrca(Node** nodes,int i,int j){
    int c = i;
    while (!isAncestor(nodes,c,j)){
        c=nodes[c]->P;
    }
    return c;
}
int getLca(int a, int b)
{
	if (isAncestor(a, b))
		return b;
	else if (isAncestor(b, a))
		return a;
		
	int node = a, lvl = 18;
	while (!isAncestor(b, ancestors[node][0])) {
		while (isAncestor(b, ancestors[node][lvl]))
			lvl--;
		
		node = ancestors[node][lvl];
	}
	
	return ancestors[node][0];
}
//----------------------------------------------------------------------------//
bool ButtonBase::calculateCurrentHoverState(const glm::vec2& cursor_pos)
{
	if (const Window* capture_wnd = getCaptureWindow())
        return
            (capture_wnd == this ||
            (capture_wnd->distributesCapturedInputs() && isAncestor(capture_wnd))) && isHit(cursor_pos);
    else
	    return getGUIContext().getWindowContainingCursor() == this;
}
int main()
{
	scanf("%d%d", &n, &m);
	
	for (int i = 0, a, b; i < n-1; i++) {
		scanf("%d%d", &a, &b);
		
		adjList[a].push_back(b);
		adjList[b].push_back(a);
	}
	
	ancestors[0][0] = 0;
	setOrders(0);
	
	for (int lvl = 1; lvl < 19; lvl++)
		for (int i = 0; i < n; i++)
			ancestors[i][lvl] = ancestors[ ancestors[i][lvl-1] ][lvl - 1];
	
	int a, b, k;
	while (m--) {
		scanf("%d%d%d", &a, &b, &k);
		
		if (isAncestor(a, b)) {
			traffic[a] += k;
			traffic[b] -= k;
		}
		else if (isAncestor(b, a)) {
			traffic[b] += k;
			traffic[a] -= k;
		}
		else {
			traffic[a] += k;
			traffic[b] += k;
			traffic[getLca(a, b)] -= 2*k;
		}
	}
	
	setMaxTraffic(0);
	
	printf("%lld\n", maxTraffic);
	return 0;
}
Beispiel #14
0
bool isAncestor(const Node_T& gramps, const Node_T* node)
{
  auto parent = node->parent();
  if (!parent)
    return false;

  if (node == &gramps)
    return true;

  return isAncestor(gramps, parent);
}
/*! \reimp */
QAccessibleInterface *QAccessibleWidget::focusChild() const
{
    if (widget()->hasFocus())
        return QAccessible::queryAccessibleInterface(object());

    QWidget *fw = widget()->focusWidget();
    if (!fw)
        return 0;

    if (isAncestor(widget(), fw) || fw == widget())
        return QAccessible::queryAccessibleInterface(fw);
    return 0;
}
Beispiel #16
0
Node* findCommonAncestor(Node* root, Node* node1, Node* node2)
{
  Node* cursor = root;

  while (cursor) {
    if (cursor->left) {
      if (isAncestor(cursor->left, node1) && isAncestor(cursor->left, node2)) {
        cursor = cursor->left;
        continue;
      }
    }
    
    if (cursor->right) {
      if (isAncestor(cursor->right, node1) && isAncestor(cursor->right, node2)) {
        cursor = cursor->right;
        continue;
      }
    }

    return cursor;
  }
}
Beispiel #17
0
static void CalculateLoopInvariants(BLOCK *b)
{
    BLOCKLIST *children = b->dominates;
    QUAD *head;
    int i;
    if (b == blockArray[0])
        for (i=0; i < tempCount; i++)
            tempInfo[i]->variantLoop = 0;
    /* again the PHI nodes are first, which satisfies the algorithm need
     * to evaluate them first
     */
    head = b->head;
    while (head != b->tail->fwd)
    {
        if (head->dc.opcode == i_phi)
        {
            PHIDATA *pd = head->dc.v.phi;
            tempInfo[pd->T0]->variantLoop = b->loopParent;
        }
        else if ((head->temps & TEMP_ANS) && head->ans->mode == i_direct)
        { 
            LOOP *varying = b->loopParent;
            LOOP *t_varying ;
            int tnum;
            if ((head->temps & TEMP_LEFT) && head->dc.left->mode == i_direct)
            {
                tnum = head->dc.left->offset->v.sp->value.i;
                t_varying = nearestAncestor(tempInfo[tnum]->variantLoop, b->loopParent);
                varying = t_varying;
            }
            if ((head->temps & TEMP_RIGHT) && head->dc.right->mode == i_direct)
            {
                tnum = head->dc.right->offset->v.sp->value.i;
                t_varying = nearestAncestor(tempInfo[tnum]->variantLoop, b->loopParent);
                // if t_varying is a more inner level than varying, we need to choose
                // t_varying because we need the innermost loop
                if (isAncestor(varying, t_varying))
                    varying = t_varying;
            }
            tnum = head->ans->offset->v.sp->value.i;
            tempInfo[tnum]->variantLoop = varying;
        }
        head = head->fwd;
    }
    while (children)
    {
        CalculateLoopInvariants(children->block);
        children = children->next;
    }
}
Beispiel #18
0
int mrca(int* & P,list<int> taxa){
    int t=taxa.front();
    taxa.pop_front();
    bool flag = false;
    while (!flag && P[t]!=-1){
          t = P[t];
          flag=true;
          for (list<int>::iterator ia=taxa.begin();ia!=taxa.end();ia++){
              int j=*ia;
              if (!isAncestor(P,t,j)) {flag=false;break;}
          }
    }       
    return t;
}
Beispiel #19
0
int mrca(Node** nodes,vector<int> taxa){
    int first=taxa.front();
    int t=first;
    bool flag = false;
    while (!flag && nodes[t]->P!=-1){
        t = nodes[t]->P;
        flag=true;
        for (vector<int>::iterator ia=taxa.begin();ia!=taxa.end();ia++){
            int j=*ia;
            if (j!=first && !isAncestor(nodes,t,j)) {flag=false;break;}
        }
    }
    return t;
}
Beispiel #20
0
VFactory::VMetaObjectList VFactory::getMetaObjectsByAncestorClassName(QString ancestorClassName)
{
  VMetaObjectList res;
  for (VHierachyMap::iterator it = hierachyMap.begin(); it != hierachyMap.end(); it++)
  {
    const QMetaObject* mobj = it.key();
    if (mobj == NULL) continue;
    if (isAncestor(mobj, ancestorClassName))
    {
      VMetaObjectList& mobjList = it.value();
      res += mobjList;
    }
  }
  return res;
}
 bool Object::isAncestor(const Object* object) const
 {
   if (this == object)
   {
     return true ;
   }
   else if (object->getParent() == NULL)
   {
     return false ;
   }
   else
   {
     return isAncestor(object->getParent()) ;
   }
 }
Beispiel #22
0
/** Adds a group to the list of \ref children of this group. It will first be detached from its previous parent group if necessary. No child id can be shared amongst several children of the same group. */
void Group::addChild(shared_ptr< Group > child) {
  // Bouml preserved body begin 0001F9E5
    if (!child)
        throw Exception("Couldn't add child, it's uninitialized.");
    shared_ptr<Group> t = shared_from_this();
    if (t == child)
        throw Exception("Couldn't add child to itself.");
    if (isChild(child))
        throw Exception("Couldn't add child, it's already a child.");
    if (isAncestor(child))
        throw Exception("Couldn't add child, it must not be any ancestor of this group.");
    if (isChild(child->getId()))
        throw Exception("Couldn't add child, its id is already in use by another child.");

    child->setParent(t);
    children.push_back(child);
  // Bouml preserved body end 0001F9E5
}
Beispiel #23
0
void extrait_outgroup(string inFile,string outFile,list<string> &outgroups,int nb){
     int n;//the number of internal nodes
	 int m;//the number of branches
     int n1,m1;
    counting(inFile,n,m,nb);
    int *P = new int[m+1];//tableau de predecesseurs
    double *B = new double[m+1];//branch lengths
    string *Support = new string[m+1];
    string* Labels = new string[m+1];
	FILE * tree = fopen(inFile.c_str(),"rt");
	if (tree==NULL) cout<<"Can not open the tree file"<<endl;
	else{
         FILE * w = fopen(outFile.c_str(),"wt");
         bool rooted=false;
         for (int y=1;y<=nb;y++){
             if (rooted){
                n++;
                m++;
             }
             tree2dataS(tree,n,m,P,B,Support,Labels);
             int * Suc1= new int[n];
             int * Suc2= new int[n];
             computeSuc(P,Suc1,Suc2,m+1,n);
             list<int> P1;
             list<double> B1;
             list<string> Support1;
             list<string> Labels1;
             list<double> T1;
             int s;
             if (m==2*n){
                rooted=true;
                rooted2unrooted(n,m,P,Suc1,Suc2,B,Labels);
                m--;
                n--;
                computeSuc(P,Suc1,Suc2,m+1,n);
             }
             s= computeSuc_unrooted(P,Suc1,Suc2,m+1,n);
             list<int> out;
             list<int> in;
             for (int i=n;i<=m;i++){
                 bool flag = true;
                 for (list<string>::iterator iter=outgroups.begin();iter!=outgroups.end();iter++){
		             if (Labels[i].compare(*iter)==0){
                        out.push_back(i);                   
                        flag=false;
                        break;
                     }
                 }
                 if (flag) in.push_back(i);
             }
             if (out.size()==0){
                cout<<"Tree "<<y<<": the tree does not contain any outgroup"<<endl;  
                exit( EXIT_SUCCESS );
             }
             else{
                 int *P_new= new int[m+1];
                 int *Suc1_new= new int[n];
                 int *Suc2_new= new int[n];
                 double *B_new= new double[m+1];
                 string* Support_new = new string[m+1];
                 int t=out.front();
                 int t1=in.front();
                  if (out.size()==1){
                     reroot(n,m,t,P,s,Suc1,Suc2,B,Support,P_new,Suc1_new,Suc2_new,B_new,Support_new);
                     subTree(n,m,P[t],P_new,Suc1_new,Suc2_new,B_new,Support_new,Labels,P1,B1,Support1,Labels1);
                     int *Po=new int[m];
                     listToArray(P1,Po);
                     double *Bo=new double[m];
                     listToArray(B1,Bo);
                     string* Supporto=new string[m];
                     listToArray(Support1,Supporto);
                     string* Labelso = new string[m];
                     listToArray(Labels1,Labelso);
                     int *Suc1o=new int[n];
                     int *Suc2o=new int[n];
                     computeSuc(Po,Suc1o,Suc2o,m,n);
                     newicktree(n,Po,Suc1o,Suc2o,Labelso,Bo,Supporto,w);
                      delete[]  Po;
                      delete[]  Bo;
                      delete[] Labelso;
                      delete[] Suc1o;
                      delete[] Suc2o;
                      delete[] Supporto;
                  }
                  else{
	                   bool flag = false;
	                   while (!flag && P[t]!=-1){
                             t = P[t];
                              flag=true;
                              for (list<int>::iterator ia=out.begin();ia!=out.end();ia++){
                                  int j=*ia; 
                                  if (!isAncestor(P,t,j)) {
                                     flag=false;break;
                                  }
                              }
                       }//t is lca of outgroups
                       if (P[t]!=-1){//lca of outgroups is not the root
                          reroot(n,m,t,P,s,Suc1,Suc2,B,Support,P_new,Suc1_new,Suc2_new,B_new,Support_new);
                          subTree(n,m,P[t],P_new,Suc1_new,Suc2_new,B_new,Support_new,Labels,P1,B1,Support1,Labels1);
                       }
                       else{//lca of outgroups is the root
	                        bool flag = false;
	                        while (!flag && P[t1]!=-1){
                                  t1 = P[t1];
                                  flag=true;
                                  for (list<int>::iterator ia=in.begin();ia!=in.end();ia++){
                                      int j=*ia;
                                      if (!isAncestor(P,t1,j)) {flag=false;break;}
                                  }
                            }
                           //t1 is lca of ingroups
                            if (P[t1]==-1){//lca of ingroups is the root
                               cout<<"Tree "<<y<<": The outgroups are not separated from the ingroups"<<endl; 
                               exit( EXIT_FAILURE );
                            }
                            else{//lca of ingroups is not the root
                                reroot(n,m,t1,P,s,Suc1,Suc2,B,Support,P_new,Suc1_new,Suc2_new,B_new,Support_new);
                                subTree(n,m,t1,P_new,Suc1_new,Suc2_new,B_new,Support_new,Labels,P1,B1,Support1,Labels1);
                          }
                      }
                      m1=(int)P1.size()-1;
                      n1=m1/2;
                      if ((n1+out.size())==n+1){
                              int *Po = new int[m1+1];
                              listToArray(P1,Po);
                              double *Bo = new double[m1+1];
                              listToArray(B1,Bo);
                              string* Supporto = new string[m1+1];
                              listToArray(Support1,Supporto);
                              string* Labelso = new string[m1+1];
                              listToArray(Labels1,Labelso);
                              int *Suc1o = new int[n1];
                              int *Suc2o = new int[n1];
                              computeSuc(Po,Suc1o,Suc2o,m1+1,n1);
                              newicktree(n1,Po,Suc1o,Suc2o,Labelso,Bo,Supporto,w);
                              delete[]  Po;
                              delete[]  Bo;
                              delete[]  Supporto;
                              delete[] Labelso;
                              delete[] Suc1o;
                              delete[] Suc2o;
                      }
                      else {
                              cout<<"Tree "<<y<<": The outgroups are not separated from the ingroups"<<endl;
                              exit( EXIT_FAILURE );
                      }
                      delete[] P_new;
                      delete[] Suc1_new;
                      delete[] Suc2_new;
                      delete[] B_new;
                      delete[] Support_new;
                  }
             delete[] Suc1;
             delete[] Suc2;
             }
             
         }
         fclose(tree);
        fclose(w);
    }
    delete[]  P;
    delete[]  B;
    delete[]  Support;
    delete[] Labels;
}
/*! \reimp */
QAccessible::Relation QAccessibleWidget::relationTo(int child,
            const QAccessibleInterface *other, int otherChild) const
{
    Relation relation = Unrelated;
    if (d->asking == this) // recursive call
        return relation;

    QObject *o = other ? other->object() : 0;
    if (!o)
        return relation;

    QWidget *focus = widget()->focusWidget();
    if (object() == focus && isAncestor(o, focus))
        relation |= FocusChild;

    QACConnectionObject *connectionObject = (QACConnectionObject*)object();
    for (int sig = 0; sig < d->primarySignals.count(); ++sig) {
        if (connectionObject->isSender(o, d->primarySignals.at(sig).toAscii())) {
            relation |= Controller;
            break;
        }
    }
    // test for passive relationships.
    // d->asking protects from endless recursion.
    d->asking = this;
    int inverse = other->relationTo(otherChild, this, child);
    d->asking = 0;

    if (inverse & Controller)
        relation |= Controlled;
    if (inverse & Label)
        relation |= Labelled;

    if(o == object()) {
        if (child && !otherChild)
            return relation | Child;
        if (!child && otherChild)
            return relation | Ancestor;
        if (!child && !otherChild)
            return relation | Self;
    }

    QObject *parent = object()->parent();
    if (o == parent)
        return relation | Child;

    if (o->parent() == parent) {
        relation |= Sibling;
        QAccessibleInterface *sibIface = QAccessible::queryAccessibleInterface(o);
        Q_ASSERT(sibIface);
        QRect wg = rect(0);
        QRect sg = sibIface->rect(0);
        if (wg.intersects(sg)) {
            QAccessibleInterface *pIface = 0;
            sibIface->navigate(Ancestor, 1, &pIface);
            if (pIface && !((sibIface->state(0) | state(0)) & Invisible)) {
                int wi = pIface->indexOfChild(this);
                int si = pIface->indexOfChild(sibIface);

                if (wi > si)
                    relation |= QAccessible::Covers;
                else
                    relation |= QAccessible::Covered;
            }
            delete pIface;
        } else {
            QPoint wc = wg.center();
            QPoint sc = sg.center();
            if (wc.x() < sc.x())
                relation |= QAccessible::Left;
            else if(wc.x() > sc.x())
                relation |= QAccessible::Right;
            if (wc.y() < sc.y())
                relation |= QAccessible::Up;
            else if (wc.y() > sc.y())
                relation |= QAccessible::Down;
        }
        delete sibIface;

        return relation;
    }

    if (isAncestor(o, object()))
        return relation | Descendent;
    if (isAncestor(object(), o))
        return relation | Ancestor;

    return relation;
}
/*! \reimp */
int QAccessibleWidget::navigate(RelationFlag relation, int entry,
                                QAccessibleInterface **target) const
{
    if (!target)
        return -1;

    *target = 0;
    QObject *targetObject = 0;

    QWidgetList childList = childWidgets(widget());
    bool complexWidget = childList.size() < childCount();

    switch (relation) {
    // Hierarchical
    case Self:
        targetObject = object();
        break;
    case Child:
        if (complexWidget) {
            if (entry > 0 && entry <= childCount())
                return entry;
            return -1;
        }else {
            if (entry > 0 && childList.size() >= entry)
                targetObject = childList.at(entry - 1);
        }
        break;
    case Ancestor:
        {
            if (entry <= 0)
                return -1;
            targetObject = widget()->parentWidget();
            int i;
            for (i = entry; i > 1 && targetObject; --i)
                targetObject = targetObject->parent();
            if (!targetObject && i == 1)
                targetObject = qApp;
        }
        break;
    case Sibling:
        {
            QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(parentObject());
            if (!iface)
                return -1;

            iface->navigate(Child, entry, target);
            delete iface;
            if (*target)
                return 0;
        }
        break;

    // Geometrical
    case QAccessible::Left:
        if (complexWidget && entry) {
            if (entry < 2 || widget()->height() > widget()->width() + 20) // looks vertical
                return -1;
            return entry - 1;
        }
        // fall through
    case QAccessible::Right:
        if (complexWidget && entry) {
            if (entry >= childCount() || widget()->height() > widget()->width() + 20) // looks vertical
                return -1;
            return entry + 1;
        }
        // fall through
    case QAccessible::Up:
        if (complexWidget && entry) {
            if (entry < 2 || widget()->width() > widget()->height() + 20) // looks horizontal
                return - 1;
            return entry - 1;
        }
        // fall through
    case QAccessible::Down:
        if (complexWidget && entry) {
            if (entry >= childCount() || widget()->width() > widget()->height()  + 20) // looks horizontal
                return - 1;
            return entry + 1;
        } else {
            QAccessibleInterface *pIface = QAccessible::queryAccessibleInterface(parentObject());
            if (!pIface)
                return -1;

            QRect startg = rect(0);
            QPoint startc = startg.center();
            QAccessibleInterface *candidate = 0;
            int mindist = 100000;
            int sibCount = pIface->childCount();
            for (int i = 0; i < sibCount; ++i) {
                QAccessibleInterface *sibling = 0;
                pIface->navigate(Child, i+1, &sibling);
                Q_ASSERT(sibling);
                if ((relationTo(0, sibling, 0) & Self) || (sibling->state(0) & QAccessible::Invisible)) {
                    //ignore ourself and invisible siblings
                    delete sibling;
                    continue;
                }

                QRect sibg = sibling->rect(0);
                QPoint sibc = sibg.center();
                QPoint sibp;
                QPoint startp;
                QPoint distp;
                switch (relation) {
                case QAccessible::Left:
                    startp = QPoint(startg.left(), startg.top() + startg.height() / 2);
                    sibp = QPoint(sibg.right(), sibg.top() + sibg.height() / 2);
                    if (QPoint(sibc - startc).x() >= 0) {
                        delete sibling;
                        continue;
                    }
                    distp = sibp - startp;
                    break;
                case QAccessible::Right:
                    startp = QPoint(startg.right(), startg.top() + startg.height() / 2);
                    sibp = QPoint(sibg.left(), sibg.top() + sibg.height() / 2);
                    if (QPoint(sibc - startc).x() <= 0) {
                        delete sibling;
                        continue;
                    }
                    distp = sibp - startp;
                    break;
                case QAccessible::Up:
                    startp = QPoint(startg.left() + startg.width() / 2, startg.top());
                    sibp = QPoint(sibg.left() + sibg.width() / 2, sibg.bottom());
                    if (QPoint(sibc - startc).y() >= 0) {
                        delete sibling;
                        continue;
                    }
                    distp = sibp - startp;
                    break;
                case QAccessible::Down:
                    startp = QPoint(startg.left() + startg.width() / 2, startg.bottom());
                    sibp = QPoint(sibg.left() + sibg.width() / 2, sibg.top());
                    if (QPoint(sibc - startc).y() <= 0) {
                        delete sibling;
                        continue;
                    }
                    distp = sibp - startp;
                    break;
		default:
		    break;
                }

                int dist = (int)qSqrt((qreal)distp.x() * distp.x() + distp.y() * distp.y());
                if (dist < mindist) {
                    delete candidate;
                    candidate = sibling;
                    mindist = dist;
                } else {
                    delete sibling;
                }
            }
            delete pIface;
            *target = candidate;
            if (*target)
                return 0;
        }
        break;
    case Covers:
        if (entry > 0) {
            QAccessibleInterface *pIface = QAccessible::queryAccessibleInterface(parentObject());
            if (!pIface)
                return -1;

            QRect r = rect(0);
            int sibCount = pIface->childCount();
            QAccessibleInterface *sibling = 0;
            for (int i = pIface->indexOfChild(this) + 1; i <= sibCount && entry; ++i) {
                pIface->navigate(Child, i, &sibling);
                if (!sibling || (sibling->state(0) & Invisible)) {
                    delete sibling;
                    sibling = 0;
                    continue;
                }
                if (sibling->rect(0).intersects(r))
                    --entry;
                if (!entry)
                    break;
                delete sibling;
                sibling = 0;
            }
            delete pIface;
            *target = sibling;
            if (*target)
                return 0;
        }
        break;
    case Covered:
        if (entry > 0) {
            QAccessibleInterface *pIface = QAccessible::queryAccessibleInterface(parentObject());
            if (!pIface)
                return -1;

            QRect r = rect(0);
            int index = pIface->indexOfChild(this);
            QAccessibleInterface *sibling = 0;
            for (int i = 1; i < index && entry; ++i) {
                pIface->navigate(Child, i, &sibling);
                Q_ASSERT(sibling);
                if (!sibling || (sibling->state(0) & Invisible)) {
                    delete sibling;
                    sibling = 0;
                    continue;
                }
                if (sibling->rect(0).intersects(r))
                    --entry;
                if (!entry)
                    break;
                delete sibling;
                sibling = 0;
            }
            delete pIface;
            *target = sibling;
            if (*target)
                return 0;
        }
        break;

    // Logical
    case FocusChild:
        {
            if (widget()->hasFocus()) {
                targetObject = object();
                break;
            }

            QWidget *fw = widget()->focusWidget();
            if (!fw)
                return -1;

            if (isAncestor(widget(), fw) || fw == widget())
                targetObject = fw;
            /* ###
            QWidget *parent = fw;
            while (parent && !targetObject) {
                parent = parent->parentWidget();
                if (parent == widget())
                    targetObject = fw;
            }
            */
        }
        break;
    case Label:
        if (entry > 0) {
            QAccessibleInterface *pIface = QAccessible::queryAccessibleInterface(parentObject());
            if (!pIface)
                return -1;

            // first check for all siblings that are labels to us
            // ideally we would go through all objects and check, but that
            // will be too expensive
            int sibCount = pIface->childCount();
            QAccessibleInterface *candidate = 0;
            for (int i = 0; i < sibCount && entry; ++i) {
                const int childId = pIface->navigate(Child, i+1, &candidate);
                Q_ASSERT(childId >= 0);
                if (childId > 0)
                    candidate = pIface;
                if (candidate->relationTo(childId, this, 0) & Label)
                    --entry;
                if (!entry)
                    break;
                if (candidate != pIface)
                    delete candidate;
                candidate = 0;
            }
            if (!candidate) {
                if (pIface->relationTo(0, this, 0) & Label)
                    --entry;
                if (!entry)
                    candidate = pIface;
            }
            if (pIface != candidate)
                delete pIface;

            *target = candidate;
            if (*target)
                return 0;
        }
        break;
    case Labelled: // only implemented in subclasses
        break;
    case Controller:
        if (entry > 0) {
            // check all senders we are connected to,
            // and figure out which one are controllers to us
            QACConnectionObject *connectionObject = (QACConnectionObject*)object();
            QObjectList allSenders = connectionObject->senderList();
            QObjectList senders;
            for (int s = 0; s < allSenders.size(); ++s) {
                QObject *sender = allSenders.at(s);
                QAccessibleInterface *candidate = QAccessible::queryAccessibleInterface(sender);
                if (!candidate)
                    continue;
                if (candidate->relationTo(0, this, 0)&Controller)
                    senders << sender;
                delete candidate;
            }
            if (entry <= senders.size())
                targetObject = senders.at(entry-1);
        }
        break;
    case Controlled:
        if (entry > 0) {
            QObjectList allReceivers;
            QACConnectionObject *connectionObject = (QACConnectionObject*)object();
            for (int sig = 0; sig < d->primarySignals.count(); ++sig) {
                QObjectList receivers = connectionObject->receiverList(d->primarySignals.at(sig).toAscii());
                allReceivers += receivers;
            }
            if (entry <= allReceivers.size())
                targetObject = allReceivers.at(entry-1);
        }
        break;
    default:
        break;
    }

    *target = QAccessible::queryAccessibleInterface(targetObject);
    return *target ? 0 : -1;
}
Beispiel #26
0
int main(int argc, char** argv) {
	
	BinaryTree tree;
	
	 if (argc != 3) {
                std::cout << "Invalid number of arguments.\nUsage: " << argv[0] << " <inputFile> <outFile>\n";
                return 1;
        }

	tree.readFile(argv[1]);
	tree.DFS();
	tree.gvout(argv[2]);
	
	cout<<"is 10 anc 23 "<<isAncestor(tree,10,23)<<endl;
	cout<<"is 10 anc 40 "<<isAncestor(tree,10,40)<<endl;
	cout<<"is 40 anc 10 "<<isAncestor(tree,40,10)<<endl;
	cout<<"is 2 anc 47 "<<isAncestor(tree,2,47)<<endl;
	cout<<"is 1 anc 160 "<<isAncestor(tree,1,160)<<endl;
	cout<<"is 160 anc 1 "<<isAncestor(tree,160,1)<<endl;
	cout<<"is 160 anc 95 "<<isAncestor(tree,160,95)<<endl;
	cout<<"is 5 anc 11 "<<isAncestor(tree,5,11)<<endl;
	cout<<"is 11 anc 5 "<<isAncestor(tree,11,5)<<endl;

	cout << "LCA of 1 and 160 is " << LCA(tree,1,160) << endl;
	cout << "LCA of 160 and 1 is " << LCA(tree,160,1) << endl;
	cout << "LCA of 160 and 95 is " << LCA(tree,160,95) << endl;
	cout << "LCA of 95 and 160 is " << LCA(tree,95,160) << endl;
	cout << "LCA of 2 and 2 is " << LCA(tree,2,2) << endl;
	cout << "LCA of 5 and 11 is " << LCA(tree,5,11) << endl;
	cout << "LCA of 11 and 5 is " << LCA(tree,11,5) << endl;
	cout << "LCA of 160 and 3 is " << LCA(tree,160,3) << endl;
	cout << "LCA of 10 and 11 is " << LCA(tree,10,11) << endl;

	cout << "Size of tree = " << subTreeSize(tree,1) << endl;
	cout << "Size of subtree at 160 = " << subTreeSize(tree,160) << endl;
	cout << "Size of subtree at 95 = " << subTreeSize(tree,95) << endl;
	cout << "Size of subtree at 5 = " << subTreeSize(tree,5) << endl;
	cout << "Size of subtree at 2 = " << subTreeSize(tree,2) << endl;

	cout << "Balance = " << isBalanced(tree, 1) << endl;
	cout << "Balance at 160 = " << isBalanced(tree, 160) << endl;
	cout << "Balance at 80 = " << isBalanced(tree, 80) << endl;
	cout << "Balance at 40 = " << isBalanced(tree, 40) << endl;
	cout << "Balance at 5 = " << isBalanced(tree, 5) << endl;
	cout << "Balance at 2 = " << isBalanced(tree, 2) << endl;
	cout << "Balance at 3 = " << isBalanced(tree, 3) << endl;

	/*
	cout << "LCA of 2 and 6 is " << LCA(tree,2,6) << endl;
	cout << "LCA of 2 and 4 is " << LCA(tree,2,4) << endl;
	cout << "LCA of 2 and 2 is " << LCA(tree,2,2) << endl;
	cout << "LCA of 7 and 4 is " << LCA(tree,7,4) << endl;
	
	cout << "Balance = " << isBalanced(tree, 1) << endl;
	cout << "Balance at 8= " << isBalanced(tree, 8) << endl;
	cout << "Balance at 4 = " << isBalanced(tree, 4) << endl;
	cout << "Balance at 3 = " << isBalanced(tree, 3) << endl;
	cout << "Balance at 2 = " << isBalanced(tree, 2) << endl;
	
	cout << "Size of tree = " << subTreeSize(tree,1) << endl;
	cout << "Size of subtree at 2 = " << subTreeSize(tree,2) << endl;
	cout << "Size of subtree at 3 = " << subTreeSize(tree,3) << endl;
	cout << "Size of subtree at 8 = " << subTreeSize(tree,8) << endl;
	*/
	cout <<"Diameter of tree =" << diameter(tree) <<endl;
	return 0;
}
void QQuickScenePosListener::itemChildRemoved(QQuickItem *, QQuickItem *child)
{
    if (isAncestor(child))
        removeAncestorListeners(child);
}