<commit_before>#include "image.h"

LicenseSymbolsArea::LicenseSymbolsArea(cv::Mat& mframe, std::vector<mArea>& mframeAreaSymbols) :
	frameAreaSymbols(mframeAreaSymbols), frame(mframe)
{}

Image::Image()
{
	iscascadeLoad = cascade.load("haarcascade_russian_plate_number.xml");
	OCR.Init(NULL, "amh");
	OCR.SetPageSegMode(tesseract::PSM_SINGLE_CHAR);
}

Image::~Image()
{
	for(size_t i = 0; i < frames.size(); ++i)
		frames.at(i).release();

	mimage.release();
}

bool Image::recognize(const cv::Mat& img)
{
	setImage(img);

	return this->recognize();
}

bool Image::recognize()
{
	frames.clear();
	licenseSymbols.clear();
	textLicense.clear();

	std::vector<cv::Rect> faces;

	cv::Mat gray, smallImg(cvRound (mimage.rows/scale), cvRound(mimage.cols/scale), CV_8UC1);

	cv::cvtColor(mimage, gray, CV_BGR2GRAY);
	cv::resize(gray, smallImg, smallImg.size(), 0, 0, cv::INTER_LINEAR);
	cv::equalizeHist(smallImg, smallImg);

	if(!iscascadeLoad)
		return false;

    cascade.detectMultiScale(smallImg, faces,
		1.1, 10, 5,
		cv::Size(70, 21));

	for(auto& r : faces)
	{
		cv::Point first = cv::Point(r.x*scale, r.y*scale);
		cv::Point two	= cv::Point((r.x + r.width)*scale, (r.y + r.height)*scale);

		frames.push_back(mimage(cv::Rect(first.x, first.y, two.x - first.x, two.y - first.y)));
	}

	for(auto& f : frames)
		recognizeSymbols(f);

	if(!licenseSymbols.empty())
		recognizeLicenseNumber();

	return true;
}

std::vector<std::string> Image::getLicenseText() const
{
	return this->textLicense;
}

std::vector<cv::Mat> Image::getFrames() const
{
	return this->frames;
}

void Image::setImage(const cv::Mat& img)
{
	this->mimage = img.clone();
}

void Image::saveFrames()
{
	for(size_t i = 0; i < frames.size(); ++i)
	{
		char name[8];
		sprintf(name, "f%Iu.jpg", i);
		cv::imwrite(name, frames.at(i));
	}
}
void Image::saveSymbols()
{
	size_t i = 0;

	for(auto& l : licenseSymbols)
	{
		cv::Mat src = l.frame;

		for(size_t b = 0; b < l.frameAreaSymbols.size(); ++b, ++i)
		{
			char name[8];
			sprintf(name, "%Iu.jpg", i);

			cv::Mat image = src(cv::Rect(l.frameAreaSymbols.at(b).minX,
							l.frameAreaSymbols.at(b).minY,
							l.frameAreaSymbols.at(b).width,
							l.frameAreaSymbols.at(b).height));

			cv::imwrite(name, image);
		}

	}
}

void Image::showimage(std::string namewindow, cv::Mat image)
{
	imshow(namewindow, image);
}
void Image::showNormalImage(std::string namewindow)
{
	showimage(namewindow, mimage);
}

void Image::showSymbol()
{
	size_t nwnd = 0;

	for(auto& l : licenseSymbols)
	{
		cv::Mat img = l.frame;

		for(size_t i = 0; i < l.frameAreaSymbols.size(); ++i)
			rectangle(img,	cv::Point(l.frameAreaSymbols.at(i).minX, l.frameAreaSymbols.at(i).minY),
							cv::Point(l.frameAreaSymbols.at(i).maxX, l.frameAreaSymbols.at(i).maxY),
							cv::Scalar(0,255,0), 2);

		char wndname[50];
		sprintf(wndname, "Symbols %Iu", nwnd);

		cv::namedWindow(wndname, cv::WINDOW_AUTOSIZE );
		cv::imshow(wndname, img);

		++nwnd;
	}
}

bool Image::recognizeSymbols(cv::Mat& src)
{
	std::vector<std::vector<cv::Point> > contours;
	std::vector<mArea> contursOut;
	std::vector<cv::Vec4i> hierarchy;
	cv::Mat cannyOutput, srcGray;

	// Convert image to gray and blur it
	cvtColor(src, srcGray, cv::COLOR_BGR2GRAY );
	cv::blur(srcGray, srcGray, cv::Size(3,3) );

	// Detect edges using canny
	cv::Canny( srcGray, cannyOutput, thresh, thresh*2, 3 );

	// Find contours
	cv::findContours(cannyOutput, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0) );

	for(auto& c : contours)
	{
		mArea area(c);

		if(((int)(area.height * area.width)) == 0)
			continue;

		int ratio = (src.cols * src.rows)/(area.height * area.width);

		if(area.height < area.width || area.width < 2 || ratio < 30 || ratio > 85 || isDuplicat(area, contursOut))
			continue;

		contursOut.push_back(area);
	}

	bool isRealSymbolLicens = (contursOut.size() == 8 || contursOut.size() == 9);

	if(isRealSymbolLicens)
	{
		std::sort(contursOut.begin(), contursOut.end());	// Set the symbols in the correct order
		licenseSymbols.push_back(LicenseSymbolsArea(src, contursOut));
	}

	return isRealSymbolLicens;
}

bool Image::recognizeLicenseNumber()
{
	for(auto& l : licenseSymbols)
	{
		std::string text;

		for(size_t i = 0; i < l.frameAreaSymbols.size(); ++i)
		{
			int minX	= l.frameAreaSymbols.at(i).minX;
			int minY	= l.frameAreaSymbols.at(i).minY;
			int height	= l.frameAreaSymbols.at(i).height;
			int width	= l.frameAreaSymbols.at(i).width;

			if(minX-1 <= 0 || minY-1 <= 0 || (minX-1 + width+3) >= l.frame.size().width || (minY-1 + height+3) >= l.frame.size().height)
				continue;

 			cv::Mat subImg = (l.frame)(cv::Rect(minX-1, minY-1, width+3, height+3));

			cv::Mat gray = cvCreateImage(subImg.size(), 8, 1);
			cv::Mat image = cvCreateImage(subImg.size(), 8, 1);
			cv::cvtColor(subImg, gray, CV_RGB2GRAY);

			if(i == 0 || i == 4 || i == 5)
				cv::resize(image, image, cv::Size(10, 18));
			else
				cv::resize(image, image, cv::Size(15, 24));

			cv::GaussianBlur(image, image, cv::Size(7, 7), 0);
			cv::threshold(gray, image, 0, 255, cv::THRESH_BINARY_INV | cv::THRESH_OTSU);

			OCR.SetVariable("tessedit_char_whitelist", ((i == 0 || i == 4 || i == 5) ? symbolChar.c_str() : symbolDigit.c_str()));

			OCR.TesseractRect(image.data, 1, image.step1(), 0, 0, image.cols, image.rows);

			char* symbol = OCR.GetUTF8Text();

			if(symbol[0] == ' ')
				symbol[0] = '?';

			text.push_back(symbol[0]);
		}

		textLicense.push_back(text);
	}

	return true;
}

bool Image::isDuplicat(mArea& a, std::vector<mArea>& vec)
{
	for(auto& v : vec)
		if(a == v)
			return true;

	return false;
}<commit_msg>Fix size resize and speed test<commit_after>#include "image.h"

LicenseSymbolsArea::LicenseSymbolsArea(cv::Mat& mframe, std::vector<mArea>& mframeAreaSymbols) :
	frameAreaSymbols(mframeAreaSymbols), frame(mframe)
{}

Image::Image()
{
	iscascadeLoad = cascade.load("haarcascade_russian_plate_number.xml");
	OCR.Init(NULL, "amh");
	OCR.SetPageSegMode(tesseract::PSM_SINGLE_CHAR);
}

Image::~Image()
{
	for(size_t i = 0; i < frames.size(); ++i)
		frames.at(i).release();

	mimage.release();
}

bool Image::recognize(const cv::Mat& img)
{
	setImage(img);

	return this->recognize();
}

bool Image::recognize()
{
	frames.clear();
	licenseSymbols.clear();
	textLicense.clear();

	std::vector<cv::Rect> faces;

	float imageAspect = (float)mimage.size().width / (float)mimage.size().height;
	int width, height;

    if (imageAspect > 1.0f)
	{
		width = 800;
		height = 800/imageAspect;
	}
	else
	{
		width = 800 * imageAspect;
		height = 800;
	}

	cv::Mat gray, smallImg(height, width, CV_8UC1);

	cv::cvtColor(mimage, gray, CV_BGR2GRAY);
	cv::resize(gray, smallImg, smallImg.size(), 0, 0, cv::INTER_LINEAR);

	if(!iscascadeLoad)
		return false;

    cascade.detectMultiScale(smallImg, faces,
		1.1, 10, 5,
		cv::Size(70, 21));

	cv::resize(mimage, mimage, smallImg.size(), 0, 0, cv::INTER_LINEAR);

	for(auto& r : faces)
	{
		cv::Point first = cv::Point(r.x, r.y);
		cv::Point two	= cv::Point((r.x + r.width), (r.y + r.height));

		frames.push_back(mimage(cv::Rect(first.x, first.y, two.x-first.x , two.y-first.y)));
	}

	for(auto& f : frames)
		recognizeSymbols(f);

	if(!licenseSymbols.empty())
		recognizeLicenseNumber();

	return true;
}

std::vector<std::string> Image::getLicenseText() const
{
	return this->textLicense;
}

std::vector<cv::Mat> Image::getFrames() const
{
	return this->frames;
}

void Image::setImage(const cv::Mat& img)
{
	this->mimage = img.clone();
}

void Image::saveFrames()
{
	for(size_t i = 0; i < frames.size(); ++i)
	{
		char name[8];
		sprintf(name, "f%Iu.jpg", i);
		cv::imwrite(name, frames.at(i));
	}
}
void Image::saveSymbols()
{
	size_t i = 0;

	for(auto& l : licenseSymbols)
	{
		cv::Mat src = l.frame;

		for(size_t b = 0; b < l.frameAreaSymbols.size(); ++b, ++i)
		{
			char name[8];
			sprintf(name, "%Iu.jpg", i);

			cv::Mat image = src(cv::Rect(l.frameAreaSymbols.at(b).minX,
							l.frameAreaSymbols.at(b).minY,
							l.frameAreaSymbols.at(b).width,
							l.frameAreaSymbols.at(b).height));

			cv::imwrite(name, image);
		}

	}
}

