<commit_before>#include "LessParser.h"

bool LessParser::parseStatement(Stylesheet* stylesheet) {
  return parseRuleset(stylesheet) || parseAtRuleOrVariable(stylesheet);
}

bool LessParser::parseAtRuleOrVariable (Stylesheet* stylesheet) {
  string keyword;
  TokenList* rule;
  AtRule* atrule = NULL;

  if (tokenizer->getTokenType() != Token::ATKEYWORD)
    return false;

  keyword = tokenizer->getToken()->str;
  tokenizer->readNextToken();
  skipWhitespace();

  if (tokenizer->getTokenType() == Token::COLON) {
    tokenizer->readNextToken();
    skipWhitespace();

    rule = parseValue();

    if (rule == NULL) {
      throw new ParseException(tokenizer->getToken()->str,
                               "value for variable");
    }
    if (tokenizer->getTokenType() != Token::DELIMITER) {
      throw new ParseException(tokenizer->getToken()->str,
                               "delimiter (';') at end of @-rule");
    }
    valueProcessor->putVariable(keyword, rule);
    tokenizer->readNextToken();
    skipWhitespace();

  } else {
    rule = new TokenList();
    while(parseAny(rule)) {};

    if (!parseBlock(rule)) {
      if (tokenizer->getTokenType() != Token::DELIMITER) {
        throw new ParseException(tokenizer->getToken()->str,
                                 "delimiter (';') at end of @-rule");
      }
      tokenizer->readNextToken();
      skipWhitespace();
    }
    atrule = new AtRule(new string(keyword));
    atrule->setRule(rule);
    stylesheet->addAtRule(atrule);
  }
  return true;
}

bool LessParser::parseRuleset (Stylesheet* stylesheet,
                                   TokenList* selector) {
  Ruleset* ruleset = NULL;
  if (selector == NULL)
    selector = parseSelector();

  if (selector == NULL) {
    if (tokenizer->getTokenType() != Token::BRACKET_OPEN)
      return false;
  } else if (tokenizer->getTokenType() != Token::BRACKET_OPEN) {
    throw new ParseException(tokenizer->getToken()->str,
                             "a declaration block ('{...}')");
  }
  tokenizer->readNextToken();

  if (selector->back()->type != Token::PAREN_CLOSED) {
    ruleset = new Ruleset(selector);
    stylesheet->addRuleset(ruleset);

    skipWhitespace();
    parseRulesetStatement(stylesheet, ruleset);

  } else {
    ruleset = new ParameterRuleset(selector);
    processParameterRuleset((ParameterRuleset*)ruleset);

    valueProcessor->pushScope();

    skipWhitespace();
    parseRulesetStatement(stylesheet, ruleset);

    valueProcessor->popScope();
  }

  if (tokenizer->getTokenType() != Token::BRACKET_CLOSED) {
    throw new ParseException(tokenizer->getToken()->str,
                             "end of declaration block ('}')");
  }
  tokenizer->readNextToken();
  skipWhitespace();

  return true;
}

bool LessParser::parseRulesetStatement (Stylesheet* stylesheet,
                                        Ruleset* ruleset) {
  Declaration* declaration;
  TokenList* selector = NULL;
  string* property = parseProperty();

  // if we can parse a property and the next token is a COLON then the
  // statement is a declaration.
  if (property != NULL) {
    declaration = parseDeclaration(property);
    if (declaration != NULL) {
      ruleset->addDeclaration(declaration);
      if (tokenizer->getTokenType() == Token::DELIMITER) {
        tokenizer->readNextToken();
        skipWhitespace();
        parseRulesetStatement(stylesheet, ruleset);
      }
      return true;
    }
  }

  // otherwise parse a selector
  selector = parseSelector();
  if (selector == NULL && property == NULL)
    return false;

  // Insert property at the front
  if (selector == NULL)
    selector = new TokenList();
  if (property != NULL) {
    selector->unshift(new Token(*property, Token::IDENTIFIER));
    delete property;
  }

  // if followed by a ruleset it's a nested rule
  if (tokenizer->getTokenType() == Token::BRACKET_OPEN) {
    if (selector->front()->str == "&")
      delete selector->shift();
    else
      selector->unshift(new Token(" ", Token::WHITESPACE));
    selector->unshift(ruleset->getSelector());

    parseRuleset(stylesheet, selector);

    parseRulesetStatement(stylesheet, ruleset);
  } else {
    // otherwise it's a mixin.
    parseMixin(selector, ruleset, stylesheet);

    if (tokenizer->getTokenType() == Token::DELIMITER) {
      tokenizer->readNextToken();
      skipWhitespace();
      parseRulesetStatement(stylesheet, ruleset);
    }
  }
  return true;
}

Declaration* LessParser::parseDeclaration (string* property) {
  Declaration* declaration;

  skipWhitespace();
  if (tokenizer->getTokenType() != Token::COLON)
    return NULL;
  tokenizer->readNextToken();
  skipWhitespace();

  declaration = new Declaration(property);

  TokenList* value = parseValue();
  if (value == NULL) {
    throw new ParseException(tokenizer->getToken()->str,
                             "value for property");
  }

  declaration->setValue(value);
  return declaration;
}

TokenList* LessParser::parseValue () {
  TokenList* value = CssParser::parseValue();
  if (value != NULL)
    valueProcessor->processValue(value);

  return value;
}

void LessParser::parseMixin(TokenList* selector, Ruleset* ruleset,
                            Stylesheet* stylesheet) {
  Ruleset* mixin;
  vector<Declaration*>* declarations;
  vector<Declaration*>::iterator it;

  if (processParameterMixin(selector, ruleset)) {

  } else if((mixin = stylesheet->getRuleset(selector)) != NULL) {
    declarations = mixin->getDeclarations();
    for (it = declarations->begin(); it < declarations->end(); it++) {
      ruleset->addDeclaration((*it)->clone());
    }

  } else {
    throw new ParseException(*selector->toString(),
                             "a mixin that has been defined");
  }
}

bool LessParser::processParameterMixin(TokenList* selector, Ruleset* parent) {
  ParameterRuleset* mixin = getParameterRuleset(selector);
  list<TokenList*>* arguments;
  list<TokenList*>::iterator ait;
  list<string> parameters;
  list<string>::iterator pit;
  vector<Declaration*>* declarations;
  vector<Declaration*>::iterator it;
  Declaration* declaration;
  TokenList* variable;
  TokenList* argsCombined;

  if (mixin == NULL)
    return false;

  // new scope
  valueProcessor->pushScope();

  // pull values
  arguments = processArguments(selector);
  parameters = mixin->getKeywords();
  argsCombined = new TokenList();

  // combine with parameter names and add to local scope
  ait = arguments->begin();
  for(pit = parameters.begin(); pit != parameters.end(); pit++) {
    if (ait != arguments->end()) {
      valueProcessor->putVariable(*pit, *ait);
      argsCombined->push((*ait)->clone());
      ait++;
    } else {
      variable = mixin->getDefault(*pit);
      if (variable == NULL) {
        throw new ParseException(*selector->toString(),
                                 "at least one more argument");
      }
      valueProcessor->putVariable(*pit, variable->clone());
      argsCombined->push(variable->clone());
    }
    argsCombined->push(new Token(" ", Token::WHITESPACE));
  }
  delete argsCombined->pop();
  valueProcessor->putVariable("@arguments", argsCombined);

  declarations = mixin->getDeclarations();
  for (it = declarations->begin(); it < declarations->end(); it++) {
    declaration = (*it)->clone();
    valueProcessor->processValue(declaration->getValue());
    parent->addDeclaration(declaration);
  }

  valueProcessor->popScope();
  return true;
}

list<TokenList*>* LessParser::processArguments(TokenList* arguments) {
  TokenList* value;
  TokenListIterator* it = arguments->iterator();
  Token* current;
  list<TokenList*>* ret = new list<TokenList*>();

  while (it->hasNext() &&
         it->next()->type != Token::PAREN_OPEN) {
  }

  while (it->hasNext()) {
    value = new TokenList();

    while (it->hasNext()) {
      current = it->next();
      if (current->str == "," || current->type == Token::PAREN_CLOSED)
        break;
      value->push(current->clone());
    }
    valueProcessor->processValue(value);

    ret->push_back(value);
  }
  return ret;
}

ParameterRuleset* LessParser::getParameterRuleset(TokenList* selector) {
  vector<ParameterRuleset*>::iterator it;
  TokenList key;
  TokenListIterator* tli = selector->iterator();
  Token* current;

  while (tli->hasNext()) {
    current = tli->next();
    if (current->type == Token::PAREN_OPEN)
      break;
    key.push(current->clone());
  }
  delete tli;

  for (it = parameterRulesets.begin(); it < parameterRulesets.end(); it++) {
    if ((*it)->getSelector()->equals(&key))
      return *it;
  }
  return NULL;
}

void LessParser::processParameterRuleset(ParameterRuleset* ruleset) {
  TokenList* selector = ruleset->getSelector();
  TokenList* newselector = new TokenList();

  while (!selector->empty() &&
         selector->front()->type != Token::PAREN_OPEN) {
    newselector->push(selector->shift());
  }
  if (selector->empty()) {
    throw new ParseException(*selector->toString(),
                             "matching parentheses.");
  }

  delete selector->shift();
  while (newselector->back()->type == Token::WHITESPACE)
    delete newselector->pop();
  ruleset->setSelector(newselector);

  while (processParameter(selector, ruleset)) {
  }
  if (selector->front()->type != Token::PAREN_CLOSED) {
    throw new ParseException(*selector->toString(),
                             "matching parentheses.");
  }
  delete selector;
  parameterRulesets.push_back(ruleset);
}

bool LessParser::processParameter(TokenList* selector,
                                  ParameterRuleset* ruleset) {
  Token* current;
  string keyword;
  TokenList* value = NULL;

  while (!selector->empty() &&
         selector->front()->type == Token::WHITESPACE) {
    delete selector->shift();
  }

  if (selector->empty() ||
      selector->front()->type != Token::ATKEYWORD)
    return false;

  keyword = selector->front()->str;
  delete selector->shift();

  if (selector->front()->type == Token::COLON) {
    delete selector->shift();
    value = new TokenList();

    while (!selector->empty() &&
           selector->front()->type != Token::PAREN_CLOSED &&
           selector->front()->str != ",") {
      value->push(selector->shift());
    }

    while (!value->empty() && value->front()->type ==
           Token::WHITESPACE) {
      delete value->shift();
    }
    if (value->empty()) {
      throw new ParseException(current->str,
                               "default value following ':'");
    }
    if (!selector->empty() && selector->front()->str == ",")
      delete selector->shift();
  }

  ruleset->addParameter(keyword, value);
  return true;
}

<commit_msg>Added check for empty values for @-rules.<commit_after>#include "LessParser.h"

bool LessParser::parseStatement(Stylesheet* stylesheet) {
  return parseRuleset(stylesheet) || parseAtRuleOrVariable(stylesheet);
}

bool LessParser::parseAtRuleOrVariable (Stylesheet* stylesheet) {
  string keyword;
  TokenList* rule;
  AtRule* atrule = NULL;

  if (tokenizer->getTokenType() != Token::ATKEYWORD)
    return false;

  keyword = tokenizer->getToken()->str;
  tokenizer->readNextToken();
  skipWhitespace();

  if (tokenizer->getTokenType() == Token::COLON) {
    tokenizer->readNextToken();
    skipWhitespace();

    rule = parseValue();

    if (rule == NULL || rule->size() == 0) {
      throw new ParseException(tokenizer->getToken()->str,
                               "value for variable");
    }
    if (tokenizer->getTokenType() != Token::DELIMITER) {
      throw new ParseException(tokenizer->getToken()->str,
                               "delimiter (';') at end of @-rule");
    }
    valueProcessor->putVariable(keyword, rule);
    tokenizer->readNextToken();
    skipWhitespace();

  } else {
    rule = new TokenList();
    while(parseAny(rule)) {};

    if (!parseBlock(rule)) {
      if (tokenizer->getTokenType() != Token::DELIMITER) {
        throw new ParseException(tokenizer->getToken()->str,
                                 "delimiter (';') at end of @-rule");
      }
      tokenizer->readNextToken();
      skipWhitespace();
    }
    atrule = new AtRule(new string(keyword));
    atrule->setRule(rule);
    stylesheet->addAtRule(atrule);
  }
  return true;
}

