Exemplo n.º 1
0
void TimeComplexity::intersection_tracktime(string listname1, string listname2, string listname3)
{
	time_actual = 0;
	long n_1 = all_lists->getN(listname2);
	long n_2 = all_lists->getN(listname3);
	long n_low, n_hi;
	string greater_list, lesser_list;

	if(n_1 > n_2)
	{
		greater_list = listname2;
		lesser_list = listname3;
		n_low = n_2;
		n_hi = n_1;
	}
	else
	{
		greater_list = listname3;
		lesser_list = listname2;
		n_low = n_1;
		n_hi = n_2;
	}
	
	//intersection's algorithm  3.95*(n_low*n_low + n_hi*n_hi + n_low*n_hi + n_low) how I got 3.95 is in the README
	long tc_estimate = 3.95*(n_low*n_low + n_hi*n_hi + n_low*n_hi + n_low);
	long c = 8;
	long upper_bound = c*((n_hi*n_hi) + (n_low*n_low)); // n^2 is the upper bound or O(n^2), since bubble sort is used to alphabetize times a constant c for the true upper bound

	all_lists->intersect_initialize(greater_list, lesser_list, listname1, "iterative"); // calling the actual intersection function to calculate the TIME_ACTUAL
	
	stringstream ss; //outputting all data in CSV format
	ss<<n_1<<","<<n_2<<","<<"intersection,"<<tc_estimate<<","<<time_actual<<","<<c<<",64,"<<upper_bound;
	string result = ss.str();
	output(result, "append");
	return;
}
void StackContainer::postfixCalc(string listID)
{
	Node* current = postfix_list->head;
	string s1, s2, itos;
	stringstream ss;
	int i = 0;
	while(current != NULL)
	{
		if(current->word == "+")
		{
			//creates a list named "union#" where # is an arbitrary iterator used to distinguish multiple unions in a single expression
			//pushes the name to the stack in order to use it for the next operation
			//However, if it is the last node in the list, it is the final operation which means the list should be instead named listID
			s1 = pop();
			s2 = pop();
			if(s1 == "" || s2 == "")
			{
				error("Stack underflow, not enough arguments for union for operation involving " +listID);
				break;
			}
			if(current->next == NULL)
			{
				all_lists->union_initialize(s1, s2, listID, "recursive");
				break;
			}
			ss << i;
			itos = ss.str();
			all_lists->union_initialize(s1, s2, "union"+itos, "recursive");
			push("union"+itos);
			i++;
			current = current->next;
			ss.str("");
			ss.clear();
			continue;
		}
		if(current->word == "*")
		{
			//creates a list named "intersection#" where # is an arbitrary iterator used to distinguish multiple intersections in a single expression
			//pushes the name to the stack in order to use it for the next operation
			//however, if it is the last node in the list, it is the final operation which means the list should be instead named listID
			s1 = pop();
			s2 = pop();
			if(s1 == "" || s2 == "")
			{
				error("Stack underflow, not enough arguments for intersection for operation involving " +listID);
				break;
			}
			if(current->next == NULL)
			{
				all_lists->intersect_initialize(s1, s2, listID, "recursive");
				break;
			}
			ss << i;
			itos = ss.str();
			all_lists->intersect_initialize(s1, s2, "intersect"+itos, "recursive");
			push("intersect"+itos);
			i++;
			current = current->next;
			ss.str("");
			ss.clear();
			continue;
		}
		
		//if not + or *, it must be an operand, and operands get pushed to stack
		push(current->word);
		current = current->next;
		i++;
	}
	
	while(pop()!=""); //clears the stack of anything left over if the expression was not legal
}