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; } }
void forestSample::remove(short pos) { short curP; node newOne; wsum -= forest[0][pos].weight; curP = utils::discreteLog(forest[0][pos].weight); //Add old parent to processing queue for deletion temp1.task = -1; temp1.level = 1; temp1.pos = curP; temp1.addr = forest[0][pos].childNum; temp1.dw = -forest[0][pos].weight; Q1.push(temp1); //update weight --N; curP = utils::discreteLog(forest[0][N].weight); forest[1][curP].children[forest[0][N].childNum] = pos; forest[0][pos] = forest[0][N]; //forest[0].erase(N); forest[0].pop_back(); processQueue(); }
void forestSample::add(double newW) { short newP; node newOne; wsum += newW; newP = utils::discreteLog(newW); //update weight newOne.weight = newW; newOne.childNum = forest[1][newP].children.size(); forest[0][N] = newOne; //forest[0].insert(std::pair<short,node>(N,newOne)); forest[0].push_back(newOne); //Add new parent to processing queue for insertion temp1.task = 1; temp1.level = 1; temp1.pos = newP; temp1.addr = N; temp1.dw = forest[0][N].weight; Q1.push(temp1); ++N; processQueue(); }
void forestSample::update(short pos, double newW) { short curP, newP; wsum += newW - forest[0][pos].weight; curP = utils::discreteLog(forest[0][pos].weight); newP = utils::discreteLog(newW); if (curP != newP) { //Add old parent to processing queue for deletion temp1.task = -1; temp1.level = 1; temp1.pos = curP; temp1.addr = forest[0][pos].childNum; temp1.dw = -forest[0][pos].weight; Q1.push(temp1); //update weight forest[0][pos].weight = newW; //Add new parent to processing queue for insertion temp1.task = 1; temp1.level = 1; temp1.pos = newP; temp1.addr = pos; temp1.dw = forest[0][pos].weight; Q1.push(temp1); } else { //Add parent to processing queue for updation temp1.task = 0; temp1.level = 1; temp1.pos = newP; temp1.addr = -1; temp1.dw = newW - forest[0][pos].weight; Q1.push(temp1); //Update weight forest[0][pos].weight = newW; } processQueue(); }
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(); } } } }