Beispiel #1
0
void insertAfterStr(forward_list<string> &flist, string flag, string insert){
	auto pre = flist.before_begin();
	auto item = flist.begin();
	while (item != flist.end() && *item != flag)
		pre++, item++;
	item == flist.end() ? flist.insert_after(pre, insert) : flist.insert_after(item, insert);
}
Beispiel #2
0
forward_list<string> func(forward_list<string> &il, string s1, string s2) {
    auto iter = il.begin();
    auto prev = il.before_begin();
    for (; iter != il.end(); prev = iter++)
        if (*iter == s1)
            break;
    if (iter == il.end())
        il.insert_after(prev, s2);
    else
        il.insert_after(iter, s2);
    return il;
}
Beispiel #3
0
void insertString(forward_list<string>& fls,string target,string insertval){
	auto cur=fls.begin();
	auto prev=fls.before_begin();
	while(cur!=fls.end()&&*cur!=target) {
	   prev=cur;
	   ++cur;
	}
	if(cur!=fls.end())
		fls.insert_after(cur,insertval);
	else{
		fls.insert_after(prev,insertval);
	}
}
forward_list<int> LinkedListsExpert::partition(forward_list<int> list, int x){
    forward_list<int> lessThanX, greaterThanX, equalX;
    forward_list<int>::iterator it = list.begin();
    while (it!=list.end()) {
        if (*it < x) {
            lessThanX.push_front(*it);
        } else if (*it > x){
            greaterThanX.push_front(*it);
        } else{
            equalX.push_front(*it);
        }
        it++;
    }
    //merge lists
    it = equalX.begin();
    while (it != equalX.end()) {
        greaterThanX.push_front(*it);
        it++;
    }
    it = lessThanX.begin();
    while (it != lessThanX.end()) {
        greaterThanX.push_front(*it);
        it++;
    }
    return greaterThanX;
}
Beispiel #5
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);
    }
}
Beispiel #6
0
void insertString(forward_list<string> &strflst, const string &search, const string &str)
{
	auto prev = strflst.before_begin();
	auto curr = strflst.begin();
	if (!strflst.empty())
	{
		bool flag = false;
		while(curr != strflst.end())
		{
			if (*curr == search)
			{
				curr = strflst.insert_after(curr, str);
				flag = true;
			}
			else
			{
				prev = curr++;
			}
		}
		if (!flag)
		{
			strflst.insert_after(prev, str);
		}
	}
}
Beispiel #7
0
void printLots(forward_list<T> L, forward_list<int> P)
{
    int num_elem = 0;
    
    for (typename forward_list<T>::iterator count = L.begin() ; count != L.end() ; ++count)
    {
        num_elem = num_elem + 1;
    }
    
    while(!P.empty())
    {
        int pos = P.front();
        
        if(pos >= num_elem)
        {
            cout << "Position out of bounds!" << endl;
            return;
        }
        
        typename forward_list<T>::iterator start = L.begin();
        
        for(int i = 0 ; i < pos ; i++)
        {
            ++start;
        }
        
        cout << *start << ", ";
        
        P.pop_front();
    }
    
    cout << endl;
}
void LinkedListsExpert::printList(forward_list<int> list){
    std::forward_list<int>::iterator it;
    for (it = list.begin(); it != list.end(); it++) {
        cout << *it << "  ";
    }
    cout << "\n";
}
	void printTroopers(void) {
		for (atk_pos = attackers_fw_list.begin(); atk_pos != attackers_fw_list.end(); atk_pos++) {
			cout << "Runde:" << atk_pos->getRound() << "   --X:" << atk_pos->getLine() << "--Y:" << atk_pos->getY()
					<< "--Owner:" << atk_pos->getOwner() << "--Range:" << atk_pos->getRange() << "--DMG:"
					<< atk_pos->getRange() << "--HP:" << atk_pos->getHp() << "--Movement:" << atk_pos->getMovement()
					<< endl << endl;
		}
	};