bool LessParser::parseRuleset (Stylesheet* stylesheet,
                                   TokenList* selector) {
  Ruleset* ruleset = NULL;
  if (selector == NULL)
    selector = parseSelector();

  if (selector == NULL) {
    if (tokenizer->getTokenType() != Token::BRACKET_OPEN)
      return false;
  } else if (tokenizer->getTokenType() != Token::BRACKET_OPEN) {
    throw new ParseException(tokenizer->getToken()->str,
                             "a declaration block ('{...}')");
  }
  tokenizer->readNextToken();

  if (selector->back()->type != Token::PAREN_CLOSED) {
    ruleset = new Ruleset(selector);
    stylesheet->addRuleset(ruleset);

    skipWhitespace();
    parseRulesetStatement(stylesheet, ruleset);

  } else {
    ruleset = new ParameterRuleset(selector);
    processParameterRuleset((ParameterRuleset*)ruleset);

    valueProcessor->pushScope();

    skipWhitespace();
    parseRulesetStatement(stylesheet, ruleset);

    valueProcessor->popScope();
  }

  if (tokenizer->getTokenType() != Token::BRACKET_CLOSED) {
    throw new ParseException(tokenizer->getToken()->str,
                             "end of declaration block ('}')");
  }
  tokenizer->readNextToken();
  skipWhitespace();

  return true;
}

bool LessParser::parseRulesetStatement (Stylesheet* stylesheet,
                                        Ruleset* ruleset) {
  Declaration* declaration;
  TokenList* selector = NULL;
  string* property = parseProperty();

  // if we can parse a property and the next token is a COLON then the
  // statement is a declaration.
  if (property != NULL) {
    declaration = parseDeclaration(property);
    if (declaration != NULL) {
      ruleset->addDeclaration(declaration);
      if (tokenizer->getTokenType() == Token::DELIMITER) {
        tokenizer->readNextToken();
        skipWhitespace();
        parseRulesetStatement(stylesheet, ruleset);
      }
      return true;
    }
  }

  // otherwise parse a selector
  selector = parseSelector();
  if (selector == NULL && property == NULL)
    return false;

  // Insert property at the front
  if (selector == NULL)
    selector = new TokenList();
  if (property != NULL) {
    selector->unshift(new Token(*property, Token::IDENTIFIER));
    delete property;
  }

  // if followed by a ruleset it's a nested rule
  if (tokenizer->getTokenType() == Token::BRACKET_OPEN) {
    if (selector->front()->str == "&")
      delete selector->shift();
    else
      selector->unshift(new Token(" ", Token::WHITESPACE));
    selector->unshift(ruleset->getSelector());

    parseRuleset(stylesheet, selector);

    parseRulesetStatement(stylesheet, ruleset);
  } else {
    // otherwise it's a mixin.
    parseMixin(selector, ruleset, stylesheet);

    if (tokenizer->getTokenType() == Token::DELIMITER) {
      tokenizer->readNextToken();
      skipWhitespace();
      parseRulesetStatement(stylesheet, ruleset);
    }
  }
  return true;
}

Declaration* LessParser::parseDeclaration (string* property) {
  Declaration* declaration;

  skipWhitespace();
  if (tokenizer->getTokenType() != Token::COLON)
    return NULL;
  tokenizer->readNextToken();
  skipWhitespace();

  declaration = new Declaration(property);

  TokenList* value = parseValue();
  if (value == NULL) {
    throw new ParseException(tokenizer->getToken()->str,
                             "value for property");
  }

  declaration->setValue(value);
  return declaration;
}

TokenList* LessParser::parseValue () {
  TokenList* value = CssParser::parseValue();
  if (value != NULL)
    valueProcessor->processValue(value);

  return value;
}

void LessParser::parseMixin(TokenList* selector, Ruleset* ruleset,
                            Stylesheet* stylesheet) {
  Ruleset* mixin;
  vector<Declaration*>* declarations;
  vector<Declaration*>::iterator it;

  if (processParameterMixin(selector, ruleset)) {

  } else if((mixin = stylesheet->getRuleset(selector)) != NULL) {
    declarations = mixin->getDeclarations();
    for (it = declarations->begin(); it < declarations->end(); it++) {
      ruleset->addDeclaration((*it)->clone());
    }

  } else {
    throw new ParseException(*selector->toString(),
                             "a mixin that has been defined");
  }
}

bool LessParser::processParameterMixin(TokenList* selector, Ruleset* parent) {
  ParameterRuleset* mixin = getParameterRuleset(selector);
  list<TokenList*>* arguments;
  list<TokenList*>::iterator ait;
  list<string> parameters;
  list<string>::iterator pit;
  vector<Declaration*>* declarations;
  vector<Declaration*>::iterator it;
  Declaration* declaration;
  TokenList* variable;
  TokenList* argsCombined;

  if (mixin == NULL)
    return false;

  // new scope
  valueProcessor->pushScope();

  // pull values
  arguments = processArguments(selector);
  parameters = mixin->getKeywords();
  argsCombined = new TokenList();

  // combine with parameter names and add to local scope
  ait = arguments->begin();
  for(pit = parameters.begin(); pit != parameters.end(); pit++) {
    if (ait != arguments->end()) {
      valueProcessor->putVariable(*pit, *ait);
      argsCombined->push((*ait)->clone());
      ait++;
    } else {
      variable = mixin->getDefault(*pit);
      if (variable == NULL) {
        throw new ParseException(*selector->toString(),
                                 "at least one more argument");
      }
      valueProcessor->putVariable(*pit, variable->clone());
      argsCombined->push(variable->clone());
    }
    argsCombined->push(new Token(" ", Token::WHITESPACE));
  }
  delete argsCombined->pop();
  valueProcessor->putVariable("@arguments", argsCombined);

  declarations = mixin->getDeclarations();
  for (it = declarations->begin(); it < declarations->end(); it++) {
    declaration = (*it)->clone();
    valueProcessor->processValue(declaration->getValue());
    parent->addDeclaration(declaration);
  }

  valueProcessor->popScope();
  return true;
}

list<TokenList*>* LessParser::processArguments(TokenList* arguments) {
  TokenList* value;
  TokenListIterator* it = arguments->iterator();
  Token* current;
  list<TokenList*>* ret = new list<TokenList*>();

  while (it->hasNext() &&
         it->next()->type != Token::PAREN_OPEN) {
  }

  while (it->hasNext()) {
    value = new TokenList();

    while (it->hasNext()) {
      current = it->next();
      if (current->str == "," || current->type == Token::PAREN_CLOSED)
        break;
      value->push(current->clone());
    }
    valueProcessor->processValue(value);

    ret->push_back(value);
  }
  return ret;
}

ParameterRuleset* LessParser::getParameterRuleset(TokenList* selector) {
  vector<ParameterRuleset*>::iterator it;
  TokenList key;
  TokenListIterator* tli = selector->iterator();
  Token* current;

  while (tli->hasNext()) {
    current = tli->next();
    if (current->type == Token::PAREN_OPEN)
      break;
    key.push(current->clone());
  }
  delete tli;

  for (it = parameterRulesets.begin(); it < parameterRulesets.end(); it++) {
    if ((*it)->getSelector()->equals(&key))
      return *it;
  }
  return NULL;
}

void LessParser::processParameterRuleset(ParameterRuleset* ruleset) {
  TokenList* selector = ruleset->getSelector();
  TokenList* newselector = new TokenList();

  while (!selector->empty() &&
         selector->front()->type != Token::PAREN_OPEN) {
    newselector->push(selector->shift());
  }
  if (selector->empty()) {
    throw new ParseException(*selector->toString(),
                             "matching parentheses.");
  }

  delete selector->shift();
  while (newselector->back()->type == Token::WHITESPACE)
    delete newselector->pop();
  ruleset->setSelector(newselector);

  while (processParameter(selector, ruleset)) {
  }
  if (selector->front()->type != Token::PAREN_CLOSED) {
    throw new ParseException(*selector->toString(),
                             "matching parentheses.");
  }
  delete selector;
  parameterRulesets.push_back(ruleset);
}

bool LessParser::processParameter(TokenList* selector,
                                  ParameterRuleset* ruleset) {
  Token* current;
  string keyword;
  TokenList* value = NULL;

  while (!selector->empty() &&
         selector->front()->type == Token::WHITESPACE) {
    delete selector->shift();
  }

  if (selector->empty() ||
      selector->front()->type != Token::ATKEYWORD)
    return false;

  keyword = selector->front()->str;
  delete selector->shift();

  if (selector->front()->type == Token::COLON) {
    delete selector->shift();
    value = new TokenList();

    while (!selector->empty() &&
           selector->front()->type != Token::PAREN_CLOSED &&
           selector->front()->str != ",") {
      value->push(selector->shift());
    }

    while (!value->empty() && value->front()->type ==
           Token::WHITESPACE) {
      delete value->shift();
    }
    if (value->empty()) {
      throw new ParseException(current->str,
                               "default value following ':'");
    }
    if (!selector->empty() && selector->front()->str == ",")
      delete selector->shift();
  }

  ruleset->addParameter(keyword, value);
  return true;
}

<|endoftext|>


<commit_before>#ifndef _GEMSET_MULTITHREADED_HPP
#define _GEMSET_MULTITHREADED_HPP

#include <vector>
#include <mutex>
#include <thread>
#include "gem_utils.hpp"
#include <semaphore.h>

using namespace std;

const int BLOCK_SIZE=10000;
const int THREADS_N=4;
sem_t sema;

vector<Gem*>** generate_gemset_multithreaded (
  vector<Gem*>*  base_gems,
  int           limit_value,
  void          (*limiter) (vector<Gem*>*&)
)
{
  vector<Gem*>** gemset = new vector<Gem*>*[limit_value+1];
  vector<vector<Gem*>*>*pools = new vector<vector<Gem*>*>[limit_value+1];
  for (int i=1; i<=limit_value; i++)
    gemset[i]=new vector<Gem*>;
  for (Gem* e:*base_gems)
    gemset[e->value]->push_back(e);
  pools[1].push_back(new vector<Gem*>);
  for (Gem* e:*gemset[1])
    pools[1].back()->push_back(e);
  sem_init(&sema, 0, THREADS_N);
  for (int i=2; i<=limit_value; i++)
  {
    cout<<"Iteration: "<<i<<"\n";
    vector<Gem*>*& current_pool=gemset[i];
    vector<thread*> threads;
    mutex m;
    for (int first_val=1; 2*first_val<=i; first_val++)
    {
      int second_val=i-first_val;
      for (vector<Gem*>* p1 : pools[first_val])
        for (vector<Gem*>* p2 : pools[second_val])
        {
//           sem_wait(&sema);
//           threads.push_back(new thread(
            [&m, &current_pool, p1, p2, limiter] ()
            {
              vector<Gem*> *tmp = new vector<Gem*>;
              tmp->reserve(p1->size()*p2->size());
              for (Gem* g1 : *p1)
                for (Gem* g2 : *p2)
                  tmp->push_back(combine(g1, g2));
              limiter(tmp);
//               sem_post(&sema);
              m.lock();
              for (Gem* a : *tmp)
                current_pool->push_back(a);
              if (current_pool->size()>1000*1000)
              {
                cout<<"overflow: "<<current_pool->size()<<" ";
                limiter(current_pool);
                current_pool->reserve(1000*1000);
                cout<<current_pool->size()<<"\n";
              }
              m.unlock();
              delete tmp;
            }();
//           ));
        }
    }

    for (thread* th : threads)
    {
      th->join();
      delete th;
    }

    limiter(current_pool);
    pools[i].push_back(new vector<Gem*>);
    for (Gem* e : *current_pool)
    {
      if (pools[i].back()->size()>=1000)
        pools[i].push_back(new vector<Gem*>);
      pools[i].back()->push_back(e);
    }
    cout<<"Pool size after limiting: "<<current_pool->size()<<"\n";
    cout<<"End of iteration "<<i<<"\n\n\n";
  }
  return gemset;
}

