<commit_before>#include <bts/app/api.hpp>
#include <bts/chain/address.hpp>
#include <bts/utilities/key_conversion.hpp>
#include <fc/io/json.hpp>
#include <fc/network/http/websocket.hpp>
#include <fc/rpc/websocket_api.hpp>
#include <fc/io/stdio.hpp>
#include <iostream>
#include <fc/rpc/cli.hpp>
#include <iomanip>

using namespace bts::app;
using namespace bts::chain;
using namespace bts::utilities;
using namespace std;

struct wallet_data
{
   flat_set<account_id_type> accounts;
   map<key_id_type, string>  keys;
   string                    ws_server = "ws://localhost:8090";
   string                    ws_user;
   string                    ws_password;
};
FC_REFLECT( wallet_data, (accounts)(keys)(ws_server)(ws_user)(ws_password) );

/**
 *  This wallet assumes nothing about where the database server is
 *  located and performs minimal caching.  This API could be provided
 *  locally to be used by a web interface.
 */
class wallet_api
{
   public:
      wallet_api( fc::api<login_api> rapi )
      :_remote_api(rapi)
      {
         _remote_db  = _remote_api->database();
         _remote_net = _remote_api->network();
      }
      string  help()const;

      string  suggest_brain_key()const
      {
        return string("dummy");
      }
      variant get_object( object_id_type id )
      {
         return _remote_db->get_objects({id});
      }
      account_object get_account( string account_name_or_id )
      {
         FC_ASSERT( account_name_or_id.size() > 0 );
         vector<optional<account_object>> opt_account;
         if( std::isdigit( account_name_or_id.front() ) )
            opt_account = _remote_db->get_accounts( {fc::variant(account_name_or_id).as<account_id_type>()} );
         else
            opt_account = _remote_db->lookup_account_names( {account_name_or_id} );
         FC_ASSERT( opt_account.size() && opt_account.front() );
         return *opt_account.front();
      }

      bool import_key( string account_name_or_id, string wif_key )
      {
         auto opt_priv_key = wif_to_key(wif_key);
         FC_ASSERT( opt_priv_key.valid() );
         auto wif_key_address = opt_priv_key->get_public_key();

         auto acnt = get_account( account_name_or_id );

         flat_set<key_id_type> keys;
         for( auto item : acnt.active.auths )
         {
             if( item.first.type() == key_object_type )
                keys.insert( item.first );
         }
         for( auto item : acnt.owner.auths )
         {
             if( item.first.type() == key_object_type )
                keys.insert( item.first );
         }
         auto opt_keys = _remote_db->get_keys( vector<key_id_type>(keys.begin(),keys.end()) );
         for( auto opt_key : opt_keys )
         {
            FC_ASSERT( opt_key.valid() );
            if( opt_key->key_address() == wif_key_address )
            {
               _wallet.keys[ opt_key->id ] = wif_key;
               return true;
            }
         }
         ilog( "key not for account ${name}", ("name",account_name_or_id) );
         return false;
      }

      string normalize_brain_key( string s )
      {
          size_t i = 0, n = s.length();
          std::string result;
          char c;
          result.reserve( n );

          bool preceded_by_whitespace = false;
          bool non_empty = false;
          while( i < n )
          {
              c = s[i++];
              switch( c )
              {
                  case ' ':  case '\t': case '\r': case '\n': case '\v': case '\f':
                      preceded_by_whitespace = true;
                      continue;

                  case 'a': c = 'A'; break;
                  case 'b': c = 'B'; break;
                  case 'c': c = 'C'; break;
                  case 'd': c = 'D'; break;
                  case 'e': c = 'E'; break;
                  case 'f': c = 'F'; break;
                  case 'g': c = 'G'; break;
                  case 'h': c = 'H'; break;
                  case 'i': c = 'I'; break;
                  case 'j': c = 'J'; break;
                  case 'k': c = 'K'; break;
                  case 'l': c = 'L'; break;
                  case 'm': c = 'M'; break;
                  case 'n': c = 'N'; break;
                  case 'o': c = 'O'; break;
                  case 'p': c = 'P'; break;
                  case 'q': c = 'Q'; break;
                  case 'r': c = 'R'; break;
                  case 's': c = 'S'; break;
                  case 't': c = 'T'; break;
                  case 'u': c = 'U'; break;
                  case 'v': c = 'V'; break;
                  case 'w': c = 'W'; break;
                  case 'x': c = 'X'; break;
                  case 'y': c = 'Y'; break;
                  case 'z': c = 'Z'; break;

                  default:
                      break;
              }
              if( preceded_by_whitespace && non_empty )
                  result.push_back(' ');
              result.push_back(c);
              preceded_by_whitespace = false;
              non_empty = true;
          }

          return result;
      }

      fc::ecc::private_key derive_private_key(
          const std::string& prefix_string, int sequence_number)
      {
           std::string sequence_string = std::to_string(sequence_number);
           fc::sha512 h = fc::sha512::hash(prefix_string + " " + sequence_string);
           fc::ecc::private_key derived_key = fc::ecc::private_key::regenerate(fc::sha256::hash(h));
           return derived_key;
      }

      signed_transaction create_account_with_brain_key(
          string brain_key,
          string account_name,
          string pay_from_account
          )
      {
        // TODO:  process when pay_from_account is ID

        account_object pay_from_account_object =
            this->get_account( pay_from_account );

        account_id_type pay_from_account_id = pay_from_account_object.id;

        string normalized_brain_key = normalize_brain_key( brain_key );
        // TODO:  scan blockchain for accounts that exist with same brain key
        fc::ecc::private_key owner_privkey = derive_private_key( normalized_brain_key, 0 );
        fc::ecc::private_key active_privkey = derive_private_key( key_to_wif(owner_privkey), 0);

        bts::chain::public_key_type owner_pubkey = owner_privkey.get_public_key();
        bts::chain::public_key_type active_pubkey = active_privkey.get_public_key();

        // get pay_from_account_id
        key_create_operation owner_key_create_op;
        owner_key_create_op.fee_paying_account = pay_from_account_id;
        owner_key_create_op.key_data = owner_pubkey;

        key_create_operation active_key_create_op;
        active_key_create_op.fee_paying_account = pay_from_account_id;
        active_key_create_op.key_data = active_pubkey;

        // key_create_op.calculate_fee(db.current_fee_schedule());

        // TODO:  Check if keys already exist!!!

        account_create_operation account_create_op;

        vector<string> v_pay_from_account;
        v_pay_from_account.push_back( pay_from_account );

        account_create_op.fee_paying_account = pay_from_account_id;

        relative_key_id_type owner_rkid(0);
        relative_key_id_type active_rkid(1);

        account_create_op.name = account_name;
        account_create_op.owner = authority(1, owner_rkid, 1);
        account_create_op.active = authority(1, active_rkid, 1);
        account_create_op.memo_key = active_rkid;
        account_create_op.voting_key = active_rkid;
        account_create_op.vote = flat_set<vote_tally_id_type>();

        // current_fee_schedule()
        // find_account(pay_from_account)

        // account_create_op.fee = account_create_op.calculate_fee(db.current_fee_schedule());

        signed_transaction tx;

        tx.operations.push_back( owner_key_create_op );
        tx.operations.push_back( active_key_create_op );
        tx.operations.push_back( account_create_op );

        tx.visit( operation_set_fee( _remote_db->get_global_properties().parameters.current_fees ) );

        vector<key_id_type> paying_keys = pay_from_account_object.active.get_keys();

        tx.validate();

        for( key_id_type& key : paying_keys )
        {
            auto it = _wallet.keys.find(key);
            if( it != _wallet.keys.end() )
            {
                fc::optional< fc::ecc::private_key > privkey = wif_to_key( it->second );
                if( !privkey.valid() )
                {
                    FC_ASSERT( false, "Malformed private key in _wallet.keys" );
                }
                tx.sign( *privkey );
            }
        }

        return tx;
      }

      signed_transaction transfer( string from,
                                   string to,
                                   uint64_t amount,
                                   string asset_symbol,
                                   string memo,
                                   bool broadcast = false )
      {
        auto opt_asset = _remote_db->lookup_asset_symbols( {asset_symbol} );
        wdump( (opt_asset) );
        return signed_transaction();
      }

      wallet_data             _wallet;
      fc::api<login_api>      _remote_api;
      fc::api<database_api>   _remote_db;
      fc::api<network_api>    _remote_net;
};

FC_API( wallet_api,
        (help)
        (import_key)
        (suggest_brain_key)
        (create_account_with_brain_key)
        (transfer)
        (get_account)
        (get_object)
        (normalize_brain_key)
       )

struct help_visitor
{
   help_visitor( std::stringstream& s ):ss(s){}
   std::stringstream& ss;
   template<typename R, typename... Args>
   void operator()( const char* name, std::function<R(Args...)>& memb )const {
      ss << std::setw(40) << std::left << fc::get_typename<R>::name() << " " << name << "( ";
      vector<string> args{ fc::get_typename<Args>::name()... };
      for( uint32_t i = 0; i < args.size(); ++i )
         ss << args[i] << (i==args.size()-1?" ":", ");
      ss << ")\n";
   }

};
string  wallet_api::help()const
{
   fc::api<wallet_api> tmp;
   std::stringstream ss;
   tmp->visit( help_visitor(ss) );
   return ss.str();
}

int main( int argc, char** argv )
{
   try {
      FC_ASSERT( argc > 1, "usage: ${cmd} WALLET_FILE", ("cmd",argv[0]) );

      fc::ecc::private_key genesis_private_key = fc::ecc::private_key::regenerate(fc::sha256::hash(string("genesis")));
      idump( (key_to_wif( genesis_private_key ) ) );
      idump( (account_id_type()) );

      wallet_data wallet;
      fc::path wallet_file(argv[1]);
      if( fc::exists( wallet_file ) )
          wallet = fc::json::from_file( wallet_file ).as<wallet_data>();

      fc::http::websocket_client client;
      auto con  = client.connect( wallet.ws_server );
      auto apic = std::make_shared<fc::rpc::websocket_api_connection>(*con);
      con->closed.connect( [&](){ elog( "connection closed" ); } );

      auto remote_api = apic->get_remote_api< login_api >();
      FC_ASSERT( remote_api->login( wallet.ws_user, wallet.ws_password ) );

      auto wapiptr = std::make_shared<wallet_api>(remote_api);
      wapiptr->_wallet = wallet;

      fc::api<wallet_api> wapi(wapiptr);

      auto wallet_cli = std::make_shared<fc::rpc::cli>();
      wallet_cli->format_result( "help", [&]( variant result, const fc::variants& a) {
                                    return result.get_string();
                                });
      wallet_cli->register_api( wapi );
      wallet_cli->start();
      wallet_cli->wait();
   }
   catch ( const fc::exception& e )
   {
      std::cout << e.to_detail_string() << "\n";
   }
   return -1;
}
<commit_msg>Fix Build<commit_after>#include <bts/app/api.hpp>
#include <bts/chain/address.hpp>
#include <bts/utilities/key_conversion.hpp>
#include <fc/io/json.hpp>
#include <fc/network/http/websocket.hpp>
#include <fc/rpc/websocket_api.hpp>
#include <fc/io/stdio.hpp>
#include <iostream>
#include <fc/rpc/cli.hpp>
#include <iomanip>

using namespace bts::app;
using namespace bts::chain;
using namespace bts::utilities;
using namespace std;

struct wallet_data
{
   flat_set<account_id_type> accounts;
   map<key_id_type, string>  keys;
   string                    ws_server = "ws://localhost:8090";
   string                    ws_user;
   string                    ws_password;
};
FC_REFLECT( wallet_data, (accounts)(keys)(ws_server)(ws_user)(ws_password) );

/**
 *  This wallet assumes nothing about where the database server is
 *  located and performs minimal caching.  This API could be provided
 *  locally to be used by a web interface.
 */
class wallet_api
{
   public:
      wallet_api( fc::api<login_api> rapi )
      :_remote_api(rapi)
      {
         _remote_db  = _remote_api->database();
         _remote_net = _remote_api->network();
      }
      string  help()const;

