<commit_before>// Copyright (c) 2015, Galaxy Authors. All Rights Reserved
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "sdk/galaxy.h"

#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <errno.h>
#include <string.h>
#include <gflags/gflags.h>

#include <tprinter.h>
#include <string_util.h>

DECLARE_string(master_host);
DECLARE_string(master_port);
DECLARE_string(flagfile);

const std::string kGalaxyUsage = "\n./galaxy_client submit <job_name> <job_package> <replica> <cpu> <mem> <start_cmd> <batch> <deploy_step> <label>\n"
                                 "./galaxy_client list\n"
                                 "./galaxy_client listagent\n"
                                 "./galaxy_client kill <jobid>\n"
                                 "./galaxy_client update <jobid> <replica>\n"
                                 "./galaxy_client label <label_name> <agent_endpoints_file>";

bool LoadAgentEndpointsFromFile(
        const std::string& file_name,
        std::vector<std::string>* agents) {
    const int LINE_BUF_SIZE = 1024;
    char line_buf[LINE_BUF_SIZE];
    std::ifstream fin(file_name.c_str(), std::ifstream::in);
    if (!fin.is_open()) {
        fprintf(stderr, "open %s failed\n", file_name.c_str());
        return false;
    }

    bool ret = true;
    while (fin.good()) {
        fin.getline(line_buf, LINE_BUF_SIZE);
        if (fin.gcount() == LINE_BUF_SIZE) {
            fprintf(stderr, "line buffer size overflow\n");
            ret = false;
            break;
        } else if (fin.gcount() == 0) {
            continue;
        }
        fprintf(stdout, "label %s\n", line_buf);
        // NOTE string size should == strlen
        std::string agent_endpoint(line_buf, strlen(line_buf));
        agents->push_back(agent_endpoint);
    }
    if (!ret) {
        fin.close();
        return false;
    }

    if (fin.fail()) {
        fin.close();
        return false;
    }
    fin.close();
    return true;
}

int LabelAgent(int argc, char* argv[]) {
    if (argc < 1) {
        return -1;
    }
    std::string label(argv[0]);
    std::vector<std::string> agent_endpoints;
    if (argc == 2 && LoadAgentEndpointsFromFile(argv[1], &agent_endpoints)) {
        return -1;
    }
    baidu::galaxy::Galaxy* galaxy = baidu::galaxy::Galaxy::ConnectGalaxy(FLAGS_master_host + ":" + FLAGS_master_port);
    if (!galaxy->LabelAgents(label, agent_endpoints)) {
        return -1;
    }
    return 0;
}

void ReadBinary(const std::string& file, std::string* binary) {
    FILE* fp = fopen(file.c_str(), "rb");
    if (fp == NULL) {
        fprintf(stderr, "Read binary fail\n");
        return;
    }
    char buf[1024];
		int len = 0;
    while ((len = fread(buf, 1, sizeof(buf), fp)) > 0) {
        binary->append(buf, len);
    }
		fclose(fp);
}

int AddJob(int argc, char* argv[]) {
    if (argc < 6) {
        return 1;
    }
    baidu::galaxy::Galaxy* galaxy = baidu::galaxy::Galaxy::ConnectGalaxy(FLAGS_master_host + ":" + FLAGS_master_port);
    baidu::galaxy::JobDescription job;
    job.job_name = argv[0];
    ReadBinary(argv[1], &job.binary);
    printf("binary size %lu\n", job.binary.size());
    job.replica = atoi(argv[2]);
    job.cpu_required = atoi(argv[3]);
    job.mem_required = atoi(argv[4]);
    job.deploy_step = 0;

    job.cmd_line = argv[5];
    job.is_batch = (argc > 6 && 0 == strcmp(argv[6], "batch"));
    job.deploy_step = job.replica;
    if (argc > 7) {
        job.deploy_step = atoi(argv[7]);
    }
    if (argc > 8) {
        job.label = argv[8];
    }
    std::string jobid = galaxy->SubmitJob(job);
    if (jobid.empty()) {
        fprintf(stderr, "Submit job fail\n");
        return 1;
    }
    printf("Submit job %s\n", jobid.c_str());
    return 0;
}

int UpdateJob(int argc, char* argv[]) {
    if (argc < 2) {
        return 1;
    }
    baidu::galaxy::Galaxy* galaxy = baidu::galaxy::Galaxy::ConnectGalaxy(FLAGS_master_host + ":" + FLAGS_master_port);
    baidu::galaxy::JobDescription desc;
    std::string jobid;
    jobid.assign(argv[0]);
    desc.replica = atoi(argv[1]);
    bool ok = galaxy->UpdateJob(jobid, desc);
    if (ok) {
        printf("Update job %s ok\n", jobid.c_str());
        return 0;
    }else {
        printf("Fail to update job %s\n", jobid.c_str());
        return 1;
    }
}

int ListAgent(int /*argc*/, char*[] /*argv*/) {
    baidu::galaxy::Galaxy* galaxy = baidu::galaxy::Galaxy::ConnectGalaxy(FLAGS_master_host + ":" + FLAGS_master_port);
    std::vector<baidu::galaxy::NodeDescription> agents;
    baidu::common::TPrinter tp(11);
    tp.AddRow(11, "", "addr","state", "pods", "cpu_used", "cpu_assigned", "cpu_total", "mem_used", "mem_assigned", "mem_total", "labels");
    if (galaxy->ListAgents(&agents)) {
        for (uint32_t i = 0; i < agents.size(); i++) {
            std::vector<std::string> vs;
            vs.push_back(baidu::common::NumToString(i + 1));
            vs.push_back(agents[i].addr);
            vs.push_back(agents[i].state);
            vs.push_back(baidu::common::NumToString(agents[i].task_num));
            vs.push_back(baidu::common::NumToString(agents[i].cpu_used));
            vs.push_back(baidu::common::NumToString(agents[i].cpu_assigned));
            vs.push_back(baidu::common::NumToString(agents[i].cpu_share));
            vs.push_back(baidu::common::NumToString(agents[i].mem_used));
            vs.push_back(baidu::common::NumToString(agents[i].mem_assigned));
            vs.push_back(baidu::common::NumToString(agents[i].mem_share));
            vs.push_back(agents[i].labels);
            tp.AddRow(vs);
		}
        printf("%s\n", tp.ToString().c_str());
				return 0;
		}
		printf("Listagent fail\n");
		return 1;
}

int ListJob(int /*argc*/, char*[] /*argv*/) {
    baidu::galaxy::Galaxy* galaxy = baidu::galaxy::Galaxy::ConnectGalaxy(FLAGS_master_host + ":" + FLAGS_master_port);
    std::vector<baidu::galaxy::JobInformation> infos;
    baidu::common::TPrinter tp(9);
    tp.AddRow(9, "", "id", "name","state","stat(r/p/d)", "replica", "batch", "cpu", "memory");
    if (galaxy->ListJobs(&infos)) {
        for (uint32_t i = 0; i < infos.size(); i++) {
            std::vector<std::string> vs;
            vs.push_back(baidu::common::NumToString(i + 1));
            vs.push_back(infos[i].job_id);
            vs.push_back(infos[i].job_name);
            vs.push_back(infos[i].state);
            vs.push_back(baidu::common::NumToString(infos[i].running_num) + "/" +
                         baidu::common::NumToString(infos[i].pending_num) + "/" +
                         baidu::common::NumToString(infos[i].deploying_num));
            vs.push_back(baidu::common::NumToString(infos[i].replica));
						vs.push_back(infos[i].is_batch ? "batch" : "");
            vs.push_back(baidu::common::NumToString(infos[i].cpu_used));
            vs.push_back(baidu::common::NumToString(infos[i].mem_used));
            tp.AddRow(vs);
        }
        printf("%s\n", tp.ToString().c_str());
        return 0;
    }
    fprintf(stderr, "List fail\n");
    return 1;
}

int KillJob(int argc, char* argv[]) {
    if (argc < 1) {
        return 1;
    }
    baidu::galaxy::Galaxy* galaxy = baidu::galaxy::Galaxy::ConnectGalaxy(FLAGS_master_host + ":" + FLAGS_master_port);
    std::string jobid(argv[0]);
    if (jobid.empty()) {
        return 1;
    }
    if (galaxy->TerminateJob(jobid)) {
        printf("terminate job %s successfully\n", argv[0]);
        return 0;
    }
    return 1;
}

int main(int argc, char* argv[]) {
    FLAGS_flagfile = "./galaxy.flag";
    ::google::SetUsageMessage(kGalaxyUsage);
    ::google::ParseCommandLineFlags(&argc, &argv, true);
    if(argc < 2){
        fprintf(stderr,"Usage:%s\n", kGalaxyUsage.c_str());
        return -1;
    }
    if (strcmp(argv[1], "submit") == 0) {
        return AddJob(argc - 2, argv + 2);
    } else if (strcmp(argv[1], "list") == 0) {
        return ListJob(argc - 2, argv + 2);
    } else if (strcmp(argv[1], "listagent") == 0) {
		return ListAgent(argc - 2, argv + 2);
    } else if (strcmp(argv[1], "label") == 0) {
        return LabelAgent(argc - 2, argv + 2);
    } else if (strcmp(argv[1], "update") == 0) {
        return UpdateJob(argc - 2, argv + 2);
    }else if (strcmp(argv[1], "kill")== 0){
        return KillJob(argc - 2 , argv + 2);
    } else {
        fprintf(stderr,"Usage:%s\n", kGalaxyUsage.c_str());
        return -1;
    }
    return 0;
}

/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */
<commit_msg>Revert "merge"<commit_after>// Copyright (c) 2015, Galaxy Authors. All Rights Reserved
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "sdk/galaxy.h"

#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <errno.h>
#include <string.h>
#include <gflags/gflags.h>

#include <tprinter.h>
#include <string_util.h>

DECLARE_string(master_host);
DECLARE_string(master_port);
DECLARE_string(flagfile);

const std::string kGalaxyUsage = "\n./galaxy_client submit <job_name> <job_package> <replica> <cpu> <mem> <start_cmd> <batch> <deploy_step> <label>\n"
                                 "./galaxy_client list\n"
                                 "./galaxy_client listagent\n"
                                 "./galaxy_client kill <jobid>\n"
                                 "./galaxy_client update <jobid> <replica>\n"
                                 "./galaxy_client label <label_name> <agent_endpoints_file>";

bool LoadAgentEndpointsFromFile(
        const std::string& file_name,
        std::vector<std::string>* agents) {
    const int LINE_BUF_SIZE = 1024;
    char line_buf[LINE_BUF_SIZE];
    std::ifstream fin(file_name.c_str(), std::ifstream::in);
    if (!fin.is_open()) {
        fprintf(stderr, "open %s failed\n", file_name.c_str());
        return false;
    }

    bool ret = true;
    while (fin.good()) {
        fin.getline(line_buf, LINE_BUF_SIZE);
        if (fin.gcount() == LINE_BUF_SIZE) {
            fprintf(stderr, "line buffer size overflow\n");
            ret = false;
            break;
        } else if (fin.gcount() == 0) {
            continue;
        }
        fprintf(stdout, "label %s\n", line_buf);
        // NOTE string size should == strlen
        std::string agent_endpoint(line_buf, strlen(line_buf));
        agents->push_back(agent_endpoint);
    }
    if (!ret) {
        fin.close();
        return false;
    }

    if (fin.fail()) {
        fin.close();
        return false;
    }
    fin.close();
    return true;
}

int LabelAgent(int argc, char* argv[]) {
    if (argc < 1) {
        return -1;
    }
    std::string label(argv[0]);
    std::vector<std::string> agent_endpoints;
    if (argc == 2 && LoadAgentEndpointsFromFile(argv[1], &agent_endpoints)) {
        return -1;
    }
    baidu::galaxy::Galaxy* galaxy = baidu::galaxy::Galaxy::ConnectGalaxy(FLAGS_master_host + ":" + FLAGS_master_port);
    if (!galaxy->LabelAgents(label, agent_endpoints)) {
        return -1;
    }
    return 0;
}