#endif // _GEMSET_MULTITHREADED_HPP
<commit_msg>debug from file removed<commit_after>#ifndef _GEMSET_MULTITHREADED_HPP
#define _GEMSET_MULTITHREADED_HPP

#include <vector>
#include <mutex>
#include <thread>
#include "gem_utils.hpp"
#include <semaphore.h>

using namespace std;

const int BLOCK_SIZE=10000;
const int THREADS_N=4;
sem_t sema;

vector<Gem*>** generate_gemset_multithreaded (
  vector<Gem*>*  base_gems,
  int           limit_value,
  void          (*limiter) (vector<Gem*>*&)
)
{
  vector<Gem*>** gemset = new vector<Gem*>*[limit_value+1];
  vector<vector<Gem*>*>*pools = new vector<vector<Gem*>*>[limit_value+1];
  for (int i=1; i<=limit_value; i++)
    gemset[i]=new vector<Gem*>;
  for (Gem* e:*base_gems)
    gemset[e->value]->push_back(e);
  pools[1].push_back(new vector<Gem*>);
  for (Gem* e:*gemset[1])
    pools[1].back()->push_back(e);
  sem_init(&sema, 0, THREADS_N);
  for (int i=2; i<=limit_value; i++)
  {
    cout<<"Iteration: "<<i<<"\n";
    vector<Gem*>*& current_pool=gemset[i];
    vector<thread*> threads;
    mutex m;
    for (int first_val=1; 2*first_val<=i; first_val++)
    {
      int second_val=i-first_val;
      for (vector<Gem*>* p1 : pools[first_val])
        for (vector<Gem*>* p2 : pools[second_val])
        {
          sem_wait(&sema);
          threads.push_back(new thread(
            [&m, &current_pool, p1, p2, limiter] ()
            {
              vector<Gem*> *tmp = new vector<Gem*>;
              tmp->reserve(p1->size()*p2->size());
              for (Gem* g1 : *p1)
                for (Gem* g2 : *p2)
                  tmp->push_back(combine(g1, g2));
              limiter(tmp);
              sem_post(&sema);
              m.lock();
              for (Gem* a : *tmp)
                current_pool->push_back(a);
              if (current_pool->size()>1000*1000)
              {
                cout<<"overflow: "<<current_pool->size()<<" ";
                limiter(current_pool);
                current_pool->reserve(1000*1000);
                cout<<current_pool->size()<<"\n";
              }
              m.unlock();
              delete tmp;
            }
          ));
        }
    }

    for (thread* th : threads)
    {
      th->join();
      delete th;
    }

    limiter(current_pool);
    pools[i].push_back(new vector<Gem*>);
    for (Gem* e : *current_pool)
    {
      if (pools[i].back()->size()>=1000)
        pools[i].push_back(new vector<Gem*>);
      pools[i].back()->push_back(e);
    }
    cout<<"Pool size after limiting: "<<current_pool->size()<<"\n";
    cout<<"End of iteration "<<i<<"\n\n\n";
  }
  return gemset;
}

#endif // _GEMSET_MULTITHREADED_HPP
<|endoftext|>


<commit_before>#include <string>
#include <iostream>
#include <assert.h>
#include <math.h>
#include <aterm2.h>

using namespace std;

class ATermMap
{
private:

    struct KeyValue
    {
        ATerm key;
        ATerm value;
    };

    /* Hash table for the map.  We use open addressing, i.e., all
       key/value pairs are stored directly in the table, and there are
       no pointers.  Collisions are resolved through probing. */
    KeyValue * hashTable;

    /* Current size of the hash table. */
    unsigned int size;

    /* Number of elements in the hash table. */
    unsigned int count;

    /* Maximum number of elements in the hash table.  If `count'
       exceeds this number, the hash table is expanded. */
    unsigned int maxCount;

public:
    /* Create a map.  `expectedCount' is the number of elements the
       map is expected to hold. */
    ATermMap(unsigned int expectedCount);

    ATermMap(const ATermMap & map);

    ~ATermMap();

    ATermMap & operator = (const ATermMap & map);

    void set(ATerm key, ATerm value);

    ATerm get(ATerm key) const;

    void remove(ATerm key);

private:
    void init(unsigned int expectedCount);

    void free();

    void resizeTable(unsigned int expectedCount);

    void copy(KeyValue * elements, unsigned int size);

    inline unsigned int hash1(ATerm key) const;
    inline unsigned int hash2(ATerm key) const;
};

ATermMap::ATermMap(unsigned int expectedCount)
{
    init(expectedCount * 10 / 9); /* slight adjustment */
}

ATermMap::ATermMap(const ATermMap & map)
{
    init(map.maxCount);
    copy(map.hashTable, map.size);
}

ATermMap & ATermMap::operator = (const ATermMap & map)
{
    if (this == &map) return *this;
    free();
    init(map.maxCount);
    copy(map.hashTable, map.size);
    return *this;
}

ATermMap::~ATermMap()
{
    free();
}

void ATermMap::init(unsigned int expectedCount)
{
    assert(sizeof(ATerm) * 2 == sizeof(KeyValue));
    size = 0;
    count = 0;
    maxCount = 0;
    hashTable = 0;
    resizeTable(expectedCount);
}

void ATermMap::free()
{
    if (hashTable) {
        ATunprotectArray((ATerm *) hashTable);
        ::free(hashTable);
        hashTable = 0;
    }
}

static unsigned int roundToPowerOf2(unsigned int x)
{
    x--;
    x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16;
    x++;
    return x;
}

static const unsigned int maxLoadFactor = /* 1 / */ 3;
static unsigned int nrResizes = 0;

void ATermMap::resizeTable(unsigned int expectedCount)
{
    if (expectedCount == 0) expectedCount = 1;
//     cout << maxCount << " -> " << expectedCount << endl;
//     cout << maxCount << " " << size << endl;
//     cout << (double) size / maxCount << endl;

    unsigned int oldSize = size;
    KeyValue * oldHashTable = hashTable;

    maxCount = expectedCount;
    size = roundToPowerOf2(maxCount * maxLoadFactor);
    hashTable = (KeyValue *) calloc(sizeof(KeyValue), size);
    ATprotectArray((ATerm *) hashTable, size * 2);

//     cout << size << endl;

    /* Re-hash the elements in the old table. */
    if (oldSize != 0) {
        count = 0;
        copy(oldHashTable, oldSize);
        ATunprotectArray((ATerm *) oldHashTable);
        ::free(oldHashTable);
        nrResizes++;
    }
}

void ATermMap::copy(KeyValue * elements, unsigned int size)
{
    for (unsigned int i = 0; i < size; ++i)
        if (elements[i].value) /* i.e., non-empty, non-deleted element */
            set(elements[i].key, elements[i].value);
}

static const unsigned int shift = 16;
static const unsigned int knuth = (unsigned int) (0.6180339887 * (1 << shift));

unsigned int ATermMap::hash1(ATerm key) const
{
    /* Don't care about the least significant bits of the ATerm
       pointer since they're always 0. */
    unsigned int key2 = ((unsigned int) key) >> 2;

    /* Approximately equal to:
    double d = key2 * 0.6180339887;
    unsigned int h = (int) (size * (d - floor(d)));
    */

    unsigned int h = (size * ((key2 * knuth) & ((1 << shift) - 1))) >> shift;

    return h;
}

unsigned int ATermMap::hash2(ATerm key) const
{
    unsigned int key2 = ((unsigned int) key) >> 2;
    /* Note: the result must be relatively prime to `size' (which is a
       power of 2), so we make sure that the result is always odd. */
    unsigned int h = ((key2 * 134217689) & (size - 1)) | 1;
    return h;
}

static unsigned int nrItemsSet = 0;
static unsigned int nrSetProbes = 0;

void ATermMap::set(ATerm key, ATerm value)
{
    if (count == maxCount) resizeTable(size * 2 / maxLoadFactor);

    nrItemsSet++;
    for (unsigned int i = 0, h = hash1(key); i < size;
         ++i, h = (h + hash2(key)) & (size - 1))
    {
        // assert(h < size);
        nrSetProbes++;
        /* Note: to see whether a slot is free, we check
           hashTable[h].value, not hashTable[h].key, since we use
           value == 0 to mark deleted slots. */
        if (hashTable[h].value == 0 || hashTable[h].key == key) {
            hashTable[h].key = key;
            hashTable[h].value = value;
            count++;
            return;
        }
    }

    abort();
}

static unsigned int nrItemsGet = 0;
static unsigned int nrGetProbes = 0;

ATerm ATermMap::get(ATerm key) const
{
    nrItemsGet++;
    for (unsigned int i = 0, h = hash1(key); i < size;
         ++i, h = (h + hash2(key)) & (size - 1))
    {
        nrGetProbes++;
        if (hashTable[h].key == 0) return 0;
        if (hashTable[h].key == key) return hashTable[h].value;
    }
    return 0;
}

void ATermMap::remove(ATerm key)
{
    for (unsigned int i = 0, h = hash1(key); i < size;
         ++i, h = (h + hash2(key)) & (size - 1))
    {
        if (hashTable[h].key == 0) return;
        if (hashTable[h].key == key) {
            hashTable[h].value = 0;
            return;
        }
    }
}

int main(int argc, char * * argv)
{
    ATerm bottomOfStack;
    ATinit(argc, argv, &bottomOfStack);

    /* Make test terms. */
    int nrTestTerms = 100000;
    ATerm testTerms[nrTestTerms];

    for (int i = 0; i < nrTestTerms; ++i) {
        char name[10];
        sprintf(name, "%d", (int) random() % 37);

        int arity = i == 0 ? 0 : (random() % 37);
        ATerm kids[arity];
        for (int j = 0; j < arity; ++j)
            kids[j] = testTerms[random() % i];

        testTerms[i] = (ATerm) ATmakeApplArray(ATmakeAFun(name, arity, ATfalse), kids);
//         ATwriteToSharedTextFile(testTerms[i], stdout);
//         printf("\n");
    }

    cout << "testing...\n";

    #define someTerm() (testTerms[(int) random() % nrTestTerms])

    for (int test = 0; test < 100000; ++test) {
        // cerr << test << endl;
        unsigned int n = 300;
        ATermMap map(300);
        ATerm keys[n], values[n];
        for (unsigned int i = 0; i < n; ++i) {
            keys[i] = someTerm();
            values[i] = someTerm();
            map.set(keys[i], values[i]);
            // cerr << "INSERT: " << keys[i] << " " << values[i] << endl;
        }
        values[n - 1] = 0;
        map.remove(keys[n - 1]);
        for (unsigned int i = 0; i < n; ++i) {
            if (map.get(keys[i]) != values[i]) {
                for (unsigned int j = i + 1; j < n; ++j)
                    if (keys[i] == keys[j]) goto x;
                cerr << "MISMATCH: " << keys[i] << " " << values[i] << " " << map.get(keys[i]) << " " << i << endl;
                abort();
            x: ;
            }
        }
        for (unsigned int i = 0; i < 100; ++i)
            map.get(someTerm());
    }

    cout << "RESIZES: " << nrResizes << endl;

    cout << "SET: "
         << nrItemsSet << " "
         << nrSetProbes << " "
         << (double) nrSetProbes / nrItemsSet << endl;

    cout << "GET: "
         << nrItemsGet << " "
         << nrGetProbes << " "
         << (double) nrGetProbes / nrItemsGet << endl;
}
<commit_msg>* Maintain the count field properly.<commit_after>#include <string>
#include <iostream>
#include <assert.h>
#include <math.h>
#include <aterm2.h>