      string  suggest_brain_key()const
      {
        return string("dummy");
      }
      variant get_object( object_id_type id )
      {
         return _remote_db->get_objects({id});
      }
      account_object get_account( string account_name_or_id )
      {
         FC_ASSERT( account_name_or_id.size() > 0 );
         vector<optional<account_object>> opt_account;
         if( std::isdigit( account_name_or_id.front() ) )
            opt_account = _remote_db->get_accounts( {fc::variant(account_name_or_id).as<account_id_type>()} );
         else
            opt_account = _remote_db->lookup_account_names( {account_name_or_id} );
         FC_ASSERT( opt_account.size() && opt_account.front() );
         return *opt_account.front();
      }

      bool import_key( string account_name_or_id, string wif_key )
      {
         auto opt_priv_key = wif_to_key(wif_key);
         FC_ASSERT( opt_priv_key.valid() );
         auto wif_key_address = opt_priv_key->get_public_key();

         auto acnt = get_account( account_name_or_id );

         flat_set<key_id_type> keys;
         for( auto item : acnt.active.auths )
         {
             if( item.first.type() == key_object_type )
                keys.insert( item.first );
         }
         for( auto item : acnt.owner.auths )
         {
             if( item.first.type() == key_object_type )
                keys.insert( item.first );
         }
         auto opt_keys = _remote_db->get_keys( vector<key_id_type>(keys.begin(),keys.end()) );
         for( auto opt_key : opt_keys )
         {
            FC_ASSERT( opt_key.valid() );
            if( opt_key->key_address() == wif_key_address )
            {
               _wallet.keys[ opt_key->id ] = wif_key;
               return true;
            }
         }
         ilog( "key not for account ${name}", ("name",account_name_or_id) );
         return false;
      }

      string normalize_brain_key( string s )
      {
          size_t i = 0, n = s.length();
          std::string result;
          char c;
          result.reserve( n );

          bool preceded_by_whitespace = false;
          bool non_empty = false;
          while( i < n )
          {
              c = s[i++];
              switch( c )
              {
                  case ' ':  case '\t': case '\r': case '\n': case '\v': case '\f':
                      preceded_by_whitespace = true;
                      continue;

                  case 'a': c = 'A'; break;
                  case 'b': c = 'B'; break;
                  case 'c': c = 'C'; break;
                  case 'd': c = 'D'; break;
                  case 'e': c = 'E'; break;
                  case 'f': c = 'F'; break;
                  case 'g': c = 'G'; break;
                  case 'h': c = 'H'; break;
                  case 'i': c = 'I'; break;
                  case 'j': c = 'J'; break;
                  case 'k': c = 'K'; break;
                  case 'l': c = 'L'; break;
                  case 'm': c = 'M'; break;
                  case 'n': c = 'N'; break;
                  case 'o': c = 'O'; break;
                  case 'p': c = 'P'; break;
                  case 'q': c = 'Q'; break;
                  case 'r': c = 'R'; break;
                  case 's': c = 'S'; break;
                  case 't': c = 'T'; break;
                  case 'u': c = 'U'; break;
                  case 'v': c = 'V'; break;
                  case 'w': c = 'W'; break;
                  case 'x': c = 'X'; break;
                  case 'y': c = 'Y'; break;
                  case 'z': c = 'Z'; break;

                  default:
                      break;
              }
              if( preceded_by_whitespace && non_empty )
                  result.push_back(' ');
              result.push_back(c);
              preceded_by_whitespace = false;
              non_empty = true;
          }

          return result;
      }

      fc::ecc::private_key derive_private_key(
          const std::string& prefix_string, int sequence_number)
      {
           std::string sequence_string = std::to_string(sequence_number);
           fc::sha512 h = fc::sha512::hash(prefix_string + " " + sequence_string);
           fc::ecc::private_key derived_key = fc::ecc::private_key::regenerate(fc::sha256::hash(h));
           return derived_key;
      }

      signed_transaction create_account_with_brain_key(
          string brain_key,
          string account_name,
          string pay_from_account
          )
      {
        // TODO:  process when pay_from_account is ID

        account_object pay_from_account_object =
            this->get_account( pay_from_account );

        account_id_type pay_from_account_id = pay_from_account_object.id;

        string normalized_brain_key = normalize_brain_key( brain_key );
        // TODO:  scan blockchain for accounts that exist with same brain key
        fc::ecc::private_key owner_privkey = derive_private_key( normalized_brain_key, 0 );
        fc::ecc::private_key active_privkey = derive_private_key( key_to_wif(owner_privkey), 0);

        bts::chain::public_key_type owner_pubkey = owner_privkey.get_public_key();
        bts::chain::public_key_type active_pubkey = active_privkey.get_public_key();

        // get pay_from_account_id
        key_create_operation owner_key_create_op;
        owner_key_create_op.fee_paying_account = pay_from_account_id;
        owner_key_create_op.key_data = owner_pubkey;

        key_create_operation active_key_create_op;
        active_key_create_op.fee_paying_account = pay_from_account_id;
        active_key_create_op.key_data = active_pubkey;

        // key_create_op.calculate_fee(db.current_fee_schedule());

        // TODO:  Check if keys already exist!!!

        account_create_operation account_create_op;

        vector<string> v_pay_from_account;
        v_pay_from_account.push_back( pay_from_account );

        account_create_op.registrar = pay_from_account_id;

        relative_key_id_type owner_rkid(0);
        relative_key_id_type active_rkid(1);

        account_create_op.name = account_name;
        account_create_op.owner = authority(1, owner_rkid, 1);
        account_create_op.active = authority(1, active_rkid, 1);
        account_create_op.memo_key = active_rkid;
        account_create_op.voting_key = active_rkid;
        account_create_op.vote = flat_set<vote_tally_id_type>();

        // current_fee_schedule()
        // find_account(pay_from_account)

        // account_create_op.fee = account_create_op.calculate_fee(db.current_fee_schedule());

        signed_transaction tx;

        tx.operations.push_back( owner_key_create_op );
        tx.operations.push_back( active_key_create_op );
        tx.operations.push_back( account_create_op );

        tx.visit( operation_set_fee( _remote_db->get_global_properties().parameters.current_fees ) );

        vector<key_id_type> paying_keys = pay_from_account_object.active.get_keys();

        tx.validate();

        for( key_id_type& key : paying_keys )
        {
            auto it = _wallet.keys.find(key);
            if( it != _wallet.keys.end() )
            {
                fc::optional< fc::ecc::private_key > privkey = wif_to_key( it->second );
                if( !privkey.valid() )
                {
                    FC_ASSERT( false, "Malformed private key in _wallet.keys" );
                }
                tx.sign( *privkey );
            }
        }

        return tx;
      }

      signed_transaction transfer( string from,
                                   string to,
                                   uint64_t amount,
                                   string asset_symbol,
                                   string memo,
                                   bool broadcast = false )
      {
        auto opt_asset = _remote_db->lookup_asset_symbols( {asset_symbol} );
        wdump( (opt_asset) );
        return signed_transaction();
      }

      wallet_data             _wallet;
      fc::api<login_api>      _remote_api;
      fc::api<database_api>   _remote_db;
      fc::api<network_api>    _remote_net;
};

FC_API( wallet_api,
        (help)
        (import_key)
        (suggest_brain_key)
        (create_account_with_brain_key)
        (transfer)
        (get_account)
        (get_object)
        (normalize_brain_key)
       )

struct help_visitor
{
   help_visitor( std::stringstream& s ):ss(s){}
   std::stringstream& ss;
   template<typename R, typename... Args>
   void operator()( const char* name, std::function<R(Args...)>& memb )const {
      ss << std::setw(40) << std::left << fc::get_typename<R>::name() << " " << name << "( ";
      vector<string> args{ fc::get_typename<Args>::name()... };
      for( uint32_t i = 0; i < args.size(); ++i )
         ss << args[i] << (i==args.size()-1?" ":", ");
      ss << ")\n";
   }

};
string  wallet_api::help()const
{
   fc::api<wallet_api> tmp;
   std::stringstream ss;
   tmp->visit( help_visitor(ss) );
   return ss.str();
}

int main( int argc, char** argv )
{
   try {
      FC_ASSERT( argc > 1, "usage: ${cmd} WALLET_FILE", ("cmd",argv[0]) );

      fc::ecc::private_key genesis_private_key = fc::ecc::private_key::regenerate(fc::sha256::hash(string("genesis")));
      idump( (key_to_wif( genesis_private_key ) ) );
      idump( (account_id_type()) );

      wallet_data wallet;
      fc::path wallet_file(argv[1]);
      if( fc::exists( wallet_file ) )
          wallet = fc::json::from_file( wallet_file ).as<wallet_data>();

      fc::http::websocket_client client;
      auto con  = client.connect( wallet.ws_server );
      auto apic = std::make_shared<fc::rpc::websocket_api_connection>(*con);
      con->closed.connect( [&](){ elog( "connection closed" ); } );

      auto remote_api = apic->get_remote_api< login_api >();
      FC_ASSERT( remote_api->login( wallet.ws_user, wallet.ws_password ) );

      auto wapiptr = std::make_shared<wallet_api>(remote_api);
      wapiptr->_wallet = wallet;

      fc::api<wallet_api> wapi(wapiptr);

      auto wallet_cli = std::make_shared<fc::rpc::cli>();
      wallet_cli->format_result( "help", [&]( variant result, const fc::variants& a) {
                                    return result.get_string();
                                });
      wallet_cli->register_api( wapi );
      wallet_cli->start();
      wallet_cli->wait();
   }
   catch ( const fc::exception& e )
   {
      std::cout << e.to_detail_string() << "\n";
   }
   return -1;
}
<|endoftext|>


<commit_before>//#include "csimplexnd.h"
#include <math.h>
#include <QVector>
#include <QFile>
#include <QStringList>
#include <iostream>
#include "graph.h"
#include <QApplication>