void Image::showimage(std::string namewindow, cv::Mat image)
{
	imshow(namewindow, image);
}
void Image::showNormalImage(std::string namewindow)
{
	showimage(namewindow, mimage);
}

void Image::showSymbol()
{
	size_t nwnd = 0;

	for(auto& l : licenseSymbols)
	{
		cv::Mat img = l.frame;

		for(size_t i = 0; i < l.frameAreaSymbols.size(); ++i)
			rectangle(img,	cv::Point(l.frameAreaSymbols.at(i).minX, l.frameAreaSymbols.at(i).minY),
							cv::Point(l.frameAreaSymbols.at(i).maxX, l.frameAreaSymbols.at(i).maxY),
							cv::Scalar(0,255,0), 2);

		char wndname[50];
		sprintf(wndname, "Symbols %Iu", nwnd);

		cv::namedWindow(wndname, cv::WINDOW_AUTOSIZE );
		cv::imshow(wndname, img);

		++nwnd;
	}
}

bool Image::recognizeSymbols(cv::Mat& src)
{
	std::vector<std::vector<cv::Point> > contours;
	std::vector<mArea> contursOut;
	std::vector<cv::Vec4i> hierarchy;
	cv::Mat cannyOutput, srcGray;

	// Convert image to gray and blur it
	cvtColor(src, srcGray, cv::COLOR_BGR2GRAY );
	cv::blur(srcGray, srcGray, cv::Size(3,3) );

	// Detect edges using canny
	cv::Canny( srcGray, cannyOutput, thresh, thresh*2, 3 );

	// Find contours
	cv::findContours(cannyOutput, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0) );

	for(auto& c : contours)
	{
		mArea area(c);

		if(((int)(area.height * area.width)) == 0)
			continue;

		int ratio = (src.cols * src.rows)/(area.height * area.width);

		if(area.height < area.width || area.width < 2 || ratio < 30 || ratio > 85 || isDuplicat(area, contursOut))
			continue;

		contursOut.push_back(area);
	}

	bool isRealSymbolLicens = (contursOut.size() == 8 || contursOut.size() == 9);

	if(isRealSymbolLicens)
	{
		std::sort(contursOut.begin(), contursOut.end());	// Set the symbols in the correct order
		licenseSymbols.push_back(LicenseSymbolsArea(src, contursOut));
	}

	return isRealSymbolLicens;
}

bool Image::recognizeLicenseNumber()
{
	for(auto& l : licenseSymbols)
	{
		std::string text;

		for(size_t i = 0; i < l.frameAreaSymbols.size(); ++i)
		{
			int minX	= l.frameAreaSymbols.at(i).minX;
			int minY	= l.frameAreaSymbols.at(i).minY;
			int height	= l.frameAreaSymbols.at(i).height;
			int width	= l.frameAreaSymbols.at(i).width;

			if(minX-1 <= 0 || minY-1 <= 0 || (minX-1 + width+3) >= l.frame.size().width || (minY-1 + height+3) >= l.frame.size().height)
				continue;

 			cv::Mat subImg = (l.frame)(cv::Rect(minX-1, minY-1, width+3, height+3));

			cv::Mat gray = cvCreateImage(subImg.size(), 8, 1);
			cv::Mat image = cvCreateImage(subImg.size(), 8, 1);
			cv::cvtColor(subImg, gray, CV_RGB2GRAY);

			if(i == 0 || i == 4 || i == 5)
				cv::resize(image, image, cv::Size(10, 18));
			else
				cv::resize(image, image, cv::Size(15, 24));

			cv::GaussianBlur(image, image, cv::Size(7, 7), 0);
			cv::threshold(gray, image, 0, 255, cv::THRESH_BINARY_INV | cv::THRESH_OTSU);

			OCR.SetVariable("tessedit_char_whitelist", ((i == 0 || i == 4 || i == 5) ? symbolChar.c_str() : symbolDigit.c_str()));

			OCR.TesseractRect(image.data, 1, image.step1(), 0, 0, image.cols, image.rows);

			char* symbol = OCR.GetUTF8Text();

			if(symbol[0] == ' ')
				symbol[0] = '?';

			text.push_back(symbol[0]);
		}

		textLicense.push_back(text);
	}

	return true;
}

bool Image::isDuplicat(mArea& a, std::vector<mArea>& vec)
{
	for(auto& v : vec)
		if(a == v)
			return true;

	return false;
}<|endoftext|>


<commit_before>#include <cmath>
#include <iostream>
#include <ale_interface.hpp>
#include <glog/logging.h>
#include <boost/program_options.hpp>
//#include "prettyprint.hpp"
#include "../include/aleAgent.hpp"
#include "../include/tools.hpp"

/*
  DeepQLearner(
    const ActionVect& legal_actions,
    double epsilon_start,
    const double epsilon_min,
    const double epsilon_decay,
    const int epsilon_explore_idx,
    const int replay_batch_size,
    const int replay_memory_capacity,
    const int replay_start_size,
    const std::string sampleStrategy,
    const int update_frequency,
    const double discount_factor,
    const std::string solver_param
    ):
*/

using namespace deepRL;
namespace po = boost::program_options;
double EpisodeLearning( ALEInterface& ale, deepRL::DeepQLearner& dqlearner, const bool update);
po::variables_map argmap;

int main(int argc, char** argv) {

    po::options_description desc("Valid options");
    desc.add_options()
        ("help,h", "Help message")
        ("gpu,u", po::value<bool>()->default_value(false), "Use GPU to brew Caffe")
	("gui,i", po::value<bool>()->default_value(false), "Open a GUI window")
        ("rom,o", po::value<std::string>()->default_value("./roms/breakout.bin"), "Rom file to play")
	("epsilon_start,s", po::value<double>()->default_value(1.0), "Epsilon start value for action selection")
	("epsilon_min,m", po::value<double>()->default_value(0.1), "The final epsilon")
	("epsilon_decay,d", po::value<double>()->default_value(0.98), "Epsilon decay ratio")
	("epsilon_explore_idx,x", po::value<int>()->default_value(100000), "Number of iterations needed for epsilon to reach epsilon_min")
	("replay_memory_capacity,c", po::value<int>()->default_value(50000), "Capacity of replay memory")
	("replay_start_size,r", po::value<int>()->default_value(500), "UEnough amount of transitions to start learning")
	("sampleStrategy,y", po::value<std::string>()->default_value("uniform")->required(), "Sampling strategy for transition")
	("update_frequency,f", po::value<int>()->default_value(1000), "Every #update_frequency set target_net_ = net_")
	("discount_factor,n", po::value<double>()->default_value(0.98), "Discount factor of future rewards (0,1]")
	("solver,v", po::value<std::string>()->default_value("./prototxt/aleSolver.prototxt")->required(), "Solver parameter file (*.prototxt)")
	("skip_frame,p", po::value<int>()->default_value(3), "Number of frames skipped")
	("show_frame,e", po::value<bool>()->default_value(false), "Show the current frame in CUI")
	("model,l", po::value<std::string>()->default_value("")->required(), "Model file to load")
	("evaluate,u", po::value<bool>()->default_value(false), "Evaluation mode: only playing a game, no updates")
	("eval_epsilon,k", po::value<double>()->default_value(0.05), "Epsilon used in evaluate mode");

    po::store(parse_command_line(argc, argv, desc), argmap);
    po::notify(argmap);

    if (argmap.count("help")) {
        std::cout << desc << std::endl;
        return 0;
    }

  google::InitGoogleLogging(argv[0]);
  google::InstallFailureSignalHandler();
  google::LogToStderr();

  if (argmap["gpu"].as<bool>()) {
    caffe::Caffe::set_mode(caffe::Caffe::GPU);
  } else {
    caffe::Caffe::set_mode(caffe::Caffe::CPU);
  }

  ALEInterface ale(argmap["gui"].as<bool>());
  std::cout << "Init ale environment ok!" << std::endl;

  // Load the ROM file
  ale.loadROM(argmap["rom"].as<std::string>());

  std::cout << "Loading rom: " << argmap["rom"].as<std::string>() << " ok!" << std::endl;

  // Get the vector of legal actions
  const auto legal_actions = ale.getMinimalActionSet();

  std::cout << "Action space: " << legal_actions.size() << std::endl;

  // deepqlearner
  deepRL::DeepQLearner dqlearner(legal_actions,
				argmap["epsilon_start"].as<double>(),
				argmap["epsilon_min"].as<double>(),
				argmap["epsilon_decay"].as<double>(),
				argmap["epsilon_explore_idx"].as<int>(),
				deepRL::kMinibatchSize,
				argmap["replay_memory_capacity"].as<int>(),
				argmap["replay_start_size"].as<int>(),
				argmap["sampleStrategy"].as<std::string>(),
				argmap["update_frequency"].as<int>(),
				argmap["discount_factor"].as<double>(),
				argmap["solver"].as<std::string>(),
				argmap["evaluate"].as<bool>(),
                                argmap["eval_epsilon"].as<double>());
  dqlearner.Initialize();

  if (argmap["model"].as<std::string>() !="null") {
    // Just evaluate the given trained model
    dqlearner.LoadPretrainedModel(argmap["model"].as<std::string>());
    std::cout << "Loading " << argmap["model"].as<std::string>() << std::endl;
  }
  //evaluate mode
  if (argmap["evaluate"].as<bool>()) {
    auto total_score = 0.0;
      const auto score =
          EpisodeLearning(ale, dqlearner, false);
      std::cout << "score: " << score << std::endl;
    return 0;
  }

  // learning mode
  for (auto episode = 0;; episode++) {
    const auto score = EpisodeLearning(ale, dqlearner, true);
    std::cout << "Episode: " << episode << " ,Score: " << score << std::endl;
    if (episode % 10 == 0) {
      // After every 10 episodes, evaluate the current strength
      const auto eval_score = EpisodeLearning(ale, dqlearner,false);
      std::cout << "evaluation score: " << eval_score << std::endl;
    }
  }
}

/**
 * one episode learning and return the total score
 */