using namespace std;

class ATermMap
{
private:

    struct KeyValue
    {
        ATerm key;
        ATerm value;
    };

    /* Hash table for the map.  We use open addressing, i.e., all
       key/value pairs are stored directly in the table, and there are
       no pointers.  Collisions are resolved through probing. */
    KeyValue * hashTable;

    /* Current size of the hash table. */
    unsigned int capacity;

    /* Number of elements in the hash table. */
    unsigned int count;

    /* Maximum number of elements in the hash table.  If `count'
       exceeds this number, the hash table is expanded. */
    unsigned int maxCount;

public:
    /* Create a map.  `expectedCount' is the number of elements the
       map is expected to hold. */
    ATermMap(unsigned int expectedCount);

    ATermMap(const ATermMap & map);

    ~ATermMap();

    ATermMap & operator = (const ATermMap & map);

    void set(ATerm key, ATerm value);

    ATerm get(ATerm key) const;

    void remove(ATerm key);

    unsigned int size();

private:
    void init(unsigned int expectedCount);

    void free();

    void resizeTable(unsigned int expectedCount);

    void copy(KeyValue * elements, unsigned int capacity);

    inline unsigned int hash1(ATerm key) const;
    inline unsigned int hash2(ATerm key) const;
};

ATermMap::ATermMap(unsigned int expectedCount)
{
    init(expectedCount * 10 / 9); /* slight adjustment */
}

ATermMap::ATermMap(const ATermMap & map)
{
    init(map.maxCount);
    copy(map.hashTable, map.capacity);
}

ATermMap & ATermMap::operator = (const ATermMap & map)
{
    if (this == &map) return *this;
    free();
    init(map.maxCount);
    copy(map.hashTable, map.capacity);
    return *this;
}

ATermMap::~ATermMap()
{
    free();
}

void ATermMap::init(unsigned int expectedCount)
{
    assert(sizeof(ATerm) * 2 == sizeof(KeyValue));
    capacity = 0;
    count = 0;
    maxCount = 0;
    hashTable = 0;
    resizeTable(expectedCount);
}

void ATermMap::free()
{
    if (hashTable) {
        ATunprotectArray((ATerm *) hashTable);
        ::free(hashTable);
        hashTable = 0;
    }
}

static unsigned int roundToPowerOf2(unsigned int x)
{
    x--;
    x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16;
    x++;
    return x;
}

static const unsigned int maxLoadFactor = /* 1 / */ 3;
static unsigned int nrResizes = 0;

void ATermMap::resizeTable(unsigned int expectedCount)
{
    if (expectedCount == 0) expectedCount = 1;
//     cout << maxCount << " -> " << expectedCount << endl;
//     cout << maxCount << " " << size << endl;
//     cout << (double) size / maxCount << endl;

    unsigned int oldCapacity = capacity;
    KeyValue * oldHashTable = hashTable;

    maxCount = expectedCount;
    capacity = roundToPowerOf2(maxCount * maxLoadFactor);
    hashTable = (KeyValue *) calloc(sizeof(KeyValue), capacity);
    ATprotectArray((ATerm *) hashTable, capacity * 2);

//     cout << capacity << endl;

    /* Re-hash the elements in the old table. */
    if (oldCapacity != 0) {
        count = 0;
        copy(oldHashTable, oldCapacity);
        ATunprotectArray((ATerm *) oldHashTable);
        ::free(oldHashTable);
        nrResizes++;
    }
}

void ATermMap::copy(KeyValue * elements, unsigned int capacity)
{
    for (unsigned int i = 0; i < capacity; ++i)
        if (elements[i].value) /* i.e., non-empty, non-deleted element */
            set(elements[i].key, elements[i].value);
}

static const unsigned int shift = 16;
static const unsigned int knuth = (unsigned int) (0.6180339887 * (1 << shift));

unsigned int ATermMap::hash1(ATerm key) const
{
    /* Don't care about the least significant bits of the ATerm
       pointer since they're always 0. */
    unsigned int key2 = ((unsigned int) key) >> 2;

    /* Approximately equal to:
    double d = key2 * 0.6180339887;
    unsigned int h = (int) (capacity * (d - floor(d)));
    */

    unsigned int h = (capacity * ((key2 * knuth) & ((1 << shift) - 1))) >> shift;

    return h;
}

unsigned int ATermMap::hash2(ATerm key) const
{
    unsigned int key2 = ((unsigned int) key) >> 2;
    /* Note: the result must be relatively prime to `capacity' (which
       is a power of 2), so we make sure that the result is always
       odd. */
    unsigned int h = ((key2 * 134217689) & (capacity - 1)) | 1;
    return h;
}

static unsigned int nrItemsSet = 0;
static unsigned int nrSetProbes = 0;

void ATermMap::set(ATerm key, ATerm value)
{
    if (count == maxCount) resizeTable(capacity * 2 / maxLoadFactor);

    nrItemsSet++;
    for (unsigned int i = 0, h = hash1(key); i < capacity;
         ++i, h = (h + hash2(key)) & (capacity - 1))
    {
        // assert(h < capacity);
        nrSetProbes++;
        /* Note: to see whether a slot is free, we check
           hashTable[h].value, not hashTable[h].key, since we use
           value == 0 to mark deleted slots. */
        if (hashTable[h].value == 0 || hashTable[h].key == key) {
            if (hashTable[h].value == 0) count++;
            hashTable[h].key = key;
            hashTable[h].value = value;
            return;
        }
    }

    abort();
}

static unsigned int nrItemsGet = 0;
static unsigned int nrGetProbes = 0;

ATerm ATermMap::get(ATerm key) const
{
    nrItemsGet++;
    for (unsigned int i = 0, h = hash1(key); i < capacity;
         ++i, h = (h + hash2(key)) & (capacity - 1))
    {
        nrGetProbes++;
        if (hashTable[h].key == 0) return 0;
        if (hashTable[h].key == key) return hashTable[h].value;
    }
    return 0;
}

void ATermMap::remove(ATerm key)
{
    for (unsigned int i = 0, h = hash1(key); i < capacity;
         ++i, h = (h + hash2(key)) & (capacity - 1))
    {
        if (hashTable[h].key == 0) return;
        if (hashTable[h].key == key) {
            if (hashTable[h].value != 0) {
                hashTable[h].value = 0;
                count--;
            }
            return;
        }
    }
}

unsigned int ATermMap::size()
{
    return count; /* STL nomenclature */
}

int main(int argc, char * * argv)
{
    ATerm bottomOfStack;
    ATinit(argc, argv, &bottomOfStack);

    /* Make test terms. */
    int nrTestTerms = 100000;
    ATerm testTerms[nrTestTerms];

    for (int i = 0; i < nrTestTerms; ++i) {
        char name[10];
        sprintf(name, "%d", (int) random() % 37);

        int arity = i == 0 ? 0 : (random() % 37);
        ATerm kids[arity];
        for (int j = 0; j < arity; ++j)
            kids[j] = testTerms[random() % i];

        testTerms[i] = (ATerm) ATmakeApplArray(ATmakeAFun(name, arity, ATfalse), kids);
//         ATwriteToSharedTextFile(testTerms[i], stdout);
//         printf("\n");
    }

    cout << "testing...\n";

    #define someTerm() (testTerms[(int) random() % nrTestTerms])

    for (int test = 0; test < 100000; ++test) {
        // cerr << test << endl;
        unsigned int n = 300;
        ATermMap map(300);
        ATerm keys[n], values[n];
        for (unsigned int i = 0; i < n; ++i) {
            keys[i] = someTerm();
            values[i] = someTerm();
            map.set(keys[i], values[i]);
            // cerr << "INSERT: " << keys[i] << " " << values[i] << endl;
        }
        unsigned int size = map.size();
        assert(size <= n);
        values[n - 1] = 0;
        map.remove(keys[n - 1]);
        assert(map.size() == size - 1);
        for (unsigned int i = 0; i < n; ++i) {
            if (map.get(keys[i]) != values[i]) {
                for (unsigned int j = i + 1; j < n; ++j)
                    if (keys[i] == keys[j]) goto x;
                cerr << "MISMATCH: " << keys[i] << " " << values[i] << " " << map.get(keys[i]) << " " << i << endl;
                abort();
            x: ;
            }
        }
        for (unsigned int i = 0; i < 100; ++i)
            map.get(someTerm());
    }

    cout << "RESIZES: " << nrResizes << endl;

    cout << "SET: "
         << nrItemsSet << " "
         << nrSetProbes << " "
         << (double) nrSetProbes / nrItemsSet << endl;

    cout << "GET: "
         << nrItemsGet << " "
         << nrGetProbes << " "
         << (double) nrGetProbes / nrItemsGet << endl;
}
<|endoftext|>


<commit_before>#pragma once

#include <unordered_map>
#include <mutex>
#include "base/common_util.hpp"
#include "client_base.hpp"

namespace czrpc
{
namespace client
{
class async_rpc_client : public client_base
{
public:
    async_rpc_client(const async_rpc_client&) = delete;
    async_rpc_client& operator=(const async_rpc_client&) = delete;
    async_rpc_client()
    {
        client_type_ = client_type::rpc_client;
    }

    virtual void run() override final
    {
        client_base::run();
        sync_connect();
    }

    using task_t = std::function<void(const response_content&)>;
    class rpc_task
    {
    public:
        rpc_task(const client_flag& flag, const request_content& content, async_rpc_client* client)
            : flag_(flag), content_(content), client_(client) {}

        using result_callback_func = std::function<void(const message_ptr&)>;
        void result(const result_callback_func& func)
        {
            task_ = [&func, this](const response_content& content)
            {
                try
                {
                    func(serialize_util::singleton::get()->deserialize(content.message_name, content.body));
                }
                catch (std::exception& e)
                {
                    log_warn(e.what());
                }
            };
            client_->async_call_one_way(flag_, content_);
            client_->add_bind_func(content_.call_id, task_);
        }

    private:
        client_flag flag_;
        request_content content_;
        task_t task_;
        async_rpc_client* client_;
    };

    auto async_call(const std::string& func_name, const message_ptr& message)
    {
        serialize_util::singleton::get()->check_message(message);
        sync_connect();
        request_content content;
        content.call_id = gen_uuid();
        content.protocol = func_name;
        content.message_name = message->GetDescriptor()->full_name();
        content.body = serialize_util::singleton::get()->serialize(message);

        client_flag flag{ serialize_mode::serialize, client_type_ };
        return rpc_task{ flag, content, this };
    }

private:
    void async_read_head()
    {
        boost::asio::async_read(get_socket(), boost::asio::buffer(res_head_buf_),
                                [this](boost::system::error_code ec, std::size_t)
        {
            if (!get_socket().is_open())
            {
                log_warn("Socket is not open");
                return;
            }

            if (ec)
            {
                log_warn(ec.message());
                return;
            }

            if (async_check_head())
            {
                async_read_content();
            }
            else
            {
                async_read_head();
            }
        });
    }

    bool async_check_head()
    {
        memcpy(&res_head_, res_head_buf_, sizeof(res_head_buf_));
        if (res_head_.call_id_len + res_head_.message_name_len + res_head_.body_len > max_buffer_len)
        {
            log_warn("Content len is too big");
            return false;
        }
        return true;
    }

    void async_read_content()
    {
        content_.clear();
        content_.resize(res_head_.call_id_len + res_head_.message_name_len + res_head_.body_len);
        boost::asio::async_read(get_socket(), boost::asio::buffer(content_),
                                [this](boost::system::error_code ec, std::size_t)
        {
            async_read_head();

            if (!get_socket().is_open())
            {
                log_warn("Socket is not open");
                return;
            }

            if (ec)
            {
                log_warn(ec.message());
                return;
            }

            response_content content;
            content.call_id.assign(&content_[0], res_head_.call_id_len);
            content.message_name.assign(&content_[res_head_.call_id_len], res_head_.message_name_len);
            content.body.assign(&content_[res_head_.call_id_len + res_head_.message_name_len], res_head_.body_len);
            if (res_head_.error_code == rpc_error_code::ok)
            {
                route(content);
            }
            else
            {
                log_warn(get_rpc_error_string(res_head_.error_code));
                std::lock_guard<std::mutex> lock(task_mutex_);
                task_map_.erase(content.call_id);
            }
        });
    }