int main(int argc, char* argv[])
{
    QString parameters;
    QStringList arguments;
    bool consoleonly=false;

    for(int i=1; i<argc; ++i)
    {
        if(argv[i][0]=='-')
            parameters+=QString::fromLocal8Bit(argv[i]+1);
        else
            arguments<<QString::fromLocal8Bit(argv[i]);
    }

    if (!parameters.isEmpty())
    {
        for(int i=0; i<parameters.length(); ++i)
        {
            switch(parameters.at(i).toAscii()){
            case 'c':
                consoleonly=true;
                break;
            default:
                goto usage;
                break;
            }
        }
    }

    if(argc<2)
    {
        usage:
        std::cerr<<"Usage:\n"
                <<"\t"<<argv[0]<<" [-c] datafile(s)\n"
                <<"\tAnd ASCII table to append to on stdin\n"
                <<"\tc - console only, no gui\n";
        return 1;
    }

    QFile ft;
    QStringList table;
    if(ft.open(0,QFile::ReadOnly))
    {
        while(true)
        {
            QByteArray buf=ft.readLine();
            if(buf.isEmpty())
                break;
            QString line=QString::fromLocal8Bit(buf);
            if(line.startsWith('#'))
                std::cout<<line.toLocal8Bit().data();
            else if(!line.trimmed().isEmpty())
                table<<line.trimmed();
        }
        ft.close();
    }

    if(table.count()<=1)
    {
        for(int i=0;i<arguments.count();++i)
            table<<"";
    } else if(table.count()!=arguments.count()+1)
    {
        std::cerr<<"Invalid ascii table specified";
        return 1;
    }

    table.first().append("\tFile\t"
                         "Simplex Resonance freq, Hz\t"
                         "Simplex Resonance ampl, V\t"
                         "Simplex Antiresonance freq, Hz\t"
                         "Simplex Antiresonance ampl, V\t"
                         "St. dev., V\t"
                         "Mean Noise, V");

    gsl_vector *units;
    units=gsl_vector_alloc(PARAM_COUNT);

    gsl_vector *a;
    a=gsl_vector_alloc(PARAM_COUNT);

    bool useC0=false ,useLm=false, useU=false, useR0=false;
    double inLm=0, inC0=0, inU=0, inR0=0;

    for(int ifile=0;ifile<arguments.count();++ifile)
    {
        QFile f(arguments[ifile]);
        if (!f.open(QIODevice::ReadOnly))
        {
            std::cerr<<"could not open "<<arguments.at(ifile).toLocal8Bit().data();
            return 2;
        }

        QVector<Point2D> func_data;//I(f)
        while(!f.atEnd())
        {
            QStringList line = QString::fromAscii(f.readLine()).split('\t');
            func_data<<fPoint2D(line[0].toDouble(),line[1].toDouble());
        }
        f.close();

        QVector<Point2D> func_sm_data; //smoothen initial data and lessen number of points
        double noise=0;
        int step=50;
        for(int i=step/2;i<func_data.count()-step/2;++i)
        {
            double y=0;
            double x=0;
            for(int k=i-step/2; k<i+step/2;++k)
            {
                x+=func_data[k].x;
                y+=func_data[k].y;
            }
            x/=step;
            y/=step;
            func_sm_data<<fPoint2D(func_data[i].x,y);
            noise+=pow(func_sm_data.last().y-func_data[i].y,2);
        }
        noise/=func_sm_data.count();//Standard deviation from mean

        Point2D Res, Antires;
        int resi,aresi;
        Res=find_extremum(func_sm_data,true,&resi);
        Antires=find_extremum(func_sm_data,false,&aresi);

        //experimental
        if(ifile==0) //we only need initial parameters if it's first run, for other files in serise,
            //parameters do not change much
        {
            double fr=Res.x, fa=Antires.x, Ir=Res.y, Ia=Antires.y;

            useC0=false;
            useLm=false;

            double C0,Lm,Rm,Cm,R0,U;
            R0=1000;
            U=5;
            double Zmax=U*R2/Ia,
            Zmin=U*R2/Ir;
            Rm=Zmin;
            if(!useC0) C0=1/sqrt(4*pow(PI,2)*pow(fa,2)*Rm*Zmax);
            else C0=inC0;
            if(!useLm) Lm=1/(4*pow(PI,2)*C0*(pow(fa,2)-pow(fr,2)));
            else Lm=inLm;
            Cm=1/(4*PI*PI*fr*fr*Lm);

            gsl_vector_set(a,0,Rm);
            gsl_vector_set(a,1,Lm);
            gsl_vector_set(a,2,Cm);
            gsl_vector_set(a,3,C0);
            gsl_vector_set(a,4,U);
            gsl_vector_set(a,5,R0);

            gsl_vector_set_all(units,1e-10);
            gsl_vector_mul(units,a);

            gsl_vector_set_all(a,1e10);
        }

        int gstatus=0;
        do{
            {//update Cm if f0 changed a bit too dramatically
                double Lm = useLm ? inLm : gsl_vector_get(a,1)*gsl_vector_get(units,1);
                double f0=1/(2*PI*sqrt(Lm*gsl_vector_get(a,2)*gsl_vector_get(units,2)));
                if(f0<func_data.first().x || f0>func_data.last().x)
                    gsl_vector_set(a,2,1/(gsl_vector_get(units,2)*4*PI*PI*Res.x*Res.x*Lm));
            }

            gsl_vector *ss;//step size
            ss=gsl_vector_alloc(PARAM_COUNT);
            gsl_vector_set_all(ss, 1e-2);
            gsl_vector_mul(ss,a);

            gsl_multimin_function func;
            func.n=PARAM_COUNT;
            func.f=&StDev;

            param_struct func_params;

            func_params.data=&func_sm_data;
            func_params.resi=resi;
            func_params.aresi=aresi;

            func_params.f_min=func_data.first().x;
            func_params.f_max=func_data.last().x;
            func_params.RmU=gsl_vector_get(units,0);
            func_params.LmU=gsl_vector_get(units,1);
            func_params.CmU=gsl_vector_get(units,2);
            func_params.C0U=gsl_vector_get(units,3);
            func_params.UU=gsl_vector_get(units,4);
            func_params.R0U=gsl_vector_get(units,5);

            func_params.Rm=-1;
            func_params.Lm=-1;
            func_params.Cm=-1;
            func_params.C0=-1;
            func_params.U=-1;
            func_params.R0=-1;

            if(useLm) func_params.Lm=inLm;
            if(useC0) func_params.C0=inC0;
            if(useU) func_params.U=inU;
            if(useR0) func_params.R0=inR0;

            func.params=(void*)&func_params;

            gsl_multimin_fminimizer * min =
                    gsl_multimin_fminimizer_alloc (gsl_multimin_fminimizer_nmsimplex, PARAM_COUNT);
            gsl_multimin_fminimizer_set(min, &func, a, ss);
            int status;
            size_t iter=0;
            double oldsize;

            do
            {
                iter++;
                if(iter % 10==0)
                    oldsize=gsl_multimin_fminimizer_size(min);
                status = gsl_multimin_fminimizer_iterate(min);

                if (status)
                    break;

                double size = gsl_multimin_fminimizer_size(min);
                //status = gsl_multimin_test_size (size, 1e-10);
                if(size!=oldsize || iter%10!=9 )
                    status=GSL_CONTINUE;
                else
                    status=GSL_SUCCESS;

                if (status == GSL_SUCCESS)
                {
                    std::cerr
                            <<"converged to minimum in \""<<f.fileName().toLocal8Bit().data()<<"\" at\n"
                            <<"Rm="<<gsl_vector_get (min->x, 0)*gsl_vector_get (units, 0)<<"\t"
                            <<"Lm=";
                    if(!useLm) std::cerr<<gsl_vector_get (min->x, 1)*gsl_vector_get (units, 1);
                    else std::cerr<<inLm;
                    std::cerr<<"\t"
                            <<"Cm="<<gsl_vector_get (min->x, 2)*gsl_vector_get (units, 2)<<"\t"
                            <<"C0=";
                    if(!useC0) std::cerr<<gsl_vector_get (min->x, 3)*gsl_vector_get (units, 3);
                    else std::cerr<<inC0;
                    std::cerr<<"\n"
                            <<"StDev="<<min->fval<<"\n"
                            <<"Noise="<<noise<<"\n";
                } else {
                    std::cerr<<iter<<"\t"
                            <<min->fval<<"\t"
                            <<size<<"\n";
                }
            }
            while (status == GSL_CONTINUE && iter < 10000);

            QVector<qreal> X_exp,Y_exp,X_f,Y_f;

            foreach(Point2D P, func_data)
            {
                X_exp.push_back(P.x);
                Y_exp.push_back(P.y);
            }

            double maxf=func_data[0].x;
            double maxI=If(min->x,&func_params,maxf);
            double minf=maxf;
            double minI=If(min->x,&func_params,maxf);

            for(double freq=func_data[0].x; freq<func_data.last().x; ++freq)
            {
                double I=If(min->x,&func_params, freq);
                X_f.push_back(freq);
                Y_f.push_back(I);
                if(I>maxI)
                {
                    maxI=I;
                    maxf=freq;
                }

                if(I<minI)
                {
                    minI=I;
                    minf=freq;
                }
            }

            gsl_vector_memcpy(a, min->x);

            gsl_vector *par;
            par = gsl_vector_alloc(PARAM_COUNT);
            gsl_vector_memcpy(par, min->x);
            gsl_vector_mul(par,units);
            if(useLm) gsl_vector_set(par,1,inLm);
            if(useC0) gsl_vector_set(par,3,inC0);
            if(useU) gsl_vector_set(par,4,inU);
            if(useR0) gsl_vector_set(par,5,inR0);

            gsl_multimin_fminimizer_free(min);
            gsl_vector_free(ss);

            QApplication app(argc,argv);
            Graph g(X_exp, Y_exp, X_f, Y_f, gsl_vector_get(par,0), gsl_vector_get(par,1),
                    gsl_vector_get(par,2), gsl_vector_get(par,4), gsl_vector_get(par,3),
                    gsl_vector_get(par,5), minf, minI, maxf, maxI);
            g.setWindowTitle(f.fileName());
            if(!consoleonly)
                gstatus=g.exec();
            else
                gstatus=QDialog::Accepted; //if it was not shown, we have to agree :)
            if(gstatus==QDialog::Rejected)
            {
                gsl_vector_set(par,0,g.Rm());
                gsl_vector_set(par,1,g.Lm());
                gsl_vector_set(par,2,g.Cm());
                gsl_vector_set(par,3,g.C0());
                gsl_vector_set(par,4,g.U());
                gsl_vector_set(par,5,g.R0());
                gsl_vector_memcpy(a,par);
                gsl_vector_div(a,units);
            } else {
                QString buf;
                table[ifile+1].append("\t"+f.fileName()+"\t");
                table[ifile+1].append(buf.setNum(maxf)+"\t");
                table[ifile+1].append(buf.setNum(maxI)+"\t");
                table[ifile+1].append(buf.setNum(minf)+"\t");
                table[ifile+1].append(buf.setNum(minI)+"\t");
                table[ifile+1].append(buf.setNum(min->fval)+"\t");
                table[ifile+1].append(buf.setNum(noise));
            }

            if(ifile==0)
            {
                useLm=true;
                inLm=gsl_vector_get(par,1);
                useC0=true;
                inC0=gsl_vector_get(par,3);
                //            useU=true;
                //            inU=gsl_vector_get(par,4);
                useR0=true;
                inR0=gsl_vector_get(par,5);
            }
            gsl_vector_free(par);
        }while(gstatus==QDialog::Rejected);
    }
    gsl_vector_free(a);
    gsl_vector_free(units);
    foreach(QString s, table)
        std::cout<<s.toLocal8Bit().data()<<"\n";
}

<commit_msg>optimized some repetitive code, added few conditions on loop, fixed some warnings.<commit_after>//#include "csimplexnd.h"
#include <math.h>
#include <QVector>
#include <QFile>
#include <QStringList>
#include <iostream>
#include "graph.h"
#include <QApplication>