void ReadBinary(const std::string& file, std::string* binary) {
    FILE* fp = fopen(file.c_str(), "rb");
    if (fp == NULL) {
        fprintf(stderr, "Read binary fail\n");
        return;
    }
    char buf[1024];
		int len = 0;
    while ((len = fread(buf, 1, sizeof(buf), fp)) > 0) {
        binary->append(buf, len);
    }
		fclose(fp);
}

int AddJob(int argc, char* argv[]) {
    if (argc < 6) {
        return 1;
    }
    baidu::galaxy::Galaxy* galaxy = baidu::galaxy::Galaxy::ConnectGalaxy(FLAGS_master_host + ":" + FLAGS_master_port);
    baidu::galaxy::JobDescription job;
    job.job_name = argv[0];
    std::string binary(argv[1]);
    if (binary.substr(0, 6) == "ftp:://") {
        job.binary = binary;
    }else {
        ReadBinary(argv[1], &job.binary);
    }
    printf("binary size %lu\n", job.binary.size());
    job.replica = atoi(argv[2]);
    job.cpu_required = atoi(argv[3]);
    job.mem_required = atoi(argv[4]);
    job.deploy_step = 0;

    job.cmd_line = argv[5];
    job.is_batch = (argc > 6 && 0 == strcmp(argv[6], "batch"));
    job.deploy_step = job.replica;
    if (argc > 7) {
        job.deploy_step = atoi(argv[7]);
    }
    if (argc > 8) {
        job.label = argv[8];
    }
    std::string jobid = galaxy->SubmitJob(job);
    if (jobid.empty()) {
        fprintf(stderr, "Submit job fail\n");
        return 1;
    }
    printf("Submit job %s\n", jobid.c_str());
    return 0;
}

int UpdateJob(int argc, char* argv[]) {
    if (argc < 2) {
        return 1;
    }
    baidu::galaxy::Galaxy* galaxy = baidu::galaxy::Galaxy::ConnectGalaxy(FLAGS_master_host + ":" + FLAGS_master_port);
    baidu::galaxy::JobDescription desc;
    std::string jobid;
    jobid.assign(argv[0]);
    desc.replica = atoi(argv[1]);
    bool ok = galaxy->UpdateJob(jobid, desc);
    if (ok) {
        printf("Update job %s ok\n", jobid.c_str());
        return 0;
    }else {
        printf("Fail to update job %s\n", jobid.c_str());
        return 1;
    }
}

int ListAgent(int /*argc*/, char*[] /*argv*/) {
    baidu::galaxy::Galaxy* galaxy = baidu::galaxy::Galaxy::ConnectGalaxy(FLAGS_master_host + ":" + FLAGS_master_port);
    std::vector<baidu::galaxy::NodeDescription> agents;
    baidu::common::TPrinter tp(11);
    tp.AddRow(11, "", "addr","state", "pods", "cpu_used", "cpu_assigned", "cpu_total", "mem_used", "mem_assigned", "mem_total", "labels");
    if (galaxy->ListAgents(&agents)) {
        for (uint32_t i = 0; i < agents.size(); i++) {
            std::vector<std::string> vs;
            vs.push_back(baidu::common::NumToString(i + 1));
            vs.push_back(agents[i].addr);
            vs.push_back(agents[i].state);
            vs.push_back(baidu::common::NumToString(agents[i].task_num));
            vs.push_back(baidu::common::NumToString(agents[i].cpu_used));
            vs.push_back(baidu::common::NumToString(agents[i].cpu_assigned));
            vs.push_back(baidu::common::NumToString(agents[i].cpu_share));
            vs.push_back(baidu::common::NumToString(agents[i].mem_used));
            vs.push_back(baidu::common::NumToString(agents[i].mem_assigned));
            vs.push_back(baidu::common::NumToString(agents[i].mem_share));
            vs.push_back(agents[i].labels);
            tp.AddRow(vs);
		}
        printf("%s\n", tp.ToString().c_str());
				return 0;
		}
		printf("Listagent fail\n");
		return 1;
}

int ListJob(int /*argc*/, char*[] /*argv*/) {
    baidu::galaxy::Galaxy* galaxy = baidu::galaxy::Galaxy::ConnectGalaxy(FLAGS_master_host + ":" + FLAGS_master_port);
    std::vector<baidu::galaxy::JobInformation> infos;
    baidu::common::TPrinter tp(9);
    tp.AddRow(9, "", "id", "name","state","stat(r/p/d)", "replica", "batch", "cpu", "memory");
    if (galaxy->ListJobs(&infos)) {
        for (uint32_t i = 0; i < infos.size(); i++) {
            std::vector<std::string> vs;
            vs.push_back(baidu::common::NumToString(i + 1));
            vs.push_back(infos[i].job_id);
            vs.push_back(infos[i].job_name);
            vs.push_back(infos[i].state);
            vs.push_back(baidu::common::NumToString(infos[i].running_num) + "/" +
                         baidu::common::NumToString(infos[i].pending_num) + "/" +
                         baidu::common::NumToString(infos[i].deploying_num));
            vs.push_back(baidu::common::NumToString(infos[i].replica));
						vs.push_back(infos[i].is_batch ? "batch" : "");
            vs.push_back(baidu::common::NumToString(infos[i].cpu_used));
            vs.push_back(baidu::common::NumToString(infos[i].mem_used));
            tp.AddRow(vs);
        }
        printf("%s\n", tp.ToString().c_str());
        return 0;
    }
    fprintf(stderr, "List fail\n");
    return 1;
}

int KillJob(int argc, char* argv[]) {
    if (argc < 1) {
        return 1;
    }
    baidu::galaxy::Galaxy* galaxy = baidu::galaxy::Galaxy::ConnectGalaxy(FLAGS_master_host + ":" + FLAGS_master_port);
    std::string jobid(argv[0]);
    if (jobid.empty()) {
        return 1;
    }
    if (galaxy->TerminateJob(jobid)) {
        printf("terminate job %s successfully\n", argv[0]);
        return 0;
    }
    return 1;
}

int main(int argc, char* argv[]) {
    FLAGS_flagfile = "./galaxy.flag";
    ::google::SetUsageMessage(kGalaxyUsage);
    ::google::ParseCommandLineFlags(&argc, &argv, true);
    if(argc < 2){
        fprintf(stderr,"Usage:%s\n", kGalaxyUsage.c_str());
        return -1;
    }
    if (strcmp(argv[1], "submit") == 0) {
        return AddJob(argc - 2, argv + 2);
    } else if (strcmp(argv[1], "list") == 0) {
        return ListJob(argc - 2, argv + 2);
    } else if (strcmp(argv[1], "listagent") == 0) {
		return ListAgent(argc - 2, argv + 2);
    } else if (strcmp(argv[1], "label") == 0) {
        return LabelAgent(argc - 2, argv + 2);
    } else if (strcmp(argv[1], "update") == 0) {
        return UpdateJob(argc - 2, argv + 2);
    }else if (strcmp(argv[1], "kill")== 0){
        return KillJob(argc - 2 , argv + 2);
    } else {
        fprintf(stderr,"Usage:%s\n", kGalaxyUsage.c_str());
        return -1;
    }
    return 0;
}

/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */
<|endoftext|>


<commit_before>#include "stdafx.h"
#include "GameData.h"
#include "OS.h"
#include "lodepng.h"

void GameData::Save() {
	static string saveDir = OS::SaveDirectory() + "Destructo_Saves/";
	//Save this class
	//Create relevant directories
	OS::BuildPath(saveDir);

	//Serialize this class (always allow the setting file to be hand editable)
	vector<unsigned char> saveData = Savable::Serialize(this,false);
	//Save the file
	lodepng::save_file(saveData,saveDir + "settings.json");
}
void GameData::Load() {
	static string saveDir = OS::SaveDirectory() + "Destructo_Saves/";
	//Load this class
	vector<unsigned char> saveData;

	//If there's no settings file the defaults will be used instead
	if (saveData.size() <= 0)
		return;

	lodepng::load_file(saveData,saveDir + "settings.json");
	//Deserialize this class (always allow the setting file to be hand editable)
	Savable::Deserialize(saveData,this,false);
}<commit_msg>Deleted a file I meant to remove several commits ago<commit_after><|endoftext|>


<commit_before>#include "ResultDialog.h"
#include "ui_ResultDialog.h"
#include "StAssert.h"

#include <QDesktopWidget>

ResultDialog::ResultDialog (QWidget *parent) :
  QDialog (parent),
  ui (new Ui::ResultDialog),
  isShowAtCapturePos_ (true) {
  ui->setupUi (this);
  setWindowFlags (Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint |
                  Qt::WindowStaysOnTopHint);

  installEventFilter (this);
}

ResultDialog::~ResultDialog () {
  delete ui;
}

bool ResultDialog::eventFilter (QObject *object, QEvent *event) {
  Q_UNUSED (object);
  if (event->type () == QEvent::MouseButtonRelease) {
    hide ();
  }
  return QDialog::eventFilter (object, event);
}

void ResultDialog::showResult (ProcessingItem item) {
  ST_ASSERT (!item.source.isNull ());
  ST_ASSERT (!item.recognized.isEmpty ());
  ST_ASSERT (!item.translated.isEmpty ());
  ST_ASSERT (!item.screenPos.isNull ());

  ui->sourceLabel->setPixmap (item.source);
  ui->recognizeLabel->setText (item.recognized);
  ui->translateLabel->setText (item.translated);

  show ();
  adjustSize ();

  QDesktopWidget *desktop = QApplication::desktop ();
  Q_CHECK_PTR (desktop);
  if (isShowAtCapturePos_) {
    QPoint correction = QPoint (ui->frame->lineWidth (), ui->frame->lineWidth ());
    move (item.screenPos - correction);
    QRect screenRect = desktop->screenGeometry (this);
    int minY = screenRect.bottom () - height ();
    if (y () > minY) {
      move (x (), minY);
    }
  }
  else {

    QRect screenRect = desktop->availableGeometry (this);
    ST_ASSERT (screenRect.isValid ());
    QPoint newPos (screenRect.width () - width (), screenRect.height () - height ());
    move (newPos);
  }
}
<commit_msg>Code simplify.<commit_after>#include "ResultDialog.h"
#include "ui_ResultDialog.h"
#include "StAssert.h"

#include <QDesktopWidget>

ResultDialog::ResultDialog (QWidget *parent) :
  QDialog (parent),
  ui (new Ui::ResultDialog),
  isShowAtCapturePos_ (true) {
  ui->setupUi (this);
  setWindowFlags (Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint |
                  Qt::WindowStaysOnTopHint);

  installEventFilter (this);
}

ResultDialog::~ResultDialog () {
  delete ui;
}

bool ResultDialog::eventFilter (QObject *object, QEvent *event) {
  Q_UNUSED (object);
  if (event->type () == QEvent::MouseButtonRelease) {
    hide ();
  }
  return QDialog::eventFilter (object, event);
}

void ResultDialog::showResult (ProcessingItem item) {
  ST_ASSERT (item.isValid ());
  ui->sourceLabel->setPixmap (item.source);
  ui->recognizeLabel->setText (item.recognized);
  ui->translateLabel->setText (item.translated);

  show ();
  adjustSize ();

  QDesktopWidget *desktop = QApplication::desktop ();
  Q_CHECK_PTR (desktop);
  if (isShowAtCapturePos_) {
    QPoint correction = QPoint (ui->frame->lineWidth (), ui->frame->lineWidth ());
    move (item.screenPos - correction);
    QRect screenRect = desktop->screenGeometry (this);
    int minY = screenRect.bottom () - height ();
    if (y () > minY) {
      move (x (), minY);
    }
  }
  else {

    QRect screenRect = desktop->availableGeometry (this);
    ST_ASSERT (screenRect.isValid ());
    QPoint newPos (screenRect.width () - width (), screenRect.height () - height ());
    move (newPos);
  }
}
<|endoftext|>