    void add_bind_func(const std::string& call_id, const task_t& task)
    {
        std::lock_guard<std::mutex> lock(task_mutex_);
        task_map_.emplace(call_id, task);
    }

    void route(const response_content& content)
    {
        std::lock_guard<std::mutex> lock(task_mutex_);
        auto iter = task_map_.find(content.call_id);
        if (iter != task_map_.end())
        {
            iter->second(content);
            task_map_.erase(iter);
            std::cout << "map size: " << task_map_.size() << std::endl;
        }
    }

    void sync_connect()
    {
        if (try_connect())
        {
            async_read_head();
        }
    }

private:
    char res_head_buf_[response_header_len];
    response_header res_head_;
    std::vector<char> content_;

    std::unordered_map<std::string, task_t> task_map_;
    std::mutex task_mutex_;
};

}
}

<commit_msg>update<commit_after>#pragma once

#include <unordered_map>
#include <mutex>
#include "base/common_util.hpp"
#include "base/table/threadsafe_unordered_map.hpp"
#include "client_base.hpp"

using namespace czrpc::base::table;

namespace czrpc
{
namespace client
{
class async_rpc_client : public client_base
{
public:
    async_rpc_client(const async_rpc_client&) = delete;
    async_rpc_client& operator=(const async_rpc_client&) = delete;
    async_rpc_client()
    {
        client_type_ = client_type::rpc_client;
    }

    virtual void run() override final
    {
        client_base::run();
        sync_connect();
    }

    using task_t = std::function<void(const response_content&)>;
    class rpc_task
    {
    public:
        rpc_task(const client_flag& flag, const request_content& content, async_rpc_client* client)
            : flag_(flag), content_(content), client_(client) {}

        using result_callback_func = std::function<void(const message_ptr&)>;
        void result(const result_callback_func& func)
        {
            task_ = [&func, this](const response_content& content)
            {
                try
                {
                    func(serialize_util::singleton::get()->deserialize(content.message_name, content.body));
                }
                catch (std::exception& e)
                {
                    log_warn(e.what());
                }
            };
            client_->async_call_one_way(flag_, content_);
            client_->add_bind_func(content_.call_id, task_);
        }

    private:
        client_flag flag_;
        request_content content_;
        task_t task_;
        async_rpc_client* client_;
    };

    auto async_call(const std::string& func_name, const message_ptr& message)
    {
        serialize_util::singleton::get()->check_message(message);
        sync_connect();
        request_content content;
        content.call_id = gen_uuid();
        content.protocol = func_name;
        content.message_name = message->GetDescriptor()->full_name();
        content.body = serialize_util::singleton::get()->serialize(message);

        client_flag flag{ serialize_mode::serialize, client_type_ };
        return rpc_task{ flag, content, this };
    }

private:
    void async_read_head()
    {
        boost::asio::async_read(get_socket(), boost::asio::buffer(res_head_buf_),
                                [this](boost::system::error_code ec, std::size_t)
        {
            if (!get_socket().is_open())
            {
                log_warn("Socket is not open");
                return;
            }

            if (ec)
            {
                log_warn(ec.message());
                return;
            }

            if (async_check_head())
            {
                async_read_content();
            }
            else
            {
                async_read_head();
            }
        });
    }

    bool async_check_head()
    {
        memcpy(&res_head_, res_head_buf_, sizeof(res_head_buf_));
        if (res_head_.call_id_len + res_head_.message_name_len + res_head_.body_len > max_buffer_len)
        {
            log_warn("Content len is too big");
            return false;
        }
        return true;
    }

    void async_read_content()
    {
        content_.clear();
        content_.resize(res_head_.call_id_len + res_head_.message_name_len + res_head_.body_len);
        boost::asio::async_read(get_socket(), boost::asio::buffer(content_),
                                [this](boost::system::error_code ec, std::size_t)
        {
            async_read_head();

            if (!get_socket().is_open())
            {
                log_warn("Socket is not open");
                return;
            }

            if (ec)
            {
                log_warn(ec.message());
                return;
            }

            response_content content;
            content.call_id.assign(&content_[0], res_head_.call_id_len);
            content.message_name.assign(&content_[res_head_.call_id_len], res_head_.message_name_len);
            content.body.assign(&content_[res_head_.call_id_len + res_head_.message_name_len], res_head_.body_len);
            if (res_head_.error_code == rpc_error_code::ok)
            {
                route(content);
            }
            else
            {
                log_warn(get_rpc_error_string(res_head_.error_code));
                task_map_.erase(content.call_id);
            }
        });
    }

    void add_bind_func(const std::string& call_id, const task_t& task)
    {
        task_map_.emplace(call_id, task);
    }

    void route(const response_content& content)
    {
        task_t task;
        if (task_map_.find(content.call_id, task))
        {
            task(content);
            task_map_.erase(content.call_id);
            std::cout << "map size: " << task_map_.size() << std::endl;
        }
    }

    void sync_connect()
    {
        if (try_connect())
        {
            task_map_.clear();
            async_read_head();
        }
    }

private:
    char res_head_buf_[response_header_len];
    response_header res_head_;
    std::vector<char> content_;
    threadsafe_unordered_map<std::string, task_t> task_map_;
};

}
}

<|endoftext|>


<commit_before>#include "MIDIWorker.h"

#include "rtmidi/RtMidi.h"

MIDIWorker::MIDIWorker(RtMidiIn * midiIn, QObject * parent)
	: QObject(parent)
	, m_midiIn(midiIn)
	, m_portNumber(0)
	, m_capturing(false)
{
}

MIDIWorker::~MIDIWorker()
{
}

void MIDIWorker::midiCallback(double deltatime, std::vector<unsigned char> * message, void * userData)
{
	if (userData)
	{
		MIDIWorker * worker = reinterpret_cast<MIDIWorker*>(userData);
		worker->midiCallback(deltatime, message);
	}
}

void MIDIWorker::midiCallback(double deltatime, std::vector<unsigned char> * message)
{
	QByteArray array = QByteArray::fromRawData(reinterpret_cast<const char *>(message->data()), static_cast<int>(message->size()));
	emit midiMessage(deltatime, array);
}

void MIDIWorker::setCaptureDevice(const QString & deviceName)
{
	QMutexLocker locker(&m_mutex);
	if (m_midiIn && m_deviceName != deviceName)
	{
		//check if we're currently capturing from the device and stop
		m_midiIn->closePort();
		m_midiIn->setCallback(NULL, NULL);
		emit captureStateChanged(false);
		//try to find device name in port list
		const QStringList list = inputDeviceNames();
		if (list.indexOf(deviceName) >= 0)
		{
			m_deviceName = deviceName;
			m_portNumber = list.indexOf(m_deviceName);
		}
		else
		{
			m_deviceName.clear();
			m_portNumber = 0;
		}
		emit captureDeviceChanged(m_deviceName);
	}
}

QString MIDIWorker::captureDevice() const
{
	QMutexLocker locker(&m_mutex);
	return m_deviceName;
}

void MIDIWorker::setCaptureState(bool capture)
{
	QMutexLocker locker(&m_mutex);
	if (m_midiIn && m_midiIn->isPortOpen() != capture)
	{
		if (capture)
		{
			//check for a valid port number
			if (!m_deviceName.isEmpty() && m_midiIn->getPortCount() > m_portNumber)
			{
				//try to open port and check if it worked
				m_midiIn->openPort(m_portNumber);
				if (m_midiIn->isPortOpen())
				{
					m_midiIn->setCallback(&MIDIWorker::midiCallback, this);
					emit captureStateChanged(true);
				}
			}
		}
		else
		{
			//try to close port and check if it worked
			m_midiIn->closePort();
			m_midiIn->setCallback(NULL, NULL);
			emit captureStateChanged(false);
		}
	}
}

bool MIDIWorker::isCapturing() const
{
	QMutexLocker locker(&m_mutex);
	return (m_midiIn && m_midiIn->isPortOpen());
}

QStringList MIDIWorker::inputDeviceNames() const
{
	QStringList names;
	unsigned int nPorts = m_midiIn->getPortCount();
	for (unsigned int i = 0; i < nPorts; i++)
	{
		try
		{
			QString portName = QString::fromStdString(m_midiIn->getPortName(i));
			names.append(portName);
		}
		catch (RtMidiError & /*error*/)
		{
		}
	}
	return names;
}

QString MIDIWorker::defaultInputDeviceName() const
{
	unsigned int nPorts = m_midiIn->getPortCount();
	if (nPorts > 0)
	{
		try
		{
			return QString::fromStdString(m_midiIn->getPortName(0));
		}
		catch (RtMidiError & /*error*/)
		{
		}
	}
	return QString();
}
<commit_msg>Fix crash when MIDI device is in use<commit_after>#include "MIDIWorker.h"

#include "rtmidi/RtMidi.h"

MIDIWorker::MIDIWorker(RtMidiIn * midiIn, QObject * parent)
	: QObject(parent)
	, m_midiIn(midiIn)
	, m_portNumber(0)
	, m_capturing(false)
{
}

MIDIWorker::~MIDIWorker()
{
}

void MIDIWorker::midiCallback(double deltatime, std::vector<unsigned char> * message, void * userData)
{
	if (userData)
	{
		MIDIWorker * worker = reinterpret_cast<MIDIWorker*>(userData);
		worker->midiCallback(deltatime, message);
	}
}

void MIDIWorker::midiCallback(double deltatime, std::vector<unsigned char> * message)
{
	QByteArray array = QByteArray::fromRawData(reinterpret_cast<const char *>(message->data()), static_cast<int>(message->size()));
	emit midiMessage(deltatime, array);
}

void MIDIWorker::setCaptureDevice(const QString & deviceName)
{
	QMutexLocker locker(&m_mutex);
	if (m_midiIn && m_deviceName != deviceName)
	{
		//check if we're currently capturing from the device and stop
		m_midiIn->closePort();
		m_midiIn->setCallback(NULL, NULL);
		emit captureStateChanged(false);
		//try to find device name in port list
		const QStringList list = inputDeviceNames();
		if (list.indexOf(deviceName) >= 0)
		{
			m_deviceName = deviceName;
			m_portNumber = list.indexOf(m_deviceName);
		}
		else
		{
			m_deviceName.clear();
			m_portNumber = 0;
		}
		emit captureDeviceChanged(m_deviceName);
	}
}

QString MIDIWorker::captureDevice() const
{
	QMutexLocker locker(&m_mutex);
	return m_deviceName;
}

void MIDIWorker::setCaptureState(bool capture)
{
	QMutexLocker locker(&m_mutex);
	if (m_midiIn && m_midiIn->isPortOpen() != capture)
	{
		if (capture)
		{
			//check for a valid port number
			if (!m_deviceName.isEmpty() && m_midiIn->getPortCount() > m_portNumber)
			{
				//try to open port and check if it worked
				try {
					m_midiIn->openPort(m_portNumber);
					if (m_midiIn->isPortOpen())
					{
						m_midiIn->setCallback(&MIDIWorker::midiCallback, this);
						emit captureStateChanged(true);
					}
				}
				catch (RtMidiError rme)
				{
					m_midiIn->closePort();
					m_midiIn->setCallback(NULL, NULL);
					emit captureStateChanged(false);
				}
			}
		}
		else
		{
			//try to close port and check if it worked
			m_midiIn->closePort();
			m_midiIn->setCallback(NULL, NULL);
			emit captureStateChanged(false);
		}
	}
}

bool MIDIWorker::isCapturing() const
{
	QMutexLocker locker(&m_mutex);
	return (m_midiIn && m_midiIn->isPortOpen());
}

