Exemplo n.º 1
0
void DeltaApplyEngine::ApplyDeltaElement(DOMNode* incDeltaElement) {
	vddprintf(("Apply delta element\n"));
	deltaElement = incDeltaElement;
	
	/* ---- Do All DELETE Operations ( including 'from' part of move ) ---- */
	
	vddprintf(("Apply Delete operations\n"));
	DOMNode* firstOp = deltaElement->getFirstChild() ;
	vddprintf(("    first sort delete operations...\n"));
	SortDeleteOperationsEngine deleteOps(xiddoc, firstOp);
	vddprintf(("    then apply all of them 1 by 1...\n"));
	while(!deleteOps.isListEmpty()) {
		DOMNode* op=deleteOps.getNextDeleteOperation();
		ApplyOperation(op);
		}

	vddprintf(("Ok, there are no more delete operations.\n"));
	
	/* ---- Do All INSERT Operations ( including 'to' part of move ) ---- */
	
	firstOp = deltaElement->getFirstChild() ;
	SortInsertOperationsEngine insertOps(xiddoc, firstOp);
	while(!insertOps.isListEmpty()) {
		DOMNode* op=insertOps.getNextInsertOperation();
		ApplyOperation(op);
		}
	
	/* ---- Do all  UPDATE  &  ATTRIBUTE  Operations ---- */

	DOMNode* child = deltaElement->getFirstChild() ;
	XMLCh iStr[100];
	XMLString::transcode("i", iStr, 99);
	XMLCh dStr[100];
	XMLString::transcode("d", dStr, 99);
	while (child != NULL) {
		if ( (!XMLString::equals(child->getLocalName(),iStr))
		
		   &&(!XMLString::equals(child->getLocalName(), dStr)) ) ApplyOperation(child);
	  child = child->getNextSibling() ;
		}
		
	/* ---- Small consistency checks ---- */

	if (moveDocument->getDocumentElement()->hasChildNodes()) THROW_AWAY(("temporary document used to move node is not empty!"));

	vddprintf(("xiddoc=%s\n",xiddoc->getXidMap().String().c_str() ));
}
string ExpressionNotationConverter::ConvertInfixToPostfix(string expression)
{
	Stack operatorStack;
	Stack operandStack;
	string outputString = "";

	auto tmp = expression;

	while (tmp.length() > 0)
	{
		auto strLength = tmp.length();
		auto numberToSkip = 0;
		auto skip = 1;

		do
		{
			auto tmp1 = tmp.substr(numberToSkip, 1);

			if (!isdigit(tmp1[0]))
				break;

			numberToSkip++;
		} while (numberToSkip - 1 < strLength);

		if (numberToSkip > 0)
		{
			skip = numberToSkip;
		}

		auto subString = tmp.substr(0, strLength - (strLength - skip));
		auto currentCharacterPrecedence = DeterminePrecidence(subString);

		if (subString == "(")
		{
			operatorStack.Push(subString);
		}
		else if (subString == ")")
		{
			while (!operatorStack.IsEmpty() && operatorStack.Peek() != "(")
			{
				auto currentOperator = operatorStack.Pop();
				auto lhs = stoi(operandStack.Pop());
				auto rhs = stoi(operandStack.Pop());
				auto result = to_string(ApplyOperation(currentOperator, lhs, rhs));

				operandStack.Push(result);

				if (currentOperator != "(" && currentOperator != ")")
				{
					outputString += currentOperator + " ";
				}
			}

			auto paren = operatorStack.Pop();
		}
		else if (currentCharacterPrecedence == OPERAND)
		{
			operandStack.Push(subString);

			outputString += subString + " ";
		}
		else
		{
			while (!operatorStack.IsEmpty() && currentCharacterPrecedence <= DeterminePrecidence(operatorStack.Peek()))
			{
				auto currentOperator = operatorStack.Pop();

				outputString += currentOperator + " ";

				if (currentOperator != "(" && currentOperator != ")")
				{
					auto lhs = stoi(operandStack.Pop());
					auto rhs = stoi(operandStack.Pop());
					auto result = to_string(ApplyOperation(currentOperator, lhs, rhs));

					operandStack.Push(result);
				}
			}

			operatorStack.Push(subString);
		}

		tmp = string(tmp.substr(skip));
	}

	while (!operatorStack.IsEmpty())
	{
		auto currentOperator = operatorStack.Pop();

		if (currentOperator == "(" || currentOperator == ")")
		{
			outputString = "Invalid Expression!";

			break;
		}

		auto lhs = stoi(operandStack.Pop());
		auto rhs = stoi(operandStack.Pop());
		auto result = to_string(ApplyOperation(currentOperator, lhs, rhs));

		operandStack.Push(result);

		outputString += currentOperator + " ";
	}

	if (outputString == "Invalid Expression!")
	{
		return outputString;
	}

	auto result = operandStack.Pop();

	return outputString += " : " + result;
}