int LinkedListsExpert::getNthToLast(forward_list<int> list, int n){
    forward_list<int>::iterator current = list.begin();
    forward_list<int>::iterator previous = list.begin();
    
    for(int i=1; i<=n; i++){
        if (current != list.end()) {
            current++;
        }
    }
    
    while (current != list.end()) {
        current++;
        previous++;
    }
    
    return *previous;
}
Beispiel #11
0
TypeInfo getInfoAtPosit(forward_list<TypeInfo> laListe, int laPosition) throw (PrecondVioleeExcep) {
    // version itérative
    // un itérateur sur la liste
    auto it = laListe.begin();
    int positionCourante = 1;
    // tantque l'on a pas atteint laPosition et que l'on est pas à la fin de la liste
    while ((positionCourante < laPosition) && (it != laListe.end())) {
        positionCourante++;
        // avancer dans la liste sur l'itérateur
        ++it;
    }
    if (it != laListe.end()) { // si on eu accès à laPosition
        // rendre l'élément pointé par l'itérateur
        return *it;
    } else { // sinon on lève une exception
        throw PrecondVioleeExcep("Accès imposible à l'indice " + to_string(laPosition));
    }
}
Beispiel #12
0
T find_last_kth(const forward_list<T>& l, int k)
{
    assert(k>=0);

    auto it = l.begin();
    for (int i=0; i<k; ++i) {
        if (it==l.end()) {
            return l.front();
        }
        ++it;
    }
    auto ret = l.begin();
    while (it!=l.end()) {
        ++it;
        ++ret;
    }
    return *ret;
}
Beispiel #13
0
void display(forward_list<T> lst)
{
    
    for(typename forward_list<T>::iterator start = lst.begin(); start != lst.end() ; ++start)
    {
        cout << *start << " ";
    }
    
    cout << endl;
}
Beispiel #14
0
void insereInfoAtPosit(forward_list<T1>& maListeOriginale, int laPosition, T2 nouvelleInfo) throw (PrecondVioleeExcep) {
    // version itérative
    // itérateur positionné sur la postion avant le premier élément de la liste
    //  -> il faut être sur le précédent de laPosition pour fair l'insertion après !!
    auto it = maListeOriginale.before_begin();
    int positionCourante = 1;
    // tantque l'on a pas atteint laPosition et que l'on est pas à la fin de la liste
    while ((positionCourante < laPosition) && (it != maListeOriginale.end())) {
        positionCourante++;
        // avancer dans la liste sur l'itérateur
        ++it;
    }
    if (it != maListeOriginale.end()) { // si on a accès à laPosition
        // insérer après l'élément pointé par l'itérateur
        it = maListeOriginale.insert_after(it, nouvelleInfo);
    } else { // sinon on lève une exception
        throw PrecondVioleeExcep("Insertion imposible à l'indice " + to_string(laPosition));
    }
}
Beispiel #15
0
void remove_evens_and_double_odds(forward_list<int>& data) {
    for (auto cur = data.begin(), prev = data.before_begin(); cur != data.end(); ) {
        if (*cur & 0x1)
            cur = data.insert_after(prev, *cur),
            advance(prev, 2),
            advance(cur, 2);
        else
            cur = data.erase_after(prev);
    }
}
Beispiel #16
0
void updateSounds() {
	auto prevIt = sounds.before_begin();
	for (auto it = sounds.begin(); it != sounds.end(); it++) {
		if (it->getStatus() == sf::Sound::Stopped) {
			sounds.erase_after(prevIt);
			it = prevIt;
		}
		else prevIt = it;
	}
}
Beispiel #17
0
template <typename T> void printList(const forward_list<T> &listRef)
{
	if (listRef.empty())
		cout<<"List is empty";
	else
	{
		ostream_iterator<T> output(cout, " ");
		copy(listRef.begin(), listRef.end(), output);
	}
}
Beispiel #18
0
void insertWord(forward_list<string> &forList,
                                           const string &str1,const string &str2)
{
    auto prev = forList.before_begin();
    for(auto i=forList.begin();i != forList.end();prev=i,++i)
        if(*i == str1){
            forList.insert_after(i,str2);
            return ;
        }
   forList.insert_after(prev,str2);
}
Beispiel #19
0
void find_and_insert(forward_list<string> &list, string const& to_find, string const& to_add)
{
    auto prev = list.before_begin();
    for (auto curr = list.begin(); curr != list.end(); prev = curr++)
    {
        if (*curr == to_find)
        {
            list.insert_after(curr, to_add);
            return;
        }
    }
    list.insert_after(prev, to_add);
}
Beispiel #20
0
// Merge a connected compontent of lines to a new line with width.
void merge(const forward_list<LineSeg*>& component, LineSegW& out) {
	// TODO: in principle no loop/collection is necessary
	// Note: profiling indicates this is not even close to a bottleneck, so ok for now
	forward_list<Point*> component2i;
	for (auto vec = component.begin(); vec != component.end(); ++vec) {
		component2i.push_front(&(*vec)->s);
		component2i.push_front(&(*vec)->e);
		//component2i.push_front((Vec2i*)&(*vec)->val[0]);
		//component2i.push_front((Vec2i*)&(*vec)->val[2]);
	}

	line_fit(component2i, out);
}
Beispiel #21
0
void func(forward_list<string> &flist, const string s1, const string s2)
{
	auto before = flist.before_begin();
	auto curr = flist.begin();
	while(curr!=flist.end()) {
		if(*curr == s1){
			flist.insert_after(curr, s2);
			return;
		}else{
			curr++;
		}
	}
	flist.insert_after(curr, s2);
}
Beispiel #22
0
bool Case::ajouterPions(forward_list<Pion*> fl, int nbPionsToAdd) {
  if(nbPions + nbPionsToAdd > nbPionsMax) {
    cout << "[Case.cpp/ajouterPions] Erreur : Impossible de rajouter les pions. Pas assez de place." << endl;
    return false;
  }
  else {
    listePions.splice_after(listePions.before_begin(), fl);
    nbPions += nbPionsToAdd;
    
    for(auto it=fl.begin() ; it!=fl.end() ; ++it)
      (*it)->setPosition(position);
    
    return true;
  }
}
Beispiel #23
0
bool Case::retirerPions(forward_list<Pion*> fl, int nbPionsToDel) {
  bool all_deleted = true;
  if(nbPions - nbPionsToDel < 0) {
    cout << "[Case.cpp/retirerPions] Erreur : Impossible de retirer les pions. Pas assez de pions." << endl;
    return false;
  }
  else {
    for(auto it=fl.begin() ; it!=fl.end(); ++it) {
      //cout << "RETIRER PIONS ON PASSE : " << *it << endl;
      if(!retirerPion(*it)) {
	cout << "[Case.cpp/retirerPions] Erreur : Impossible de retirer le pion. Il n'est pas present." << endl;
	all_deleted = false;
      }
    }
  }
  return all_deleted;
}
Beispiel #24
0
// input: two trees waiting to be compared, the resultpointer) repository
// output: the updated resultpointer) repository (it's a hashing table with "long int: int" pair in)
void IBD_hash(char * tree1, char * tree2, unordered_map<long int, double> * resultpointer)
{
	// NOTE:
	// in practice, we can store the second hashing table, and when next time we enter, we can directly use it
	// we can achieve this by storing the second hashing table as global in this scope

	(*resultpointer).clear();

	// clean the global variables
	h11.clear();
	list1.clear();
	list3.clear();
	rubbish.clear();

	parser1(tree1);

	// DEBUG
	/*
	for(auto itr = h11[124.8].begin(); itr != h11[124.8].end(); itr ++)
	{
		cout << (*itr)->tMRCA << ":" << (*itr)->start << " " << (*itr)->middle << " " << (*itr)->end << " " << endl;
	}
	*/


	parser2(tree2, resultpointer);

	// empty the temporary rubbish pool
	for(auto itr = rubbish.begin(); itr != rubbish.end(); itr ++)
	{
		free(*itr);
	}

	// free the h11 memory for fear that there will be memory overflow
	for(auto itr = h11.begin(); itr != h11.end(); itr ++)
	{
		// (*itr) is a forward_list<MRCA *>
		for(auto itr1 = (*itr).second.begin(); itr1 != (*itr).second.end(); itr1 ++)
		{
			// (*itr1) is a (MRCA *)
			free(*itr1);
		}
	}
	return;
}
Beispiel #25
0
// Merge a connected compontent of lines to a new line with width.
void merge(const forward_list<LineSegW*>& component, LineSegW& out) {
	// TODO: in principle no loop/collection is necessary
	// Note: profiling indicates this is not even close to a bottleneck, so ok for now

	float maxw = 0.0f;

	forward_list<Point*> component2i;
	for (auto vec = component.begin(); vec != component.end(); ++vec) {
		component2i.push_front(&(*vec)->s);
		component2i.push_front(&(*vec)->e);
		if ((*vec)->width > maxw) {
			maxw = (*vec)->width;
		}
	}

	line_fit(component2i, out);
	out.width = max(maxw, out.width);
}
forward_list<int> LinkedListsExpert::deleteDuplicates(forward_list<int> list){
    forward_list<int>::iterator current = list.begin();
    forward_list<int>::iterator previous = list.begin();
    
    unordered_map<int, int> table;
    table[*current] = 1;
    current++;
    while (current != list.end()) {
        if (table.count(*current) == 0) {
            table[*current] = 1;
            current++;
            previous++;
        } else{ //delete node
            current = list.erase_after(previous);
        }
    }
    return list;
}
Beispiel #27
0
inline int primeCount(forward_list<int> lst, forward_list<int>::iterator &itr)
{
    if (itr == lst.end()) //base case once the iterator reaches the end
    {
        return 0;
    }
    
    else if (isPrime(*itr) == true) //if the # is prime, add one
    {
        return 1 + primeCount(lst, ++itr);
    }
    
    else //if not add 0 and do recursive call 
    {
        return 0 + primeCount(lst, ++itr);
    }
    
    return -1; 
}
Beispiel #28
0
void replace(forward_list<string> &sflst, const string &str1, const string &str2)
{
        int     flag = 0;
        auto    prev = sflst.before_begin();
        auto    curr = sflst.begin();
        auto    end = sflst.end();
        while (curr != end)
                if (*curr == str1)
                {
                        curr = sflst.emplace_after(curr, str2);
                        flag = 1;
                }
                else
                {
                        prev = curr;
                        ++curr;
                }
        if (!flag)
                sflst.insert_after(prev, str2);
}
void FindStringInsertString(forward_list<string>& strlist, string strtofind, string strtoinsert)
{
	forward_list<string>::iterator current = strlist.begin();
	forward_list<string>::iterator pre = strlist.before_begin();
	bool findflag = false;
	while (current != strlist.end())
	{
		if (*current == strtofind)
		{
			current = strlist.insert_after(current,strtoinsert);
			findflag = true;
		}
		pre = current;
		current++;
	}
	
	if (!findflag)
	{
		strlist.insert_after(pre, strtoinsert);
	}
	return;
}