QStringList MIDIWorker::inputDeviceNames() const
{
	QStringList names;
	unsigned int nPorts = m_midiIn->getPortCount();
	for (unsigned int i = 0; i < nPorts; i++)
	{
		try
		{
			QString portName = QString::fromStdString(m_midiIn->getPortName(i));
			names.append(portName);
		}
		catch (RtMidiError & /*error*/)
		{
		}
	}
	return names;
}

QString MIDIWorker::defaultInputDeviceName() const
{
	unsigned int nPorts = m_midiIn->getPortCount();
	if (nPorts > 0)
	{
		try
		{
			return QString::fromStdString(m_midiIn->getPortName(0));
		}
		catch (RtMidiError & /*error*/)
		{
		}
	}
	return QString();
}
<|endoftext|>


<commit_before>#ifndef __RATING_H__
#define __RATING_H__

#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>

#include "elements.hpp"
#include "model.hpp"

class RatingMatrix {
  public:
    int                   n_users, n_items;
    std::vector<rating>   ratings;
    std::vector<int>      idx;

    int                   ndcg_k = 0;
    bool                  is_dcg_max_computed = false;
    std::vector<double>   dcg_max;

    void compute_dcgmax(int);
    double compute_user_ndcg(int, const std::vector<double>&) const;

    RatingMatrix() : n_users(0), n_items(0) {}
    RatingMatrix(int nu, int ni): n_users(nu), n_items(ni) {}

    void read_lsvm(const std::string&);
    void read_spformat(const std::string&);
};

std::ifstream::pos_type filesize(const char* filename)
{
    std::ifstream in(filename, std::ios::binary | std::ios::ate);
    return in.tellg();
}

double RatingMatrix::compute_user_ndcg(int uid, const std::vector<double>& score) const {
  std::vector<std::pair<double,int> > ranking;
  for(int j=0; j<score.size(); ++j) ranking.push_back(std::pair<double,int>(score[j],0));

  double min_score = ranking[0].first;
  for(int j=0; j<ranking.size(); ++j) min_score = std::min(min_score, ranking[j].first);

  double dcg = 0.;
  for(int k=1; k<=ndcg_k; ++k) {
    int topk_idx = -1;
    double max_score = min_score - 1.;
    for(int j=0; j<ranking.size(); ++j) {
      if ((ranking[j].second == 0) && (ranking[j].first > max_score)) {
        max_score = ranking[j].first;
        topk_idx = j;
      }
    }
    ranking[topk_idx].second = k;

    dcg += (double)(pow(2,ratings[idx[uid]+topk_idx].score) - 1) / log2((double)(k+1));
  }

  return dcg / dcg_max[uid];
}

void RatingMatrix::read_lsvm(const std::string& filename) {

  std::cout << "Reading " << filename << ".." << std::endl;

  ratings.clear();
  idx.clear();

  n_users = 0;
  n_items = 0;

  // Read ratings file for NDCG
  std::vector<rating> ratings_current_user(0);
  std::string user_str, attribute_str;
  std::stringstream attribute_sstr;

  std::ifstream f;

  f.open(filename);
  if (f.is_open()) {
    int    uid = 0, iid;
    double sc;
    idx.push_back(0);

    while(1) {
      getline(f, user_str);

      size_t pos1 = 0, pos2;
      while(1) {
        pos2 = user_str.find(':', pos1); if (pos2 == std::string::npos) break;
        attribute_str = user_str.substr(pos1, pos2-pos1);
        attribute_sstr.clear(); attribute_sstr.str(attribute_str);
        attribute_sstr >> iid;
        n_items = std::max(n_items, iid);
        --iid;
        pos1 = pos2+1;

        pos2 = user_str.find(' ', pos1); attribute_str = user_str.substr(pos1, pos2-pos1);
        attribute_sstr.clear(); attribute_sstr.str(attribute_str);
        attribute_sstr >> sc;
        pos1 = pos2+1;

        ratings_current_user.push_back(rating(uid, iid, sc));
      }
      if (ratings_current_user.size() == 0) break;

      sort(ratings_current_user.begin(), ratings_current_user.end(), rating_userwise);

      for(int j=0; j<ratings_current_user.size(); ++j) ratings.push_back(ratings_current_user[j]);
      idx.push_back(ratings.size());

      ratings_current_user.clear();

      ++uid;
    }

    n_users = uid;

    printf("Read dataset with %d users, %d items \n", n_users, n_items);

  } else {
      printf("Error in opening the extracted rating file!\n");
      std::cout << filename << std::endl;
      exit(EXIT_FAILURE);
  }

  f.close();
}

void RatingMatrix::compute_dcgmax(int ndcgK) {

  ndcg_k = ndcgK;

  std::vector<rating> ratings_current_user(0);

  dcg_max.resize(n_users, 0.);
  for(int uid=0; uid<n_users; ++uid) {
    for(int i=idx[uid]; i<idx[uid+1]; ++i) ratings_current_user.push_back(ratings[i]);

    sort(ratings_current_user.begin(), ratings_current_user.end(), rating_scorewise);

    for(int k=0; k<ndcg_k; ++k) dcg_max[uid] += (double)(pow(2,ratings_current_user[k].score) - 1.) / log2(k+1);

    ratings_current_user.clear();
  }

  is_dcg_max_computed = true;
}

void RatingMatrix::read_spformat(const std::string& filename) {
  // read sparse matrix format
}

#endif
<commit_msg>to recover compute_ndcg<commit_after>#ifndef __RATING_H__
#define __RATING_H__

#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>

#include "elements.hpp"
#include "model.hpp"

class RatingMatrix {
  public:
    int                   n_users, n_items;
    std::vector<rating>   ratings;
    std::vector<int>      idx;

    int                   ndcg_k = 0;
    bool                  is_dcg_max_computed = false;
    std::vector<double>   dcg_max;

    void compute_dcgmax(int);
    double compute_user_ndcg(int, const std::vector<double>&) const;

    RatingMatrix() : n_users(0), n_items(0) {}
    RatingMatrix(int nu, int ni): n_users(nu), n_items(ni) {}

    void read_lsvm(const std::string&);
    void read_spformat(const std::string&);
};

std::ifstream::pos_type filesize(const char* filename)
{
    std::ifstream in(filename, std::ios::binary | std::ios::ate);
    return in.tellg();
}

double RatingMatrix::compute_user_ndcg(int uid, const std::vector<double>& score) const {
  std::vector<std::pair<double,int> > ranking;
  for(int j=0; j<score.size(); ++j) ranking.push_back(std::pair<double,int>(score[j],0));

  double min_score = ranking[0].first;
  for(int j=0; j<ranking.size(); ++j) min_score = std::min(min_score, ranking[j].first);

  double dcg = 0.;
  for(int k=1; k<=ndcg_k; ++k) {
    int topk_idx = -1;
    double max_score = min_score - 1.;
    for(int j=0; j<ranking.size(); ++j) {
      if ((ranking[j].second == 0) && (ranking[j].first > max_score)) {
        max_score = ranking[j].first;
        topk_idx = j;
      }
    }
    ranking[topk_idx].second = k;

    dcg += (double)(pow(2,ratings[idx[uid]+topk_idx].score) - 1) / log2((double)(k+1));
  }

  return dcg / dcg_max[uid];
}

void RatingMatrix::read_lsvm(const std::string& filename) {

  std::cout << "Reading " << filename << ".." << std::endl;

  ratings.clear();
  idx.clear();

  n_users = 0;
  n_items = 0;

  // Read ratings file for NDCG
  std::vector<rating> ratings_current_user(0);
  std::string user_str, attribute_str;
  std::stringstream attribute_sstr;

  std::ifstream f;

  f.open(filename);
  if (f.is_open()) {
    int    uid = 0, iid;
    double sc;
    idx.push_back(0);

    while(1) {
      getline(f, user_str);

      size_t pos1 = 0, pos2;
      while(1) {
        pos2 = user_str.find(':', pos1); if (pos2 == std::string::npos) break;
        attribute_str = user_str.substr(pos1, pos2-pos1);
        attribute_sstr.clear(); attribute_sstr.str(attribute_str);
        attribute_sstr >> iid;
        n_items = std::max(n_items, iid);
        --iid;
        pos1 = pos2+1;

        pos2 = user_str.find(' ', pos1); attribute_str = user_str.substr(pos1, pos2-pos1);
        attribute_sstr.clear(); attribute_sstr.str(attribute_str);
        attribute_sstr >> sc;
        pos1 = pos2+1;

        ratings_current_user.push_back(rating(uid, iid, sc));
      }
      if (ratings_current_user.size() == 0) break;

      sort(ratings_current_user.begin(), ratings_current_user.end(), rating_userwise);

      for(int j=0; j<ratings_current_user.size(); ++j) ratings.push_back(ratings_current_user[j]);
      idx.push_back(ratings.size());

      ratings_current_user.clear();

      ++uid;
    }

    n_users = uid;

    printf("Read dataset with %d users, %d items \n", n_users, n_items);

  } else {
      printf("Error in opening the extracted rating file!\n");
      std::cout << filename << std::endl;
      exit(EXIT_FAILURE);
  }

  f.close();
}

void RatingMatrix::compute_dcgmax(int ndcgK) {

  ndcg_k = ndcgK;

  std::vector<rating> ratings_current_user(0);

  dcg_max.resize(n_users, 0.);
  for(int uid=0; uid<n_users; ++uid) {
    for(int i=idx[uid]; i<idx[uid+1]; ++i) ratings_current_user.push_back(ratings[i]);

    sort(ratings_current_user.begin(), ratings_current_user.end(), rating_scorewise);

    for(int k=1; k<=ndcg_k; ++k) dcg_max[uid] += (double)(pow(2,ratings_current_user[k-1].score) - 1.) / log2(k+1);

    ratings_current_user.clear();
  }

  is_dcg_max_computed = true;
}

void RatingMatrix::read_spformat(const std::string& filename) {
  // read sparse matrix format
}

#endif
<|endoftext|>


<commit_before>#include <cstdlib>
#include <iostream>
#include <fstream>
#include <vector>
#include <iterator>
#include <cmath>
#include <iomanip>

using namespace std;

int line_len = 100;
vector<float> get_data(char *filename)
{
    ifstream file( filename, ios::binary );
    if( !file.is_open()) {
        cout << "file \"" << filename << "\" not open" << endl;
        exit(0);
    }
    file.seekg(0, ios::end );
    int num_bytes = file.tellg();
    cout << num_bytes << " = num_bytes( " << filename << " )" << endl;
    if(num_bytes % sizeof(float) != 0) {
        cout << "\nwarning, file does not evenly break into floats" << endl;
    }
    vector<float> buffer(num_bytes/sizeof(float));

    file.seekg( 0, ios::beg);
    file.read(reinterpret_cast<char *>(buffer.data()), num_bytes);

    return buffer;
}

void print_line(int N) {
    for(int i = 0; i < N; i++)
        cout << "-";
    cout << endl;
}

void print_header(){
    print_line(line_len);
    cout << "| " << left << setw(10) << "tolerance";
    cout << "| " << setw(20 + 3) << "# non-equal elements" ;
    cout << "| " << setw(13) << "sum of diffs";
    cout << "| " << setw(12) << "avg diff";
    cout << "| " << setw(12) << "max diff" ;
    cout << endl;
    print_line(line_len);
}
void analyze(vector<float> buff1, vector<float> buff2, float tolerance)
{
    //vector<float> buff3(buff1.size());
    int num_different = 0;
    float sum_difference = 0;
    float biggest_percent = 0;
    int biggest_diff_idx = -1;

    for(int i = 0; i < buff1.size(); i++) {
        float val = buff2[i] - buff1[i];
        sum_difference += val;
        if(val > tolerance) {
            num_different++;
        }
        if(val > tolerance && buff1[i] > tolerance) {
            float percent_diff = 100.0 *(val / buff1[i]);
            if(percent_diff > biggest_percent) {
                biggest_percent = percent_diff;
                biggest_diff_idx = i;
            }
        }
    }
    float avg_diff;
    if(num_different == 0) {
        avg_diff = 0;
    } else {
        avg_diff = (sum_difference / (float)num_different);
    }
    cout << "| " << setw(10) << tolerance
         << "| " << setw(10) << right << num_different << " / " << left << setw(10) << buff1.size()
         << "| " << setw(13) << sum_difference
         << "| " << setw(12) << avg_diff
         << "| " << biggest_percent << "%";
    if(biggest_diff_idx > 0) {
        cout << " ( " << buff1[biggest_diff_idx] << " vs " << buff2[biggest_diff_idx] << " )";
    }
    cout << endl;
}

