Beispiel #1
0
void print_s(my_stack<T>& s)
{
    int size = s.size();
    for (int i=0; i<size; ++i) {
        cout << "min: " << s.min() << " elem: " << s.pop() << endl;
    }
}
Beispiel #2
0
void forestSample::processQueue()
{
	double newW;
	char t;
	short l, pos, m;
	short curP, newP;

	while (Q1.size())
	{
		temp1 = Q1.top(); Q1.pop();

		t = temp1.task;
		l = temp1.level;
		pos = temp1.pos;
		m = temp1.addr;
		newW = temp1.dw;

		curP = utils::discreteLog(forest[l][pos].weight);
		newP = utils::discreteLog(forest[l][pos].weight + newW);

		if (t == 1)		//Inserstion of a child
		{
			if (forest[l][pos].children.size() > 1)
			{
				if (curP != newP)
				{
					//Add old parent to processing queue for deletion
					temp1.task = -1;
					temp1.level = l + 1;
					temp1.pos = curP;
					temp1.addr = forest[l][pos].childNum;
					temp1.dw = -forest[l][pos].weight;
					Q1.push(temp1);

					//Insert new child and Update weight
					forest[l - 1][m].childNum = forest[l][pos].children.size();
					forest[l][pos].children.push_back(m);
					forest[l][pos].weight += newW;

					//Add new parent to processing queue for insertion
					temp1.task = 1;
					temp1.level = l + 1;
					temp1.pos = newP;
					temp1.addr = pos;
					temp1.dw = forest[l][pos].weight;
					Q1.push(temp1);
				}

				else
				{
					//Insert new child and Update weight
					forest[l - 1][m].childNum = forest[l][pos].children.size();
					forest[l][pos].children.push_back(m);
					forest[l][pos].weight += newW;

					//Add parent to processing queue for updation
					temp1.task = 0;
					temp1.level = l + 1;
					temp1.pos = newP;
					temp1.addr = -1;
					temp1.dw = newW;
					Q1.push(temp1);
				}
			}
			else if (forest[l][pos].children.size() == 1)
			{
				//remove from table
				forest[l][levels[l].children.back()].childNum = forest[l][pos].childNum;
				levels[l].children[forest[l][pos].childNum] = levels[l].children.back();
				levels[l].children.pop_back();
				levels[l].weight -= forest[l][pos].weight;

				//Insert new child and Update weight
				forest[l - 1][m].childNum = 1;
				forest[l][pos].children.push_back(m);
				forest[l][pos].weight += newW;

				//Add new parent to processing queue for insertion
				temp1.task = 1;
				temp1.level = l + 1;
				temp1.pos = newP;
				temp1.addr = pos;
				temp1.dw = forest[l][pos].weight;
				Q1.push(temp1);
			}
			else
			{
				//Insert new child and Update weight
				forest[l - 1][m].childNum = 0;
				forest[l][pos].children.push_back(m);
				forest[l][pos].weight += newW;

				//add to table
				forest[l][pos].childNum = levels[l].children.size();
				levels[l].weight += forest[l][pos].weight;
				levels[l].children.push_back(pos);
			}
		}
		else if (t == 0) //updation
		{
			if (forest[l][pos].children.size() > 1)
			{
				if (curP != newP)
				{
					//Add old parent to processing queue for deletion
					temp1.task = -1;
					temp1.level = l + 1;
					temp1.pos = curP;
					temp1.addr = forest[l][pos].childNum;
					temp1.dw = -forest[l][pos].weight;
					Q1.push(temp1);

					//Update weight
					forest[l][pos].weight += newW;

					//Add new parent to processing queue for insertion
					temp1.task = 1;
					temp1.level = l + 1;
					temp1.pos = newP;
					temp1.addr = pos;
					temp1.dw = forest[l][pos].weight;
					Q1.push(temp1);
				}
				else
				{
					//Update weight
					forest[l][pos].weight += newW;

					//Add parent to processing queue for updation
					temp1.task = 0;
					temp1.level = l + 1;
					temp1.pos = newP;
					temp1.addr = -1;
					temp1.dw = newW;
					Q1.push(temp1);
				}
			}
			else if (forest[l][pos].children.size() == 1)
			{
				//Update weight
				forest[l][pos].weight += newW;

				//Update in table
				levels[l].weight += newW;
			}
		}
		else if (t == -1)	//deletion
		{
			if (forest[l][pos].children.size() > 2)
			{
				if (curP != newP)
				{
					//Add old parent to processing queue for deletion
					temp1.task = -1;
					temp1.level = l + 1;
					temp1.pos = curP;
					temp1.addr = forest[l][pos].childNum;
					temp1.dw = -forest[l][pos].weight;
					Q1.push(temp1);

					//Remove old child and Update weight
					//forest[l - 1][forest[l][pos].children.back()].childNum = m*(m != forest[l][pos].children.size() - 1) + forest[l][pos].children.back()*(m == forest[l][pos].children.size() - 1);
					//forest[l][pos].children[m] = forest[l][pos].children.back();
					if (m != forest[l][pos].children.size() - 1)
					{
						forest[l - 1][forest[l][pos].children.back()].childNum = m;
						forest[l][pos].children[m] = forest[l][pos].children.back();
					}
					forest[l][pos].children.pop_back();
					forest[l][pos].weight += newW;

					//Add new parent to processing queue for insertion
					temp1.task = 1;
					temp1.level = l + 1;
					temp1.pos = newP;
					temp1.addr = pos;
					temp1.dw = forest[l][pos].weight;
					Q1.push(temp1);
				}

				else
				{
					//Remove old child and Update weight
					//forest[l - 1][forest[l][pos].children.back()].childNum = m*(m != forest[l][pos].children.size() - 1) + forest[l][pos].children.back()*(m == forest[l][pos].children.size() - 1);
					//forest[l][pos].children[m] = forest[l][pos].children.back();
					if (m != forest[l][pos].children.size() - 1)
					{
						forest[l - 1][forest[l][pos].children.back()].childNum = m;
						forest[l][pos].children[m] = forest[l][pos].children.back();
					}
					forest[l][pos].children.pop_back();
					forest[l][pos].weight += newW;

					//Add parent to processing queue for updation
					temp1.task = 0;
					temp1.level = l + 1;
					temp1.pos = newP;
					temp1.addr = -1;
					temp1.dw = newW;
					Q1.push(temp1);
				}
			}
			else if (forest[l][pos].children.size() == 2)
			{
				//Add old parent to processing queue for deletion
				temp1.task = -1;
				temp1.level = l + 1;
				temp1.pos = curP;
				temp1.addr = forest[l][pos].childNum;
				temp1.dw = -forest[l][pos].weight;
				Q1.push(temp1);

				//Remove old child and Update weight
				//forest[l - 1][forest[l][pos].children.back()].childNum = m*(m != forest[l][pos].children.size() - 1) + forest[l][pos].children.back()*(m == forest[l][pos].children.size() - 1);
				//forest[l][pos].children[m] = forest[l][pos].children.back();
				if (m != forest[l][pos].children.size() - 1)
				{
					forest[l - 1][forest[l][pos].children.back()].childNum = m;
					forest[l][pos].children[m] = forest[l][pos].children.back();
				}
				forest[l][pos].children.pop_back();
				forest[l][pos].weight += newW;

				//add to table
				forest[l][pos].childNum = levels[l].children.size();
				levels[l].weight += forest[l][pos].weight;
				levels[l].children.push_back(pos);
			}
			else
			{
				//remove from table
				forest[l][levels[l].children.back()].childNum = forest[l][pos].childNum;
				levels[l].children[forest[l][pos].childNum] = levels[l].children.back();
				levels[l].children.pop_back();
				levels[l].weight -= forest[l][pos].weight;

				//Remove old child and reset weight
				forest[l][pos].weight = 0;
				forest[l][pos].childNum = -1;
				forest[l][pos].children.clear();
			}
		}
	}
}