double EpisodeLearning( ALEInterface& ale, deepRL::DeepQLearner& dqlearner, const bool update) {
  assert(!ale.game_over());
  std::deque<deepRL::FrameDataSp> past_frames;
  //dqlearner.replay_memory_.resetPool();

  auto total_score = 0.0;
  for (auto frame = 0; !ale.game_over(); ++frame) {
    //std::cout << "frame: " << frame << std::endl;
    const auto current_frame = deepRL::PreprocessScreen(ale.getScreen());
    if (argmap["show_frame"].as<bool>()) {
      std::cout << deepRL::DrawFrame(*current_frame) << std::endl;
    }
    past_frames.push_back(current_frame);
    if (past_frames.size() < deepRL::kInputFrameCount) {
      // If there are not past frames enough for DQN input, just select NOOP
      for (auto i = 0; i < argmap["skip_frame"].as<int>() + 1 && !ale.game_over(); ++i) {
        total_score += ale.act(PLAYER_A_NOOP);
      }
    } else {
      if (past_frames.size() > deepRL::kInputFrameCount) {
        past_frames.pop_front();
      }
      deepRL::InputFrames input_frames;
      std::copy(past_frames.begin(), past_frames.end(), input_frames.begin());
      const auto action = dqlearner.SelectAction(input_frames);
      auto immediate_score = 0.0;

      for (auto i = 0; i < argmap["skip_frame"].as<int>() + 1 && !ale.game_over(); ++i) {
        // Last action is repeated on skipped frames
        immediate_score += ale.act(action);
      }

      total_score += immediate_score;

      //clip reward for robust gradient update
      // Rewards for DQN are normalized as follows:
      // 1 for any positive score, -1 for any negative score, otherwise 0
      const auto reward =
          immediate_score == 0 ?
              0 :
              immediate_score /= std::abs(immediate_score);

      if (update) {
        // Add the current transition to replay memory
        const auto transition = ale.game_over() ?
            deepRL::Transition(input_frames, action, reward, boost::none) :
            deepRL::Transition(
                input_frames,
                action,
                reward,
                deepRL::PreprocessScreen(ale.getScreen()));
        dqlearner.replay_memory_.addTransition(transition);
	//std::cout << "Memorypool Size: " << dqlearner.replay_memory_.memory_size() << std::endl;
        // If the size of replay memory is enough, update DQN
        if (dqlearner.replay_memory_.memory_size() >= argmap["replay_start_size"].as<int>()) {
          dqlearner.BatchUpdate();
        }
      }
    }
  }
  ale.reset_game();
  return total_score;
}

<commit_msg>fix q and target_net_ update error<commit_after>#include <cmath>
#include <iostream>
#include <ale_interface.hpp>
#include <glog/logging.h>
#include <boost/program_options.hpp>
//#include "prettyprint.hpp"
#include "../include/aleAgent.hpp"
#include "../include/tools.hpp"

/*
  DeepQLearner(
    const ActionVect& legal_actions,
    double epsilon_start,
    const double epsilon_min,
    const double epsilon_decay,
    const int epsilon_explore_idx,
    const int replay_batch_size,
    const int replay_memory_capacity,
    const int replay_start_size,
    const std::string sampleStrategy,
    const int update_frequency,
    const double discount_factor,
    const std::string solver_param
    ):
*/

using namespace deepRL;
namespace po = boost::program_options;
double EpisodeLearning( ALEInterface& ale, deepRL::DeepQLearner& dqlearner, const bool update);
po::variables_map argmap;

int main(int argc, char** argv) {

    po::options_description desc("Valid options");
    desc.add_options()
        ("help,h", "Help message")
        ("gpu,u", po::value<bool>()->default_value(false), "Use GPU to brew Caffe")
	("gui,i", po::value<bool>()->default_value(false), "Open a GUI window")
        ("rom,o", po::value<std::string>()->default_value("./roms/breakout.bin"), "Rom file to play")
	("epsilon_start,s", po::value<double>()->default_value(1.0), "Epsilon start value for action selection")
	("epsilon_min,m", po::value<double>()->default_value(0.1), "The final epsilon")
	("epsilon_decay,d", po::value<double>()->default_value(0.98), "Epsilon decay ratio")
	("epsilon_explore_idx,x", po::value<int>()->default_value(100000), "Number of iterations needed for epsilon to reach epsilon_min")
	("replay_memory_capacity,c", po::value<int>()->default_value(50000), "Capacity of replay memory")
	("replay_start_size,r", po::value<int>()->default_value(500), "UEnough amount of transitions to start learning")
	("sampleStrategy,y", po::value<std::string>()->default_value("uniform")->required(), "Sampling strategy for transition")
	("update_frequency,f", po::value<int>()->default_value(1000), "Every #update_frequency set target_net_ = net_")
	("discount_factor,n", po::value<double>()->default_value(0.98), "Discount factor of future rewards (0,1]")
	("solver,v", po::value<std::string>()->default_value("./prototxt/aleSolver.prototxt")->required(), "Solver parameter file (*.prototxt)")
	("skip_frame,p", po::value<int>()->default_value(3), "Number of frames skipped")
	("show_frame,e", po::value<bool>()->default_value(false), "Show the current frame in CUI")
	("model,l", po::value<std::string>()->default_value("")->required(), "Model file to load")
	("evaluate,u", po::value<bool>()->default_value(false), "Evaluation mode: only playing a game, no updates")
	("eval_epsilon,k", po::value<double>()->default_value(0.05), "Epsilon used in evaluate mode")
	("target_q_freq,g", po::value<int>()->default_value(1000), "Taregt_q_net_ update frequency");

    po::store(parse_command_line(argc, argv, desc), argmap);
    po::notify(argmap);

    if (argmap.count("help")) {
        std::cout << desc << std::endl;
        return 0;
    }

  google::InitGoogleLogging(argv[0]);
  google::InstallFailureSignalHandler();
  google::LogToStderr();

  if (argmap["gpu"].as<bool>()) {
    caffe::Caffe::set_mode(caffe::Caffe::GPU);
  } else {
    caffe::Caffe::set_mode(caffe::Caffe::CPU);
  }

  ALEInterface ale(argmap["gui"].as<bool>());
  std::cout << "Init ale environment ok!" << std::endl;

  // Load the ROM file
  ale.loadROM(argmap["rom"].as<std::string>());

  std::cout << "Loading rom: " << argmap["rom"].as<std::string>() << " ok!" << std::endl;

  // Get the vector of legal actions
  const auto legal_actions = ale.getMinimalActionSet();

  std::cout << "Action space: " << legal_actions.size() << std::endl;

  // deepqlearner
  deepRL::DeepQLearner dqlearner(legal_actions,
				argmap["epsilon_start"].as<double>(),
				argmap["epsilon_min"].as<double>(),
				argmap["epsilon_decay"].as<double>(),
				argmap["epsilon_explore_idx"].as<int>(),
				deepRL::kMinibatchSize,
				argmap["replay_memory_capacity"].as<int>(),
				argmap["replay_start_size"].as<int>(),
				argmap["sampleStrategy"].as<std::string>(),
				argmap["update_frequency"].as<int>(),
				argmap["discount_factor"].as<double>(),
				argmap["solver"].as<std::string>(),
				argmap["evaluate"].as<bool>(),
                                argmap["eval_epsilon"].as<double>(),
                                argmap["target_q_freq"].as<int>());
  dqlearner.Initialize();

  if (argmap["model"].as<std::string>() !="null") {
    // Just evaluate the given trained model
    dqlearner.LoadPretrainedModel(argmap["model"].as<std::string>());
    std::cout << "Loading " << argmap["model"].as<std::string>() << std::endl;
  }
  //evaluate mode
  if (argmap["evaluate"].as<bool>()) {
    auto total_score = 0.0;
      const auto score =
          EpisodeLearning(ale, dqlearner, false);
      std::cout << "score: " << score << std::endl;
    return 0;
  }

  // learning mode
  for (auto episode = 0;; episode++) {
    const auto score = EpisodeLearning(ale, dqlearner, true);
    std::cout << "Episode: " << episode << " ,Score: " << score << std::endl;
    if (episode % 10 == 0) {
      // After every 10 episodes, evaluate the current strength
      const auto eval_score = EpisodeLearning(ale, dqlearner,false);
      std::cout << "evaluation score: " << eval_score << std::endl;
    }
  }
}

/**
 * one episode learning and return the total score
 */
double EpisodeLearning( ALEInterface& ale, deepRL::DeepQLearner& dqlearner, const bool update) {
  assert(!ale.game_over());
  std::deque<deepRL::FrameDataSp> past_frames;
  //dqlearner.replay_memory_.resetPool();

  auto total_score = 0.0;
  for (auto frame = 0; !ale.game_over(); ++frame) {
    //std::cout << "frame: " << frame << std::endl;
    const auto current_frame = deepRL::PreprocessScreen(ale.getScreen());
    if (argmap["show_frame"].as<bool>()) {
      std::cout << deepRL::DrawFrame(*current_frame) << std::endl;
    }
    past_frames.push_back(current_frame);
    if (past_frames.size() < deepRL::kInputFrameCount) {
      // If there are not past frames enough for DQN input, just select NOOP
      for (auto i = 0; i < argmap["skip_frame"].as<int>() + 1 && !ale.game_over(); ++i) {
        total_score += ale.act(PLAYER_A_NOOP);
      }
    } else {
      if (past_frames.size() > deepRL::kInputFrameCount) {
        past_frames.pop_front();
      }
      deepRL::InputFrames input_frames;
      std::copy(past_frames.begin(), past_frames.end(), input_frames.begin());
      const auto action = dqlearner.SelectAction(input_frames);
      auto immediate_score = 0.0;

      for (auto i = 0; i < argmap["skip_frame"].as<int>() + 1 && !ale.game_over(); ++i) {
        // Last action is repeated on skipped frames
        immediate_score += ale.act(action);
      }

      total_score += immediate_score;

      //clip reward for robust gradient update
      // Rewards for DQN are normalized as follows:
      // 1 for any positive score, -1 for any negative score, otherwise 0
      const auto reward =
          immediate_score == 0 ?
              0 :
              immediate_score /= std::abs(immediate_score);

      if (update) {
        // Add the current transition to replay memory
        const auto transition = ale.game_over() ?
            deepRL::Transition(input_frames, action, reward, boost::none) :
            deepRL::Transition(
                input_frames,
                action,
                reward,
                deepRL::PreprocessScreen(ale.getScreen()));
        dqlearner.replay_memory_.addTransition(transition);
	//std::cout << "Memorypool Size: " << dqlearner.replay_memory_.memory_size() << std::endl;
        // If the size of replay memory is enough, update DQN
        if (dqlearner.replay_memory_.memory_size() >= argmap["replay_start_size"].as<int>()
	    and dqlearner.numSteps()%argmap["update_frequency"].as<int>()==0 ) {
          dqlearner.BatchUpdate();
        }
      }
    }
  }
  ale.reset_game();
  return total_score;
}

<|endoftext|>


<commit_before>
#include "HS.hpp"
#include <onions-common/containers/records/CreateR.hpp>
#include <onions-common/tcp/TorStream.hpp>
#include <onions-common/Config.hpp>
#include <onions-common/Log.hpp>
#include <onions-common/Utils.hpp>
#include <iostream>

