Exemple #1
0
void pure_numeric_algo(){
    cout<<endl<<"pure_numeric_algo :"<<endl;
    int ia[11] = {0, 1, 2, 3, 4, 5, 6,6,6, 7, 8 };
    vector<int> iv(ia,ia+11);	vector<int> iv2(ia+6,ia+8);	vector<int>::iterator itr;
    itr = adjacent_find(iv.begin(),iv.end(), equal_to<int>());	//找到相邻元素相等的第一个元素
    cout<<"adjacent_find: "<<*itr<<endl;
    cout<<"count: "<<count(iv.begin(),iv.end(), 6)<<endl;	//找到元素值等于6的个数
    cout<<"count_if: "<<count_if(iv.begin(),iv.end(), bind2nd(less<int>() , 7))<<endl;		//找到小于7的元素个数
    itr = find(iv.begin(),iv.end(), 4);				//找到元素等于4的第一个元素位置
    cout<<"find: "<<*itr<<endl;
    itr = find_if(iv.begin(),iv.end(), bind2nd(greater<int>() , 2));				//找到元素大于2的第一个元素位置
    cout<<"find_if: "<<*itr<<endl;
    itr = find_end(iv.begin(),iv.end(), iv2.begin(),iv2.end());				//找到iv序列中最后子序列匹配出现的位置
    cout<<"find_end: "<<*(itr+3)<<endl;
    itr = find_first_of(iv.begin(),iv.end(), iv2.begin(),iv2.end());			//找到iv序列中最先子序列匹配出现的位置
    cout<<"find_end: "<<*(itr+3)<<endl;
    remove(iv.begin(),iv.end(), 6);				//删除元素,向前移,但是容器size不变,后面会剩余数据
    cout<<"remove: "<<iv<<endl;
    vector<int> iv3(12,-1);
    remove_copy(iv.begin(),iv.end(), iv3.begin(), 6);	//删除元素,将数据拷贝到新容器,后面会剩余数据
    cout<<"remove_copy: "<<iv3<<endl;
    remove_if(iv.begin(),iv.end(), bind2nd(less<int>(), 6));	//删除小于6的元素,后面会剩余数据
    cout<<"remove_if: "<<iv<<endl;
    remove_copy_if(iv.begin(),iv.end(), iv3.begin(), bind2nd(less<int>(), 7));		//删除小于7的元素,并拷贝到新容器
    cout<<"remove_copy_if: "<<iv3<<endl;
    replace(iv.begin(),iv.end(), 6, 3);			//将所有元素值为6的改为3
    cout<<"replace: "<<iv<<endl;
    replace_copy(iv.begin(),iv.end(),iv3.begin(), 3, 5);			//将所有元素值为3的改为5,结果保存在新容器中
    cout<<"replace_copy: "<<iv3<<endl;
    replace_if(iv.begin(),iv.end(), bind2nd(less<int>(),5), 2);			//将所有元素值小于5的改为2
    cout<<"replace_if: "<<iv<<endl;
    replace_copy_if(iv.begin(),iv.end(),iv3.begin(), bind2nd(equal_to<int>(),8), 9);			//将所有元素值为8的改为9,结果保存在新容器中
    cout<<"replace_copy_if: "<<iv3<<endl;
    reverse(iv.begin(),iv.end());			cout<<"reverse: "<<iv<<endl;		//反转
    reverse_copy(iv.begin(),iv.end(),iv3.begin());		cout<<"reverse_copy: "<<iv3<<endl;	//反转,结果保存在新容器
    rotate(iv.begin(),iv.begin() + 4, iv.end());	cout<<"rotate: "<<iv<<endl;			//互换元素
    rotate_copy(iv.begin(),iv.begin() + 5,iv.end(),iv3.begin());		cout<<"rotate_copy: "<<iv3<<endl;	//互换元素,结果保存在新容器
    int ia2[] = {2, 8};		vector<int> iv4(ia2,ia2+2);
    cout<<"search:  "<<*search(iv.begin(),iv.end(),iv4.begin(),iv4.end())<<endl;		//查找子序列出现的第一次出现地点
    swap_ranges(iv4.begin(),iv4.end(),iv.begin());				//按区域交换
    cout<<"swap_ranges:  "<<iv<<endl<<iv4<<endl;
    transform(iv.begin(),iv.end(),iv.begin(),bind2nd(minus<int>(), 2));		//所有元素减2
    cout<<"transform:  "<<iv<<endl;
    transform(iv4.begin(),iv4.end(),iv.begin(),iv4.begin(),plus<int>());		//区间对应元素相加
    cout<<"transform:  "<<iv4<<endl;
    /************************************************************************/
    vector<int> iv5(ia,ia+11);	vector<int> iv6(ia+4,ia+8);	vector<int> iv7(15);
    cout<<"max_element:  "<<*max_element(iv5.begin(), iv5.end())<<endl;		//最大元素游标
    cout<<"min_element:  "<<*min_element(iv5.begin(), iv5.end())<<endl;
    cout<<"includes:  "<<includes(iv5.begin(),iv5.end(),iv6.begin(),iv6.end())<<endl;	//iv6中元素是不是都在iv5中,这两个必须排过序
    merge(iv5.begin(),iv5.end(),iv6.begin(),iv6.end(),iv7.begin());	//两个排序号的容器合并
    cout<<"merge:  "<<iv7<<endl;
    partition(iv7.begin(),iv7.end(),bind2nd(equal_to<int>(), 5));	//满足条件的放在左边,不满足条件的放在右边
    cout<<"partition:  "<<iv7<<endl;
    unique(iv5.begin(),iv5.end());				//去重,重复的元素放在后面
    cout<<"unique:  "<<iv5<<endl;
    unique_copy(iv5.begin(),iv5.end(),iv7.begin());				//去重,结果保存在新容器
    cout<<"unique_copy:  "<<iv7<<endl;
}
// median of the nonzero elements of s.homework, or 0 if no such elements exist
double optimistic_median(const Student_info& s)
{
    std::vector<double> nonzero;
    remove_copy(s.homework.begin(), s.homework.end(),
        back_inserter(nonzero), 0);
    if (nonzero.empty())
        return grade(s.midterm, s.final, 0);
    else
        return grade(s.midterm, s.final, median(nonzero));
// Atualiza palavra
void Palavra::setPalavra( string word ) {

	//word[word.size() - 1] pega o ultimo caracter
	if( word[word.size() - 1] == '.' || word[word.size() - 1] == ',' || word[word.size() - 1] == '!' || word[word.size() - 1] == '?' ) { // Verifica qual e o ultimo caractere, nao funciona para espaco e enter pq o proprio file >> word ignora esses caracteres
		string result;
		remove_copy(word.begin(), word.end(), back_inserter(result), word[word.size() - 1]); // Pega a string sem o periodo
		palavra = result; // Salva a string sem o periodo
		periodo = word[word.size() - 1]; // Salva o periodo
	} else
		palavra = word; // Salva a string caso nao tenha periodo
}
void BuilderKaom::buildPattern() {
	//local variables used to store and modify sub-contents
	string entity, actEntity, entityID, pattern, key;
	unsigned int begPos, size, posMark, sclnPos;
	bool listEmpty;

	//extract the content from one keyword until the next keyword and store it as the working entity
	begPos = argsQ_.top().first;
	key = argsQ_.top().second;
	argsQ_.pop();
	if (!argsQ_.empty()) {
		entity = entity_.substr(begPos, argsQ_.top().first - begPos);
	} else {
		entity = entity_.substr(begPos);
	}

	//set the beginning to the end of the keyword
	//and search for a marked ending
	//or set the ending to the end of the working entity
	begPos = key.length();
	sclnPos = entity.find(";", begPos);
	if (sclnPos == string::npos) {
		cerr << "Missing semicolon after " << key << " in the annotation of " << entityType_ << endl;
		sclnPos = entity.length() - 1;
	}

	//test if there is a content before the ending
	if (begPos > sclnPos) {
		cerr << key << "the annotation of " << entityType_ << "is partially empty without a semicolon. -> skipping" << endl;
		key = "error";
	}

	//switch to the current keyword
	switch (key[0]) {
	//in case of kind
	case 'k':
		//compute the length of the entry and extract it
		size = sclnPos - begPos;
		entityType_ = entity.substr(begPos, size);

		//start a new entityMap_ entry
		mapEntry_ = " @portConstraints Free entity <ID@lias> \"<@lias>\" {";
		break;

		//in case of input or output
	case 'i':
	case 'o':
		//for all entries in the current entity
		do {
			//compute the length of the current content
			//if this is not the last entry then use the next comma
			//else use the semicolon as marked ending
			posMark = entity.find(",", begPos);
			if (posMark < sclnPos) {
				size = posMark - begPos;
				listEmpty = true;
			} else {
				size = sclnPos - begPos;
				listEmpty = false;
			}

			//check if the entry has content
			if (size != 0) {
				//extract the entry and update the position of the next comma
				actEntity = entity.substr(begPos, size);
				begPos = posMark + 1;

				//check if there is a label
				size = actEntity.find("\"");
				if (size != string::npos) {
					//extract the ID without blanks and find the second quote
					remove_copy(actEntity.begin(), actEntity.begin() + size, back_inserter(entityID), ' ');
					posMark = actEntity.find("\"", size + 1);

					//extract the label or skip it if it is corrupted
					if (posMark != string::npos) {
						actEntity = actEntity.substr(size + 1, posMark - 1 - size);
					} else {
						cerr << "found single a quote within " << key << " in the annotation of " << entityType_ << " -> skipping label" << endl;
						actEntity = actEntity.substr(0, size);
					}
				} else {
					//erase all blanks from the ID
					remove_copy(actEntity.begin(), actEntity.end(), back_inserter(entityID), ' ');
				}
				//replace all special chars in the ID
				replaceSpecChar(entityID);

				//build KAOM content for the port and add it to the entityMap_ entry
				pattern = "port <ID@lias>_" + entityID + " \"" + actEntity + "\";";
				mapEntry_.append(pattern);

				//update variables for next round
				entityID.clear();
			} else {
				//if there is no content skip the entry
				cerr << "Found two consecutive commas within " << key << " in the annotation of " << entityType_ << " -> skipping" << endl;
				begPos = posMark + 1;
			}
		} while (listEmpty);
		break;

		//in case of link
	case 'l':
		//for all entries in the current entity
		do {
			//compute the length of the current content
			//if this is not the last entry then use the next comma
			//else use the semicolon as marked ending
			posMark = entity.find(",", begPos);
			if (posMark < sclnPos) {
				size = posMark - begPos;
				listEmpty = true;
			} else {
				size = sclnPos - begPos;
				listEmpty = false;
			}

			//check if the entry has content
			if (size != 0) {
				//extract the entry without blanks and update the position of the next comma
				actEntity = entity.substr(begPos, size);
				begPos = posMark + 1;
				remove_copy(actEntity.begin(), actEntity.end(), back_inserter(entityID), ' ');

				//separate source and target of the link
				posMark = entityID.find("->");
				if (posMark != string::npos) {
					actEntity = entityID.substr(posMark + 2);
					entityID = entityID.substr(0, posMark);

					//build KAOM content for the target
					//separate function name and replace special chars
					//and check if the target is specified directly
					posMark = actEntity.rfind(":");
					replaceSpecChar(actEntity);
					if (posMark == string::npos) {
						actEntity = "<ID@lias>_" + actEntity;
					} else {
						actEntity.replace(posMark, 1, "_");
					}

					//build KAOM content for the source
					//separate function name and replace special chars
					//and check if the source is specified directly
					posMark = entityID.rfind(":");
					replaceSpecChar(entityID);
					if (posMark == string::npos) {
						entityID = "<ID@lias>_" + entityID;
					} else {
						//replace the colon with a underline
						entityID.replace(posMark, 1, "_");
					}

					//build KAOM content for the link  and add it to the entityMap_ entry
					pattern = "link " + entityID + " to " + actEntity + ";";
					mapEntry_.append(pattern);
				}

				//skip corrupted entries
				else {
					cerr << "found no arrow within " << key << " in the annotation of " << entityType_ << " -> skipping" << endl;
				}

				//update variables for next round
				entityID.clear();
			} else {
				cerr << "Found two consecutive commas within " << key << " in the annotation of " << entityType_ << " -> skipping" << endl;
				begPos = posMark + 1;
			}
		} while (listEmpty);
		break;

		//in case of content
	case 'c':
		//for all entries in the current entity
		do {
			//compute the length of the current content
			//if this is not the last entry then use the next comma
			//else use the semicolon as marked ending
			posMark = entity.find(",", begPos);
			if (posMark < sclnPos) {
				size = posMark - begPos;
				listEmpty = true;
			} else {
				size = sclnPos - begPos;
				listEmpty = false;
			}

			//check if the entry has content
			if (size != 0) {
				//extract the entry
				actEntity = entity.substr(begPos, size);

				//build KAOM content for the inner entity and add it to the entityMap_ entry
				pattern = "repl@ce " + actEntity + ";";
				mapEntry_.append(pattern);
			} else {
				//if there is no content skip the entry
				cerr << "Found two consecutive commas within " << key << " in the annotation of " << entityType_ << " -> skipping" << endl;
			}

			//update the position of the next comma
			begPos = posMark + 1;
		} while (listEmpty);
		break;

		//in case of toplevel
	case 't':
		//compute the length of the entry and extract it
		size = sclnPos - begPos;
		actEntity = entity.substr(begPos, size);

		//build KAOM content for the entity and add it to the result
		result_.append("repl@ce " + entityType_ + ":" + actEntity + ";");
		break;

	default:
		//nothing
		break;
	}
}
void BuilderKaom::buildKaom() {
	//local variables used to select and modify contents
	unsigned int begPos, size, posMark, rPos;
	bool remaindID;
	string actEntity, actType, entityID;
	entityID.clear();

	//complete the result and find the position of first replace occurrence in the result
	begPos = result_.find("repl@ce ");
	result_.append("}");

	//check if there is a start and then proceed with the filling of the model
	if (begPos != string::npos) {
		do {
			//calculate the call to the current entity and select it
			size = result_.find(";", begPos) - begPos;
			actEntity = result_.substr(begPos, size);

			//check if the call is not corrupted
			//and extract the arguments of the call
			posMark = actEntity.find(":");
			if (posMark != string::npos) {
				actType = actEntity.substr(8, posMark - 8);
				actEntity = actEntity.substr(posMark + 1);

				//check if the function name exists
				//and insert the appropriate pattern
				if (entityMap_.count(actType) > 0) {
					result_.replace(begPos, size + 1, entityMap_[actType]);

					//check if a label exists
					actType = actEntity;
					posMark = actEntity.find("\"");
					if (posMark != string::npos) {
						//select the ID and look for the next quote
						//if there is one select the label else select only the ID
						actType.erase(actType.begin() + posMark, actType.end());
						if (actEntity.find("\"", posMark + 1) != string::npos) {
							actEntity = actEntity.substr(posMark + 1, actEntity.find("\"", posMark + 1) - 1 - posMark);
						} else {
							cerr << "found single a quote within " << actEntity << " -> skipping label" << endl;
							actEntity = actType;
						}
					}

					//replace the special chars in the ID
					replaceSpecChar(actType);
					remove_copy(actType.begin(), actType.end(), back_inserter(entityID), ' ');

					//insert the label
					if (result_.find("<@lias>", begPos) < result_.find("repl@ce ", begPos)) {
						posMark = result_.find("<@lias>", begPos);
						result_.replace(posMark, 7, actEntity);
					}

					//insert all IDs
					do {
						//find the position of the next ID and the next replace marker
						posMark = result_.find("<ID@lias>", begPos);
						rPos = result_.find("repl@ce ", begPos);

						//check if the ID is in the current scope and insert the ID
						if (posMark < rPos) {
							result_.replace(posMark, 9, entityID);
							remaindID = true;
						}

						//count the closing braces and update the predecessor ID
						else {
							begPos = result_.find("}", begPos);
							while (begPos < rPos) {

								//for every closing brace erase one predecessor
								posMark = entityID.rfind("@@");
								posMark = (posMark == string::npos ? 0 : posMark);
								entityID.erase(entityID.begin() + posMark, entityID.end());
								begPos = result_.find("}", begPos + 1);
							}

							//if this is not a toplevel entity then add a marker for a predecessor
							if (entityID.length() > 0)
								entityID += "@@";
							remaindID = false;
						}
					} while (remaindID);

					//update beginning position to the next replace marker
					begPos = rPos;
				}

				//skip corrupted entries
				else {
					cerr << "found no object with ID " << actType << " -> skipping" << endl;
					result_.replace(begPos, size + 1, "");
					begPos = result_.find("repl@ce ", begPos);
				}
			} else {
				cerr << "found no colon in " << actEntity << " -> skipping" << endl;
				result_.replace(begPos, size + 1, "");
				begPos = result_.find("repl@ce ", begPos);
			}
		} while (begPos != string::npos);

		//replace all predecessor markers with underlines
		posMark = result_.rfind("@@");
		while (posMark != string::npos) {
			result_.replace(posMark, 2, "_");
			posMark = result_.rfind("@@");
		}

	} else {
		cerr << "found no toplevel for " << outputFileName_ << endl;
		isValid_ = false;
	}
}