Beispiel #1
0
void listCopy(forward_list<T> L, forward_list<T> &P) // reverse L and put it into P
{
    for(typename forward_list<T>::iterator start = L.begin() ; start != L.end() ; ++start)
    {
        P.push_front(*start);
    }
}
void copieListe(forward_list<TypeInfo> maListeOriginale, forward_list<TypeInfo>& maListecopiee) {
    // pour conserver l'ordre dans la copie il faut :
    // 1 empiler les valeurs de la liste originale en faisant cun parcours complet gauche droite de la liste
    // 2 dépiler les valeurs de la liste originale en les insérant en tête de la copie
    // on peut au choix utiliser la ListeChaineePile déjà définie, une liste chainée en avant de la STL, ou une pile de la STL

    // AVEC UNE LISTE de la STL
    forward_list<TypeInfo> maCopieInversee;
    // étape 1
    for (TypeInfo& val : maListeOriginale) {
        maCopieInversee.push_front(val);
    } // end for
    // étape 2
    for (TypeInfo& val : maCopieInversee) {
        maListecopiee.push_front(val);
    } // end for
    
    /*
    // AVEC UNE PILE de la STL
    stack<TypeInfo> maPile;
    // étape 1
    for (TypeInfo& val : maListeOriginale) {
        maPile.push(val);
    } // end for
    // étape 2
    while (!maPile.empty()) {
        maListecopiee.push_front(maPile.top());
        maPile.pop();
    } // end while
    */
}
Beispiel #3
0
static void parseQuotes(istringstream& result, forward_list<pair<string,quote_>>& quotes)
{
  string row;
  string date;
  float price[4];
  result.ignore(256, '\n');

  while(result.peek() != EOF)
  {
    getline(result, date, ',');
    result>> row;
    result.ignore();
    sscanf(row.c_str(), "%f,%f,%f,%f", &price[0], &price[1], &price[2], &price[3]);

    quotes.push_front(pair<string,quote_>(date, {price[0], price[1], price[2], price[3]}));
  }
}
// input: the reference to the hashing table, list1 and list2 from parser1, the reference to
//		the resultpointer) table/vector/array, the second tree
// output: the updated resultpointer) repository
// what's that:
// local list3: in-order position (in list1) list, vector<int> data type
// local h2: the stack behavior temporary hashing table (we always need a stack for the traversal)
// local h21: the final resultpointer) hashing table; it seems that we don't need h21
void parser2(char * tree, unordered_map<long int, double> * resultpointer)
{
	//unordered_map<double, forward_list<MRCA>> h2, h21;
	// temporarily we dob't consider the h21
	unordered_map<double, forward_list<MRCA *>> h2;

	// the following stack is local; should be released after this function
	forward_list<double> stack;

	// to get the actual start of a tree; from the "(" to start
	int i = 0;
	while(tree[i] != '(')i++;

	while(tree[i] != ';')
	{
		if(tree[i] == ',' || tree[i] == ' ' || tree[i] == ';')
		{
			i++;
			continue;
		}

		if(tree[i] == '(')
		{
			i++;
			stack.push_front(-1);
			continue;
		}

		if(tree[i] >= 48 && tree[i] <= 57)
		{

			char node[5];
			int j = 0;
			while(tree[i] != '.')
			{
				node[j++] = tree[i++];
			}
			node[j] = '\0';
			long int node_value = string_long(node);
			while(tree[i++] != ':');
			char tmrca[20];  // I give this double type number 20 effective digits; it is just kidding of course
			j = 0;
			while(tree[i] != ',' && tree[i] != ')' && tree[i] != '\0')
			{
				tmrca[j++] = tree[i];
				i++;
			}
			tmrca[j] = '\0';
			double tmrca_value = string_double(tmrca);

			// for the list and the stack:
			// the list is used to put all the leaves in-order, using their previous positions
			// the stack is usual as the previous one, assisting in the stack operation
			list3.push_back(list2[node_value-1]);

			/*
			// DEBUG: see the positions of specific nodes in list3
			if(node_value ==  64 || node_value == 170)
			{
				cout << node_value << " " << int(list3.size()-1) << endl;
			}
			*/

			if(stack.front() == -1)
			{
				stack.front() = tmrca_value;
				double tmrca_value1 = partial_tMRCA(tmrca_value, PRECISION);

				std::unordered_map<double, forward_list<MRCA *>>::const_iterator got = h2.find(tmrca_value1);
  				if(got == h2.end())
  				{
  					forward_list<MRCA *> temp;
  					MRCA * mrcapointer = (MRCA *)malloc(sizeof(MRCA));
					(*mrcapointer) = {tmrca_value, int(list3.size()-1), int(list3.size()-1), 0};
  					temp.push_front(mrcapointer);
  					h2[tmrca_value1] = temp;
  					//h2.emplace(partial_tMRCA(stack.front(), PRECISION), temp);
  				}
  				else
  				{
  					MRCA * mrcapointer = (MRCA *)malloc(sizeof(MRCA));
  					(*mrcapointer) = {tmrca_value, int(list3.size()-1), int(list3.size()-1), 0};
  					h2[tmrca_value1].push_front(mrcapointer);
					//got->second.push_front(mrca);
  					//h2[tmrca_value1].push_front(mrca);
  				}
			}
			else
			{
				h2[partial_tMRCA(stack.front(), PRECISION)].front()->end = list3.size()-1;
			}
			continue;
		}


		if(tree[i] == ')')
		{
			double tmrca_value = stack.front();
			double tmrca_value1 = partial_tMRCA(tmrca_value, PRECISION);
			// perform the intersection from here

			std::unordered_map<double, forward_list<MRCA *>>::const_iterator got1 = h11.find(tmrca_value1);
			double key1 = int((tmrca_value1 + PRECISION) * (1/PRECISION) + NUMERIC) * 1.0 / (1/PRECISION);	// the float number will always have problems, which is frustrating
			std::unordered_map<double, forward_list<MRCA *>>::const_iterator got2 = h11.find(key1);
			double key2 = int((tmrca_value1 - PRECISION) * (1/PRECISION) + NUMERIC) * 1.0 / (1/PRECISION);	// the float number will always have problems, which is frustrating
			std::unordered_map<double, forward_list<MRCA *>>::const_iterator got3 = h11.find(key2);
			if(got1 != h11.end() || got2 != h11.end() || got3 != h11.end())
			{
				int start = h2[tmrca_value1].front()->start;
				int middle = h2[tmrca_value1].front()->middle;
				int end = h2[tmrca_value1].front()->end;
				intersection(tmrca_value, start, middle, end, resultpointer);
			}

			/*
			// DEBUG
			if(tmrca_value1 == 124.8)
			{
				printf("@@\n");
				cout << key1 << endl;
				cout << h2[tmrca_value1].front()->start << endl;
				cout << h2[tmrca_value1].front()->middle << endl;
				cout << h2[tmrca_value1].front()->end << endl;
				printf("@@\n");
			}
			*/


			// judge whether or not it is the end of the string
			if(tree[i+1] != ';')
			{
				// we can get a tP here
				i+=2;
				char tmrca[20];  // I give this double type number 20 effective digits
				int j = 0;
				while(tree[i] != ',' && tree[i] != ')' && tree[i] != '\0')
				{
					tmrca[j++] = tree[i];
					i++;
				}
				tmrca[j] = '\0';
				double tP = string_double(tmrca);
				
				// get the updated tMRCA and the concatenated list
				// get the information from the un-useful element
				double tmrca_value1 = partial_tMRCA(stack.front(), PRECISION);
				int start = h2[tmrca_value1].front()->start;
				int end = h2[tmrca_value1].front()->end;
				rubbish.push_front(h2[tmrca_value1].front());
				h2[tmrca_value1].pop_front();

				// we don't care h21 any more because we are only testing this algorithm
				// no we should care, because h21 is used to store the final resultpointer), and h2 for stack
				// we have performd the intersection already, so just ignore the h21
				// the following is Python codes --!
				//list_temp1 = h2[stack[-1]//1].pop()
				//if stack[-1]//1 in h21:
				//	h21[stack[-1]//1].append(list_temp1)
				//else:
				//	h21[stack[-1]//1] = [list_temp1]

				double tmrca_value = stack.front() + tP;
				tmrca_value1 = partial_tMRCA(tmrca_value, PRECISION);
				stack.pop_front();

				// judge this is a left sub-tree or a right sub-tree
				if(stack.front() == -1)
				{
					// this is a left sub-tree
					stack.front() = tmrca_value;

					std::unordered_map<double, forward_list<MRCA *>>::const_iterator got = h2.find(tmrca_value1);
	  				if(got == h2.end())
	  				{
	  					forward_list<MRCA *> temp;
	  					MRCA * mrcapointer = (MRCA *)malloc(sizeof(MRCA));
						(*mrcapointer) = {tmrca_value, start, end, 0};
	  					temp.push_front(mrcapointer);
	  					h2[tmrca_value1] = temp;
	  					//h11.emplace(partial_tMRCA(stack.front(), PRECISION), temp);
	  				}
	  				else
	  				{
	  					MRCA * mrcapointer = (MRCA *)malloc(sizeof(MRCA));
						(*mrcapointer) = {tmrca_value, start, end, 0};
	  					h2[tmrca_value1].push_front(mrcapointer);
						//got->second.push_front(mrca);
	  					//h2[tmrca_value1].push_front(mrca);
	  				}
				}
				else
				{
					// this is a right sub-tree
					h2[partial_tMRCA(stack.front(), PRECISION)].front()->end = end;
				}
			}
			else
			{
				// here we are finished the parsing; just continue
				i++;  // --> for break out the while loop

				// remember to move up here!!
				double tmrca_value1 = partial_tMRCA(stack.front(), PRECISION);
				rubbish.push_front(h2[tmrca_value1].front());
				h2[tmrca_value1].pop_front();
				// don't need to move up, because we don't use h21 any more!!
				/*

				std::unordered_map<double, forward_list<MRCA>>::const_iterator got = h21.find(partial_tMRCA(stack.front(), PRECISION));
  				if(got == h21.end())
  				{
  					forward_list<MRCA> temp;
  					temp.push_front(mrca);
  					h21[partial_tMRCA(stack.front(), PRECISION)] = temp;
  					//h21.emplace(partial_tMRCA(stack.front(), PRECISION), temp);
  				}
  				else
  				{
  					h21[partial_tMRCA(stack.front(), PRECISION)].push_front(mrca);
					//got->second.push_front(mrca);
  					//h21[tmrca_value1].push_front(mrca);
  				}
  				*/
  				stack.pop_front();
			}
			continue;
		}
	}
	return;
}