<commit_before>/*
 *  nextpnr -- Next Generation Place and Route
 *
 *  Copyright (C) 2018  Clifford Wolf <clifford@symbioticeda.com>
 *  Copyright (C) 2018  Miodrag Milanovic <miodrag@symbioticeda.com>
 *
 *  Permission to use, copy, modify, and/or distribute this software for any
 *  purpose with or without fee is hereby granted, provided that the above
 *  copyright notice and this permission notice appear in all copies.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 *  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 *  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 *  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 *  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 *  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

#ifndef NO_GUI
#include <QApplication>
#include "application.h"
#include "mainwindow.h"
#endif
#ifndef NO_PYTHON
#include "pybindings.h"
#endif

#include <boost/algorithm/string/join.hpp>
#include <boost/filesystem/convenience.hpp>
#include <boost/program_options.hpp>
#include <fstream>
#include <iostream>
#include "command.h"
#include "design_utils.h"
#include "jsonparse.h"
#include "log.h"
#include "timing.h"
#include "util.h"
#include "version.h"

NEXTPNR_NAMESPACE_BEGIN

CommandHandler::CommandHandler(int argc, char **argv) : argc(argc), argv(argv) { log_streams.clear(); }

bool CommandHandler::parseOptions()
{
    options.add(getGeneralOptions()).add(getArchOptions());
    try {
        po::parsed_options parsed =
                po::command_line_parser(argc, argv)
                        .style(po::command_line_style::default_style ^ po::command_line_style::allow_guessing)
                        .options(options)
                        .positional(pos)
                        .run();
        po::store(parsed, vm);
        po::notify(vm);
        return true;
    } catch (std::exception &e) {
        std::cout << e.what() << "\n";
        return false;
    }
}

bool CommandHandler::executeBeforeContext()
{
    if (vm.count("help") || argc == 1) {
        std::cerr << boost::filesystem::basename(argv[0])
                  << " -- Next Generation Place and Route (git sha1 " GIT_COMMIT_HASH_STR ")\n";
        std::cerr << options << "\n";
        return argc != 1;
    }

    if (vm.count("version")) {
        std::cerr << boost::filesystem::basename(argv[0])
                  << " -- Next Generation Place and Route (git sha1 " GIT_COMMIT_HASH_STR ")\n";
        return true;
    }
    validate();

    if (vm.count("quiet")) {
        log_streams.push_back(std::make_pair(&std::cerr, LogLevel::WARNING_MSG));
    } else {
        log_streams.push_back(std::make_pair(&std::cerr, LogLevel::LOG_MSG));
    }

    if (vm.count("log")) {
        std::string logfilename = vm["log"].as<std::string>();
        logfile = std::ofstream(logfilename);
        if (!logfile)
            log_error("Failed to open log file '%s' for writing.\n", logfilename.c_str());
        log_streams.push_back(std::make_pair(&logfile, LogLevel::LOG_MSG));
    }
    return false;
}

po::options_description CommandHandler::getGeneralOptions()
{
    po::options_description general("General options");
    general.add_options()("help,h", "show help");
    general.add_options()("verbose,v", "verbose output");
    general.add_options()("quiet,q", "quiet mode, only errors and warnings displayed");
    general.add_options()("log,l", po::value<std::string>(),
                          "log file, all log messages are written to this file regardless of -q");
    general.add_options()("debug", "debug output");
    general.add_options()("force,f", "keep running after errors");
#ifndef NO_GUI
    general.add_options()("gui", "start gui");
#endif
#ifndef NO_PYTHON
    general.add_options()("run", po::value<std::vector<std::string>>(),
                          "python file to execute instead of default flow");
    pos.add("run", -1);
    general.add_options()("pre-pack", po::value<std::vector<std::string>>(), "python file to run before packing");
    general.add_options()("pre-place", po::value<std::vector<std::string>>(), "python file to run before placement");
    general.add_options()("pre-route", po::value<std::vector<std::string>>(), "python file to run before routing");
    general.add_options()("post-route", po::value<std::vector<std::string>>(), "python file to run after routing");

#endif
    general.add_options()("json", po::value<std::string>(), "JSON design file to ingest");
    general.add_options()("seed", po::value<int>(), "seed value for random number generator");
    general.add_options()("randomize-seed,r", "randomize seed value for random number generator");

    general.add_options()(
            "placer", po::value<std::string>(),
            std::string("placer algorithm to use; available: " + boost::algorithm::join(Arch::availablePlacers, ", ") +
                        "; default: " + Arch::defaultPlacer)
                    .c_str());

    general.add_options()("slack_redist_iter", po::value<int>(), "number of iterations between slack redistribution");
    general.add_options()("cstrweight", po::value<float>(), "placer weighting for relative constraint satisfaction");
    general.add_options()("starttemp", po::value<float>(), "placer SA start temperature");
    general.add_options()("placer-budgets", "use budget rather than criticality in placer timing weights");

    general.add_options()("pack-only", "pack design only without placement or routing");

    general.add_options()("ignore-loops", "ignore combinational loops in timing analysis");

    general.add_options()("version,V", "show version");
    general.add_options()("test", "check architecture database integrity");
    general.add_options()("freq", po::value<double>(), "set target frequency for design in MHz");
    general.add_options()("timing-allow-fail", "allow timing to fail in design");
    general.add_options()("no-tmdriv", "disable timing-driven placement");
    general.add_options()("save", po::value<std::string>(), "project file to write");
    general.add_options()("load", po::value<std::string>(), "project file to read");
    return general;
}

void CommandHandler::setupContext(Context *ctx)
{
    if (vm.count("verbose")) {
        ctx->verbose = true;
    }

    if (vm.count("debug")) {
        ctx->verbose = true;
        ctx->debug = true;
    }

    if (vm.count("force")) {
        ctx->force = true;
    }

    if (vm.count("seed")) {
        ctx->rngseed(vm["seed"].as<int>());
    }

    if (vm.count("randomize-seed")) {
        srand(time(NULL));
        int r;
        do {
            r = rand();
        } while (r == 0);
        ctx->rngseed(r);
    }

    if (vm.count("slack_redist_iter")) {
        ctx->slack_redist_iter = vm["slack_redist_iter"].as<int>();
        if (vm.count("freq") && vm["freq"].as<double>() == 0) {
            ctx->auto_freq = true;
#ifndef NO_GUI
            if (!vm.count("gui"))
#endif
                log_warning("Target frequency not specified. Will optimise for max frequency.\n");
        }
    }

    if (vm.count("ignore-loops")) {
        settings->set("timing/ignoreLoops", true);
    }

    if (vm.count("timing-allow-fail")) {
        settings->set("timing/allowFail", true);
    }

    if (vm.count("placer")) {
        std::string placer = vm["placer"].as<std::string>();
        if (std::find(Arch::availablePlacers.begin(), Arch::availablePlacers.end(), placer) ==
            Arch::availablePlacers.end())
            log_error("Placer algorithm '%s' is not supported (available options: %s)\n", placer.c_str(),
                      boost::algorithm::join(Arch::availablePlacers, ", ").c_str());
        settings->set("placer", placer);
    } else {
        settings->set("placer", Arch::defaultPlacer);
    }

    if (vm.count("cstrweight")) {
        settings->set("placer1/constraintWeight", vm["cstrweight"].as<float>());
    }
    if (vm.count("starttemp")) {
        settings->set("placer1/startTemp", vm["starttemp"].as<float>());
    }

    if (vm.count("placer-budgets")) {
        settings->set("placer1/budgetBased", true);
    }
    if (vm.count("freq")) {
        auto freq = vm["freq"].as<double>();
        if (freq > 0)
            ctx->target_freq = freq * 1e6;
    }

    ctx->timing_driven = true;
    if (vm.count("no-tmdriv"))
        ctx->timing_driven = false;
}

int CommandHandler::executeMain(std::unique_ptr<Context> ctx)
{
    if (vm.count("test")) {
        ctx->archcheck();
        return 0;
    }

#ifndef NO_GUI
    if (vm.count("gui")) {
        Application a(argc, argv);
        MainWindow w(std::move(ctx), chipArgs);
        try {
            if (vm.count("json")) {
                std::string filename = vm["json"].as<std::string>();
                std::ifstream f(filename);
                w.notifyChangeContext();
                if (!parse_json_file(f, filename, w.getContext()))
                    log_error("Loading design failed.\n");

                customAfterLoad(w.getContext());
                w.updateLoaded();
            } else if (vm.count("load")) {
                w.projectLoad(vm["load"].as<std::string>());
            } else
                w.notifyChangeContext();
        } catch (log_execution_error_exception) {
            // show error is handled by gui itself
        }
        w.show();

        return a.exec();
    }
#endif
    if (vm.count("json")) {
        std::string filename = vm["json"].as<std::string>();
        std::ifstream f(filename);
        if (!parse_json_file(f, filename, ctx.get()))
            log_error("Loading design failed.\n");

        customAfterLoad(ctx.get());
    }

#ifndef NO_PYTHON
    init_python(argv[0], true);
    python_export_global("ctx", *ctx);

    if (vm.count("run")) {

        std::vector<std::string> files = vm["run"].as<std::vector<std::string>>();
        for (auto filename : files)
            execute_python_file(filename.c_str());
    } else
#endif
            if (vm.count("json") || vm.count("load")) {
        run_script_hook("pre-pack");
        if (!ctx->pack() && !ctx->force)
            log_error("Packing design failed.\n");
        assign_budget(ctx.get());
        ctx->check();
        print_utilisation(ctx.get());
        run_script_hook("pre-place");

        if (!vm.count("pack-only")) {
            if (!ctx->place() && !ctx->force)
                log_error("Placing design failed.\n");
            ctx->check();
            run_script_hook("pre-route");

            if (!ctx->route() && !ctx->force)
                log_error("Routing design failed.\n");
        }
        run_script_hook("post-route");

        customBitstream(ctx.get());
    }

    if (vm.count("save")) {
        project.save(ctx.get(), vm["save"].as<std::string>());
    }

#ifndef NO_PYTHON
    deinit_python();
#endif

    return had_nonfatal_error ? 1 : 0;
}

void CommandHandler::conflicting_options(const boost::program_options::variables_map &vm, const char *opt1,
                                         const char *opt2)
{
    if (vm.count(opt1) && !vm[opt1].defaulted() && vm.count(opt2) && !vm[opt2].defaulted()) {
        std::string msg = "Conflicting options '" + std::string(opt1) + "' and '" + std::string(opt2) + "'.";
        log_error("%s\n", msg.c_str());
    }
}

void CommandHandler::printFooter()
{
    int warning_count = get_or_default(message_count_by_level, LogLevel::WARNING_MSG, 0),
        error_count = get_or_default(message_count_by_level, LogLevel::ERROR_MSG, 0);
    if (warning_count > 0 || error_count > 0)
        log_always("%d warning%s, %d error%s\n", warning_count, warning_count == 1 ? "" : "s", error_count,
                   error_count == 1 ? "" : "s");
}

int CommandHandler::exec()
{
    try {
        if (!parseOptions())
            return -1;

        if (executeBeforeContext())
            return 0;

        std::unique_ptr<Context> ctx;
        if (vm.count("load") && vm.count("gui") == 0) {
            ctx = project.load(vm["load"].as<std::string>());
        } else {
            ctx = createContext();
        }
        settings = std::unique_ptr<Settings>(new Settings(ctx.get()));
        setupContext(ctx.get());
        setupArchContext(ctx.get());
        int rc = executeMain(std::move(ctx));
        printFooter();
        return rc;
    } catch (log_execution_error_exception) {
        printFooter();
        return -1;
    }
}

void CommandHandler::run_script_hook(const std::string &name)
{
#ifndef NO_PYTHON
    if (vm.count(name)) {
        std::vector<std::string> files = vm[name].as<std::vector<std::string>>();
        for (auto filename : files)
            execute_python_file(filename.c_str());
    }
#endif
}

NEXTPNR_NAMESPACE_END
<commit_msg>common: avoid std::ofstream copy<commit_after>/*
 *  nextpnr -- Next Generation Place and Route
 *
 *  Copyright (C) 2018  Clifford Wolf <clifford@symbioticeda.com>
 *  Copyright (C) 2018  Miodrag Milanovic <miodrag@symbioticeda.com>
 *
 *  Permission to use, copy, modify, and/or distribute this software for any
 *  purpose with or without fee is hereby granted, provided that the above
 *  copyright notice and this permission notice appear in all copies.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 *  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 *  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 *  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 *  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 *  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

#ifndef NO_GUI
#include <QApplication>
#include "application.h"
#include "mainwindow.h"
#endif
#ifndef NO_PYTHON
#include "pybindings.h"
#endif

#include <boost/algorithm/string/join.hpp>
#include <boost/filesystem/convenience.hpp>
#include <boost/program_options.hpp>
#include <fstream>
#include <iostream>
#include "command.h"
#include "design_utils.h"
#include "jsonparse.h"
#include "log.h"
#include "timing.h"
#include "util.h"
#include "version.h"

NEXTPNR_NAMESPACE_BEGIN

CommandHandler::CommandHandler(int argc, char **argv) : argc(argc), argv(argv) { log_streams.clear(); }

bool CommandHandler::parseOptions()
{
    options.add(getGeneralOptions()).add(getArchOptions());
    try {
        po::parsed_options parsed =
                po::command_line_parser(argc, argv)
                        .style(po::command_line_style::default_style ^ po::command_line_style::allow_guessing)
                        .options(options)
                        .positional(pos)
                        .run();
        po::store(parsed, vm);
        po::notify(vm);
        return true;
    } catch (std::exception &e) {
        std::cout << e.what() << "\n";
        return false;
    }
}

bool CommandHandler::executeBeforeContext()
{
    if (vm.count("help") || argc == 1) {
        std::cerr << boost::filesystem::basename(argv[0])
                  << " -- Next Generation Place and Route (git sha1 " GIT_COMMIT_HASH_STR ")\n";
        std::cerr << options << "\n";
        return argc != 1;
    }

    if (vm.count("version")) {
        std::cerr << boost::filesystem::basename(argv[0])
                  << " -- Next Generation Place and Route (git sha1 " GIT_COMMIT_HASH_STR ")\n";
        return true;
    }
    validate();

    if (vm.count("quiet")) {
        log_streams.push_back(std::make_pair(&std::cerr, LogLevel::WARNING_MSG));
    } else {
        log_streams.push_back(std::make_pair(&std::cerr, LogLevel::LOG_MSG));
    }

    if (vm.count("log")) {
        std::string logfilename = vm["log"].as<std::string>();
        logfile.open(logfilename);
        if (!logfile.is_open())
            log_error("Failed to open log file '%s' for writing.\n", logfilename.c_str());
        log_streams.push_back(std::make_pair(&logfile, LogLevel::LOG_MSG));
    }
    return false;
}

po::options_description CommandHandler::getGeneralOptions()
{
    po::options_description general("General options");
    general.add_options()("help,h", "show help");
    general.add_options()("verbose,v", "verbose output");
    general.add_options()("quiet,q", "quiet mode, only errors and warnings displayed");
    general.add_options()("log,l", po::value<std::string>(),
                          "log file, all log messages are written to this file regardless of -q");
    general.add_options()("debug", "debug output");
    general.add_options()("force,f", "keep running after errors");
#ifndef NO_GUI
    general.add_options()("gui", "start gui");
#endif
#ifndef NO_PYTHON
    general.add_options()("run", po::value<std::vector<std::string>>(),
                          "python file to execute instead of default flow");
    pos.add("run", -1);
    general.add_options()("pre-pack", po::value<std::vector<std::string>>(), "python file to run before packing");
    general.add_options()("pre-place", po::value<std::vector<std::string>>(), "python file to run before placement");
    general.add_options()("pre-route", po::value<std::vector<std::string>>(), "python file to run before routing");
    general.add_options()("post-route", po::value<std::vector<std::string>>(), "python file to run after routing");

#endif
    general.add_options()("json", po::value<std::string>(), "JSON design file to ingest");
    general.add_options()("seed", po::value<int>(), "seed value for random number generator");
    general.add_options()("randomize-seed,r", "randomize seed value for random number generator");

    general.add_options()(
            "placer", po::value<std::string>(),
            std::string("placer algorithm to use; available: " + boost::algorithm::join(Arch::availablePlacers, ", ") +
                        "; default: " + Arch::defaultPlacer)
                    .c_str());

    general.add_options()("slack_redist_iter", po::value<int>(), "number of iterations between slack redistribution");
    general.add_options()("cstrweight", po::value<float>(), "placer weighting for relative constraint satisfaction");
    general.add_options()("starttemp", po::value<float>(), "placer SA start temperature");
    general.add_options()("placer-budgets", "use budget rather than criticality in placer timing weights");

    general.add_options()("pack-only", "pack design only without placement or routing");

    general.add_options()("ignore-loops", "ignore combinational loops in timing analysis");

    general.add_options()("version,V", "show version");
    general.add_options()("test", "check architecture database integrity");
    general.add_options()("freq", po::value<double>(), "set target frequency for design in MHz");
    general.add_options()("timing-allow-fail", "allow timing to fail in design");
    general.add_options()("no-tmdriv", "disable timing-driven placement");
    general.add_options()("save", po::value<std::string>(), "project file to write");
    general.add_options()("load", po::value<std::string>(), "project file to read");
    return general;
}

void CommandHandler::setupContext(Context *ctx)
{
    if (vm.count("verbose")) {
        ctx->verbose = true;
    }

    if (vm.count("debug")) {
        ctx->verbose = true;
        ctx->debug = true;
    }

    if (vm.count("force")) {
        ctx->force = true;
    }

    if (vm.count("seed")) {
        ctx->rngseed(vm["seed"].as<int>());
    }

    if (vm.count("randomize-seed")) {
        srand(time(NULL));
        int r;
        do {
            r = rand();
        } while (r == 0);
        ctx->rngseed(r);
    }

    if (vm.count("slack_redist_iter")) {
        ctx->slack_redist_iter = vm["slack_redist_iter"].as<int>();
        if (vm.count("freq") && vm["freq"].as<double>() == 0) {
            ctx->auto_freq = true;
#ifndef NO_GUI
            if (!vm.count("gui"))
#endif
                log_warning("Target frequency not specified. Will optimise for max frequency.\n");
        }
    }

    if (vm.count("ignore-loops")) {
        settings->set("timing/ignoreLoops", true);
    }

    if (vm.count("timing-allow-fail")) {
        settings->set("timing/allowFail", true);
    }

    if (vm.count("placer")) {
        std::string placer = vm["placer"].as<std::string>();
        if (std::find(Arch::availablePlacers.begin(), Arch::availablePlacers.end(), placer) ==
            Arch::availablePlacers.end())
            log_error("Placer algorithm '%s' is not supported (available options: %s)\n", placer.c_str(),
                      boost::algorithm::join(Arch::availablePlacers, ", ").c_str());
        settings->set("placer", placer);
    } else {
        settings->set("placer", Arch::defaultPlacer);
    }

    if (vm.count("cstrweight")) {
        settings->set("placer1/constraintWeight", vm["cstrweight"].as<float>());
    }
    if (vm.count("starttemp")) {
        settings->set("placer1/startTemp", vm["starttemp"].as<float>());
    }

    if (vm.count("placer-budgets")) {
        settings->set("placer1/budgetBased", true);
    }
    if (vm.count("freq")) {
        auto freq = vm["freq"].as<double>();
        if (freq > 0)
            ctx->target_freq = freq * 1e6;
    }

    ctx->timing_driven = true;
    if (vm.count("no-tmdriv"))
        ctx->timing_driven = false;
}

int CommandHandler::executeMain(std::unique_ptr<Context> ctx)
{
    if (vm.count("test")) {
        ctx->archcheck();
        return 0;
    }

#ifndef NO_GUI
    if (vm.count("gui")) {
        Application a(argc, argv);
        MainWindow w(std::move(ctx), chipArgs);
        try {
            if (vm.count("json")) {
                std::string filename = vm["json"].as<std::string>();
                std::ifstream f(filename);
                w.notifyChangeContext();
                if (!parse_json_file(f, filename, w.getContext()))
                    log_error("Loading design failed.\n");

                customAfterLoad(w.getContext());
                w.updateLoaded();
            } else if (vm.count("load")) {
                w.projectLoad(vm["load"].as<std::string>());
            } else
                w.notifyChangeContext();
        } catch (log_execution_error_exception) {
            // show error is handled by gui itself
        }
        w.show();

        return a.exec();
    }
#endif
    if (vm.count("json")) {
        std::string filename = vm["json"].as<std::string>();
        std::ifstream f(filename);
        if (!parse_json_file(f, filename, ctx.get()))
            log_error("Loading design failed.\n");

        customAfterLoad(ctx.get());
    }

#ifndef NO_PYTHON
    init_python(argv[0], true);
    python_export_global("ctx", *ctx);

    if (vm.count("run")) {

        std::vector<std::string> files = vm["run"].as<std::vector<std::string>>();
        for (auto filename : files)
            execute_python_file(filename.c_str());
    } else
#endif
            if (vm.count("json") || vm.count("load")) {
        run_script_hook("pre-pack");
        if (!ctx->pack() && !ctx->force)
            log_error("Packing design failed.\n");
        assign_budget(ctx.get());
        ctx->check();
        print_utilisation(ctx.get());
        run_script_hook("pre-place");

        if (!vm.count("pack-only")) {
            if (!ctx->place() && !ctx->force)
                log_error("Placing design failed.\n");
            ctx->check();
            run_script_hook("pre-route");

            if (!ctx->route() && !ctx->force)
                log_error("Routing design failed.\n");
        }
        run_script_hook("post-route");

        customBitstream(ctx.get());
    }

    if (vm.count("save")) {
        project.save(ctx.get(), vm["save"].as<std::string>());
    }

#ifndef NO_PYTHON
    deinit_python();
#endif

    return had_nonfatal_error ? 1 : 0;
}

void CommandHandler::conflicting_options(const boost::program_options::variables_map &vm, const char *opt1,
                                         const char *opt2)
{
    if (vm.count(opt1) && !vm[opt1].defaulted() && vm.count(opt2) && !vm[opt2].defaulted()) {
        std::string msg = "Conflicting options '" + std::string(opt1) + "' and '" + std::string(opt2) + "'.";
        log_error("%s\n", msg.c_str());
    }
}

void CommandHandler::printFooter()
{
    int warning_count = get_or_default(message_count_by_level, LogLevel::WARNING_MSG, 0),
        error_count = get_or_default(message_count_by_level, LogLevel::ERROR_MSG, 0);
    if (warning_count > 0 || error_count > 0)
        log_always("%d warning%s, %d error%s\n", warning_count, warning_count == 1 ? "" : "s", error_count,
                   error_count == 1 ? "" : "s");
}

int CommandHandler::exec()
{
    try {
        if (!parseOptions())
            return -1;

        if (executeBeforeContext())
            return 0;

        std::unique_ptr<Context> ctx;
        if (vm.count("load") && vm.count("gui") == 0) {
            ctx = project.load(vm["load"].as<std::string>());
        } else {
            ctx = createContext();
        }
        settings = std::unique_ptr<Settings>(new Settings(ctx.get()));
        setupContext(ctx.get());
        setupArchContext(ctx.get());
        int rc = executeMain(std::move(ctx));
        printFooter();
        return rc;
    } catch (log_execution_error_exception) {
        printFooter();
        return -1;
    }
}

void CommandHandler::run_script_hook(const std::string &name)
{
#ifndef NO_PYTHON
    if (vm.count(name)) {
        std::vector<std::string> files = vm[name].as<std::vector<std::string>>();
        for (auto filename : files)
            execute_python_file(filename.c_str());
    }
#endif
}

NEXTPNR_NAMESPACE_END
<|endoftext|>


<commit_before>/*
 * Copyright 2014 Open Connectome Project (http://openconnecto.me)
 * Written by Disa Mhembere (disa@jhu.edu)
 *
 * This file is part of FlashGraph.
 *
 * 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.
 */

