Esempio n. 1
0
RcppExport SEXP LocalRecProg_cpp(SEXP data, SEXP K_Level_R, SEXP FindLowestK_R, SEXP ancestor_R, SEXP weight_R, SEXP range_R, SEXP categoryCount_R, SEXP LowMemory_R, SEXP g_MissingValue_R)
{
BEGIN_RCPP
  int i, j;
  int c2;
  int K_Level;
  //uint TimeStart = TimeGetMilliSecond(), TimeMid;
  EMatch Match = e_Match_Complete;
  BOOL FindLowestK = FALSE, LowMemory = FALSE;

    //============= Init
  Uninit_LocalRec();

  g_Debug_LocalRec = FALSE;
  g_Output = e_Output_Average;
  g_Epsilon = es_Epsilon;

  Rcpp::NumericMatrix Mat_LocalRec(data);  // creates Rcpp matrix from SEXP
  g_NbRow_LocRec = Mat_LocalRec.rows();
  int NbVar_LocRec = Mat_LocalRec.cols();
  // NbVar_LocRec = NbTotalVar / 2;
  //=============init Parameters
  K_Level = Rcpp::as<int>(K_Level_R);
  //  printf("klevel %d \n",K_Level);
  if(K_Level >= 1) Match = e_Match_Kneib;
  if(Rcpp::as<bool>(range_R)) g_Output = e_Output_Range;
  FindLowestK = Rcpp::as<bool>(FindLowestK_R);
  LowMemory = Rcpp::as<bool>(LowMemory_R);
  double g_MissingValue_LocalRec = Rcpp::as<double>(g_MissingValue_R);
  if(g_MissingValue_LocalRec == 0) g_MissingValue_LocalRec = -1.0f;
  // Ancestors
  // 1. Column: Position of the variable with ancestor variables (0-based)
  // 2. Column: number of ancestor variables
  Rcpp::NumericMatrix ancestors(ancestor_R);
  int ancestorLen = ancestors.rows();
  int ancestorCol = ancestors.cols();
  if(ancestorLen > 1 && ancestorCol>1)
  {
    for(j=0; j < ancestorLen; j++)
    {
      NbVar_LocRec -= ancestors(j,1);
    }
    // NbVar_LocRec = NbTotalVar / 2;
    // NbVar_LocRec: position of the first ancestor variable
    // ancestors: ancestor array
    CData::InitAncestors(NbVar_LocRec, ancestors);
  }
  Rcpp::NumericMatrix res(g_NbRow_LocRec, NbVar_LocRec); // result matrix that is returned

  // weight-vector:
  // 1. Column: Value of the weight for the variable
  // 2. Column: TRUE/FALSE if variable is of type categorial
  Rcpp::NumericMatrix weight(weight_R);
  if(NbVar_LocRec == weight.rows()) {
    CData::Init(NbVar_LocRec, weight);
  } else {
    Uninit_LocalRec();
    return Rcpp::wrap("Error: Not enough weights specified");
  }

  bool categoryCount = Rcpp::as<bool>(categoryCount_R);
  if(categoryCount) {
    CData::m_CategoryCountVar = TRUE;
    --NbVar_LocRec;
  }
  //printf("NbVar_LocRec %d \n",NbVar_LocRec);
  if (g_NbRow_LocRec <= 0 || CData::m_NbVariable <= 0)
  {
    Uninit_LocalRec();
    return Rcpp::wrap(-3);
  }

  if (Match == e_Match_Kneib
    && (K_Level >= g_NbRow_LocRec || K_Level < 1))
  {
    Uninit_LocalRec();
    return Rcpp::wrap(-4);
  }
    //============= Data Allocation & Loading
  g_Data = new CData[g_NbRow_LocRec];

  if (LoadData(g_NbRow_LocRec, g_MissingValue_LocalRec, g_Data, Mat_LocalRec) != g_NbRow_LocRec)
  {
    Uninit_LocalRec();
    return Rcpp::wrap(-5);
  }

  if (!LowMemory && g_NbRow_LocRec > 1 && CData::m_NbVariable > 2)
  {
    TDist *pAllDist = new TDist[g_NbRow_LocRec * (g_NbRow_LocRec - 1) / 2];
    int NbEntry = 0;

    ForLoop (i, g_NbRow_LocRec)
    {
      g_Data[i].m_pDist = pAllDist + NbEntry;

      int NbDist = g_NbRow_LocRec - i - 1;

      ForLoop (j, NbDist)
        g_Data[i].m_pDist[j] = dist(g_Data + i, g_Data + i + j + 1);

      --g_Data[i].m_pDist;    //Optim: avoid to have to do it each time dist() is called
      NbEntry += NbDist;
    }
Esempio n. 2
0
void TemplateFileServer::Serve(HTTPProvider *server, const Anope::string &page_name, HTTPClient *client, HTTPMessage &message, HTTPReply &reply, Replacements &r)
{
	int fd = open((template_base + "/" + this->file_name).c_str(), O_RDONLY);
	if (fd < 0)
	{
		Log(LOG_NORMAL, "httpd") << "Error serving file " << page_name << " (" << (template_base + "/" + this->file_name) << "): " << strerror(errno);

		client->SendError(HTTP_PAGE_NOT_FOUND, "Page not found");
		return;
	}

	Anope::string buf;

	int i;
	char buffer[BUFSIZE];
	while ((i = read(fd, buffer, sizeof(buffer) - 1)) > 0)
	{
		buffer[i] = 0;
		buf += buffer;
	}

	close(fd);

	Anope::string finished;

	bool escaped = false;
	for (unsigned j = 0; j < buf.length(); ++j)
	{
		if (buf[j] == '\\' && j + 1 < buf.length() && (buf[j + 1] == '{' || buf[j + 1] == '}'))
			escaped = true;
		else if (buf[j] == '{' && !escaped)
		{
			size_t f = buf.substr(j).find('}');
			if (f == Anope::string::npos)
				break;
			const Anope::string &content = buf.substr(j + 1, f - 1);

			if (content.find("IF ") == 0)
			{
				std::vector<Anope::string> tokens;
				spacesepstream(content).GetTokens(tokens);

				if (tokens.size() == 4 && tokens[1] == "EQ")
				{
					Anope::string first = FindReplacement(r, tokens[2]), second = FindReplacement(r, tokens[3]);
					if (first.empty())
						first = tokens[2];
					if (second.empty())
						second = tokens[3];

					bool stackok = IfStack.empty() || IfStack.top();
					IfStack.push(stackok && first == second);
				}
				else if (tokens.size() == 3 && tokens[1] == "EXISTS")
				{
					bool stackok = IfStack.empty() || IfStack.top();
					IfStack.push(stackok && r.count(tokens[2]) > 0);
				}
				else
					Log() << "Invalid IF in web template " << this->file_name;
			}
			else if (content == "ELSE")
			{
				if (IfStack.empty())
					Log() << "Invalid ELSE with no stack in web template" << this->file_name;
				else
				{
					bool old = IfStack.top();
					IfStack.pop(); // Pop off previous if()
					bool stackok = IfStack.empty() || IfStack.top();
					IfStack.push(stackok && !old); // Push back the opposite of what was popped
				}
			}
			else if (content == "END IF")
			{
				if (IfStack.empty())
					Log() << "END IF with empty stack?";
				else
					IfStack.pop();
			}
			else if (content.find("FOR ") == 0)
			{
				std::vector<Anope::string> tokens;
				spacesepstream(content).GetTokens(tokens);

				if (tokens.size() != 4 || tokens[2] != "IN")
					Log() << "Invalid FOR in web template " << this->file_name;
				else
				{
					std::vector<Anope::string> temp_variables, real_variables;
					commasepstream(tokens[1]).GetTokens(temp_variables);
					commasepstream(tokens[3]).GetTokens(real_variables);

					if (temp_variables.size() != real_variables.size())
						Log() << "Invalid FOR in web template " << this->file_name << " variable mismatch";
					else
						ForLoop::Stack.push_back(ForLoop(j + f, r, temp_variables, real_variables));
				}
			}
			else if (content == "END FOR")
			{
				if (ForLoop::Stack.empty())
					Log() << "END FOR with empty stack?";
				else
				{
					ForLoop &fl = ForLoop::Stack.back();
					if (fl.finished(r))
						ForLoop::Stack.pop_back();
					else
					{
						fl.increment(r);
						if (fl.finished(r))
							ForLoop::Stack.pop_back();
						else
						{
							j = fl.start; // Move pointer back to start of the loop
							continue; // To prevent skipping over this block which doesn't exist anymore
						}
					}
				}
			}
			else if (content.find("INCLUDE ") == 0)
			{
				std::vector<Anope::string> tokens;
				spacesepstream(content).GetTokens(tokens);

				if (tokens.size() != 2)
					Log() << "Invalid INCLUDE in web template " << this->file_name;
				else
				{
					if (!finished.empty())
					{
						reply.Write(finished); // Write out what we have currently so we insert this files contents here
						finished.clear();
					}

					TemplateFileServer tfs(tokens[1]);
					tfs.Serve(server, page_name, client, message, reply, r);
				}
			}
			else
			{
				// If the if stack is empty or we are in a true statement
				bool ifok = IfStack.empty() || IfStack.top();
				bool forok = ForLoop::Stack.empty() || !ForLoop::Stack.back().finished(r);

				if (ifok && forok)
				{
					const Anope::string &replacement = FindReplacement(r, content.substr(0, f - 1));
					finished += replacement;
				}
			}

			j += f; // Skip over this whole block
		}
		else
		{
			escaped = false;

			// If the if stack is empty or we are in a true statement
			bool ifok = IfStack.empty() || IfStack.top();
			bool forok = ForLoop::Stack.empty() || !ForLoop::Stack.back().finished(r);

			if (ifok && forok)
				finished += buf[j];
		}
	}

	if (!finished.empty())
		reply.Write(finished);
}