std::string HS::keyPath_;

RecordPtr HS::createRecord(uint8_t workers)
{
  auto r = promptForRecord();

  std::cout << "\n" << *r << "\n" << std::endl;
  std::cout << "Record prepared. Hit Enter to begin making it valid. ";
  std::string tmp;
  std::getline(std::cin, tmp);

  r->makeValid(workers);

  std::cout << std::endl;
  std::cout << *r << std::endl;

  std::cout << std::endl;
  auto json = r->asJSON();
  std::cout << "Final Record is " << json.length()
            << " bytes, ready for transmission: \n\n" << json << std::endl;

  return r;
}

RecordPtr HS::promptForRecord()
{
  std::cout
      << "Here you can claim a .tor domain and multiple subdomains for your"
         " hidden service. They can point to either a .tor or a .onion domain,"
         " keeping it all within Tor. For example, you may claim"
         " \"example.tor\" -> \"onions55e7yam27n.onion\", \"foo.example.tor\""
         " -> \"foo.tor\", \"bar.example.tor\" -> \"bar.tor\", and"
         " \"a.b.c.example.tor\" -> \"3g2upl4pq6kufc4m.onion\"."
         " The association between these names and your hidden service is"
         " cryptographically locked and made valid after some computational"
         " work. This work will follow the prompts for the domains. \n";

  std::string name;
  std::cout << "\nThe primary domain name must end in \".tor\"" << std::endl;
  while (name.length() < 5 || !Utils::strEndsWith(name, ".tor"))
  {
    std::cout << "The primary domain name: ";
    std::getline(std::cin, name);
    name = Utils::trimString(name);
  }

  std::string pgp;
  std::cout << "\nYou may optionally supply your PGP fingerprint, \n"
               "which must be a power of two in length." << std::endl;
  while (!Utils::isPowerOfTwo(pgp.length()))
  {
    std::cout << "Your PGP fingerprint: ";
    std::getline(std::cin, pgp);  //"AD97364FC20BEC80"

    pgp = Utils::trimString(pgp);
    if (pgp.empty())
      break;
  }

  std::cout << "\nYou may provide up to 24 subdomain-destination pairs.\n"
               "These must end with \"." << name
            << "\". Leave the "
               "subdomain blank when finished." << std::endl;

  NameList list;
  for (int n = 0; n < 24; n++)
  {
    std::string src = name, dest;

    while (!Utils::strEndsWith(src, "." + name))
    {
      std::cout << "Subdomain " << (n + 1) << ": ";
      std::getline(std::cin, src);
      src = Utils::trimString(src);

      if (src.length() == 0)
        break;
    }

    if (src.length() == 0)
      break;

    while ((!Utils::strEndsWith(dest, ".tor") &&
            !Utils::strEndsWith(dest, ".onion")) ||
           (Utils::strEndsWith(dest, ".onion") && dest.length() != 16 + 6))
    {
      std::cout << "   Destination: ";
      std::getline(std::cin, dest);
      dest = Utils::trimString(dest);
    }

    src.erase(src.find("." + name));
    list.push_back(std::make_pair(src, dest));
    std::cout << std::endl;
  }

  std::cout << std::endl;
  auto r = std::make_shared<CreateR>(Utils::loadKey(keyPath_), name, pgp);
  r->setSubdomains(list);

  return r;
}

bool HS::sendRecord(const RecordPtr& r, short socksPort)
{
  auto nodeConf = Config::getQuorumNode()[0];
  const auto SERVER_PORT = Const::SERVER_PORT;
  TorStream quorumNode("127.0.0.1", socksPort, nodeConf["addr"].asString(),
                       SERVER_PORT);

  std::cout << "Uploading Record..." << std::endl;
  auto received = quorumNode.sendReceive("upload", r->asJSON());
  if (received["type"].asString() == "error")
  {
    Log::get().error(received["value"].asString());
    return false;
  }

  std::cout << "Record upload complete; your claim has been accepted.\n";

  return true;
}

void HS::setKeyPath(const std::string& keyPath)
{
  keyPath_ = keyPath;
}
<commit_msg>Update upload command<commit_after>
#include "HS.hpp"
#include <onions-common/containers/records/CreateR.hpp>
#include <onions-common/tcp/AuthenticatedStream.hpp>
#include <onions-common/Config.hpp>
#include <onions-common/Log.hpp>
#include <onions-common/Utils.hpp>
#include <iostream>

std::string HS::keyPath_;

RecordPtr HS::createRecord(uint8_t workers)
{
  auto r = promptForRecord();

  std::cout << "\n" << *r << "\n" << std::endl;
  std::cout << "Record prepared. Hit Enter to begin making it valid. ";
  std::string tmp;
  std::getline(std::cin, tmp);

  r->makeValid(workers);

  std::cout << std::endl;
  std::cout << *r << std::endl;

  std::cout << std::endl;
  auto json = r->asJSON();
  std::cout << "Final Record is " << json.length()
            << " bytes, ready for transmission: \n\n" << json << std::endl;

  return r;
}

RecordPtr HS::promptForRecord()
{
  std::cout
      << "Here you can claim a .tor domain and multiple subdomains for your"
         " hidden service. They can point to either a .tor or a .onion domain,"
         " keeping it all within Tor. For example, you may claim"
         " \"example.tor\" -> \"onions55e7yam27n.onion\", \"foo.example.tor\""
         " -> \"foo.tor\", \"bar.example.tor\" -> \"bar.tor\", and"
         " \"a.b.c.example.tor\" -> \"3g2upl4pq6kufc4m.onion\"."
         " The association between these names and your hidden service is"
         " cryptographically locked and made valid after some computational"
         " work. This work will follow the prompts for the domains. \n";

  std::string name;
  std::cout << "\nThe primary domain name must end in \".tor\"" << std::endl;
  while (name.length() < 5 || !Utils::strEndsWith(name, ".tor"))
  {
    std::cout << "The primary domain name: ";
    std::getline(std::cin, name);
    name = Utils::trimString(name);
  }

  std::string pgp;
  std::cout << "\nYou may optionally supply your PGP fingerprint, \n"
               "which must be a power of two in length." << std::endl;
  while (!Utils::isPowerOfTwo(pgp.length()))
  {
    std::cout << "Your PGP fingerprint: ";
    std::getline(std::cin, pgp);  //"AD97364FC20BEC80"

    pgp = Utils::trimString(pgp);
    if (pgp.empty())
      break;
  }

  std::cout << "\nYou may provide up to 24 subdomain-destination pairs.\n"
               "These must end with \"." << name
            << "\". Leave the "
               "subdomain blank when finished." << std::endl;

  NameList list;
  for (int n = 0; n < 24; n++)
  {
    std::string src = name, dest;

    while (!Utils::strEndsWith(src, "." + name))
    {
      std::cout << "Subdomain " << (n + 1) << ": ";
      std::getline(std::cin, src);
      src = Utils::trimString(src);

      if (src.length() == 0)
        break;
    }

    if (src.length() == 0)
      break;

    while ((!Utils::strEndsWith(dest, ".tor") &&
            !Utils::strEndsWith(dest, ".onion")) ||
           (Utils::strEndsWith(dest, ".onion") && dest.length() != 16 + 6))
    {
      std::cout << "   Destination: ";
      std::getline(std::cin, dest);
      dest = Utils::trimString(dest);
    }

    src.erase(src.find("." + name));
    list.push_back(std::make_pair(src, dest));
    std::cout << std::endl;
  }

  std::cout << std::endl;
  auto r = std::make_shared<CreateR>(Utils::loadKey(keyPath_), name, pgp);
  r->setSubdomains(list);

  return r;
}

bool HS::sendRecord(const RecordPtr& r, short socksPort)
{
  const auto Q_NODE = Config::getQuorumNode()[0];
  const auto Q_ONION = Q_NODE["addr"].asString();
  const auto Q_KEY = Q_NODE["key"].asString();
  const auto SERVER_PORT = Const::SERVER_PORT;

  ED_KEY qPubKey;
  std::copy(Q_KEY.begin(), Q_KEY.end(), qPubKey.data());

  AuthenticatedStream qStream("127.0.0.1", socksPort, Q_ONION, SERVER_PORT,
                              qPubKey);

  std::cout << "Uploading Record..." << std::endl;
  auto received = qStream.sendReceive("putRecord", r->asJSON());
  if (received["type"].asString() == "error")
  {
    Log::get().error(received["value"].asString());
    return false;
  }

  std::cout << "Record upload complete; your claim has been accepted.\n";

  return true;
}

void HS::setKeyPath(const std::string& keyPath)
{
  keyPath_ = keyPath;
}
<|endoftext|>


<commit_before>#ifndef Bull_RenderWindow_hpp
#define Bull_RenderWindow_hpp

#include <Bull/Core/Time/Clock.hpp>
#include <Bull/Core/Time/Time.hpp>
#include <Bull/Core/System/Config.hpp>

#include <Bull/Render/RenderTarget.hpp>

#include <Bull/Utility/Window/VideoMode.hpp>
#include <Bull/Utility/Window/Window.hpp>

namespace Bull
{
    class BULL_API RenderWindow : public Window, public RenderTarget
    {
    public:

        /*! \brief Default constructor
         *
         */
        RenderWindow();

        /*! \brief Constructor
         *
         * \param mode     The VideoMode
         * \param title    The title of the window
         * \param style    The window decorations
         * \param settings Settings to use to create the OpenGL context
         *
         */
        RenderWindow(const VideoMode& mode, const String& title, Uint32 style = Style::Default, const ContextSettings& settings = ContextSettings::Best);

        /*! \brief Open the window. If a window was already opened, its closed
         *
         * \param mode     The VideoMode
         * \param title    The title of the window
         * \param style    The window decorations
         * \param settings Settings to use to create the OpenGL context
         *
         * \return Return true if the window was open successfully, false otherwise
         *
         */
        bool open(const VideoMode& mode, const String& title, Uint32 style = Style::Default, const ContextSettings& settings = ContextSettings::Best);

        /*! \brief Display what has been rendered so far
         *
         */
        void display() override;

        /*! \brief Set the maximum framerate of the RenderWindow
         *
         * \param limit The maximum
         *
         */
        void setFramerateLimit(unsigned int limit);

        /*! \brief Get the maximum framerate of the RenderWindow
         *
         * \param limit The maximum
         *
         */
        unsigned int getFramerateLimit() const;

