Example #1
0
float sparsify_size (VectorSf & sparse, int size)
{
  ASSERT_LT(0, size);
  ASSERT_LT(size, sparse.nonZeros());

  static std::vector<SparseVectorEntry> entries;

  for (VectorSf::InnerIterator iter(sparse); iter; ++iter) {
    entries.push_back(SparseVectorEntry(iter.value(), iter.index()));
  }

  std::nth_element(entries.begin(), entries.begin() + size, entries.end());

  VectorSf sparser(sparse.size());
  sparser.reserve(size);

  for (int e = 0; e < size; ++e) {
    const SparseVectorEntry & entry = entries[e];
    sparser.insert(entry.index) = entry.value;
  }

  sparser.finalize();
  entries.clear();

  std::swap(sparse, sparser);

  return sparser.sum() - sparse.sum();
}
Example #2
0
bool PrologEngine::load(QString code)
{
    lexer.lexer.init(code);
    lexer.lexer.skipTokens.insert(Prolog::Spacing);
    lexer.lexer.tokenize();

    errors << lexer.lexer.errors;
    if(!errors.empty())
        return false;

    Prolog::Program program;
    initProgram(program);
    Prolog::PrologParser parser(lexer.lexer.acceptedTokens, program);
    parser.parse();

    errors << parser.errors;
    if(!errors.empty())
        return false;

    Prolog::PrologCompiler compiler(program);
    compiler.compile();

    errors << compiler.errors;
    if(!errors.empty())
        return false;

    SExp.lexer.init(compiler.getOutput());
    SExp.lexer.skipTokens.insert(SExp::Spacing);
    SExp.lexer.tokenize();

    errors << SExp.lexer.errors;
    if(!errors.empty())
        return false;

    SExpressionParser sparser(SExp.lexer.acceptedTokens);
    QVector<shared_ptr<SExpression> >sexps = sparser.parse();

    errors << sparser.errors;
    if(!errors.empty())
        return false;

    wam.Load(sexps);

    errors << wam.errors;
    if(!errors.empty())
        return false;

    return true;
}
Example #3
0
float sparsify_size (MatrixSf & sparse, int size)
{
  ASSERT_LT(0, size);
  ASSERT_LT(size, sparse.nonZeros());

  LOG("sparsifying " << sparse.rows() << " x " << sparse.cols()
      << " matrix from " << sparse.nonZeros() << " to " << size << " entries");

  static std::vector<SparseMatrixEntry> entries;

  for (int i = 0; i < sparse.outerSize(); ++i) {
    for (MatrixSf::InnerIterator iter(sparse,i); iter; ++iter) {
      entries.push_back(
          SparseMatrixEntry(iter.value(), iter.row(), iter.col()));
    }
  }

  std::nth_element(entries.begin(), entries.begin() + size, entries.end());

  MatrixSf sparser(sparse.rows(), sparse.cols());
  sparser.reserve(size);

  for (int e = 0; e < size; ++e) {
    const SparseMatrixEntry & entry = entries[e];
    sparser.insert(entry.row, entry.col) = entry.value;
  }

  sparser.finalize();
  entries.clear();

  std::swap(sparse, sparser);

  float old_sum = sparser.sum();
  float new_sum = sparse.sum();
  float loss = (old_sum - new_sum) / old_sum;
  float density = float(size) / sparser.nonZeros();
  LOG("sparsifying to density " << density
      << " loses " << (loss*100) << "% of mass");

  return old_sum - new_sum;
}
Example #4
0
int main(int argc, char **argv)
{
    Poco::Stopwatch sw;

    std::string dir = Poco::Environment::get("POCO_BASE") + "/JSON/samples/Benchmark/";
    Poco::Path  filePath(dir, "input.big.json");

    std::ostringstream ostr;

    if (filePath.isFile()) {
        Poco::File inputFile(filePath);

        if (inputFile.exists()) {
            sw.start();
            Poco::FileInputStream fis(filePath.toString());
            Poco::StreamCopier::copyStream(fis, ostr);
            sw.stop();
        } else {
            std::cout << filePath.toString() << " doesn't exist!" << std::endl;
            return 1;
        }
    }

    std::cout << "JSON Benchmark" << std::endl;
    std::cout << "==============" << std::endl;

    std::string jsonStr = ostr.str();
    std::cout << "Total of " << jsonStr.size() << " bytes," << std::endl << "loaded in " << sw.elapsed() << " [us]," << std::endl;

    std::cout << std::endl << "POCO JSON barebone parse" << std::endl;
    Poco::JSON::Parser sparser(0);
    sw.restart();
    sparser.parse(jsonStr);
    sw.stop();
    std::cout << "---------------------------------" << std::endl;
    std::cout << "[std::string] parsed in " << sw.elapsed() << " [us]" << std::endl;
    std::cout << "---------------------------------" << std::endl;

    Poco::JSON::Parser  iparser(0);
    std::istringstream  istr(jsonStr);
    sw.restart();
    iparser.parse(istr);
    sw.stop();
    std::cout << "----------------------------------------" << std::endl;
    std::cout << "[std::istringstream] parsed in " << sw.elapsed() << " [us]" << std::endl;
    std::cout << "----------------------------------------" << std::endl;

    std::cout << std::endl << "POCO JSON Handle/Stringify" << std::endl;
    try
    {
        Poco::JSON::Parser sparser;
        sw.restart();
        sparser.parse(jsonStr);
        Poco::DynamicAny result = sparser.result();
        sw.stop();
        std::cout << "-----------------------------------------" << std::endl;
        std::cout << "[std::string] parsed/handled in " << sw.elapsed() << " [us]" << std::endl;
        std::cout << "-----------------------------------------" << std::endl;

        Poco::JSON::Parser  isparser;
        std::istringstream  istr(jsonStr);
        sw.restart();
        isparser.parse(istr);
        result = isparser.result();
        sw.stop();
        std::cout << "------------------------------------------------" << std::endl;
        std::cout << "[std::istringstream] parsed/handled in " << sw.elapsed() << " [us]" << std::endl;
        std::cout << "------------------------------------------------" << std::endl;

        // Serialize to string
        Poco::JSON::Object::Ptr obj;

        if (result.type() == typeid(Poco::JSON::Object::Ptr)) {
            obj = result.extract <Poco::JSON::Object::Ptr> ();
        }

        std::ostringstream out;
        sw.restart();
        obj->stringify(out);
        sw.stop();
        std::cout << "-----------------------------------" << std::endl;
        std::cout << "stringified in " << sw.elapsed() << " [us]" << std::endl;
        std::cout << "-----------------------------------" << std::endl;
        std::cout << std::endl;
    }
    catch(Poco::JSON::JSONException jsone)
    {
        std::cout << jsone.message() << std::endl;
    }

    return 0;
}