<commit_before>/*
 * fairness_test.cpp
 */
#include <chrono>
#include <iostream>
#include <mutex>
#include <thread>
#include "gtest/gtest.h"
#include "fair_mutex.hpp"
#include "yamc_testutil.hpp"

#define WAIT_TICK \
  std::this_thread::sleep_for(std::chrono::milliseconds(200))

#if 0
namespace {
std::mutex g_guard;
#define TRACE(msg_) \
  (std::unique_lock<std::mutex>(g_guard),\
   std::cout << std::this_thread::get_id() << ':' << (msg_) << std::endl)
}
#else
#define TRACE(msg_)
#endif

using FairMutexTypes = ::testing::Types<
  yamc::fair::mutex,
  yamc::fair::recursive_mutex
>;

template <typename Mutex>
struct FairMutexTest : ::testing::Test {};

TYPED_TEST_CASE(FairMutexTest, FairMutexTypes);

// FIFO scheduling
TYPED_TEST(FairMutexTest, FifoSched)
{
  yamc::test::phaser phaser(3);
  int step = 0;
  TypeParam mtx;
  yamc::test::join_thread thd0([&]{
    auto ph = phaser.get(0);
    WAIT_TICK;
    TRACE("request... L1");
    mtx.lock();                 // 1) acquire 1st lock
    TRACE("acquire L1");
    WAIT_TICK;
    ph.advance(2);
    WAIT_TICK;
    ph.await();
    WAIT_TICK;
    EXPECT_EQ(1, ++step);
    mtx.unlock();               // 4) unlock
    TRACE("release L1");
  });
  yamc::test::join_thread thd1([&]{
    auto ph = phaser.get(1);
    WAIT_TICK;
    ph.await();
    WAIT_TICK;
    ph.advance(2);
    WAIT_TICK;
    TRACE("request... L2");
    mtx.lock();                 // 2) request 2nd lock
    TRACE("acquire L2");
    EXPECT_EQ(2, ++step);       // 5) acquire 2nd lock (before 3rd lock)
    WAIT_TICK;
    mtx.unlock();               // 6) unlock
    TRACE("release L2");
  });
  {
    auto ph = phaser.get(2);
    WAIT_TICK;
    ph.await();
    WAIT_TICK;
    ph.await();
    WAIT_TICK;
    ph.advance(1);
    WAIT_TICK;
    TRACE("request... L3");
    mtx.lock();                 // 3) request 3rd lock
    TRACE("acquire L3");
    EXPECT_EQ(3, ++step);       // 7) acquire 3rd lock
    WAIT_TICK;
    mtx.unlock();
    TRACE("release L3");
  }
}
<commit_msg>refine FairMutexTest test<commit_after>/*
 * fairness_test.cpp
 */
#include <chrono>
#include <iostream>
#include <mutex>
#include <thread>
#include "gtest/gtest.h"
#include "fair_mutex.hpp"
#include "yamc_testutil.hpp"

#define TEST_TICKS std::chrono::milliseconds(200)

#define EXPECT_STEP(n_) \
  { EXPECT_EQ(n_, ++step); std::this_thread::sleep_for(TEST_TICKS); }

#if 0
namespace {
std::mutex g_guard;
#define TRACE(msg_) \
  (std::unique_lock<std::mutex>(g_guard),\
   std::cout << std::this_thread::get_id() << ':' << (msg_) << std::endl)
}
#else
#define TRACE(msg_)
#endif

using FairMutexTypes = ::testing::Types<
  yamc::fair::mutex,
  yamc::fair::recursive_mutex
>;

template <typename Mutex>
struct FairMutexTest : ::testing::Test {};

TYPED_TEST_CASE(FairMutexTest, FairMutexTypes);

// FIFO scheduling
TYPED_TEST(FairMutexTest, FifoSched)
{
  yamc::test::phaser phaser(3);
  int step = 0;
  TypeParam mtx;
  yamc::test::stopwatch<> sw;
  yamc::test::task_runner(3, [&](std::size_t id) {
    auto ph = phaser.get(id);
    switch (id) {
    case 0:
      TRACE("request... L1");
      mtx.lock();                 // 1) acquire 1st lock
      TRACE("acquire L1");
      EXPECT_STEP(1)
      ph.advance(2);
      ph.await();
      EXPECT_STEP(4)
      mtx.unlock();               // 4) unlock
      TRACE("release L1");
      break;
    case 1:
      ph.await();
      EXPECT_STEP(2)
      ph.advance(2);
      TRACE("request... L2");
      mtx.lock();                 // 2) request 2nd lock
      TRACE("acquire L2");
      EXPECT_STEP(5)              // 5) acquire 2nd lock (before 3rd lock)
      mtx.unlock();               // 6) unlock
      TRACE("release L2");
      break;
    case 2:
      ph.await();
      ph.await();
      EXPECT_STEP(3)
      ph.advance(1);
      TRACE("request... L3");
      mtx.lock();                 // 3) request 3rd lock
      TRACE("acquire L3");
      EXPECT_STEP(6)              // 7) acquire 3rd lock (after 2nd unlock)
      mtx.unlock();
      TRACE("release L3");
      break;
    }
  });
  ASSERT_LE(TEST_TICKS * step, sw.elapsed());
}
<|endoftext|>


<commit_before>/*
 * Copyright (C) 2021-present ScyllaDB
 */

/*
 * This file is part of Scylla.
 *
 * Scylla is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Scylla is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Scylla.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once
#include <type_traits>
#include <seastar/util/concepts.hh>

namespace utils {

/*
 * Wraps a collection into immutable form.
 *
 * Immutability here means that the collection itself cannot be modified,
 * i.e. adding or removing elements is not possible. Read-only methods such
 * as find(), begin()/end(), lower_bound(), etc. are available and are
 * transparently forwarded to the underlying collection. Return values from
 * those methods are also returned as-is so it's pretty much like a const
 * reference on the collection.
 *
 * The important difference from the const reference is that obtained
 * elements or iterators are not necessarily const too, so it's possible
 * to modify the found or iterated over elements.
 */

template <typename Collection>
class immutable_collection {
    Collection& _col;

public:
    immutable_collection(Collection& col) noexcept : _col(col) {}

#define DO_WRAP_METHOD(method, is_const)                                                                           \
    template <typename... Args>                                                                                \
    auto method(Args&&... args) is_const noexcept(noexcept(std::declval<is_const Collection>().method(args...))) { \
        return _col.method(std::forward<Args>(args)...);                                                           \
    }

#define WRAP_CONST_METHOD(method)    \
    DO_WRAP_METHOD(method, const)

#define WRAP_METHOD(method)          \
    WRAP_CONST_METHOD(method)        \
    DO_WRAP_METHOD(method, )

    WRAP_METHOD(find)
    WRAP_METHOD(lower_bound)
    WRAP_METHOD(upper_bound)
    WRAP_METHOD(slice)
    WRAP_METHOD(lower_slice)
    WRAP_METHOD(upper_slice)

    WRAP_CONST_METHOD(empty)
    WRAP_CONST_METHOD(size)
    WRAP_CONST_METHOD(calculate_size)
    WRAP_CONST_METHOD(external_memory_usage)

    WRAP_METHOD(begin)
    WRAP_METHOD(end)
    WRAP_METHOD(rbegin)
    WRAP_METHOD(rend)
    WRAP_CONST_METHOD(cbegin)
    WRAP_CONST_METHOD(cend)
    WRAP_CONST_METHOD(crbegin)
    WRAP_CONST_METHOD(crend)

#undef WRAP_METHOD
#undef WRAP_CONST_METHOD
#undef DO_WRAP_METHOD
};

} // namespace utils
<commit_msg>utils: immutable-collection: Make movable<commit_after>/*
 * Copyright (C) 2021-present ScyllaDB
 */

/*
 * This file is part of Scylla.
 *
 * Scylla is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Scylla is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Scylla.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once
#include <type_traits>
#include <seastar/util/concepts.hh>

namespace utils {

/*
 * Wraps a collection into immutable form.
 *
 * Immutability here means that the collection itself cannot be modified,
 * i.e. adding or removing elements is not possible. Read-only methods such
 * as find(), begin()/end(), lower_bound(), etc. are available and are
 * transparently forwarded to the underlying collection. Return values from
 * those methods are also returned as-is so it's pretty much like a const
 * reference on the collection.
 *
 * The important difference from the const reference is that obtained
 * elements or iterators are not necessarily const too, so it's possible
 * to modify the found or iterated over elements.
 */

template <typename Collection>
class immutable_collection {
    Collection* _col;

public:
    immutable_collection(Collection& col) noexcept : _col(&col) {}

#define DO_WRAP_METHOD(method, is_const)                                                                           \
    template <typename... Args>                                                                                \
    auto method(Args&&... args) is_const noexcept(noexcept(std::declval<is_const Collection>().method(args...))) { \
        return _col->method(std::forward<Args>(args)...);                                                           \
    }

#define WRAP_CONST_METHOD(method)    \
    DO_WRAP_METHOD(method, const)

#define WRAP_METHOD(method)          \
    WRAP_CONST_METHOD(method)        \
    DO_WRAP_METHOD(method, )

    WRAP_METHOD(find)
    WRAP_METHOD(lower_bound)
    WRAP_METHOD(upper_bound)
    WRAP_METHOD(slice)
    WRAP_METHOD(lower_slice)
    WRAP_METHOD(upper_slice)

    WRAP_CONST_METHOD(empty)
    WRAP_CONST_METHOD(size)
    WRAP_CONST_METHOD(calculate_size)
    WRAP_CONST_METHOD(external_memory_usage)

    WRAP_METHOD(begin)
    WRAP_METHOD(end)
    WRAP_METHOD(rbegin)
    WRAP_METHOD(rend)
    WRAP_CONST_METHOD(cbegin)
    WRAP_CONST_METHOD(cend)
    WRAP_CONST_METHOD(crbegin)
    WRAP_CONST_METHOD(crend)

#undef WRAP_METHOD
#undef WRAP_CONST_METHOD
#undef DO_WRAP_METHOD
};

} // namespace utils
<|endoftext|>


<commit_before>/*
-------------------------------------------------------------------------
   This file is part of BayesOpt, an efficient C++ library for
   Bayesian optimization.

   Copyright (C) 2011-2013 Ruben Martinez-Cantin <rmcantin@unizar.es>

   BayesOpt 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.

   BayesOpt is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with BayesOpt.  If not, see <http://www.gnu.org/licenses/>.
------------------------------------------------------------------------
*/

#include <ctime>
#include <fstream>
#include "testfunctions.hpp"