int main( int argc, char **argv )
{
    float tolerance = -6;
    if( argc < 3 ) {
        cout << "requires 2 files names" << endl;
        return 0;
    }
    if( argc > 3) {
        tolerance = atoi(argv[3]);
    }

    vector<float> buff1 = get_data(argv[1]);
    vector<float> buff2 = get_data(argv[2]);

    if(buff1.size() != buff2.size()) {
        cout << "error, files are different size: "
             << buff1.size() << " for file1 vs "
             << buff2.size() << " for file2" << endl;
        return 0;
    }

    print_header();
    for(int tol = tolerance-3; tol < tolerance+4; tol++) {
        analyze(buff1, buff2, pow(10.0, tol));
    }
    print_line(line_len);
    return 0;
}
<commit_msg>shortened the output again<commit_after>#include <cstdlib>
#include <iostream>
#include <fstream>
#include <vector>
#include <iterator>
#include <cmath>
#include <iomanip>

using namespace std;

int line_len = 100;
vector<float> get_data(char *filename)
{
    ifstream file( filename, ios::binary );
    if( !file.is_open()) {
        cout << "file \"" << filename << "\" not open" << endl;
        exit(0);
    }
    file.seekg(0, ios::end );
    int num_bytes = file.tellg();
    //cout << num_bytes << " = num_bytes( " << filename << " )" << endl;
    if(num_bytes % sizeof(float) != 0) {
        cout << "\nwarning, file does not evenly break into floats" << endl;
    }
    vector<float> buffer(num_bytes/sizeof(float));

    file.seekg( 0, ios::beg);
    file.read(reinterpret_cast<char *>(buffer.data()), num_bytes);

    return buffer;
}

void print_line(int N) {
    for(int i = 0; i < N; i++)
        cout << "-";
    cout << endl;
}

void print_header(){
    print_line(line_len);
    cout << "| " << left << setw(10) << "tolerance";
    cout << "| " << setw(20 + 3) << "# non-equal elements" ;
    cout << "| " << setw(13) << "sum of diffs";
    cout << "| " << setw(12) << "avg diff";
    cout << "| " << setw(12) << "max diff" ;
    cout << endl;
    print_line(line_len);
}
void analyze(vector<float> buff1, vector<float> buff2, float tolerance)
{
    //vector<float> buff3(buff1.size());
    int num_different = 0;
    float sum_difference = 0;
    float biggest_percent = 0;
    int biggest_diff_idx = -1;

    for(int i = 0; i < buff1.size(); i++) {
        float val = buff2[i] - buff1[i];
        sum_difference += val;
        if(val > tolerance) {
            num_different++;
        }
        if(val > tolerance && buff1[i] > tolerance) {
            float percent_diff = 100.0 *(val / buff1[i]);
            if(percent_diff > biggest_percent) {
                biggest_percent = percent_diff;
                biggest_diff_idx = i;
            }
        }
    }
    float avg_diff;
    if(num_different == 0) {
        avg_diff = 0;
    } else {
        avg_diff = (sum_difference / (float)num_different);
    }
    cout << "| " << setw(10) << tolerance
         << "| " << setw(10) << right << num_different << " / " << left << setw(10) << buff1.size()
         << "| " << setw(13) << sum_difference
         << "| " << setw(12) << avg_diff
         << "| " << biggest_percent << "%";
    if(biggest_diff_idx > 0) {
        cout << " ( " << buff1[biggest_diff_idx] << " vs " << buff2[biggest_diff_idx] << " )";
    }
    cout << endl;
}

int main( int argc, char **argv )
{
    float tolerance = -6;
    if( argc < 3 ) {
        cout << "requires 2 files names" << endl;
        return 0;
    }
    if( argc > 3) {
        tolerance = atoi(argv[3]);
    }

    vector<float> buff1 = get_data(argv[1]);
    vector<float> buff2 = get_data(argv[2]);

    if(buff1.size() != buff2.size()) {
        cout << "error, files are different size: "
             << buff1.size() << " for file1 vs "
             << buff2.size() << " for file2" << endl;
        return 0;
    }

    print_header();
    for(int tol = tolerance-3; tol < tolerance+4; tol++) {
        analyze(buff1, buff2, pow(10.0, tol));
    }
    return 0;
}
<|endoftext|>


<commit_before>#include "Moves/Move.h"

#include <cassert>
#include <cmath>
#include <cctype>
#include <string>

#include "Game/Board.h"
#include "Game/Square.h"
#include "Game/Game_Result.h"
#include "Game/Piece.h"

Move::Move(Square start, Square end) noexcept :
               origin(start),
               destination(end)
{
    assert(start.inside_board());
    assert(end.inside_board());
    assert(file_change() != 0 || rank_change() != 0);
}

void Move::side_effects(Board&) const noexcept
{
}

bool Move::is_legal(const Board& board) const noexcept
{
#ifndef NDEBUG
    auto moving_piece = board.piece_on_square(start());
#endif
    assert(moving_piece);
    assert(moving_piece.color() == board.whose_turn());
    assert(moving_piece.can_move(this));

    auto attacked_piece = board.piece_on_square(end());
    if(attacked_piece)
    {
        if( ! can_capture() || board.whose_turn() == attacked_piece.color())
        {
            return false;
        }
    }

    return move_specific_legal(board) && ! board.king_is_in_check_after_move(*this);
}

bool Move::move_specific_legal(const Board&) const noexcept
{
    return true;
}

bool Move::can_capture() const noexcept
{
    return able_to_capture;
}

Square Move::start() const noexcept
{
    return origin;
}

Square Move::end() const noexcept
{
    return destination;
}

Square_Difference Move::movement() const noexcept
{
    return end() - start();
}

int Move::file_change() const noexcept
{
    return end().file() - start().file();
}

int Move::rank_change() const noexcept
{
    return end().rank() - start().rank();
}

std::string Move::game_record_item(const Board& board) const noexcept
{
    return game_record_move_item(board) + game_record_ending_item(board);
}

std::string Move::game_record_move_item(const Board& board) const noexcept
{
    auto original_piece = board.piece_on_square(start());
    std::string move_record = original_piece.pgn_symbol();

    auto record_file = false;
    auto record_rank = false;
    for(auto other_square : Square::all_squares())
    {
        auto new_piece = board.piece_on_square(other_square);
        if(original_piece == new_piece && board.is_legal(other_square, end()))
        {
            if(other_square.file() != start().file() && ! record_file)
            {
                record_file = true;
            }
            else if(other_square.rank() != start().rank())
            {
                record_rank = true;
            }
        }
    }

    if(record_file)
    {
        move_record += start().file();
    }

    if(record_rank)
    {
        move_record += std::to_string(start().rank());
    }

    if(board.piece_on_square(end()))
    {
        move_record += 'x';
    }

    move_record += end().string();
    return move_record;
}

std::string Move::game_record_ending_item(Board board) const noexcept
{
    auto result = board.submit_move(*this);
    if(board.king_is_in_check())
    {
        if(result.winner() == NONE)
        {
            return "+";
        }
        else
        {
            return "#";
        }
    }
    else
    {
        return {};
    }
}

std::string Move::coordinate_move() const noexcept
{
    auto result = start().string() + end().string();

    if(promotion_piece_symbol())
    {
        result += char(std::tolower(promotion_piece_symbol()));
    }

    return result;
}

bool Move::is_en_passant() const noexcept
{
    return is_en_passant_move;
}

char Move::promotion_piece_symbol() const noexcept
{
    return '\0';
}

void Move::adjust_end_file(int adjust) noexcept
{
    destination += Square_Difference{adjust, 0};
}

void Move::adjust_end_rank(int adjust) noexcept
{
    destination += Square_Difference{0, adjust};
}

size_t Move::attack_index() const noexcept
{
    return attack_index(movement());
}

size_t Move::attack_index(const Square_Difference& move) noexcept
{
    size_t result = 0;

    // First bit == knight move or not
    if(std::abs(move.rank_change*move.file_change) == 2) // 1x2 or 2x1
    {
        result += 8;
        result += 4*(move.file_change > 0);
        result += 2*(move.rank_change > 0);
        result += 1*(std::abs(move.file_change) > std::abs(move.rank_change));
    }
    else
    {
        // Second bit == rook move or not
        if(move.rank_change == 0 || move.file_change == 0)
        {
            result += 4;
            result += 2*(move.rank_change != 0);
            result += 1*(move.rank_change + move.file_change > 0);
        }
        else
        {
            // Bishop moves
            result += 2*(move.file_change > 0);
            result += 1*(move.rank_change > 0);
        }
    }

    return result;
}

Square_Difference Move::attack_direction_from_index(size_t index) noexcept
{
    if(index & 8)
    {
        // Knight move
        auto file_direction = (index & 4) ? 1 : -1;
        auto rank_direction = (index & 2) ? 1 : -1;
        auto file_step = (index & 1) ? 2 : 1;
        auto rank_step = 3 - file_step;
        return {file_step*file_direction, rank_step*rank_direction};
    }
    else
    {
        if(index & 4)
        {
            // Rook move
            if(index & 2)
            {
                // Vertical move
                return {0, (index & 1) ? 1 : -1};
            }
            else
            {
                // Horizontal move
                return {(index & 1) ? 1 : -1, 0};
            }
        }
        else
        {
            // Bishop move
            return {(index & 2) ? 1 : -1, (index & 1) ? 1 : -1};
        }
    }
}

void Move::disable_capturing() noexcept
{
    able_to_capture = false;
}

void Move::enable_capturing() noexcept
{
    able_to_capture = true;
}

void Move::mark_as_en_passant() noexcept
{
    is_en_passant_move = true;
}
<commit_msg>Reduce scope of variable<commit_after>#include "Moves/Move.h"

#include <cassert>
#include <cmath>
#include <cctype>
#include <string>

#include "Game/Board.h"
#include "Game/Square.h"
#include "Game/Game_Result.h"
#include "Game/Piece.h"

Move::Move(Square start, Square end) noexcept :
               origin(start),
               destination(end)
{
    assert(start.inside_board());
    assert(end.inside_board());
    assert(file_change() != 0 || rank_change() != 0);
}

void Move::side_effects(Board&) const noexcept
{
}

bool Move::is_legal(const Board& board) const noexcept
{
#ifndef NDEBUG
    auto moving_piece = board.piece_on_square(start());
#endif
    assert(moving_piece);
    assert(moving_piece.color() == board.whose_turn());
    assert(moving_piece.can_move(this));

    if(auto attacked_piece = board.piece_on_square(end()))
    {
        if( ! can_capture() || board.whose_turn() == attacked_piece.color())
        {
            return false;
        }
    }

    return move_specific_legal(board) && ! board.king_is_in_check_after_move(*this);
}

bool Move::move_specific_legal(const Board&) const noexcept
{
    return true;
}

bool Move::can_capture() const noexcept
{
    return able_to_capture;
}

Square Move::start() const noexcept
{
    return origin;
}

Square Move::end() const noexcept
{
    return destination;
}

Square_Difference Move::movement() const noexcept
{
    return end() - start();
}

int Move::file_change() const noexcept
{
    return end().file() - start().file();
}

int Move::rank_change() const noexcept
{
    return end().rank() - start().rank();
}

std::string Move::game_record_item(const Board& board) const noexcept
{
    return game_record_move_item(board) + game_record_ending_item(board);
}