        /*! \brief Activate or deactivate the vertical synchronization
         *
         * \param active True to activate, false to deactivate
         *
         * \return Return true if success, false otherwise
         *
         */
        void enableVsync(bool active);

        /*! \brief Get the default viewport of the RenderTarget
         *
         * \return Return the viewport
         *
         */
        Viewport getDefaultViewport() const override;

    private:

        Time  m_frameDelay;
        Clock m_clock;
    };
}

#endif // Bull_RenderWindow_hpp
<commit_msg>[Render/RenderTarget] Remove useless include<commit_after>#ifndef Bull_RenderWindow_hpp
#define Bull_RenderWindow_hpp

#include <Bull/Core/Time/Clock.hpp>
#include <Bull/Core/Time/Time.hpp>

#include <Bull/Render/RenderTarget.hpp>

#include <Bull/Utility/Window/VideoMode.hpp>
#include <Bull/Utility/Window/Window.hpp>

namespace Bull
{
    class BULL_API RenderWindow : public Window, public RenderTarget
    {
    public:

        /*! \brief Default constructor
         *
         */
        RenderWindow();

        /*! \brief Constructor
         *
         * \param mode     The VideoMode
         * \param title    The title of the window
         * \param style    The window decorations
         * \param settings Settings to use to create the OpenGL context
         *
         */
        RenderWindow(const VideoMode& mode, const String& title, Uint32 style = Style::Default, const ContextSettings& settings = ContextSettings::Best);

        /*! \brief Open the window. If a window was already opened, its closed
         *
         * \param mode     The VideoMode
         * \param title    The title of the window
         * \param style    The window decorations
         * \param settings Settings to use to create the OpenGL context
         *
         * \return Return true if the window was open successfully, false otherwise
         *
         */
        bool open(const VideoMode& mode, const String& title, Uint32 style = Style::Default, const ContextSettings& settings = ContextSettings::Best);

        /*! \brief Display what has been rendered so far
         *
         */
        void display() override;

        /*! \brief Set the maximum framerate of the RenderWindow
         *
         * \param limit The maximum
         *
         */
        void setFramerateLimit(unsigned int limit);

        /*! \brief Get the maximum framerate of the RenderWindow
         *
         * \param limit The maximum
         *
         */
        unsigned int getFramerateLimit() const;

        /*! \brief Activate or deactivate the vertical synchronization
         *
         * \param active True to activate, false to deactivate
         *
         * \return Return true if success, false otherwise
         *
         */
        void enableVsync(bool active);

        /*! \brief Get the default viewport of the RenderTarget
         *
         * \return Return the viewport
         *
         */
        Viewport getDefaultViewport() const override;

    private:

        Time  m_frameDelay;
        Clock m_clock;
    };
}

#endif // Bull_RenderWindow_hpp
<|endoftext|>


<commit_before>/* vm.cc
   Jeremy Barnes, 22 February 2010
   Copyright (c) 2010 Jeremy Barnes.  All rights reserved.

   Virtual memory functions.
*/

#include "vm.h"
#include "jml/arch/format.h"
#include "jml/arch/exception.h"
#include "jml/utils/guard.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

#include <boost/bind.hpp>
#include <fstream>

using namespace std;

namespace ML {

std::string
Pagemap_Entry::
print() const
{
    if (present)
        return format("P%c %09x s%5d", (swapped ? 'S' : '.'),
                      pfn, (int)shift);
    else if (swapped)
        return format(".S %02d/%06x s%5d", (int)swap_type, (int)swap_offset);
    else return "..                 ";
}

std::string
Page_Info::
print_mapping() const
{
    return Pagemap_Entry::print();
}

std::string
Page_Info::
print_flags() const
{
    const char * letters = "lerudLaswRbmAShtHUPnk";
    string result;
    uint64_t val = 1;
    for (unsigned i = 0;  i < 22;  ++i) {
        bool f = flags & val;
        val <<= 1;
        if (f) result += letters[i];
        else result += '.';
    }

    result += format(" %06x", flags);

    return result;
}

std::string
Page_Info::
print() const
{
    string result = print_mapping();
    if (pfn != 0 && (count != 0 || flags != 0))
        result = result + format("  c:%6d ", count)
            + print_flags();
    return result;
}

std::vector<Page_Info> page_info(const void * addr, int npages)
{
    int pm_fd = open("/proc/self/pagemap", O_RDONLY);
    if (pm_fd == -1)
        throw Exception("open pagemap; " + string(strerror(errno)));
    Call_Guard close_pm_fd(boost::bind(::close, pm_fd));

    // Both of these might fail as these two files require special priviledges
    // to read
    int pf_fd = open("/proc/kpageflags", O_RDONLY);
    int pc_fd = open("/proc/kpagecount", O_RDONLY);

    // These will call guard with an fd of -1 if the files weren't open, which
    // won't hurt us
    Call_Guard close_pf_fd(boost::bind(::close, pf_fd));
    Call_Guard close_pc_fd(boost::bind(::close, pc_fd));

    size_t page_num = (size_t)addr / 4096;

    int res = lseek(pm_fd, page_num * 8, SEEK_SET);
    if (res == -1)
        throw Exception("page_info(): lseek: " + string(strerror(errno)));

    vector<Page_Info> result;
    for (unsigned i = 0;  i < npages;  ++i) {
        Page_Info info;

        int res = read(pm_fd, &info.mapping, 8);
        if (res != 8)
            throw Exception("read pm_fd");

        if (info.present) {
            if (pf_fd != -1) {
                int res = lseek(pf_fd, info.pfn * 8, SEEK_SET);
                if (res == -1)
                    throw Exception("lseek pf_fd");
                res = read(pf_fd, &info.flags, 8);
                if (res != 8)
                    throw Exception("read flags");
            }

            if (pc_fd != -1) {
                res = lseek(pc_fd, info.pfn * 8, SEEK_SET);
                if (res == -1)
                    throw Exception("lseek pc_fd");
                res = read(pc_fd, &info.count, 8);
                if (res != 8)
                    throw Exception("read count");
            }
        }

        result.push_back(info);
    }

    return result;
}

void dump_page_info(const void * start, const void * end,
                    std::ostream & stream)
{
    const char * p1 = (const char *)start;
    const char * p2 = (const char *)end;
    size_t dist = p2 - p1;
    size_t pages = dist / page_size;
    size_t rem   = dist % page_size;
    if (rem > 0) ++pages;

    if (pages > 10000)
        throw Exception("dump_page_info: too many pages");

    vector<Page_Info> info = page_info(start, pages);

    if (info.size() != pages)
        throw Exception("no pages");

    const char * p = page_start(p1);
    for (unsigned i = 0;  i < pages;  ++i, p += page_size) {
        stream << format("%04x %012p ", i, p)
               << info[i] << endl;
    }
}

std::vector<unsigned char> page_flags(const void * addr, int npages)
{
    int pm_fd = open("/proc/self/pagemap", O_RDONLY);
    if (pm_fd == -1)
        throw Exception("open pagemap; " + string(strerror(errno)));
    Call_Guard close_pm_fd(boost::bind(::close, pm_fd));

    size_t page_num = (size_t)addr / 4096;

    int res = lseek(pm_fd, page_num * 8, SEEK_SET);
    if (res == -1)
        throw Exception("page_info(): lseek: " + string(strerror(errno)));

    vector<unsigned char> result;
    result.reserve(npages);

    Page_Info info;

    uint64_t buf[1024];

    for (unsigned i = 0;  i < npages;  i += 1024) {
        int n = min<size_t>(1024, npages - i);

        int res = read(pm_fd, buf, 8 * n);

        if (res != 8 * n)
            throw Exception("read pm_fd");

        for (unsigned j = 0;  j < n;  ++j) {
            info.mapping = buf[j];
            result.push_back(info.present);
        }
    }

    return result;

}

void dump_maps(std::ostream & out)
{
    std::ifstream stream("/proc/self/maps");

    out << string(60, '=') << endl;
    out << "maps" << endl;

    while (stream) {
        string s;
        std::getline(stream, s);
        out << s << endl;
    }

    out << string(60, '=') << endl;
    out << endl << endl;
}

/*****************************************************************************/
/* PAGEMAP_READER                                                            */
/*****************************************************************************/

Pagemap_Reader::
Pagemap_Reader(const char * mem, size_t bytes,
               Pagemap_Entry * entries, int fd)
    : fd(fd), mem(mem), entries(entries),
      delete_entries(entries == 0), close_fd(fd == -1)
{
    npages = to_page_num(mem + bytes) - to_page_num(mem);

#if 0
    cerr << "mem = " << (const void *)mem << endl;
    cerr << "mem + bytes = " << (const void *)(mem + bytes) << endl;
    cerr << "page1 = " << to_page_num(mem + bytes) << endl;
    cerr << "page2 = " << to_page_num(mem) << endl;
    cerr << "bytes = " << bytes << endl;
    cerr << "npages = " << npages << endl;
#endif

    if (close_fd)
        this->fd = open("/proc/self/pagemap", O_RDONLY);
    if (this->fd == -1)
        throw Exception(errno, "Pagemap_Reader()",
                        "open(\"proc/self/pagemap\", O_RDONLY)");
    Call_Guard do_close_fd(boost::bind(close, this->fd));
    if (!close_fd)
        do_close_fd.clear();

    if (delete_entries)
        this->entries = new Pagemap_Entry[npages];

    try {
        update();
    } catch (...) {
        delete[] entries;
        throw;
    }

    do_close_fd.clear();
}

Pagemap_Reader::
~Pagemap_Reader()
{
    if (delete_entries)
        delete[] entries;

    if (close_fd && fd != -1) {
        int res = close(fd);
        if (res == -1)
            cerr << "~Pagemap_Reader(): close on fd: " << strerror(errno)
                 << endl;
    }

    entries = 0;
}

size_t
Pagemap_Reader::
update(ssize_t first_page, ssize_t last_page)
{
    if (last_page == -1)
        last_page = npages;

    if (first_page < 0 || last_page > npages || last_page < first_page)
        throw Exception("Pagemap_Reader::update(): pages out of range");

    // Where do we seek to in the pagemap file?

    // Counts the number of modified page map entries
    size_t result = 0;

    size_t CHUNK = 1024;  // pages at a time

    // Buffer to read them into
    Pagemap_Entry buf[CHUNK];

    size_t base_page_num = to_page_num(mem);

    //cerr << "update: first_page = " << first_page
    //     << " last_page = " << last_page << endl;

    // Update a chunk at a time
    for (size_t page = first_page;  page < last_page;  /* no inc */) {
        size_t limit = std::min<size_t>(page + CHUNK, last_page);

        //cerr << "page = " << page << " last_page = " << last_page
        //     << " limit = " << limit << endl;

        // Where to seek in the pagemap file?
        off_t seek_pos = (base_page_num + page) * sizeof(Pagemap_Entry);

        //cerr << "seek_pos = " << seek_pos << " base_page_num = "
        //     << base_page_num << endl;

        ssize_t res = pread(fd, buf,
                            (limit - page) * sizeof(Pagemap_Entry),
                            seek_pos);
        if (res == -1)
            throw Exception(errno, "Pagemap_Reader::update()", "pread");
        if (res <= 0)
            throw Exception("Pagemap_Reader::update(): nothing read "
                            "from pagemap file");

        //cerr << "res = " << res << endl;

        res /= sizeof(Pagemap_Entry);  // convert bytes to objects

        //cerr << "read " << res << " objects" << endl;

        for (unsigned i = 0;  i < res;  ++i) {
            result += this->entries[page + i] != buf[i];
            this->entries[page + i] = buf[i];
        }

        page += res;
    }

    return result;
}

void
Pagemap_Reader::
dump(std::ostream & stream) const
{
    const char * p = mem;
    for (unsigned i = 0;  i < npages;  ++i, p += page_size)
        stream << format("%04x %012p ", i, p)
               << entries[i] << endl;
}

} // namespace ML