int main(int nargs, char *args[])
{
  bopt_params par = initialize_parameters_to_default();
  par.n_iterations = 190;
  par.verbose_level = 1;
  par.noise = 1e-10;
  par.force_jump = 30;

  std::ofstream log;
  std::clock_t start_t;

  /* Branin */
  log.open("branin.log");
  par.n_init_samples = 5;
  par.n_iterations = 190;

  for (size_t ii = 0; ii < 10; ++ii)
    {
      par.random_seed = ii;
      BraninNormalized branin(par);
      vectord result(2);

      start_t = clock();
      branin.initializeOptimization();

      for (size_t jj = 0; jj < par.n_iterations; ++jj)
  	{
  	  branin.stepOptimization();
  	  if (jj == 50)
  	    {
  	      result = branin.getFinalResult();
  	      log << branin.evaluateSample(result) << ", ";
  	    }
  	}
      result = branin.getFinalResult();
      log << branin.evaluateSample(result) << ", ";

      log << static_cast<double>(clock() - start_t) / static_cast<double>(CLOCKS_PER_SEC)
  	  << std::endl;
      }

  log.close();

  /* Camel */
  log.open("camel.log");
  par.n_init_samples = 5;
  par.n_iterations = 90;

  for (size_t ii = 0; ii < 10; ++ii)
    {
      par.random_seed = ii;
      ExampleCamelback camel(par);
      vectord result(2);

      vectord lb(2); lb(0) = -2; lb(1) = -1;
      vectord ub(2); ub(0) =  2; ub(1) = 1;

      camel.setBoundingBox(lb,ub);

      start_t = clock();
      camel.initializeOptimization();

      for (size_t jj = 0; jj < par.n_iterations; ++jj)
  	{
  	  camel.stepOptimization();
  	  if (jj == 50)
  	    {
  	      result = camel.getFinalResult();
  	      log << camel.evaluateSample(result) << ", ";
  	    }
  	}
      result = camel.getFinalResult();
      log << camel.evaluateSample(result) << ", ";

      log << static_cast<double>(clock() - start_t) / static_cast<double>(CLOCKS_PER_SEC)
  	  << std::endl;
      }

  log.close();

  /* Hart */
  log.open("hart.log");
  par.n_init_samples = 10;
  par.n_iterations = 190;

  for (size_t ii = 0; ii < 10; ++ii)
    {
      par.random_seed = ii;
      ExampleHartmann6 hart(par);
      vectord result(6);

      start_t = clock();
      hart.initializeOptimization();

      for (size_t jj = 0; jj < par.n_iterations; ++jj)
  	{
  	  hart.stepOptimization();
  	  if (jj == 50)
  	    {
  	      result = hart.getFinalResult();
  	      log << hart.evaluateSample(result) << ", ";
  	    }
  	}
      result = hart.getFinalResult();
      log << hart.evaluateSample(result) << ", ";

      log << static_cast<double>(clock() - start_t) / static_cast<double>(CLOCKS_PER_SEC)
  	  << std::endl;
      }

  log.close();

  /***********************************************************************/
  par.n_init_samples = 2;
  par.n_iter_relearn = 1;

  par.l_type = L_MCMC;
  par.sc_type = SC_MAP;

  /* Branin */
  log.open("branin_mcmc.log");

  for (size_t ii = 0; ii < 10; ++ii)
    {
      par.random_seed = ii;
      par.n_iterations = 190;
      BraninNormalized branin(par);
      vectord result(2);

      start_t = clock();
      branin.initializeOptimization();

      for (size_t jj = 0; jj < par.n_iterations; ++jj)
	{
	  branin.stepOptimization();
	  if (jj == 50)
	    {
	      result = branin.getFinalResult();
	      log << branin.evaluateSample(result) << ", ";
	    }
	}
      result = branin.getFinalResult();
      log << branin.evaluateSample(result) << ", ";

      log << static_cast<double>(clock() - start_t) / static_cast<double>(CLOCKS_PER_SEC)
	  << std::endl;
      }

  log.close();

  /* Camel */
  log.open("camel_mcmc.log");

  for (size_t ii = 0; ii < 10; ++ii)
    {
      par.random_seed = ii;
      par.n_iterations = 90;
      ExampleCamelback camel(par);
      vectord result(2);

      vectord lb(2); lb(0) = -2; lb(1) = -1;
      vectord ub(2); ub(0) =  2; ub(1) = 1;

      camel.setBoundingBox(lb,ub);

      start_t = clock();
      camel.initializeOptimization();

      for (size_t jj = 0; jj < par.n_iterations; ++jj)
	{
	  camel.stepOptimization();
	  if (jj == 50)
	    {
	      result = camel.getFinalResult();
	      log << camel.evaluateSample(result) << ", ";
	    }
	}
      result = camel.getFinalResult();
      log << camel.evaluateSample(result) << ", ";

      log << static_cast<double>(clock() - start_t) / static_cast<double>(CLOCKS_PER_SEC)
	  << std::endl;
      }

  log.close();

  /* Hart */
  log.open("hart_mcmc.log");

  for (size_t ii = 0; ii < 10; ++ii)
    {
      par.random_seed = ii;
      par.n_iterations = 190;
      ExampleHartmann6 hart(par);
      vectord result(6);

      start_t = clock();
      hart.initializeOptimization();

      for (size_t jj = 0; jj < par.n_iterations; ++jj)
	{
	  hart.stepOptimization();
	  if (jj == 50)
	    {
	      result = hart.getFinalResult();
	      log << hart.evaluateSample(result) << ", ";
	    }
	}
      result = hart.getFinalResult();
      log << hart.evaluateSample(result) << ", ";

      log << static_cast<double>(clock() - start_t) / static_cast<double>(CLOCKS_PER_SEC)
	  << std::endl;
      }

  log.close();

  return 0;
}
<commit_msg>Fixing minor bug<commit_after>/*
-------------------------------------------------------------------------
   This file is part of BayesOpt, an efficient C++ library for
   Bayesian optimization.

   Copyright (C) 2011-2013 Ruben Martinez-Cantin <rmcantin@unizar.es>

   BayesOpt 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.

   BayesOpt is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with BayesOpt.  If not, see <http://www.gnu.org/licenses/>.
------------------------------------------------------------------------
*/

#include <ctime>
#include <fstream>
#include "testfunctions.hpp"

int main(int nargs, char *args[])
{
  bopt_params par = initialize_parameters_to_default();
  par.verbose_level = 0;
  par.noise = 1e-10;
  par.force_jump = 30;

  std::ofstream log;
  std::clock_t start_t;

  /* Branin */
  log.open("branin.log");
  par.n_init_samples = 5;
  par.n_iterations = 195;

  for (size_t ii = 0; ii < 10; ++ii)
    {
      par.random_seed = ii;
      BraninNormalized branin(par);
      vectord result(2);

      start_t = clock();
      branin.initializeOptimization();

      for (size_t jj = 0; jj < par.n_iterations; ++jj)
  	{
  	  branin.stepOptimization();
  	  if (jj == 50)
  	    {
  	      result = branin.getFinalResult();
  	      log << branin.evaluateSample(result) << ", ";
  	    }
  	}
      result = branin.getFinalResult();
      log << branin.evaluateSample(result) << ", ";

      log << static_cast<double>(clock() - start_t) / static_cast<double>(CLOCKS_PER_SEC)
  	  << std::endl;
      }

  log.close();

  /* Camel */
  log.open("camel.log");
  par.n_init_samples = 5;
  par.n_iterations = 95;

  for (size_t ii = 0; ii < 10; ++ii)
    {
      par.random_seed = ii;
      ExampleCamelback camel(par);
      vectord result(2);

      vectord lb(2); lb(0) = -2; lb(1) = -1;
      vectord ub(2); ub(0) =  2; ub(1) = 1;

      camel.setBoundingBox(lb,ub);

      start_t = clock();
      camel.initializeOptimization();

      for (size_t jj = 0; jj < par.n_iterations; ++jj)
  	{
  	  camel.stepOptimization();
  	  if (jj == 50)
  	    {
  	      result = camel.getFinalResult();
  	      log << camel.evaluateSample(result) << ", ";
  	    }
  	}
      result = camel.getFinalResult();
      log << camel.evaluateSample(result) << ", ";

      log << static_cast<double>(clock() - start_t) / static_cast<double>(CLOCKS_PER_SEC)
  	  << std::endl;
      }

  log.close();

  /* Hart */
  log.open("hart.log");
  par.n_init_samples = 10;
  par.n_iterations = 190;

  for (size_t ii = 0; ii < 10; ++ii)
    {
      par.random_seed = ii;
      ExampleHartmann6 hart(par);
      vectord result(6);

      start_t = clock();
      hart.initializeOptimization();

      for (size_t jj = 0; jj < par.n_iterations; ++jj)
  	{
  	  hart.stepOptimization();
  	  if (jj == 50)
  	    {
  	      result = hart.getFinalResult();
  	      log << hart.evaluateSample(result) << ", ";
  	    }
  	}
      result = hart.getFinalResult();
      log << hart.evaluateSample(result) << ", ";

      log << static_cast<double>(clock() - start_t) / static_cast<double>(CLOCKS_PER_SEC)
  	  << std::endl;
      }

  log.close();

  /***********************************************************************/
  par.n_init_samples = 2;
  par.n_iter_relearn = 1;

  par.l_type = L_MCMC;
  par.sc_type = SC_MAP;

  /* Branin */
  log.open("branin_mcmc.log");
  par.n_iterations = 198;

  for (size_t ii = 0; ii < 10; ++ii)
    {
      par.random_seed = ii;
      BraninNormalized branin(par);
      vectord result(2);

      start_t = clock();
      branin.initializeOptimization();

      for (size_t jj = 0; jj < par.n_iterations; ++jj)
	{
	  branin.stepOptimization();
	  if (jj == 50)
	    {
	      result = branin.getFinalResult();
	      log << branin.evaluateSample(result) << ", ";
	    }
	}
      result = branin.getFinalResult();
      log << branin.evaluateSample(result) << ", ";

      log << static_cast<double>(clock() - start_t) / static_cast<double>(CLOCKS_PER_SEC)
	  << std::endl;
      }

  log.close();

  /* Camel */
  log.open("camel_mcmc.log");
  par.n_iterations = 98;

  for (size_t ii = 0; ii < 10; ++ii)
    {
      par.random_seed = ii;
      ExampleCamelback camel(par);
      vectord result(2);

      vectord lb(2); lb(0) = -2; lb(1) = -1;
      vectord ub(2); ub(0) =  2; ub(1) = 1;

      camel.setBoundingBox(lb,ub);

      start_t = clock();
      camel.initializeOptimization();

      for (size_t jj = 0; jj < par.n_iterations; ++jj)
	{
	  camel.stepOptimization();
	  if (jj == 50)
	    {
	      result = camel.getFinalResult();
	      log << camel.evaluateSample(result) << ", ";
	    }
	}
      result = camel.getFinalResult();
      log << camel.evaluateSample(result) << ", ";

      log << static_cast<double>(clock() - start_t) / static_cast<double>(CLOCKS_PER_SEC)
	  << std::endl;
      }

  log.close();

  /* Hart */
  log.open("hart_mcmc.log");
  par.n_iterations = 198;

  for (size_t ii = 0; ii < 10; ++ii)
    {
      par.random_seed = ii;
      ExampleHartmann6 hart(par);
      vectord result(6);

      start_t = clock();
      hart.initializeOptimization();

      for (size_t jj = 0; jj < par.n_iterations; ++jj)
	{
	  hart.stepOptimization();
	  if (jj == 50)
	    {
	      result = hart.getFinalResult();
	      log << hart.evaluateSample(result) << ", ";
	    }
	}
      result = hart.getFinalResult();
      log << hart.evaluateSample(result) << ", ";

      log << static_cast<double>(clock() - start_t) / static_cast<double>(CLOCKS_PER_SEC)
	  << std::endl;
      }

  log.close();

  return 0;
}
<|endoftext|>