std::string Move::game_record_move_item(const Board& board) const noexcept
{
    auto original_piece = board.piece_on_square(start());
    std::string move_record = original_piece.pgn_symbol();

    auto record_file = false;
    auto record_rank = false;
    for(auto other_square : Square::all_squares())
    {
        auto new_piece = board.piece_on_square(other_square);
        if(original_piece == new_piece && board.is_legal(other_square, end()))
        {
            if(other_square.file() != start().file() && ! record_file)
            {
                record_file = true;
            }
            else if(other_square.rank() != start().rank())
            {
                record_rank = true;
            }
        }
    }

    if(record_file)
    {
        move_record += start().file();
    }

    if(record_rank)
    {
        move_record += std::to_string(start().rank());
    }

    if(board.piece_on_square(end()))
    {
        move_record += 'x';
    }

    move_record += end().string();
    return move_record;
}

std::string Move::game_record_ending_item(Board board) const noexcept
{
    auto result = board.submit_move(*this);
    if(board.king_is_in_check())
    {
        if(result.winner() == NONE)
        {
            return "+";
        }
        else
        {
            return "#";
        }
    }
    else
    {
        return {};
    }
}

std::string Move::coordinate_move() const noexcept
{
    auto result = start().string() + end().string();

    if(promotion_piece_symbol())
    {
        result += char(std::tolower(promotion_piece_symbol()));
    }

    return result;
}

bool Move::is_en_passant() const noexcept
{
    return is_en_passant_move;
}

char Move::promotion_piece_symbol() const noexcept
{
    return '\0';
}

void Move::adjust_end_file(int adjust) noexcept
{
    destination += Square_Difference{adjust, 0};
}

void Move::adjust_end_rank(int adjust) noexcept
{
    destination += Square_Difference{0, adjust};
}

size_t Move::attack_index() const noexcept
{
    return attack_index(movement());
}

size_t Move::attack_index(const Square_Difference& move) noexcept
{
    size_t result = 0;

    // First bit == knight move or not
    if(std::abs(move.rank_change*move.file_change) == 2) // 1x2 or 2x1
    {
        result += 8;
        result += 4*(move.file_change > 0);
        result += 2*(move.rank_change > 0);
        result += 1*(std::abs(move.file_change) > std::abs(move.rank_change));
    }
    else
    {
        // Second bit == rook move or not
        if(move.rank_change == 0 || move.file_change == 0)
        {
            result += 4;
            result += 2*(move.rank_change != 0);
            result += 1*(move.rank_change + move.file_change > 0);
        }
        else
        {
            // Bishop moves
            result += 2*(move.file_change > 0);
            result += 1*(move.rank_change > 0);
        }
    }

    return result;
}

Square_Difference Move::attack_direction_from_index(size_t index) noexcept
{
    if(index & 8)
    {
        // Knight move
        auto file_direction = (index & 4) ? 1 : -1;
        auto rank_direction = (index & 2) ? 1 : -1;
        auto file_step = (index & 1) ? 2 : 1;
        auto rank_step = 3 - file_step;
        return {file_step*file_direction, rank_step*rank_direction};
    }
    else
    {
        if(index & 4)
        {
            // Rook move
            if(index & 2)
            {
                // Vertical move
                return {0, (index & 1) ? 1 : -1};
            }
            else
            {
                // Horizontal move
                return {(index & 1) ? 1 : -1, 0};
            }
        }
        else
        {
            // Bishop move
            return {(index & 2) ? 1 : -1, (index & 1) ? 1 : -1};
        }
    }
}

void Move::disable_capturing() noexcept
{
    able_to_capture = false;
}

void Move::enable_capturing() noexcept
{
    able_to_capture = true;
}

void Move::mark_as_en_passant() noexcept
{
    is_en_passant_move = true;
}
<|endoftext|>


<commit_before>#include "Moves/Move.h"

#include <cassert>

#include "Game/Board.h"
#include "Game/Game_Result.h"
#include "Game/Piece.h"
#include "Utility.h"

Move::Move(char file_start, int rank_start,
           char file_end,   int rank_end) :
               starting_file(file_start),
               starting_rank(rank_start),
               ending_file(file_end),
               ending_rank(rank_end),
               able_to_capture(true),
               is_en_passant_move(false),
               is_castling_move(false)
{
    assert(std::abs(file_change()) < 8);
    assert(std::abs(rank_change()) < 8);
    assert(file_change() != 0 || rank_change() != 0);
}

void Move::side_effects(Board&) const
{
}

bool Move::is_legal(const Board& board) const
{
    assert(Board::inside_board(starting_file, starting_rank));
    assert(Board::inside_board(ending_file, ending_rank));

#ifndef NDEBUG
    auto moving_piece = board.piece_on_square(starting_file, starting_rank);
#endif
    assert(moving_piece);
    assert(moving_piece->color() == board.whose_turn());
    assert(moving_piece->can_move(this));

    auto attacked_piece = board.piece_on_square(ending_file, ending_rank);
    if(attacked_piece)
    {
        if(board.whose_turn() == attacked_piece->color())
        {
            return false;
        }

        if( ! can_capture())
        {
            return false;
        }
    }

    if( ! move_specific_legal(board))
    {
        return false;
    }

    return ! board.king_is_in_check_after_move(*this);
}

bool Move::move_specific_legal(const Board&) const
{
    return true;
}

bool Move::can_capture() const
{
    return able_to_capture;
}

char Move::start_file() const
{
    return starting_file;
}

int Move::start_rank() const
{
    return starting_rank;
}

int Move::file_change() const
{
    return ending_file - starting_file;
}

int Move::rank_change() const
{
    return ending_rank - starting_rank;
}

char Move::end_file() const
{
    return ending_file;
}

int Move::end_rank() const
{
    return ending_rank;
}

std::string Move::game_record_item(const Board& board) const
{
    return game_record_move_item(board) + game_record_ending_item(board);
}

std::string Move::game_record_move_item(const Board& board) const
{
    auto original_piece = board.piece_on_square(starting_file, starting_rank);
    std::string move_record = original_piece->pgn_symbol();

    bool record_file = false;
    bool record_rank = false;
    for(char file_other = 'a'; file_other <= 'h'; ++file_other)
    {
        for(int rank_other = 1; rank_other <= 8; ++rank_other)
        {
            if( ! board.piece_on_square(file_other, rank_other))
            {
                continue;
            }
            if(file_other == starting_file && rank_other == starting_rank)
            {
                continue;
            }
            if(file_other == ending_file && rank_other == ending_rank)
            {
                continue;
            }
            auto new_piece = board.piece_on_square(file_other, rank_other);
            if(original_piece != new_piece)
            {
                continue;
            }

            if(board.is_legal(file_other, rank_other, ending_file, ending_rank))
            {
                if(file_other != starting_file && ! record_file)
                {
                    record_file = true;
                    continue;
                }
                if(rank_other != starting_rank)
                {
                    record_rank = true;
                }
            }
        }
    }

    if(record_file)
    {
        move_record += starting_file;
    }
    if(record_rank)
    {
        move_record += std::to_string(starting_rank);
    }

    if(board.piece_on_square(ending_file, ending_rank))
    {
        move_record += 'x';
    }

    move_record += ending_file + std::to_string(ending_rank);

    return move_record;
}

std::string Move::game_record_ending_item(Board board) const
{
    auto result = board.submit_move(*this);
    std::string appendage;

    if(board.king_is_in_check() && ! result.game_has_ended())
    {
        appendage.push_back('+');
    }

    return appendage + result.game_record_annotation();
}

std::string Move::coordinate_move() const
{
    return starting_file
           + std::to_string(starting_rank)
           + ending_file
           + std::to_string(ending_rank);
}

bool Move::is_en_passant() const
{
    return is_en_passant_move;
}

bool Move::is_castling() const
{
    return is_castling_move;
}

char Move::promotion_piece_symbol() const
{
    return '\0';
}
<commit_msg>Fix move annotation when game ends in stalemate with king in check<commit_after>#include "Moves/Move.h"

#include <cassert>

#include "Game/Board.h"
#include "Game/Game_Result.h"
#include "Game/Piece.h"
#include "Utility.h"

Move::Move(char file_start, int rank_start,
           char file_end,   int rank_end) :
               starting_file(file_start),
               starting_rank(rank_start),
               ending_file(file_end),
               ending_rank(rank_end),
               able_to_capture(true),
               is_en_passant_move(false),
               is_castling_move(false)
{
    assert(std::abs(file_change()) < 8);
    assert(std::abs(rank_change()) < 8);
    assert(file_change() != 0 || rank_change() != 0);
}

void Move::side_effects(Board&) const
{
}

bool Move::is_legal(const Board& board) const
{
    assert(Board::inside_board(starting_file, starting_rank));
    assert(Board::inside_board(ending_file, ending_rank));

#ifndef NDEBUG
    auto moving_piece = board.piece_on_square(starting_file, starting_rank);
#endif
    assert(moving_piece);
    assert(moving_piece->color() == board.whose_turn());
    assert(moving_piece->can_move(this));

    auto attacked_piece = board.piece_on_square(ending_file, ending_rank);
    if(attacked_piece)
    {
        if(board.whose_turn() == attacked_piece->color())
        {
            return false;
        }

        if( ! can_capture())
        {
            return false;
        }
    }

    if( ! move_specific_legal(board))
    {
        return false;
    }

    return ! board.king_is_in_check_after_move(*this);
}

bool Move::move_specific_legal(const Board&) const
{
    return true;
}

bool Move::can_capture() const
{
    return able_to_capture;
}

char Move::start_file() const
{
    return starting_file;
}

int Move::start_rank() const
{
    return starting_rank;
}

int Move::file_change() const
{
    return ending_file - starting_file;
}

int Move::rank_change() const
{
    return ending_rank - starting_rank;
}

char Move::end_file() const
{
    return ending_file;
}

int Move::end_rank() const
{
    return ending_rank;
}

std::string Move::game_record_item(const Board& board) const
{
    return game_record_move_item(board) + game_record_ending_item(board);
}

std::string Move::game_record_move_item(const Board& board) const
{
    auto original_piece = board.piece_on_square(starting_file, starting_rank);
    std::string move_record = original_piece->pgn_symbol();

    bool record_file = false;
    bool record_rank = false;
    for(char file_other = 'a'; file_other <= 'h'; ++file_other)
    {
        for(int rank_other = 1; rank_other <= 8; ++rank_other)
        {
            if( ! board.piece_on_square(file_other, rank_other))
            {
                continue;
            }
            if(file_other == starting_file && rank_other == starting_rank)
            {
                continue;
            }
            if(file_other == ending_file && rank_other == ending_rank)
            {
                continue;
            }
            auto new_piece = board.piece_on_square(file_other, rank_other);
            if(original_piece != new_piece)
            {
                continue;
            }

            if(board.is_legal(file_other, rank_other, ending_file, ending_rank))
            {
                if(file_other != starting_file && ! record_file)
                {
                    record_file = true;
                    continue;
                }
                if(rank_other != starting_rank)
                {
                    record_rank = true;
                }
            }
        }
    }

    if(record_file)
    {
        move_record += starting_file;
    }
    if(record_rank)
    {
        move_record += std::to_string(starting_rank);
    }

    if(board.piece_on_square(ending_file, ending_rank))
    {
        move_record += 'x';
    }

    move_record += ending_file + std::to_string(ending_rank);

    return move_record;
}

std::string Move::game_record_ending_item(Board board) const
{
    auto result = board.submit_move(*this);
    std::string appendage;

    if(board.king_is_in_check() && result.winner() == NONE)
    {
        appendage.push_back('+');
    }

    return appendage + result.game_record_annotation();
}

std::string Move::coordinate_move() const
{
    return starting_file
           + std::to_string(starting_rank)
           + ending_file
           + std::to_string(ending_rank);
}

bool Move::is_en_passant() const
{
    return is_en_passant_move;
}

bool Move::is_castling() const
{
    return is_castling_move;
}

char Move::promotion_piece_symbol() const
{
    return '\0';
}
<|endoftext|>