<commit_before>/*

Copyright (c) 2015, Project OSRM contributors
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

// based on
// https://svn.apache.org/repos/asf/mesos/tags/release-0.9.0-incubating-RC0/src/common/json.hpp

#ifndef JSON_V8_RENDERER_HPP
#define JSON_V8_RENDERER_HPP

#include <osrm/json_container.hpp>

// v8
#include <nan.h>

#include <functional>

namespace node_osrm
{

struct V8Renderer : mapbox::util::static_visitor<>
{
    explicit V8Renderer(v8::Local<v8::Value> &_out) : out(_out) {}

    void operator()(const osrm::json::String &string) const
    {
        out = Nan::New(std::cref(string.value)).ToLocalChecked();
    }

    void operator()(const osrm::json::Number &number) const { out = Nan::New(number.value); }

    void operator()(const osrm::json::Object &object) const
    {
        v8::Local<v8::Object> obj = Nan::New<v8::Object>();
        for (const auto &keyValue : object.values)
        {
            v8::Local<v8::Value> child;
            mapbox::util::apply_visitor(V8Renderer(child), keyValue.second);
            obj->Set(Nan::New(keyValue.first).ToLocalChecked(), child);
        }
        out = obj;
    }

    void operator()(const osrm::json::Array &array) const
    {
        v8::Local<v8::Array> a = Nan::New<v8::Array>(array.values.size());
        for (auto i = 0u; i < array.values.size(); ++i)
        {
            v8::Local<v8::Value> child;
            mapbox::util::apply_visitor(V8Renderer(child), array.values[i]);
            a->Set(i, child);
        }
        out = a;
    }

    void operator()(const osrm::json::True &) const { out = Nan::New(true); }

    void operator()(const osrm::json::False &) const { out = Nan::New(false); }

    void operator()(const osrm::json::Null &) const { out = Nan::Null(); }

  private:
    v8::Local<v8::Value> &out;
};

inline void renderToV8(v8::Local<v8::Value> &out, const osrm::json::Object &object)
{
    // FIXME this should be a cast?
    osrm::json::Value value = object;
    mapbox::util::apply_visitor(V8Renderer(out), value);
}
}

#endif // JSON_V8_RENDERER_HPP
<commit_msg>Don't use static_visitor anymore<commit_after>/*

Copyright (c) 2015, Project OSRM contributors
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

// based on
// https://svn.apache.org/repos/asf/mesos/tags/release-0.9.0-incubating-RC0/src/common/json.hpp

#ifndef JSON_V8_RENDERER_HPP
#define JSON_V8_RENDERER_HPP

#include <osrm/json_container.hpp>

// v8
#include <nan.h>

#include <functional>

namespace node_osrm
{

struct V8Renderer
{
    explicit V8Renderer(v8::Local<v8::Value> &_out) : out(_out) {}

    void operator()(const osrm::json::String &string) const
    {
        out = Nan::New(std::cref(string.value)).ToLocalChecked();
    }

    void operator()(const osrm::json::Number &number) const { out = Nan::New(number.value); }

    void operator()(const osrm::json::Object &object) const
    {
        v8::Local<v8::Object> obj = Nan::New<v8::Object>();
        for (const auto &keyValue : object.values)
        {
            v8::Local<v8::Value> child;
            mapbox::util::apply_visitor(V8Renderer(child), keyValue.second);
            obj->Set(Nan::New(keyValue.first).ToLocalChecked(), child);
        }
        out = obj;
    }

    void operator()(const osrm::json::Array &array) const
    {
        v8::Local<v8::Array> a = Nan::New<v8::Array>(array.values.size());
        for (auto i = 0u; i < array.values.size(); ++i)
        {
            v8::Local<v8::Value> child;
            mapbox::util::apply_visitor(V8Renderer(child), array.values[i]);
            a->Set(i, child);
        }
        out = a;
    }

    void operator()(const osrm::json::True &) const { out = Nan::New(true); }

    void operator()(const osrm::json::False &) const { out = Nan::New(false); }

    void operator()(const osrm::json::Null &) const { out = Nan::Null(); }

  private:
    v8::Local<v8::Value> &out;
};

inline void renderToV8(v8::Local<v8::Value> &out, const osrm::json::Object &object)
{
    // FIXME this should be a cast?
    osrm::json::Value value = object;
    mapbox::util::apply_visitor(V8Renderer(out), value);
}
}

#endif // JSON_V8_RENDERER_HPP
<|endoftext|>


<commit_before>#include "stdafx.h"
#include "test_regex.h"

//     typedef basic_regex<char> regex;
//     typedef basic_regex<wchar_t> wregex;
//     typedef match_results<const char *> cmatch;
//     typedef match_results<const wchar_t *> wcmatch;
//     typedef match_results<string::const_iterator> smatch;
//     typedef match_results<wstring::const_iterator> wsmatch;
//     typedef sub_match<const char *> csub_match;
//     typedef sub_match<const wchar_t *> wcsub_match;
//     typedef sub_match<string::const_iterator> ssub_match;
//     typedef sub_match<wstring::const_iterator> wssub_match;
//     typedef regex_token_iterator<const char *> cregex_token_iterator;
//     typedef regex_token_iterator<const wchar_t *> wcregex_token_iterator;
//     typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
//     typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;

bool is_email_valid(const std::string& email)
{
    const std::regex pattern("(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+");
    return std::regex_match(email, pattern);
}

void show_ip_parts(const std::string& ip)
{
    ///> ͨ()ִ
    const std::regex pattern("(\\d{1,2}|1\\d{2}|20\\d|21\\d|22\\d|23\\d|24\\d|250|251|252|253|254|255)\\.(\\d{1,2}|1\\d{2}|20\\d|21\\d|22\\d|23\\d|24\\d|250|251|252|253|254|255)\\.(\\d{1,2}|1\\d{2}|20\\d|21\\d|22\\d|23\\d|24\\d|250|251|252|253|254|255)\\.(\\d{1,2}|1\\d{2}|20\\d|21\\d|22\\d|23\\d|24\\d|250|251|252|253|254|255)");
    std:: match_results<std::string::const_iterator> result;
    bool valid = std:: regex_match(ip, result, pattern);
    if(valid)
    {
        std::cout << "b1: " << result[1] << std::endl;
        std::cout << "b2: " << result[2] << std::endl;
        std::cout << "b3: " << result[3] << std::endl;
        std::cout << "b4: " << result[4] << std::endl;
    }
}

void test_regex_match()
{
    std::string email1 = "marius.bancila@domain.com";
    std::string email2 = "mariusbancila@domain.com";
    std::string email3 = "marius_b@domain.co.uk";
    std::string email4 = "marius@domain";
    assert(is_email_valid(email1));
    assert(is_email_valid(email2));
    assert(is_email_valid(email3));
    assert(is_email_valid(email4) == false);

    show_ip_parts("1.22.33.255");
    show_ip_parts("1.22.33.256");
    show_ip_parts("100.200");
}

void test_regex_search()
{
    ///> ֻܵõһ
    const std::regex pattern("(\\w+day)");
    std::string weekend = "Saturday and Sunday";
    std::smatch result;
    bool match = std::regex_search(weekend, result, pattern);
    if(match)
    {
        for(size_t i = 1; i < result.size(); ++i)
        {
            std::cout << result[i] << std::endl;
        }
    }
    std::cout<<std::endl;
}

void test_regex_iterator()
{
    const std::regex pattern("\\w+day");
    std::string weekend = "Saturday and Sunday, but some Fridays also.";
    const std::sregex_token_iterator end;
    for (std::sregex_token_iterator i(weekend.begin(),weekend.end(), pattern); i != end; ++i)
    {
        std::cout << i->str() << std::endl;
    }
    std::cout<<std::endl;
}

std::string format_date(const std::string& date)
{
    const std::regex pattern("(\\d{1,2})(\\.|-|/)(\\d{1,2})(\\.|-|/)(\\d{4})");
    std::string replacer = "$5$4$3$2$1";
    return std::regex_replace(date, pattern, replacer);
}

void test_regex_replace()
{
    std::string text = "This is a element and this a unique ID.";
    const std::regex pattern("(\\ba (a|e|i|u|o))+");
    std::string replace = "an $2";
    std::string newtext = std::regex_replace(text, pattern, replace);
    std::cout << newtext << std::endl;
    std::cout << std::endl;

    std::string date1 = "1/2/2008";
    std::string date2 = "12.08.2008";
    std::cout << date1 << " -> " << format_date(date1) << std::endl;
    std::cout << date2 << " -> " << format_date(date2) << std::endl;
    std::cout << std::endl;
}

void test_regex()
{
    test_regex_match();
    test_regex_search();
    test_regex_iterator();
    test_regex_replace();
}
<commit_msg>* learn regex 加一个QQ邮箱的测试<commit_after>#include "stdafx.h"
#include "test_regex.h"

//     typedef basic_regex<char> regex;
//     typedef basic_regex<wchar_t> wregex;
//     typedef match_results<const char *> cmatch;
//     typedef match_results<const wchar_t *> wcmatch;
//     typedef match_results<string::const_iterator> smatch;
//     typedef match_results<wstring::const_iterator> wsmatch;
//     typedef sub_match<const char *> csub_match;
//     typedef sub_match<const wchar_t *> wcsub_match;
//     typedef sub_match<string::const_iterator> ssub_match;
//     typedef sub_match<wstring::const_iterator> wssub_match;
//     typedef regex_token_iterator<const char *> cregex_token_iterator;
//     typedef regex_token_iterator<const wchar_t *> wcregex_token_iterator;
//     typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
//     typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;

bool is_email_valid(const std::string& email)
{
    const std::regex pattern("(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+");
    return std::regex_match(email, pattern);
}

void show_ip_parts(const std::string& ip)
{
    ///> ͨ()ִ
    const std::regex pattern("(\\d{1,2}|1\\d{2}|20\\d|21\\d|22\\d|23\\d|24\\d|250|251|252|253|254|255)\\.(\\d{1,2}|1\\d{2}|20\\d|21\\d|22\\d|23\\d|24\\d|250|251|252|253|254|255)\\.(\\d{1,2}|1\\d{2}|20\\d|21\\d|22\\d|23\\d|24\\d|250|251|252|253|254|255)\\.(\\d{1,2}|1\\d{2}|20\\d|21\\d|22\\d|23\\d|24\\d|250|251|252|253|254|255)");
    std:: match_results<std::string::const_iterator> result;
    bool valid = std:: regex_match(ip, result, pattern);
    if(valid)
    {
        std::cout << "b1: " << result[1] << std::endl;
        std::cout << "b2: " << result[2] << std::endl;
        std::cout << "b3: " << result[3] << std::endl;
        std::cout << "b4: " << result[4] << std::endl;
    }
}

void test_regex_match()
{
    std::string email1 = "marius.bancila@domain.com";
    std::string email2 = "mariusbancila@domain.com";
    std::string email3 = "marius_b@domain.co.uk";
    std::string email4 = "marius@domain";
    std::string email5 = "12345@qq.com";
    assert(is_email_valid(email1));
    assert(is_email_valid(email2));
    assert(is_email_valid(email3));
    assert(is_email_valid(email4) == false);
    assert(is_email_valid(email5));

    show_ip_parts("1.22.33.255");
    show_ip_parts("1.22.33.256");
    show_ip_parts("100.200");
}

void test_regex_search()
{
    ///> ֻܵõһ
    const std::regex pattern("(\\w+day)");
    std::string weekend = "Saturday and Sunday";
    std::smatch result;
    bool match = std::regex_search(weekend, result, pattern);
    if(match)
    {
        for(size_t i = 1; i < result.size(); ++i)
        {
            std::cout << result[i] << std::endl;
        }
    }
    std::cout<<std::endl;
}

void test_regex_iterator()
{
    const std::regex pattern("\\w+day");
    std::string weekend = "Saturday and Sunday, but some Fridays also.";
    const std::sregex_token_iterator end;
    for (std::sregex_token_iterator i(weekend.begin(),weekend.end(), pattern); i != end; ++i)
    {
        std::cout << i->str() << std::endl;
    }
    std::cout<<std::endl;
}

std::string format_date(const std::string& date)
{
    const std::regex pattern("(\\d{1,2})(\\.|-|/)(\\d{1,2})(\\.|-|/)(\\d{4})");
    std::string replacer = "$5$4$3$2$1";
    return std::regex_replace(date, pattern, replacer);
}

void test_regex_replace()
{
    std::string text = "This is a element and this a unique ID.";
    const std::regex pattern("(\\ba (a|e|i|u|o))+");
    std::string replace = "an $2";
    std::string newtext = std::regex_replace(text, pattern, replace);
    std::cout << newtext << std::endl;
    std::cout << std::endl;

    std::string date1 = "1/2/2008";
    std::string date2 = "12.08.2008";
    std::cout << date1 << " -> " << format_date(date1) << std::endl;
    std::cout << date2 << " -> " << format_date(date2) << std::endl;
    std::cout << std::endl;
}

void test_regex()
{
    test_regex_match();
    test_regex_search();
    test_regex_iterator();
    test_regex_replace();
}
<|endoftext|>


<commit_before>/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Written (W) 2012 Heiko Strathmann
 */