<commit_before>/*
 * Author: Jon Trulson <jtrulson@ics.com>
 * Copyright (c) 2016 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "stdio.h"
#include "iostream"
//! [Interesting]
#include "uart_ow.hpp"

using namespace std;

int
main(int argc, char** argv)
{
    mraa::UartOW* uart = new mraa::UartOW(0);

    // Reset the ow bus and see if anything is present
    mraa::Result rv;

    if ((rv = uart->reset()) == mraa::SUCCESS) {
        cout << "Reset succeeded, device(s) detected!" << endl;
    } else {
        cout << "Reset failed, returned " << int(rv) << ". No devices on bus?" << endl;
        return 1;
    }

    cout << "Looking for devices..." << endl;
    ;

    uint8_t count = 0;
    // start the search from scratch
    string id = uart->search(true);

    if (id.empty()) {
        cout << "No devices detected." << endl;
        return 1;
    }

    while (!id.empty()) {
        // hack so we don't need to cast each element of the romcode
        // for printf purposes
        uint8_t* ptr = (uint8_t*) id.c_str();

        // The first byte (0) is the device type (family) code.
        // The last byte (7) is the rom code CRC value.  The
        // intervening bytes are the unique 48 bit device ID.

        printf("Device %02d Type 0x%02x ID %02x%02x%02x%02x%02x%02x CRC 0x%02x\n", count, ptr[0],
               ptr[6], ptr[5], ptr[4], ptr[3], ptr[2], ptr[1], ptr[7]);
        count++;

        // continue the search with start argument set to false
        id = uart->search(false);
    }

    cout << "Exiting..." << endl;

    delete uart;

    return 0;
}
//! [Interesting]
<commit_msg>UartOW.cpp example: Use .data() string accessor instead of .c_str().<commit_after>/*
 * Author: Jon Trulson <jtrulson@ics.com>
 * Copyright (c) 2016 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "stdio.h"
#include "iostream"
//! [Interesting]
#include "uart_ow.hpp"

using namespace std;

int
main(int argc, char** argv)
{
    mraa::UartOW* uart = new mraa::UartOW(0);

    // Reset the ow bus and see if anything is present
    mraa::Result rv;

    if ((rv = uart->reset()) == mraa::SUCCESS) {
        cout << "Reset succeeded, device(s) detected!" << endl;
    } else {
        cout << "Reset failed, returned " << int(rv) << ". No devices on bus?" << endl;
        return 1;
    }

    cout << "Looking for devices..." << endl;
    ;

    uint8_t count = 0;
    // start the search from scratch
    string id = uart->search(true);

    if (id.empty()) {
        cout << "No devices detected." << endl;
        return 1;
    }

    while (!id.empty()) {
        // hack so we don't need to cast each element of the romcode
        // for printf purposes
        uint8_t* ptr = (uint8_t*) id.data();

        // The first byte (0) is the device type (family) code.
        // The last byte (7) is the rom code CRC value.  The
        // intervening bytes are the unique 48 bit device ID.

        printf("Device %02d Type 0x%02x ID %02x%02x%02x%02x%02x%02x CRC 0x%02x\n", count, ptr[0],
               ptr[6], ptr[5], ptr[4], ptr[3], ptr[2], ptr[1], ptr[7]);
        count++;

        // continue the search with start argument set to false
        id = uart->search(false);
    }

    cout << "Exiting..." << endl;

    delete uart;

    return 0;
}
//! [Interesting]
<|endoftext|>


<commit_before>// RUN: clang -parse-noop %s -verify

void f() {
  int a;
  while (a) ;
  while (int x) ; // expected-error {{expected '=' after declarator}}
  while (float x = 0) ;
  if (const int x = a) ;
  switch (int x = a+10) {}
  for (; int x = ++a; ) ;
}
<commit_msg>Fix test case.<commit_after>// RUN: clang -parse-noop -verify %s

void f() {
  int a;
  while (a) ;
  while (int x) ; // expected-error {{expected '=' after declarator}}
  while (float x = 0) ;
  if (const int x = a) ;
  switch (int x = a+10) {}
  for (; int x = ++a; ) ;
}
<|endoftext|>


<commit_before><commit_msg>vcl: avoid possible leaks in case of unexpected exceptions<commit_after><|endoftext|>


<commit_before>#include "ESP8266_AT.h"

ESP8266_AT::ESP8266_AT(uint32_t rxPin, uint32_t txPin, uint32_t baud) :
    m_rxPin(rxPin), m_txPin(txPin)
{
    SoftwareSerial *serial = new SoftwareSerial(rxPin, txPin);
    serial->begin(baud);
    m_serial = serial;
}

ESP8266_AT::ESP8266_AT(SoftwareSerial &serial) :
    m_rxPin(0), m_txPin(0), m_serial(&serial)
{
}

ESP8266_AT::ESP8266_AT(HardwareSerial &serial) :
    m_rxPin(0), m_txPin(0), m_serial(&serial)
{
}

ESP8266_AT::~ESP8266_AT() {
    disconnectAP();
    if(m_rxPin != 0 && m_txPin !=0) delete m_serial;
}

void ESP8266_AT::rxClear() {
    while(m_serial->available() > 0) m_serial->read();
}

bool ESP8266_AT::checkATResponse(String *buf, String target, uint32_t timeout) {
    *buf = "";
    char c;
    unsigned long start = millis();
    while (millis() - start < timeout) {
        while(m_serial->available() > 0) {
            c = m_serial->read(); // 1 byte
            if(c == '\0') continue;
            *buf += c;
        }
        if (buf->indexOf(target) != -1) return true;
    }
    return false;
}

bool ESP8266_AT::checkATResponse(String target, uint32_t timeout) {
    String buf;
    return checkATResponse(&buf, target, timeout);
}

bool ESP8266_AT::statusAT() {
    rxClear();
    m_serial->println("AT");
    return checkATResponse();
}

bool ESP8266_AT::restart() {
    rxClear();
    m_serial->println("AT+RST");
    if(!checkATResponse()) return false;
    delay(2000);
    unsigned long start = millis();
    while(millis() - start < 3000) {
        if(statusAT()) {
            delay(1500);
            return true;
        }
        delay(100);
    }
    return false;
}

bool ESP8266_AT::connectAP(String ssid, String password) {
    rxClear();
    m_serial->println("AT+CWMODE_DEF=1"); // 1: station(client) mode, 2: softAP(server) mode, 3: 1&2
    if(!(checkATResponse() && restart())) return false; // change "DEF"ault cwMode and restart

    // Connect to an AP
    rxClear();
    m_serial->print("AT+CWJAP_DEF=\"");
    m_serial->print(ssid);
    m_serial->print("\",\"");
    m_serial->print(password);
    m_serial->println("\"");
    return checkATResponse("OK", 10000);
}

bool ESP8266_AT::disconnectAP() {
    rxClear();
    m_serial->println("AT+CWQAP");
    return checkATResponse();
}

uint8_t ESP8266_AT::ipStatus() {
    String buf;
    rxClear();
    m_serial->println("AT+CIPSTATUS");
    checkATResponse(&buf, "S:");
    uint32_t index = buf.indexOf(":");
    return buf.substring(index + 1, index + 2).toInt();
}

bool ESP8266_AT::statusWiFi() {
    return (ipStatus() != 5);
}

int ESP8266_AT::connect(const char *host, uint16_t port) {
    if(connected()) stop();
    String buf;
    uint8_t retry = 10;
    while(retry--) {
        rxClear();
        m_serial->print("AT+CIPSTART=\"TCP\",\"");
        m_serial->print(host);
        m_serial->print("\",");
        m_serial->println(port);
        checkATResponse(&buf);
        if(buf.indexOf("OK") != -1 || buf.indexOf("ALREADY") != -1) {
            return 1; // SUCCESS
        }
        delay(100);
    }
    return -1; // TIMED_OUT
}

int ESP8266_AT::connect(IPAddress ip, uint16_t port) {
    String host = "";
    for(uint8_t i = 0; i < 4;) {
        host += String(ip[i]);
        if(++i < 4) host += ".";
    }
    return connect(host.c_str(), port);
}

void ESP8266_AT::stop() {
    rxClear();
    m_serial->println("AT+CIPCLOSE");
    checkATResponse();
}

uint8_t ESP8266_AT::connected() {
    uint8_t retry = 5;
    while(retry--) {
        if(ipStatus() == 3) return 1;
        delay(100);
    }
    return 0;
}

size_t ESP8266_AT::write(const uint8_t *buf, size_t size) {
    return 0; // TODO
}

size_t ESP8266_AT::write(uint8_t) {
    return 0; // TODO
}

int ESP8266_AT::available() {
    return 0; // TODO
}

int ESP8266_AT::read() {
    return 0; // TODO
}

int ESP8266_AT::read(uint8_t *buf, size_t size) {
    return 0; // TODO
}

int ESP8266_AT::peek() {
    return 0; // TODO
}

void ESP8266_AT::flush() {
    // TODO
}

ESP8266_AT::operator bool() {
    // TODO
}
<commit_msg>Added some retry functionality for some unstable network methods<commit_after>#include "ESP8266_AT.h"

ESP8266_AT::ESP8266_AT(uint32_t rxPin, uint32_t txPin, uint32_t baud) :
    m_rxPin(rxPin), m_txPin(txPin)
{
    SoftwareSerial *serial = new SoftwareSerial(rxPin, txPin);
    serial->begin(baud);
    m_serial = serial;
}

ESP8266_AT::ESP8266_AT(SoftwareSerial &serial) :
    m_rxPin(0), m_txPin(0), m_serial(&serial)
{
}

ESP8266_AT::ESP8266_AT(HardwareSerial &serial) :
    m_rxPin(0), m_txPin(0), m_serial(&serial)
{
}

ESP8266_AT::~ESP8266_AT() {
    disconnectAP();
    if(m_rxPin != 0 && m_txPin !=0) delete m_serial;
}

void ESP8266_AT::rxClear() {
    while(m_serial->available() > 0) m_serial->read();
}

bool ESP8266_AT::checkATResponse(String *buf, String target, uint32_t timeout) {
    *buf = "";
    char c;
    unsigned long start = millis();
    while (millis() - start < timeout) {
        while(m_serial->available() > 0) {
            c = m_serial->read(); // 1 byte
            if(c == '\0') continue;
            *buf += c;
        }
        if (buf->indexOf(target) != -1) return true;
    }
    return false;
}

bool ESP8266_AT::checkATResponse(String target, uint32_t timeout) {
    String buf;
    return checkATResponse(&buf, target, timeout);
}

bool ESP8266_AT::statusAT() {
    rxClear();
    m_serial->println("AT");
    return checkATResponse();
}

bool ESP8266_AT::restart() {
    rxClear();
    m_serial->println("AT+RST");
    if(!checkATResponse()) return false;
    delay(2000);
    unsigned long start = millis();
    while(millis() - start < 3000) {
        if(statusAT()) {
            delay(1500);
            return true;
        }
        delay(100);
    }
    return false;
}

bool ESP8266_AT::connectAP(String ssid, String password) {
    rxClear();
    m_serial->println("AT+CWMODE_DEF=1"); // 1: station(client) mode, 2: softAP(server) mode, 3: 1&2
    if(!(checkATResponse() && restart())) return false; // change "DEF"ault cwMode and restart

    uint8_t retry = 5;
    while(retry--) {
        // Connect to an AP
        rxClear();
        m_serial->print("AT+CWJAP_DEF=\"");
        m_serial->print(ssid);
        m_serial->print("\",\"");
        m_serial->print(password);
        m_serial->println("\"");
        if(checkATResponse("OK", 10000)) return true;
        delay(100);
    }
    return false;
}

bool ESP8266_AT::disconnectAP() {
    rxClear();
    m_serial->println("AT+CWQAP");
    return checkATResponse();
}

uint8_t ESP8266_AT::ipStatus() {
    String buf;
    rxClear();
    m_serial->println("AT+CIPSTATUS");
    checkATResponse(&buf, "S:");
    uint32_t index = buf.indexOf(":");
    return buf.substring(index + 1, index + 2).toInt();
}

bool ESP8266_AT::statusWiFi() {
    uint8_t checkCnt = 5;
    while(checkCnt--) {
        if(ipStatus() == 5) return false;
        delay(100);
    }
    return true;
}

int ESP8266_AT::connect(const char *host, uint16_t port) {
    if(connected()) stop();
    String buf;
    uint8_t retry = 10;
    while(retry--) {
        rxClear();
        m_serial->print("AT+CIPSTART=\"TCP\",\"");
        m_serial->print(host);
        m_serial->print("\",");
        m_serial->println(port);
        checkATResponse(&buf);
        if(buf.indexOf("OK") != -1 || buf.indexOf("ALREADY") != -1) {
            return 1; // SUCCESS
        }
        delay(100);
    }
    return -1; // TIMED_OUT
}

int ESP8266_AT::connect(IPAddress ip, uint16_t port) {
    String host = "";
    for(uint8_t i = 0; i < 4;) {
        host += String(ip[i]);
        if(++i < 4) host += ".";
    }
    return connect(host.c_str(), port);
}

void ESP8266_AT::stop() {
    rxClear();
    m_serial->println("AT+CIPCLOSE");
    checkATResponse();
}

uint8_t ESP8266_AT::connected() {
    uint8_t retry = 5;
    while(retry--) {
        if(ipStatus() == 3) return 1;
        delay(100);
    }
    return 0;
}

size_t ESP8266_AT::write(const uint8_t *buf, size_t size) {
    return 0; // TODO
}

size_t ESP8266_AT::write(uint8_t) {
    return 0; // TODO
}

int ESP8266_AT::available() {
    return 0; // TODO
}

int ESP8266_AT::read() {
    return 0; // TODO
}

int ESP8266_AT::read(uint8_t *buf, size_t size) {
    return 0; // TODO
}

int ESP8266_AT::peek() {
    return 0; // TODO
}

void ESP8266_AT::flush() {
    // TODO
}

ESP8266_AT::operator bool() {
    // TODO
}
<|endoftext|>


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

#include <salsys.hxx>
#include <stacktrace.hxx>

#include <stdio.h>

#include <salunx.h>
#include <saldisp.hxx>
#include <dtint.hxx>
#include <msgbox.hxx>
#include <button.hxx>
#include <svdata.hxx>

// -----------------------------------------------------------------------

String GetSalSummarySystemInfos( ULONG nFlags )
{
    sal_PostMortem aPostMortem;

    /*
     *  unimplemented flags:
     *  SALSYSTEM_GETSYSTEMINFO_MODULES
     *  SALSYSTEM_GETSYSTEMINFO_MOUSEINFO
     *  SALSYSTEM_GETSYSTEMINFO_SYSTEMDIRS
     *  SALSYSTEM_GETSYSTEMINFO_LOCALVOLUMES
     */

    ByteString aRet;
    if( nFlags & SALSYSTEM_GETSYSTEMINFO_SYSTEMVERSION )
        aRet += aPostMortem.getSystemInfo();
    if( nFlags & SALSYSTEM_GETSYSTEMINFO_CPUTYPE )
        aRet += aPostMortem.getProcessorInfo();
    if( nFlags & SALSYSTEM_GETSYSTEMINFO_MEMORYINFO )
        aRet += aPostMortem.getMemoryInfo();
    if( nFlags & SALSYSTEM_GETSYSTEMINFO_STACK )
        aRet += aPostMortem.getStackTrace();
    if( nFlags & SALSYSTEM_GETSYSTEMINFO_GRAPHICSSYSTEM )
        aRet += aPostMortem.getGraphicsSystem();

