コード例 #1
0
ファイル: KNearest.cpp プロジェクト: snailcoder/PASS
UIntVector KNearest::Nearest(const RealCoord &state, 
						     const RealCoordVector &state_set) const {
  UIntVector neighbors;
  std::size_t state_num = state_set.size();
  if (state_num <= k_) {
    for (unsigned i = 0; i < state_num; ++i) {
      neighbors.push_back(i);
    }
  } else {
    double *dist = new double[state_num];
	for (unsigned i = 0; i < state_num; ++i) {
      dist[i] = metric_->Distance(state, state_set[i]);
	}
	for (unsigned i = 0; i <= k_; ++i) {
      neighbors.push_back(i);
	}
	for (unsigned i = 1; i < k_; ++i) {
      double di = dist[i];
      unsigned j = i - 1;
	  while (j != std::numeric_limits<unsigned>::max() && dist[i] < dist[j]) {
        dist[j + 1] = dist[j];
        neighbors[j + 1] = neighbors[j];
        --j;
      }
      dist[j + 1] = di;
      neighbors[j + 1] = i;
    }
    for (unsigned i = k_; i < state_num; ++i) {
      double di = dist[i];
      unsigned j = k_ - 1;
	  while (j != std::numeric_limits<unsigned>::max() && dist[i] < dist[j]) {
        dist[j + 1] = dist[j];
        neighbors[j + 1] = neighbors[j];
        --j;
	  }
      neighbors[j + 1] = i;
	}
    delete[] dist;
  }
  neighbors.pop_back();
  return neighbors;
}
コード例 #2
0
ファイル: CPythonCounter.cpp プロジェクト: zackkk/MIPS
/*!
* Extracts and stores logical lines of code.
* Determines and extract logical SLOC to place in the result variable
* using addSLOC function. Each time the addSLOC function is called,
* a new logical SLOC is added. This function assumes that the directive
* is handled before it is called.
*
* \param result counter results
* \param line processed physical line of code
* \param lineBak original physical line of code
* \param strLSLOC processed logical string
* \param strLSLOCBak original logical string
* \param paren_cnt count of parenthesis
* \param loopWhiteSpace count of white space to determine loop ends
*/
void CPythonCounter::LSLOC(results* result, string line, size_t lineNumber, string lineBak, string &strLSLOC, string &strLSLOCBak,
						   unsigned int &paren_cnt, UIntVector &loopWhiteSpace)
{
#define CONT_STR_LENGTH 18
	string continuation_str[] = {"is", "in", "not", "+", "-", "*", "/", "=", "<", ">", "|", "&", "%", "^", "\\", "~", ",", "$"};
	
	size_t start = 0;	// starting index of the working string
	size_t i = 0, idx, strSize;
	int n;
	bool trunc_flag = false;
	unsigned int cnt = 0, numWS;
	string exclude = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_$";

	string tmp;

	// process:
	// paren_cnt is used with {} [] ()
	// 1. check if the  current char is in one of the parentheses
	// 2.   if no, check if the line has : or ; (statement separators), except else:
	// 3.      if yes, count and put the statement in the result
	// 4. if the line does not ends with a continuation string or a statement separator (handled)
	//      and the line is not in one of the parentheses
	//      then count and put the statement in the result
	// 5. physical count considers all lines executables (or directives, no declarations)

	// check for loop ends, new loops, and record white space in order to determine ends
	if (print_cmplx)
	{
		// check white space for loop ends
		if (loopWhiteSpace.size() > 0)
		{
			// get white space
			tmp = line;
			tmp = CUtil::TrimString(tmp, -1);
			numWS = (unsigned)(line.length() - tmp.length());

			// check for loop ends
			for (n = (int)loopWhiteSpace.size() - 1; n >= 0; n--)
			{
				if (loopWhiteSpace.at(n) != numWS)
					break;
				else
					loopWhiteSpace.pop_back();
			}
		}

		// check for loop keywords (for, while)
		cnt = 0;
		CUtil::CountTally(line, loop_keywords, cnt, 1, exclude, "", "", NULL);
		if (cnt > 0)
		{
			if (loopWhiteSpace.size() < 1)
			{
				// get white space
				tmp = line;
				tmp = CUtil::TrimString(tmp, -1);
				numWS = (unsigned)(line.length() - tmp.length());
			}

			// add nested loop white space and record nested loop level
			for (i = 0; i < cnt; i++)
			{
				loopWhiteSpace.push_back(numWS);

				if ((unsigned int)result->cmplx_nestloop_count.size() < loopWhiteSpace.size())
					result->cmplx_nestloop_count.push_back(1);
				else
					result->cmplx_nestloop_count[loopWhiteSpace.size()-1]++;
			}
		}
	}

	line = CUtil::TrimString(line);
	lineBak = CUtil::TrimString(lineBak);
	size_t line_length = line.length();
	bool lineContinued = false;

	while (i < line_length)
	{
		switch (line[i])
		{
		case '{': case '[': case '(': // parentheses opener
			paren_cnt++;
			break;
		case '}': case ']': case ')': // parentheses closer
			if (paren_cnt > 0)
				paren_cnt--;
			break;
		}

		// 2. if no parentheses enclosing, and if the char is a statement separator
		if (paren_cnt == 0 && (line[i] == ';' || line[i] == ':'))
		{
			tmp = CUtil::ClearRedundantSpaces(line);
			// if line[..i] is else: then exit the outer if
			if (tmp.rfind("else:") != tmp.length() - 5)
			{
				// 3.
				strSize = CUtil::TruncateLine(i + 1 - start, strLSLOC.length(), this->lsloc_truncate, trunc_flag);
				if (strSize > 0)
				{
					strLSLOC += line.substr(start, i);
					strLSLOCBak += lineBak.substr(start, i);
				}
				if (result->addSLOC(strLSLOCBak, lineNumber, trunc_flag))
				{
					// increase logical SLOC here
					result->exec_lines[LOG]++;
				}
				strLSLOC = strLSLOCBak = "";
				start = i + 1;
			}
			else
				lineContinued = true;
		}
		i++;
	}

	if (paren_cnt == 0)
	{
		// add logical SLOC if the line does not end with a continuation string/char
		if (!lineContinued)
		{
			for (i = 0; i < CONT_STR_LENGTH; i++)
			{
				if (continuation_str[i].length() == 1)
				{
					if (line[line_length - 1] == continuation_str[i][0])
					{
						lineContinued = true;
						break;
					}
				}
				else
				{
					idx = CUtil::FindKeyword(line, continuation_str[i]);
					if (idx != string::npos && idx == line_length - continuation_str[i].length() - 1)
					{
						lineContinued = true;
						break;
					}
				}
			}
		}

		if (!lineContinued)
		{
			strSize = CUtil::TruncateLine(line_length - start, strLSLOC.length(), this->lsloc_truncate, trunc_flag);
			if (strSize > 0)
			{
				strLSLOC += line.substr(start, line_length);
				strLSLOCBak += lineBak.substr(start, line_length);
			}
			if (result->addSLOC(strLSLOCBak, lineNumber, trunc_flag))
			{
				// increase logical SLOC here
				result->exec_lines[LOG]++;
			}
			strLSLOC = strLSLOCBak = "";
		}
		else
		{
			tmp = CUtil::TrimString(line.substr(start, line_length - start));
			strSize = CUtil::TruncateLine(tmp.length(), strLSLOC.length(), this->lsloc_truncate, trunc_flag);
			if (strSize > 0)
			{
				strLSLOC += tmp.substr(0, strSize);
				tmp = CUtil::TrimString(lineBak.substr(start, line_length - start));
				strLSLOCBak += tmp.substr(0, strSize);
			}
		}
	}
	result->exec_lines[PHY]++;
}