#include <shogun/statistics/LinearTimeMMD.h>
#include <shogun/features/Features.h>
#include <shogun/mathematics/Statistics.h>
#include <shogun/features/CombinedFeatures.h>
#include <shogun/kernel/CombinedKernel.h>

#include <shogun/lib/external/libqp.h>

using namespace shogun;

CLinearTimeMMD::CLinearTimeMMD() :
		CKernelTwoSampleTestStatistic()
{
	init();
}

CLinearTimeMMD::CLinearTimeMMD(CKernel* kernel, CFeatures* p_and_q,
		index_t q_start) :
		CKernelTwoSampleTestStatistic(kernel, p_and_q, q_start)
{
	init();

	if (p_and_q && q_start!=p_and_q->get_num_vectors()/2)
	{
		SG_ERROR("CLinearTimeMMD: Only features with equal number of vectors "
				"are currently possible\n");
	}
}

CLinearTimeMMD::CLinearTimeMMD(CKernel* kernel, CFeatures* p, CFeatures* q) :
		CKernelTwoSampleTestStatistic(kernel, p, q)
{
	init();

	if (p->get_num_vectors()!=q->get_num_vectors())
	{
		SG_ERROR("CLinearTimeMMD: Only features with equal number of vectors "
				"are currently possible\n");
	}
}

CLinearTimeMMD::~CLinearTimeMMD()
{

}

void CLinearTimeMMD::init()
{
	SG_ADD(&m_opt_max_iterations, "opt_max_iterations", "Maximum number of "
			"iterations for qp solver", MS_NOT_AVAILABLE);
	SG_ADD(&m_opt_epsilon, "opt_epsilon", "Stopping criterion for qp solver",
			MS_NOT_AVAILABLE);
	SG_ADD(&m_opt_low_cut, "opt_low_cut", "Low cut value for optimization "
			"kernel weights", MS_NOT_AVAILABLE);
	SG_ADD(&m_opt_regularization_eps, "opt_regularization_eps", "Regularization"
			" value that is added to diagonal of Q matrix", MS_NOT_AVAILABLE);

	m_opt_max_iterations=10000;
	m_opt_epsilon=10E-15;
	m_opt_low_cut=10E-7;
	m_opt_regularization_eps=10E-5;
}

float64_t CLinearTimeMMD::compute_statistic()
{
	SG_DEBUG("entering CLinearTimeMMD::compute_statistic()\n");

	if (!m_kernel)
		SG_ERROR("%s::compute_statistic(): No kernel specified!\n", get_name());

	/* TODO features with a different number of vectors should be allowed */

	/* m is number of samples from each distribution, m_2 is half of it
	 * using names from JLMR paper (see class documentation) */
	index_t m=m_q_start;
	index_t m_2=m/2;

	SG_DEBUG("m_q_start=%d\n", m_q_start);

	/* compute traces of kernel matrices for linear MMD */
	m_kernel->init(m_p_and_q, m_p_and_q);

	float64_t pp=0;
	float64_t qq=0;
	float64_t pq=0;
	float64_t qp=0;

	/* compute traces */
	for (index_t i=0; i<m_2; ++i)
	{
		pp+=m_kernel->kernel(i, m_2+i);
		qq+=m_kernel->kernel(m+i, m+m_2+i);
		pq+=m_kernel->kernel(i, m+m_2+i);
		qp+=m_kernel->kernel(m_2+i, m+i);
	}

	SG_DEBUG("returning: 1/%d*(%f+%f-%f-%f)\n", m_2, pp, qq, pq, qp);

	/* mean of sum all traces is linear time mmd */
	SG_DEBUG("leaving CLinearTimeMMD::compute_statistic()\n");
	return 1.0/m_2*(pp+qq-pq-qp);
}

float64_t CLinearTimeMMD::compute_p_value(float64_t statistic)
{
	float64_t result=0;

	switch (m_null_approximation_method)
	{
	case MMD1_GAUSSIAN:
		if (m_p_and_q->get_num_vectors()<10000)
		{
			SG_WARNING("CLinearTimeMMD::compute_p_value: The number of samples"
					" should be very large (at least 10000)  in order to get a"
					" good Gaussian approximation using MMD1_GAUSSIAN.\n");
		}

		{
			/* compute variance and use to estimate Gaussian distribution */
			float64_t std_dev=CMath::sqrt(compute_variance_estimate());
			result=1.0-CStatistics::normal_cdf(statistic, std_dev);
		}
		break;
	default:
		result=CKernelTwoSampleTestStatistic::compute_p_value(statistic);
		break;
	}

	return result;
}

float64_t CLinearTimeMMD::compute_threshold(float64_t alpha)
{
	SG_ERROR("%s::compute_threshold is not yet implemented!\n");
	return 0;
}

float64_t CLinearTimeMMD::compute_variance_estimate()
{
	/* this corresponds to computing the statistic itself, however, the
	 * difference is that all terms (of the traces) have to be stored */
	index_t m=m_q_start;
	index_t m_2=m/2;

	m_kernel->init(m_p_and_q, m_p_and_q);

	/* allocate memory for traces */
	SGVector<float64_t> traces(m_2);

	/* sum up diagonals of all kernel matrices */
	for (index_t i=0; i<m_2; ++i)
	{
		/* init for code beauty :) */
		traces[i]=0;

		/* p and p */
		traces[i]+=m_kernel->kernel(i, m_2+i);

		/* q and q */
		traces[i]+=m_kernel->kernel(m+i, m+m_2+i);

		/* p and q */
		traces[i]-=m_kernel->kernel(i, m+m_2+i);

		/* q and p */
		traces[i]-=m_kernel->kernel(m_2+i, m+i);
	}

	/* return linear time variance estimate */
	return CStatistics::variance(traces)/m_2;
}

#ifdef HAVE_LAPACK
void CLinearTimeMMD::optimize_kernel_weights()
{
	if (m_kernel->get_kernel_type()!=K_COMBINED)
	{
		SG_ERROR("CLinearTimeMMD::optimize_kernel_weights(): Only possible "
				"with a combined kernel!\n");
	}

	if (m_p_and_q->get_feature_class()!=C_COMBINED)
	{
		SG_ERROR("CLinearTimeMMD::optimize_kernel_weights(): Only possible "
				"with combined features!\n");
	}

	/* TODO think about casting and types here */
	CCombinedFeatures* combined_p_and_q=
			dynamic_cast<CCombinedFeatures*>(m_p_and_q);
	CCombinedKernel* combined_kernel=dynamic_cast<CCombinedKernel*>(m_kernel);
	ASSERT(combined_p_and_q);
	ASSERT(combined_kernel);

	if (combined_kernel->get_num_subkernels()!=
			combined_p_and_q->get_num_feature_obj())
	{
		SG_ERROR("CLinearTimeMMD::optimize_kernel_weights(): Only possible "
				"when number of sub-kernels (%d) equal number of sub-features "
				"(%d)\n", combined_kernel->get_num_subkernels(),
				combined_p_and_q->get_num_feature_obj());
	}

	/* init kernel with features */
	m_kernel->init(m_p_and_q, m_p_and_q);

	/* number of kernels and data */
	index_t num_kernels=combined_kernel->get_num_subkernels();
	index_t m2=m_q_start/2;

	/* matrix with all h entries for all kernels and data */
	SGMatrix<float64_t> hs(m2, num_kernels);

	/* mmds are needed and are means of columns of hs */
	SGVector<float64_t> mmds(num_kernels);

	float64_t pp;
	float64_t qq;
	float64_t pq;
	float64_t qp;
	/* compute all h entries */
	for (index_t i=0; i<num_kernels; ++i)
	{
		CKernel* current=combined_kernel->get_kernel(i);
		mmds[i]=0;
		for (index_t j=0; j<m2; ++j)
		{
			pp=current->kernel(j, m2+j);
			qq=current->kernel(m_q_start+j, m_q_start+m2+j);
			pq=current->kernel(j, m_q_start+m2+j);
			qp=current->kernel(m2+j, m_q_start+j);
			hs(j, i)=pp+qq-pq-qp;
			mmds[i]+=hs(j, i);
		}

		/* mmd is simply mean. This is the unbiased linear time estimate */
		mmds[i]/=m2;

		SG_UNREF(current);
	}

	/* compute covariance matrix of h vector, in place is safe now since h
	 * is not needed anymore */
	m_Q=CStatistics::covariance_matrix(hs, true);

	/* evtl regularize to avoid numerical problems (ratio of MMD and std-dev
	 * blows up when variance is small */
	if (m_opt_regularization_eps)
	{
		SG_DEBUG("regularizing matrix Q by adding %f to diagonal\n",
				m_opt_regularization_eps);
		for (index_t i=0; i<num_kernels; ++i)
			m_Q(i,i)+=m_opt_regularization_eps;
	}

	if (sg_io->get_loglevel()==MSG_DEBUG)
	{
		m_Q.display_matrix("(evtl. regularized) Q");
		mmds.display_vector("mmds");
	}

	/* compute sum of mmds to generate feasible point for convex program */
	float64_t sum_mmds=0;
	for (index_t i=0; i<mmds.vlen; ++i)
		sum_mmds+=mmds[i];

	/* QP: 0.5*x'*Q*x + f'*x
	 * subject to
	 * mmds'*x = b
	 * LB[i] <= x[i] <= UB[i]   for all i=1..n */
	SGVector<float64_t> Q_diag(num_kernels);
	SGVector<float64_t> f(num_kernels);
	SGVector<float64_t> lb(num_kernels);
	SGVector<float64_t> ub(num_kernels);
	SGVector<float64_t> x(num_kernels);

	/* init everything, there are two cases possible: i) at least one mmd is
	 * is positive, ii) all mmds are negative */
	bool one_pos;
	for (index_t i=0; i<mmds.vlen; ++i)
	{
		if (mmds[i]>0)
		{
			SG_DEBUG("found at least one positive MMD\n");
			one_pos=true;
			break;
		}
		one_pos=false;
	}

	if (!one_pos)
	{
		SG_WARNING("All mmd estimates are negative. This is techical possible,"
				" although extremely rare. Current problem might bad\n");

		/* if no element is positive, Q has to be replaced by -Q */
		for (index_t i=0; i<num_kernels*num_kernels; ++i)
			m_Q.matrix[i]*=-1;
	}

	/* init vectors */
	for (index_t i=0; i<num_kernels; ++i)
	{
		Q_diag[i]=m_Q(i,i);
		f[i]=0;
		lb[i]=0;
		ub[i]=CMath::INFTY;

		/* initial point has to be feasible, i.e. mmds'*x = b */
		x[i]=1.0/sum_mmds;
	}

	/* start libqp solver with desired parameters */
	SG_DEBUG("starting libqp\n");
	libqp_state_T qp_exitflag=libqp_gsmo_solver(&get_Q_col, Q_diag.vector,
			f.vector, mmds.vector,
			one_pos ? 1 : -1,
			lb.vector, ub.vector,
			x.vector, num_kernels, m_opt_max_iterations,
			m_opt_regularization_eps, &print_state);

	SG_DEBUG("libqp returns: nIts=%d, exit_flag: %d\n", qp_exitflag.nIter,
			qp_exitflag.exitflag);

	/* set really small entries to zero and sum up for normalization */
	float64_t sum_weights=0;
	for (index_t i=0; i<x.vlen; ++i)
	{
		if (x[i]<m_opt_low_cut)
		{
			SG_DEBUG("lowcut: weight[%i]=%f<%f; setting to zero\n", i, x[i],
					m_opt_low_cut);
			x[i]=0;
		}

		sum_weights+=x[i];
	}

	/* normalize (allowed since problem is scale invariant) */
	for (index_t i=0; i<x.vlen; ++i)
		x[i]/=sum_weights;

	/* set weights to kernel */
	m_kernel->set_subkernel_weights(x);
}