#ifdef DEBUG
    fprintf( stderr, "SalSystem::GetSummarySystemInfos() =\n%s", aRet.GetBuffer() );
#endif
    return String( aRet, RTL_TEXTENCODING_ISO_8859_1 );
}

bool GetSalSystemDisplayInfo( System::DisplayInfo& rInfo )
{
    bool bSuccess = false;
    Display* pDisplay = XOpenDisplay( NULL );
    if( pDisplay )
    {
        int nScreen = DefaultScreen( pDisplay );
        XVisualInfo aVI;
        /*  note: SalDisplay::BestVisual does not
         *  access saldata or any other data available
         *  only after InitVCL; nor does SalOpenGL:MakeVisualWeights
         *  which gets called by SalDisplay::BestVisual.
         *  this is crucial since GetSalSystemDisplayInfo
         *  gets called BEFORE Init.
         */
        SalDisplay::BestVisual( pDisplay, nScreen, aVI );
        rInfo.nDepth    = aVI.depth;
        rInfo.nWidth    = DisplayWidth( pDisplay, nScreen );
        rInfo.nHeight   = DisplayHeight( pDisplay, nScreen );
        XCloseDisplay( pDisplay );
        bSuccess = true;
    }
    return bSuccess;
}

int ImplShowNativeDialog( const String& rTitle, const String& rMessage, const std::list< String >& rButtons, int nDefButton )
{
    int nRet = -1;

    ImplSVData* pSVData = ImplGetSVData();
    if( pSVData->mpIntroWindow )
        pSVData->mpIntroWindow->Hide();

    DtIntegrator* pIntegrator = DtIntegrator::CreateDtIntegrator( NULL );
    if( pIntegrator->GetDtType() == DtGNOME )
    {
        ByteString aCmdLine( "msgbox-gnome ");
        int nButton = 0;
        for( std::list< String >::const_iterator it = rButtons.begin(); it != rButtons.end(); ++it )
        {
            if( nButton == nDefButton )
                aCmdLine.Append( "-defaultbutton" );
            else
                aCmdLine.Append( "-button" );
            nButton++;
            aCmdLine.Append( " \"" );
            aCmdLine.Append( ByteString( *it, RTL_TEXTENCODING_UTF8 ) );
            aCmdLine.Append( "\" " );
        }
        aCmdLine.Append( " \"" );
        aCmdLine.Append( ByteString( rTitle, RTL_TEXTENCODING_UTF8 ) );
        aCmdLine.Append( "\" \"" );
        aCmdLine.Append( ByteString( rMessage, RTL_TEXTENCODING_UTF8 ) );
        aCmdLine.Append( "\" 2>/dev/null" );

        FILE* fp = popen( aCmdLine.GetBuffer(), "r" );
        if( fp )
        {
            ByteString aAnswer;
            char buf[16];
            while( fgets( buf, sizeof( buf ), fp ) )
            {
                aAnswer.Append( buf );
            }
            pclose( fp );
            nRet = aAnswer.ToInt32();
        }
    }
    else // default to a VCL dialogue since we do not have a native implementation
    {
        WarningBox aWarn( NULL, WB_STDWORK, rMessage );
        aWarn.SetText( rTitle );
        aWarn.Clear();

        USHORT nButton = 0;
        for( std::list< String >::const_iterator it = rButtons.begin(); it != rButtons.end(); ++it )
        {
            aWarn.AddButton( *it, nButton+1, nButton == (USHORT)nDefButton ? BUTTONDIALOG_DEFBUTTON : 0 );
            nButton++;
        }
        aWarn.SetFocusButton( (USHORT)nDefButton+1 );

        nRet = ((int)aWarn.Execute()) - 1;
    }

    // normalize behaviour, actually this should never happen
    if( nRet < -1 || nRet >= rButtons.size() )
        nRet = -1;

    return nRet;
}

int ImplShowNativeMessageBox(const String& rTitle, const String& rMessage, int nButtonCombination, int nDefaultButton)
{
    int nDefButton = 0;
    std::list< String > aButtons;
    int nButtonIds[5], nBut = 0;

    if( nButtonCombination == SALSYSTEM_SHOWNATIVEMSGBOX_BTNCOMBI_OK ||
        nButtonCombination == SALSYSTEM_SHOWNATIVEMSGBOX_BTNCOMBI_OK_CANCEL )
    {
        aButtons.push_back( Button::GetStandardText( BUTTON_OK ) );
        nButtonIds[nBut++] = SALSYSTEM_SHOWNATIVEMSGBOX_BTN_OK;
    }
    if( nButtonCombination == SALSYSTEM_SHOWNATIVEMSGBOX_BTNCOMBI_YES_NO_CANCEL ||
        nButtonCombination == SALSYSTEM_SHOWNATIVEMSGBOX_BTNCOMBI_YES_NO )
    {
        aButtons.push_back( Button::GetStandardText( BUTTON_YES ) );
        nButtonIds[nBut++] = SALSYSTEM_SHOWNATIVEMSGBOX_BTN_YES;
        aButtons.push_back( Button::GetStandardText( BUTTON_NO ) );
        nButtonIds[nBut++] = SALSYSTEM_SHOWNATIVEMSGBOX_BTN_NO;
        if( nDefaultButton == SALSYSTEM_SHOWNATIVEMSGBOX_BTN_NO )
            nDefButton = 1;
    }
    if( nButtonCombination == SALSYSTEM_SHOWNATIVEMSGBOX_BTNCOMBI_OK_CANCEL ||
        nButtonCombination == SALSYSTEM_SHOWNATIVEMSGBOX_BTNCOMBI_YES_NO_CANCEL ||
        nButtonCombination == SALSYSTEM_SHOWNATIVEMSGBOX_BTNCOMBI_RETRY_CANCEL )
    {
        if( nButtonCombination == SALSYSTEM_SHOWNATIVEMSGBOX_BTNCOMBI_RETRY_CANCEL )
        {
            aButtons.push_back( Button::GetStandardText( BUTTON_RETRY ) );
            nButtonIds[nBut++] = SALSYSTEM_SHOWNATIVEMSGBOX_BTN_RETRY;
        }
        aButtons.push_back( Button::GetStandardText( BUTTON_CANCEL ) );
        nButtonIds[nBut++] = SALSYSTEM_SHOWNATIVEMSGBOX_BTN_CANCEL;
        if( nDefaultButton == SALSYSTEM_SHOWNATIVEMSGBOX_BTN_CANCEL )
            nDefButton = aButtons.size()-1;
    }
    if( nButtonCombination == SALSYSTEM_SHOWNATIVEMSGBOX_BTNCOMBI_ABORT_RETRY_IGNORE )
    {
        aButtons.push_back( Button::GetStandardText( BUTTON_ABORT ) );
        nButtonIds[nBut++] = SALSYSTEM_SHOWNATIVEMSGBOX_BTN_ABORT;
        aButtons.push_back( Button::GetStandardText( BUTTON_RETRY ) );
        nButtonIds[nBut++] = SALSYSTEM_SHOWNATIVEMSGBOX_BTN_RETRY;
        aButtons.push_back( Button::GetStandardText( BUTTON_IGNORE ) );
        nButtonIds[nBut++] = SALSYSTEM_SHOWNATIVEMSGBOX_BTN_IGNORE;
        switch( nDefaultButton )
        {
            case SALSYSTEM_SHOWNATIVEMSGBOX_BTN_RETRY: nDefButton = 1;break;
            case SALSYSTEM_SHOWNATIVEMSGBOX_BTN_IGNORE: nDefButton = 2;break;
        }
    }
    int nResult = ImplShowNativeDialog( rTitle, rMessage, aButtons, nDefButton );

    return nResult != -1 ? nButtonIds[ nResult ] : 0;
}
<commit_msg>INTEGRATION: CWS dbgmacros1 (1.5.4.1.56); FILE MERGED 2003/04/10 09:28:31 kso 1.5.4.1.56.1: #108413# - debug macro unification.<commit_after>/*************************************************************************
 *
 *  $RCSfile: salsys.cxx,v $
 *
 *  $Revision: 1.7 $
 *
 *  last change: $Author: vg $ $Date: 2003-04-15 16:08:33 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

#include <salsys.hxx>
#include <stacktrace.hxx>

#include <stdio.h>

#include <salunx.h>
#include <saldisp.hxx>
#include <dtint.hxx>
#include <msgbox.hxx>
#include <button.hxx>
#include <svdata.hxx>

// -----------------------------------------------------------------------

String GetSalSummarySystemInfos( ULONG nFlags )
{
    sal_PostMortem aPostMortem;

    /*
     *  unimplemented flags:
     *  SALSYSTEM_GETSYSTEMINFO_MODULES
     *  SALSYSTEM_GETSYSTEMINFO_MOUSEINFO
     *  SALSYSTEM_GETSYSTEMINFO_SYSTEMDIRS
     *  SALSYSTEM_GETSYSTEMINFO_LOCALVOLUMES
     */

    ByteString aRet;
    if( nFlags & SALSYSTEM_GETSYSTEMINFO_SYSTEMVERSION )
        aRet += aPostMortem.getSystemInfo();
    if( nFlags & SALSYSTEM_GETSYSTEMINFO_CPUTYPE )
        aRet += aPostMortem.getProcessorInfo();
    if( nFlags & SALSYSTEM_GETSYSTEMINFO_MEMORYINFO )
        aRet += aPostMortem.getMemoryInfo();
    if( nFlags & SALSYSTEM_GETSYSTEMINFO_STACK )
        aRet += aPostMortem.getStackTrace();
    if( nFlags & SALSYSTEM_GETSYSTEMINFO_GRAPHICSSYSTEM )
        aRet += aPostMortem.getGraphicsSystem();

#if OSL_DEBUG_LEVEL > 1
    fprintf( stderr, "SalSystem::GetSummarySystemInfos() =\n%s", aRet.GetBuffer() );
#endif
    return String( aRet, RTL_TEXTENCODING_ISO_8859_1 );
}

bool GetSalSystemDisplayInfo( System::DisplayInfo& rInfo )
{
    bool bSuccess = false;
    Display* pDisplay = XOpenDisplay( NULL );
    if( pDisplay )
    {
        int nScreen = DefaultScreen( pDisplay );
        XVisualInfo aVI;
        /*  note: SalDisplay::BestVisual does not
         *  access saldata or any other data available
         *  only after InitVCL; nor does SalOpenGL:MakeVisualWeights
         *  which gets called by SalDisplay::BestVisual.
         *  this is crucial since GetSalSystemDisplayInfo
         *  gets called BEFORE Init.
         */
        SalDisplay::BestVisual( pDisplay, nScreen, aVI );
        rInfo.nDepth    = aVI.depth;
        rInfo.nWidth    = DisplayWidth( pDisplay, nScreen );
        rInfo.nHeight   = DisplayHeight( pDisplay, nScreen );
        XCloseDisplay( pDisplay );
        bSuccess = true;
    }
    return bSuccess;
}

int ImplShowNativeDialog( const String& rTitle, const String& rMessage, const std::list< String >& rButtons, int nDefButton )
{
    int nRet = -1;

    ImplSVData* pSVData = ImplGetSVData();
    if( pSVData->mpIntroWindow )
        pSVData->mpIntroWindow->Hide();

    DtIntegrator* pIntegrator = DtIntegrator::CreateDtIntegrator( NULL );
    if( pIntegrator->GetDtType() == DtGNOME )
    {
        ByteString aCmdLine( "msgbox-gnome ");
        int nButton = 0;
        for( std::list< String >::const_iterator it = rButtons.begin(); it != rButtons.end(); ++it )
        {
            if( nButton == nDefButton )
                aCmdLine.Append( "-defaultbutton" );
            else
                aCmdLine.Append( "-button" );
            nButton++;
            aCmdLine.Append( " \"" );
            aCmdLine.Append( ByteString( *it, RTL_TEXTENCODING_UTF8 ) );
            aCmdLine.Append( "\" " );
        }
        aCmdLine.Append( " \"" );
        aCmdLine.Append( ByteString( rTitle, RTL_TEXTENCODING_UTF8 ) );
        aCmdLine.Append( "\" \"" );
        aCmdLine.Append( ByteString( rMessage, RTL_TEXTENCODING_UTF8 ) );
        aCmdLine.Append( "\" 2>/dev/null" );

        FILE* fp = popen( aCmdLine.GetBuffer(), "r" );
        if( fp )
        {
            ByteString aAnswer;
            char buf[16];
            while( fgets( buf, sizeof( buf ), fp ) )
            {
                aAnswer.Append( buf );
            }
            pclose( fp );
            nRet = aAnswer.ToInt32();
        }
    }
    else // default to a VCL dialogue since we do not have a native implementation
    {
        WarningBox aWarn( NULL, WB_STDWORK, rMessage );
        aWarn.SetText( rTitle );
        aWarn.Clear();

        USHORT nButton = 0;
        for( std::list< String >::const_iterator it = rButtons.begin(); it != rButtons.end(); ++it )
        {
            aWarn.AddButton( *it, nButton+1, nButton == (USHORT)nDefButton ? BUTTONDIALOG_DEFBUTTON : 0 );
            nButton++;
        }
        aWarn.SetFocusButton( (USHORT)nDefButton+1 );

        nRet = ((int)aWarn.Execute()) - 1;
    }

    // normalize behaviour, actually this should never happen
    if( nRet < -1 || nRet >= rButtons.size() )
        nRet = -1;

    return nRet;
}