int main(int argc, char* argv[])
{
    QString parameters;
    QStringList arguments;
    bool consoleonly=false;

    for(int i=1; i<argc; ++i)
    {
        if(argv[i][0]=='-')
            parameters+=QString::fromLocal8Bit(argv[i]+1);
        else
            arguments<<QString::fromLocal8Bit(argv[i]);
    }

    if (!parameters.isEmpty())
    {
        for(int i=0; i<parameters.length(); ++i)
        {
            switch(parameters.at(i).toAscii()){
            case 'c':
                consoleonly=true;
                break;
            default:
                goto usage;
                break;
            }
        }
    }

    if(argc<2)
    {
        usage:
        std::cerr<<"Usage:\n"
                <<"\t"<<argv[0]<<" [-c] datafile(s)\n"
                <<"\tAnd ASCII table to append to on stdin\n"
                <<"\tc - console only, no gui\n";
        return 1;
    }

    QFile ft;
    QStringList table;
    if(ft.open(0,QFile::ReadOnly))
    {
        while(true)
        {
            QByteArray buf=ft.readLine();
            if(buf.isEmpty())
                break;
            QString line=QString::fromLocal8Bit(buf);
            if(line.startsWith('#'))
                std::cout<<line.toLocal8Bit().data();
            else if(!line.trimmed().isEmpty())
                table<<line.trimmed();
        }
        ft.close();
    }

    if(table.count()<=1)
    {
        for(int i=0;i<arguments.count();++i)
            table<<"";
    } else if(table.count()!=arguments.count()+1)
    {
        std::cerr<<"Invalid ascii table specified";
        return 1;
    }

    table.first().append("\tFile\t"
                         "Simplex Resonance freq, Hz\t"
                         "Simplex Resonance ampl, V\t"
                         "Simplex Antiresonance freq, Hz\t"
                         "Simplex Antiresonance ampl, V\t"
                         "St. dev., V\t"
                         "Mean Noise, V");

    gsl_vector *units;
    units=gsl_vector_alloc(PARAM_COUNT);

    gsl_vector *a;
    a=gsl_vector_alloc(PARAM_COUNT);

    bool useC0=false ,useLm=false, useU=false, useR0=false;
    double inLm=0, inC0=0, inU=0, inR0=0;

    for(int ifile=0;ifile<arguments.count();++ifile)
    {
        QFile f(arguments[ifile]);
        if (!f.open(QIODevice::ReadOnly))
        {
            std::cerr<<"could not open "<<arguments.at(ifile).toLocal8Bit().data();
            return 2;
        }

        QVector<Point2D> func_data;//I(f)
        while(!f.atEnd())
        {
            QStringList line = QString::fromAscii(f.readLine()).split('\t');
            func_data<<fPoint2D(line[0].toDouble(),line[1].toDouble());
        }
        f.close();

        QVector<Point2D> func_sm_data; //smoothen initial data and lessen number of points
        double noise=0;
        int step=50;
        for(int i=step/2;i<func_data.count()-step/2;++i)
        {
            double y=0;
            double x=0;
            for(int k=i-step/2; k<i+step/2;++k)
            {
                x+=func_data[k].x;
                y+=func_data[k].y;
            }
            x/=step;
            y/=step;
            func_sm_data<<fPoint2D(func_data[i].x,y);
            noise+=pow(func_sm_data.last().y-func_data[i].y,2);
        }
        noise/=func_sm_data.count();//Standard deviation from mean

        Point2D Res, Antires;
        int resi,aresi;
        Res=find_extremum(func_sm_data,true,&resi);
        Antires=find_extremum(func_sm_data,false,&aresi);

        //experimental
        if(ifile==0) //we only need initial parameters if it's first run, for other files in serise,
            //parameters do not change much
        {
            double fr=Res.x, fa=Antires.x, Ir=Res.y, Ia=Antires.y;

            useC0=false;
            useLm=false;

            double C0,Lm,Rm,Cm,R0,U;
            R0=1000;
            U=5;
            double Zmax=U*R2/Ia,
            Zmin=U*R2/Ir;
            Rm=Zmin;
            if(!useC0) C0=1/sqrt(4*pow(PI,2)*pow(fa,2)*Rm*Zmax);
            else C0=inC0;
            if(!useLm) Lm=1/(4*pow(PI,2)*C0*(pow(fa,2)-pow(fr,2)));
            else Lm=inLm;
            Cm=1/(4*PI*PI*fr*fr*Lm);

            gsl_vector_set(a,0,Rm);
            gsl_vector_set(a,1,Lm);
            gsl_vector_set(a,2,Cm);
            gsl_vector_set(a,3,C0);
            gsl_vector_set(a,4,U);
            gsl_vector_set(a,5,R0);

            gsl_vector_set_all(units,1e-10);
            gsl_vector_mul(units,a);

            gsl_vector_set_all(a,1e10);
        }

        int gstatus=0;
        do{
            {//update Cm if f0 changed a bit too dramatically
                double Lm = useLm ? inLm : gsl_vector_get(a,1)*gsl_vector_get(units,1);
                double f0=1/(2*PI*sqrt(Lm*gsl_vector_get(a,2)*gsl_vector_get(units,2)));
                if(f0<func_data.first().x || f0>func_data.last().x)
                    gsl_vector_set(a,2,1/(gsl_vector_get(units,2)*4*PI*PI*Res.x*Res.x*Lm));
            }

            gsl_vector *ss;//step size
            ss=gsl_vector_alloc(PARAM_COUNT);
            gsl_vector_set_all(ss, 1e-2);
            gsl_vector_mul(ss,a);

            gsl_multimin_function func;
            func.n=PARAM_COUNT;
            func.f=&StDev;

            param_struct func_params;

            func_params.data=&func_sm_data;
            func_params.resi=resi;
            func_params.aresi=aresi;

            func_params.f_min=func_data.first().x;
            func_params.f_max=func_data.last().x;
            func_params.RmU=gsl_vector_get(units,0);
            func_params.LmU=gsl_vector_get(units,1);
            func_params.CmU=gsl_vector_get(units,2);
            func_params.C0U=gsl_vector_get(units,3);
            func_params.UU=gsl_vector_get(units,4);
            func_params.R0U=gsl_vector_get(units,5);

            func_params.Rm=-1;
            func_params.Lm=-1;
            func_params.Cm=-1;
            func_params.C0=-1;
            func_params.U=-1;
            func_params.R0=-1;

            if(useLm) func_params.Lm=inLm;
            if(useC0) func_params.C0=inC0;
            if(useU) func_params.U=inU;
            if(useR0) func_params.R0=inR0;

            func.params=(void*)&func_params;

            gsl_multimin_fminimizer * min =
                    gsl_multimin_fminimizer_alloc (gsl_multimin_fminimizer_nmsimplex, PARAM_COUNT);
            gsl_multimin_fminimizer_set(min, &func, a, ss);
            int status;
            size_t iter=0;
            double oldsize=0;

            do
            {
                iter++;
                if(iter % 10==0)
                    oldsize=gsl_multimin_fminimizer_size(min);
                status = gsl_multimin_fminimizer_iterate(min);

                if (status)
                    break;

                double size = gsl_multimin_fminimizer_size(min);
                //status = gsl_multimin_test_size (size, 1e-10);
                if(size!=oldsize || iter%10!=9 || size>10)
                    status=GSL_CONTINUE;
                else
                    status=GSL_SUCCESS;

                if (status == GSL_SUCCESS)
                {
                    std::cerr
                            <<"converged to minimum in \""<<f.fileName().toLocal8Bit().data()<<"\" at\n"
                            <<"Rm="<<gsl_vector_get (min->x, 0)*gsl_vector_get (units, 0)<<"\t"
                            <<"Lm=";
                    if(!useLm) std::cerr<<gsl_vector_get (min->x, 1)*gsl_vector_get (units, 1);
                    else std::cerr<<inLm;
                    std::cerr<<"\t"
                            <<"Cm="<<gsl_vector_get (min->x, 2)*gsl_vector_get (units, 2)<<"\t"
                            <<"C0=";
                    if(!useC0) std::cerr<<gsl_vector_get (min->x, 3)*gsl_vector_get (units, 3);
                    else std::cerr<<inC0;
                    std::cerr<<"\n"
                            <<"StDev="<<min->fval<<"\n"
                            <<"Noise="<<noise<<"\n";
                } else {
                    std::cerr<<iter<<"\t"
                            <<min->fval<<"\t"
                            <<size<<"\n";
                }
            }
            while (status == GSL_CONTINUE && iter < 10000);

            QVector<qreal> X_exp,Y_exp,X_f,Y_f;

            foreach(Point2D P, func_data)
            {
                X_exp.push_back(P.x);
                Y_exp.push_back(P.y);
            }

            double maxf=func_data[0].x;
            double maxI=If(min->x,&func_params,maxf);
            double minf=maxf;
            double minI=If(min->x,&func_params,maxf);

            for(double freq=func_data[0].x; freq<func_data.last().x; ++freq)
            {
                double I=If(min->x,&func_params, freq);
                X_f.push_back(freq);
                Y_f.push_back(I);
                if(I>maxI)
                {
                    maxI=I;
                    maxf=freq;
                }

                if(I<minI)
                {
                    minI=I;
                    minf=freq;
                }
            }

            gsl_vector_memcpy(a, min->x);

            gsl_vector *par;
            par = gsl_vector_alloc(PARAM_COUNT);
            gsl_vector_memcpy(par, min->x);
            gsl_vector_mul(par,units);
            if(useLm) gsl_vector_set(par,1,inLm);
            if(useC0) gsl_vector_set(par,3,inC0);
            if(useU) gsl_vector_set(par,4,inU);
            if(useR0) gsl_vector_set(par,5,inR0);

            gsl_multimin_fminimizer_free(min);
            gsl_vector_free(ss);

            QApplication app(argc,argv);
            Graph g(X_exp, Y_exp, X_f, Y_f, gsl_vector_get(par,0), gsl_vector_get(par,1),
                    gsl_vector_get(par,2), gsl_vector_get(par,4), gsl_vector_get(par,3),
                    gsl_vector_get(par,5), minf, minI, maxf, maxI);
            g.setWindowTitle(f.fileName());
            if(!consoleonly)
                gstatus=g.exec();
            else
                gstatus=QDialog::Accepted; //if it was not shown, we have to agree :)
            if(gstatus==QDialog::Rejected)
            {
                gsl_vector_set(par,0,g.Rm());
                gsl_vector_set(par,1,g.Lm());
                gsl_vector_set(par,2,g.Cm());
                gsl_vector_set(par,3,g.C0());
                gsl_vector_set(par,4,g.U());
                gsl_vector_set(par,5,g.R0());
                gsl_vector_memcpy(a,par);
                gsl_vector_div(a,units);
            } else {
#define table_append_num(v) table[ifile+1].append(QString::number(v,'f',10)+"\t")
                table[ifile+1].append("\t"+f.fileName()+"\t");
                table_append_num(maxf);
                table_append_num(maxI);
                table_append_num(minf);
                table_append_num(minI);
                table_append_num(min->fval);
                table_append_num(noise);
            }

            if(ifile==0)
            {
                useLm=true;
                inLm=gsl_vector_get(par,1);
                useC0=true;
                inC0=gsl_vector_get(par,3);
                //            useU=true;
                //            inU=gsl_vector_get(par,4);
                useR0=true;
                inR0=gsl_vector_get(par,5);
            }
            gsl_vector_free(par);
        }while(gstatus==QDialog::Rejected);
    }
    gsl_vector_free(a);
    gsl_vector_free(units);
    foreach(QString s, table)
        std::cout<<s.toLocal8Bit().data()<<"\n";
}

<|endoftext|>


<commit_before>/*
 * Copyright 2010-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
 * that can be found in the LICENSE file.
 */

#include "CompilerGenerated.h"
#include "Types.h"

namespace {

TypeInfo theAnyTypeInfoImpl = {};
TypeInfo theArrayTypeInfoImpl = {};
TypeInfo theBooleanArrayTypeInfoImpl = {};
TypeInfo theByteArrayTypeInfoImpl = {};
TypeInfo theCharArrayTypeInfoImpl = {};
TypeInfo theDoubleArrayTypeInfoImpl = {};
TypeInfo theFloatArrayTypeInfoImpl = {};
TypeInfo theForeignObjCObjectTypeInfoImpl = {};
TypeInfo theFreezableAtomicReferenceTypeInfoImpl = {};
TypeInfo theIntArrayTypeInfoImpl = {};
TypeInfo theLongArrayTypeInfoImpl = {};
TypeInfo theNativePtrArrayTypeInfoImpl = {};
TypeInfo theObjCObjectWrapperTypeInfoImpl = {};
TypeInfo theOpaqueFunctionTypeInfoImpl = {};
TypeInfo theShortArrayTypeInfoImpl = {};
TypeInfo theStringTypeInfoImpl = {};
TypeInfo theThrowableTypeInfoImpl = {};
TypeInfo theUnitTypeInfoImpl = {};
TypeInfo theWorkerBoundReferenceTypeInfoImpl = {};

ArrayHeader theEmptyStringImpl = { &theStringTypeInfoImpl, /* element count */ 0 };

template <class T>
struct KBox {
    ObjHeader header;
    const T value;
};

} // namespace

