Exemple #1
0
Tree setIntersection (Tree A, Tree B)
{
	if (isNil(A)) 		return A;
	if (isNil(B)) 		return B;
	if (hd(A) == hd(B)) return cons(hd(A), setIntersection(tl(A),tl(B)));
	if (hd(A) < hd(B)) 	return setIntersection(tl(A),B);
	/* (hd(A) > hd(B)*/	return setIntersection(A,tl(B));
}
Exemple #2
0
int main()
{
    char buffer[80];
    int i, index;
    Set set1, set2, set3;

    i = 0;  
    while ( (scanf("%s",buffer)) != EOF )
    {
        strcpy(names[i],buffer);
        i++;
    }
    printf("The initial data is:\n");
    print(names,i);
    bubbleSort(names,i);
    printf("\n\nThe sorted data is:\n");
    print(names,i);

    clearSet(set1);
    clearSet(set2);
    clearSet(set3);

    printf("Adding Warner, Faulk and Pace to set1:\n");
    addName2Set(set1,"Warner");
    addName2Set(set1,"Faulk");
    addName2Set(set1,"Pace");
    printf("Set1 = ");
    printSet(set1);

    printf("Adding Snider, Robinson and Koufax to set2:\n");
    addName2Set(set2,"Snider");
    addName2Set(set2,"Robinson");
    addName2Set(set2,"Koufax");
    printf("Set2 = ");
    printSet(set2);

    setUnion(set1, set2, set3);
    printf("The union of sets 1 and 2 is: ");
    printSet(set3);
    
    printf("The intersection of sets 3 and 2 is: ");
    setIntersection(set3, set2, set3);
    printSet(set3);

    printf("Deleting Snider from set3:\n");
    deleteNameFromSet(set3,"Snider");    
    printf("Set3 = ");
    printSet(set3);
}
Exemple #3
0
void test()
{
    int s1Init[] = {1,6,7,3,99};
    VSet<int> s1(s1Init, ARRAYEND(s1Init));
    int s2Init[] = {1,-4,42,7,100};
    VSet<int> s2(s2Init, ARRAYEND(s2Init));
    std::cout << s1 << "\n";

    std::cout << "s1.contains(1) = " << s1.contains(1) << "\n";
    std::cout << "s1.contains(-1) = " << s1.contains(-1) << "\n";

    int smallInit[] = {1,6};
    VSet<int> small(smallInit, ARRAYEND(smallInit));
    std::cout << s1 << ".includes(" << small << ") = " << s1.includes(small) << "\n";


    VSet<int> u;
    setUnion(s1, s2, u);
    std::cout << "union(" << s1 << ", " << s2 << ") = " << u << "\n";
    VSet<int> i;
    setIntersection(s1, s2, i);
    std::cout << "intersection(" << s1 << ", " << s2 << ") = " << i << "\n";
}
bool Foam::triangleFuncs::intersect
(
    const point& va0,
    const point& va10,
    const point& va20,

    const point& base,
    const point& normal,

    point& pInter0,
    point& pInter1
)
{
    // Get triangle normal
    vector na = va10 ^ va20;
    scalar magArea = mag(na);
    na/magArea;

    if (mag(na & normal) > (1 - SMALL))
    {
        // Parallel
        return false;
    }

    const point va1 = va0 + va10;
    const point va2 = va0 + va20;

    // Find the triangle point on the other side.
    scalar sign0 = (va0 - base) & normal;
    scalar sign1 = (va1 - base) & normal;
    scalar sign2 = (va2 - base) & normal;

    label oppositeVertex = -1;

    if (sign0 < 0)
    {
        if (sign1 < 0)
        {
            if (sign2 < 0)
            {
                // All on same side of plane
                return false;
            }
            else    // sign2 >= 0
            {
                // 2 on opposite side.
                oppositeVertex = 2;
            }
        }
        else    // sign1 >= 0
        {
            if (sign2 < 0)
            {
                // 1 on opposite side.
                oppositeVertex = 1;
            }
            else
            {
                // 0 on opposite side.
                oppositeVertex = 0;
            }
        }
    }
    else    // sign0 >= 0
    {
        if (sign1 < 0)
        {
            if (sign2 < 0)
            {
                // 0 on opposite side.
                oppositeVertex = 0;
            }
            else    // sign2 >= 0
            {
                // 1 on opposite side.
                oppositeVertex = 1;
            }
        }
        else    // sign1 >= 0
        {
            if (sign2 < 0)
            {
                // 2 on opposite side.
                oppositeVertex = 2;
            }
            else    // sign2 >= 0
            {
                // All on same side of plane
                return false;
            }
        }
    }

    scalar tol = SMALL*Foam::sqrt(magArea);

    if (oppositeVertex == 0)
    {
        // 0 on opposite side. Cut edges 01 and 02
        setIntersection(va0, sign0, va1, sign1, tol, pInter0);
        setIntersection(va0, sign0, va2, sign2, tol, pInter1);
    }
    else if (oppositeVertex == 1)
    {
        // 1 on opposite side. Cut edges 10 and 12
        setIntersection(va1, sign1, va0, sign0, tol, pInter0);
        setIntersection(va1, sign1, va2, sign2, tol, pInter1);
    }
    else // oppositeVertex == 2
    {
        // 2 on opposite side. Cut edges 20 and 21
        setIntersection(va2, sign2, va0, sign0, tol, pInter0);
        setIntersection(va2, sign2, va1, sign1, tol, pInter1);
    }

    return true;
}
static int setTestDriver(int argc, UC8 **argv) {  
	const UC8 me[]="setTestDriver"; 
	 /* Lets keep complaints about not using argc/v quiet... */
	if (!argc) { 
		printf("%s: No Args: .\n", me);
	} else {
		int i;
		UC8 *mem;
		UC8 *otherSet;
		UC8 *uSet;
		UC8 newSet[300];
		UC8 newSet_B[300];
		myBool isMem;
		while (--argc > 0 && (*++argv)[0] == '-')
			switch ((*argv)[1]) {
			case 's':  
				--argc; argv++; i=setShow(mk0Null(*argv)); printf("\n");
				printf("%s: Show:          :%-15s: Result:           %3d\n", "set", *argv, i);
				return i;
			case 'c':
				--argc; argv++; i=setCardinal(mk0Null(*argv));
				printf("%s: Cardinal:      :%-15s:                : Result:%3d \n", "set", *argv, i);
				return i;
			case 'n':
				--argc; argv++; 
				// !! setNormalise writes to what's pointed at by its params. 
				//    That MUST be writable.  I don't think **argv is  is....
				if (*argv) strcpy(newSet, *argv); else newSet[0]=0;   // strcpy cant cope with NULLs.....
				i=setNormalise(mk0Null(newSet));
				printf("%s: Normalise:     :%-15s:%-15s : Status:%3d (%s)\n", "set", *argv, newSet, i, setStatus2Str(i) );
				return i;
			case 'm':
				--argc; argv++; mem=mk0Null(*argv);
				--argc; argv++; isMem=setIsNotMember(mem, mk0Null(*argv));
				printf("%s: isNotMember:   :%-15s:%-15s : Result:%3d (%s)\n", "set", mem, *argv, isMem, setStatus2Str(isMem));
				return isMem;
			case 'e': // Note - setIsNotEqual calls normalise, so *argv won't do......
				--argc; argv++; 
				otherSet=mk0Null(*argv);
				if (*argv) strcpy(newSet, otherSet); else newSet[0]=0;   // strcpy cant cope with NULLs.....
				--argc; argv++; 
				if (mk0Null(*argv)) strcpy(newSet_B, mk0Null(*argv)); else newSet_B[0]=0;   // strcpy cant cope with NULLs.....
				isMem=setIsNotEqual(newSet, newSet_B);
				printf("%s: setIsNotEqual: :%-15s, %-15s: Result:%3d (%s)\n", "set", otherSet, *argv, isMem, setStatus2Str(isMem));
				return isMem;
			case 'u':
				--argc; argv++; otherSet=mk0Null(*argv);
				--argc; argv++; uSet=setUnion(otherSet, mk0Null(*argv));
				printf("%s: setUnion:      :%-15s, %-15s: Result: %-15s\n", "set", otherSet, *argv, uSet);
				//printf("%s: : setUnion: Doing The CleanUp\n", me);
				setDelete(uSet);
				//printf("%s: : setUnion: CleanUp Done\n", me);
				// Like to return a decent value here
				break;
			case 'i':
				--argc; argv++; otherSet=mk0Null(*argv);
				--argc; argv++; uSet=setIntersection(otherSet, mk0Null(*argv));
				printf("%s: setIntersection:%-15s, %-15s: Result: %-15s\n", "set", otherSet, mk0Null(*argv), uSet);
				//printf("%s: : setIntersection: Doing The CleanUp\n", me);
				setDelete(uSet);
				//printf("%s: : setIntersection: CleanUp Done\n", me);
				// Like to return a decent value here
				break;
			default:
				printf("%s: : ERROR UNKNOWN: %c:\n", "set", (*argv)[1] );
			}
	}
	//printf("%s: Complete. No Status Value returned (only 0)\n", me);
	return 0; // What we return if we can't return anythingelse
}
Exemple #6
0
int main() {
	int i, num, cNum;
	char temp1[257], temp2[257], classString[12], buffer[80], call[80], test[80], next[80];
	stack s;
	Set dset;
	initStack(&s);

	DBRecord **students;

	scanf("%d", &num);
	printf("%d \n", num);

    students = (DBRecord **) malloc(num * sizeof(DBRecord *));

	for(i = 0; i < num; i++) {
		students[i] = (DBRecord *) malloc (sizeof(DBRecord));
		students[i]->bitID = i;
		scanf("%s %s %s %d %s %f %d", temp1, temp2, students[i]->idNum, &students[i]->age, classString, &students[i]->gpa, &students[i]->expectedGrad);

		//  allocate space for character strings, and copy the strings */
		students[i]->lName = (char *) malloc(strlen(temp1) + 1);
		strcpy(students[i]->lName, temp1);
		students[i]->fName = (char *) malloc(strlen(temp2) + 1);
		strcpy(students[i]->fName, temp2);

		// set the year field from the strings of Class values
		if( (strcmp(classString,"firstYear")) == 0 )
			students[i]->year = firstYear;

		else if( (strcmp(classString,"sophmore")) == 0 )
			students[i]->year = sophomore;

		else if( (strcmp(classString,"junior")) == 0 )
			students[i]->year = junior;

		else if( (strcmp(classString,"senior")) == 0 )
			students[i]->year = senior;

		else if( (strcmp(classString,"grad")) == 0 )
			students[i]->year = grad;

		else
			assert(0);

	}
	printf("Done scanning \n");

	while (scanf("%s", buffer) != EOF) {
		if (strcmp(buffer, "create") == 0) {
			clearSet(dset);
			scanf("%s", call);
			scanf("%s", test);

			if (strcmp(call, "lastName") == 0) {
				scanf("%s", next);
				if (strcmp(test, "=") == 0) {
					for(i = 0; i < num; i++) {
						if (strcmp(students[i]->lName, next) == 0)
							add2Set(dset, students[i]->bitID);
					}
				}
				if (strcmp(test, "<") == 0) {
					for(i = 0; i < num; i++) {
						if (strcmp(students[i]->lName, next) < 0)
							add2Set(dset, students[i]->bitID);
					}
				}
				if (strcmp(test, ">") == 0) {
					for(i = 0; i < num; i++) {
						if (strcmp(students[i]->lName, next) > 0)
							add2Set(dset, students[i]->bitID);
					}
				}
			}
			if (strcmp(call, "firstName") == 0) {
				scanf("%s", next);
				if (strcmp(test, "=") == 0) {
					for(i = 0; i < num; i++) {
						if (strcmp(students[i]->fName, next) == 0)
							add2Set(dset, students[i]->bitID);
					}
				}
				if (strcmp(test, "<") == 0) {
					for(i = 0; i < num; i++) {
						if (strcmp(students[i]->fName, next) < 0)
							add2Set(dset, students[i]->bitID);
					}
				}
				if (strcmp(test, ">") == 0) {
					for(i = 0; i < num; i++) {
						if (strcmp(students[i]->fName, next) > 0)
							add2Set(dset, students[i]->bitID);
					}
				}
			}
			if (strcmp(call, "idNum") == 0) {
				scanf("%s", next);
				if (strcmp(test, "=") == 0) {
					for(i = 0; i < num; i++) {
						if (strcmp(students[i]->idNum, next) == 0)
							add2Set(dset, students[i]->bitID);
					}
				}
				if (strcmp(test, "<") == 0) {
					for(i = 0; i < num; i++) {
						if (strcmp(students[i]->idNum, next) < 0)
							add2Set(dset, students[i]->bitID);
					}
				}
				if (strcmp(test, ">") == 0) {
					for(i = 0; i < num; i++) {
						if (strcmp(students[i]->idNum, next) > 0)
							add2Set(dset, students[i]->bitID);
					}
				}
			}
			if (strcmp(call, "age") == 0) {
				scanf("%d", &cNum);
				if (strcmp(test, "=") == 0) {
					for(i = 0; i < num; i++) {
						if(students[i]->age == cNum)
							add2Set(dset, students[i]->bitID);
					}
				}
				if (strcmp(test, "<") == 0) {
					for(i = 0; i < num; i++) {
						if(students[i]->age == cNum)
							add2Set(dset, students[i]->bitID);
					}
				}
				if (strcmp(test, ">") == 0) {
					for(i = 0; i < num; i++) {
						if(students[i]->age == cNum)
							add2Set(dset, students[i]->bitID);
					}
				}
			}
			if (strcmp(call, "year") == 0) {
				scanf("%d", &cNum);
				if (strcmp(test, "=") == 0) {
					for(i = 0; i < num; i++) {
						if(students[i]->year == cNum)
							add2Set(dset, students[i]->bitID);
					}
				}
				if (strcmp(test, "<") == 0) {
					for(i = 0; i < num; i++) {
						if(students[i]->year == cNum)
							add2Set(dset, students[i]->bitID);
					}
				}
				if (strcmp(test, ">") == 0) {
					for(i = 0; i < num; i++) {
						if(students[i]->year == cNum)
							add2Set(dset, students[i]->bitID);
					}
				}
			}
			if (strcmp(call, "gpa") == 0) {
				scanf("%d", &cNum);
				if (strcmp(test, "=") == 0) {
					for(i = 0; i < num; i++) {
						if(students[i]->gpa == cNum)
							add2Set(dset, students[i]->bitID);
					}
				}
				if (strcmp(test, "<") == 0) {
					for(i = 0; i < num; i++) {
						if(students[i]->gpa == cNum)
							add2Set(dset, students[i]->bitID);
					}
				}
				if (strcmp(test, ">") == 0) {
					for(i = 0; i < num; i++) {
						if(students[i]->gpa == cNum)
							add2Set(dset, students[i]->bitID);
					}
				}
			}
			if (strcmp(call, "expectedGrad") == 0) {
				scanf("%d", &cNum);
				if (strcmp(test, "=") == 0) {
					for(i = 0; i < num; i++) {
						if(students[i]->expectedGrad == cNum)
							add2Set(dset, students[i]->bitID);
					}
				}
				if (strcmp(test, "<") == 0) {
					for(i = 0; i < num; i++) {
						if(students[i]->expectedGrad == cNum)
							add2Set(dset, students[i]->bitID);
					}
				}
				if (strcmp(test, ">") == 0) {
					for(i = 0; i < num; i++) {
						if(students[i]->expectedGrad == cNum)
							add2Set(dset, students[i]->bitID);
					}
				}
			}
			push(&s, dset);
			printf("Created & Pushed Set \n");
		}
		if (strcmp(buffer, "union")) {
			Set uset1, uset2, urset;
			memcpy(uset2, pop(&s), 10 * sizeof(unsigned int));
			memcpy(uset1, pop(&s), 10 * sizeof(unsigned int));
			setUnion(uset1, uset2, urset);
			push(&s, urset);
			printf("Pushed Set \n");
		}
		if (strcmp(buffer, "intersection")) {
			Set iset1, iset2, irset;
			memcpy(iset2, pop(&s), 10 * sizeof(unsigned int));
			memcpy(iset1, pop(&s), 10 * sizeof(unsigned int));
			setIntersection(iset1, iset2, irset);
			push(&s, irset);
			printf("Pushed Set \n");
		}
		if (strcmp(buffer, "print")) {
			Set pset;
			memcpy(pset, pop(&s), 10 * sizeof(unsigned int));
			printSet(pset);
		}
		
	}
}
Exemple #7
0
static Box* setIntersectionUpdate(BoxedSet* self, BoxedTuple* args) {
    Box* tmp = setIntersection(self, args);
    AUTO_DECREF(tmp);
    std::swap(self->s, ((BoxedSet*)tmp)->s);
    return incref(None);
}
/**
* @brief Computes the FuncInfo and returns it.
*/
ShPtr<OptimFuncInfo> OptimFuncInfoCFGTraversal::performComputation() {
	// First, we pre-compute varsAlwaysModifiedBeforeRead. The reason is that
	// their computation differs from the computation of the rest of the sets.
	precomputeAlwaysModifiedVarsBeforeRead();

	// Every function's body is of the following form:
	//
	//    (1) definitions of local variables, including assignments of global
	//        variables into local variables
	//    (2) other statements
	//
	// We store which variables are read/modified in (1). Then, we start the
	// traversal from (2). During the traversal, we check which variables are
	// read/modified and update funcInfo accordingly. The stored information
	// from (1) is used to compute the set of global variables which are read
	// in the function, but not modified.
	//
	// To give a specific example, consider the following code:
	//
	// def func(mango):
	//    global orange
	//    global plum
	//    lychee = orange
	//    achira = plum
	//    orange = mango
	//    plum = rand()
	//    result = plum * apple + orange
	//    orange = lychee
	//    plum = achira
	//    return result
	//
	// Here, even though the global variable orange is modified, its value
	// before calling func() is the same as after calling func(). Indeed, its
	// value is restored before the return statement. Hence, we may put it into
	// funcInfo->varsWithNeverChangedValue.
	// TODO Implement a more robust analysis.
	ShPtr<Statement> currStmt = traversedFunc->getBody();
	while (isVarDefOrAssignStmt(currStmt)) {
		updateFuncInfo(currStmt);

		ShPtr<Expression> lhs(getLhs(currStmt));
		ShPtr<Expression> rhs(getRhs(currStmt));

		// If there is no right-hand side, it is a VarDefStmt with no
		// initializer, which we may skip.
		if (!rhs) {
			currStmt = currStmt->getSuccessor();
			continue;
		}

		// If there are any function calls or dereferences, we have reached
		// (2).
		ShPtr<ValueData> currStmtData(va->getValueData(currStmt));
		if (currStmtData->hasCalls() || currStmtData->hasDerefs()) {
			break;
		}

		// Check whether the statement is of the form localVar = globalVar.
		ShPtr<Variable> localVar(cast<Variable>(lhs));
		ShPtr<Variable> globalVar(cast<Variable>(rhs));
		if (!localVar || !globalVar || hasItem(globalVars, localVar) ||
				!hasItem(globalVars, globalVar)) {
			// It is not of the abovementioned form, so skip it.
			currStmt = currStmt->getSuccessor();
			continue;
		}

		storedGlobalVars[globalVar] = localVar;
		currStmt = currStmt->getSuccessor();
	}

	// Perform the traversal only if we haven't reached the end of the function
	// yet. Since empty statements are not present in a CFG, skip them before
	// the traversal.
	if ((currStmt = skipEmptyStmts(currStmt))) {
		performTraversal(currStmt);
	}

	// We use the exit node of the CFG to check that every variable from
	// storedGlobalVars is retrieved its original value before every return.
	ShPtr<CFG::Node> exitNode(cfg->getExitNode());
	// For every predecessor of the exit node...
	for (auto i = exitNode->pred_begin(), e = exitNode->pred_end(); i != e; ++i) {
		bool checkingShouldContinue = checkExitNodesPredecessor((*i)->getSrc());
		if (!checkingShouldContinue) {
			break;
		}
	}

	// Update funcInfo using the remaining variables in storedGlobalVars.
	for (const auto &p : storedGlobalVars) {
		funcInfo->varsWithNeverChangedValue.insert(p.first);
	}

	// Update funcInfo->never{Read,Modified}Vars by global variables which are
	// untouched in this function.
	for (auto i = module->global_var_begin(), e = module->global_var_end();
			i != e; ++i) {
		ShPtr<Variable> var((*i)->getVar());
		if (!hasItem(funcInfo->mayBeReadVars, var) &&
				!hasItem(funcInfo->mayBeModifiedVars, var)) {
			funcInfo->neverReadVars.insert(var);
			funcInfo->neverModifiedVars.insert(var);
		}
	}

	// If the cfg contains only a single non-{entry,exit} node, every
	// mayBe{Read,Modifed} variable can be turned into a always{Read,Modified}
	// variable.
	if (cfg->getNumberOfNodes() == 3) {
		addToSet(funcInfo->mayBeReadVars, funcInfo->alwaysReadVars);
		addToSet(funcInfo->mayBeModifiedVars, funcInfo->alwaysModifiedVars);
	}

	// Add all variables which are never read and never modified to
	// varsWithNeverChangedValue.
	VarSet neverReadAndModifedVars(setIntersection(funcInfo->neverReadVars,
		funcInfo->neverModifiedVars));
	addToSet(neverReadAndModifedVars, funcInfo->varsWithNeverChangedValue);

	// Add all global variables are not read in this function into
	// varsAlwaysModifiedBeforeRead.
	addToSet(setDifference(globalVars, funcInfo->mayBeReadVars),
		funcInfo->varsAlwaysModifiedBeforeRead);

	return funcInfo;
}
vector<SplitEdge> pairing(const vector<int> &Xi,const vector<int> &Xj, int delij, vector<SplitEdge> &B, const vector<SplitEdge> &g, int s, historyIndex &h)
{
	vector<SplitEdge> GHat = g;
	while (delij > 0)
	{
		int u = 0;
		int v = 0;
		vector<int> gamXi = setIntersection(Xi, neighbors(GHat, s));
		vector<int> gamXj = setIntersection(Xj, neighbors(GHat, s));
		if (gamXi.size() == 0 || gamXj.size() == 0)
		{
			cout << "Detected 0, dumping status" << endl;
			cout << "GHat:" << endl;
			output(GHat);
			cout << "B:" << endl;
			output(B);
			cout << "S: " << s << endl;
			cout << "delij: " << delij << endl;
			cout << "Xi:" << endl;
			output(Xi);
			cout << "neighbors of s in GHat:" << endl;
			output(neighbors(GHat, s));
			cout << "Xj:" << endl;
			output(Xj);
			cout << "gamXi:" << endl;
			output(gamXi);
			cout << "gamXj:" << endl;
			output(gamXj);
		}
		u = gamXi[0];
		v = gamXj[0];
		if (u == v)
		{
			cout << "U == V. Quitting" << endl;
			throw logic_error("");
		}
		int delta = min(min(cG(s, u, GHat), cG(s, v, GHat)),delij);
		GHat = split(GHat, u, v, s, delta, h);
		//Add weight delta to (u,v) in B
		bool found = false;
		for (unsigned i = 0; i < B.size(); i++)
		{
			if (connects(B[i], u, v))
			{
				B[i].weight += delta;
				found = true;
				break;
			}
		}
		if (!found)
		{
			for (unsigned i = 0; i < GHat.size(); i++)
			{
				if (connects(GHat[i], u, v))
				{
					SplitEdge e(GHat[i].end0, GHat[i].end1, delta, GHat[i].orig0, GHat[i].orig1);
					B.push_back(e);
					found = true;
					break;
				}
			}
		}
		if (!found)
		{
			SplitEdge e(u, v, delta, u, v);
			B.push_back(e);
		}
		delij -= delta;
	}
	return GHat;
}