int ImplShowNativeMessageBox(const String& rTitle, const String& rMessage, int nButtonCombination, int nDefaultButton)
{
    int nDefButton = 0;
    std::list< String > aButtons;
    int nButtonIds[5], nBut = 0;

    if( nButtonCombination == SALSYSTEM_SHOWNATIVEMSGBOX_BTNCOMBI_OK ||
        nButtonCombination == SALSYSTEM_SHOWNATIVEMSGBOX_BTNCOMBI_OK_CANCEL )
    {
        aButtons.push_back( Button::GetStandardText( BUTTON_OK ) );
        nButtonIds[nBut++] = SALSYSTEM_SHOWNATIVEMSGBOX_BTN_OK;
    }
    if( nButtonCombination == SALSYSTEM_SHOWNATIVEMSGBOX_BTNCOMBI_YES_NO_CANCEL ||
        nButtonCombination == SALSYSTEM_SHOWNATIVEMSGBOX_BTNCOMBI_YES_NO )
    {
        aButtons.push_back( Button::GetStandardText( BUTTON_YES ) );
        nButtonIds[nBut++] = SALSYSTEM_SHOWNATIVEMSGBOX_BTN_YES;
        aButtons.push_back( Button::GetStandardText( BUTTON_NO ) );
        nButtonIds[nBut++] = SALSYSTEM_SHOWNATIVEMSGBOX_BTN_NO;
        if( nDefaultButton == SALSYSTEM_SHOWNATIVEMSGBOX_BTN_NO )
            nDefButton = 1;
    }
    if( nButtonCombination == SALSYSTEM_SHOWNATIVEMSGBOX_BTNCOMBI_OK_CANCEL ||
        nButtonCombination == SALSYSTEM_SHOWNATIVEMSGBOX_BTNCOMBI_YES_NO_CANCEL ||
        nButtonCombination == SALSYSTEM_SHOWNATIVEMSGBOX_BTNCOMBI_RETRY_CANCEL )
    {
        if( nButtonCombination == SALSYSTEM_SHOWNATIVEMSGBOX_BTNCOMBI_RETRY_CANCEL )
        {
            aButtons.push_back( Button::GetStandardText( BUTTON_RETRY ) );
            nButtonIds[nBut++] = SALSYSTEM_SHOWNATIVEMSGBOX_BTN_RETRY;
        }
        aButtons.push_back( Button::GetStandardText( BUTTON_CANCEL ) );
        nButtonIds[nBut++] = SALSYSTEM_SHOWNATIVEMSGBOX_BTN_CANCEL;
        if( nDefaultButton == SALSYSTEM_SHOWNATIVEMSGBOX_BTN_CANCEL )
            nDefButton = aButtons.size()-1;
    }
    if( nButtonCombination == SALSYSTEM_SHOWNATIVEMSGBOX_BTNCOMBI_ABORT_RETRY_IGNORE )
    {
        aButtons.push_back( Button::GetStandardText( BUTTON_ABORT ) );
        nButtonIds[nBut++] = SALSYSTEM_SHOWNATIVEMSGBOX_BTN_ABORT;
        aButtons.push_back( Button::GetStandardText( BUTTON_RETRY ) );
        nButtonIds[nBut++] = SALSYSTEM_SHOWNATIVEMSGBOX_BTN_RETRY;
        aButtons.push_back( Button::GetStandardText( BUTTON_IGNORE ) );
        nButtonIds[nBut++] = SALSYSTEM_SHOWNATIVEMSGBOX_BTN_IGNORE;
        switch( nDefaultButton )
        {
            case SALSYSTEM_SHOWNATIVEMSGBOX_BTN_RETRY: nDefButton = 1;break;
            case SALSYSTEM_SHOWNATIVEMSGBOX_BTN_IGNORE: nDefButton = 2;break;
        }
    }
    int nResult = ImplShowNativeDialog( rTitle, rMessage, aButtons, nDefButton );

    return nResult != -1 ? nButtonIds[ nResult ] : 0;
}
<|endoftext|>


<commit_before>#include "vsclanguage.h"
#include "hdddevice.hpp"
#include "debug.hpp"
#include "vschddone.h"
#include "factory.hpp"

extern Factory *gFactory;

VSCLanguage::VSCLanguage(QWidget *parent)
    : QWidget(parent)
{
	ui.setupUi(this);

	gFactory->GetLang(m_lang);
	connect( this->ui.pushButtonApply, SIGNAL( clicked() ), this, SLOT(applyConfig()));
	SetupValue();
}
void VSCLanguage::SetupValue()
{
    switch(m_lang)
    {
	case VSC_LANG_EN:
        ui.radioButtonEN->setChecked(true);
        break;
	case VSC_LANG_ZH:
        ui.radioButtonZH->setChecked(true);
        break;
    default:
        return;
    }
}

void VSCLanguage::applyConfig()
{
	if (ui.radioButtonEN->isChecked() == true)
	{
		m_lang = VSC_LANG_EN;
	}else if (ui.radioButtonZH->isChecked() == true)
	{
		m_lang = VSC_LANG_ZH;
	}

	gFactory->SetLang(m_lang);
	return;

}

<commit_msg>fix lang for tabbed<commit_after>#include "vsclanguage.h"
#include "hdddevice.hpp"
#include "debug.hpp"
#include "vschddone.h"
#include "factory.hpp"

extern Factory *gFactory;

VSCLanguage::VSCLanguage(QWidget *parent)
    : QWidget(parent)
{
	ui.setupUi(this);

	gFactory->GetLang(m_lang);
	connect( this->ui.pushButtonApply, SIGNAL( clicked() ), this, SLOT(applyConfig()));
	SetupValue();
}
void VSCLanguage::SetupValue()
{
    switch(m_lang)
    {
	case VSC_LANG_AUTO:
        ui.radioButtonAuto->setChecked(true);
        break;
	case VSC_LANG_EN:
        ui.radioButtonEN->setChecked(true);
        break;
	case VSC_LANG_ZH:
        ui.radioButtonZH->setChecked(true);
        break;
    default:
        return;
    }
}

void VSCLanguage::applyConfig()
{
	if (ui.radioButtonAuto->isChecked() == true)
	{
		m_lang = VSC_LANG_AUTO;
	}
	else if (ui.radioButtonEN->isChecked() == true)
	{
		m_lang = VSC_LANG_EN;
	}
	else if (ui.radioButtonZH->isChecked() == true)
	{
		m_lang = VSC_LANG_ZH;
	}

	gFactory->SetLang(m_lang);
	return;

}

<|endoftext|>


<commit_before>// Copyright (c) 2015 Chaobin Zhang. All rights reserved.
// Use of this source code is governed by the BSD license that can be
// found in the LICENSE file.

#include "master/master_main_runner.h"

#include "base/bind.h"
#include "base/files/file_util.h"
#include "base/files/scoped_file.h"
#include "base/hash.h"
#include "base/md5.h"
#include "base/strings/string_piece.h"
#include "base/sys_info.h"
#include "base/threading/thread_restrictions.h"
#include "common/util.h"
#include "curl/curl.h"
#include "master/master_rpc.h"
#include "ninja/dn_builder.h"
#include "ninja/ninja_main.h"
#include "thread/ninja_thread.h"

namespace {

const char kHttp[] = "http://";

// Curl helper, with md5 check sum.
class CurlHelper {
 public:
  static size_t WriteDataStatic(void* ptr, size_t size, size_t count,
                                CurlHelper* curl_helper) {
    return curl_helper->WriteData(ptr, size, count);
  }

  CurlHelper() : curl_(curl_easy_init()) {
  }

  ~CurlHelper() {
    curl_easy_cleanup(curl_);
  }

  std::string Get(const std::string& url, const base::FilePath& filename) {
    base::MD5Init(&md5_context_);
    CHECK(base::CreateDirectory(filename.DirName()));
    file_.reset(base::OpenFile(filename, "wb"));
    curl_easy_setopt(curl_, CURLOPT_URL, url.c_str());
    curl_easy_setopt(curl_, CURLOPT_WRITEFUNCTION, CurlHelper::WriteDataStatic);
    curl_easy_setopt(curl_, CURLOPT_WRITEDATA, this);
    if (curl_easy_perform(curl_) != CURLE_OK)
      return "";

    base::MD5Digest digest;
    base::MD5Final(&digest, &md5_context_);
    return MD5DigestToBase16(digest);
  }

  size_t WriteData(void* ptr, size_t size, size_t count) {
    base::MD5Update(
        &md5_context_,
        base::StringPiece(reinterpret_cast<char*>(ptr), size * count));
    return fwrite(ptr, size, count, file_.get());
  }

 private:
  CURL* curl_;
  base::MD5Context md5_context_;
  base::ScopedFILE file_;
};

}  // namespace