extern "C" {

extern const int KonanNeedDebugInfo = 0;

extern const TypeInfo* theAnyTypeInfo = &theAnyTypeInfoImpl;
extern const TypeInfo* theArrayTypeInfo = &theArrayTypeInfoImpl;
extern const TypeInfo* theBooleanArrayTypeInfo = &theBooleanArrayTypeInfoImpl;
extern const TypeInfo* theByteArrayTypeInfo = &theByteArrayTypeInfoImpl;
extern const TypeInfo* theCharArrayTypeInfo = &theCharArrayTypeInfoImpl;
extern const TypeInfo* theDoubleArrayTypeInfo = &theDoubleArrayTypeInfoImpl;
extern const TypeInfo* theFloatArrayTypeInfo = &theFloatArrayTypeInfoImpl;
extern const TypeInfo* theForeignObjCObjectTypeInfo = &theForeignObjCObjectTypeInfoImpl;
extern const TypeInfo* theFreezableAtomicReferenceTypeInfo = &theFreezableAtomicReferenceTypeInfoImpl;
extern const TypeInfo* theIntArrayTypeInfo = &theIntArrayTypeInfoImpl;
extern const TypeInfo* theLongArrayTypeInfo = &theLongArrayTypeInfoImpl;
extern const TypeInfo* theNativePtrArrayTypeInfo = &theNativePtrArrayTypeInfoImpl;
extern const TypeInfo* theObjCObjectWrapperTypeInfo = &theObjCObjectWrapperTypeInfoImpl;
extern const TypeInfo* theOpaqueFunctionTypeInfo = &theOpaqueFunctionTypeInfoImpl;
extern const TypeInfo* theShortArrayTypeInfo = &theShortArrayTypeInfoImpl;
extern const TypeInfo* theStringTypeInfo = &theStringTypeInfoImpl;
extern const TypeInfo* theThrowableTypeInfo = &theThrowableTypeInfoImpl;
extern const TypeInfo* theUnitTypeInfo = &theUnitTypeInfoImpl;
extern const TypeInfo* theWorkerBoundReferenceTypeInfo = &theWorkerBoundReferenceTypeInfoImpl;

extern const ArrayHeader theEmptyArray = { &theArrayTypeInfoImpl, /* element count */0 };

OBJ_GETTER0(TheEmptyString) {
    RETURN_OBJ(theEmptyStringImpl.obj());
}

RUNTIME_NORETURN OBJ_GETTER(makeWeakReferenceCounter, void*) {
    THROW_NOT_IMPLEMENTED
}

RUNTIME_NORETURN OBJ_GETTER(makePermanentWeakReferenceImpl, void*) {
    THROW_NOT_IMPLEMENTED
}

RUNTIME_NORETURN OBJ_GETTER(makeObjCWeakReferenceImpl, void*) {
    THROW_NOT_IMPLEMENTED
}

void checkRangeIndexes(KInt from, KInt to, KInt size) {
    if (from < 0 || to > size) {
        throw std::out_of_range("Index out of bounds: from=" + std::to_string(from)
                + ", to=" + std::to_string(to)
                + ", size=" + std::to_string(size));
    }
    if (from > to) {
        throw std::invalid_argument("Illegal argument: from > to, from=" + std::to_string(from) + ", to=" + std::to_string(to));
    }
}

RUNTIME_NORETURN OBJ_GETTER(WorkerLaunchpad, KRef) {
    THROW_NOT_IMPLEMENTED
}

void RUNTIME_NORETURN ThrowWorkerInvalidState() {
    THROW_NOT_IMPLEMENTED
}

void RUNTIME_NORETURN ThrowNullPointerException() {
    THROW_NOT_IMPLEMENTED
}

void RUNTIME_NORETURN ThrowArrayIndexOutOfBoundsException() {
    THROW_NOT_IMPLEMENTED
}

void RUNTIME_NORETURN ThrowClassCastException(const ObjHeader* instance, const TypeInfo* type_info) {
    THROW_NOT_IMPLEMENTED
}

void RUNTIME_NORETURN ThrowArithmeticException() {
    THROW_NOT_IMPLEMENTED
}

void RUNTIME_NORETURN ThrowNumberFormatException() {
    THROW_NOT_IMPLEMENTED
}

void RUNTIME_NORETURN ThrowOutOfMemoryError() {
    THROW_NOT_IMPLEMENTED
}

void RUNTIME_NORETURN ThrowNotImplementedError() {
    THROW_NOT_IMPLEMENTED
}

void RUNTIME_NORETURN ThrowCharacterCodingException() {
    THROW_NOT_IMPLEMENTED
}

void RUNTIME_NORETURN ThrowIllegalArgumentException() {
    THROW_NOT_IMPLEMENTED
}

void RUNTIME_NORETURN ThrowIllegalStateException() {
    THROW_NOT_IMPLEMENTED
}

void RUNTIME_NORETURN ThrowInvalidMutabilityException(KConstRef where) {
    THROW_NOT_IMPLEMENTED
}

void RUNTIME_NORETURN ThrowIncorrectDereferenceException() {
    THROW_NOT_IMPLEMENTED
}

void RUNTIME_NORETURN ThrowIllegalObjectSharingException(KConstNativePtr typeInfo, KConstNativePtr address) {
    THROW_NOT_IMPLEMENTED
}

void RUNTIME_NORETURN ThrowFreezingException(KRef toFreeze, KRef blocker) {
    THROW_NOT_IMPLEMENTED
}

void ReportUnhandledException(KRef throwable) {
    konan::consolePrintf("Uncaught Kotlin exception.");
}

RUNTIME_NORETURN OBJ_GETTER(DescribeObjectForDebugging, KConstNativePtr typeInfo, KConstNativePtr address) {
    THROW_NOT_IMPLEMENTED
}

void ExceptionReporterLaunchpad(KRef reporter, KRef throwable) {
    THROW_NOT_IMPLEMENTED
}

void Kotlin_WorkerBoundReference_freezeHook(KRef thiz) {
    THROW_NOT_IMPLEMENTED
}

extern const KBoolean BOOLEAN_RANGE_FROM = false;
extern const KBoolean BOOLEAN_RANGE_TO = true;
extern KBox<KBoolean> BOOLEAN_CACHE[] = {
        {{}, false},
        {{}, true},
};

OBJ_GETTER(Kotlin_boxBoolean, KBoolean value) {
    if (value) {
        RETURN_OBJ(&BOOLEAN_CACHE[1].header);
    } else {
        RETURN_OBJ(&BOOLEAN_CACHE[0].header);
    }
}

extern const KByte BYTE_RANGE_FROM = -1;
extern const KByte BYTE_RANGE_TO = 1;
extern KBox<KByte> BYTE_CACHE[] = {
        {{}, -1},
        {{}, 0},
        {{}, 1},
};

extern const KChar CHAR_RANGE_FROM = 0;
extern const KChar CHAR_RANGE_TO = 2;
extern KBox<KChar> CHAR_CACHE[] = {
        {{}, 0},
        {{}, 1},
        {{}, 2},
};

extern const KShort SHORT_RANGE_FROM = -1;
extern const KShort SHORT_RANGE_TO = 1;
extern KBox<KShort> SHORT_CACHE[] = {
        {{}, -1},
        {{}, 0},
        {{}, 1},
};

extern const KInt INT_RANGE_FROM = -1;
extern const KInt INT_RANGE_TO = 1;
extern KBox<KInt> INT_CACHE[] = {
        {{}, -1},
        {{}, 0},
        {{}, 1},
};

extern const KLong LONG_RANGE_FROM = -1;
extern const KLong LONG_RANGE_TO = 1;
extern KBox<KLong> LONG_CACHE[] = {
        {{}, -1},
        {{}, 0},
        {{}, 1},
};

RUNTIME_NORETURN OBJ_GETTER(Kotlin_Throwable_getMessage, KRef throwable) {
    THROW_NOT_IMPLEMENTED
}

} // extern "C"
<commit_msg>[Runtime testing] Enable runtime assertions in runtime tests<commit_after>/*
 * Copyright 2010-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
 * that can be found in the LICENSE file.
 */

#include "CompilerGenerated.h"
#include "Types.h"

namespace {

TypeInfo theAnyTypeInfoImpl = {};
TypeInfo theArrayTypeInfoImpl = {};
TypeInfo theBooleanArrayTypeInfoImpl = {};
TypeInfo theByteArrayTypeInfoImpl = {};
TypeInfo theCharArrayTypeInfoImpl = {};
TypeInfo theDoubleArrayTypeInfoImpl = {};
TypeInfo theFloatArrayTypeInfoImpl = {};
TypeInfo theForeignObjCObjectTypeInfoImpl = {};
TypeInfo theFreezableAtomicReferenceTypeInfoImpl = {};
TypeInfo theIntArrayTypeInfoImpl = {};
TypeInfo theLongArrayTypeInfoImpl = {};
TypeInfo theNativePtrArrayTypeInfoImpl = {};
TypeInfo theObjCObjectWrapperTypeInfoImpl = {};
TypeInfo theOpaqueFunctionTypeInfoImpl = {};
TypeInfo theShortArrayTypeInfoImpl = {};
TypeInfo theStringTypeInfoImpl = {};
TypeInfo theThrowableTypeInfoImpl = {};
TypeInfo theUnitTypeInfoImpl = {};
TypeInfo theWorkerBoundReferenceTypeInfoImpl = {};

ArrayHeader theEmptyStringImpl = { &theStringTypeInfoImpl, /* element count */ 0 };

template <class T>
struct KBox {
    ObjHeader header;
    const T value;
};

} // namespace

extern "C" {

// Set to 1 to enable runtime assertions.
extern const int KonanNeedDebugInfo = 1;

extern const TypeInfo* theAnyTypeInfo = &theAnyTypeInfoImpl;
extern const TypeInfo* theArrayTypeInfo = &theArrayTypeInfoImpl;
extern const TypeInfo* theBooleanArrayTypeInfo = &theBooleanArrayTypeInfoImpl;
extern const TypeInfo* theByteArrayTypeInfo = &theByteArrayTypeInfoImpl;
extern const TypeInfo* theCharArrayTypeInfo = &theCharArrayTypeInfoImpl;
extern const TypeInfo* theDoubleArrayTypeInfo = &theDoubleArrayTypeInfoImpl;
extern const TypeInfo* theFloatArrayTypeInfo = &theFloatArrayTypeInfoImpl;
extern const TypeInfo* theForeignObjCObjectTypeInfo = &theForeignObjCObjectTypeInfoImpl;
extern const TypeInfo* theFreezableAtomicReferenceTypeInfo = &theFreezableAtomicReferenceTypeInfoImpl;
extern const TypeInfo* theIntArrayTypeInfo = &theIntArrayTypeInfoImpl;
extern const TypeInfo* theLongArrayTypeInfo = &theLongArrayTypeInfoImpl;
extern const TypeInfo* theNativePtrArrayTypeInfo = &theNativePtrArrayTypeInfoImpl;
extern const TypeInfo* theObjCObjectWrapperTypeInfo = &theObjCObjectWrapperTypeInfoImpl;
extern const TypeInfo* theOpaqueFunctionTypeInfo = &theOpaqueFunctionTypeInfoImpl;
extern const TypeInfo* theShortArrayTypeInfo = &theShortArrayTypeInfoImpl;
extern const TypeInfo* theStringTypeInfo = &theStringTypeInfoImpl;
extern const TypeInfo* theThrowableTypeInfo = &theThrowableTypeInfoImpl;
extern const TypeInfo* theUnitTypeInfo = &theUnitTypeInfoImpl;
extern const TypeInfo* theWorkerBoundReferenceTypeInfo = &theWorkerBoundReferenceTypeInfoImpl;

extern const ArrayHeader theEmptyArray = { &theArrayTypeInfoImpl, /* element count */0 };

OBJ_GETTER0(TheEmptyString) {
    RETURN_OBJ(theEmptyStringImpl.obj());
}

RUNTIME_NORETURN OBJ_GETTER(makeWeakReferenceCounter, void*) {
    THROW_NOT_IMPLEMENTED
}

RUNTIME_NORETURN OBJ_GETTER(makePermanentWeakReferenceImpl, void*) {
    THROW_NOT_IMPLEMENTED
}

RUNTIME_NORETURN OBJ_GETTER(makeObjCWeakReferenceImpl, void*) {
    THROW_NOT_IMPLEMENTED
}

void checkRangeIndexes(KInt from, KInt to, KInt size) {
    if (from < 0 || to > size) {
        throw std::out_of_range("Index out of bounds: from=" + std::to_string(from)
                + ", to=" + std::to_string(to)
                + ", size=" + std::to_string(size));
    }
    if (from > to) {
        throw std::invalid_argument("Illegal argument: from > to, from=" + std::to_string(from) + ", to=" + std::to_string(to));
    }
}

RUNTIME_NORETURN OBJ_GETTER(WorkerLaunchpad, KRef) {
    THROW_NOT_IMPLEMENTED
}

void RUNTIME_NORETURN ThrowWorkerInvalidState() {
    THROW_NOT_IMPLEMENTED
}

void RUNTIME_NORETURN ThrowNullPointerException() {
    THROW_NOT_IMPLEMENTED
}

void RUNTIME_NORETURN ThrowArrayIndexOutOfBoundsException() {
    THROW_NOT_IMPLEMENTED
}

void RUNTIME_NORETURN ThrowClassCastException(const ObjHeader* instance, const TypeInfo* type_info) {
    THROW_NOT_IMPLEMENTED
}

void RUNTIME_NORETURN ThrowArithmeticException() {
    THROW_NOT_IMPLEMENTED
}

void RUNTIME_NORETURN ThrowNumberFormatException() {
    THROW_NOT_IMPLEMENTED
}

void RUNTIME_NORETURN ThrowOutOfMemoryError() {
    THROW_NOT_IMPLEMENTED
}

void RUNTIME_NORETURN ThrowNotImplementedError() {
    THROW_NOT_IMPLEMENTED
}

void RUNTIME_NORETURN ThrowCharacterCodingException() {
    THROW_NOT_IMPLEMENTED
}

void RUNTIME_NORETURN ThrowIllegalArgumentException() {
    THROW_NOT_IMPLEMENTED
}

void RUNTIME_NORETURN ThrowIllegalStateException() {
    THROW_NOT_IMPLEMENTED
}

void RUNTIME_NORETURN ThrowInvalidMutabilityException(KConstRef where) {
    THROW_NOT_IMPLEMENTED
}

void RUNTIME_NORETURN ThrowIncorrectDereferenceException() {
    THROW_NOT_IMPLEMENTED
}

void RUNTIME_NORETURN ThrowIllegalObjectSharingException(KConstNativePtr typeInfo, KConstNativePtr address) {
    THROW_NOT_IMPLEMENTED
}

void RUNTIME_NORETURN ThrowFreezingException(KRef toFreeze, KRef blocker) {
    THROW_NOT_IMPLEMENTED
}

void ReportUnhandledException(KRef throwable) {
    konan::consolePrintf("Uncaught Kotlin exception.");
}

RUNTIME_NORETURN OBJ_GETTER(DescribeObjectForDebugging, KConstNativePtr typeInfo, KConstNativePtr address) {
    THROW_NOT_IMPLEMENTED
}

void ExceptionReporterLaunchpad(KRef reporter, KRef throwable) {
    THROW_NOT_IMPLEMENTED
}

void Kotlin_WorkerBoundReference_freezeHook(KRef thiz) {
    THROW_NOT_IMPLEMENTED
}

extern const KBoolean BOOLEAN_RANGE_FROM = false;
extern const KBoolean BOOLEAN_RANGE_TO = true;
extern KBox<KBoolean> BOOLEAN_CACHE[] = {
        {{}, false},
        {{}, true},
};

OBJ_GETTER(Kotlin_boxBoolean, KBoolean value) {
    if (value) {
        RETURN_OBJ(&BOOLEAN_CACHE[1].header);
    } else {
        RETURN_OBJ(&BOOLEAN_CACHE[0].header);
    }
}

extern const KByte BYTE_RANGE_FROM = -1;
extern const KByte BYTE_RANGE_TO = 1;
extern KBox<KByte> BYTE_CACHE[] = {
        {{}, -1},
        {{}, 0},
        {{}, 1},
};

extern const KChar CHAR_RANGE_FROM = 0;
extern const KChar CHAR_RANGE_TO = 2;
extern KBox<KChar> CHAR_CACHE[] = {
        {{}, 0},
        {{}, 1},
        {{}, 2},
};

extern const KShort SHORT_RANGE_FROM = -1;
extern const KShort SHORT_RANGE_TO = 1;
extern KBox<KShort> SHORT_CACHE[] = {
        {{}, -1},
        {{}, 0},
        {{}, 1},
};

extern const KInt INT_RANGE_FROM = -1;
extern const KInt INT_RANGE_TO = 1;
extern KBox<KInt> INT_CACHE[] = {
        {{}, -1},
        {{}, 0},
        {{}, 1},
};

extern const KLong LONG_RANGE_FROM = -1;
extern const KLong LONG_RANGE_TO = 1;
extern KBox<KLong> LONG_CACHE[] = {
        {{}, -1},
        {{}, 0},
        {{}, 1},
};

RUNTIME_NORETURN OBJ_GETTER(Kotlin_Throwable_getMessage, KRef throwable) {
    THROW_NOT_IMPLEMENTED
}

} // extern "C"
<|endoftext|>