#include "../../../../libkcommon/util.hpp"
#include "../../../../libkcommon/dist_matrix.hpp"
#include "../../../../libkcommon/clusters.hpp"
#include "../../../../libkcommon/io.hpp"

namespace kpmbase = kpmeans::base;
namespace kpmprune = kpmeans::prune;

static kpmbase::prune_clusters::ptr g_clusters;
constexpr unsigned NCOL = 5;

std::vector<double> test_init_g_clusters(const size_t k=4) {
    BOOST_LOG_TRIVIAL(info) << "Running init g_clusters";
    BOOST_VERIFY(k == 4);

    const std::vector<double> v1 {1, 2, 3, 4, 5};
    const std::vector<double> v2 {6, 7, 8, 9, 10};
    const std::vector<double> v3 {6E-12, -23423.7, .82342342432, 93., 10};
    const std::vector<double> v4 {-.2342, -23.342, -.000003232, -3.234232, 1};

    const std::vector<double> v {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
        6E-12, -23423.7, .82342342432, 93., 10, -.2342, -23.342, -.000003232, -3.234232, 1};
    g_clusters = kpmbase::prune_clusters::create(k, NCOL, v); // ctor & init

    printf("Set clusters: \n");
    g_clusters->print_means();

    for (size_t cl = 0; cl < k; cl++) {
        printf("c:%lu =>\n", cl);
        kpmbase::print_arr(&(v[cl*NCOL]), NCOL);

        BOOST_VERIFY(kpmbase::eq_all<double>(&v[0], &(g_clusters->get_means()[0]), NCOL*k));
    }
    printf("Exiting test_init_g_clusters!\n");
    return v;
}