namespace master {

MasterMainRunner::MasterMainRunner(const std::string& bind_ip, uint16 port)
    : bind_ip_(bind_ip),
      port_(port),
      number_of_slave_processors_(0) {
  // |curl_global_init| is not thread-safe, following advice in docs of
  // |curl_easy_init|, we call it manually.
  curl_global_init(CURL_GLOBAL_ALL);
}

MasterMainRunner::~MasterMainRunner() {
  curl_global_cleanup();
}

bool MasterMainRunner::PostCreateThreads() {
  master_rpc_.reset(new MasterRPC(bind_ip_, port_, this));
  return true;
}

void MasterMainRunner::StartBuild() {
  std::string error;
  config_.parallelism = common::GuessParallelism();
  ninja_main()->RunBuild(ninja_main()->state().DefaultNodes(&error), this);
}

bool MasterMainRunner::LocalCanRunMore() {
  size_t subproc_number =
      subprocs_.running_.size() + subprocs_.finished_.size();
  return static_cast<int>(subproc_number) < config_.parallelism;
}

bool MasterMainRunner::RemoteCanRunMore() {
  return static_cast<int>(outstanding_edges_.size()) <=
      number_of_slave_processors_;
}

bool MasterMainRunner::StartCommand(Edge* edge, bool run_in_local) {
  if (run_in_local)
    return StartCommandLocally(edge);
  else
    return StartCommandRemotely(edge);
}

bool MasterMainRunner::StartCommandLocally(Edge* edge) {
  base::ThreadRestrictions::AssertIOAllowed();

  // Create directories necessary for outputs.
  for (vector<Node*>::iterator o = edge->outputs_.begin();
       o != edge->outputs_.end(); ++o) {
    if (!ninja_main()->disk_interface()->MakeDirs((*o)->path()))
      return false;
  }

  // Create response file, if needed
  std::string rspfile = edge->GetUnescapedRspfile();
  if (!rspfile.empty()) {
    std::string content = edge->GetBinding("rspfile_content");
    if (!ninja_main()->disk_interface()->WriteFile(rspfile, content))
      return false;
  }

  std::string command = edge->EvaluateCommand();
  Subprocess* subproc = subprocs_.Add(command, edge->use_console());
  if (!subproc)
    return false;
  subproc_to_edge_.insert(make_pair(subproc, edge));
  return true;
}

bool MasterMainRunner::StartCommandRemotely(Edge* edge) {
  int connection_id = FindMostAvailableSlave();
  if (connection_id == INT_MIN)
    return false;

  MasterRPC::OutputPaths output_paths;
  for (vector<Node*>::iterator o = edge->outputs_.begin();
       o != edge->outputs_.end();
       ++o) {
    output_paths.push_back((*o)->path());
  }

  std::string command = edge->EvaluateCommand();
  uint32 edge_id = base::Hash(command);
  outstanding_edges_[edge_id] = edge;
  NinjaThread::PostTask(
      NinjaThread::RPC,
      FROM_HERE,
      base::Bind(&MasterRPC::StartCommandRemotely,
                 base::Unretained(master_rpc_.get()),
                 connection_id,
                 output_paths,
                 edge->GetUnescapedRspfile(),
                 edge->GetBinding("rspfile_content"),
                 command,
                 edge_id));
  return true;
}

bool MasterMainRunner::WaitForCommand(CommandRunner::Result* result) {
  Subprocess* subproc;
  while ((subproc = subprocs_.NextFinished()) == NULL) {
    bool interrupted = subprocs_.DoWork();
    if (interrupted)
      return false;
  }

  result->status = subproc->Finish();
  result->output = subproc->GetOutput();

  SubprocessToEdgeMap::iterator e = subproc_to_edge_.find(subproc);
  result->edge = e->second;
  subproc_to_edge_.erase(e);

  delete subproc;
  return true;
}

std::vector<Edge*> MasterMainRunner::GetActiveEdges() {
  std::vector<Edge*> edges;
  for (SubprocessToEdgeMap::iterator e = subproc_to_edge_.begin();
       e != subproc_to_edge_.end(); ++e)
    edges.push_back(e->second);
  return edges;
}

void MasterMainRunner::Abort() {
  subprocs_.Clear();
}

bool MasterMainRunner::HasPendingLocalCommands() {
  return !subproc_to_edge_.empty();
}

void MasterMainRunner::OnFetchTargetsDone(CommandRunner::Result result) {
  DCHECK(NinjaThread::CurrentlyOn(NinjaThread::MAIN));
  std::string error;
  if (!ninja_main()->builder()->HasRemoteCommandRunLocally(result.edge))
    ninja_main()->builder()->FinishCommand(&result, &error);
}

void MasterMainRunner::OnRemoteCommandDone(
    int connection_id,
    uint32 edge_id,
    ExitStatus status,
    const std::string& output,
    const std::vector<std::string>& md5s) {
  OutstandingEdgeMap::iterator it = outstanding_edges_.find(edge_id);
  DCHECK(it != outstanding_edges_.end());
  CommandRunner::Result result;
  result.edge = it->second;
  result.status = status;
  result.output = output;  // The output stream of the command.
  outstanding_edges_.erase(it);
  std::string error;
  if (status != ExitSuccess) {
    ninja_main()->builder()->FinishCommand(&result, &error);
    return;
  }

  DCHECK(result.edge->outputs_.size() == md5s.size());
  TargetVector targets;
  for (size_t i = 0; i < result.edge->outputs_.size(); ++i) {
    targets.push_back(
        std::make_pair(result.edge->outputs_[i]->path(), md5s[i]));
  }

  DCHECK(slave_info_id_map_.find(connection_id) != slave_info_id_map_.end());
  // TODO(zhchbin): Remove hard code 8080.
  std::string host = slave_info_id_map_[connection_id].ip + ":" + "8080";
  NinjaThread::PostBlockingPoolTask(
      FROM_HERE,
      base::Bind(&MasterMainRunner::FetchTargetsOnBlockingPool,
                 this,
                 host,
                 targets,
                 result));
}

void MasterMainRunner::OnSlaveSystemInfoAvailable(int connection_id,
                                                  const SlaveInfo& info) {
  if (slave_info_id_map_.find(connection_id) != slave_info_id_map_.end())
    return;
  if (info.operating_system_name != base::SysInfo::OperatingSystemName() ||
      info.operating_system_architecture !=
          base::SysInfo::OperatingSystemArchitecture()) {
    static const string kRejectReason =
        "Different system name or architecture, system info of master: \"" +
        base::SysInfo::OperatingSystemName() + ", " +
        base::SysInfo::OperatingSystemArchitecture() + "\".";

    NinjaThread::PostTask(
        NinjaThread::RPC,
        FROM_HERE,
        base::Bind(&MasterRPC::QuitSlave,
                   base::Unretained(master_rpc_.get()),
                   connection_id,
                   kRejectReason));
    return;
  }

  slave_info_id_map_[connection_id] = info;
  number_of_slave_processors_ += info.number_of_processors;
}

void MasterMainRunner::OnSlaveStatusUpdate(
    int connection_id,
    double load_average,
    int amount_of_running_commands,
    int64 amount_of_available_physical_memory) {
  // Don't update the status until |OnSlaveSystemInfoAvailable| is called.
  SlaveInfoIdMap::iterator it = slave_info_id_map_.find(connection_id);
  if (it == slave_info_id_map_.end())
    return;

  it->second.load_average = load_average;
  it->second.amount_of_running_commands = amount_of_running_commands;
  it->second.amount_of_available_physical_memory =
      amount_of_available_physical_memory;
}

void MasterMainRunner::OnSlaveClose(int connection_id) {
  DCHECK(slave_info_id_map_.find(connection_id) != slave_info_id_map_.end());
  slave_info_id_map_.erase(connection_id);
}

int MasterMainRunner::FindMostAvailableSlave() {
  int connection_id = INT_MIN;
  int max_number_of_available_processors = INT_MIN;
  for (SlaveInfoIdMap::iterator it = slave_info_id_map_.begin();
       it != slave_info_id_map_.end();
       ++it) {
    int tmp =
        it->second.number_of_processors - it->second.amount_of_running_commands;
    if (tmp > max_number_of_available_processors) {
      tmp = max_number_of_available_processors;
      connection_id = it->first;
    }
  }

  return connection_id;
}

void MasterMainRunner::FetchTargetsOnBlockingPool(
    const std::string& host,
    const TargetVector& targets,
    CommandRunner::Result result) {
  bool success = true;
  if (result.success()) {
    CurlHelper curl_helper;
    for (size_t i = 0; i < targets.size(); ++i) {
      base::FilePath filename =
          base::FilePath::FromUTF8Unsafe(targets[i].first);
      std::string url = kHttp + host + "/" + targets[i].first;
      success = (curl_helper.Get(url, filename) == targets[i].second);
      if (!success)
        break;
    }
  }

  if (success) {
    NinjaThread::PostTask(
        NinjaThread::MAIN,
        FROM_HERE,
        base::Bind(&MasterMainRunner::OnFetchTargetsDone, this, result));
  }

  // DO NOT call |MasterMainRunner::OnFetchTargetsDone| if curl is failed,
  // since we will try to start unfinished outstanding edges locally.
}

}  // namespace master
<commit_msg>[WIN] Unlock file when curl done.<commit_after>// Copyright (c) 2015 Chaobin Zhang. All rights reserved.
// Use of this source code is governed by the BSD license that can be
// found in the LICENSE file.

#include "master/master_main_runner.h"

#include "base/bind.h"
#include "base/files/file_util.h"
#include "base/files/scoped_file.h"
#include "base/hash.h"
#include "base/md5.h"
#include "base/strings/string_piece.h"
#include "base/sys_info.h"
#include "base/threading/thread_restrictions.h"
#include "common/util.h"
#include "curl/curl.h"
#include "master/master_rpc.h"
#include "ninja/dn_builder.h"
#include "ninja/ninja_main.h"
#include "thread/ninja_thread.h"

namespace {

const char kHttp[] = "http://";

// Curl helper, with md5 check sum.
class CurlHelper {
 public:
  static size_t WriteDataStatic(void* ptr, size_t size, size_t count,
                                CurlHelper* curl_helper) {
    return curl_helper->WriteData(ptr, size, count);
  }

  CurlHelper() : curl_(curl_easy_init()) {
  }

  ~CurlHelper() {
    curl_easy_cleanup(curl_);
  }

  std::string Get(const std::string& url, const base::FilePath& filename) {
    base::MD5Init(&md5_context_);
    CHECK(base::CreateDirectory(filename.DirName()));
    file_.InitializeUnsafe(filename,
                           base::File::FLAG_CREATE | base::File::FLAG_WRITE);
    if (!file_.IsValid()) {
      LOG(ERROR) << filename.value();
      return "";
    }

    curl_easy_setopt(curl_, CURLOPT_URL, url.c_str());
    curl_easy_setopt(curl_, CURLOPT_WRITEFUNCTION, CurlHelper::WriteDataStatic);
    curl_easy_setopt(curl_, CURLOPT_WRITEDATA, this);
    if (curl_easy_perform(curl_) != CURLE_OK)
      return "";

    file_.Unlock();
    file_.Close();
    base::MD5Digest digest;
    base::MD5Final(&digest, &md5_context_);
    return MD5DigestToBase16(digest);
  }

  size_t WriteData(void* ptr, size_t size, size_t count) {
    base::MD5Update(
        &md5_context_,
        base::StringPiece(reinterpret_cast<char*>(ptr), size * count));
    return file_.WriteAtCurrentPos(reinterpret_cast<char*>(ptr), size * count);
  }

 private:
  CURL* curl_;
  base::MD5Context md5_context_;
  base::File file_;
};

}  // namespace