<commit_msg>don't close -1 fds<commit_after>/* vm.cc
   Jeremy Barnes, 22 February 2010
   Copyright (c) 2010 Jeremy Barnes.  All rights reserved.

   Virtual memory functions.
*/

#include "vm.h"
#include "jml/arch/format.h"
#include "jml/arch/exception.h"
#include "jml/utils/guard.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

#include <boost/bind.hpp>
#include <fstream>

using namespace std;

namespace ML {

std::string
Pagemap_Entry::
print() const
{
    if (present)
        return format("P%c %09x s%5d", (swapped ? 'S' : '.'),
                      pfn, (int)shift);
    else if (swapped)
        return format(".S %02d/%06x s%5d", (int)swap_type, (int)swap_offset);
    else return "..                 ";
}

std::string
Page_Info::
print_mapping() const
{
    return Pagemap_Entry::print();
}

std::string
Page_Info::
print_flags() const
{
    const char * letters = "lerudLaswRbmAShtHUPnk";
    string result;
    uint64_t val = 1;
    for (unsigned i = 0;  i < 22;  ++i) {
        bool f = flags & val;
        val <<= 1;
        if (f) result += letters[i];
        else result += '.';
    }

    result += format(" %06x", flags);

    return result;
}

std::string
Page_Info::
print() const
{
    string result = print_mapping();
    if (pfn != 0 && (count != 0 || flags != 0))
        result = result + format("  c:%6d ", count)
            + print_flags();
    return result;
}

std::vector<Page_Info> page_info(const void * addr, int npages)
{
    int pm_fd = open("/proc/self/pagemap", O_RDONLY);
    if (pm_fd == -1)
        throw Exception("open pagemap; " + string(strerror(errno)));
    Call_Guard close_pm_fd(boost::bind(::close, pm_fd));

    // Both of these might fail as these two files require special priviledges
    // to read
    int pf_fd = open("/proc/kpageflags", O_RDONLY);
    int pc_fd = open("/proc/kpagecount", O_RDONLY);

    // These will call guard with an fd of -1 if the files weren't open, which
    // won't hurt us
    Call_Guard close_pf_fd(boost::bind(::close, pf_fd), pf_fd != -1);
    Call_Guard close_pc_fd(boost::bind(::close, pc_fd), pc_fd != -1);

    size_t page_num = (size_t)addr / 4096;

    int res = lseek(pm_fd, page_num * 8, SEEK_SET);
    if (res == -1)
        throw Exception("page_info(): lseek: " + string(strerror(errno)));

    vector<Page_Info> result;
    for (unsigned i = 0;  i < npages;  ++i) {
        Page_Info info;

        int res = read(pm_fd, &info.mapping, 8);
        if (res != 8)
            throw Exception("read pm_fd");

        if (info.present) {
            if (pf_fd != -1) {
                int res = lseek(pf_fd, info.pfn * 8, SEEK_SET);
                if (res == -1)
                    throw Exception("lseek pf_fd");
                res = read(pf_fd, &info.flags, 8);
                if (res != 8)
                    throw Exception("read flags");
            }

            if (pc_fd != -1) {
                res = lseek(pc_fd, info.pfn * 8, SEEK_SET);
                if (res == -1)
                    throw Exception("lseek pc_fd");
                res = read(pc_fd, &info.count, 8);
                if (res != 8)
                    throw Exception("read count");
            }
        }

        result.push_back(info);
    }

    return result;
}

void dump_page_info(const void * start, const void * end,
                    std::ostream & stream)
{
    const char * p1 = (const char *)start;
    const char * p2 = (const char *)end;
    size_t dist = p2 - p1;
    size_t pages = dist / page_size;
    size_t rem   = dist % page_size;
    if (rem > 0) ++pages;

    if (pages > 10000)
        throw Exception("dump_page_info: too many pages");

    vector<Page_Info> info = page_info(start, pages);

    if (info.size() != pages)
        throw Exception("no pages");

    const char * p = page_start(p1);
    for (unsigned i = 0;  i < pages;  ++i, p += page_size) {
        stream << format("%04x %012p ", i, p)
               << info[i] << endl;
    }
}

std::vector<unsigned char> page_flags(const void * addr, int npages)
{
    int pm_fd = open("/proc/self/pagemap", O_RDONLY);
    if (pm_fd == -1)
        throw Exception("open pagemap; " + string(strerror(errno)));
    Call_Guard close_pm_fd(boost::bind(::close, pm_fd));

    size_t page_num = (size_t)addr / 4096;

    int res = lseek(pm_fd, page_num * 8, SEEK_SET);
    if (res == -1)
        throw Exception("page_info(): lseek: " + string(strerror(errno)));

    vector<unsigned char> result;
    result.reserve(npages);

    Page_Info info;

    uint64_t buf[1024];

    for (unsigned i = 0;  i < npages;  i += 1024) {
        int n = min<size_t>(1024, npages - i);

        int res = read(pm_fd, buf, 8 * n);

        if (res != 8 * n)
            throw Exception("read pm_fd");

        for (unsigned j = 0;  j < n;  ++j) {
            info.mapping = buf[j];
            result.push_back(info.present);
        }
    }

    return result;

}

void dump_maps(std::ostream & out)
{
    std::ifstream stream("/proc/self/maps");

    out << string(60, '=') << endl;
    out << "maps" << endl;

    while (stream) {
        string s;
        std::getline(stream, s);
        out << s << endl;
    }

    out << string(60, '=') << endl;
    out << endl << endl;
}

/*****************************************************************************/
/* PAGEMAP_READER                                                            */
/*****************************************************************************/

Pagemap_Reader::
Pagemap_Reader(const char * mem, size_t bytes,
               Pagemap_Entry * entries, int fd)
    : fd(fd), mem(mem), entries(entries),
      delete_entries(entries == 0), close_fd(fd == -1)
{
    npages = to_page_num(mem + bytes) - to_page_num(mem);

#if 0
    cerr << "mem = " << (const void *)mem << endl;
    cerr << "mem + bytes = " << (const void *)(mem + bytes) << endl;
    cerr << "page1 = " << to_page_num(mem + bytes) << endl;
    cerr << "page2 = " << to_page_num(mem) << endl;
    cerr << "bytes = " << bytes << endl;
    cerr << "npages = " << npages << endl;
#endif

    if (close_fd)
        this->fd = open("/proc/self/pagemap", O_RDONLY);
    if (this->fd == -1)
        throw Exception(errno, "Pagemap_Reader()",
                        "open(\"proc/self/pagemap\", O_RDONLY)");
    Call_Guard do_close_fd(boost::bind(close, this->fd), close_fd);

    if (delete_entries)
        this->entries = new Pagemap_Entry[npages];

    try {
        update();
    } catch (...) {
        delete[] entries;
        throw;
    }

    do_close_fd.clear();
}

Pagemap_Reader::
~Pagemap_Reader()
{
    if (delete_entries)
        delete[] entries;

    if (close_fd && fd != -1) {
        int res = close(fd);
        if (res == -1)
            cerr << "~Pagemap_Reader(): close on fd: " << strerror(errno)
                 << endl;
    }

    entries = 0;
}

size_t
Pagemap_Reader::
update(ssize_t first_page, ssize_t last_page)
{
    if (last_page == -1)
        last_page = npages;

    if (first_page < 0 || last_page > npages || last_page < first_page)
        throw Exception("Pagemap_Reader::update(): pages out of range");

    // Where do we seek to in the pagemap file?

    // Counts the number of modified page map entries
    size_t result = 0;

    size_t CHUNK = 1024;  // pages at a time

    // Buffer to read them into
    Pagemap_Entry buf[CHUNK];

    size_t base_page_num = to_page_num(mem);

    //cerr << "update: first_page = " << first_page
    //     << " last_page = " << last_page << endl;

    // Update a chunk at a time
    for (size_t page = first_page;  page < last_page;  /* no inc */) {
        size_t limit = std::min<size_t>(page + CHUNK, last_page);

        //cerr << "page = " << page << " last_page = " << last_page
        //     << " limit = " << limit << endl;

        // Where to seek in the pagemap file?
        off_t seek_pos = (base_page_num + page) * sizeof(Pagemap_Entry);

        //cerr << "seek_pos = " << seek_pos << " base_page_num = "
        //     << base_page_num << endl;

        ssize_t res = pread(fd, buf,
                            (limit - page) * sizeof(Pagemap_Entry),
                            seek_pos);
        if (res == -1)
            throw Exception(errno, "Pagemap_Reader::update()", "pread");
        if (res <= 0)
            throw Exception("Pagemap_Reader::update(): nothing read "
                            "from pagemap file");

        //cerr << "res = " << res << endl;

        res /= sizeof(Pagemap_Entry);  // convert bytes to objects

        //cerr << "read " << res << " objects" << endl;

        for (unsigned i = 0;  i < res;  ++i) {
            result += this->entries[page + i] != buf[i];
            this->entries[page + i] = buf[i];
        }

        page += res;
    }

    return result;
}

void
Pagemap_Reader::
dump(std::ostream & stream) const
{
    const char * p = mem;
    for (unsigned i = 0;  i < npages;  ++i, p += page_size)
        stream << format("%04x %012p ", i, p)
               << entries[i] << endl;
}

} // namespace ML

<|endoftext|>


<commit_before>#include <binary_search_tree.hpp>
#include <catch.hpp>