<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the  "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#if !defined(MEMORYMANAGERIMPL_HEADER_GUARD_1357924680)
#define MEMORYMANAGERIMPL_HEADER_GUARD_1357924680

// Base include file.  Must be first.
#include <new>

#include <xalanc/Include/PlatformDefinitions.hpp>

#include <xercesc/framework/MemoryManager.hpp>

#if defined(XALAN_WINDOWS)
#include <windows.h>
#include <stdexcept>
#include <stdlib.h>

class XalanMemoryManagerImpl : public XALAN_CPP_NAMESPACE_QUALIFIER MemoryManagerType
{
public:

    XalanMemoryManagerImpl( DWORD defSize = 0 ) :
	    m_heapHandle(NULL)
	{
		m_heapHandle = HeapCreate(	HEAP_NO_SERIALIZE,
		                            0, // dwInitialSize
		                            defSize);

        if( m_heapHandle == NULL )
		{
            XALAN_USING_STD(runtime_error)

            char buffer[20];
            buffer[0] = 0;

            _ultoa( GetLastError(), buffer, 10);

            throw runtime_error(buffer);
		}
	}

	virtual void*
	allocate( size_t size )
    {
        LPVOID ptr = HeapAlloc(
                        m_heapHandle,   //HANDLE hHeap
                        0,              //DWORD dwFlags
                        size            //SIZE_T dwBytes
                        );

        if( ptr == 0)
        {
            XALAN_USING_STD(bad_alloc)

            throw bad_alloc();
        }

        return ptr;
    }

	virtual void
	deallocate(  void* 	pDataPointer  )
	{
        if ( 0 == HeapFree(
                        m_heapHandle,   //HANDLE hHeap,
                        0,              //DWORD dwFlags,
                        pDataPointer ) )//*LPVOID lpMem
        {
            XALAN_USING_STD(bad_alloc)

            throw bad_alloc();
        }
    }

    virtual MemoryManager*
    getExceptionMemoryManager()
    {
        return this;
    }

    virtual
	~XalanMemoryManagerImpl()
	{
       if( 0 == HeapDestroy(m_heapHandle) )
       {
            XALAN_USING_STD(runtime_error)

            char buffer[20];
            buffer[0] = 0;

            _ultoa( GetLastError(), buffer, 10);

            throw runtime_error(buffer);
       }
	}

private:

    // Not implemented
	XalanMemoryManagerImpl&
	operator=(const XalanMemoryManagerImpl&);

	XalanMemoryManagerImpl(const XalanMemoryManagerImpl&);

	//Data
	HANDLE  m_heapHandle;
};

#else

class XalanMemoryManagerImpl : public XALAN_CPP_NAMESPACE_QUALIFIER MemoryManagerType
{
public:

    virtual
	~XalanMemoryManagerImpl()
	{
	}

	virtual void*
	allocate( size_t 	size )
	{
	    void* memptr = ::operator new(size);

	    if (memptr != NULL)
	    {
	        return memptr;
	    }

		XALAN_USING_STD(bad_alloc)

		throw bad_alloc();
	}
	virtual void
	deallocate(  void* 		pDataPointer  )
	{
		operator delete(pDataPointer);
	}

    MemoryManager*
    getExceptionMemoryManager()
    {
        return this;
    }
};

#endif

#endif  // MEMORYMANAGERIMPL_HEADER_GUARD_1357924680
<commit_msg>More cleanup.<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the  "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#if !defined(MEMORYMANAGERIMPL_HEADER_GUARD_1357924680)
#define MEMORYMANAGERIMPL_HEADER_GUARD_1357924680

// Base include file.  Must be first.
#include <new>

#include <xalanc/Include/PlatformDefinitions.hpp>

#include <xercesc/framework/MemoryManager.hpp>

#if defined(XALAN_WINDOWS)
#include <windows.h>
#include <stdexcept>
#include <stdlib.h>

class XalanMemoryManagerImpl : public XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager
{
public:

    XalanMemoryManagerImpl( DWORD defSize = 0 ) :
	    m_heapHandle(NULL)
	{
		m_heapHandle = HeapCreate(	HEAP_NO_SERIALIZE,
		                            0, // dwInitialSize
		                            defSize);

        if( m_heapHandle == NULL )
		{
            XALAN_USING_STD(runtime_error)

            char buffer[20];
            buffer[0] = 0;

            _ultoa( GetLastError(), buffer, 10);

            throw runtime_error(buffer);
		}
	}

	virtual void*
	allocate( size_t size )
    {
        LPVOID ptr = HeapAlloc(
                        m_heapHandle,   //HANDLE hHeap
                        0,              //DWORD dwFlags
                        size            //SIZE_T dwBytes
                        );

        if( ptr == 0)
        {
            XALAN_USING_STD(bad_alloc)

            throw bad_alloc();
        }

        return ptr;
    }

	virtual void
	deallocate(  void* 	pDataPointer  )
	{
        if ( 0 == HeapFree(
                        m_heapHandle,   //HANDLE hHeap,
                        0,              //DWORD dwFlags,
                        pDataPointer ) )//*LPVOID lpMem
        {
            XALAN_USING_STD(bad_alloc)

            throw bad_alloc();
        }
    }

    virtual MemoryManager*
    getExceptionMemoryManager()
    {
        return this;
    }

    virtual
	~XalanMemoryManagerImpl()
	{
       if( 0 == HeapDestroy(m_heapHandle) )
       {
            XALAN_USING_STD(runtime_error)

            char buffer[20];
            buffer[0] = 0;

            _ultoa( GetLastError(), buffer, 10);

            throw runtime_error(buffer);
       }
	}

private:

    // Not implemented
	XalanMemoryManagerImpl&
	operator=(const XalanMemoryManagerImpl&);

	XalanMemoryManagerImpl(const XalanMemoryManagerImpl&);

	//Data
	HANDLE  m_heapHandle;
};

#else

class XalanMemoryManagerImpl : public XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager
{
public:

    virtual
	~XalanMemoryManagerImpl()
	{
	}

	virtual void*
	allocate( size_t 	size )
	{
	    void* memptr = ::operator new(size);

	    if (memptr != NULL)
	    {
	        return memptr;
	    }

		XALAN_USING_STD(bad_alloc)

		throw bad_alloc();
	}
	virtual void
	deallocate(  void* 		pDataPointer  )
	{
		operator delete(pDataPointer);
	}

    virtual MemoryManager*
    getExceptionMemoryManager()
    {
        return this;
    }
};

#endif

#endif  // MEMORYMANAGERIMPL_HEADER_GUARD_1357924680
<|endoftext|>


<commit_before>#include "bufferAgent.h"
#include "helpers/storageHelperFactory.h"
#include "glog/logging.h"

using boost::shared_ptr;
using boost::thread;
using boost::bind;

using std::string;