namespace master {

MasterMainRunner::MasterMainRunner(const std::string& bind_ip, uint16 port)
    : bind_ip_(bind_ip),
      port_(port),
      number_of_slave_processors_(0) {
  // |curl_global_init| is not thread-safe, following advice in docs of
  // |curl_easy_init|, we call it manually.
  curl_global_init(CURL_GLOBAL_ALL);
}

MasterMainRunner::~MasterMainRunner() {
  curl_global_cleanup();
}

bool MasterMainRunner::PostCreateThreads() {
  master_rpc_.reset(new MasterRPC(bind_ip_, port_, this));
  return true;
}

void MasterMainRunner::StartBuild() {
  std::string error;
  config_.parallelism = common::GuessParallelism();
  ninja_main()->RunBuild(ninja_main()->state().DefaultNodes(&error), this);
}

bool MasterMainRunner::LocalCanRunMore() {
  size_t subproc_number =
      subprocs_.running_.size() + subprocs_.finished_.size();
  return static_cast<int>(subproc_number) < config_.parallelism;
}

bool MasterMainRunner::RemoteCanRunMore() {
  return static_cast<int>(outstanding_edges_.size()) <=
      number_of_slave_processors_;
}

bool MasterMainRunner::StartCommand(Edge* edge, bool run_in_local) {
  if (run_in_local)
    return StartCommandLocally(edge);
  else
    return StartCommandRemotely(edge);
}

bool MasterMainRunner::StartCommandLocally(Edge* edge) {
  base::ThreadRestrictions::AssertIOAllowed();

  // Create directories necessary for outputs.
  for (vector<Node*>::iterator o = edge->outputs_.begin();
       o != edge->outputs_.end(); ++o) {
    if (!ninja_main()->disk_interface()->MakeDirs((*o)->path()))
      return false;
  }

  // Create response file, if needed
  std::string rspfile = edge->GetUnescapedRspfile();
  if (!rspfile.empty()) {
    std::string content = edge->GetBinding("rspfile_content");
    if (!ninja_main()->disk_interface()->WriteFile(rspfile, content))
      return false;
  }

  std::string command = edge->EvaluateCommand();
  Subprocess* subproc = subprocs_.Add(command, edge->use_console());
  if (!subproc)
    return false;
  subproc_to_edge_.insert(make_pair(subproc, edge));
  return true;
}

bool MasterMainRunner::StartCommandRemotely(Edge* edge) {
  int connection_id = FindMostAvailableSlave();
  if (connection_id == INT_MIN)
    return false;

  MasterRPC::OutputPaths output_paths;
  for (vector<Node*>::iterator o = edge->outputs_.begin();
       o != edge->outputs_.end();
       ++o) {
    output_paths.push_back((*o)->path());
  }

  std::string command = edge->EvaluateCommand();
  uint32 edge_id = base::Hash(command);
  outstanding_edges_[edge_id] = edge;
  NinjaThread::PostTask(
      NinjaThread::RPC,
      FROM_HERE,
      base::Bind(&MasterRPC::StartCommandRemotely,
                 base::Unretained(master_rpc_.get()),
                 connection_id,
                 output_paths,
                 edge->GetUnescapedRspfile(),
                 edge->GetBinding("rspfile_content"),
                 command,
                 edge_id));
  return true;
}

bool MasterMainRunner::WaitForCommand(CommandRunner::Result* result) {
  Subprocess* subproc;
  while ((subproc = subprocs_.NextFinished()) == NULL) {
    bool interrupted = subprocs_.DoWork();
    if (interrupted)
      return false;
  }

  result->status = subproc->Finish();
  result->output = subproc->GetOutput();

  SubprocessToEdgeMap::iterator e = subproc_to_edge_.find(subproc);
  result->edge = e->second;
  subproc_to_edge_.erase(e);

  delete subproc;
  return true;
}

std::vector<Edge*> MasterMainRunner::GetActiveEdges() {
  std::vector<Edge*> edges;
  for (SubprocessToEdgeMap::iterator e = subproc_to_edge_.begin();
       e != subproc_to_edge_.end(); ++e)
    edges.push_back(e->second);
  return edges;
}

void MasterMainRunner::Abort() {
  subprocs_.Clear();
}

bool MasterMainRunner::HasPendingLocalCommands() {
  return !subproc_to_edge_.empty();
}

void MasterMainRunner::OnFetchTargetsDone(CommandRunner::Result result) {
  DCHECK(NinjaThread::CurrentlyOn(NinjaThread::MAIN));
  std::string error;
  if (!ninja_main()->builder()->HasRemoteCommandRunLocally(result.edge))
    ninja_main()->builder()->FinishCommand(&result, &error);
}

void MasterMainRunner::OnRemoteCommandDone(
    int connection_id,
    uint32 edge_id,
    ExitStatus status,
    const std::string& output,
    const std::vector<std::string>& md5s) {
  OutstandingEdgeMap::iterator it = outstanding_edges_.find(edge_id);
  DCHECK(it != outstanding_edges_.end());
  CommandRunner::Result result;
  result.edge = it->second;
  result.status = status;
  result.output = output;  // The output stream of the command.
  outstanding_edges_.erase(it);
  std::string error;
  if (status != ExitSuccess) {
    ninja_main()->builder()->FinishCommand(&result, &error);
    return;
  }

  DCHECK(result.edge->outputs_.size() == md5s.size());
  TargetVector targets;
  for (size_t i = 0; i < result.edge->outputs_.size(); ++i) {
    targets.push_back(
        std::make_pair(result.edge->outputs_[i]->path(), md5s[i]));
  }

  DCHECK(slave_info_id_map_.find(connection_id) != slave_info_id_map_.end());
  // TODO(zhchbin): Remove hard code 8080.
  std::string host = slave_info_id_map_[connection_id].ip + ":" + "8080";
  NinjaThread::PostBlockingPoolTask(
      FROM_HERE,
      base::Bind(&MasterMainRunner::FetchTargetsOnBlockingPool,
                 this,
                 host,
                 targets,
                 result));
}

void MasterMainRunner::OnSlaveSystemInfoAvailable(int connection_id,
                                                  const SlaveInfo& info) {
  if (slave_info_id_map_.find(connection_id) != slave_info_id_map_.end())
    return;
  if (info.operating_system_name != base::SysInfo::OperatingSystemName() ||
      info.operating_system_architecture !=
          base::SysInfo::OperatingSystemArchitecture()) {
    static const string kRejectReason =
        "Different system name or architecture, system info of master: \"" +
        base::SysInfo::OperatingSystemName() + ", " +
        base::SysInfo::OperatingSystemArchitecture() + "\".";

    NinjaThread::PostTask(
        NinjaThread::RPC,
        FROM_HERE,
        base::Bind(&MasterRPC::QuitSlave,
                   base::Unretained(master_rpc_.get()),
                   connection_id,
                   kRejectReason));
    return;
  }

  slave_info_id_map_[connection_id] = info;
  number_of_slave_processors_ += info.number_of_processors;
}

void MasterMainRunner::OnSlaveStatusUpdate(
    int connection_id,
    double load_average,
    int amount_of_running_commands,
    int64 amount_of_available_physical_memory) {
  // Don't update the status until |OnSlaveSystemInfoAvailable| is called.
  SlaveInfoIdMap::iterator it = slave_info_id_map_.find(connection_id);
  if (it == slave_info_id_map_.end())
    return;

  it->second.load_average = load_average;
  it->second.amount_of_running_commands = amount_of_running_commands;
  it->second.amount_of_available_physical_memory =
      amount_of_available_physical_memory;
}

void MasterMainRunner::OnSlaveClose(int connection_id) {
  DCHECK(slave_info_id_map_.find(connection_id) != slave_info_id_map_.end());
  slave_info_id_map_.erase(connection_id);
}

int MasterMainRunner::FindMostAvailableSlave() {
  int connection_id = INT_MIN;
  int max_number_of_available_processors = INT_MIN;
  for (SlaveInfoIdMap::iterator it = slave_info_id_map_.begin();
       it != slave_info_id_map_.end();
       ++it) {
    int tmp =
        it->second.number_of_processors - it->second.amount_of_running_commands;
    if (tmp > max_number_of_available_processors) {
      tmp = max_number_of_available_processors;
      connection_id = it->first;
    }
  }

  return connection_id;
}

void MasterMainRunner::FetchTargetsOnBlockingPool(
    const std::string& host,
    const TargetVector& targets,
    CommandRunner::Result result) {
  bool success = true;
  if (result.success()) {
    CurlHelper curl_helper;
    for (size_t i = 0; i < targets.size(); ++i) {
      base::FilePath filename =
          base::FilePath::FromUTF8Unsafe(targets[i].first);
      std::string url = kHttp + host + "/" + targets[i].first;
      success = (curl_helper.Get(url, filename) == targets[i].second);
      if (!success)
        break;
    }
  }

  if (success) {
    NinjaThread::PostTask(
        NinjaThread::MAIN,
        FROM_HERE,
        base::Bind(&MasterMainRunner::OnFetchTargetsDone, this, result));
  }

  // DO NOT call |MasterMainRunner::OnFetchTargetsDone| if curl is failed,
  // since we will try to start unfinished outstanding edges locally.
}

}  // namespace master
<|endoftext|>


<commit_before>// $Id$
#include <vector>

#include "ScoreComponentCollection.h"
#include "StaticData.h"

using namespace std;

namespace Moses
{

ScoreComponentCollection::ScoreIndexMap ScoreComponentCollection::s_scoreIndexes;
size_t ScoreComponentCollection::s_denseVectorSize = 0;

ScoreComponentCollection::ScoreComponentCollection() : m_scores(s_denseVectorSize)
{}

void ScoreComponentCollection::RegisterScoreProducer
(const FeatureFunction* scoreProducer)
{
  size_t start = s_denseVectorSize;
  size_t end = start + scoreProducer->GetNumScoreComponents();
  VERBOSE(1, "FeatureFunction: " << scoreProducer->GetScoreProducerDescription() << " start: " << start << " end: " << end << endl);
  s_scoreIndexes[scoreProducer] = pair<size_t,size_t>(start,end);
  s_denseVectorSize = end;
}

float ScoreComponentCollection::GetWeightedScore() const
{
  return m_scores.inner_product(StaticData::Instance().GetAllWeights().m_scores);
}

void ScoreComponentCollection::MultiplyEquals(float scalar)
{
  m_scores *= scalar;
}

// Multiply all weights of this sparse producer by a given scalar
void ScoreComponentCollection::MultiplyEquals(const FeatureFunction* sp, float scalar)
{
  std::string prefix = sp->GetScoreProducerDescription() + FName::SEP;
  for(FVector::FNVmap::const_iterator i = m_scores.cbegin(); i != m_scores.cend(); i++) {
    std::stringstream name;
    name << i->first;
    if (name.str().substr( 0, prefix.length() ).compare( prefix ) == 0)
      m_scores[i->first] = i->second * scalar;
  }
}

// Count weights belonging to this sparse producer
size_t ScoreComponentCollection::GetNumberWeights(const FeatureFunction* sp)
{
  std::string prefix = sp->GetScoreProducerDescription() + FName::SEP;
  size_t weights = 0;
  for(FVector::FNVmap::const_iterator i = m_scores.cbegin(); i != m_scores.cend(); i++) {
    std::stringstream name;
    name << i->first;
    if (name.str().substr( 0, prefix.length() ).compare( prefix ) == 0)
      weights++;
  }
  return weights;
}

void ScoreComponentCollection::DivideEquals(float scalar)
{
  m_scores /= scalar;
}

void ScoreComponentCollection::CoreDivideEquals(float scalar)
{
  m_scores.coreDivideEquals(scalar);
}

void ScoreComponentCollection::DivideEquals(const ScoreComponentCollection& rhs)
{
  m_scores.divideEquals(rhs.m_scores);
}

void ScoreComponentCollection::MultiplyEquals(const ScoreComponentCollection& rhs)
{
  m_scores *= rhs.m_scores;
}

void ScoreComponentCollection::MultiplyEqualsBackoff(const ScoreComponentCollection& rhs, float backoff)
{
  m_scores.multiplyEqualsBackoff(rhs.m_scores, backoff);
}

void ScoreComponentCollection::MultiplyEquals(float core_r0, float sparse_r0)
{
  m_scores.multiplyEquals(core_r0, sparse_r0);
}

std::ostream& operator<<(std::ostream& os, const ScoreComponentCollection& rhs)
{
  os << rhs.m_scores;
  return os;
}
void ScoreComponentCollection::L1Normalise()
{
  m_scores /= m_scores.l1norm_coreFeatures();
}

float ScoreComponentCollection::GetL1Norm() const
{
  return m_scores.l1norm();
}

float ScoreComponentCollection::GetL2Norm() const
{
  return m_scores.l2norm();
}

float ScoreComponentCollection::GetLInfNorm() const
{
  return m_scores.linfnorm();
}

size_t ScoreComponentCollection::L1Regularize(float lambda)
{
  return m_scores.l1regularize(lambda);
}

void ScoreComponentCollection::L2Regularize(float lambda)
{
  m_scores.l2regularize(lambda);
}

size_t ScoreComponentCollection::SparseL1Regularize(float lambda)
{
  return m_scores.sparseL1regularize(lambda);
}

void ScoreComponentCollection::SparseL2Regularize(float lambda)
{
  m_scores.sparseL2regularize(lambda);
}

void ScoreComponentCollection::Save(ostream& out) const
{
  ScoreIndexMap::const_iterator iter = s_scoreIndexes.begin();
  for (; iter != s_scoreIndexes.end(); ++iter ) {
    string name = iter->first->GetScoreProducerDescription();
    IndexPair ip = iter->second; // feature indices
    if (ip.second-ip.first == 1) {
      out << name << " " << m_scores[ip.first] << endl;
    } else {
      for (size_t i=ip.first; i < ip.second; ++i) {
        ostringstream fullname;
        fullname << name << "_" << (i + 1 - ip.first);
        out << fullname.str() << " " << m_scores[i] << endl;
      }
    }
  }

  // write sparse features
  m_scores.write(out);
}

void ScoreComponentCollection::Save(const string& filename) const
{
  ofstream out(filename.c_str());
  if (!out) {
    ostringstream msg;
    msg << "Unable to open " << filename;
    throw runtime_error(msg.str());
  }
  Save(out);
  out.close();
}

void ScoreComponentCollection::Assign(const FeatureFunction* sp, const string line)
{
  istringstream istr(line);
  while(istr) {
    string namestring;
    FValue value;
    istr >> namestring;
    if (!istr) break;
    istr >> value;
    FName fname(sp->GetScoreProducerDescription(), namestring);
    m_scores[fname] = value;
  }
}

void ScoreComponentCollection::ZeroDenseFeatures(const FeatureFunction* sp)
{
  size_t numScores = sp->GetNumScoreComponents();
  Scores vec(numScores, 0);

  Assign(sp, vec);
}

//! get subset of scores that belong to a certain sparse ScoreProducer
FVector ScoreComponentCollection::GetVectorForProducer(const FeatureFunction* sp) const
{
  FVector fv(s_denseVectorSize);
  std::string prefix = sp->GetScoreProducerDescription() + FName::SEP;
  for(FVector::FNVmap::const_iterator i = m_scores.cbegin(); i != m_scores.cend(); i++) {
    std::stringstream name;
    name << i->first;
    if (name.str().substr( 0, prefix.length() ).compare( prefix ) == 0)
      fv[i->first] = i->second;
  }
  return fv;
}

}

<commit_msg>minor debug tweak<commit_after>// $Id$
#include <vector>

#include "ScoreComponentCollection.h"
#include "StaticData.h"

using namespace std;