SCENARIO("default constructor")
{
	BinarySearchTree<int> bst;
	REQUIRE(bst.root() == nullptr);
}

SCENARIO("insertElement")
{
	BinarySearchTree<int> bst;
	bst.insert(7);
	REQUIRE(bst.value_() == 7);
	REQUIRE(bst.leftNode_() == nullptr);
	REQUIRE(bst.rightNode_() == nullptr);
}

SCENARIO("findElement")
{
	BinarySearchTree<int> bst;
	bst.insert(7);
	REQUIRE(bst.isFound(7) == 1);
}

SCENARIO("infile")
{
	BinarySearchTree<int> bst;
	bst.infile("file.txt");
	REQUIRE(bst.count() == 0);
}

SCENARIO("count")
{
	BinarySearchTree<int> bst;
	int count = 0;
	bst.insert(7);
	count = bst.count();
	REQUIRE(count == 1);
}
<commit_msg>Update init.cpp<commit_after>#include <binary_search_tree.hpp>
#include <catch.hpp>

SCENARIO("default constructor")
{
	BinarySearchTree<int> bst;
	REQUIRE(bst.root() == nullptr);
}

SCENARIO("insertElement")
{
	BinarySearchTree<int> bst;
	bst.insert(7);
	REQUIRE(bst.value_() == 7);
	REQUIRE(bst.leftNode_() == nullptr);
	REQUIRE(bst.rightNode_() == nullptr);
}

SCENARIO("findElement")
{
	BinarySearchTree<int> bst;
	bst.insert(7);
	REQUIRE(bst.isFound(7) == 1);
}

SCENARIO("infile")
{
	BinarySearchTree<int> bst;
	bst.infile("file.txt");
	REQUIRE(bst.count() == 0);
}

SCENARIO("_count")
{
	BinarySearchTree<int> bst;
	int count = 0;
	bst.insert(7);
	count = bst.count();
	REQUIRE(count == 1);
}
<|endoftext|>


<commit_before>/*************************************************************************
 *
 *  $RCSfile: vendorbase.hxx,v $
 *
 *  $Revision: 1.3 $
 *
 *  last change: $Author: kz $ $Date: 2004-12-16 11:46:22 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

#if !defined INCLUDED_JFW_PLUGIN_VENDORBASE_HXX
#define INCLUDED_JFW_PLUGIN_VENDORBASE_HXX

#include "rtl/ustring.hxx"
#include "rtl/ref.hxx"
#include "salhelper/simplereferenceobject.hxx"
#include <vector>

namespace jfw_plugin
{

//Used by subclasses of VendorBase to build paths to Java runtime
#if defined UNX
#if defined SPARC
#define JFW_PLUGIN_ARCH "sparc"
#elif defined INTEL
#define JFW_PLUGIN_ARCH "i386"
#elif defined POWERPC
#define JFW_PLUGIN_ARCH "ppc"
#elif defined MIPS
#define JFW_PLUGIN_ARCH "mips"
#elif defined S390
#define JFW_PLUGIN_ARCH "s390"
#else // SPARC, INTEL, POWERPC, MIPS
#error unknown plattform
#endif // SPARC, INTEL, POWERPC, MIPS
#endif

class MalformedVersionException
{
public:
    MalformedVersionException();

    MalformedVersionException(const MalformedVersionException &);

    virtual ~MalformedVersionException();

    MalformedVersionException & operator =(const MalformedVersionException &);
};

class VendorBase: public salhelper::SimpleReferenceObject
{
public:
    VendorBase();
    /* returns relativ paths to the java executable as
       file URLs.

       For example "bin/java.exe". You need
       to implement this function in a derived class, if
       the paths differ.  this implmentation provides for
       Windows "bin/java.exe" and for Unix "bin/java".
       The paths are relative file URLs. That is, they always
       contain '/' even on windows. The paths are relative
       to the installation directory of a JRE.

       The signature of this function must correspond to
       getJavaExePaths_func.
     */
    static char const* const *  getJavaExePaths(int* size);

    /* creates an instance of this class. MUST be overridden
       in a derived class.
       ####################################################
       OVERRIDE in derived class
       ###################################################
       @param
       Key - value pairs of the system properties of the JRE.
     */
    static rtl::Reference<VendorBase> createInstance();

    /* called automatically on the instance created by createInstance.

       @return
       true - the object could completely initialize.
       false - the object could not completly initialize. In this case
       it will be discarded by the caller.
    */
    virtual bool initialize(
        std::vector<std::pair<rtl::OUString, rtl::OUString> > props);

    /* returns relative file URLs to the runtime library.
       For example         "/bin/client/jvm.dll"
    */
    virtual char const* const* getRuntimePaths(int* size);

    virtual char const* const* getLibraryPaths(int* size);

    virtual const rtl::OUString & getVendor() const;
    virtual const rtl::OUString & getVersion() const;
    virtual const rtl::OUString & getHome() const;
    virtual const rtl::OUString & getRuntimeLibrary() const;
    virtual const rtl::OUString & getLibraryPaths() const;
    virtual bool supportsAccessibility() const;
    /* determines if prior to running java something has to be done,
       like setting the LD_LIBRARY_PATH. This implementation checks
       if an LD_LIBRARY_PATH (getLD_LIBRARY_PATH) needs to be set and
       if so, needsRestart returns true.
     */
    virtual bool needsRestart() const;

    /* compares versions of this vendor. MUST be overridden
       in a derived class.
       ####################################################
       OVERRIDE in derived class
       ###################################################
      @return
      0 this.version == sSecond
      1 this.version > sSecond
      -1 this.version < sSEcond

      @throw
      MalformedVersionException if the version string was not recognized.
     */
    virtual int compareVersions(const rtl::OUString& sSecond) const;

protected:

    rtl::OUString m_sVendor;
    rtl::OUString m_sVersion;
    rtl::OUString m_sHome;
    rtl::OUString m_sRuntimeLibrary;
    rtl::OUString m_sLD_LIBRARY_PATH;
    bool m_bAccessibility;

    typedef rtl::Reference<VendorBase> (* createInstance_func) ();
    friend rtl::Reference<VendorBase> createInstance(
        createInstance_func pFunc,
        std::vector<std::pair<rtl::OUString, rtl::OUString> > properties);
};

}

#endif
<commit_msg>INTEGRATION: CWS ooo19126 (1.3.32); FILE MERGED 2005/09/05 17:12:25 rt 1.3.32.1: #i54170# Change license header: remove SISSL<commit_after>/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: vendorbase.hxx,v $
 *
 *  $Revision: 1.4 $
 *
 *  last change: $Author: rt $ $Date: 2005-09-07 19:33:05 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/

#if !defined INCLUDED_JFW_PLUGIN_VENDORBASE_HXX
#define INCLUDED_JFW_PLUGIN_VENDORBASE_HXX

#include "rtl/ustring.hxx"
#include "rtl/ref.hxx"
#include "salhelper/simplereferenceobject.hxx"
#include <vector>

namespace jfw_plugin
{

//Used by subclasses of VendorBase to build paths to Java runtime
#if defined UNX
#if defined SPARC
#define JFW_PLUGIN_ARCH "sparc"
#elif defined INTEL
#define JFW_PLUGIN_ARCH "i386"
#elif defined POWERPC
#define JFW_PLUGIN_ARCH "ppc"
#elif defined MIPS
#define JFW_PLUGIN_ARCH "mips"
#elif defined S390
#define JFW_PLUGIN_ARCH "s390"
#else // SPARC, INTEL, POWERPC, MIPS
#error unknown plattform
#endif // SPARC, INTEL, POWERPC, MIPS
#endif

class MalformedVersionException
{
public:
    MalformedVersionException();

    MalformedVersionException(const MalformedVersionException &);

    virtual ~MalformedVersionException();

    MalformedVersionException & operator =(const MalformedVersionException &);
};

class VendorBase: public salhelper::SimpleReferenceObject
{
public:
    VendorBase();
    /* returns relativ paths to the java executable as
       file URLs.

       For example "bin/java.exe". You need
       to implement this function in a derived class, if
       the paths differ.  this implmentation provides for
       Windows "bin/java.exe" and for Unix "bin/java".
       The paths are relative file URLs. That is, they always
       contain '/' even on windows. The paths are relative
       to the installation directory of a JRE.

       The signature of this function must correspond to
       getJavaExePaths_func.
     */
    static char const* const *  getJavaExePaths(int* size);

    /* creates an instance of this class. MUST be overridden
       in a derived class.
       ####################################################
       OVERRIDE in derived class
       ###################################################
       @param
       Key - value pairs of the system properties of the JRE.
     */
    static rtl::Reference<VendorBase> createInstance();

    /* called automatically on the instance created by createInstance.

       @return
       true - the object could completely initialize.
       false - the object could not completly initialize. In this case
       it will be discarded by the caller.
    */
    virtual bool initialize(
        std::vector<std::pair<rtl::OUString, rtl::OUString> > props);

    /* returns relative file URLs to the runtime library.
       For example         "/bin/client/jvm.dll"
    */
    virtual char const* const* getRuntimePaths(int* size);

    virtual char const* const* getLibraryPaths(int* size);

    virtual const rtl::OUString & getVendor() const;
    virtual const rtl::OUString & getVersion() const;
    virtual const rtl::OUString & getHome() const;
    virtual const rtl::OUString & getRuntimeLibrary() const;
    virtual const rtl::OUString & getLibraryPaths() const;
    virtual bool supportsAccessibility() const;
    /* determines if prior to running java something has to be done,
       like setting the LD_LIBRARY_PATH. This implementation checks
       if an LD_LIBRARY_PATH (getLD_LIBRARY_PATH) needs to be set and
       if so, needsRestart returns true.
     */
    virtual bool needsRestart() const;

    /* compares versions of this vendor. MUST be overridden
       in a derived class.
       ####################################################
       OVERRIDE in derived class
       ###################################################
      @return
      0 this.version == sSecond
      1 this.version > sSecond
      -1 this.version < sSEcond

      @throw
      MalformedVersionException if the version string was not recognized.
     */
    virtual int compareVersions(const rtl::OUString& sSecond) const;

protected:

    rtl::OUString m_sVendor;
    rtl::OUString m_sVersion;
    rtl::OUString m_sHome;
    rtl::OUString m_sRuntimeLibrary;
    rtl::OUString m_sLD_LIBRARY_PATH;
    bool m_bAccessibility;

    typedef rtl::Reference<VendorBase> (* createInstance_func) ();
    friend rtl::Reference<VendorBase> createInstance(
        createInstance_func pFunc,
        std::vector<std::pair<rtl::OUString, rtl::OUString> > properties);
};

}