namespace veil {
namespace helpers {

BufferAgent::BufferAgent(write_fun w, read_fun r)
  : m_agentActive(false),
    doWrite(w),
    doRead(r)
{
    agentStart();
}
BufferAgent::~BufferAgent()
{
    agentStop();
}

int BufferAgent::onOpen(std::string path, ffi_type ffi)
{
    {
        unique_lock guard(m_wrMutex);
        m_wrCacheMap.erase(ffi->fh);

        write_buffer_ptr lCache(new WriteCache());
        lCache->fileName = path;
        lCache->buffer = newFileCache(true);
        lCache->ffi = *ffi;

        m_wrCacheMap[ffi->fh] = lCache;
    }

    {
        unique_lock guard(m_rdMutex);
        read_cache_map_t::iterator it;
        if(( it = m_rdCacheMap.find(path) ) != m_rdCacheMap.end()) {
            it->second->openCount++;
        } else {
            read_buffer_ptr lCache(new ReadCache());
            lCache->fileName = path;
            lCache->openCount = 1;
            lCache->ffi = *ffi;
            lCache->buffer = newFileCache(false);
        }

        m_rdJobQueue.push_front(PrefetchJob(path, 0, 512));
    }

    return 0;
}

int BufferAgent::onWrite(std::string path, const std::string &buf, size_t size, off_t offset, ffi_type ffi)
{
    unique_lock guard(m_wrMutex);
        write_buffer_ptr wrapper = m_wrCacheMap[ffi->fh];
    guard.unlock();

    {
        if(wrapper->buffer->byteSize() > 1024 * 1024 * 64) {
            if(int fRet = onFlush(path, ffi)) {
                return fRet;
            }
        }

        if(wrapper->buffer->byteSize() > 1024 * 1024 * 64) {
            return doWrite(path, buf, size, offset, ffi);
        }

        wrapper->buffer->writeData(offset, buf);
    }

    unique_lock buffGuard(wrapper->mutex);
    guard.lock();
    if(!wrapper->opPending)
    {
        wrapper->opPending = true;
        m_wrJobQueue.push_back(ffi->fh);
        m_wrCond.notify_one();
    }

    return size;
}

int BufferAgent::onRead(std::string path, std::string &buf, size_t size, off_t offset, ffi_type ffi)
{
    unique_lock guard(m_rdMutex);
        read_buffer_ptr wrapper = m_rdCacheMap[path];
    guard.unlock();

    wrapper->buffer->readData(offset, size, buf);

    if(buf.size() < size) {
        string buf2;
        int ret = doRead(path, buf2, buf.size() - size, offset + buf.size(), &wrapper->ffi);
        if(ret < 0)
            return ret;

        buf += buf2;

        {
            unique_lock buffGuard(wrapper->mutex);
            wrapper->blockSize = std::min((size_t) 1024 * 1024, (size_t) std::max(size, 2*wrapper->blockSize));
        }

        guard.lock();
            m_rdJobQueue.push_back(PrefetchJob(wrapper->fileName, buf.size(), wrapper->blockSize));
        guard.unlock();
    } else {
        string tmp;
        size_t prefSize = std::max(2*size, wrapper->blockSize);
        wrapper->buffer->readData(offset + size, prefSize, tmp);

        if(tmp.size() != prefSize) {
            guard.lock();
                m_rdJobQueue.push_back(PrefetchJob(wrapper->fileName, offset + size + tmp.size(), wrapper->blockSize));
                m_rdJobQueue.push_back(PrefetchJob(wrapper->fileName, offset + size + tmp.size() + wrapper->blockSize, wrapper->blockSize));
            guard.unlock();
        }
    }

    return buf.size();
}

int BufferAgent::onFlush(std::string path, ffi_type ffi)
{
    unique_lock guard(m_wrMutex);
        write_buffer_ptr wrapper = m_wrCacheMap[ffi->fh];
    guard.unlock();

    unique_lock sendGuard(wrapper->sendMutex);
    unique_lock buff_guard(wrapper->mutex);

    while(wrapper->buffer->blockCount() > 0)
    {
        block_ptr block = wrapper->buffer->removeOldestBlock();
        uint64_t start = utils::mtime<uint64_t>();
        int res = doWrite(wrapper->fileName, block->data, block->data.size(), block->offset, &wrapper->ffi);
        uint64_t end = utils::mtime<uint64_t>();

        LOG(INFO) << "Roundtrip: " << (end - start) << " for " << block->data.size() << " bytes";

        if(res < 0)
        {
            while(wrapper->buffer->blockCount() > 0)
            {
                (void) wrapper->buffer->removeOldestBlock();
            }
            return res;
        }
    }

    guard.lock();
    m_wrJobQueue.remove(ffi->fh);

    return 0;
}

int BufferAgent::onRelease(std::string path, ffi_type ffi)
{
    {
        unique_lock guard(m_wrMutex);
        m_wrCacheMap.erase(ffi->fh);
        m_wrJobQueue.remove(ffi->fh);
    }

    {
        unique_lock guard(m_rdMutex);

        read_cache_map_t::iterator it;
        if(( it = m_rdCacheMap.find(path) ) != m_rdCacheMap.end()) {
            it->second->openCount--;
            if(it->second->openCount <= 0) {
                m_rdCacheMap.erase(it);
            }
        }
    }

    return 0;
}

void BufferAgent::agentStart(int worker_count)
{
    m_workers.clear();
    m_agentActive = true;

    while(worker_count--)
    {
        m_workers.push_back(shared_ptr<thread>(new thread(bind(&BufferAgent::writerLoop, this))));
        m_workers.push_back(shared_ptr<thread>(new thread(bind(&BufferAgent::readerLoop, this))));
    }
}

void BufferAgent::agentStop()
{
    m_agentActive = false;
    m_wrCond.notify_all();
    m_rdCond.notify_all();

    while(m_workers.size() > 0)
    {
        m_workers.back()->join();
        m_workers.pop_back();
    }
}

void BufferAgent::readerLoop()
{
    unique_lock guard(m_rdMutex);
    while(m_agentActive)
    {
        while(m_rdJobQueue.empty() && m_agentActive)
            m_rdCond.wait(guard);

        if(!m_agentActive)
            return;

        PrefetchJob job = m_rdJobQueue.front();
        read_buffer_ptr wrapper = m_rdCacheMap[job.fileName];
        m_rdJobQueue.pop_front();
        m_rdCond.notify_one();

        if(!wrapper)
            continue;

        guard.unlock();

        {
            string buff;
            int ret = doRead(wrapper->fileName, buff, job.size, job.offset, &wrapper->ffi);
            if(ret > 0 && buff.size() >= ret) {
                wrapper->buffer->writeData(job.offset, buff);
            }
        }

    }
}

void BufferAgent::writerLoop()
{
    unique_lock guard(m_wrMutex);
    while(m_agentActive)
    {
        while(m_wrJobQueue.empty() && m_agentActive)
            m_wrCond.wait(guard);

        if(!m_agentActive)
            return;

        fd_type file = m_wrJobQueue.front();
        write_buffer_ptr wrapper = m_wrCacheMap[file];
        m_wrJobQueue.pop_front();
        m_wrCond.notify_one();

        if(!wrapper)
            continue;

        guard.unlock();

        {
            unique_lock sendGuard(wrapper->sendMutex);

            block_ptr block;
            {
                unique_lock buff_guard(wrapper->mutex);
                block = wrapper->buffer->removeOldestBlock();
            }

            if(block)
            {
                uint64_t start = utils::mtime<uint64_t>();
                int res = doWrite(wrapper->fileName, block->data, block->data.size(), block->offset, &wrapper->ffi);
                uint64_t end = utils::mtime<uint64_t>();

                LOG(INFO) << "Roundtrip: " << (end - start) << " for " << block->data.size() << " bytes";

                wrapper->cond.notify_all();
            }

            {
                unique_lock buff_guard(wrapper->mutex);
                guard.lock();
                if(wrapper->buffer->blockCount() > 0)
                {
                    m_wrJobQueue.push_back(file);
                }
                else
                {
                    wrapper->opPending = false;
                }
            }
        }

        wrapper->cond.notify_all();
    }
}

boost::shared_ptr<FileCache> BufferAgent::newFileCache(bool isBuffer)
{
    return boost::shared_ptr<FileCache>(new FileCache(10 * 1024 * 1024, isBuffer));
}

} // namespace helpers
} // namespace veil

<commit_msg>VFS-292: improve read speed<commit_after>#include "bufferAgent.h"
#include "helpers/storageHelperFactory.h"
#include "glog/logging.h"

using boost::shared_ptr;
using boost::thread;
using boost::bind;

using std::string;

namespace veil {
namespace helpers {

BufferAgent::BufferAgent(write_fun w, read_fun r)
  : m_agentActive(false),
    doWrite(w),
    doRead(r)
{
    agentStart();
}
BufferAgent::~BufferAgent()
{
    agentStop();
}

int BufferAgent::onOpen(std::string path, ffi_type ffi)
{
    {
        unique_lock guard(m_wrMutex);
        m_wrCacheMap.erase(ffi->fh);

        write_buffer_ptr lCache(new WriteCache());
        lCache->fileName = path;
        lCache->buffer = newFileCache(true);
        lCache->ffi = *ffi;

        m_wrCacheMap[ffi->fh] = lCache;
    }

    {
        unique_lock guard(m_rdMutex);
        read_cache_map_t::iterator it;
        if(( it = m_rdCacheMap.find(path) ) != m_rdCacheMap.end()) {
            it->second->openCount++;
        } else {
            read_buffer_ptr lCache(new ReadCache());
            lCache->fileName = path;
            lCache->openCount = 1;
            lCache->ffi = *ffi;
            lCache->buffer = newFileCache(false);

            m_rdCacheMap[path] = lCache;
        }

        m_rdJobQueue.push_front(PrefetchJob(path, 0, 512));
    }

    return 0;
}

int BufferAgent::onWrite(std::string path, const std::string &buf, size_t size, off_t offset, ffi_type ffi)
{
    unique_lock guard(m_wrMutex);
        write_buffer_ptr wrapper = m_wrCacheMap[ffi->fh];
    guard.unlock();

    {
        if(wrapper->buffer->byteSize() > 1024 * 1024 * 64) {
            if(int fRet = onFlush(path, ffi)) {
                return fRet;
            }
        }

        if(wrapper->buffer->byteSize() > 1024 * 1024 * 64) {
            return doWrite(path, buf, size, offset, ffi);
        }

        wrapper->buffer->writeData(offset, buf);
    }

    unique_lock buffGuard(wrapper->mutex);
    guard.lock();
    if(!wrapper->opPending)
    {
        wrapper->opPending = true;
        m_wrJobQueue.push_back(ffi->fh);
        m_wrCond.notify_one();
    }

    return size;
}

int BufferAgent::onRead(std::string path, std::string &buf, size_t size, off_t offset, ffi_type ffi)
{
    unique_lock guard(m_rdMutex);
        read_buffer_ptr wrapper = m_rdCacheMap[path];
    guard.unlock();

    wrapper->buffer->readData(offset, size, buf);

    if(buf.size() < size) {
        string buf2;
        int ret = doRead(path, buf2, buf.size() - size, offset + buf.size(), &wrapper->ffi);
        if(ret < 0)
            return ret;

        buf += buf2;

        {
            unique_lock buffGuard(wrapper->mutex);
            wrapper->blockSize = std::min((size_t) 1024 * 1024, (size_t) std::max(size, 2*wrapper->blockSize));
        }

        guard.lock();
            m_rdJobQueue.push_back(PrefetchJob(wrapper->fileName, buf.size(), wrapper->blockSize));
        guard.unlock();
    } else {
        string tmp;
        size_t prefSize = std::max(2*size, wrapper->blockSize);
        wrapper->buffer->readData(offset + size, prefSize, tmp);

        if(tmp.size() != prefSize) {
            guard.lock();
                m_rdJobQueue.push_back(PrefetchJob(wrapper->fileName, offset + size + tmp.size(), wrapper->blockSize));
                m_rdJobQueue.push_back(PrefetchJob(wrapper->fileName, offset + size + tmp.size() + wrapper->blockSize, wrapper->blockSize));
            guard.unlock();
        }
    }

    return buf.size();
}

int BufferAgent::onFlush(std::string path, ffi_type ffi)
{
    unique_lock guard(m_wrMutex);
        write_buffer_ptr wrapper = m_wrCacheMap[ffi->fh];
    guard.unlock();

    unique_lock sendGuard(wrapper->sendMutex);
    unique_lock buff_guard(wrapper->mutex);

    while(wrapper->buffer->blockCount() > 0)
    {
        block_ptr block = wrapper->buffer->removeOldestBlock();
        uint64_t start = utils::mtime<uint64_t>();
        int res = doWrite(wrapper->fileName, block->data, block->data.size(), block->offset, &wrapper->ffi);
        uint64_t end = utils::mtime<uint64_t>();

        LOG(INFO) << "Roundtrip: " << (end - start) << " for " << block->data.size() << " bytes";

        if(res < 0)
        {
            while(wrapper->buffer->blockCount() > 0)
            {
                (void) wrapper->buffer->removeOldestBlock();
            }
            return res;
        }
    }

    guard.lock();
    m_wrJobQueue.remove(ffi->fh);

    return 0;
}

int BufferAgent::onRelease(std::string path, ffi_type ffi)
{
    {
        unique_lock guard(m_wrMutex);
        m_wrCacheMap.erase(ffi->fh);
        m_wrJobQueue.remove(ffi->fh);
    }

    {
        unique_lock guard(m_rdMutex);

        read_cache_map_t::iterator it;
        if(( it = m_rdCacheMap.find(path) ) != m_rdCacheMap.end()) {
            it->second->openCount--;
            if(it->second->openCount <= 0) {
                m_rdCacheMap.erase(it);
            }
        }
    }

    return 0;
}

void BufferAgent::agentStart(int worker_count)
{
    m_workers.clear();
    m_agentActive = true;

    while(worker_count--)
    {
        m_workers.push_back(shared_ptr<thread>(new thread(bind(&BufferAgent::writerLoop, this))));
        m_workers.push_back(shared_ptr<thread>(new thread(bind(&BufferAgent::readerLoop, this))));
    }
}

void BufferAgent::agentStop()
{
    m_agentActive = false;
    m_wrCond.notify_all();
    m_rdCond.notify_all();

    while(m_workers.size() > 0)
    {
        m_workers.back()->join();
        m_workers.pop_back();
    }
}

void BufferAgent::readerLoop()
{
    unique_lock guard(m_rdMutex);
    while(m_agentActive)
    {
        while(m_rdJobQueue.empty() && m_agentActive)
            m_rdCond.wait(guard);

        if(!m_agentActive)
            return;

        PrefetchJob job = m_rdJobQueue.front();
        read_buffer_ptr wrapper = m_rdCacheMap[job.fileName];
        m_rdJobQueue.pop_front();
        m_rdCond.notify_one();

        if(!wrapper)
            continue;

        guard.unlock();

        {
            string buff;
            int ret = doRead(wrapper->fileName, buff, job.size, job.offset, &wrapper->ffi);
            if(ret > 0 && buff.size() >= ret) {
                wrapper->buffer->writeData(job.offset, buff);
            }
        }

    }
}

void BufferAgent::writerLoop()
{
    unique_lock guard(m_wrMutex);
    while(m_agentActive)
    {
        while(m_wrJobQueue.empty() && m_agentActive)
            m_wrCond.wait(guard);

        if(!m_agentActive)
            return;

        fd_type file = m_wrJobQueue.front();
        write_buffer_ptr wrapper = m_wrCacheMap[file];
        m_wrJobQueue.pop_front();
        m_wrCond.notify_one();

        if(!wrapper)
            continue;

        guard.unlock();

        {
            unique_lock sendGuard(wrapper->sendMutex);

            block_ptr block;
            {
                unique_lock buff_guard(wrapper->mutex);
                block = wrapper->buffer->removeOldestBlock();
            }

            if(block)
            {
                uint64_t start = utils::mtime<uint64_t>();
                int res = doWrite(wrapper->fileName, block->data, block->data.size(), block->offset, &wrapper->ffi);
                uint64_t end = utils::mtime<uint64_t>();

                LOG(INFO) << "Roundtrip: " << (end - start) << " for " << block->data.size() << " bytes";

                wrapper->cond.notify_all();
            }

            {
                unique_lock buff_guard(wrapper->mutex);
                guard.lock();
                if(wrapper->buffer->blockCount() > 0)
                {
                    m_wrJobQueue.push_back(file);
                }
                else
                {
                    wrapper->opPending = false;
                }
            }
        }

        wrapper->cond.notify_all();
    }
}

boost::shared_ptr<FileCache> BufferAgent::newFileCache(bool isBuffer)
{
    return boost::shared_ptr<FileCache>(new FileCache(10 * 1024 * 1024, isBuffer));
}

} // namespace helpers
} // namespace veil