SGMatrix<float64_t> CLinearTimeMMD::m_Q=SGMatrix<float64_t>();

const float64_t* CLinearTimeMMD::get_Q_col(uint32_t i)
{
	return &m_Q[m_Q.num_rows*i];
}

void CLinearTimeMMD::print_state(libqp_state_T state)
{
	SG_SDEBUG("libqp state: primal=%f\n", state.QP);
}

#endif //HAVE_LAPACK

<commit_msg>minor cleanups and removed inition regularization<commit_after>/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Written (W) 2012 Heiko Strathmann
 */

#include <shogun/statistics/LinearTimeMMD.h>
#include <shogun/features/Features.h>
#include <shogun/mathematics/Statistics.h>
#include <shogun/features/CombinedFeatures.h>
#include <shogun/kernel/CombinedKernel.h>

#include <shogun/lib/external/libqp.h>

using namespace shogun;

CLinearTimeMMD::CLinearTimeMMD() :
		CKernelTwoSampleTestStatistic()
{
	init();
}

CLinearTimeMMD::CLinearTimeMMD(CKernel* kernel, CFeatures* p_and_q,
		index_t q_start) :
		CKernelTwoSampleTestStatistic(kernel, p_and_q, q_start)
{
	init();

	if (p_and_q && q_start!=p_and_q->get_num_vectors()/2)
	{
		SG_ERROR("CLinearTimeMMD: Only features with equal number of vectors "
				"are currently possible\n");
	}
}

CLinearTimeMMD::CLinearTimeMMD(CKernel* kernel, CFeatures* p, CFeatures* q) :
		CKernelTwoSampleTestStatistic(kernel, p, q)
{
	init();

	if (p->get_num_vectors()!=q->get_num_vectors())
	{
		SG_ERROR("CLinearTimeMMD: Only features with equal number of vectors "
				"are currently possible\n");
	}
}

CLinearTimeMMD::~CLinearTimeMMD()
{

}

void CLinearTimeMMD::init()
{
	SG_ADD(&m_opt_max_iterations, "opt_max_iterations", "Maximum number of "
			"iterations for qp solver", MS_NOT_AVAILABLE);
	SG_ADD(&m_opt_epsilon, "opt_epsilon", "Stopping criterion for qp solver",
			MS_NOT_AVAILABLE);
	SG_ADD(&m_opt_low_cut, "opt_low_cut", "Low cut value for optimization "
			"kernel weights", MS_NOT_AVAILABLE);
	SG_ADD(&m_opt_regularization_eps, "opt_regularization_eps", "Regularization"
			" value that is added to diagonal of Q matrix", MS_NOT_AVAILABLE);

	m_opt_max_iterations=10000;
	m_opt_epsilon=10E-15;
	m_opt_low_cut=10E-7;
	m_opt_regularization_eps=0;
}

float64_t CLinearTimeMMD::compute_statistic()
{
	SG_DEBUG("entering CLinearTimeMMD::compute_statistic()\n");

	if (!m_kernel)
		SG_ERROR("%s::compute_statistic(): No kernel specified!\n", get_name());

	/* TODO features with a different number of vectors should be allowed */

	/* m is number of samples from each distribution, m_2 is half of it
	 * using names from JLMR paper (see class documentation) */
	index_t m=m_q_start;
	index_t m_2=m/2;

	SG_DEBUG("m_q_start=%d\n", m_q_start);

	/* compute traces of kernel matrices for linear MMD */
	m_kernel->init(m_p_and_q, m_p_and_q);

	float64_t pp=0;
	float64_t qq=0;
	float64_t pq=0;
	float64_t qp=0;

	/* compute traces */
	for (index_t i=0; i<m_2; ++i)
	{
		pp+=m_kernel->kernel(i, m_2+i);
		qq+=m_kernel->kernel(m+i, m+m_2+i);
		pq+=m_kernel->kernel(i, m+m_2+i);
		qp+=m_kernel->kernel(m_2+i, m+i);
	}

	SG_DEBUG("returning: 1/%d*(%f+%f-%f-%f)\n", m_2, pp, qq, pq, qp);

	/* mean of sum all traces is linear time mmd */
	SG_DEBUG("leaving CLinearTimeMMD::compute_statistic()\n");
	return 1.0/m_2*(pp+qq-pq-qp);
}

float64_t CLinearTimeMMD::compute_p_value(float64_t statistic)
{
	float64_t result=0;

	switch (m_null_approximation_method)
	{
	case MMD1_GAUSSIAN:
		if (m_p_and_q->get_num_vectors()<10000)
		{
			SG_WARNING("CLinearTimeMMD::compute_p_value: The number of samples"
					" should be very large (at least 10000)  in order to get a"
					" good Gaussian approximation using MMD1_GAUSSIAN.\n");
		}

		{
			/* compute variance and use to estimate Gaussian distribution */
			float64_t std_dev=CMath::sqrt(compute_variance_estimate());
			result=1.0-CStatistics::normal_cdf(statistic, std_dev);
		}
		break;
	default:
		result=CKernelTwoSampleTestStatistic::compute_p_value(statistic);
		break;
	}

	return result;
}

float64_t CLinearTimeMMD::compute_threshold(float64_t alpha)
{
	SG_ERROR("%s::compute_threshold is not yet implemented!\n");
	return 0;
}

float64_t CLinearTimeMMD::compute_variance_estimate()
{
	/* this corresponds to computing the statistic itself, however, the
	 * difference is that all terms (of the traces) have to be stored */
	index_t m=m_q_start;
	index_t m_2=m/2;

	m_kernel->init(m_p_and_q, m_p_and_q);

	/* allocate memory for traces */
	SGVector<float64_t> traces(m_2);

	/* sum up diagonals of all kernel matrices */
	for (index_t i=0; i<m_2; ++i)
	{
		/* init for code beauty :) */
		traces[i]=0;

		/* p and p */
		traces[i]+=m_kernel->kernel(i, m_2+i);

		/* q and q */
		traces[i]+=m_kernel->kernel(m+i, m+m_2+i);

		/* p and q */
		traces[i]-=m_kernel->kernel(i, m+m_2+i);

		/* q and p */
		traces[i]-=m_kernel->kernel(m_2+i, m+i);
	}

	/* return linear time variance estimate */
	return CStatistics::variance(traces)/m_2;
}

#ifdef HAVE_LAPACK
void CLinearTimeMMD::optimize_kernel_weights()
{
	/** TODO check whether other types of combined kernels/features might be
	 * allowed */
	if (m_kernel->get_kernel_type()!=K_COMBINED)
	{
		SG_ERROR("CLinearTimeMMD::optimize_kernel_weights(): Only possible "
				"with a combined kernel!\n");
	}

	if (m_p_and_q->get_feature_class()!=C_COMBINED)
	{
		SG_ERROR("CLinearTimeMMD::optimize_kernel_weights(): Only possible "
				"with combined features!\n");
	}

	/* think about casting and types of different kernels/features here */
	CCombinedFeatures* combined_p_and_q=
			dynamic_cast<CCombinedFeatures*>(m_p_and_q);
	CCombinedKernel* combined_kernel=dynamic_cast<CCombinedKernel*>(m_kernel);
	ASSERT(combined_p_and_q);
	ASSERT(combined_kernel);

	if (combined_kernel->get_num_subkernels()!=
			combined_p_and_q->get_num_feature_obj())
	{
		SG_ERROR("CLinearTimeMMD::optimize_kernel_weights(): Only possible "
				"when number of sub-kernels (%d) equal number of sub-features "
				"(%d)\n", combined_kernel->get_num_subkernels(),
				combined_p_and_q->get_num_feature_obj());
	}

	/* init kernel with features */
	m_kernel->init(m_p_and_q, m_p_and_q);

	/* number of kernels and data */
	index_t num_kernels=combined_kernel->get_num_subkernels();
	index_t m2=m_q_start/2;

	/* matrix with all h entries for all kernels and data */
	SGMatrix<float64_t> hs(m2, num_kernels);

	/* mmds are needed and are means of columns of hs */
	SGVector<float64_t> mmds(num_kernels);

	float64_t pp;
	float64_t qq;
	float64_t pq;
	float64_t qp;
	/* compute all h entries */
	for (index_t i=0; i<num_kernels; ++i)
	{
		CKernel* current=combined_kernel->get_kernel(i);
		mmds[i]=0;
		for (index_t j=0; j<m2; ++j)
		{
			pp=current->kernel(j, m2+j);
			qq=current->kernel(m_q_start+j, m_q_start+m2+j);
			pq=current->kernel(j, m_q_start+m2+j);
			qp=current->kernel(m2+j, m_q_start+j);
			hs(j, i)=pp+qq-pq-qp;
			mmds[i]+=hs(j, i);
		}

		/* mmd is simply mean. This is the unbiased linear time estimate */
		mmds[i]/=m2;

		SG_UNREF(current);
	}

	/* compute covariance matrix of h vector, in place is safe now since h
	 * is not needed anymore */
	m_Q=CStatistics::covariance_matrix(hs, true);

	/* evtl regularize to avoid numerical problems (ratio of MMD and std-dev
	 * blows up when variance is small */
	if (m_opt_regularization_eps)
	{
		SG_DEBUG("regularizing matrix Q by adding %f to diagonal\n",
				m_opt_regularization_eps);
		for (index_t i=0; i<num_kernels; ++i)
			m_Q(i,i)+=m_opt_regularization_eps;
	}

	if (sg_io->get_loglevel()==MSG_DEBUG)
	{
		m_Q.display_matrix("(evtl. regularized) Q");
		mmds.display_vector("mmds");
	}

	/* compute sum of mmds to generate feasible point for convex program */
	float64_t sum_mmds=0;
	for (index_t i=0; i<mmds.vlen; ++i)
		sum_mmds+=mmds[i];

	/* QP: 0.5*x'*Q*x + f'*x
	 * subject to
	 * mmds'*x = b
	 * LB[i] <= x[i] <= UB[i]   for all i=1..n */
	SGVector<float64_t> Q_diag(num_kernels);
	SGVector<float64_t> f(num_kernels);
	SGVector<float64_t> lb(num_kernels);
	SGVector<float64_t> ub(num_kernels);
	SGVector<float64_t> x(num_kernels);

	/* init everything, there are two cases possible: i) at least one mmd is
	 * is positive, ii) all mmds are negative */
	bool one_pos;
	for (index_t i=0; i<mmds.vlen; ++i)
	{
		if (mmds[i]>0)
		{
			SG_DEBUG("found at least one positive MMD\n");
			one_pos=true;
			break;
		}
		one_pos=false;
	}

	if (!one_pos)
	{
		SG_WARNING("All mmd estimates are negative. This is techical possible,"
				" although extremely rare. Current problem might bad\n");

		/* if no element is positive, Q has to be replaced by -Q */
		for (index_t i=0; i<num_kernels*num_kernels; ++i)
			m_Q.matrix[i]=-m_Q.matrix[i];
	}

	/* init vectors */
	for (index_t i=0; i<num_kernels; ++i)
	{
		Q_diag[i]=m_Q(i,i);
		f[i]=0;
		lb[i]=0;
		ub[i]=CMath::INFTY;

		/* initial point has to be feasible, i.e. mmds'*x = b */
		x[i]=1.0/sum_mmds;
	}

	/* start libqp solver with desired parameters */
	SG_DEBUG("starting libqp optimization\n");
	libqp_state_T qp_exitflag=libqp_gsmo_solver(&get_Q_col, Q_diag.vector,
			f.vector, mmds.vector,
			one_pos ? 1 : -1,
			lb.vector, ub.vector,
			x.vector, num_kernels, m_opt_max_iterations,
			m_opt_regularization_eps, &print_state);

	SG_DEBUG("libqp returns: nIts=%d, exit_flag: %d\n", qp_exitflag.nIter,
			qp_exitflag.exitflag);

	/* set really small entries to zero and sum up for normalization */
	float64_t sum_weights=0;
	for (index_t i=0; i<x.vlen; ++i)
	{
		if (x[i]<m_opt_low_cut)
		{
			SG_DEBUG("lowcut: weight[%i]=%f<%f; setting to zero\n", i, x[i],
					m_opt_low_cut);
			x[i]=0;
		}

		sum_weights+=x[i];
	}

	/* normalize (allowed since problem is scale invariant) */
	for (index_t i=0; i<x.vlen; ++i)
		x[i]/=sum_weights;

	/* set weights to kernel */
	m_kernel->set_subkernel_weights(x);
}