#endif
<|endoftext|>


<commit_before>#include "catch.hpp"
#include <iostream>
#include "Database_Creator.hpp"
#include "Database_Sorter.hpp"

person readPerson(std::string file_name, size_t index) {
	person result;
	std::ifstream file(file_name);
	for (size_t i = 0; i < index + 1; i++) {
		file >> result;
	}
	file.close();
	return result;
}
/*bool isALessThanB(char A, char B, bool is_capital) {
	std::string alphabet;
	if (is_capital) {
		alphabet = " АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ";
	}
	else {
		alphabet = " абвгдеёжзийклмнопрстуфхцчшщъыьэюя";
	}
	size_t A_i;
	size_t B_i;
	for (size_t i = 0; i < alphabet.size(); i++) {
		if (alphabet[i] == A) {
			A_i = i;
		}
		if (alphabet[i] == B) {
			B_i = i;
		}
	}
	return A_i < B_i;
}*/
bool isANotMoreThanB(person A, person B) {
	for (size_t i = 0; i < A.surname.length(); i++) {
		char A_char = A.surname[i];
		char B_char = (i < B.surname.length()) ? B.surname[i] : ' ';
		if (letterI(A.surname[i], i == 0) < letterI(B.surname[i], i == 0)) {
			return true;
		}
		else {
			if (A.surname[i] != B.surname[i]) {
				return false;
			}
		}
	}
	return true;
}
SCENARIO("LetterI", "[l]") {
   REQUIRE(letterI('A', true) == 1);
   REQUIRE(letterI('d', false) == 4);
}
SCENARIO("Sort", "[s]") {
	//setlocale(LC_ALL, "ru_RU.utf8");
	std::ifstream ru("russian_letters.txt");
	for (size_t i = 0; i < 6; i++) {
		std::string str;
		ru >> str;
		russian_letters[i] = str[0];
	}
	ru.close();
	size_t n_persons = 2001;
	size_t RAM_amount = 10000;
	std::string names_file_name = "Names";
	std::string surnames_file_name = "Surnames";
	std::string database_file_name = "database.txt";
	std::string output_file_name = "sorted_database.txt";
	{
		Database_Creator database_creator(database_file_name, names_file_name, surnames_file_name, n_persons);
		Database_Sorter database_sorter(database_file_name, output_file_name, RAM_amount);
		size_t start = clock();
		database_sorter.sortDatabase();
		size_t result = clock() - start;
		std::cout << result << std::endl;
		system("pause");
	}

	for (size_t i = 0; i < 250; i++) {
		size_t person1_i = rand() % n_persons;
		size_t person2_i = person1_i + rand() % (n_persons - person1_i);
                REQUIRE(person1_i < person2_i);
		person person1 = readPerson(output_file_name, person1_i);
		person person2 = readPerson(output_file_name, person2_i);
		REQUIRE(isANotMoreThanB(person1, person2));
	}

	/*std::string str = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ";
	REQUIRE(letterI(str[1], true) == 2);
	REQUIRE(letterI(str[2], true) == 3);
	REQUIRE(letterI(str[3], true) == 4);
	REQUIRE(letterI(str[4], true) == 5);*/
}
<commit_msg>Update init.cpp<commit_after>#include "catch.hpp"
#include <iostream>
#include "Database_Creator.hpp"
#include "Database_Sorter.hpp"

person readPerson(std::string file_name, size_t index) {
	person result;
	std::ifstream file(file_name);
	for (size_t i = 0; i < index + 1; i++) {
		file >> result;
	}
	file.close();
	return result;
}
/*bool isALessThanB(char A, char B, bool is_capital) {
	std::string alphabet;
	if (is_capital) {
		alphabet = " АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ";
	}
	else {
		alphabet = " абвгдеёжзийклмнопрстуфхцчшщъыьэюя";
	}
	size_t A_i;
	size_t B_i;
	for (size_t i = 0; i < alphabet.size(); i++) {
		if (alphabet[i] == A) {
			A_i = i;
		}
		if (alphabet[i] == B) {
			B_i = i;
		}
	}
	return A_i < B_i;
}*/
bool isANotMoreThanB(person A, person B) {
	for (size_t i = 0; i < A.surname.length(); i++) {
		char A_char = A.surname[i];
		char B_char = (i < B.surname.length()) ? B.surname[i] : ' ';
		if (letterI(A.surname[i], i == 0) < letterI(B.surname[i], i == 0)) {
			return true;
		}
		else {
			if (A.surname[i] != B.surname[i]) {
				return false;
			}
		}
	}
	return true;
}
SCENARIO("LetterI", "[l]") {
   REQUIRE(letterI('A', true) == 1);
   REQUIRE(letterI('d', false) == 4);
}
SCENARIO("Sort", "[s]") {
	//setlocale(LC_ALL, "ru_RU.utf8");
	std::ifstream ru("russian_letters.txt");
	for (size_t i = 0; i < 6; i++) {
		std::string str;
		ru >> str;
		russian_letters[i] = str[0];
	}
	ru.close();
	size_t n_persons = 2001;
	size_t RAM_amount = 10000;
	std::string names_file_name = "Names";
	std::string surnames_file_name = "Surnames";
	std::string database_file_name = "database.txt";
	std::string output_file_name = "sorted_database.txt";
	{
		Database_Creator database_creator(database_file_name, names_file_name, surnames_file_name, n_persons);
		Database_Sorter database_sorter(database_file_name, output_file_name, RAM_amount);
		size_t start = clock();
		database_sorter.sortDatabase();
		size_t result = clock() - start;
		std::cout << result << std::endl;
		system("pause");
	}

	for (size_t i = 0; i < 250; i++) {
		size_t person1_i = rand() % n_persons;
		size_t person2_i = person1_i + rand() % (n_persons - person1_i);
                REQUIRE(person1_i <= person2_i);
		person person1 = readPerson(output_file_name, person1_i);
		person person2 = readPerson(output_file_name, person2_i);
		REQUIRE(isANotMoreThanB(person1, person2));
	}

	/*std::string str = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ";
	REQUIRE(letterI(str[1], true) == 2);
	REQUIRE(letterI(str[2], true) == 3);
	REQUIRE(letterI(str[3], true) == 4);
	REQUIRE(letterI(str[4], true) == 5);*/
}
<|endoftext|>


<commit_before>// (c) ZaKlaus 2016; Apache 2 Licensed, see LICENSE;;

#if !defined(DZM_REP_H)

static inline void
test_load_file(const char *Name)
{
    b32 OldOk = PrintOk;
    PrintOk = 0;

    FILE *File = fopen(Name, "r");

    if(File == 0)
    {
        fprintf(stderr, "File not found\n");
        exit(1);
    }
    OBJECT *Exp;
    while(1)
    {
        Exp = read(File);
        if(Exp == 0)
        {
            break;
        }
        write(stdout, eval(Exp, GlobalEnv));
    }
    fclose(File);
    PrintOk = OldOk;
}

static inline void
test_repl(void)
{
    PrintOk = 1;
    for(;;)
    {
        printf(": ");
        FILE *Stream = read_input(stdin);
        write(stdout, eval(read(Stream), GlobalEnv));
        printf("\n");
    }
}

static inline void
test_init(int argc, char** argv)
{
    GlobalArena = (MEMORY_ARENA *)malloc(sizeof(MEMORY_ARENA));

    if(!GlobalArena)
    {
        printf("Memory initialization error!\n");
        exit(-1);
    }

    initialize_arena(GlobalArena, MAX_VM_SIZE, malloc(MAX_VM_SIZE));

    init_mem();

    init_logging();
    FILE *Log = fopen("dzm_log.txt", "w");
    set_log_output(Log);
    set_log_verbose(1);

    init_defs();
    test_load_file("std/stdlib.scm");

    if(argc < 2)
    {
        printf("DZMLang REPL; By ZaKlaus.\nUse ^C to exit.\n");
        printf("Version: %s\n", DZM_VERSION);
        test_repl();
        return;
    }
    printf("DZMLang Interpreter; By ZaKlaus.\n");

    if(!strcmp(argv[1], "--repl"))
    {
        test_load_file(argv[2]);
        test_repl();
    }
    else test_load_file(argv[1]);

    fclose(Log);
}

#define DZM_REP_H
#endif
<commit_msg>Instead of exiting the process, run in minimal mode, if std is not available.<commit_after>// (c) ZaKlaus 2016; Apache 2 Licensed, see LICENSE;;

#if !defined(DZM_REP_H)

static inline void
test_load_file(const char *Name)
{
    b32 OldOk = PrintOk;
    PrintOk = 0;

    FILE *File = fopen(Name, "r");

    if(File == 0)
    {
        if(!strcmp(Name,"std/stdlib.scm"))
        {
            LOG(ERR_WARN, "%s\n", "Standard Library couldn't be loaded! Running in minimal mode!");
            return;
        }
        else
        {
            fprintf(stderr, "File not found\n");
            exit(0);
        }
    }
    OBJECT *Exp;
    while(1)
    {
        Exp = read(File);
        if(Exp == 0)
        {
            break;
        }
        write(stdout, eval(Exp, GlobalEnv));
    }
    fclose(File);
    PrintOk = OldOk;
}

static inline void
test_repl(void)
{
    PrintOk = 1;
    for(;;)
    {
        printf(": ");
        FILE *Stream = read_input(stdin);
        write(stdout, eval(read(Stream), GlobalEnv));
        printf("\n");
    }
}

static inline void
test_init(int argc, char** argv)
{
    GlobalArena = (MEMORY_ARENA *)malloc(sizeof(MEMORY_ARENA));

    if(!GlobalArena)
    {
        printf("Memory initialization error!\n");
        exit(-1);
    }

    initialize_arena(GlobalArena, MAX_VM_SIZE, malloc(MAX_VM_SIZE));

    init_mem();

    init_logging();
    FILE *Log = fopen("dzm_log.txt", "w");
    set_log_output(Log);
    set_log_verbose(1);

    init_defs();
    test_load_file("std/stdlib.scm");

    if(argc < 2)
    {
        printf("DZMLang REPL; By ZaKlaus.\nUse ^C to exit.\n");
        printf("Version: %s\n", DZM_VERSION);
        test_repl();
        return;
    }
    printf("DZMLang Interpreter; By ZaKlaus.\n");

    if(!strcmp(argv[1], "--repl"))
    {
        test_load_file(argv[2]);
        test_repl();
    }
    else test_load_file(argv[1]);

    fclose(Log);
}

#define DZM_REP_H
#endif
<|endoftext|>