namespace Moses
{

ScoreComponentCollection::ScoreIndexMap ScoreComponentCollection::s_scoreIndexes;
size_t ScoreComponentCollection::s_denseVectorSize = 0;

ScoreComponentCollection::ScoreComponentCollection() : m_scores(s_denseVectorSize)
{}

void ScoreComponentCollection::RegisterScoreProducer
(const FeatureFunction* scoreProducer)
{
  size_t start = s_denseVectorSize;
  size_t end = start + scoreProducer->GetNumScoreComponents();
  VERBOSE(1, "FeatureFunction: " << scoreProducer->GetScoreProducerDescription() << " start: " << start << " end: " << (end-1) << endl);
  s_scoreIndexes[scoreProducer] = pair<size_t,size_t>(start,end);
  s_denseVectorSize = end;
}

float ScoreComponentCollection::GetWeightedScore() const
{
  return m_scores.inner_product(StaticData::Instance().GetAllWeights().m_scores);
}

void ScoreComponentCollection::MultiplyEquals(float scalar)
{
  m_scores *= scalar;
}

// Multiply all weights of this sparse producer by a given scalar
void ScoreComponentCollection::MultiplyEquals(const FeatureFunction* sp, float scalar)
{
  std::string prefix = sp->GetScoreProducerDescription() + FName::SEP;
  for(FVector::FNVmap::const_iterator i = m_scores.cbegin(); i != m_scores.cend(); i++) {
    std::stringstream name;
    name << i->first;
    if (name.str().substr( 0, prefix.length() ).compare( prefix ) == 0)
      m_scores[i->first] = i->second * scalar;
  }
}

// Count weights belonging to this sparse producer
size_t ScoreComponentCollection::GetNumberWeights(const FeatureFunction* sp)
{
  std::string prefix = sp->GetScoreProducerDescription() + FName::SEP;
  size_t weights = 0;
  for(FVector::FNVmap::const_iterator i = m_scores.cbegin(); i != m_scores.cend(); i++) {
    std::stringstream name;
    name << i->first;
    if (name.str().substr( 0, prefix.length() ).compare( prefix ) == 0)
      weights++;
  }
  return weights;
}

void ScoreComponentCollection::DivideEquals(float scalar)
{
  m_scores /= scalar;
}

void ScoreComponentCollection::CoreDivideEquals(float scalar)
{
  m_scores.coreDivideEquals(scalar);
}

void ScoreComponentCollection::DivideEquals(const ScoreComponentCollection& rhs)
{
  m_scores.divideEquals(rhs.m_scores);
}

void ScoreComponentCollection::MultiplyEquals(const ScoreComponentCollection& rhs)
{
  m_scores *= rhs.m_scores;
}

void ScoreComponentCollection::MultiplyEqualsBackoff(const ScoreComponentCollection& rhs, float backoff)
{
  m_scores.multiplyEqualsBackoff(rhs.m_scores, backoff);
}

void ScoreComponentCollection::MultiplyEquals(float core_r0, float sparse_r0)
{
  m_scores.multiplyEquals(core_r0, sparse_r0);
}

std::ostream& operator<<(std::ostream& os, const ScoreComponentCollection& rhs)
{
  os << rhs.m_scores;
  return os;
}
void ScoreComponentCollection::L1Normalise()
{
  m_scores /= m_scores.l1norm_coreFeatures();
}

float ScoreComponentCollection::GetL1Norm() const
{
  return m_scores.l1norm();
}

float ScoreComponentCollection::GetL2Norm() const
{
  return m_scores.l2norm();
}

float ScoreComponentCollection::GetLInfNorm() const
{
  return m_scores.linfnorm();
}

size_t ScoreComponentCollection::L1Regularize(float lambda)
{
  return m_scores.l1regularize(lambda);
}

void ScoreComponentCollection::L2Regularize(float lambda)
{
  m_scores.l2regularize(lambda);
}

size_t ScoreComponentCollection::SparseL1Regularize(float lambda)
{
  return m_scores.sparseL1regularize(lambda);
}

void ScoreComponentCollection::SparseL2Regularize(float lambda)
{
  m_scores.sparseL2regularize(lambda);
}

void ScoreComponentCollection::Save(ostream& out) const
{
  ScoreIndexMap::const_iterator iter = s_scoreIndexes.begin();
  for (; iter != s_scoreIndexes.end(); ++iter ) {
    string name = iter->first->GetScoreProducerDescription();
    IndexPair ip = iter->second; // feature indices
    if (ip.second-ip.first == 1) {
      out << name << " " << m_scores[ip.first] << endl;
    } else {
      for (size_t i=ip.first; i < ip.second; ++i) {
        ostringstream fullname;
        fullname << name << "_" << (i + 1 - ip.first);
        out << fullname.str() << " " << m_scores[i] << endl;
      }
    }
  }

  // write sparse features
  m_scores.write(out);
}

void ScoreComponentCollection::Save(const string& filename) const
{
  ofstream out(filename.c_str());
  if (!out) {
    ostringstream msg;
    msg << "Unable to open " << filename;
    throw runtime_error(msg.str());
  }
  Save(out);
  out.close();
}

void ScoreComponentCollection::Assign(const FeatureFunction* sp, const string line)
{
  istringstream istr(line);
  while(istr) {
    string namestring;
    FValue value;
    istr >> namestring;
    if (!istr) break;
    istr >> value;
    FName fname(sp->GetScoreProducerDescription(), namestring);
    m_scores[fname] = value;
  }
}

void ScoreComponentCollection::ZeroDenseFeatures(const FeatureFunction* sp)
{
  size_t numScores = sp->GetNumScoreComponents();
  Scores vec(numScores, 0);

  Assign(sp, vec);
}

//! get subset of scores that belong to a certain sparse ScoreProducer
FVector ScoreComponentCollection::GetVectorForProducer(const FeatureFunction* sp) const
{
  FVector fv(s_denseVectorSize);
  std::string prefix = sp->GetScoreProducerDescription() + FName::SEP;
  for(FVector::FNVmap::const_iterator i = m_scores.cbegin(); i != m_scores.cend(); i++) {
    std::stringstream name;
    name << i->first;
    if (name.str().substr( 0, prefix.length() ).compare( prefix ) == 0)
      fv[i->first] = i->second;
  }
  return fv;
}

}

<|endoftext|>


<commit_before>//
// Created by jonas on 10.12.15.
//

#include "image.h"

#if IMAGE_MAGICK
#include <Magick++/Image.h>
#include <Magick++/Montage.h>
#include <Magick++/STL.h>

using namespace Magick;
#elif PNG_LIB
#include <png.h>
#endif

void dump_image( Direction direction )
{

  GLint view[4];
  glGetIntegerv( GL_VIEWPORT, view );
  GLint w = view[2];
  GLint h = view[3];

  GLbyte* pixels = new GLbyte[3 * w * h];
  glPixelStorei( GL_PACK_ALIGNMENT, 1 );
  glReadPixels( 0, 0, w, h, GL_RGB, GL_BYTE, pixels );

#if IMAGE_MAGICK
  static list<Image> images;
  for ( int j = 0; j * 2 < h; ++j )
  {
    int x = j * w * 3;
    int y = (h - 1 - j) * w * 3;
    for ( int i = w * 3; i > 0; --i )
    {
      char tmp = pixels[x];
      pixels[x] = pixels[y];
      pixels[y] = tmp;
      x++;
      y++;
    }
  }

  Image image;
  image.read( static_cast<const size_t>(w), static_cast<const size_t>(h), "RGB", CharPixel, pixels );
  image.transparent( "black" );
  image.trim();

  images.push_back(image);

  if( direction == RIGHT)
  {
    Montage m;
    m.backgroundColor("black");
    m.geometry("300x300+2+2");
    m.tile("3x2");
    m.fileName(mesh->get_name()+".png");
    list<Image> mImages;

    montageImages(&mImages, images.begin(), images.end(), m);
    writeImages(mImages.begin(), mImages.end(), m.fileName());
  }
#else
  string name = mesh->get_name();
  name += to_string( direction );
  name += ".png";
  save_png( name, pixels, w, h );
#endif
  delete[] pixels;

}

#if PNG_LIB

bool save_png( const string& name, GLbyte* pixels, GLint w, GLint h )
{
  png_structp png = png_create_write_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL );
  if ( !png )
    return false;

  png_infop info = png_create_info_struct( png );
  if ( !info )
  {
    png_destroy_write_struct( &png, &info );
    return false;
  }

  FILE* fp = fopen( name.c_str(), "wb" );
  if ( !fp )
  {
    png_destroy_write_struct( &png, &info );
    return false;
  }

  png_init_io( png, fp );
  png_set_IHDR( png, info, static_cast<png_uint_32>(w), static_cast<png_uint_32>(h), 8 /* depth */, PNG_COLOR_TYPE_RGB,
                PNG_INTERLACE_NONE,
                PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE );
  png_colorp palette = ( png_colorp ) png_malloc( png, PNG_MAX_PALETTE_LENGTH * sizeof( png_color ));
  if ( !palette )
  {
    fclose( fp );
    png_destroy_write_struct( &png, &info );
    return false;
  }
  png_set_PLTE( png, info, palette, PNG_MAX_PALETTE_LENGTH );
  png_write_info( png, info );
  png_set_packing( png );

  png_bytepp rows = ( png_bytepp ) png_malloc( png, h * sizeof( png_bytep ));
  for ( int i = 0; i < h; ++i )
    rows[i] = ( png_bytep ) (pixels + (h - i - 1) * w * 3);

  png_write_image( png, rows );
  png_write_end( png, info );
  png_free( png, palette );
  png_destroy_write_struct( &png, &info );

  fclose( fp );
  delete[] rows;
  return true;
}
#endif
<commit_msg>insert image_path into the image store method<commit_after>//
// Created by jonas on 10.12.15.
//

#include "image.h"

#if IMAGE_MAGICK
#include <Magick++/Image.h>
#include <Magick++/Montage.h>
#include <Magick++/STL.h>

using namespace Magick;
#elif PNG_LIB
#include <png.h>
#endif

void dump_image( Direction direction )
{

  GLint view[4];
  glGetIntegerv( GL_VIEWPORT, view );
  GLint w = view[2];
  GLint h = view[3];

  GLbyte* pixels = new GLbyte[3 * w * h];
  glPixelStorei( GL_PACK_ALIGNMENT, 1 );
  glReadPixels( 0, 0, w, h, GL_RGB, GL_BYTE, pixels );

#if IMAGE_MAGICK
  static list<Image> images;
  for ( int j = 0; j * 2 < h; ++j )
  {
    int x = j * w * 3;
    int y = (h - 1 - j) * w * 3;
    for ( int i = w * 3; i > 0; --i )
    {
      char tmp = pixels[x];
      pixels[x] = pixels[y];
      pixels[y] = tmp;
      x++;
      y++;
    }
  }

  Image image;
  image.read( static_cast<const size_t>(w), static_cast<const size_t>(h), "RGB", CharPixel, pixels );
  image.transparent( "black" );
  image.trim();

  images.push_back(image);

  if( direction == RIGHT)
  {
    Montage m;
    m.backgroundColor("black");
    m.geometry("300x300+2+2");
    m.tile("3x2");
    m.fileName(image_path + "/" + mesh->get_name()+".png");
    list<Image> mImages;

    montageImages(&mImages, images.begin(), images.end(), m);
    writeImages(mImages.begin(), mImages.end(), m.fileName());
  }
#else
  string name = image_path + "/" + mesh->get_name();
  name += to_string( direction );
  name += ".png";
  save_png( name, pixels, w, h );
#endif
  delete[] pixels;

}

#if PNG_LIB

bool save_png( const string& name, GLbyte* pixels, GLint w, GLint h )
{
  png_structp png = png_create_write_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL );
  if ( !png )
    return false;

  png_infop info = png_create_info_struct( png );
  if ( !info )
  {
    png_destroy_write_struct( &png, &info );
    return false;
  }

  FILE* fp = fopen( name.c_str(), "wb" );
  if ( !fp )
  {
    png_destroy_write_struct( &png, &info );
    return false;
  }

  png_init_io( png, fp );
  png_set_IHDR( png, info, static_cast<png_uint_32>(w), static_cast<png_uint_32>(h), 8 /* depth */, PNG_COLOR_TYPE_RGB,
                PNG_INTERLACE_NONE,
                PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE );
  png_colorp palette = ( png_colorp ) png_malloc( png, PNG_MAX_PALETTE_LENGTH * sizeof( png_color ));
  if ( !palette )
  {
    fclose( fp );
    png_destroy_write_struct( &png, &info );
    return false;
  }
  png_set_PLTE( png, info, palette, PNG_MAX_PALETTE_LENGTH );
  png_write_info( png, info );
  png_set_packing( png );

  png_bytepp rows = ( png_bytepp ) png_malloc( png, h * sizeof( png_bytep ));
  for ( int i = 0; i < h; ++i )
    rows[i] = ( png_bytep ) (pixels + (h - i - 1) * w * 3);

  png_write_image( png, rows );
  png_write_end( png, info );
  png_free( png, palette );
  png_destroy_write_struct( &png, &info );

  fclose( fp );
  delete[] rows;
  return true;
}
#endif
<|endoftext|>