void test_eucl() {
    // Positive
    std::vector<double> v1 {1, 2, 3, 4, 5};
    std::vector<double> v2 {6, 7, 8, 9, 10};
    BOOST_VERIFY(kpmbase::eucl_dist(&v1[0], &v2[0], NCOL) == sqrt(125.0));
    BOOST_VERIFY(kpmbase::eucl_dist(&v2[0], &v1[0], NCOL) == sqrt(125.0));

    // Neg-pos, Pos-neg
    std::vector<double> v3 {6E-12, -23423.7, .82342342432, 93., 10};
    BOOST_VERIFY(ceil(kpmbase::eucl_dist(&v1[0], &v3[0], NCOL)) ==
            ceil(sqrt(548771372.227)));
    BOOST_VERIFY(ceil(kpmbase::eucl_dist(&v3[0], &v1[0], NCOL))
            == ceil(sqrt(548771372.227)));

    // No-op
    std::vector<double> v4 {0, 0, 0, 0, 0};
    BOOST_VERIFY(kpmbase::eucl_dist(&v1[0], &v4[0], NCOL) ==
            kpmbase::eucl_dist(&v4[0], &v1[0], NCOL));
    BOOST_VERIFY(kpmbase::eucl_dist(&v4[0], &v1[0], NCOL) == sqrt(55));

    // Neg-neg
    std::vector<double> v5 {-.2342, -23.342, -.000003232, -3.234232, 1};
    BOOST_VERIFY(ceil(kpmbase::eucl_dist(&v5[0], &v3[0], NCOL))
            == ceil(sqrt(547586097.2884537)));
    BOOST_VERIFY(ceil(kpmbase::eucl_dist(&v3[0], &v5[0], NCOL))
            == ceil(sqrt(547586097.2884537)));

    double arr1[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    double arr2[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    BOOST_VERIFY(kpmbase::eucl_dist(&arr1[0], &arr2[5], NCOL) == sqrt(125));

    printf("Exiting test_eucl ==> ");
}

void test_dist_matrix() {
    constexpr unsigned k = 4;
    test_init_g_clusters();
    kpmprune::dist_matrix::ptr dm = kpmprune::dist_matrix::create(k);

    /* Test compute_dist */
    dm->compute_dist(g_clusters, NCOL);

    printf("Clusters:\n"); g_clusters->print_means();
    printf("Cluster distance :\n"); dm->print();

    /* Test s_val */
    printf("Printing s_vals:\n");
    for (unsigned i = 0; i < k; i++) {
        BOOST_VERIFY(g_clusters->get_s_val(i) ==
                dm->get_min_dist(i));
    }
    printf("\n");
    printf("Exiting test_dist_matrix ==> ");
}

int main(int argc, char* argv[]) {
    if (argc > 1) {
        if (std::string(argv[1]) == "-e") {
            test_eucl(); std::cout << "Test eucl Success ...\n";
        } else if (std::string(argv[1]) == "-d") {
            test_dist_matrix(); std::cout << "Test distance matrix Success ...\n";
        } else {
            fprintf(stderr, "Unknown test option '%s'", argv[1]);
        }
    } else { /* Do all tests */
        test_eucl(); std::cout << "Test eucl Success ...\n";
        test_dist_matrix(); std::cout << "Test distance matrix Success ...\n";
    }
    return EXIT_SUCCESS;
}
<commit_msg>cran: removing boost<commit_after>/*
 * Copyright 2014 Open Connectome Project (http://openconnecto.me)
 * Written by Disa Mhembere (disa@jhu.edu)
 *
 * This file is part of FlashGraph.
 *
 * 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.
 */

#include "../../../../libkcommon/util.hpp"
#include "../../../../libkcommon/dist_matrix.hpp"
#include "../../../../libkcommon/clusters.hpp"
#include "../../../../libkcommon/io.hpp"

namespace kpmbase = kpmeans::base;
namespace kpmprune = kpmeans::prune;

static kpmbase::prune_clusters::ptr g_clusters;
constexpr unsigned NCOL = 5;

std::vector<double> test_init_g_clusters(const size_t k=4) {
    printf("Running init g_clusters\n");
    assert(k == 4);

    const std::vector<double> v1 {1, 2, 3, 4, 5};
    const std::vector<double> v2 {6, 7, 8, 9, 10};
    const std::vector<double> v3 {6E-12, -23423.7, .82342342432, 93., 10};
    const std::vector<double> v4 {-.2342, -23.342, -.000003232, -3.234232, 1};

    const std::vector<double> v {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
        6E-12, -23423.7, .82342342432, 93., 10, -.2342, -23.342, -.000003232, -3.234232, 1};
    g_clusters = kpmbase::prune_clusters::create(k, NCOL, v); // ctor & init

    printf("Set clusters: \n");
    g_clusters->print_means();

    for (size_t cl = 0; cl < k; cl++) {
        printf("c:%lu =>\n", cl);
        kpmbase::print_arr(&(v[cl*NCOL]), NCOL);

        assert(kpmbase::eq_all<double>(&v[0], &(g_clusters->get_means()[0]), NCOL*k));
    }
    printf("Exiting test_init_g_clusters!\n");
    return v;
}

void test_eucl() {
    // Positive
    std::vector<double> v1 {1, 2, 3, 4, 5};
    std::vector<double> v2 {6, 7, 8, 9, 10};
    assert(kpmbase::eucl_dist(&v1[0], &v2[0], NCOL) == sqrt(125.0));
    assert(kpmbase::eucl_dist(&v2[0], &v1[0], NCOL) == sqrt(125.0));

    // Neg-pos, Pos-neg
    std::vector<double> v3 {6E-12, -23423.7, .82342342432, 93., 10};
    assert(ceil(kpmbase::eucl_dist(&v1[0], &v3[0], NCOL)) ==
            ceil(sqrt(548771372.227)));
    assert(ceil(kpmbase::eucl_dist(&v3[0], &v1[0], NCOL))
            == ceil(sqrt(548771372.227)));

    // No-op
    std::vector<double> v4 {0, 0, 0, 0, 0};
    assert(kpmbase::eucl_dist(&v1[0], &v4[0], NCOL) ==
            kpmbase::eucl_dist(&v4[0], &v1[0], NCOL));
    assert(kpmbase::eucl_dist(&v4[0], &v1[0], NCOL) == sqrt(55));

    // Neg-neg
    std::vector<double> v5 {-.2342, -23.342, -.000003232, -3.234232, 1};
    assert(ceil(kpmbase::eucl_dist(&v5[0], &v3[0], NCOL))
            == ceil(sqrt(547586097.2884537)));
    assert(ceil(kpmbase::eucl_dist(&v3[0], &v5[0], NCOL))
            == ceil(sqrt(547586097.2884537)));

    double arr1[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    double arr2[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    assert(kpmbase::eucl_dist(&arr1[0], &arr2[5], NCOL) == sqrt(125));

    printf("Exiting test_eucl ==> ");
}

void test_dist_matrix() {
    constexpr unsigned k = 4;
    test_init_g_clusters();
    kpmprune::dist_matrix::ptr dm = kpmprune::dist_matrix::create(k);

    /* Test compute_dist */
    dm->compute_dist(g_clusters, NCOL);

    printf("Clusters:\n"); g_clusters->print_means();
    printf("Cluster distance :\n"); dm->print();

    /* Test s_val */
    printf("Printing s_vals:\n");
    for (unsigned i = 0; i < k; i++) {
        assert(g_clusters->get_s_val(i) ==
                dm->get_min_dist(i));
    }
    printf("\n");
    printf("Exiting test_dist_matrix ==> ");
}

int main(int argc, char* argv[]) {
    if (argc > 1) {
        if (std::string(argv[1]) == "-e") {
            test_eucl(); std::cout << "Test eucl Success ...\n";
        } else if (std::string(argv[1]) == "-d") {
            test_dist_matrix(); std::cout << "Test distance matrix Success ...\n";
        } else {
            fprintf(stderr, "Unknown test option '%s'", argv[1]);
        }
    } else { /* Do all tests */
        test_eucl(); std::cout << "Test eucl Success ...\n";
        test_dist_matrix(); std::cout << "Test distance matrix Success ...\n";
    }
    return EXIT_SUCCESS;
}
<|endoftext|>


<commit_before>/*
 * Copyright(c) Sophist Solutions, Inc. 1990-2013.  All rights reserved
 */
#ifndef _Stroika_Foundation_DataExchangeFormat_ObjectVariantMapper_inl_
#define _Stroika_Foundation_DataExchangeFormat_ObjectVariantMapper_inl_  1

/*
 ********************************************************************************
 ***************************** Implementation Details ***************************
 ********************************************************************************
 */
#include    "../Execution/Exceptions.h"

#include    "BadFormatException.h"

namespace   Stroika {
    namespace   Foundation {
        namespace   DataExchangeFormat {

            /*
             ********************************************************************************
             ******************* ObjectVariantMapper::StructureFieldInfo ********************
             ********************************************************************************
             */
            inline  ObjectVariantMapper::StructureFieldInfo::StructureFieldInfo (size_t fieldOffset, type_index typeInfo, const String& serializedFieldName)
                : fOffset (fieldOffset)
                , fTypeInfo (typeInfo)
                , fSerializedFieldName (serializedFieldName)
            {
            }

            /*
             ********************************************************************************
             ******************************** ObjectVariantMapper ***************************
             ********************************************************************************
             */
            inline  Set<ObjectVariantMapper::TypeMappingDetails>    ObjectVariantMapper::GetTypeMappingRegistry () const
            {
                return fSerializers_;
            }
            inline  void    ObjectVariantMapper::SetTypeMappingRegistry (const Set<TypeMappingDetails>& s)
            {
                fSerializers_ = s;
            }
            template    <typename CLASS>
            inline  void    ObjectVariantMapper::RegisterClass (const Sequence<StructureFieldInfo>& fieldDescriptions)
            {
                RegisterTypeMapper (TypeMappingDetails (typeid (CLASS), sizeof (CLASS), fieldDescriptions));
            }
            template    <typename CLASS>
            inline  void    ObjectVariantMapper::ToObject (const Memory::VariantValue& v, CLASS* into) const
            {
                ToObject  (typeid (CLASS), v, reinterpret_cast<Byte*> (into));
            }
            template    <typename CLASS>
            inline  CLASS    ObjectVariantMapper::ToObject (const Memory::VariantValue& v) const
            {
                CLASS tmp;
                ToObject (v, &tmp);
                return tmp;
            }
            template    <typename CLASS>
            inline  VariantValue    ObjectVariantMapper::FromObject (const CLASS& from) const
            {
                return FromObject  (typeid (CLASS), reinterpret_cast<const Byte*> (&from));
            }
            template <typename T, size_t SZ>
            ObjectVariantMapper::TypeMappingDetails ObjectVariantMapper::MakeCommonSerializer_Array ()
            {
                auto toVariantMapper = [] (const ObjectVariantMapper * mapper, const Byte * fromObjOfTypeT) -> VariantValue {
                    Sequence<VariantValue> s;
                    const T*  actualMember    =   reinterpret_cast<const T*> (fromObjOfTypeT);
                    for (auto i = actualMember; i < actualMember + SZ; ++i) {
                        s.Append (mapper->FromObject<T> (*i));
                    }
                    return VariantValue (s);
                };
                auto fromVariantMapper = [] (const ObjectVariantMapper * mapper, const VariantValue & d, Byte * intoObjOfTypeT) -> void {
                    Sequence<VariantValue> s  =   d.As<Sequence<T>> ();
                    T*  actualMember    =   reinterpret_cast<T*> (intoObjOfTypeT);
                    if (s.size () > SZ) {
                        Execution::DoThrow<BadFormatException> (BadFormatException ());
                    }
                    size_t idx = 0;
                    for (auto i : s) {
                        actualMember[idx++] = mapper->ToObject<T> (i);
                    }
                    while (idx < SZ) {
                        actualMember[idx++] = T ();
                    }
                };
                return ObjectVariantMapper::TypeMappingDetails (typeid (Sequence<T>), toVariantMapper, fromVariantMapper);
            }
            template    <typename T>
            ObjectVariantMapper::TypeMappingDetails ObjectVariantMapper::MakeCommonSerializer_Sequence ()
            {
                auto toVariantMapper = [] (const ObjectVariantMapper * mapper, const Byte * fromObjOfTypeT) -> VariantValue {
                    Sequence<VariantValue> s;
                    const Sequence<T>*  actualMember    =   reinterpret_cast<const Sequence<T>*> (fromObjOfTypeT);
                    for (auto i : *actualMember) {
                        s.Append (mapper->FromObject<T> (i));
                    }
                    return VariantValue (s);
                };
                auto fromVariantMapper = [] (const ObjectVariantMapper * mapper, const VariantValue & d, Byte * intoObjOfTypeT) -> void {
                    Sequence<VariantValue> s  =   d.As<Sequence<T>> ();
                    Sequence<T>*    actualInto  =   reinterpret_cast<Sequence<T>*> (intoObjOfTypeT);
                    actualInto->clear ();
                    for (auto i : s) {
                        actualInto->Append (mapper->ToObject<T> (i));
                    }
                };
                return ObjectVariantMapper::TypeMappingDetails (typeid (Sequence<T>), toVariantMapper, fromVariantMapper);
            }
            template    <typename KEY_TYPE, typename VALUE_TYPE>
            ObjectVariantMapper::TypeMappingDetails ObjectVariantMapper::MakeCommonSerializer_Mapping ()
            {
                auto toVariantMapper = [] (const ObjectVariantMapper * mapper, const Byte * fromObjOfTypeT) -> VariantValue {
                    Sequence<VariantValue> s;
                    const Mapping<KEY_TYPE, VALUE_TYPE>*  actualMember    =   reinterpret_cast<const Mapping<KEY_TYPE, VALUE_TYPE>*> (fromObjOfTypeT);
                    for (auto i : *actualMember) {
                        Sequence<VariantValue>  encodedPair;
                        encodedPair.Append (mapper->FromObject<KEY_TYPE> (i.first));
                        encodedPair.Append (mapper->FromObject<VALUE_TYPE> (i.second));
                        s.Append (VariantValue (encodedPair));
                    }
                    return VariantValue (s);
                };
                auto fromVariantMapper = [] (const ObjectVariantMapper * mapper, const VariantValue & d, Byte * intoObjOfTypeT) -> void {
                    Sequence<VariantValue>          s  =   d.As<Sequence<VariantValue>> ();
                    Mapping<KEY_TYPE, VALUE_TYPE>*  actualInto  =   reinterpret_cast<Mapping<KEY_TYPE, VALUE_TYPE>*> (intoObjOfTypeT);
                    actualInto->clear ();
                    for (VariantValue encodedPair : s) {
                        Sequence<VariantValue>  p   =   p.As<Sequence<VariantValue>> ();
                        if (p.size () != 2) {
                            Execution::DoThrow<BadFormatException> (BadFormatException ());
                        }
                        actualInto->Add (mapper->ToObject<KEY_TYPE> (p[0]), mapper->ToObject<VALUE_TYPE> (p[1]));
                    }
                };
                return ObjectVariantMapper::TypeMappingDetails (typeid (Mapping<KEY_TYPE, VALUE_TYPE>), toVariantMapper, fromVariantMapper);
            }
            template <typename RANGE_TYPE>
            ObjectVariantMapper::TypeMappingDetails ObjectVariantMapper::MakeCommonSerializer_Range ()
            {
                auto toVariantMapper = [] (const ObjectVariantMapper * mapper, const Byte * fromObjOfTypeT) -> VariantValue {
                    typedef typename RANGE_TYPE::ElementType    ElementType;
                    Mapping<String, VariantValue> m;
                    const RANGE_TYPE*  actualMember    =   reinterpret_cast<const RANGE_TYPE*> (fromObjOfTypeT);
                    m.Add (L"Begin", mapper->FromObject<ElementType> (actualMember->begin ()));
                    m.Add (L"End", mapper->FromObject<ElementType> (actualMember->end ()));
                    return VariantValue (m);
                };
                auto fromVariantMapper = [] (const ObjectVariantMapper * mapper, const VariantValue & d, Byte * intoObjOfTypeT) -> void {
                    typedef typename RANGE_TYPE::ElementType    ElementType;
                    Mapping<String, VariantValue>          m  =   d.As<Mapping<String, VariantValue>> ();
                    RANGE_TYPE*  actualInto  =   reinterpret_cast<RANGE_TYPE*> (intoObjOfTypeT);
                    if (m.size () != 2) {
                        Execution::DoThrow<BadFormatException> (BadFormatException ());
                    }
                    if (not m.ContainsKey (L"Begin") or not m.ContainsKey (L"End")) {
                        Execution::DoThrow<BadFormatException> (BadFormatException ());
                    }
                    ElementType from    =   mapper->ToObject<ElementType> (*m.Lookup (L"Begin"));
                    ElementType to      =   mapper->ToObject<ElementType> (*m.Lookup (L"End"));
                    if (not (RANGE_TYPE::TraitsType::kMin <= from and from < RANGE_TYPE::TraitsType::kMax)) {
                        Execution::DoThrow<BadFormatException> (BadFormatException ());
                    }
                    if (not (RANGE_TYPE::TraitsType::kMin <= to and to < RANGE_TYPE::TraitsType::kMax)) {
                        Execution::DoThrow<BadFormatException> (BadFormatException ());
                    }
                    * actualInto = RANGE_TYPE (from, to);
                };
                return ObjectVariantMapper::TypeMappingDetails (typeid (RANGE_TYPE), toVariantMapper, fromVariantMapper);
            }
            template <typename ENUM_TYPE>
            ObjectVariantMapper::TypeMappingDetails ObjectVariantMapper::MakeCommonSerializer_Enumeration ()
            {
                Require (std::is_enum<ENUM_TYPE>::value);
#if     qCompilerAndStdLib_Supports_TypeTraits_underlying_type
                typedef typename std::underlying_type<ENUM_TYPE>::type SerializeAsType;
#else
                typedef long long SerializeAsType;
#endif
                auto toVariantMapper = [] (const ObjectVariantMapper * mapper, const Byte * fromObjOfTypeT) -> VariantValue {
                    const ENUM_TYPE*  actualMember    =   reinterpret_cast<const ENUM_TYPE*> (fromObjOfTypeT);
                    Assert (sizeof (SerializeAsType) == sizeof (ENUM_TYPE));
                    Assert (static_cast<ENUM_TYPE> (static_cast<SerializeAsType> (*actualMember)) == *actualMember);    // no round-trip loss
                    return VariantValue (static_cast<SerializeAsType> (*actualMember));
                };
                auto fromVariantMapper = [] (const ObjectVariantMapper * mapper, const VariantValue & d, Byte * intoObjOfTypeT) -> void {
                    ENUM_TYPE*  actualInto  =   reinterpret_cast<ENUM_TYPE*> (intoObjOfTypeT);
                    * actualInto = static_cast<ENUM_TYPE> (d.As<SerializeAsType> ());
                    Assert (sizeof (SerializeAsType) == sizeof (ENUM_TYPE));
                    Assert (static_cast<SerializeAsType> (*actualInto) == d.As<SerializeAsType> ());  // no round-trip loss
                    if (not (ENUM_TYPE::eSTART <= *actualInto and * actualInto <= ENUM_TYPE::eEND)) {
                        Execution::DoThrow<BadFormatException> (BadFormatException ());
                    }
                };
                return ObjectVariantMapper::TypeMappingDetails (typeid (ENUM_TYPE), toVariantMapper, fromVariantMapper);
            }

#if 0
            template    <typename T>
            inline  ObjectVariantMapper::TypeMappingDetails ObjectVariantMapper::MakeCommonSerializer<Sequence<T>> ()
            {
                return mkSerializerInfoFor_Sequence_<T> ();
            }
#endif

        }
    }
}
#endif  /*_Stroika_Foundation_DataExchangeFormat_ObjectVariantMapper_inl_*/
<commit_msg>silence assert test if not qCompilerAndStdLib_Supports_TypeTraits_underlying_type (cuz we cannot get right type)<commit_after>/*
 * Copyright(c) Sophist Solutions, Inc. 1990-2013.  All rights reserved
 */
#ifndef _Stroika_Foundation_DataExchangeFormat_ObjectVariantMapper_inl_
#define _Stroika_Foundation_DataExchangeFormat_ObjectVariantMapper_inl_  1

/*
 ********************************************************************************
 ***************************** Implementation Details ***************************
 ********************************************************************************
 */
#include    "../Execution/Exceptions.h"

#include    "BadFormatException.h"

namespace   Stroika {
    namespace   Foundation {
        namespace   DataExchangeFormat {

            /*
             ********************************************************************************
             ******************* ObjectVariantMapper::StructureFieldInfo ********************
             ********************************************************************************
             */
            inline  ObjectVariantMapper::StructureFieldInfo::StructureFieldInfo (size_t fieldOffset, type_index typeInfo, const String& serializedFieldName)
                : fOffset (fieldOffset)
                , fTypeInfo (typeInfo)
                , fSerializedFieldName (serializedFieldName)
            {
            }

            /*
             ********************************************************************************
             ******************************** ObjectVariantMapper ***************************
             ********************************************************************************
             */
            inline  Set<ObjectVariantMapper::TypeMappingDetails>    ObjectVariantMapper::GetTypeMappingRegistry () const
            {
                return fSerializers_;
            }
            inline  void    ObjectVariantMapper::SetTypeMappingRegistry (const Set<TypeMappingDetails>& s)
            {
                fSerializers_ = s;
            }
            template    <typename CLASS>
            inline  void    ObjectVariantMapper::RegisterClass (const Sequence<StructureFieldInfo>& fieldDescriptions)
            {
                RegisterTypeMapper (TypeMappingDetails (typeid (CLASS), sizeof (CLASS), fieldDescriptions));
            }
            template    <typename CLASS>
            inline  void    ObjectVariantMapper::ToObject (const Memory::VariantValue& v, CLASS* into) const
            {
                ToObject  (typeid (CLASS), v, reinterpret_cast<Byte*> (into));
            }
            template    <typename CLASS>
            inline  CLASS    ObjectVariantMapper::ToObject (const Memory::VariantValue& v) const
            {
                CLASS tmp;
                ToObject (v, &tmp);
                return tmp;
            }
            template    <typename CLASS>
            inline  VariantValue    ObjectVariantMapper::FromObject (const CLASS& from) const
            {
                return FromObject  (typeid (CLASS), reinterpret_cast<const Byte*> (&from));
            }
            template <typename T, size_t SZ>
            ObjectVariantMapper::TypeMappingDetails ObjectVariantMapper::MakeCommonSerializer_Array ()
            {
                auto toVariantMapper = [] (const ObjectVariantMapper * mapper, const Byte * fromObjOfTypeT) -> VariantValue {
                    Sequence<VariantValue> s;
                    const T*  actualMember    =   reinterpret_cast<const T*> (fromObjOfTypeT);
                    for (auto i = actualMember; i < actualMember + SZ; ++i) {
                        s.Append (mapper->FromObject<T> (*i));
                    }
                    return VariantValue (s);
                };
                auto fromVariantMapper = [] (const ObjectVariantMapper * mapper, const VariantValue & d, Byte * intoObjOfTypeT) -> void {
                    Sequence<VariantValue> s  =   d.As<Sequence<T>> ();
                    T*  actualMember    =   reinterpret_cast<T*> (intoObjOfTypeT);
                    if (s.size () > SZ) {
                        Execution::DoThrow<BadFormatException> (BadFormatException ());
                    }
                    size_t idx = 0;
                    for (auto i : s) {
                        actualMember[idx++] = mapper->ToObject<T> (i);
                    }
                    while (idx < SZ) {
                        actualMember[idx++] = T ();
                    }
                };
                return ObjectVariantMapper::TypeMappingDetails (typeid (Sequence<T>), toVariantMapper, fromVariantMapper);
            }
            template    <typename T>
            ObjectVariantMapper::TypeMappingDetails ObjectVariantMapper::MakeCommonSerializer_Sequence ()
            {
                auto toVariantMapper = [] (const ObjectVariantMapper * mapper, const Byte * fromObjOfTypeT) -> VariantValue {
                    Sequence<VariantValue> s;
                    const Sequence<T>*  actualMember    =   reinterpret_cast<const Sequence<T>*> (fromObjOfTypeT);
                    for (auto i : *actualMember) {
                        s.Append (mapper->FromObject<T> (i));
                    }
                    return VariantValue (s);
                };
                auto fromVariantMapper = [] (const ObjectVariantMapper * mapper, const VariantValue & d, Byte * intoObjOfTypeT) -> void {
                    Sequence<VariantValue> s  =   d.As<Sequence<T>> ();
                    Sequence<T>*    actualInto  =   reinterpret_cast<Sequence<T>*> (intoObjOfTypeT);
                    actualInto->clear ();
                    for (auto i : s) {
                        actualInto->Append (mapper->ToObject<T> (i));
                    }
                };
                return ObjectVariantMapper::TypeMappingDetails (typeid (Sequence<T>), toVariantMapper, fromVariantMapper);
            }
            template    <typename KEY_TYPE, typename VALUE_TYPE>
            ObjectVariantMapper::TypeMappingDetails ObjectVariantMapper::MakeCommonSerializer_Mapping ()
            {
                auto toVariantMapper = [] (const ObjectVariantMapper * mapper, const Byte * fromObjOfTypeT) -> VariantValue {
                    Sequence<VariantValue> s;
                    const Mapping<KEY_TYPE, VALUE_TYPE>*  actualMember    =   reinterpret_cast<const Mapping<KEY_TYPE, VALUE_TYPE>*> (fromObjOfTypeT);
                    for (auto i : *actualMember) {
                        Sequence<VariantValue>  encodedPair;
                        encodedPair.Append (mapper->FromObject<KEY_TYPE> (i.first));
                        encodedPair.Append (mapper->FromObject<VALUE_TYPE> (i.second));
                        s.Append (VariantValue (encodedPair));
                    }
                    return VariantValue (s);
                };
                auto fromVariantMapper = [] (const ObjectVariantMapper * mapper, const VariantValue & d, Byte * intoObjOfTypeT) -> void {
                    Sequence<VariantValue>          s  =   d.As<Sequence<VariantValue>> ();
                    Mapping<KEY_TYPE, VALUE_TYPE>*  actualInto  =   reinterpret_cast<Mapping<KEY_TYPE, VALUE_TYPE>*> (intoObjOfTypeT);
                    actualInto->clear ();
                    for (VariantValue encodedPair : s) {
                        Sequence<VariantValue>  p   =   p.As<Sequence<VariantValue>> ();
                        if (p.size () != 2) {
                            Execution::DoThrow<BadFormatException> (BadFormatException ());
                        }
                        actualInto->Add (mapper->ToObject<KEY_TYPE> (p[0]), mapper->ToObject<VALUE_TYPE> (p[1]));
                    }
                };
                return ObjectVariantMapper::TypeMappingDetails (typeid (Mapping<KEY_TYPE, VALUE_TYPE>), toVariantMapper, fromVariantMapper);
            }
            template <typename RANGE_TYPE>
            ObjectVariantMapper::TypeMappingDetails ObjectVariantMapper::MakeCommonSerializer_Range ()
            {
                auto toVariantMapper = [] (const ObjectVariantMapper * mapper, const Byte * fromObjOfTypeT) -> VariantValue {
                    typedef typename RANGE_TYPE::ElementType    ElementType;
                    Mapping<String, VariantValue> m;
                    const RANGE_TYPE*  actualMember    =   reinterpret_cast<const RANGE_TYPE*> (fromObjOfTypeT);
                    m.Add (L"Begin", mapper->FromObject<ElementType> (actualMember->begin ()));
                    m.Add (L"End", mapper->FromObject<ElementType> (actualMember->end ()));
                    return VariantValue (m);
                };
                auto fromVariantMapper = [] (const ObjectVariantMapper * mapper, const VariantValue & d, Byte * intoObjOfTypeT) -> void {
                    typedef typename RANGE_TYPE::ElementType    ElementType;
                    Mapping<String, VariantValue>          m  =   d.As<Mapping<String, VariantValue>> ();
                    RANGE_TYPE*  actualInto  =   reinterpret_cast<RANGE_TYPE*> (intoObjOfTypeT);
                    if (m.size () != 2) {
                        Execution::DoThrow<BadFormatException> (BadFormatException ());
                    }
                    if (not m.ContainsKey (L"Begin") or not m.ContainsKey (L"End")) {
                        Execution::DoThrow<BadFormatException> (BadFormatException ());
                    }
                    ElementType from    =   mapper->ToObject<ElementType> (*m.Lookup (L"Begin"));
                    ElementType to      =   mapper->ToObject<ElementType> (*m.Lookup (L"End"));
                    if (not (RANGE_TYPE::TraitsType::kMin <= from and from < RANGE_TYPE::TraitsType::kMax)) {
                        Execution::DoThrow<BadFormatException> (BadFormatException ());
                    }
                    if (not (RANGE_TYPE::TraitsType::kMin <= to and to < RANGE_TYPE::TraitsType::kMax)) {
                        Execution::DoThrow<BadFormatException> (BadFormatException ());
                    }
                    * actualInto = RANGE_TYPE (from, to);
                };
                return ObjectVariantMapper::TypeMappingDetails (typeid (RANGE_TYPE), toVariantMapper, fromVariantMapper);
            }
            template <typename ENUM_TYPE>
            ObjectVariantMapper::TypeMappingDetails ObjectVariantMapper::MakeCommonSerializer_Enumeration ()
            {
                Require (std::is_enum<ENUM_TYPE>::value);
#if     qCompilerAndStdLib_Supports_TypeTraits_underlying_type
                typedef typename std::underlying_type<ENUM_TYPE>::type SerializeAsType;
#else
                typedef long long SerializeAsType;
#endif
                auto toVariantMapper = [] (const ObjectVariantMapper * mapper, const Byte * fromObjOfTypeT) -> VariantValue {
                    const ENUM_TYPE*  actualMember    =   reinterpret_cast<const ENUM_TYPE*> (fromObjOfTypeT);
#if     qCompilerAndStdLib_Supports_TypeTraits_underlying_type
                    Assert (sizeof (SerializeAsType) == sizeof (ENUM_TYPE));
#endif
                    Assert (static_cast<ENUM_TYPE> (static_cast<SerializeAsType> (*actualMember)) == *actualMember);    // no round-trip loss
                    return VariantValue (static_cast<SerializeAsType> (*actualMember));
                };
                auto fromVariantMapper = [] (const ObjectVariantMapper * mapper, const VariantValue & d, Byte * intoObjOfTypeT) -> void {
                    ENUM_TYPE*  actualInto  =   reinterpret_cast<ENUM_TYPE*> (intoObjOfTypeT);
                    * actualInto = static_cast<ENUM_TYPE> (d.As<SerializeAsType> ());
#if     qCompilerAndStdLib_Supports_TypeTraits_underlying_type
                    Assert (sizeof (SerializeAsType) == sizeof (ENUM_TYPE));
#endif
                    Assert (static_cast<SerializeAsType> (*actualInto) == d.As<SerializeAsType> ());  // no round-trip loss
                    if (not (ENUM_TYPE::eSTART <= *actualInto and * actualInto <= ENUM_TYPE::eEND)) {
                        Execution::DoThrow<BadFormatException> (BadFormatException ());
                    }
                };
                return ObjectVariantMapper::TypeMappingDetails (typeid (ENUM_TYPE), toVariantMapper, fromVariantMapper);
            }

#if 0
            template    <typename T>
            inline  ObjectVariantMapper::TypeMappingDetails ObjectVariantMapper::MakeCommonSerializer<Sequence<T>> ()
            {
                return mkSerializerInfoFor_Sequence_<T> ();
            }
#endif

        }
    }
}
#endif  /*_Stroika_Foundation_DataExchangeFormat_ObjectVariantMapper_inl_*/
<|endoftext|>


<commit_before>#include <boost/python.hpp>

#include "mue_algorithm.h"
#include "teams.h"
#include "distances.h"
#include "seen_table.h"

char const * peng() {
	return mue::peng().c_str();
}

BOOST_PYTHON_MODULE (_pymue)
{
	using namespace boost::python;

	def ("peng", peng);

	class_<mue::Team>("Team", init<int>())
		.def("id", &mue::Team::id);

	class_<mue::Distance_matrix>("DistanceMatrix", init<int>())
		.def("set_cost", &mue::Distance_matrix::set_cost)
		.def("lookup", &mue::Distance_matrix::lookup);

	class_<mue::Seen_table>("SeenTable", init<int>())
		.def("clone", &mue::Seen_table::clone)
		.def("generation", &mue::Seen_table::generation)
		.def("seen", &mue::Seen_table::seen)
		.def("add_meeting", &mue::Seen_table::add_meeting);
}
<commit_msg>Replace Seen_table with Seen_table_wrapper in the binding<commit_after>#include <boost/python.hpp>

#include "mue_algorithm.h"
#include "teams.h"
#include "distances.h"

#include "seen_table_wrapper.h"

char const * peng() {
	return mue::peng().c_str();
}

BOOST_PYTHON_MODULE (_pymue)
{
	using namespace boost::python;

	def ("peng", peng);

	class_<mue::Team>("Team", init<int>())
		.def("id", &mue::Team::id);

	class_<mue::Distance_matrix>("DistanceMatrix", init<int>())
		.def("set_cost", &mue::Distance_matrix::set_cost)
		.def("lookup", &mue::Distance_matrix::lookup);

	class_<pymue::Seen_table_wrapper>("SeenTable", init<int>())
		.def("clone", &pymue::Seen_table_wrapper::clone)
		.def("generation", &pymue::Seen_table_wrapper::generation)
		.def("seen", &pymue::Seen_table_wrapper::seen)
		.def("add_meeting", &pymue::Seen_table_wrapper::add_meeting);
}
<|endoftext|>


<commit_before>#pragma once

#include "evaluation.hpp"
#include "csv_parser.hpp"

#include <stdexcept>

#include <boost/filesystem.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/regex.hpp>
#include <boost/optional.hpp>

namespace vakkenranking
{
	class complete_parser
	{
		static evaluation parse_filename(const std::string& str)
		{
			//Antwoorden_Vragenlijst_NWI-I00032 (2013-01-29)_67288_Geavanceerd Programmeren.xls.csv
			//Results_survey_NWI-IBC017_2014-11-04_758478_Calculus and Probability Theory.csv
			const static boost::regex r1(".*_(?:NWI-)?([^\\_^-]+) \\((.+)\\)_([0-9]+)_(.+)\\.csv");
			const static boost::regex r2(".*_(?:NWI-)?([^\\_^-]+)_(.+)_([0-9]+)_(.+)\\.csv");

			boost::smatch match;
			if(!boost::regex_match(str, match, r1) && !boost::regex_match(str, match, r2))
				throw std::runtime_error(std::string("Can not parse filename (complete): ") + str);

			return evaluation(match[1], match[4], match[3], match[2]);
		}

		static size_t find_course_grade_key(const std::vector<std::string>& line, const std::string& path)
		{
			const static std::vector<std::string> keys = {
				"[Ik geef deze cursus het volgende rapportcijfer] Ik geef deze cursus het volgende rapportcijfer",
				"Ik geef deze cursus het volgende rapportcijfer [Ik geef deze cursus het volgende rapportcijfer]",
				"[I would rate this course] On a scale from 1 to 10 (10 being excellent) I would rate this course"
			};

			return find_key(line, keys, path, "course_grade");
		}

		static size_t find_teacher_grade_key(const std::vector<std::string>& line, const std::string& path)
		{
			const static std::vector<std::string> keys = {
				"[Ik geef de docent(en) het volgende rapportcijfer] Ik geef de docent(en) het volgende rapportcijfer",
				"Ik geef de docent(en) het volgende rapportcijfer [Ik geef de docent(en) het volgende rapportcijfer]",
				"[I would rate the lecturer(s)/teacher(s)] On a scale from 1 to 10 (10 being excellent) I would rate the lecturer(s)/teacher(s)"
			};

			return find_key(line, keys, path, "teacher_grade");
		}

		static size_t find_key(const std::vector<std::string>& line, const std::vector<std::string>& keys, const std::string& path, const std::string& function_name)
		{
			for(const auto& key : keys)
			{
				size_t key_i = std::distance(line.begin(), std::find(line.begin(), line.end(), key));
				if(key_i < line.size())
					return key_i;
			}

			throw std::runtime_error(std::string("Can not find key for ") + function_name + " in " + path);
		}

		static void parse_file(evaluation& e, const std::string& path)
		{
			csv_parser parser(path);
			size_t course_grade_key_i, teacher_grade_key_i;

			{
				std::vector<std::string> line;
				if(!parser.read(line))
					throw std::runtime_error("No headerline");

				course_grade_key_i = find_course_grade_key(line, path);
				teacher_grade_key_i = find_teacher_grade_key(line, path);
			}

			size_t i = 0;
			for(std::vector<std::string> line; parser.read(line); ++i)
			{
				if(line.size() > 1)
				{
					if(line.at(course_grade_key_i) != "") //Answer has not been filled in
						e.course_grade.ratings.push_back(boost::lexical_cast<double>(line.at(course_grade_key_i)));

					if(line.at(teacher_grade_key_i) != "") //Answer has not been filled in
						e.teacher_grade.ratings.push_back(boost::lexical_cast<double>(line.at(teacher_grade_key_i)));
				}
			}
		}

		void operator=(complete_parser&) = delete;
		complete_parser() = delete;

	public:
		static evaluation parse(const boost::filesystem::path& p)
		{
			evaluation e = parse_filename(p.filename().string());
			parse_file(e, p.string());
			return e;
		}
	};
}
<commit_msg>Added new phrasing of course grading<commit_after>#pragma once

#include "evaluation.hpp"
#include "csv_parser.hpp"

#include <stdexcept>

#include <boost/filesystem.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/regex.hpp>
#include <boost/optional.hpp>

namespace vakkenranking
{
	class complete_parser
	{
		static evaluation parse_filename(const std::string& str)
		{
			//Antwoorden_Vragenlijst_NWI-I00032 (2013-01-29)_67288_Geavanceerd Programmeren.xls.csv
			//Results_survey_NWI-IBC017_2014-11-04_758478_Calculus and Probability Theory.csv
			const static boost::regex r1(".*_(?:NWI-)?([^\\_^-]+) \\((.+)\\)_([0-9]+)_(.+)\\.csv");
			const static boost::regex r2(".*_(?:NWI-)?([^\\_^-]+)_(.+)_([0-9]+)_(.+)\\.csv");

			boost::smatch match;
			if(!boost::regex_match(str, match, r1) && !boost::regex_match(str, match, r2))
				throw std::runtime_error(std::string("Can not parse filename (complete): ") + str);

			return evaluation(match[1], match[4], match[3], match[2]);
		}

		static size_t find_course_grade_key(const std::vector<std::string>& line, const std::string& path)
		{
			const static std::vector<std::string> keys = {
				"[Ik geef deze cursus het volgende rapportcijfer] Ik geef deze cursus het volgende rapportcijfer",
				"Ik geef deze cursus het volgende rapportcijfer [Ik geef deze cursus het volgende rapportcijfer]",
				"[I would rate this course] On a scale from 1 to 10 (10 being excellent) I would rate this course",
				"On a scale from 1 to 10 (10 being excellent) I would rate this course [I would rate this course]"
			};

			return find_key(line, keys, path, "course_grade");
		}

		static size_t find_teacher_grade_key(const std::vector<std::string>& line, const std::string& path)
		{
			const static std::vector<std::string> keys = {
				"[Ik geef de docent(en) het volgende rapportcijfer] Ik geef de docent(en) het volgende rapportcijfer",
				"Ik geef de docent(en) het volgende rapportcijfer [Ik geef de docent(en) het volgende rapportcijfer]",
				"[I would rate the lecturer(s)/teacher(s)] On a scale from 1 to 10 (10 being excellent) I would rate the lecturer(s)/teacher(s)",
				"On a scale from 1 to 10 (10 being excellent) I would rate the lecturer(s)/teacher(s) [I would rate the lecturer(s)/teacher(s)]"
			};

			return find_key(line, keys, path, "teacher_grade");
		}

		static size_t find_key(const std::vector<std::string>& line, const std::vector<std::string>& keys, const std::string& path, const std::string& function_name)
		{
			for(const auto& key : keys)
			{
				size_t key_i = std::distance(line.begin(), std::find(line.begin(), line.end(), key));
				if(key_i < line.size())
					return key_i;
			}

			throw std::runtime_error(std::string("Can not find key for ") + function_name + " in " + path);
		}

		static void parse_file(evaluation& e, const std::string& path)
		{
			csv_parser parser(path);
			size_t course_grade_key_i, teacher_grade_key_i;

			{
				std::vector<std::string> line;
				if(!parser.read(line))
					throw std::runtime_error("No headerline");

				course_grade_key_i = find_course_grade_key(line, path);
				teacher_grade_key_i = find_teacher_grade_key(line, path);
			}

			size_t i = 0;
			for(std::vector<std::string> line; parser.read(line); ++i)
			{
				if(line.size() > 1)
				{
					if(line.at(course_grade_key_i) != "") //Answer has not been filled in
						e.course_grade.ratings.push_back(boost::lexical_cast<double>(line.at(course_grade_key_i)));

					if(line.at(teacher_grade_key_i) != "") //Answer has not been filled in
						e.teacher_grade.ratings.push_back(boost::lexical_cast<double>(line.at(teacher_grade_key_i)));
				}
			}
		}

		void operator=(complete_parser&) = delete;
		complete_parser() = delete;

	public:
		static evaluation parse(const boost::filesystem::path& p)
		{
			evaluation e = parse_filename(p.filename().string());
			parse_file(e, p.string());
			return e;
		}
	};
}
<|endoftext|>


<commit_before>#include "EdgeRankToufiq.h"

using namespace NeuroProof;

EdgeRankToufiq::EdgeRankToufiq(BioStack* pstack, Rag_t& prag): EdgeRank(&prag)
{
    ils = new IterativeLearn_semi(pstack);
    maxEdges = 5000;
    subset_sz= 10;

    num_processed = 0;
    ils->get_initial_edges(new_idx);
    ils->edgelist_from_index(new_idx, edgebuffer);
    new_lbl.resize(edgebuffer.size());
    edge_ptr = 0;
    printf("Edge Rank initialized\n");

    ils->get_extra_edges(backup_idx, subset_sz);
    ils->edgelist_from_index(backup_idx, backupbuffer);

    threadp = NULL;

};

bool EdgeRankToufiq::get_top_edge(NodePair& top_edge_){
    if(edgebuffer.size() < 1)
	return false;

    std::pair<Node_t, Node_t> pair1 = edgebuffer[edge_ptr];
    Node_t node1 = pair1.first;
    Node_t node2 = pair1.second;
    boost::get<0>(top_edge_) =  node1;
    boost::get<1>(top_edge_) =  node2;

    return true;
}
void EdgeRankToufiq::repopulate_buffer()
{

    printf("repopulate buffer\n");
    ils->update_new_labels(tmp_idx, tmp_lbl);
    ils->get_next_edge_set(subset_sz, backup_idx);
    ils->edgelist_from_index(backup_idx, backupbuffer);

}
bool EdgeRankToufiq::is_finished(){

    if(num_processed>=maxEdges){
	printf("Max number of edges reached, save ssession\n");
	return true;
    }
    if (edge_ptr >= edgebuffer.size()){
	if (new_lbl.size() != new_idx.size()){
	    printf("size different: new_idx, new_lbl\n");
	    return false;
	}
	tmp_idx = new_idx;
	tmp_lbl = new_lbl;
	repopulate_buffer();
	edgebuffer = backupbuffer;
	if (edgebuffer.size()<1)
	    return true;
	new_idx = backup_idx;
	new_lbl.resize(edgebuffer.size());
	edge_ptr = 0;

// // // // 	ils->update_new_labels(new_idx, new_lbl);
// // // 	tmp_idx = new_idx;
// // // 	tmp_lbl = new_lbl;
// // //
// // // 	if (threadp)
// // // 	  threadp->join();
// // // 	edgebuffer = backupbuffer;
// // // 	new_idx = backup_idx;
// // // 	new_lbl.resize(edgebuffer.size());
// // // 	edge_ptr = 0;
// // // 	if (edgebuffer.size()<1)
// // // 	    return true;
// // // 	if (threadp)
// // // 	  delete threadp;
// // // // 	repopulate_buffer();
// // // 	threadp = new boost::thread(&EdgeRankToufiq::repopulate_buffer, this);

   }
   return false;
}

void EdgeRankToufiq::set_label(int plbl) {

    printf("egde %d labeled as %d\n",num_processed,plbl);
    new_lbl[edge_ptr++] = plbl;
    num_processed++;

};
<commit_msg>fix max edges to train<commit_after>#include "EdgeRankToufiq.h"

using namespace NeuroProof;

EdgeRankToufiq::EdgeRankToufiq(BioStack* pstack, Rag_t& prag): EdgeRank(&prag)
{
    ils = new IterativeLearn_semi(pstack);
    maxEdges = 50000;
    subset_sz= 10;

    num_processed = 0;
    ils->get_initial_edges(new_idx);
    ils->edgelist_from_index(new_idx, edgebuffer);
    new_lbl.resize(edgebuffer.size());
    edge_ptr = 0;
    printf("Edge Rank initialized\n");

    ils->get_extra_edges(backup_idx, subset_sz);
    ils->edgelist_from_index(backup_idx, backupbuffer);

    threadp = NULL;

};

bool EdgeRankToufiq::get_top_edge(NodePair& top_edge_){
    if(edgebuffer.size() < 1)
	return false;

    std::pair<Node_t, Node_t> pair1 = edgebuffer[edge_ptr];
    Node_t node1 = pair1.first;
    Node_t node2 = pair1.second;
    boost::get<0>(top_edge_) =  node1;
    boost::get<1>(top_edge_) =  node2;

    return true;
}
void EdgeRankToufiq::repopulate_buffer()
{

    printf("repopulate buffer\n");
    ils->update_new_labels(tmp_idx, tmp_lbl);
    ils->get_next_edge_set(subset_sz, backup_idx);
    ils->edgelist_from_index(backup_idx, backupbuffer);

}
bool EdgeRankToufiq::is_finished(){

    if(num_processed>=maxEdges){
	printf("Max number of edges reached, save ssession\n");
	return true;
    }
    if (edge_ptr >= edgebuffer.size()){
	if (new_lbl.size() != new_idx.size()){
	    printf("size different: new_idx, new_lbl\n");
	    return false;
	}
	tmp_idx = new_idx;
	tmp_lbl = new_lbl;
	repopulate_buffer();
	edgebuffer = backupbuffer;
	if (edgebuffer.size()<1)
	    return true;
	new_idx = backup_idx;
	new_lbl.resize(edgebuffer.size());
	edge_ptr = 0;

// // // // 	ils->update_new_labels(new_idx, new_lbl);
// // // 	tmp_idx = new_idx;
// // // 	tmp_lbl = new_lbl;
// // //
// // // 	if (threadp)
// // // 	  threadp->join();
// // // 	edgebuffer = backupbuffer;
// // // 	new_idx = backup_idx;
// // // 	new_lbl.resize(edgebuffer.size());
// // // 	edge_ptr = 0;
// // // 	if (edgebuffer.size()<1)
// // // 	    return true;
// // // 	if (threadp)
// // // 	  delete threadp;
// // // // 	repopulate_buffer();
// // // 	threadp = new boost::thread(&EdgeRankToufiq::repopulate_buffer, this);

   }
   return false;
}

void EdgeRankToufiq::set_label(int plbl) {

    printf("egde %d labeled as %d\n",num_processed,plbl);
    new_lbl[edge_ptr++] = plbl;
    num_processed++;

};
<|endoftext|>


<commit_before>/*=========================================================================
 *
 *  Copyright Insight Software Consortium
 *
 *  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.txt
 *
 *  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.
 *
 *=========================================================================*/
/*=========================================================================
 *
 *  Portions of this file are subject to the VTK Toolkit Version 3 copyright.
 *
 *  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
 *
 *  For complete copyright, license and disclaimer of warranty information
 *  please refer to the NOTICE file at the top of the ITK source tree.
 *
 *=========================================================================*/
#ifndef itkIntensityWindowingImageFilter_hxx
#define itkIntensityWindowingImageFilter_hxx

#include "itkIntensityWindowingImageFilter.h"
#include "itkMinimumMaximumImageCalculator.h"

namespace itk
{
/**
 *
 */
template< typename TInputImage, typename TOutputImage >
IntensityWindowingImageFilter< TInputImage, TOutputImage >
::IntensityWindowingImageFilter()
{
  m_OutputMaximum   = NumericTraits< OutputPixelType >::max();
  m_OutputMinimum   = NumericTraits< OutputPixelType >::NonpositiveMin();

  m_WindowMaximum   = NumericTraits< InputPixelType >::max();
  m_WindowMinimum   = NumericTraits< InputPixelType >::NonpositiveMin();

  m_Scale = 1.0;
  m_Shift = 0.0;
}

/**
 *
 */
template< typename TInputImage, typename TOutputImage >
void
IntensityWindowingImageFilter< TInputImage, TOutputImage >
::SetWindowLevel(const InputPixelType & window, const InputPixelType & level)
{
  typedef typename NumericTraits< InputPixelType >::RealType InputRealType;
  InputRealType tmp1, tmp2;

  tmp1 = static_cast< InputRealType >( level )
         - ( static_cast< InputRealType >( window ) / 2.0 );
  if ( tmp1 < NumericTraits< InputPixelType >::NonpositiveMin() )
    {
    tmp1 = 0.0;
    }

  tmp2 = static_cast< InputRealType >( level )
         + ( static_cast< InputRealType >( window ) / 2.0 );
  if ( tmp2 > NumericTraits< InputPixelType >::max() )
    {
    tmp2 = NumericTraits< InputPixelType >::max();
    }

  m_WindowMinimum = static_cast< InputPixelType >( tmp1 );
  m_WindowMaximum = static_cast< InputPixelType >( tmp2 );
}

/**
 *
 */
template< typename TInputImage, typename TOutputImage >
typename IntensityWindowingImageFilter< TInputImage, TOutputImage >::InputPixelType
IntensityWindowingImageFilter< TInputImage, TOutputImage >
::GetWindow() const
{
  return m_WindowMaximum - m_WindowMinimum;
}

/**
 *
 */
template< typename TInputImage, typename TOutputImage >
typename IntensityWindowingImageFilter< TInputImage, TOutputImage >::InputPixelType
IntensityWindowingImageFilter< TInputImage, TOutputImage >
::GetLevel() const
{
  return ( m_WindowMaximum + m_WindowMinimum ) / 2;
}

/**
 *
 */
template< typename TInputImage, typename TOutputImage >
void
IntensityWindowingImageFilter< TInputImage, TOutputImage >
::PrintSelf(std::ostream & os, Indent indent) const
{
  Superclass::PrintSelf(os, indent);

  os << indent << "Output Minimum: "
     << static_cast< typename NumericTraits< OutputPixelType >::PrintType >( m_OutputMinimum )
     << std::endl;
  os << indent << "Output Maximum: "
     << static_cast< typename NumericTraits< OutputPixelType >::PrintType >( m_OutputMaximum )
     << std::endl;
  os << indent << "Window Minimum: "
     << static_cast< typename NumericTraits< InputPixelType >::PrintType >( m_WindowMinimum )
     << std::endl;
  os << indent << "Window Maximum: "
     << static_cast< typename NumericTraits< InputPixelType >::PrintType >( m_WindowMaximum )
     << std::endl;
  os << indent << "Scale Factor: "
     << static_cast< typename NumericTraits< RealType >::PrintType >( m_Scale )
     << std::endl;
  os << indent << "Shift offset: "
     << static_cast< typename NumericTraits< RealType >::PrintType >( m_Shift )
     << std::endl;
}

/**
 *
 */
template< typename TInputImage, typename TOutputImage >
void
IntensityWindowingImageFilter< TInputImage, TOutputImage >
::BeforeThreadedGenerateData()
{
  m_Scale =
    ( static_cast< RealType >( m_OutputMaximum )
      - static_cast< RealType >( m_OutputMinimum ) )
    / ( static_cast< RealType >( m_WindowMaximum )
        - static_cast< RealType >( m_WindowMinimum ) );

  m_Shift =
    static_cast< RealType >( m_OutputMinimum )
    - static_cast< RealType >( m_WindowMinimum ) * m_Scale;

  // set up the functor values
  this->GetFunctor().SetOutputMinimum(m_OutputMinimum);
  this->GetFunctor().SetOutputMaximum(m_OutputMaximum);

  this->GetFunctor().SetWindowMinimum(m_WindowMinimum);
  this->GetFunctor().SetWindowMaximum(m_WindowMaximum);

  this->GetFunctor().SetFactor(m_Scale);
  this->GetFunctor().SetOffset(m_Shift);
}
} // end namespace itk

#endif
<commit_msg>STYLE: Improve the itkIntensityWindowingImageFilter style<commit_after>/*=========================================================================
 *
 *  Copyright Insight Software Consortium
 *
 *  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.txt
 *
 *  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.
 *
 *=========================================================================*/
/*=========================================================================
 *
 *  Portions of this file are subject to the VTK Toolkit Version 3 copyright.
 *
 *  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
 *
 *  For complete copyright, license and disclaimer of warranty information
 *  please refer to the NOTICE file at the top of the ITK source tree.
 *
 *=========================================================================*/
#ifndef itkIntensityWindowingImageFilter_hxx
#define itkIntensityWindowingImageFilter_hxx

#include "itkIntensityWindowingImageFilter.h"
#include "itkMinimumMaximumImageCalculator.h"

namespace itk
{

template< typename TInputImage, typename TOutputImage >
IntensityWindowingImageFilter< TInputImage, TOutputImage >
::IntensityWindowingImageFilter()
{
  this->m_OutputMaximum = NumericTraits< OutputPixelType >::max();
  this->m_OutputMinimum = NumericTraits< OutputPixelType >::NonpositiveMin();

  this->m_WindowMaximum = NumericTraits< InputPixelType >::max();
  this->m_WindowMinimum = NumericTraits< InputPixelType >::NonpositiveMin();

  this->m_Scale = 1.0;
  this->m_Shift = 0.0;
}

template< typename TInputImage, typename TOutputImage >
void
IntensityWindowingImageFilter< TInputImage, TOutputImage >
::SetWindowLevel(const InputPixelType & window, const InputPixelType & level)
{
  typedef typename NumericTraits< InputPixelType >::RealType InputRealType;
  InputRealType tmp1, tmp2;

  tmp1 = static_cast< InputRealType >( level )
         - ( static_cast< InputRealType >( window ) / 2.0 );
  if ( tmp1 < NumericTraits< InputPixelType >::NonpositiveMin() )
    {
    tmp1 = 0.0;
    }

  tmp2 = static_cast< InputRealType >( level )
         + ( static_cast< InputRealType >( window ) / 2.0 );
  if ( tmp2 > NumericTraits< InputPixelType >::max() )
    {
    tmp2 = NumericTraits< InputPixelType >::max();
    }

  this->m_WindowMinimum = static_cast< InputPixelType >( tmp1 );
  this->m_WindowMaximum = static_cast< InputPixelType >( tmp2 );
}

template< typename TInputImage, typename TOutputImage >
typename IntensityWindowingImageFilter< TInputImage, TOutputImage >::InputPixelType
IntensityWindowingImageFilter< TInputImage, TOutputImage >
::GetWindow() const
{
  return this->m_WindowMaximum - this->m_WindowMinimum;
}

template< typename TInputImage, typename TOutputImage >
typename IntensityWindowingImageFilter< TInputImage, TOutputImage >::InputPixelType
IntensityWindowingImageFilter< TInputImage, TOutputImage >
::GetLevel() const
{
  return ( this->m_WindowMaximum + this->m_WindowMinimum ) / 2;
}

template< typename TInputImage, typename TOutputImage >
void
IntensityWindowingImageFilter< TInputImage, TOutputImage >
::BeforeThreadedGenerateData()
{
  this->m_Scale =
    ( static_cast< RealType >( this->m_OutputMaximum )
      - static_cast< RealType >( this->m_OutputMinimum ) )
    / ( static_cast< RealType >( this->m_WindowMaximum )
        - static_cast< RealType >( this->m_WindowMinimum ) );

  this->m_Shift =
    static_cast< RealType >( this->m_OutputMinimum )
    - static_cast< RealType >( this->m_WindowMinimum ) * this->m_Scale;

  // set up the functor values
  this->GetFunctor().SetOutputMinimum( this->m_OutputMinimum );
  this->GetFunctor().SetOutputMaximum( this->m_OutputMaximum );

  this->GetFunctor().SetWindowMinimum( this->m_WindowMinimum );
  this->GetFunctor().SetWindowMaximum( this->m_WindowMaximum );

  this->GetFunctor().SetFactor( this->m_Scale );
  this->GetFunctor().SetOffset( this->m_Shift );
}

template< typename TInputImage, typename TOutputImage >
void
IntensityWindowingImageFilter< TInputImage, TOutputImage >
::PrintSelf(std::ostream & os, Indent indent) const
{
  Superclass::PrintSelf(os, indent);

  os << indent << "Output Minimum: "
     << static_cast< typename NumericTraits< OutputPixelType >::PrintType >( this->m_OutputMinimum )
     << std::endl;
  os << indent << "Output Maximum: "
     << static_cast< typename NumericTraits< OutputPixelType >::PrintType >( this->m_OutputMaximum )
     << std::endl;
  os << indent << "Window Minimum: "
     << static_cast< typename NumericTraits< InputPixelType >::PrintType >( this->m_WindowMinimum )
     << std::endl;
  os << indent << "Window Maximum: "
     << static_cast< typename NumericTraits< InputPixelType >::PrintType >( this->m_WindowMaximum )
     << std::endl;
  os << indent << "Scale Factor: "
     << static_cast< typename NumericTraits< RealType >::PrintType >( this->m_Scale )
     << std::endl;
  os << indent << "Shift offset: "
     << static_cast< typename NumericTraits< RealType >::PrintType >( this->m_Shift )
     << std::endl;
}

} // end namespace itk

#endif
<|endoftext|>