SGMatrix<float64_t> CLinearTimeMMD::m_Q=SGMatrix<float64_t>();

const float64_t* CLinearTimeMMD::get_Q_col(uint32_t i)
{
	return &m_Q[m_Q.num_rows*i];
}

void CLinearTimeMMD::print_state(libqp_state_T state)
{
	SG_SDEBUG("libqp state: primal=%f\n", state.QP);
}

#endif //HAVE_LAPACK

<|endoftext|>


<commit_before>#include "selectsignaldialog.h"
#include "ui_selectsignaldialog.h"

#include "signaldescription.h"
#include "signalhandler.h"
#include "jointnames.h"

#include <cstdio>
#include <cassert>

class SelectSignalDialog::Internal : public Ui::SelectSignalDialog
{
public:

  QList<QListWidget*> ArrayKeyWidgets;

};

SelectSignalDialog::SelectSignalDialog(QWidget* parent) : QDialog(parent)
{
  mInternal = new Internal;
  mInternal->setupUi(this);

  QStringList channels;
  channels << "ATLAS_STATE" << "ATLAS_STATE_EXTRA" << "ATLAS_STATUS" << "ATLAS_COMMAND" << "ATLAS_FOOT_POS_EST" << "TRUE_ROBOT_STATE" << "EST_ROBOT_STATE" << "VICON_ATLAS" << "SE_INS_POSE_STATE" << "SE_MATLAB_DATAFUSION_REQ";

  QStringList messageTypes = SignalHandlerFactory::instance().messageTypes();
  QStringList messageFields;

  mInternal->ChannelListBox->addItems(channels);
  mInternal->MessageTypeListBox->addItems(messageTypes);
  mInternal->MessageFieldListBox->addItems(messageFields);

  mInternal->ChannelListBox->setCurrentRow(0);
  mInternal->MessageTypeListBox->setCurrentRow(0);
  mInternal->MessageFieldListBox->setCurrentRow(0);

  this->connect(mInternal->MessageTypeListBox, SIGNAL(currentRowChanged(int)), SLOT(onMessageTypeChanged()));
  this->connect(mInternal->MessageFieldListBox, SIGNAL(currentRowChanged(int)), SLOT(onFieldNameChanged()));
  this->onMessageTypeChanged();
}

void SelectSignalDialog::onMessageTypeChanged()
{
  if (!mInternal->MessageTypeListBox->currentItem())
  {
    return;
  }

  QString messageType = mInternal->MessageTypeListBox->currentItem()->text();

  QStringList fieldNames = SignalHandlerFactory::instance().fieldNames(messageType);
  fieldNames.sort();

  mInternal->MessageFieldListBox->clear();
  mInternal->MessageFieldListBox->addItems(fieldNames);
  mInternal->MessageFieldListBox->setCurrentRow(0);
  this->onFieldNameChanged();
}

void SelectSignalDialog::onFieldNameChanged()
{
  if (!mInternal->MessageFieldListBox->currentItem())
  {
    return;
  }

  QString messageType = mInternal->MessageTypeListBox->currentItem()->text();
  QString fieldName = mInternal->MessageFieldListBox->currentItem()->text();

  foreach (QListWidget* listWidget, mInternal->ArrayKeyWidgets)
  {
    delete listWidget;
  }
  mInternal->ArrayKeyWidgets.clear();

  const QList<QList<QString> >& validArrayKeys = SignalHandlerFactory::instance().validArrayKeys(messageType, fieldName);

  bool useArrayKeys = !validArrayKeys.empty();
  mInternal->ArrayKeysLabel->setVisible(useArrayKeys);
  mInternal->ArrayKeysContainer->setVisible(useArrayKeys);

  foreach (const QList<QString> & keys, validArrayKeys)
  {
    assert(keys.size());
    QListWidget* listWidget = new QListWidget;
    listWidget->addItems(keys);
    listWidget->setCurrentRow(0);
    mInternal->ArrayKeyWidgets.append(listWidget);
    mInternal->ArrayKeysContainer->layout()->addWidget(listWidget);
  }
}

SelectSignalDialog::~SelectSignalDialog()
{
  delete mInternal;
}

SignalHandler* SelectSignalDialog::createSignalHandler() const
{
  QString channel = mInternal->ChannelListBox->currentItem()->text();
  QString messageType = mInternal->MessageTypeListBox->currentItem()->text();
  QString messageField = mInternal->MessageFieldListBox->currentItem()->text();

  SignalDescription desc;
  desc.mChannel = channel;
  desc.mMessageType = messageType;
  desc.mFieldName = messageField;

  foreach (QListWidget* listWidget, mInternal->ArrayKeyWidgets)
  {
    QString arrayKey = listWidget->currentItem()->text();
    desc.mArrayKeys.append(arrayKey);
  }

  SignalHandler* signalHandler = SignalHandlerFactory::instance().createHandler(&desc);
  assert(signalHandler);
  return signalHandler;
}

<commit_msg>Added scaled robot state to signal scope<commit_after>#include "selectsignaldialog.h"
#include "ui_selectsignaldialog.h"

#include "signaldescription.h"
#include "signalhandler.h"
#include "jointnames.h"

#include <cstdio>
#include <cassert>

class SelectSignalDialog::Internal : public Ui::SelectSignalDialog
{
public:

  QList<QListWidget*> ArrayKeyWidgets;

};

SelectSignalDialog::SelectSignalDialog(QWidget* parent) : QDialog(parent)
{
  mInternal = new Internal;
  mInternal->setupUi(this);

  QStringList channels;
  channels << "ATLAS_STATE" << "ATLAS_STATE_EXTRA" << "ATLAS_STATUS" << "ATLAS_COMMAND" << "ATLAS_FOOT_POS_EST" << "TRUE_ROBOT_STATE" << "EST_ROBOT_STATE" << "SCALED_ROBOT_STATE" << "VICON_ATLAS" << "SE_INS_POSE_STATE" << "SE_MATLAB_DATAFUSION_REQ";

  QStringList messageTypes = SignalHandlerFactory::instance().messageTypes();
  QStringList messageFields;

  mInternal->ChannelListBox->addItems(channels);
  mInternal->MessageTypeListBox->addItems(messageTypes);
  mInternal->MessageFieldListBox->addItems(messageFields);

  mInternal->ChannelListBox->setCurrentRow(0);
  mInternal->MessageTypeListBox->setCurrentRow(0);
  mInternal->MessageFieldListBox->setCurrentRow(0);

  this->connect(mInternal->MessageTypeListBox, SIGNAL(currentRowChanged(int)), SLOT(onMessageTypeChanged()));
  this->connect(mInternal->MessageFieldListBox, SIGNAL(currentRowChanged(int)), SLOT(onFieldNameChanged()));
  this->onMessageTypeChanged();
}

void SelectSignalDialog::onMessageTypeChanged()
{
  if (!mInternal->MessageTypeListBox->currentItem())
  {
    return;
  }

  QString messageType = mInternal->MessageTypeListBox->currentItem()->text();

  QStringList fieldNames = SignalHandlerFactory::instance().fieldNames(messageType);
  fieldNames.sort();

  mInternal->MessageFieldListBox->clear();
  mInternal->MessageFieldListBox->addItems(fieldNames);
  mInternal->MessageFieldListBox->setCurrentRow(0);
  this->onFieldNameChanged();
}

void SelectSignalDialog::onFieldNameChanged()
{
  if (!mInternal->MessageFieldListBox->currentItem())
  {
    return;
  }

  QString messageType = mInternal->MessageTypeListBox->currentItem()->text();
  QString fieldName = mInternal->MessageFieldListBox->currentItem()->text();

  foreach (QListWidget* listWidget, mInternal->ArrayKeyWidgets)
  {
    delete listWidget;
  }
  mInternal->ArrayKeyWidgets.clear();

  const QList<QList<QString> >& validArrayKeys = SignalHandlerFactory::instance().validArrayKeys(messageType, fieldName);

  bool useArrayKeys = !validArrayKeys.empty();
  mInternal->ArrayKeysLabel->setVisible(useArrayKeys);
  mInternal->ArrayKeysContainer->setVisible(useArrayKeys);

  foreach (const QList<QString> & keys, validArrayKeys)
  {
    assert(keys.size());
    QListWidget* listWidget = new QListWidget;
    listWidget->addItems(keys);
    listWidget->setCurrentRow(0);
    mInternal->ArrayKeyWidgets.append(listWidget);
    mInternal->ArrayKeysContainer->layout()->addWidget(listWidget);
  }
}

SelectSignalDialog::~SelectSignalDialog()
{
  delete mInternal;
}

SignalHandler* SelectSignalDialog::createSignalHandler() const
{
  QString channel = mInternal->ChannelListBox->currentItem()->text();
  QString messageType = mInternal->MessageTypeListBox->currentItem()->text();
  QString messageField = mInternal->MessageFieldListBox->currentItem()->text();

  SignalDescription desc;
  desc.mChannel = channel;
  desc.mMessageType = messageType;
  desc.mFieldName = messageField;

  foreach (QListWidget* listWidget, mInternal->ArrayKeyWidgets)
  {
    QString arrayKey = listWidget->currentItem()->text();
    desc.mArrayKeys.append(arrayKey);
  }

  SignalHandler* signalHandler = SignalHandlerFactory::instance().createHandler(&desc);
  assert(signalHandler);
  return signalHandler;
}

