コード例 #1
0
ファイル: managed.cpp プロジェクト: JFreaker/boomerang
// Substitute s into all members of the set
void LocationSet::substitute(Assign& a)
{
    Exp* lhs = a.getLeft();
    if (lhs == NULL) return;
    Exp* rhs = a.getRight();
    if (rhs == NULL) return;		// ? Will this ever happen?
    std::set<Exp*, lessExpStar>::iterator it;
    // Note: it's important not to change the pointer in the set of pointers to expressions, without removing and
    // inserting again. Otherwise, the set becomes out of order, and operations such as set comparison fail!
    // To avoid any funny behaviour when iterating the loop, we use the following two sets
    LocationSet removeSet;			// These will be removed after the loop
    LocationSet removeAndDelete;	// These will be removed then deleted
    LocationSet insertSet;			// These will be inserted after the loop
    bool change;
    for (it = lset.begin(); it != lset.end(); it++)
        {
            Exp* loc = *it;
            Exp* replace;
            if (loc->search(lhs, replace))
                {
                    if (rhs->isTerminal())
                        {
                            // This is no longer a location of interest (e.g. %pc)
                            removeSet.insert(loc);
                            continue;
                        }
                    loc = loc->clone()->searchReplaceAll(lhs, rhs, change);
                    if (change)
                        {
                            loc = loc->simplifyArith();
                            loc = loc->simplify();
                            // If the result is no longer a register or memory (e.g.
                            // r[28]-4), then delete this expression and insert any
                            // components it uses (in the example, just r[28])
                            if (!loc->isRegOf() && !loc->isMemOf())
                                {
                                    // Note: can't delete the expression yet, because the
                                    // act of insertion into the remove set requires silent
                                    // calls to the compare function
                                    removeAndDelete.insert(*it);
                                    loc->addUsedLocs(insertSet);
                                    continue;
                                }
                            // Else we just want to replace it
                            // Regardless of whether the top level expression pointer has
                            // changed, remove and insert it from the set of pointers
                            removeSet.insert(*it);		// Note: remove the unmodified ptr
                            insertSet.insert(loc);
                        }
                }
        }
    makeDiff(removeSet);	   // Remove the items to be removed
    makeDiff(removeAndDelete); // These are to be removed as well
    makeUnion(insertSet);	   // Insert the items to be added
    // Now delete the expressions that are no longer needed
    std::set<Exp*, lessExpStar>::iterator dd;
    for (dd = removeAndDelete.lset.begin(); dd != removeAndDelete.lset.end();
            dd++)
        delete *dd;				// Plug that memory leak
}
コード例 #2
0
int main()
{
    long n,r,i,e,M,cs=1;
    long p,q;

    int count[50005];

    while(scanf("%ld%ld",&n,&r))
    {
        if(n==0 && r==0)break;

        for(i=1; i<=n; i++)count[prev[i]=i]=0;
        for(i=1; i<=r; i++)
        {
            scanf("%ld%ld",&p,&q);
            makeUnion(p,q);
        }

        for(i=1; i<=n; i++) count[Parent(i)]++;
        M=0;
        for(i=1; i<=n; i++)
        {
            if(count[i])M++;
        }
        printf("Case %ld: %ld\n",cs++,M);
    }
    return 0;
}
コード例 #3
0
int main() {
	Forest someForest = {{0}, {0}};
	Forest * f = &someForest;
	makeSet(f, 1);
	makeSet(f, 2);
	makeSet(f, 3);
	printf("are 1 and 3 in the same set? %s\n", findSet(f, 1) == findSet(f, 3) ? "yes" : "no");
	printf("makeUnion(f, 1, 3)\n");
	makeUnion(f, 1, 3);
	printf("are 1 and 3 in the same set? %s\n", findSet(f, 1) == findSet(f, 3) ? "yes" : "no");
	printSet(f);
	return 0;
}
コード例 #4
0
ファイル: 599.cpp プロジェクト: Blimeo/Contests
int main() {
    int t, u;
    int cases = 0;
    int i, count[26];
    char letters[100], buffer[100];
    scanf("%d", &cases);
    getchar();

    while (cases--) {
        for (i = 0; i < 26; i++) {
            set[i] = i;
            count[i] = 0;
        }

        while (gets(buffer) && buffer[0] != '*') {
            makeUnion(buffer[1] - 'A', buffer[3] - 'A', set);
        }

        gets(letters);

        for (i = 0; letters[i]; i++) {
            if (letters[i] != ',') {
                count[getParent(letters[i] - 'A', set)]++;
            }
        }

        t = u = 0;

        for (i = 0; i < 26; i++) {
            if (count[i] > 1) {
                t++;
            } else if (count[i] == 1) {
                u++;
            }
        }
        printf("There are %d tree(s) and %d acorn(s).\n", t, u);
    }

    return 0;
}
コード例 #5
0
ファイル: ejsArray.c プロジェクト: embedthis/ejs-1
static EjsVar *invokeArrayOperator(Ejs *ejs, EjsVar *lhs, int opcode,  EjsVar *rhs)
{
    EjsVar      *result;

    if (rhs == 0 || lhs->type != rhs->type) {
        if ((result = coerceArrayOperands(ejs, lhs, opcode, rhs)) != 0) {
            return result;
        }
    }

    switch (opcode) {

    case EJS_OP_COMPARE_EQ: case EJS_OP_COMPARE_STRICTLY_EQ:
    case EJS_OP_COMPARE_LE: case EJS_OP_COMPARE_GE:
        return (EjsVar*) ejsCreateBoolean(ejs, (lhs == rhs));

    case EJS_OP_COMPARE_NE: case EJS_OP_COMPARE_STRICTLY_NE:
    case EJS_OP_COMPARE_LT: case EJS_OP_COMPARE_GT:
        return (EjsVar*) ejsCreateBoolean(ejs, !(lhs == rhs));

    /*
     *  Unary operators
     */
    case EJS_OP_COMPARE_NOT_ZERO:
        return (EjsVar*) ejs->trueValue;

    case EJS_OP_COMPARE_UNDEFINED:
    case EJS_OP_COMPARE_NULL:
    case EJS_OP_COMPARE_FALSE:
    case EJS_OP_COMPARE_TRUE:
    case EJS_OP_COMPARE_ZERO:
        return (EjsVar*) ejs->falseValue;

    case EJS_OP_LOGICAL_NOT: case EJS_OP_NOT: case EJS_OP_NEG:
        return (EjsVar*) ejs->oneValue;

    /*
     *  Binary operators
     */
    case EJS_OP_DIV: case EJS_OP_MUL: case EJS_OP_REM:
    case EJS_OP_SHR: case EJS_OP_USHR: case EJS_OP_XOR:
        return (EjsVar*) ejs->zeroValue;

#if BLD_FEATURE_EJS_LANG >= EJS_SPEC_PLUS
    /*
     *  Operator overload
     */
    case EJS_OP_ADD:
        result = (EjsVar*) ejsCreateArray(ejs, 0);
        pushArray(ejs, (EjsArray*) result, 1, (EjsVar**) &lhs);
        pushArray(ejs, (EjsArray*) result, 1, (EjsVar**) &rhs);
        return result;

    case EJS_OP_AND:
        return (EjsVar*) makeIntersection(ejs, (EjsArray*) lhs, (EjsArray*) rhs);

    case EJS_OP_OR:
        return (EjsVar*) makeUnion(ejs, (EjsArray*) lhs, (EjsArray*) rhs);

    case EJS_OP_SHL:
        return pushArray(ejs, (EjsArray*) lhs, 1, &rhs);

    case EJS_OP_SUB:
        return (EjsVar*) removeArrayElements(ejs, (EjsArray*) lhs, (EjsArray*) rhs);
#endif

    default:
        ejsThrowTypeError(ejs, "Opcode %d not implemented for type %s", opcode, lhs->type->qname.name);
        return 0;
    }

    mprAssert(0);
}
コード例 #6
0
ファイル: ejsArray.c プロジェクト: leemit/ejscript
static EjsAny *invokeArrayOperator(Ejs *ejs, EjsAny *lhs, int opcode, EjsAny *rhs)
{
    EjsAny  *result;

    if (rhs == 0 || TYPE(lhs) != TYPE(rhs)) {
        if ((result = coerceArrayOperands(ejs, lhs, opcode, rhs)) != 0) {
            return result;
        }
    }

    switch (opcode) {

    case EJS_OP_COMPARE_EQ: case EJS_OP_COMPARE_STRICTLY_EQ:
    case EJS_OP_COMPARE_LE: case EJS_OP_COMPARE_GE:
        return ejsCreateBoolean(ejs, (lhs == rhs));

    case EJS_OP_COMPARE_NE: case EJS_OP_COMPARE_STRICTLY_NE:
    case EJS_OP_COMPARE_LT: case EJS_OP_COMPARE_GT:
        return ejsCreateBoolean(ejs, !(lhs == rhs));

    /*
        Unary operators
     */
    case EJS_OP_COMPARE_NOT_ZERO:
        return ESV(true);

    case EJS_OP_COMPARE_UNDEFINED:
    case EJS_OP_COMPARE_NULL:
    case EJS_OP_COMPARE_FALSE:
    case EJS_OP_COMPARE_TRUE:
    case EJS_OP_COMPARE_ZERO:
        return ESV(false);

    case EJS_OP_LOGICAL_NOT: case EJS_OP_NOT: case EJS_OP_NEG:
        return ESV(one);

    /*
        Binary operators
     */
    case EJS_OP_DIV: case EJS_OP_MUL: case EJS_OP_REM:
    case EJS_OP_SHR: case EJS_OP_USHR: case EJS_OP_XOR:
        return ESV(zero);

    /*
        Operator overload
     */
    case EJS_OP_ADD:
        result = ejsCreateArray(ejs, 0);
        pushArray(ejs, result, 1, &lhs);
        pushArray(ejs, result, 1, &rhs);
        return result;

    case EJS_OP_AND:
        return makeIntersection(ejs, lhs, rhs);

    case EJS_OP_OR:
        return makeUnion(ejs, lhs, rhs);

    case EJS_OP_SHL:
        return pushArray(ejs, lhs, 1, &rhs);

    case EJS_OP_SUB:
        return ejsRemoveItems(ejs, lhs, rhs);

    default:
        ejsThrowTypeError(ejs, "Opcode %d not implemented for type %@", opcode, TYPE(lhs)->qname.name);
        return 0;
    }
    assert(0);
}
コード例 #7
0
ファイル: FibHeap.cpp プロジェクト: cran/CRF
FibHeapNode *FibHeap::extractMin()
{
  FibHeapNode *Result;
  FibHeap *m_childHeap = NULL;
  
  // Remove minimum node and set m_min_root to next node
  
  if ((Result = minimum()) == NULL) return NULL;
  
  m_min_root = Result->m_right;
  Result->m_right->m_left = Result->m_left;
  Result->m_left->m_right = Result->m_right;
  Result->m_left = Result->m_right = NULL;
  
  m_num_nodes --;
  if (Result->m_mark) {
    m_num_marked_nodes --;
    Result->m_mark = 0;
  }
  Result->m_degree = 0;
  
  // Attach m_child list of Minimum node to the root list of the heap
  // If there is no m_child list, then do no work
  
  if (Result->m_child == NULL) {
    if (m_min_root == Result) m_min_root = NULL;
  }
  
  // If m_min_root==Result then there was only one root tree, so the
  // root list is simply the m_child list of that node (which is
  // NULL if this is the last node in the list)
  
  else if (m_min_root == Result) {
    m_min_root = Result->m_child;
  }
  
  // If m_min_root is different, then the m_child list is pushed into a
  // new temporary heap, which is then merged by Union() onto the
  // root list of this heap.
  
  else {
    m_childHeap = new FibHeap();
    m_childHeap->m_min_root = Result->m_child;
  }
  
  // Complete the disassociation of the Result node from the heap
  
  if (Result->m_child != NULL)
    Result->m_child->m_parent = NULL;
  Result->m_child = Result->m_parent = NULL;
  
  // If there was a m_child list, then we now merge it with the
  //	rest of the root list
  
  if (m_childHeap) makeUnion(m_childHeap);
  
  // Consolidate heap to find new minimum and do reorganize work
  
  if (m_min_root != NULL) consolidate();
  
  // Return the minimum node, which is now disassociated with the heap
  // It has m_left, m_right, m_parent, m_child, m_mark and m_degree cleared.
  
  return Result;
}
// ---------------------------------------------------------------------------
// COperationSubscribePresentityPresence::ProcessL()
// ---------------------------------------------------------------------------
//
void COperationSubscribePresentityPresence::ProcessL()
    {
    TRACE(_L("COperationSubscribePresentityPresence::ProcessL()" ) );
    CXIMPOperationBase::ProcessL();

    // MatchAddressTo( subscriptions, iIdentity )
    CPresentityPresenceSubscriptionItem& subItem =
                iMyHost->PresenceDataAccess().PresenceDataCache().PresentityPresenceSubscriptionItemLC( *iIdentity );
    CleanupStack::Pop(); // subItem
    iSubItem = &subItem;

    // Get the subscription status
    // Notice that if there is more than one client and clients are using identity
    // which will alternate for request complete, this will affect wrong logic mapping
    // for subscription operation. 
    
    // For example: Client 1 is subscription "name" and server alters it to [email protected]
    // Client 2 subscribes with "name" and it will get subscription status ENotSubscribedAtAll
    // when it should get ESubscribedForOtherCtxOnly.
    iSubscriptionStatus = iSubItem->SubscriptionStatus( iContext );

    MProtocolPresenceWatching& watching = iMyHost->GetConnection().ProtocolPresenceFeatures().PresenceWatching();

    switch( iSubscriptionStatus )
        {
        case CPresentityPresenceSubscriptionItem::ENotSubscribedAtAll:
            {
            TRACE(_L("COperationSubscribePresentityPresence::ProcessL() not subscribed" ) );
            iMergedPif = TXIMPObjectCloner< CPresenceInfoFilterImp >::CloneL( *iPif );
            // If this leaves, subscription is not created
            watching.DoSubscribePresentityPresenceL( iSubItem->Identity(),
                    *iMergedPif,
                    iReqId );
            break;
            }
        case CPresentityPresenceSubscriptionItem::ESubscribedForOtherCtxOnly:
            {
            TRACE(_L("COperationSubscribePresentityPresence::ProcessL() subscribed for other ctx" ) );
            // Aggregate subscription and context pifs
            // Check aggregate and if needed.. update subscription
            iMergedPif = DocumentUtils::InfoFilterUnionL( iSubItem->SubscriptionPif(), *iPif );
            if( iMergedPif->Contains( iSubItem->SubscriptionPif() ) )
                {
                TRACE(_L("COperationSubscribePresentityPresence::ProcessL() DoUpdatePresentityPresenceSubscriptionPifL 1" ) );
                // If this leaves, subscription is not created
                watching.DoUpdatePresentityPresenceSubscriptionPifL( *iIdentity, *iMergedPif, iReqId );
                }
            else
                {
                TRACE(_L("COperationSubscribePresentityPresence::ProcessL() FakeCompleteRequest" ) );
                iMyHost->FakeCompleteRequest( iReqId, KErrNone );
                }
            break;
            }
        case CPresentityPresenceSubscriptionItem::ESubscribedForCtxOnly:
        case CPresentityPresenceSubscriptionItem::ESubscribedForCtxAndOthers:
            {
            TRACE(_L("COperationSubscribePresentityPresence::ProcessL() subscribed" ) );
            CPresenceInfoFilterImp* oldPif = 
                    iContext->PresenceInfoFilter( MXIMPPscContext::EPresentityPresenceFilter, iIdentity );
            CPresenceInfoFilterImp& oldMergedPif = iSubItem->SubscriptionPif();
            TBool fake( ETrue );
            TBool makeUnion( EFalse );
            if( !oldPif )
                {
                TRACE(_L("COperationSubscribePresentityPresence::ProcessL() no old pif" ) );
                makeUnion = ETrue;
                }
            else if ( *oldPif != *iPif ) 
                {
                makeUnion = ETrue;
                }
            if( makeUnion )
                {
                iDoNotForce = ETrue;
                CPresenceInfoFilterImp* withoutPif = 
                            iSubItem->CollectSubscriptionPifWithoutCtxL( iContext );
                iMergedPif = DocumentUtils::InfoFilterUnionL( *withoutPif, *iPif );
                if( iMergedPif->Contains( oldMergedPif ) )
                    {
                    fake = EFalse;
                    TRACE(_L("COperationSubscribePresentityPresence::ProcessL() DoUpdatePresentityPresenceSubscriptionPifL 2" ) );
                    watching.DoUpdatePresentityPresenceSubscriptionPifL( *iIdentity, *iMergedPif, iReqId );
                    }
                }
            else
                {
                iMergedPif = TXIMPObjectCloner< CPresenceInfoFilterImp >::CloneL( oldMergedPif );
                }

            if( fake )
                {
                TRACE(_L("COperationSubscribePresentityPresence::ProcessL() FakeCompleteRequest" ) );
                iMyHost->FakeCompleteRequest( iReqId, KErrNone );
                }
            break;
            }

        XIMP_DEFAULT_CASE_UNSUPPORTED( NXIMPPrivPanic::EInvalidSubscriptionStatus );
        }
    }