<|endoftext|>


<commit_before>
// Statistics.cpp

#include "Globals.h"  // NOTE: MSVC stupidness requires this to be the same across all modules

#include "StatisticsManager.h"

bool StatisticsManager::SatisfiesPrerequisite(const CustomStatistic a_Stat) const
{
	switch (a_Stat)
	{
		case CustomStatistic::AchMineWood:           return IsStatisticPresent(CustomStatistic::AchOpenInventory);
		case CustomStatistic::AchBuildWorkBench:     return IsStatisticPresent(CustomStatistic::AchMineWood);
		case CustomStatistic::AchBuildHoe:           return IsStatisticPresent(CustomStatistic::AchBuildWorkBench);
		case CustomStatistic::AchBakeCake:           return IsStatisticPresent(CustomStatistic::AchBuildHoe);
		case CustomStatistic::AchMakeBread:          return IsStatisticPresent(CustomStatistic::AchBuildHoe);
		case CustomStatistic::AchBuildSword:         return IsStatisticPresent(CustomStatistic::AchBuildWorkBench);
		case CustomStatistic::AchKillCow:            return IsStatisticPresent(CustomStatistic::AchBuildSword);
		case CustomStatistic::AchFlyPig:             return IsStatisticPresent(CustomStatistic::AchKillCow);
		case CustomStatistic::AchBreedCow:           return IsStatisticPresent(CustomStatistic::AchKillCow);
		case CustomStatistic::AchKillEnemy:          return IsStatisticPresent(CustomStatistic::AchBuildSword);
		case CustomStatistic::AchSnipeSkeleton:      return IsStatisticPresent(CustomStatistic::AchKillEnemy);
		case CustomStatistic::AchBuildPickaxe:       return IsStatisticPresent(CustomStatistic::AchBuildWorkBench);
		case CustomStatistic::AchBuildBetterPickaxe: return IsStatisticPresent(CustomStatistic::AchBuildPickaxe);
		case CustomStatistic::AchBuildFurnace:       return IsStatisticPresent(CustomStatistic::AchBuildWorkBench);
		case CustomStatistic::AchCookFish:           return IsStatisticPresent(CustomStatistic::AchBuildFurnace);
		case CustomStatistic::AchAcquireIron:        return IsStatisticPresent(CustomStatistic::AchBuildFurnace);
		case CustomStatistic::AchOnARail:            return IsStatisticPresent(CustomStatistic::AchAcquireIron);
		case CustomStatistic::AchDiamonds:           return IsStatisticPresent(CustomStatistic::AchAcquireIron);
		case CustomStatistic::AchPortal:             return IsStatisticPresent(CustomStatistic::AchDiamonds);
		case CustomStatistic::AchGhast:              return IsStatisticPresent(CustomStatistic::AchPortal);
		case CustomStatistic::AchBlazeRod:           return IsStatisticPresent(CustomStatistic::AchPortal);
		case CustomStatistic::AchPotion:             return IsStatisticPresent(CustomStatistic::AchBlazeRod);
		case CustomStatistic::AchTheEnd:             return IsStatisticPresent(CustomStatistic::AchBlazeRod);
		case CustomStatistic::AchTheEnd2:            return IsStatisticPresent(CustomStatistic::AchTheEnd);
		case CustomStatistic::AchEnchantments:       return IsStatisticPresent(CustomStatistic::AchDiamonds);
		case CustomStatistic::AchOverkill:           return IsStatisticPresent(CustomStatistic::AchEnchantments);
		case CustomStatistic::AchBookcase:           return IsStatisticPresent(CustomStatistic::AchEnchantments);
		case CustomStatistic::AchExploreAllBiomes:   return IsStatisticPresent(CustomStatistic::AchTheEnd);
		case CustomStatistic::AchSpawnWither:        return IsStatisticPresent(CustomStatistic::AchTheEnd2);
		case CustomStatistic::AchKillWither:         return IsStatisticPresent(CustomStatistic::AchSpawnWither);
		case CustomStatistic::AchFullBeacon:         return IsStatisticPresent(CustomStatistic::AchKillWither);
		case CustomStatistic::AchDiamondsToYou:      return IsStatisticPresent(CustomStatistic::AchDiamonds);
		default: UNREACHABLE("Unsupported achievement type");
	}
}

bool StatisticsManager::IsStatisticPresent(const CustomStatistic a_Stat) const
{
	const auto Result = Custom.find(a_Stat);
	if (Result != Custom.end())
	{
		return Result->second > 0;
	}
	return false;
}
<commit_msg>fixed open inventory crash (#5233)<commit_after>
// Statistics.cpp

#include "Globals.h"  // NOTE: MSVC stupidness requires this to be the same across all modules

#include "StatisticsManager.h"

bool StatisticsManager::SatisfiesPrerequisite(const CustomStatistic a_Stat) const
{
	switch (a_Stat)
	{
		case CustomStatistic::AchOpenInventory:      return true;
		case CustomStatistic::AchMineWood:           return IsStatisticPresent(CustomStatistic::AchOpenInventory);
		case CustomStatistic::AchBuildWorkBench:     return IsStatisticPresent(CustomStatistic::AchMineWood);
		case CustomStatistic::AchBuildHoe:           return IsStatisticPresent(CustomStatistic::AchBuildWorkBench);
		case CustomStatistic::AchBakeCake:           return IsStatisticPresent(CustomStatistic::AchBuildHoe);
		case CustomStatistic::AchMakeBread:          return IsStatisticPresent(CustomStatistic::AchBuildHoe);
		case CustomStatistic::AchBuildSword:         return IsStatisticPresent(CustomStatistic::AchBuildWorkBench);
		case CustomStatistic::AchKillCow:            return IsStatisticPresent(CustomStatistic::AchBuildSword);
		case CustomStatistic::AchFlyPig:             return IsStatisticPresent(CustomStatistic::AchKillCow);
		case CustomStatistic::AchBreedCow:           return IsStatisticPresent(CustomStatistic::AchKillCow);
		case CustomStatistic::AchKillEnemy:          return IsStatisticPresent(CustomStatistic::AchBuildSword);
		case CustomStatistic::AchSnipeSkeleton:      return IsStatisticPresent(CustomStatistic::AchKillEnemy);
		case CustomStatistic::AchBuildPickaxe:       return IsStatisticPresent(CustomStatistic::AchBuildWorkBench);
		case CustomStatistic::AchBuildBetterPickaxe: return IsStatisticPresent(CustomStatistic::AchBuildPickaxe);
		case CustomStatistic::AchBuildFurnace:       return IsStatisticPresent(CustomStatistic::AchBuildWorkBench);
		case CustomStatistic::AchCookFish:           return IsStatisticPresent(CustomStatistic::AchBuildFurnace);
		case CustomStatistic::AchAcquireIron:        return IsStatisticPresent(CustomStatistic::AchBuildFurnace);
		case CustomStatistic::AchOnARail:            return IsStatisticPresent(CustomStatistic::AchAcquireIron);
		case CustomStatistic::AchDiamonds:           return IsStatisticPresent(CustomStatistic::AchAcquireIron);
		case CustomStatistic::AchPortal:             return IsStatisticPresent(CustomStatistic::AchDiamonds);
		case CustomStatistic::AchGhast:              return IsStatisticPresent(CustomStatistic::AchPortal);
		case CustomStatistic::AchBlazeRod:           return IsStatisticPresent(CustomStatistic::AchPortal);
		case CustomStatistic::AchPotion:             return IsStatisticPresent(CustomStatistic::AchBlazeRod);
		case CustomStatistic::AchTheEnd:             return IsStatisticPresent(CustomStatistic::AchBlazeRod);
		case CustomStatistic::AchTheEnd2:            return IsStatisticPresent(CustomStatistic::AchTheEnd);
		case CustomStatistic::AchEnchantments:       return IsStatisticPresent(CustomStatistic::AchDiamonds);
		case CustomStatistic::AchOverkill:           return IsStatisticPresent(CustomStatistic::AchEnchantments);
		case CustomStatistic::AchBookcase:           return IsStatisticPresent(CustomStatistic::AchEnchantments);
		case CustomStatistic::AchExploreAllBiomes:   return IsStatisticPresent(CustomStatistic::AchTheEnd);
		case CustomStatistic::AchSpawnWither:        return IsStatisticPresent(CustomStatistic::AchTheEnd2);
		case CustomStatistic::AchKillWither:         return IsStatisticPresent(CustomStatistic::AchSpawnWither);
		case CustomStatistic::AchFullBeacon:         return IsStatisticPresent(CustomStatistic::AchKillWither);
		case CustomStatistic::AchDiamondsToYou:      return IsStatisticPresent(CustomStatistic::AchDiamonds);
		default: UNREACHABLE("Unsupported achievement type");
	}
}

bool StatisticsManager::IsStatisticPresent(const CustomStatistic a_Stat) const
{
	const auto Result = Custom.find(a_Stat);
	if (Result != Custom.end())
	{
		return Result->second > 0;
	}
	return false;
}
<|endoftext|>