<|endoftext|>


<commit_before>/*
 * SourceCode.cpp
 *
 * This file is part of the XShaderCompiler project (Copyright (c) 2014-2016 by Lukas Hermanns)
 * See "LICENSE.txt" for license information.
 */

#include "SourceCode.h"
#include <algorithm>

namespace Xsc
{

SourceCode::SourceCode(const std::shared_ptr<std::istream>& stream) :
    stream_{ stream }
{
}

bool SourceCode::IsValid() const
{
    return (stream_ != nullptr && stream_->good());
}

char SourceCode::Next()
{
    if (!IsValid())
        return 0;

    /* Check if reader is at end-of-line */
    while (pos_.Column() >= currentLine_.size())
    {
        /* Read new line in source file */
        std::getline(*stream_, currentLine_);
        currentLine_ += '\n';
        pos_.IncRow();

        /* Store current line for later reports */
        lines_.push_back(currentLine_);

        /* Check if end-of-file is reached */
        if (stream_->eof())
            return 0;
    }

    /* Increment column and return current character */
    auto chr = currentLine_[pos_.Column()];
    pos_.IncColumn();

    return chr;
}

static bool FinalizeMarker(
    const SourceArea& area, const std::string& lineIn, std::string& lineOut, std::string& markerOut)
{
    if (area.Pos().Column() >= lineIn.size() || area.Pos().Column() == 0 || area.Length() == 0)
        return false;

    /* Copy input line into output line */
    lineOut = lineIn;

    /* Replace all tabs with blanks after source area to make the line marker always fit ('\t' -> ' ') */
    for (auto i = static_cast<std::size_t>(area.Pos().Column()); i < lineOut.size(); ++i)
    {
        if (lineOut[i] == '\t')
            lineOut[i] = ' ';
    }

    /* Construct the space offset */
    markerOut = std::string(area.Pos().Column() - 1, ' ');

    for (size_t i = 0, n = markerOut.size(); i < n; ++i)
    {
        if (lineIn[i] == '\t')
            markerOut[i] = '\t';
    }

    /* Construct the marker */
    auto len = std::min(area.Length(), static_cast<unsigned int>(lineIn.size()) - area.Pos().Column());

    if (len > 0)
    {
        if (area.Offset() < len)
        {
            markerOut += std::string(area.Offset(), '~');
            markerOut += '^';
            markerOut += std::string(len - 1 - area.Offset(), '~');
        }
        else
            markerOut += std::string(len, '~');
        return true;
    }

    return false;
}

bool SourceCode::FetchLineMarker(const SourceArea& area, std::string& line, std::string& marker)
{
    if (area.Length() > 0)
    {
        auto row = area.Pos().Row();
        if (row == pos_.Row())
            return FinalizeMarker(area, Line(), line, marker);
        else if (row > 0)
            return FinalizeMarker(area, GetLine(static_cast<std::size_t>(row - 1)), line, marker);
    }
    return false;
}

void SourceCode::NextSourceOrigin(const std::string& filename, int lineOffset)
{
    auto origin = std::make_shared<SourceOrigin>();
    {
        origin->filename    = filename;
        origin->lineOffset  = lineOffset;
    }
    pos_.SetOrigin(origin);
}

/*
 * ======= Private: =======
 */

std::string SourceCode::GetLine(std::size_t lineIndex) const
{
    return (lineIndex < lines_.size() ? lines_[lineIndex] : "");
}

} // /namespace Xsc

// ================================================================================
<commit_msg>Enhanced "Next" function in SourceCode class to also retrieve the last line in a source file.<commit_after>/*
 * SourceCode.cpp
 *
 * This file is part of the XShaderCompiler project (Copyright (c) 2014-2016 by Lukas Hermanns)
 * See "LICENSE.txt" for license information.
 */

#include "SourceCode.h"
#include <algorithm>

namespace Xsc
{

SourceCode::SourceCode(const std::shared_ptr<std::istream>& stream) :
    stream_{ stream }
{
}

bool SourceCode::IsValid() const
{
    return (stream_ != nullptr && stream_->good());
}

char SourceCode::Next()
{
    /* Check if reader is at end-of-line */
    while (pos_.Column() >= currentLine_.size())
    {
        /* Check if end-of-file is reached */
        if (!IsValid() || stream_->eof())
            return 0;

        /* Read new line in source file */
        std::getline(*stream_, currentLine_);
        currentLine_ += '\n';
        pos_.IncRow();

        /* Store current line for later reports */
        lines_.push_back(currentLine_);
    }

    /* Increment column and return current character */
    auto chr = currentLine_[pos_.Column()];
    pos_.IncColumn();

    return chr;
}

static bool FinalizeMarker(
    const SourceArea& area, const std::string& lineIn, std::string& lineOut, std::string& markerOut)
{
    if (area.Pos().Column() >= lineIn.size() || area.Pos().Column() == 0 || area.Length() == 0)
        return false;

    /* Copy input line into output line */
    lineOut = lineIn;

    /* Replace all tabs with blanks after source area to make the line marker always fit ('\t' -> ' ') */
    for (auto i = static_cast<std::size_t>(area.Pos().Column()); i < lineOut.size(); ++i)
    {
        if (lineOut[i] == '\t')
            lineOut[i] = ' ';
    }

    /* Construct the space offset */
    markerOut = std::string(area.Pos().Column() - 1, ' ');

    for (size_t i = 0, n = markerOut.size(); i < n; ++i)
    {
        if (lineIn[i] == '\t')
            markerOut[i] = '\t';
    }

    /* Construct the marker */
    auto len = std::min(area.Length(), static_cast<unsigned int>(lineIn.size()) - area.Pos().Column());

    if (len > 0)
    {
        if (area.Offset() < len)
        {
            markerOut += std::string(area.Offset(), '~');
            markerOut += '^';
            markerOut += std::string(len - 1 - area.Offset(), '~');
        }
        else
            markerOut += std::string(len, '~');
        return true;
    }

    return false;
}

bool SourceCode::FetchLineMarker(const SourceArea& area, std::string& line, std::string& marker)
{
    if (area.Length() > 0)
    {
        auto row = area.Pos().Row();
        if (row == pos_.Row())
            return FinalizeMarker(area, Line(), line, marker);
        else if (row > 0)
            return FinalizeMarker(area, GetLine(static_cast<std::size_t>(row - 1)), line, marker);
    }
    return false;
}

void SourceCode::NextSourceOrigin(const std::string& filename, int lineOffset)
{
    auto origin = std::make_shared<SourceOrigin>();
    {
        origin->filename    = filename;
        origin->lineOffset  = lineOffset;
    }
    pos_.SetOrigin(origin);
}

/*
 * ======= Private: =======
 */

std::string SourceCode::GetLine(std::size_t lineIndex) const
{
    return (lineIndex < lines_.size() ? lines_[lineIndex] : "");
}

} // /namespace Xsc

// ================================================================================
<|endoftext|>


<commit_before>// [WriteFile Name=ReadSymbolsFromMobileStyle, Category=DisplayInformation]
// [Legal]
// Copyright 2019 Esri.

// Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// [Legal]

#include "ReadSymbolsFromMobileStyle.h"

#include "Map.h"
#include "MapQuickView.h"
#include "GraphicsOverlay.h"
#include "Graphic.h"
#include "SymbolStyle.h"
#include "MultilayerSymbol.h"
#include "Point.h"
#include "MultilayerPointSymbol.h"

#include "SymbolImageProvider.h"

#include <QDir>
#include <QObject>
#include <QQmlContext>
#include <QTemporaryDir>
#include <QtCore/qglobal.h>

#ifdef Q_OS_IOS
#include <QStandardPaths>
#endif // Q_OS_IOS

using namespace Esri::ArcGISRuntime;

// helper method to get cross platform data path
namespace {
QString defaultDataPath()
{
  QString dataPath;

#ifdef Q_OS_ANDROID
  dataPath = "/sdcard";
#elif defined Q_OS_IOS
  dataPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
#else
  dataPath = QDir::homePath();
#endif

  return dataPath;
}
} // namespace

ReadSymbolsFromMobileStyle::ReadSymbolsFromMobileStyle(QObject* parent /* = nullptr */) :
  QObject(parent),
  m_map(new Map(Basemap::topographic(this), this)),
  m_graphicParent(new QObject())
{
  m_symbolStyle = new SymbolStyle(defaultDataPath() + "/ArcGIS/Runtime/Data/styles/emoji-mobile.stylx", this);

  // Connect to the search completed signal of the style
  connect(m_symbolStyle, &SymbolStyle::searchSymbolsCompleted, this, [this](QUuid id, SymbolStyleSearchResultListModel* results)
  {
    const int index = m_taskIds.indexOf(id);
    m_models[index] = results;

    emit symbolResultsChanged();
  });

  // Load the style
  connect(m_symbolStyle, &SymbolStyle::doneLoading, this, [this](Error e)
  {
    if (!e.isEmpty())
      return;

    // search for hat symbol layers
    SymbolStyleSearchParameters hatParams;
    hatParams.setCategories({"Hat"});
    TaskWatcher hatWatcher = m_symbolStyle->searchSymbols(hatParams);
    m_taskIds.append(hatWatcher.taskId());

    // search for mouth symbol layers
    SymbolStyleSearchParameters mouthParams;
    mouthParams.setCategories({"Mouth"});
    TaskWatcher mouthWatcher = m_symbolStyle->searchSymbols(mouthParams);
    m_taskIds.append(mouthWatcher.taskId());

    // search for eyes symbol layers
    SymbolStyleSearchParameters eyeParams;
    eyeParams.setCategories({"Eyes"});
    TaskWatcher eyeWatcher = m_symbolStyle->searchSymbols(eyeParams);
    m_taskIds.append(eyeWatcher.taskId());

    // search for face symbol layers
    SymbolStyleSearchParameters faceParams;
    faceParams.setCategories({"Face"});
    TaskWatcher faceWatcher = m_symbolStyle->searchSymbols(faceParams);
    m_taskIds.append(faceWatcher.taskId());
  });

  m_symbolStyle->load();

  // Connect to fetchSymbol completed signal
  connect(m_symbolStyle, &SymbolStyle::fetchSymbolCompleted, this, [this](QUuid, Symbol* symbol)
  {
    if (m_currentSymbol)
      delete m_currentSymbol;

    // store the resulting symbol
    m_currentSymbol = static_cast<MultilayerPointSymbol*>(symbol);

    // ensure cast was successful
    if (!m_currentSymbol)
      return;

    // set the size
    m_currentSymbol->setSize(m_symbolSize);

    // set the color preferences per layer
    for (SymbolLayer* lyr : *(m_currentSymbol->symbolLayers()))
    {
      lyr->setColorLocked(true);
    }

    m_currentSymbol->symbolLayers()->at(0)->setColorLocked(false);

    // set the color
    m_currentSymbol->setColor(m_currentColor);

    // request symbol swatch
    connect(m_currentSymbol, &MultilayerPointSymbol::createSwatchCompleted, m_currentSymbol, [this](QUuid id, QImage img)
    {
      if (!m_symbolImageProvider)
        return;

      // convert the QUuid into a QString
      const QString imageId = id.toString().remove("{").remove("}");

      // add the image to the provider
      m_symbolImageProvider->addImage(imageId, img);

      // update the URL with the unique id
      m_symbolImageUrl = QString("image://%1/%2").arg(SymbolImageProvider::imageProviderId(), imageId);

      // emit the signal to trigger the QML Image to update
      emit symbolImageUrlChanged();
    });

    m_currentSymbol->createSwatch();
  });
}

ReadSymbolsFromMobileStyle::~ReadSymbolsFromMobileStyle() = default;