コード例 #9
0
ファイル: neuromlreader.cpp プロジェクト: dragly/photonflow
bool NeuroMlReader::load(const std::string &path)
{
    m_path = path;
    if(m_path.empty()) {
        cerr << "NeuroMlReader: Path is empty, cannot read file." << endl;
        return false;
    }

    m_cylinders.clear();

    BoundingBox boundingBox;

    QString pathString = QString::fromStdString(path);
    QFile file(pathString);
    file.open(QFile::ReadOnly);
    if(!file.isOpen()) {
        cerr << "NeuroMlReader: Could not read file " << path << endl;
        return false;
    }
    QXmlStreamReader reader(&file);
    reader.readNext();
    //Reading from the file
    QVector<Segment> segments;
    bool dummySegment = true;
    Segment segment;
    while (!reader.isEndDocument())
    {
        if(reader.isStartElement()) {
            if(reader.name() == "segment") {
                if(!dummySegment) {
                    if(!segment.hasProximal && !segment.hasParentID) {
                        cerr << "WARNING: NeuroMlReader: Got segment without proximal or parent." << endl;
                    } else if(!segment.hasDistal) {
                        cerr << "WARNING: NeuroMlReader: Got segment without distal." << endl;
                    } else {
                        segments.append(segment);
                    }
                }
                dummySegment = false;
                segment = Segment();
                segment.id = reader.attributes().value("id").toInt();
                if(reader.attributes().hasAttribute("parent")) {
                    segment.parentID = reader.attributes().value("parent").toInt();
                    segment.hasParentID = true;
                }
            }
            if(reader.name() == "parent") {
                segment.parentID = reader.attributes().value("segment").toInt();
                segment.hasParentID = true;
            }
            if(reader.name() == "proximal") {
                //                qDebug() << reader.attributes().value("x") << reader.attributes().value("y") << reader.attributes().value("z");
                segment.proximal = Point3D(reader.attributes().value("x").toDouble() * 1.0_um,
                                           reader.attributes().value("y").toDouble() * 1.0_um,
                                           reader.attributes().value("z").toDouble() * 1.0_um);
                segment.proximalWidth = reader.attributes().value("diameter").toDouble() * 1.0_um;
                segment.hasProximal = true;
            }
            if(reader.name() == "distal") {
                //                qDebug() << reader.attributes().value("x") << reader.attributes().value("y") << reader.attributes().value("z") << reader.attributes().value("diameter");
                segment.distal = Point3D(reader.attributes().value("x").toDouble() * 1.0_um,
                                         reader.attributes().value("y").toDouble() * 1.0_um,
                                         reader.attributes().value("z").toDouble() * 1.0_um);
                segment.distalWidth = reader.attributes().value("diameter").toDouble() * 1.0_um;
                segment.hasDistal = true;
            }
        }
        reader.readNext();
    }
    segments.append(segment);

    for(Segment &segment : segments) {
        if(!segment.hasProximal && segment.hasParentID) {
            for(Segment &otherSegment : segments) {
                if(otherSegment.id == segment.parentID) {
                    Segment &parent = otherSegment;
                    segment.proximal = parent.distal;
                    break;
                }
            }
        }
        if(segment.proximalWidth == 0.0_um) {
            segment.proximalWidth = segment.distalWidth;
        }
    }

    m_cylinders.clear();
    for(const Segment &segment : segments) {
        CylinderFrustum cylinder(segment.proximal,
                                 segment.distal,
                                 segment.proximalWidth * 0.5,
                                 segment.distalWidth * 0.5);

        boundingBox = makeUnion(boundingBox, cylinder.start);
        boundingBox = makeUnion(boundingBox, cylinder.end);
        m_cylinders.push_back(cylinder);
    }

    m_boundingBox = boundingBox;
    return true;
}