void ReadSymbolsFromMobileStyle::init()
{
  // Register the map view for QML
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<ReadSymbolsFromMobileStyle>("Esri.Samples", 1, 0, "ReadSymbolsFromMobileStyleSample");
  qmlRegisterUncreatableType<SymbolStyleSearchResultListModel>("Esri.Samples", 1, 0, "SymbolStyleSearchResultListModel", "SymbolStyleSearchResultListModel is uncreateable");
}

MapQuickView* ReadSymbolsFromMobileStyle::mapView() const
{
  return m_mapView;
}

// Set the view (created in QML)
void ReadSymbolsFromMobileStyle::setMapView(MapQuickView* mapView)
{
  if (!mapView || mapView == m_mapView)
    return;

  m_mapView = mapView;
  m_mapView->setMap(m_map);

  // Get the image provider from the QML Engine
  QQmlEngine* engine = QQmlEngine::contextForObject(this)->engine();
  engine->addImageProvider(SymbolImageProvider::imageProviderId(), new SymbolImageProvider);
  m_symbolImageProvider = static_cast<SymbolImageProvider*>(engine->imageProvider(SymbolImageProvider::imageProviderId()));

  // add a graphics overlay
  GraphicsOverlay* overlay = new GraphicsOverlay(this);
  m_mapView->graphicsOverlays()->append(overlay);

  // connect to mouse clicked signal
  connect(m_mapView, &MapQuickView::mouseClicked, this, [this, overlay](QMouseEvent mouseEvent)
  {
    if (!m_currentSymbol)
      return;

    const Point clickedPoint = m_mapView->screenToLocation(mouseEvent.x(), mouseEvent.y());
    Graphic* graphic = new Graphic(clickedPoint, m_currentSymbol, m_graphicParent.get());
    overlay->graphics()->append(graphic);
  });

  emit mapViewChanged();
}

// Clear the graphics from the overlay and delete each object
void ReadSymbolsFromMobileStyle::clearGraphics()
{
  if (!m_mapView)
    return;

  GraphicsOverlay* overlay = m_mapView->graphicsOverlays()->first();
  if (!overlay)
    return;

  // clear the list model
  overlay->graphics()->clear();

  // reset m_graphicsParent to delete all children
  m_graphicParent.reset(new QObject());
}

void ReadSymbolsFromMobileStyle::updateSymbol(int hatIndex, int mouthIndex, int eyeIndex, QColor color, int size)
{
  if (!m_symbolStyle || !hatResults() || !mouthResults() || !eyeResults())
    return;

  // set the color and size members
  m_currentColor = color;
  m_symbolSize = size;

  // fetch the new symbol based on keys
  QStringList keys;
  keys.append(faceResults()->searchResults().at(0).key());
  keys.append(eyeResults()->searchResults().at(eyeIndex).key());
  keys.append(mouthResults()->searchResults().at(mouthIndex).key());
  keys.append(hatResults()->searchResults().at(hatIndex).key());
  m_symbolStyle->fetchSymbol(keys);
}

SymbolStyleSearchResultListModel* ReadSymbolsFromMobileStyle::hatResults() const
{
  return m_models[0];
}

SymbolStyleSearchResultListModel* ReadSymbolsFromMobileStyle::mouthResults() const
{
  return m_models[1];
}

SymbolStyleSearchResultListModel* ReadSymbolsFromMobileStyle::eyeResults() const
{
  return m_models[2];
}

SymbolStyleSearchResultListModel* ReadSymbolsFromMobileStyle::faceResults() const
{
  return m_models[3];
}
<commit_msg>fixing crash by checking for nullptr (#871)<commit_after>// [WriteFile Name=ReadSymbolsFromMobileStyle, Category=DisplayInformation]
// [Legal]
// Copyright 2019 Esri.

// Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// [Legal]

#include "ReadSymbolsFromMobileStyle.h"

#include "Map.h"
#include "MapQuickView.h"
#include "GraphicsOverlay.h"
#include "Graphic.h"
#include "SymbolStyle.h"
#include "MultilayerSymbol.h"
#include "Point.h"
#include "MultilayerPointSymbol.h"

#include "SymbolImageProvider.h"

#include <QDir>
#include <QObject>
#include <QQmlContext>
#include <QTemporaryDir>
#include <QtCore/qglobal.h>

#ifdef Q_OS_IOS
#include <QStandardPaths>
#endif // Q_OS_IOS

using namespace Esri::ArcGISRuntime;

// helper method to get cross platform data path
namespace {
QString defaultDataPath()
{
  QString dataPath;

#ifdef Q_OS_ANDROID
  dataPath = "/sdcard";
#elif defined Q_OS_IOS
  dataPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
#else
  dataPath = QDir::homePath();
#endif

  return dataPath;
}
} // namespace

ReadSymbolsFromMobileStyle::ReadSymbolsFromMobileStyle(QObject* parent /* = nullptr */) :
  QObject(parent),
  m_map(new Map(Basemap::topographic(this), this)),
  m_graphicParent(new QObject())
{
  m_symbolStyle = new SymbolStyle(defaultDataPath() + "/ArcGIS/Runtime/Data/styles/emoji-mobile.stylx", this);

  // Connect to the search completed signal of the style
  connect(m_symbolStyle, &SymbolStyle::searchSymbolsCompleted, this, [this](QUuid id, SymbolStyleSearchResultListModel* results)
  {
    const int index = m_taskIds.indexOf(id);
    m_models[index] = results;

    emit symbolResultsChanged();
    updateSymbol(0, 0, 0, QColor(Qt::yellow), 40);
  });

  // Load the style
  connect(m_symbolStyle, &SymbolStyle::doneLoading, this, [this](Error e)
  {
    if (!e.isEmpty())
      return;

    // search for hat symbol layers
    SymbolStyleSearchParameters hatParams;
    hatParams.setCategories({"Hat"});
    TaskWatcher hatWatcher = m_symbolStyle->searchSymbols(hatParams);
    m_taskIds.append(hatWatcher.taskId());

    // search for mouth symbol layers
    SymbolStyleSearchParameters mouthParams;
    mouthParams.setCategories({"Mouth"});
    TaskWatcher mouthWatcher = m_symbolStyle->searchSymbols(mouthParams);
    m_taskIds.append(mouthWatcher.taskId());

    // search for eyes symbol layers
    SymbolStyleSearchParameters eyeParams;
    eyeParams.setCategories({"Eyes"});
    TaskWatcher eyeWatcher = m_symbolStyle->searchSymbols(eyeParams);
    m_taskIds.append(eyeWatcher.taskId());

    // search for face symbol layers
    SymbolStyleSearchParameters faceParams;
    faceParams.setCategories({"Face"});
    TaskWatcher faceWatcher = m_symbolStyle->searchSymbols(faceParams);
    m_taskIds.append(faceWatcher.taskId());
  });

  m_symbolStyle->load();

  // Connect to fetchSymbol completed signal
  connect(m_symbolStyle, &SymbolStyle::fetchSymbolCompleted, this, [this](QUuid, Symbol* symbol)
  {
    if (m_currentSymbol)
      delete m_currentSymbol;

    // store the resulting symbol
    m_currentSymbol = static_cast<MultilayerPointSymbol*>(symbol);

    // ensure cast was successful
    if (!m_currentSymbol)
      return;

    // set the size
    m_currentSymbol->setSize(m_symbolSize);

    // set the color preferences per layer
    for (SymbolLayer* lyr : *(m_currentSymbol->symbolLayers()))
    {
      lyr->setColorLocked(true);
    }

    m_currentSymbol->symbolLayers()->at(0)->setColorLocked(false);

    // set the color
    m_currentSymbol->setColor(m_currentColor);

    // request symbol swatch
    connect(m_currentSymbol, &MultilayerPointSymbol::createSwatchCompleted, m_currentSymbol, [this](QUuid id, QImage img)
    {
      if (!m_symbolImageProvider)
        return;

      // convert the QUuid into a QString
      const QString imageId = id.toString().remove("{").remove("}");

      // add the image to the provider
      m_symbolImageProvider->addImage(imageId, img);

      // update the URL with the unique id
      m_symbolImageUrl = QString("image://%1/%2").arg(SymbolImageProvider::imageProviderId(), imageId);

      // emit the signal to trigger the QML Image to update
      emit symbolImageUrlChanged();
    });

    m_currentSymbol->createSwatch();
  });
}

ReadSymbolsFromMobileStyle::~ReadSymbolsFromMobileStyle() = default;

void ReadSymbolsFromMobileStyle::init()
{
  // Register the map view for QML
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<ReadSymbolsFromMobileStyle>("Esri.Samples", 1, 0, "ReadSymbolsFromMobileStyleSample");
  qmlRegisterUncreatableType<SymbolStyleSearchResultListModel>("Esri.Samples", 1, 0, "SymbolStyleSearchResultListModel", "SymbolStyleSearchResultListModel is uncreateable");
}

MapQuickView* ReadSymbolsFromMobileStyle::mapView() const
{
  return m_mapView;
}

// Set the view (created in QML)
void ReadSymbolsFromMobileStyle::setMapView(MapQuickView* mapView)
{
  if (!mapView || mapView == m_mapView)
    return;

  m_mapView = mapView;
  m_mapView->setMap(m_map);

  // Get the image provider from the QML Engine
  QQmlEngine* engine = QQmlEngine::contextForObject(this)->engine();
  engine->addImageProvider(SymbolImageProvider::imageProviderId(), new SymbolImageProvider);
  m_symbolImageProvider = static_cast<SymbolImageProvider*>(engine->imageProvider(SymbolImageProvider::imageProviderId()));

  // add a graphics overlay
  GraphicsOverlay* overlay = new GraphicsOverlay(this);
  m_mapView->graphicsOverlays()->append(overlay);

  // connect to mouse clicked signal
  connect(m_mapView, &MapQuickView::mouseClicked, this, [this, overlay](QMouseEvent mouseEvent)
  {
    if (!m_currentSymbol)
      return;

    const Point clickedPoint = m_mapView->screenToLocation(mouseEvent.x(), mouseEvent.y());
    Graphic* graphic = new Graphic(clickedPoint, m_currentSymbol, m_graphicParent.get());
    overlay->graphics()->append(graphic);
  });

  emit mapViewChanged();
}

// Clear the graphics from the overlay and delete each object
void ReadSymbolsFromMobileStyle::clearGraphics()
{
  if (!m_mapView)
    return;

  GraphicsOverlay* overlay = m_mapView->graphicsOverlays()->first();
  if (!overlay)
    return;

  // clear the list model
  overlay->graphics()->clear();

  // reset m_graphicsParent to delete all children
  m_graphicParent.reset(new QObject());
}

void ReadSymbolsFromMobileStyle::updateSymbol(int hatIndex, int mouthIndex, int eyeIndex, QColor color, int size)
{
  if (!m_symbolStyle || !hatResults() || !mouthResults() || !eyeResults() || !faceResults())
    return;

  // set the color and size members
  m_currentColor = color;
  m_symbolSize = size;

  // fetch the new symbol based on keys
  QStringList keys;
  keys.append(faceResults()->searchResults().at(0).key());
  keys.append(eyeResults()->searchResults().at(eyeIndex).key());
  keys.append(mouthResults()->searchResults().at(mouthIndex).key());
  keys.append(hatResults()->searchResults().at(hatIndex).key());
  m_symbolStyle->fetchSymbol(keys);
}

SymbolStyleSearchResultListModel* ReadSymbolsFromMobileStyle::hatResults() const
{
  return m_models[0];
}

SymbolStyleSearchResultListModel* ReadSymbolsFromMobileStyle::mouthResults() const
{
  return m_models[1];
}

SymbolStyleSearchResultListModel* ReadSymbolsFromMobileStyle::eyeResults() const
{
  return m_models[2];
}

SymbolStyleSearchResultListModel* ReadSymbolsFromMobileStyle::faceResults() const
{
  return m_models[3];
}
<|endoftext|>