<commit_before>///
/// @file  pi_deleglise_rivat_parallel1.cpp
/// @brief Parallel implementation of the Deleglise-Rivat prime
///        counting algorithm. In the Deleglise-Rivat algorithm there
///        are 3 additional types of special leaves compared to the
///        Lagarias-Miller-Odlyzko algorithm: trivial special leaves,
///        clustered easy leaves and sparse easy leaves.
///
///        This implementation is based on the paper:
///        Tomás Oliveira e Silva, Computing pi(x): the combinatorial
///        method, Revista do DETUA, vol. 4, no. 6, March 2006,
///        pp. 759-768.
///
/// Copyright (C) 2014 Kim Walisch, <kim.walisch@gmail.com>
///
/// This file is distributed under the BSD License. See the COPYING
/// file in the top level directory.
///

#include "S2.hpp"
#include <primecount-internal.hpp>
#include <aligned_vector.hpp>
#include <BitSieve.hpp>
#include <generate.hpp>
#include <pmath.hpp>
#include <PhiTiny.hpp>
#include <S2LoadBalancer.hpp>
#include <tos_counters.hpp>
#include <S1.hpp>

#include <stdint.h>
#include <algorithm>
#include <vector>

#ifdef _OPENMP
  #include <omp.h>
#endif

using namespace std;
using namespace primecount;

namespace {

/// For each prime calculate its first multiple >= low
vector<int64_t> generate_next_multiples(int64_t low, int64_t size, vector<int32_t>& primes)
{
  vector<int64_t> next;

  next.reserve(size);
  next.push_back(0);

  for (int64_t b = 1; b < size; b++)
  {
    int64_t prime = primes[b];
    int64_t next_multiple = ceil_div(low, prime) * prime;
    next_multiple += prime * (~next_multiple & 1);
    next.push_back(next_multiple);
  }

  return next;
}

template <typename T>
void cross_off(int64_t prime,
               int64_t low,
               int64_t high,
               int64_t& next_multiple,
               BitSieve& sieve,
               T& counters)
{
  int64_t segment_size = sieve.size();
  int64_t k = next_multiple;

  for (; k < high; k += prime * 2)
  {
    if (sieve[k - low])
    {
      sieve.unset(k - low);
      cnt_update(counters, k - low, segment_size);
    }
  }
  next_multiple = k;
}

/// Compute the S2 contribution of the special leaves that require
/// a sieve. Each thread processes the interval
/// [low_thread, low_thread + segments * segment_size[
/// and the missing special leaf contributions for the interval
/// [1, low_process[ are later reconstructed and added in
/// the parent S2_sieve() function.
///
int64_t S2_sieve_thread(int64_t x,
                        int64_t y,
                        int64_t z,
                        int64_t c,
                        int64_t segment_size,
                        int64_t segments_per_thread,
                        int64_t thread_num,
                        int64_t low,
                        int64_t limit,
                        vector<int32_t>& pi,
                        vector<int32_t>& primes,
                        vector<int32_t>& lpf,
                        vector<int32_t>& mu,
                        vector<int64_t>& mu_sum,
                        vector<int64_t>& phi)
{
  low += segment_size * segments_per_thread * thread_num;
  limit = min(low + segment_size * segments_per_thread, limit);
  int64_t pi_sqrty = pi[isqrt(y)];
  int64_t max_prime = min3(isqrt(x / low), isqrt(z), y);
  int64_t pi_max = pi[max_prime];
  int64_t S2_thread = 0;

  BitSieve sieve(segment_size);
  vector<int32_t> counters(segment_size);
  vector<int64_t> next = generate_next_multiples(low, pi_max + 1, primes);
  phi.resize(pi_max + 1, 0);
  mu_sum.resize(pi_max + 1, 0);

  // segmeted sieve of Eratosthenes
  for (; low < limit; low += segment_size)
  {
    // Current segment = interval [low, high[
    int64_t high = min(low + segment_size, limit);
    int64_t b = c + 1;

    // check if we need the sieve
    if (c <= pi_max)
    {
      sieve.fill(low, high);

      // phi(y, i) nodes with i <= c do not contribute to S2, so we
      // simply sieve out the multiples of the first c primes
      for (int64_t i = 2; i <= c; i++)
      {
        int64_t k = next[i];
        for (int64_t prime = primes[i]; k < high; k += prime * 2)
          sieve.unset(k - low);
        next[i] = k;
      }

      // Initialize special tree data structure from sieve
      cnt_finit(sieve, counters, segment_size);
    }

    // For c + 1 <= b <= pi_sqrty
    // Find all special leaves: n = primes[b] * m, with mu[m] != 0 and primes[b] < lpf[m]
    // which satisfy: low <= (x / n) < high
    for (int64_t end = min(pi_sqrty, pi_max); b <= end; b++)
    {
      int64_t prime = primes[b];
      int64_t min_m = max(x / (prime * high), y / prime);
      int64_t max_m = min(x / (prime * low), y);

      if (prime >= max_m)
        goto next_segment;

      for (int64_t m = max_m; m > min_m; m--)
      {
        if (mu[m] != 0 && prime < lpf[m])
        {
          int64_t n = prime * m;
          int64_t count = cnt_query(counters, (x / n) - low);
          int64_t phi_xn = phi[b] + count;
          S2_thread -= mu[m] * phi_xn;
          mu_sum[b] -= mu[m];
        }
      }

      phi[b] += cnt_query(counters, (high - 1) - low);
      cross_off(prime, low, high, next[b], sieve, counters);
    }

    // For pi_sqrty <= b <= pi_sqrtz
    // Find all hard special leaves: n = primes[b] * primes[l]
    // which satisfy: low <= (x / n) < high
    for (; b <= pi_max; b++)
    {
      int64_t prime = primes[b];
      int64_t l = pi[min3(x / (prime * low), z / prime, y)];
      int64_t min_hard_leaf = max3(x / (prime * high), y / prime, prime);

      if (prime >= primes[l])
        goto next_segment;

      for (; primes[l] > min_hard_leaf; l--)
      {
        int64_t n = prime * primes[l];
        int64_t xn = x / n;
        int64_t count = cnt_query(counters, xn - low);
        int64_t phi_xn = phi[b] + count;
        S2_thread += phi_xn;
        mu_sum[b]++;
      }

      phi[b] += cnt_query(counters, (high - 1) - low);
      cross_off(prime, low, high, next[b], sieve, counters);
    }

    next_segment:;
  }

  return S2_thread;
}

/// Calculate the contribution of the special leaves which require
/// a sieve (in order to reduce the memory usage).
/// This is a parallel implementation with advanced load balancing.
/// As most special leaves tend to be in the first segments we
/// start off with a small segment size and few segments
/// per thread, after each iteration we dynamically increase
/// the segment size and the segments per thread.
///
int64_t S2_sieve(int64_t x,
                 int64_t y,
                 int64_t z,
                 int64_t c,
                 vector<int32_t>& pi,
                 vector<int32_t>& primes,
                 vector<int32_t>& lpf,
                 vector<int32_t>& mu,
                 int threads)
{
  int64_t S2_total = 0;
  int64_t low = 1;
  int64_t limit = z + 1;

  S2LoadBalancer loadBalancer(x, limit, threads);
  int64_t segment_size = loadBalancer.get_min_segment_size();
  int64_t segments_per_thread = 1;
  vector<int64_t> phi_total(pi[min(isqrt(z), y)] + 1, 0);

  while (low < limit)
  {
    int64_t segments = ceil_div(limit - low, segment_size);
    threads = in_between(1, threads, segments);
    segments_per_thread = in_between(1, segments_per_thread, ceil_div(segments, threads));

    aligned_vector<vector<int64_t> > phi(threads);
    aligned_vector<vector<int64_t> > mu_sum(threads);
    aligned_vector<double> timings(threads);

    #pragma omp parallel for num_threads(threads) reduction(+: S2_total)
    for (int i = 0; i < threads; i++)
    {
      timings[i] = get_wtime();
      S2_total += S2_sieve_thread(x, y, z, c, segment_size, segments_per_thread,
          i, low, limit, pi, primes, lpf, mu, mu_sum[i], phi[i]);
      timings[i] = get_wtime() - timings[i];
    }

    // Once all threads have finished reconstruct and add the
    // missing contribution of all special leaves. This must
    // be done in order as each thread (i) requires the sum of
    // the phi values from the previous threads.
    //
    for (int i = 0; i < threads; i++)
    {
      for (size_t j = 1; j < phi[i].size(); j++)
      {
        S2_total += phi_total[j] * mu_sum[i][j];
        phi_total[j] += phi[i][j];
      }
    }

    low += segments_per_thread * threads * segment_size;
    loadBalancer.update(low, threads, &segment_size, &segments_per_thread, timings);
  }

  return S2_total;
}

/// Calculate the contribution of the special leaves.
/// @pre y > 0 && c > 1
///
int64_t S2(int64_t x,
           int64_t y,
           int64_t z,
           int64_t c,
           vector<int32_t>& primes,
           vector<int32_t>& lpf,
           vector<int32_t>& mu,
           int threads)
{
  int64_t S2_total = 0;
  int64_t limit = z + 1;
  threads = validate_threads(threads, limit);

  vector<int32_t> pi = generate_pi(y);

  S2_total += S2_trivial(x, y, z, c, pi, primes, threads);
  S2_total += S2_easy(x, y, z, c, pi, primes, threads);
  S2_total += S2_sieve(x, y, z, c, pi, primes, lpf, mu, threads);

  return S2_total;
}

/// alpha is a tuning factor which should grow like (log(x))^3
/// for the Deleglise-Rivat prime counting algorithm.
///
double compute_alpha(int64_t x)
{
  double d = (double) x;
  double alpha = log(d) * log(d) * log(d) / 1500;
  return in_between(1, alpha, iroot<6>(x));
}

} // namespace

namespace primecount {

/// Calculate the number of primes below x using the
/// Deleglise-Rivat algorithm.
/// Run time: O(x^(2/3) / (log x)^2) operations, O(x^(1/3) * (log x)^3) space.
///
int64_t pi_deleglise_rivat_parallel1(int64_t x, int threads)
{
  if (x < 2)
    return 0;

  double alpha = compute_alpha(x);
  int64_t y = (int64_t) (alpha * iroot<3>(x));
  int64_t z = x / y;
  int64_t p2 = P2(x, y, threads);

  vector<int32_t> mu = generate_moebius(y);
  vector<int32_t> lpf = generate_least_prime_factors(y);
  vector<int32_t> primes = generate_primes(y);

  int64_t pi_y = pi_bsearch(primes, y);
  int64_t c = min(pi_y, PhiTiny::max_a());
  int64_t s1 = S1(x, y, c, primes[c], lpf , mu, threads);
  int64_t s2 = S2(x, y, z, c, primes, lpf , mu, threads);
  int64_t phi = s1 + s2;
  int64_t sum = phi + pi_y - 1 - p2;

  return sum;
}

} // namespace primecount
<commit_msg>Refactoring<commit_after>///
/// @file  pi_deleglise_rivat_parallel1.cpp
/// @brief Parallel implementation of the Deleglise-Rivat prime
///        counting algorithm. In the Deleglise-Rivat algorithm there
///        are 3 additional types of special leaves compared to the
///        Lagarias-Miller-Odlyzko algorithm: trivial special leaves,
///        clustered easy leaves and sparse easy leaves.
///
///        This implementation is based on the paper:
///        Tomás Oliveira e Silva, Computing pi(x): the combinatorial
///        method, Revista do DETUA, vol. 4, no. 6, March 2006,
///        pp. 759-768.
///
/// Copyright (C) 2014 Kim Walisch, <kim.walisch@gmail.com>
///
/// This file is distributed under the BSD License. See the COPYING
/// file in the top level directory.
///

#include "S2.hpp"
#include <primecount-internal.hpp>
#include <aligned_vector.hpp>
#include <BitSieve.hpp>
#include <generate.hpp>
#include <pmath.hpp>
#include <PhiTiny.hpp>
#include <S2LoadBalancer.hpp>
#include <tos_counters.hpp>
#include <S1.hpp>

#include <stdint.h>
#include <algorithm>
#include <vector>

#ifdef _OPENMP
  #include <omp.h>
#endif

using namespace std;
using namespace primecount;

namespace {

/// For each prime calculate its first multiple >= low
vector<int64_t> generate_next_multiples(int64_t low, int64_t size, vector<int32_t>& primes)
{
  vector<int64_t> next;

  next.reserve(size);
  next.push_back(0);

  for (int64_t b = 1; b < size; b++)
  {
    int64_t prime = primes[b];
    int64_t next_multiple = ceil_div(low, prime) * prime;
    next_multiple += prime * (~next_multiple & 1);
    next.push_back(next_multiple);
  }

  return next;
}

template <typename T>
void cross_off(int64_t prime,
               int64_t low,
               int64_t high,
               int64_t& next_multiple,
               BitSieve& sieve,
               T& counters)
{
  int64_t segment_size = sieve.size();
  int64_t k = next_multiple;

  for (; k < high; k += prime * 2)
  {
    if (sieve[k - low])
    {
      sieve.unset(k - low);
      cnt_update(counters, k - low, segment_size);
    }
  }
  next_multiple = k;
}

/// Compute the S2 contribution of the special leaves that require
/// a sieve. Each thread processes the interval
/// [low_thread, low_thread + segments * segment_size[
/// and the missing special leaf contributions for the interval
/// [1, low_process[ are later reconstructed and added in
/// the parent S2_sieve() function.
///
int64_t S2_sieve_thread(int64_t x,
                        int64_t y,
                        int64_t z,
                        int64_t c,
                        int64_t segment_size,
                        int64_t segments_per_thread,
                        int64_t thread_num,
                        int64_t low,
                        int64_t limit,
                        vector<int32_t>& pi,
                        vector<int32_t>& primes,
                        vector<int32_t>& lpf,
                        vector<int32_t>& mu,
                        vector<int64_t>& mu_sum,
                        vector<int64_t>& phi)
{
  low += segment_size * segments_per_thread * thread_num;
  limit = min(low + segment_size * segments_per_thread, limit);
  int64_t pi_sqrty = pi[isqrt(y)];
  int64_t max_prime = min3(isqrt(x / low), isqrt(z), y);
  int64_t pi_max = pi[max_prime];
  int64_t S2_thread = 0;

  BitSieve sieve(segment_size);
  vector<int32_t> counters(segment_size);
  vector<int64_t> next = generate_next_multiples(low, pi_max + 1, primes);
  phi.resize(pi_max + 1, 0);
  mu_sum.resize(pi_max + 1, 0);

  // segmeted sieve of Eratosthenes
  for (; low < limit; low += segment_size)
  {
    // Current segment = interval [low, high[
    int64_t high = min(low + segment_size, limit);
    int64_t b = c + 1;

    // check if we need the sieve
    if (c <= pi_max)
    {
      sieve.fill(low, high);

      // phi(y, i) nodes with i <= c do not contribute to S2, so we
      // simply sieve out the multiples of the first c primes
      for (int64_t i = 2; i <= c; i++)
      {
        int64_t k = next[i];
        for (int64_t prime = primes[i]; k < high; k += prime * 2)
          sieve.unset(k - low);
        next[i] = k;
      }

      // Initialize special tree data structure from sieve
      cnt_finit(sieve, counters, segment_size);
    }

    // For c + 1 <= b <= pi_sqrty
    // Find all special leaves: n = primes[b] * m, with mu[m] != 0 and primes[b] < lpf[m]
    // which satisfy: low <= (x / n) < high
    for (int64_t end = min(pi_sqrty, pi_max); b <= end; b++)
    {
      int64_t prime = primes[b];
      int64_t min_m = max(x / (prime * high), y / prime);
      int64_t max_m = min(x / (prime * low), y);

      if (prime >= max_m)
        goto next_segment;

      for (int64_t m = max_m; m > min_m; m--)
      {
        if (mu[m] != 0 && prime < lpf[m])
        {
          int64_t n = prime * m;
          int64_t count = cnt_query(counters, (x / n) - low);
          int64_t phi_xn = phi[b] + count;
          S2_thread -= mu[m] * phi_xn;
          mu_sum[b] -= mu[m];
        }
      }

      phi[b] += cnt_query(counters, (high - 1) - low);
      cross_off(prime, low, high, next[b], sieve, counters);
    }

    // For pi_sqrty <= b <= pi_sqrtz
    // Find all hard special leaves: n = primes[b] * primes[l]
    // which satisfy: low <= (x / n) < high
    for (; b <= pi_max; b++)
    {
      int64_t prime = primes[b];
      int64_t l = pi[min3(x / (prime * low), z / prime, y)];
      int64_t min_hard_leaf = max3(x / (prime * high), y / prime, prime);

      if (prime >= primes[l])
        goto next_segment;

      for (; primes[l] > min_hard_leaf; l--)
      {
        int64_t n = prime * primes[l];
        int64_t xn = x / n;
        int64_t count = cnt_query(counters, xn - low);
        int64_t phi_xn = phi[b] + count;
        S2_thread += phi_xn;
        mu_sum[b]++;
      }

      phi[b] += cnt_query(counters, (high - 1) - low);
      cross_off(prime, low, high, next[b], sieve, counters);
    }

    next_segment:;
  }

  return S2_thread;
}

/// Calculate the contribution of the special leaves which require
/// a sieve (in order to reduce the memory usage).
/// This is a parallel implementation with advanced load balancing.
/// As most special leaves tend to be in the first segments we
/// start off with a small segment size and few segments
/// per thread, after each iteration we dynamically increase
/// the segment size and the segments per thread.
///
int64_t S2_sieve(int64_t x,
                 int64_t y,
                 int64_t z,
                 int64_t c,
                 vector<int32_t>& pi,
                 vector<int32_t>& primes,
                 vector<int32_t>& lpf,
                 vector<int32_t>& mu,
                 int threads)
{
  int64_t S2_total = 0;
  int64_t low = 1;
  int64_t limit = z + 1;

  S2LoadBalancer loadBalancer(x, limit, threads);
  int64_t segment_size = loadBalancer.get_min_segment_size();
  int64_t segments_per_thread = 1;
  vector<int64_t> phi_total(pi[min(isqrt(z), y)] + 1, 0);

  while (low < limit)
  {
    int64_t segments = ceil_div(limit - low, segment_size);
    threads = in_between(1, threads, segments);
    segments_per_thread = in_between(1, segments_per_thread, ceil_div(segments, threads));

    aligned_vector<vector<int64_t> > phi(threads);
    aligned_vector<vector<int64_t> > mu_sum(threads);
    aligned_vector<double> timings(threads);

    #pragma omp parallel for num_threads(threads) reduction(+: S2_total)
    for (int i = 0; i < threads; i++)
    {
      timings[i] = get_wtime();
      S2_total += S2_sieve_thread(x, y, z, c, segment_size, segments_per_thread,
          i, low, limit, pi, primes, lpf, mu, mu_sum[i], phi[i]);
      timings[i] = get_wtime() - timings[i];
    }

    // Once all threads have finished reconstruct and add the
    // missing contribution of all special leaves. This must
    // be done in order as each thread (i) requires the sum of
    // the phi values from the previous threads.
    //
    for (int i = 0; i < threads; i++)
    {
      for (size_t j = 1; j < phi[i].size(); j++)
      {
        S2_total += phi_total[j] * mu_sum[i][j];
        phi_total[j] += phi[i][j];
      }
    }

    low += segments_per_thread * threads * segment_size;
    loadBalancer.update(low, threads, &segment_size, &segments_per_thread, timings);
  }

  return S2_total;
}

/// Calculate the contribution of the special leaves.
/// @pre y > 0 && c > 1
///
int64_t S2(int64_t x,
           int64_t y,
           int64_t z,
           int64_t c,
           vector<int32_t>& primes,
           vector<int32_t>& lpf,
           vector<int32_t>& mu,
           int threads)
{
  int64_t limit = z + 1;
  threads = validate_threads(threads, limit);
  vector<int32_t> pi = generate_pi(y);

  int64_t s2_trivial = S2_trivial(x, y, z, c, pi, primes, threads);
  int64_t s2_easy = S2_easy(x, y, z, c, pi, primes, threads);
  int64_t s2_sieve = S2_sieve(x, y, z, c, pi, primes, lpf, mu, threads);
  int64_t s2 = s2_trivial + s2_easy + s2_sieve;

  return s2;
}

/// alpha is a tuning factor which should grow like (log(x))^3
/// for the Deleglise-Rivat prime counting algorithm.
///
double compute_alpha(int64_t x)
{
  double d = (double) x;
  double alpha = log(d) * log(d) * log(d) / 1500;
  return in_between(1, alpha, iroot<6>(x));
}

} // namespace

namespace primecount {

/// Calculate the number of primes below x using the
/// Deleglise-Rivat algorithm.
/// Run time: O(x^(2/3) / (log x)^2) operations, O(x^(1/3) * (log x)^3) space.
///
int64_t pi_deleglise_rivat_parallel1(int64_t x, int threads)
{
  if (x < 2)
    return 0;

  double alpha = compute_alpha(x);
  int64_t y = (int64_t) (alpha * iroot<3>(x));
  int64_t z = x / y;
  int64_t p2 = P2(x, y, threads);

  vector<int32_t> mu = generate_moebius(y);
  vector<int32_t> lpf = generate_least_prime_factors(y);
  vector<int32_t> primes = generate_primes(y);

  int64_t pi_y = pi_bsearch(primes, y);
  int64_t c = min(pi_y, PhiTiny::max_a());
  int64_t s1 = S1(x, y, c, primes[c], lpf , mu, threads);
  int64_t s2 = S2(x, y, z, c, primes, lpf , mu, threads);
  int64_t phi = s1 + s2;
  int64_t sum = phi + pi_y - 1 - p2;

  return sum;
}

} // namespace primecount
<|endoftext|>


<commit_before>#include "file_func.h"
#include "Datapath.h"
#include "Scratchpad.h"
#include "dddg.h"
#include "traceProfiler.h"
#include "profile_base_address.h"
#include <stdio.h>

int main( int argc, const char *argv[])
{
  if(argc < 5)
  {
    cout << "Aladdin" << endl;
    cout << "./aladdin <bench> <trace name> <config file> <base_addr>" << endl;
    exit(0);
  }

  string bench(argv[1]);
  string trace_file(argv[2]);
  string config_file(argv[3]);
  string base_addr_file(argv[4]);
  parse_config(bench, config_file);

  /*Build Initial DDDG*/
  build_initial_dddg(bench, trace_file);
  /*Profiling*/
  profile_init_stats(bench, trace_file);
  profile_base_address(bench, base_addr_file);

  Datapath *acc;
  Scratchpad *spad;

  float mem_latency = 1;
  float cycle_time = 10;
  spad = new Scratchpad(mem_latency, 1);
  acc = new Datapath(bench, cycle_time);
  acc->setScratchpad(spad);
  //get the complete graph
  acc->setGlobalGraph();
  acc->globalOptimizationPass();

  acc->clearGlobalGraph();
  cerr << "after clearing global graph" << endl;
  vector<string> v_method_order;
  unordered_map<string, int > map_method_2_callinst;
  cerr << "before init method order " << endl;
  init_method_order(bench, v_method_order, map_method_2_callinst);

  cerr << "start per graph computation" << endl;
  //return 0;
  ofstream method_latency;
  method_latency.open(bench+ "_method_latency", ofstream::out);
  for(unsigned method_id = 0; method_id < v_method_order.size(); ++method_id)
  {
    string current_dynamic_method;
    string graph_file;
    if (method_id != v_method_order.size() -1)
    {
      current_dynamic_method = v_method_order.at(method_id);
      graph_file = bench + "_" + current_dynamic_method;
    }
    else
    {
      current_dynamic_method = bench;
      graph_file = bench;
      break;
    }

    unsigned base_method;
    int dynamic_count;
    char dash;
    istringstream parser(current_dynamic_method);
    parser >> base_method >> dash >> dynamic_count;

    acc->setGraphName(graph_file);
    acc->optimizationPass();
    //graph, edgeLatency, edgeType fixed

    cerr << current_dynamic_method  << endl;
    acc->setGraphForStepping(graph_file, map_method_2_callinst[current_dynamic_method] + 1);
    while(!acc->step())
      spad->step();
    int cycles = acc->clearGraph();
    cerr << current_dynamic_method << "," << cycles << endl;
    method_latency
      << map_method_2_callinst[current_dynamic_method]  << ","
      << cycles << endl;
  }
  method_latency.close();
  acc->dumpStats();
  delete acc;
  delete spad;
}
<commit_msg>testing membase<commit_after>#include "file_func.h"
#include "Datapath.h"
#include "Scratchpad.h"
#include "dddg.h"
#include "traceProfiler.h"
#include "profile_base_address.h"
#include <stdio.h>

int main( int argc, const char *argv[])
{
  if(argc < 5)
  {
    cout << "Aladdin" << endl;
    cout << "./aladdin <bench> <trace name> <config file> <base_addr>" << endl;
    exit(0);
  }

  string bench(argv[1]);
  string trace_file(argv[2]);
  string config_file(argv[3]);
  string base_addr_file(argv[4]);
  parse_config(bench, config_file);

  /*Build Initial DDDG*/
  build_initial_dddg(bench, trace_file);
  /*Profiling*/
  profile_init_stats(bench, trace_file);
  profile_base_address(bench, base_addr_file);

  return 0;
  Datapath *acc;
  Scratchpad *spad;

  float mem_latency = 1;
  float cycle_time = 10;
  spad = new Scratchpad(mem_latency, 1);
  acc = new Datapath(bench, cycle_time);
  acc->setScratchpad(spad);
  //get the complete graph
  acc->setGlobalGraph();
  acc->globalOptimizationPass();

  acc->clearGlobalGraph();
  cerr << "after clearing global graph" << endl;
  vector<string> v_method_order;
  unordered_map<string, int > map_method_2_callinst;
  cerr << "before init method order " << endl;
  init_method_order(bench, v_method_order, map_method_2_callinst);

  cerr << "start per graph computation" << endl;
  //return 0;
  ofstream method_latency;
  method_latency.open(bench+ "_method_latency", ofstream::out);
  for(unsigned method_id = 0; method_id < v_method_order.size(); ++method_id)
  {
    string current_dynamic_method;
    string graph_file;
    if (method_id != v_method_order.size() -1)
    {
      current_dynamic_method = v_method_order.at(method_id);
      graph_file = bench + "_" + current_dynamic_method;
    }
    else
    {
      current_dynamic_method = bench;
      graph_file = bench;
      break;
    }

    unsigned base_method;
    int dynamic_count;
    char dash;
    istringstream parser(current_dynamic_method);
    parser >> base_method >> dash >> dynamic_count;

    acc->setGraphName(graph_file);
    acc->optimizationPass();
    //graph, edgeLatency, edgeType fixed

    cerr << current_dynamic_method  << endl;
    acc->setGraphForStepping(graph_file, map_method_2_callinst[current_dynamic_method] + 1);
    while(!acc->step())
      spad->step();
    int cycles = acc->clearGraph();
    cerr << current_dynamic_method << "," << cycles << endl;
    method_latency
      << map_method_2_callinst[current_dynamic_method]  << ","
      << cycles << endl;
  }
  method_latency.close();
  acc->dumpStats();
  delete acc;
  delete spad;
}
<|endoftext|>


<commit_before>#include <QtCore/QtPlugin>
#include <QtWidgets/QApplication>

#include "mainwindow/mainWindow.h"
#include "thirdparty/windowsmodernstyle.h"
#include "qRealApplication.h"

using namespace qReal;

void clearConfig()
{
	SettingsManager::clearSettings();
	SettingsManager::instance()->saveData();
}

int main(int argc, char *argv[])
{
	QRealApplication app(argc, argv);

	QTranslator guiTranslator;
	QTranslator utilsTranslator;
	QTranslator qtTranslator;
	if (app.arguments().count() <= 1 || app.arguments().at(1) != "--no-locale") {
		guiTranslator.load(":/qrgui_" + QLocale::system().name());
		utilsTranslator.load(":/qrutils_" + QLocale::system().name());
		qtTranslator.load(":/qt_" + QLocale::system().name());
		app.installTranslator(&guiTranslator);
		app.installTranslator(&utilsTranslator);
		app.installTranslator(&qtTranslator);
	}

	QString fileToOpen;
	if (app.arguments().count() > 1) {
		if (app.arguments().contains("--clear-conf")) {
			clearConfig();
			return 0;
		} else {
			int setIndex = app.arguments().indexOf("--config");
			if (setIndex > -1) {
				QString settingsFileName = app.arguments().at(setIndex + 1);
				SettingsManager::instance()->loadSettings(settingsFileName);
			}
			for (int i = 0; i < argc; i++) {
				if (app.arguments().at(i).endsWith(".qrs")) {
					fileToOpen = app.arguments().at(i);
				}
			}
		}
	}

#ifndef NO_STYLE_WINDOWSMODERN
	app.setStyle(new WindowsModernStyle());
#endif

	MainWindow window(fileToOpen);
	if (window.isVisible()) {
		return app.exec();
	} else {  // The window decided to not show itself, exiting now.
		return 0;
	}
}
<commit_msg>with break<commit_after>#include <QtCore/QtPlugin>
#include <QtWidgets/QApplication>

#include "mainwindow/mainWindow.h"
#include "thirdparty/windowsmodernstyle.h"
#include "qRealApplication.h"

using namespace qReal;

void clearConfig()
{
	SettingsManager::clearSettings();
	SettingsManager::instance()->saveData();
}

int main(int argc, char *argv[])
{
	QRealApplication app(argc, argv);

	QTranslator guiTranslator;
	QTranslator utilsTranslator;
	QTranslator qtTranslator;
	if (app.arguments().count() <= 1 || app.arguments().at(1) != "--no-locale") {
		guiTranslator.load(":/qrgui_" + QLocale::system().name());
		utilsTranslator.load(":/qrutils_" + QLocale::system().name());
		qtTranslator.load(":/qt_" + QLocale::system().name());
		app.installTranslator(&guiTranslator);
		app.installTranslator(&utilsTranslator);
		app.installTranslator(&qtTranslator);
	}

	QString fileToOpen;
	if (app.arguments().count() > 1) {
		if (app.arguments().contains("--clear-conf")) {
			clearConfig();
			return 0;
		} else {
			int setIndex = app.arguments().indexOf("--config");
			if (setIndex > -1) {
				QString settingsFileName = app.arguments().at(setIndex + 1);
				SettingsManager::instance()->loadSettings(settingsFileName);
			}
			for (int i = 0; i < argc; i++) {
				if (app.arguments().at(i).endsWith(".qrs")) {
					fileToOpen = app.arguments().at(i);
					break;
				}
			}
		}
	}

#ifndef NO_STYLE_WINDOWSMODERN
	app.setStyle(new WindowsModernStyle());
#endif

	MainWindow window(fileToOpen);
	if (window.isVisible()) {
		return app.exec();
	} else {  // The window decided to not show itself, exiting now.
		return 0;
	}
}
<|endoftext|>


<commit_before><commit_msg>routing: set missing parking ID in routing response when parking point is given.<commit_after><|endoftext|>


<commit_before>#include <nanyc/library.h>
#include <nanyc/program.h>
#include <yuni/yuni.h>
#include <yuni/core/getopt.h>
#include <yuni/core/string.h>
#include <yuni/io/filename-manipulation.h>
#include <yuni/datetime/timestamp.h>
#include <yuni/core/system/console/console.h>
#include <yuni/core/process/program.h>
#include <yuni/core/system/cpu.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <memory>
#include <random>
#include "libnanyc.h"

namespace ny {
namespace unittests {
namespace {

constexpr const char runningText[] = "running ";

struct Entry final {
	yuni::String module;
	yuni::String name;
};

struct Result final {
	Entry entry;
	bool success;
	int64_t duration_ms;
};

struct App final {
	App();
	App(App&&) = default;
	~App();

	void importFilenames(const std::vector<AnyString>&);
	void fetch(bool nsl);
	void run(const Entry&);
	int run();
	bool statstics(int64_t duration);
	void setcolor(yuni::System::Console::Color) const;
	void resetcolor() const;
	bool inExecutorMode() const;

	struct final {
		uint32_t total = 0;
		uint32_t passing = 0;
		uint32_t failed = 0;
	}
	stats;
	nycompile_opts_t opts;
	bool interactive = true;
	bool colors = true;
	uint32_t loops = 1;
	bool shuffle = false;
	uint32_t timeout_s = 30;
	std::vector<Entry> unittests;
	std::vector<yuni::String> filenames;
	std::vector<Result> results;
	uint32_t jobs = 0;

	Entry execinfo;
	AnyString argv0;

private:
	void startEntry(const Entry&);
	void endEntry(const Entry&, bool, int64_t);
	bool execute(const Entry& entry);

	yuni::String runningMsg;
};

App::App() {
	memset(&opts, 0x0, sizeof(opts));
	opts.userdata = this;;
}

App::~App() {
	free(opts.sources.items);
}

void App::setcolor(yuni::System::Console::Color c) const {
	if (colors)
		yuni::System::Console::SetTextColor(std::cout, c);
}

void App::resetcolor() const {
	if (colors)
		yuni::System::Console::ResetTextColor(std::cout);
}

bool App::inExecutorMode() const {
	return not execinfo.name.empty();
}

auto now() {
	return yuni::DateTime::NowMilliSeconds();
}

bool operator < (const Entry& a, const Entry& b) {
	return std::tie(a.module, a.name) < std::tie(b.module, b.name);
}

const char* plurals(auto count, const char* single, const char* many) {
	return (count <= 1) ? single : many;
}

uint32_t numberOfJobs(uint32_t jobs) {
	if (jobs == 0) {
		jobs = yuni::System::CPU::Count();
	}
	else if (jobs > 256) // arbitrary
		jobs = 256;
	return jobs;
}

void App::importFilenames(const std::vector<AnyString>& list) {
	uint32_t count = static_cast<uint32_t>(list.size());
	filenames.resize(count);
	std::transform(std::begin(list), std::end(list), std::begin(filenames), [](auto& item) -> yuni::String {
		return std::move(yuni::IO::Canonicalize(item));
	});
	opts.sources.count = count;
	opts.sources.items = (nysource_opts_t*) calloc(count, sizeof(nysource_opts_t));
	if (unlikely(!opts.sources.items))
		throw std::bad_alloc();
	for (uint32_t i = 0; i != count; ++i) {
		opts.sources.items[i].filename.len = filenames[i].size();
		opts.sources.items[i].filename.c_str = filenames[i].c_str();
	}
}

void App::fetch(bool nsl) {
	unittests.reserve(512); // arbitrary
	opts.with_nsl_unittests = nsl ? nytrue : nyfalse;
	opts.on_unittest = [](void* userdata, const char* mod, uint32_t mlen, const char* name, uint32_t nlen) {
		auto& self = *reinterpret_cast<App*>(userdata);
		self.unittests.emplace_back();
		auto& entry = self.unittests.back();
		entry.module.assign(mod, mlen);
		entry.name.assign(name, nlen);
	};
	std::cout << "searching for unittests in all source files...\n";
	auto start = now();
	nyprogram_compile(&opts);
	opts.on_unittest = nullptr;
	std::sort(std::begin(unittests), std::end(unittests));
	auto duration = now() - start;
	std::cout << unittests.size() << ' ' << plurals(unittests.size(), "test", "tests");
	std::cout << " found (in " << duration << "ms)\n";
}

void App::startEntry(const Entry& entry) {
	if (interactive) {
		uint32_t progress = static_cast<uint32_t>((100. / stats.total) * static_cast<uint32_t>(results.size()));
		if (progress > 99)
			progress = 99;
		if (progress < 10)
			std::cout << "   ";
		else
			std::cout << "  ";
		std::cout << static_cast<uint32_t>(progress) << "% - ";
		setcolor(yuni::System::Console::bold);
		std::cout << runningText;
		resetcolor();
		auto previousLength = runningMsg.size();
		runningMsg.clear();
		runningMsg << entry.module << '/' << entry.name << "... ";
		if (runningMsg.size() < previousLength)
			runningMsg.resize(previousLength, " ");
		std::cout << runningMsg << '\r' << std::flush;
	}
}

void App::endEntry(const Entry& entry, bool success, int64_t duration) {
	Result result;
	result.entry = entry;
	result.success = success;
	result.duration_ms = duration;
	results.emplace_back(std::move(result));
}

bool App::statstics(int64_t duration) {
	if (interactive) {
		runningMsg.resize(runningMsg.size() + AnyString(runningText).size() + 8 /*%*/);
		runningMsg.fill(' ');
		std::cout << runningMsg << '\r';
	}
	for (auto& result: results) {
		++(result.success ? stats.passing : stats.failed);
		if (result.success) {
			setcolor(yuni::System::Console::green);
			#ifndef YUNI_OS_WINDOWS
			std::cout << "    \u2713  ";
			#else
			std::cout << "   OK  ";
			#endif
			resetcolor();
		}
		else {
			setcolor(yuni::System::Console::red);
			std::cout << "  ERR  ";
			resetcolor();
		}
		std::cout << result.entry.module << '/' << result.entry.name;
		setcolor(yuni::System::Console::lightblue);
		std::cout << "  (" << result.duration_ms << "ms)";
		resetcolor();
		std::cout << '\n';
	}
	std::cout << "\n       " << stats.total << ' ' << plurals(stats.total, "test", "tests");
	if (stats.passing != 0) {
		std::cout << ", ";
		setcolor(yuni::System::Console::red);
		std::cout << stats.passing << " passing";
		resetcolor();
	}
	if (stats.failed) {
		std::cout << ", ";
		setcolor(yuni::System::Console::red);
		std::cout << stats.failed << " failed";
		resetcolor();
	}
	std::cout << "  (";
	if (duration < 10000)
		std::cout << duration << "ms)";
	else
		std::cout << (duration / 1000) << "s)";
	std::cout << "\n\n";
	return stats.failed == 0 and stats.total != 0;
}

bool App::execute(const Entry& entry) {
	auto* program = nyprogram_compile(&opts);
	bool success = program != nullptr;
	if (program) {
		nyprogram_free(program);
	}
	return success;
}

void App::run(const Entry& entry) {
	startEntry(entry);
	yuni::Process::Program program;
	program.durationPrecision(yuni::Process::Program::dpMilliseconds);
	program.program(argv0);
	program.argumentAdd("--executor-module");
	program.argumentAdd(entry.module);
	program.argumentAdd("--executor-name");
	program.argumentAdd(entry.name);
	for (auto& filename: filenames)
		program.argumentAdd(filename);
	auto start = now();
	bool success = program.execute(timeout_s);
	success = success and (program.wait() == 0);
	auto duration = now() - start;
	endEntry(entry, success, duration);
}

void shuffleDeck(std::vector<Entry>& unittests) {
	std::cout << "shuffling the tests...\n" << std::flush;
	auto seed = now();
	auto useed = static_cast<uint32_t>(seed);
	std::shuffle(unittests.begin(), unittests.end(), std::default_random_engine(useed));
}

int App::run() {
	bool success;
	if (not inExecutorMode()) {
		if (not interactive) {
			std::cout << '\n';
			setcolor(yuni::System::Console::bold);
			std::cout << "running all tests...";
			resetcolor();
			std::cout << '\n';
		}
		stats.total = static_cast<uint32_t>(loops * unittests.size());
		results.reserve(stats.total);
		std::cout << '\n';
		auto start = now();
		for (uint32_t l = 0; l != loops; ++l) {
			if (unlikely(shuffle))
				shuffleDeck(unittests);
			for (auto& entry: unittests)
				run(entry);
		}
		auto duration = now() - start;
		success = statstics(duration);
	}
	else {
		success = execute(execinfo);
	}
	return success ? EXIT_SUCCESS : EXIT_FAILURE;
}

int printVersion() {
	std::cout << libnanyc_version_to_cstr() << '\n';
	return EXIT_SUCCESS;
}

int printBugreport() {
	uint32_t length;
	auto* text = libnanyc_get_bugreportdetails(&length);
	if (text) {
		std::cout.write(text, length);
		free(text);
	}
	std::cout << '\n';
	return EXIT_SUCCESS;
}

App prepare(int argc, char** argv) {
	App app;
	bool version = false;
	bool bugreport = false;
	bool nsl = false;
	bool verbose = false;
	bool nocolors = false;
	bool nointeractive = false;
	std::vector<AnyString> filenames;
	yuni::GetOpt::Parser options;
	options.add(filenames, 'i', "", "Input nanyc source files");
	options.addFlag(nsl, ' ', "nsl", "Import NSL unittests");
	options.add(app.timeout_s, 't', "timeout", "Timeout for executing an unittest (seconds)");
	options.add(app.jobs, 'j', "jobs", "Number of concurrent jobs (default: auto)");
	options.add(app.execinfo.module, ' ', "executor-module", "Executor mode, module name (internal use)", false);
	options.add(app.execinfo.name, ' ', "executor-name", "Executor mode, unittest (internal use)", false);
	options.addParagraph("\nEntropy");
	options.add(app.loops, 'n', "loops", "Number of loops (default: 1)");
	options.addFlag(app.shuffle, 's', "shuffle", "Randomly rearrange the unittests");
	options.addParagraph("\nDisplay");
	options.addFlag(nocolors, ' ', "no-colors", "Disable color output");
	options.addFlag(nointeractive, ' ', "no-progress", "Disable progression reporting");
	options.addParagraph("\nHelp");
	options.addFlag(verbose, 'v', "verbose", "More stuff on the screen");
	options.addFlag(bugreport, 'b', "bugreport", "Display some useful information to report a bug");
	options.addFlag(version, ' ', "version", "Print the version");
	options.remainingArguments(filenames);
	if (not options(argc, argv)) {
		if (options.errors())
			throw std::runtime_error("Abort due to error");
		throw EXIT_SUCCESS;
	}
	if (unlikely(version))
		throw printVersion();
	if (unlikely(bugreport))
		throw printBugreport();
	if (unlikely(verbose))
		printBugreport();
	app.importFilenames(filenames);
	if (not app.inExecutorMode()) {
		if (unlikely(app.loops > 100))
			throw "number of loops greater than hard-limit '100'";
		if (unlikely(app.timeout_s == 0))
			throw "invalid null timeout (-t,--timeout)";
		bool istty = yuni::System::Console::IsStdoutTTY();
		app.interactive = not nointeractive and istty;
		app.colors = (not nocolors) and istty;
		app.argv0 = argv[0];
		app.jobs = numberOfJobs(app.jobs);
		app.fetch(nsl);
	}
	return app;
}

} // namespace
} // namespace unittests
} // namespace ny

int main(int argc, char** argv) {
	try {
		auto app = ny::unittests::prepare(argc, argv);
		return app.run();
	}
	catch (const char* e) {
		std::cerr << "error: " << e << '\n';
	}
	catch (const std::exception& e) {
		std::cerr << "exception: " << e.what() << '\n';
	}
	catch (int e) {
		return e;
	}
	return EXIT_FAILURE;;
}
<commit_msg>unittests: add queueservice to run unittests in parallel<commit_after>#include <nanyc/library.h>
#include <nanyc/program.h>
#include <yuni/yuni.h>
#include <yuni/core/getopt.h>
#include <yuni/core/string.h>
#include <yuni/io/filename-manipulation.h>
#include <yuni/datetime/timestamp.h>
#include <yuni/core/system/console/console.h>
#include <yuni/core/process/program.h>
#include <yuni/core/system/cpu.h>
#include <yuni/job/queue/service.h>
#include <yuni/thread/utility.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <memory>
#include <random>
#include "libnanyc.h"

namespace ny {
namespace unittests {
namespace {

constexpr const char runningText[] = "running ";

struct Entry final {
	yuni::String module;
	yuni::String name;
};

struct Result final {
	Entry entry;
	bool success;
	int64_t duration_ms;
};

struct App final {
	App();
	App(App&&) = default;
	~App();

	void importFilenames(const std::vector<AnyString>&);
	void fetch(bool nsl);
	void run(const Entry&);
	int run();
	bool statstics(int64_t duration);
	void setcolor(yuni::System::Console::Color) const;
	void resetcolor() const;
	bool inExecutorMode() const;

	struct final {
		uint32_t total = 0;
		uint32_t passing = 0;
		uint32_t failed = 0;
	}
	stats;
	nycompile_opts_t opts;
	bool interactive = true;
	bool colors = true;
	uint32_t loops = 1;
	bool shuffle = false;
	uint32_t timeout_s = 30;
	std::vector<Entry> unittests;
	std::vector<yuni::String> filenames;
	std::vector<Result> results;
	uint32_t jobs = 0;
	yuni::Mutex mutex;

	Entry execinfo;
	AnyString argv0;

private:
	void startEntry(const Entry&);
	void endEntry(const Entry&, bool, int64_t);
	bool execute(const Entry& entry);

	yuni::String runningMsg;
};

App::App() {
	memset(&opts, 0x0, sizeof(opts));
	opts.userdata = this;;
}

App::~App() {
	free(opts.sources.items);
}

void App::setcolor(yuni::System::Console::Color c) const {
	if (colors)
		yuni::System::Console::SetTextColor(std::cout, c);
}

void App::resetcolor() const {
	if (colors)
		yuni::System::Console::ResetTextColor(std::cout);
}

bool App::inExecutorMode() const {
	return not execinfo.name.empty();
}

auto now() {
	return yuni::DateTime::NowMilliSeconds();
}

bool operator < (const Entry& a, const Entry& b) {
	return std::tie(a.module, a.name) < std::tie(b.module, b.name);
}

const char* plurals(auto count, const char* single, const char* many) {
	return (count <= 1) ? single : many;
}

uint32_t numberOfJobs(uint32_t jobs) {
	if (jobs == 0) {
		jobs = yuni::System::CPU::Count();
	}
	else if (jobs > 256) // arbitrary
		jobs = 256;
	return jobs;
}

void App::importFilenames(const std::vector<AnyString>& list) {
	uint32_t count = static_cast<uint32_t>(list.size());
	filenames.resize(count);
	std::transform(std::begin(list), std::end(list), std::begin(filenames), [](auto& item) -> yuni::String {
		return std::move(yuni::IO::Canonicalize(item));
	});
	opts.sources.count = count;
	opts.sources.items = (nysource_opts_t*) calloc(count, sizeof(nysource_opts_t));
	if (unlikely(!opts.sources.items))
		throw std::bad_alloc();
	for (uint32_t i = 0; i != count; ++i) {
		opts.sources.items[i].filename.len = filenames[i].size();
		opts.sources.items[i].filename.c_str = filenames[i].c_str();
	}
}

void App::fetch(bool nsl) {
	unittests.reserve(512); // arbitrary
	opts.with_nsl_unittests = nsl ? nytrue : nyfalse;
	opts.on_unittest = [](void* userdata, const char* mod, uint32_t mlen, const char* name, uint32_t nlen) {
		auto& self = *reinterpret_cast<App*>(userdata);
		self.unittests.emplace_back();
		auto& entry = self.unittests.back();
		entry.module.assign(mod, mlen);
		entry.name.assign(name, nlen);
	};
	std::cout << "searching for unittests in all source files...\n";
	auto start = now();
	nyprogram_compile(&opts);
	opts.on_unittest = nullptr;
	std::sort(std::begin(unittests), std::end(unittests));
	auto duration = now() - start;
	std::cout << unittests.size() << ' ' << plurals(unittests.size(), "test", "tests");
	std::cout << " found (in " << duration << "ms)\n";
}

void App::startEntry(const Entry& entry) {
	if (interactive) {
		yuni::MutexLocker locker(mutex);
		uint32_t progress = static_cast<uint32_t>((100. / stats.total) * static_cast<uint32_t>(results.size()));
		if (progress > 99)
			progress = 99;
		if (progress < 10)
			std::cout << "   ";
		else
			std::cout << "  ";
		std::cout << static_cast<uint32_t>(progress) << "% - ";
		setcolor(yuni::System::Console::bold);
		std::cout << runningText;
		resetcolor();
		auto previousLength = runningMsg.size();
		runningMsg.clear();
		runningMsg << entry.module << '/' << entry.name << "... ";
		if (runningMsg.size() < previousLength)
			runningMsg.resize(previousLength, " ");
		std::cout << runningMsg << '\r' << std::flush;
	}
}

void App::endEntry(const Entry& entry, bool success, int64_t duration) {
	Result result;
	result.entry = entry;
	result.success = success;
	result.duration_ms = duration;
	yuni::MutexLocker locker(mutex);
	results.emplace_back(std::move(result));
}

bool App::statstics(int64_t duration) {
	if (interactive) {
		runningMsg.resize(runningMsg.size() + AnyString(runningText).size() + 8 /*%*/);
		runningMsg.fill(' ');
		std::cout << runningMsg << '\r';
	}
	for (auto& result: results) {
		++(result.success ? stats.passing : stats.failed);
		if (result.success) {
			setcolor(yuni::System::Console::green);
			#ifndef YUNI_OS_WINDOWS
			std::cout << "    \u2713  ";
			#else
			std::cout << "   OK  ";
			#endif
			resetcolor();
		}
		else {
			setcolor(yuni::System::Console::red);
			std::cout << "  ERR  ";
			resetcolor();
		}
		std::cout << result.entry.module << '/' << result.entry.name;
		setcolor(yuni::System::Console::lightblue);
		std::cout << "  (" << result.duration_ms << "ms)";
		resetcolor();
		std::cout << '\n';
	}
	std::cout << "\n       " << stats.total << ' ' << plurals(stats.total, "test", "tests");
	if (stats.passing != 0) {
		std::cout << ", ";
		setcolor(yuni::System::Console::red);
		std::cout << stats.passing << " passing";
		resetcolor();
	}
	if (stats.failed) {
		std::cout << ", ";
		setcolor(yuni::System::Console::red);
		std::cout << stats.failed << " failed";
		resetcolor();
	}
	std::cout << "  (";
	if (duration < 10000)
		std::cout << duration << "ms)";
	else
		std::cout << (duration / 1000) << "s)";
	std::cout << "\n\n";
	return stats.failed == 0 and stats.total != 0;
}

bool App::execute(const Entry& entry) {
	auto* program = nyprogram_compile(&opts);
	bool success = program != nullptr;
	if (program) {
		nyprogram_free(program);
	}
	return success;
}

void App::run(const Entry& entry) {
	startEntry(entry);
	yuni::Process::Program program;
	program.durationPrecision(yuni::Process::Program::dpMilliseconds);
	program.program(argv0);
	program.argumentAdd("--executor-module");
	program.argumentAdd(entry.module);
	program.argumentAdd("--executor-name");
	program.argumentAdd(entry.name);
	for (auto& filename: filenames)
		program.argumentAdd(filename);
	auto start = now();
	bool success = program.execute(timeout_s);
	success = success and (program.wait() == 0);
	auto duration = now() - start;
	endEntry(entry, success, duration);
}

void shuffleDeck(std::vector<Entry>& unittests) {
	std::cout << "shuffling the tests...\n" << std::flush;
	auto seed = now();
	auto useed = static_cast<uint32_t>(seed);
	std::shuffle(unittests.begin(), unittests.end(), std::default_random_engine(useed));
}

int App::run() {
	bool success;
	if (not inExecutorMode()) {
		if (not interactive) {
			std::cout << '\n';
			setcolor(yuni::System::Console::bold);
			std::cout << "running all tests...";
			resetcolor();
			std::cout << '\n';
		}
		stats.total = static_cast<uint32_t>(loops * unittests.size());
		results.reserve(stats.total);
		std::cout << '\n';
		yuni::Job::QueueService queueservice;
		queueservice.maximumThreadCount(jobs);
		queueservice.minimumThreadCount(jobs);
		for (uint32_t l = 0; l != loops; ++l) {
			if (unlikely(shuffle))
				shuffleDeck(unittests);
			for (auto& entry: unittests)
				yuni::async(queueservice, [=] { run(entry); });
		}
		auto start = now();
		queueservice.start();
		queueservice.wait(yuni::qseIdle);
		auto duration = now() - start;
		success = statstics(duration);
	}
	else {
		success = execute(execinfo);
	}
	return success ? EXIT_SUCCESS : EXIT_FAILURE;
}

int printVersion() {
	std::cout << libnanyc_version_to_cstr() << '\n';
	return EXIT_SUCCESS;
}

int printBugreport() {
	uint32_t length;
	auto* text = libnanyc_get_bugreportdetails(&length);
	if (text) {
		std::cout.write(text, length);
		free(text);
	}
	std::cout << '\n';
	return EXIT_SUCCESS;
}

App prepare(int argc, char** argv) {
	App app;
	bool version = false;
	bool bugreport = false;
	bool nsl = false;
	bool verbose = false;
	bool nocolors = false;
	bool nointeractive = false;
	std::vector<AnyString> filenames;
	yuni::GetOpt::Parser options;
	options.add(filenames, 'i', "", "Input nanyc source files");
	options.addFlag(nsl, ' ', "nsl", "Import NSL unittests");
	options.add(app.timeout_s, 't', "timeout", "Timeout for executing an unittest (seconds)");
	options.add(app.jobs, 'j', "jobs", "Number of concurrent jobs (default: auto)");
	options.add(app.execinfo.module, ' ', "executor-module", "Executor mode, module name (internal use)", false);
	options.add(app.execinfo.name, ' ', "executor-name", "Executor mode, unittest (internal use)", false);
	options.addParagraph("\nEntropy");
	options.add(app.loops, 'n', "loops", "Number of loops (default: 1)");
	options.addFlag(app.shuffle, 's', "shuffle", "Randomly rearrange the unittests");
	options.addParagraph("\nDisplay");
	options.addFlag(nocolors, ' ', "no-colors", "Disable color output");
	options.addFlag(nointeractive, ' ', "no-progress", "Disable progression reporting");
	options.addParagraph("\nHelp");
	options.addFlag(verbose, 'v', "verbose", "More stuff on the screen");
	options.addFlag(bugreport, 'b', "bugreport", "Display some useful information to report a bug");
	options.addFlag(version, ' ', "version", "Print the version");
	options.remainingArguments(filenames);
	if (not options(argc, argv)) {
		if (options.errors())
			throw std::runtime_error("Abort due to error");
		throw EXIT_SUCCESS;
	}
	if (unlikely(version))
		throw printVersion();
	if (unlikely(bugreport))
		throw printBugreport();
	if (unlikely(verbose))
		printBugreport();
	app.importFilenames(filenames);
	if (not app.inExecutorMode()) {
		if (unlikely(app.loops > 100))
			throw "number of loops greater than hard-limit '100'";
		if (unlikely(app.timeout_s == 0))
			throw "invalid null timeout (-t,--timeout)";
		bool istty = yuni::System::Console::IsStdoutTTY();
		app.interactive = not nointeractive and istty;
		app.colors = (not nocolors) and istty;
		app.argv0 = argv[0];
		app.jobs = numberOfJobs(app.jobs);
		app.fetch(nsl);
	}
	return app;
}

} // namespace
} // namespace unittests
} // namespace ny

int main(int argc, char** argv) {
	try {
		auto app = ny::unittests::prepare(argc, argv);
		return app.run();
	}
	catch (const char* e) {
		std::cerr << "error: " << e << '\n';
	}
	catch (const std::exception& e) {
		std::cerr << "exception: " << e.what() << '\n';
	}
	catch (int e) {
		return e;
	}
	return EXIT_FAILURE;;
}
<|endoftext|>


<commit_before>/*
 * SegmentInformation.cpp
 *****************************************************************************
 * Copyright (C) 2014 - VideoLAN Authors
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
#include "SegmentInformation.hpp"

#include "Segment.h"
#include "SegmentBase.h"
#include "SegmentList.h"
#include "SegmentTemplate.h"
#include "SegmentTimeline.h"
#include "AbstractPlaylist.hpp"

using namespace adaptative::playlist;
using namespace std;

SegmentInformation::SegmentInformation(SegmentInformation *parent_) :
    ICanonicalUrl( parent_ ),
    TimescaleAble( parent_ )
{
    parent = parent_;
    init();
}

SegmentInformation::SegmentInformation(AbstractPlaylist * parent_) :
    ICanonicalUrl(parent_),
    TimescaleAble()
{
    parent = NULL;
    init();
}

void SegmentInformation::init()
{
    baseUrl.Set(NULL);
    segmentBase = NULL;
    segmentList = NULL;
    mediaSegmentTemplate = NULL;
    bitswitch_policy = BITSWITCH_INHERIT;
}

SegmentInformation::~SegmentInformation()
{
    delete segmentBase;
    delete segmentList;
    delete mediaSegmentTemplate;
}

vector<ISegment *> SegmentInformation::getSegments(SegmentInfoType type) const
{
    vector<ISegment *> retSegments;

    switch (type)
    {
        case INFOTYPE_INIT:
        {
            /* init segments are always single segment */
            if( segmentBase && segmentBase->initialisationSegment.Get() )
            {
                retSegments.push_back( segmentBase->initialisationSegment.Get() );
            }
            else if( segmentList && segmentList->initialisationSegment.Get() )
            {
                retSegments.push_back( segmentList->initialisationSegment.Get() );
            }
            else if( mediaSegmentTemplate && mediaSegmentTemplate->initialisationSegment.Get() )
            {
                retSegments.push_back( mediaSegmentTemplate->initialisationSegment.Get() );
            }
        }
        break;

        case INFOTYPE_MEDIA:
        {
            if( mediaSegmentTemplate )
            {
                retSegments.push_back( mediaSegmentTemplate );
            }
            else if ( segmentList && !segmentList->getSegments().empty() )
            {
                std::vector<Segment *>::const_iterator it;
                for(it=segmentList->getSegments().begin();
                    it!=segmentList->getSegments().end(); ++it)
                {
                    std::vector<ISegment *> list = (*it)->subSegments();
                    retSegments.insert( retSegments.end(), list.begin(), list.end() );
                }
            }
            else if( segmentBase )
            {
                std::vector<ISegment *> list = segmentBase->subSegments();
                retSegments.insert( retSegments.end(), list.begin(), list.end() );
            }
        }
        break;

        case INFOTYPE_INDEX:
        {
            /* index segments are always single segment */
            if( segmentBase && segmentBase->indexSegment.Get() )
            {
                retSegments.push_back( segmentBase->indexSegment.Get() );
            }
            else if( segmentList && segmentList->indexSegment.Get() )
            {
                retSegments.push_back( segmentList->indexSegment.Get() );
            }
            // templated index ?
        }

        default:
        break;
    }

    if( retSegments.empty() && parent )
        return parent->getSegments( type );
    else
        return retSegments;
}

vector<ISegment *> SegmentInformation::getSegments() const
{
    vector<ISegment *> retSegments;
    for(int i=0; i<InfoTypeCount; i++)
    {
        vector<ISegment *> segs = getSegments(static_cast<SegmentInfoType>(i));
        retSegments.insert( retSegments.end(), segs.begin(), segs.end() );
    }
    return retSegments;
}

ISegment * SegmentInformation::getSegment(SegmentInfoType type, uint64_t pos) const
{
    ISegment *segment = NULL;

    vector<ISegment *> retSegments = getSegments( type );
    const size_t size = retSegments.size();
    if( size )
    {
        /* check if that's a template (fixme: find a better way) */
        MediaSegmentTemplate *templ;
        if( size == 1 && (templ = dynamic_cast<MediaSegmentTemplate*>(retSegments[0])) )
        {
            if(templ->segmentTimeline.Get() == NULL ||
               templ->segmentTimeline.Get()->maxElementNumber() > pos)
                return templ;
        }
        else if( pos < size )
        {
            segment = retSegments[pos];
        }
    }

    return segment;
}

bool SegmentInformation::getSegmentNumberByTime(mtime_t time, uint64_t *ret) const
{
    SegmentList *segList;
    MediaSegmentTemplate *mediaTemplate;
    uint64_t timescale = 1;
    mtime_t duration = 0;

    if( (mediaTemplate = inheritSegmentTemplate()) )
    {
        timescale = mediaTemplate->inheritTimescale();
        duration = mediaTemplate->duration.Get();
    }
    else if ( (segList = inheritSegmentList()) )
    {
        timescale = segList->inheritTimescale();
        duration = segList->duration.Get();
    }

    if(duration)
    {
        *ret = time / (CLOCK_FREQ * duration / timescale);
        return true;
    }

    return false;
}

mtime_t SegmentInformation::getPlaybackTimeBySegmentNumber(uint64_t number) const
{
    SegmentList *segList;
    MediaSegmentTemplate *mediaTemplate;
    uint64_t timescale = 1;
    mtime_t time = 0;
    if( (mediaTemplate = inheritSegmentTemplate()) )
    {
        timescale = mediaTemplate->inheritTimescale();
        if(mediaTemplate->segmentTimeline.Get())
        {
            time = mediaTemplate->segmentTimeline.Get()->
                    getScaledPlaybackTimeByElementNumber(number);
        }
        else
        {
            time = number * mediaTemplate->duration.Get();
        }
    }
    else if ( (segList = inheritSegmentList()) )
    {
        timescale = segList->inheritTimescale();
        time = number * segList->duration.Get();
    }

    if(time)
        time = CLOCK_FREQ * time / timescale;

    return time;
}

void SegmentInformation::collectTimelines(std::vector<SegmentTimeline *> *timelines) const
{
    if(mediaSegmentTemplate && mediaSegmentTemplate->segmentTimeline.Get())
        timelines->push_back(mediaSegmentTemplate->segmentTimeline.Get());

    std::vector<SegmentInformation *>::const_iterator it;
    for(it = childs.begin(); it != childs.end(); ++it)
        (*it)->collectTimelines(timelines);
}

bool SegmentInformation::canBitswitch() const
{
    if(bitswitch_policy == BITSWITCH_INHERIT)
        return (parent) ? parent->canBitswitch() : false;
    else
        return (bitswitch_policy == BITSWITCH_YES);
}

mtime_t SegmentInformation::getPeriodStart() const
{
    if(parent)
        return parent->getPeriodStart();
    else
        return 0;
}

void SegmentInformation::setSegmentList(SegmentList *list)
{
    segmentList = list;
}

void SegmentInformation::setSegmentBase(SegmentBase *base)
{
    segmentBase = base;
}

void SegmentInformation::setSegmentTemplate(MediaSegmentTemplate *templ)
{
    mediaSegmentTemplate = templ;
}

static void insertIntoSegment(std::vector<ISegment *> &seglist, size_t start,
                              size_t end, mtime_t time)
{
    std::vector<ISegment *>::iterator segIt;
    for(segIt = seglist.begin(); segIt < seglist.end(); ++segIt)
    {
        ISegment *segment = *segIt;
        if(segment->getClassId() == Segment::CLASSID_SEGMENT &&
           segment->contains(end + segment->getOffset()))
        {
            SubSegment *subsegment = new SubSegment(segment,
                                                    start + segment->getOffset(),
                                                    (end != 0) ? end + segment->getOffset() : 0);
            segment->addSubSegment(subsegment);
            segment->startTime.Set(time);
            break;
        }
    }
}

void SegmentInformation::SplitUsingIndex(std::vector<SplitPoint> &splitlist)
{
    std::vector<ISegment *> seglist = getSegments(INFOTYPE_MEDIA);
    std::vector<SplitPoint>::const_iterator splitIt;
    size_t start = 0, end = 0;
    mtime_t time = 0;

    for(splitIt = splitlist.begin(); splitIt < splitlist.end(); ++splitIt)
    {
        start = end;
        SplitPoint split = *splitIt;
        end = split.offset;
        if(splitIt == splitlist.begin() && split.offset == 0)
            continue;
        time = split.time;
        insertIntoSegment(seglist, start, end, time);
        end++;
    }

    if(start != 0)
    {
        start = end;
        end = 0;
        insertIntoSegment(seglist, start, end, time);
    }
}

void SegmentInformation::setBitstreamSwitching(bool bitswitch)
{
    bitswitch_policy = (bitswitch) ? BITSWITCH_YES : BITSWITCH_NO;
}

Url SegmentInformation::getUrlSegment() const
{
    if(baseUrl.Get() && baseUrl.Get()->hasScheme())
    {
        return *(baseUrl.Get());
    }
    else
    {
        Url ret = getParentUrlSegment();
        if (baseUrl.Get())
            ret.append(*(baseUrl.Get()));
        return ret;
    }
}

SegmentBase * SegmentInformation::inheritSegmentBase() const
{
    if(segmentBase)
        return segmentBase;
    else if (parent)
        return parent->inheritSegmentBase();
    else
        return NULL;
}

SegmentList * SegmentInformation::inheritSegmentList() const
{
    if(segmentList)
        return segmentList;
    else if (parent)
        return parent->inheritSegmentList();
    else
        return NULL;
}

MediaSegmentTemplate * SegmentInformation::inheritSegmentTemplate() const
{
    if(mediaSegmentTemplate)
        return mediaSegmentTemplate;
    else if (parent)
        return parent->inheritSegmentTemplate();
    else
        return NULL;
}
<commit_msg>demux: adaptative: set split time to child, not parent<commit_after>/*
 * SegmentInformation.cpp
 *****************************************************************************
 * Copyright (C) 2014 - VideoLAN Authors
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
#include "SegmentInformation.hpp"

#include "Segment.h"
#include "SegmentBase.h"
#include "SegmentList.h"
#include "SegmentTemplate.h"
#include "SegmentTimeline.h"
#include "AbstractPlaylist.hpp"

using namespace adaptative::playlist;
using namespace std;

SegmentInformation::SegmentInformation(SegmentInformation *parent_) :
    ICanonicalUrl( parent_ ),
    TimescaleAble( parent_ )
{
    parent = parent_;
    init();
}

SegmentInformation::SegmentInformation(AbstractPlaylist * parent_) :
    ICanonicalUrl(parent_),
    TimescaleAble()
{
    parent = NULL;
    init();
}

void SegmentInformation::init()
{
    baseUrl.Set(NULL);
    segmentBase = NULL;
    segmentList = NULL;
    mediaSegmentTemplate = NULL;
    bitswitch_policy = BITSWITCH_INHERIT;
}

SegmentInformation::~SegmentInformation()
{
    delete segmentBase;
    delete segmentList;
    delete mediaSegmentTemplate;
}

vector<ISegment *> SegmentInformation::getSegments(SegmentInfoType type) const
{
    vector<ISegment *> retSegments;

    switch (type)
    {
        case INFOTYPE_INIT:
        {
            /* init segments are always single segment */
            if( segmentBase && segmentBase->initialisationSegment.Get() )
            {
                retSegments.push_back( segmentBase->initialisationSegment.Get() );
            }
            else if( segmentList && segmentList->initialisationSegment.Get() )
            {
                retSegments.push_back( segmentList->initialisationSegment.Get() );
            }
            else if( mediaSegmentTemplate && mediaSegmentTemplate->initialisationSegment.Get() )
            {
                retSegments.push_back( mediaSegmentTemplate->initialisationSegment.Get() );
            }
        }
        break;

        case INFOTYPE_MEDIA:
        {
            if( mediaSegmentTemplate )
            {
                retSegments.push_back( mediaSegmentTemplate );
            }
            else if ( segmentList && !segmentList->getSegments().empty() )
            {
                std::vector<Segment *>::const_iterator it;
                for(it=segmentList->getSegments().begin();
                    it!=segmentList->getSegments().end(); ++it)
                {
                    std::vector<ISegment *> list = (*it)->subSegments();
                    retSegments.insert( retSegments.end(), list.begin(), list.end() );
                }
            }
            else if( segmentBase )
            {
                std::vector<ISegment *> list = segmentBase->subSegments();
                retSegments.insert( retSegments.end(), list.begin(), list.end() );
            }
        }
        break;

        case INFOTYPE_INDEX:
        {
            /* index segments are always single segment */
            if( segmentBase && segmentBase->indexSegment.Get() )
            {
                retSegments.push_back( segmentBase->indexSegment.Get() );
            }
            else if( segmentList && segmentList->indexSegment.Get() )
            {
                retSegments.push_back( segmentList->indexSegment.Get() );
            }
            // templated index ?
        }

        default:
        break;
    }

    if( retSegments.empty() && parent )
        return parent->getSegments( type );
    else
        return retSegments;
}

vector<ISegment *> SegmentInformation::getSegments() const
{
    vector<ISegment *> retSegments;
    for(int i=0; i<InfoTypeCount; i++)
    {
        vector<ISegment *> segs = getSegments(static_cast<SegmentInfoType>(i));
        retSegments.insert( retSegments.end(), segs.begin(), segs.end() );
    }
    return retSegments;
}

ISegment * SegmentInformation::getSegment(SegmentInfoType type, uint64_t pos) const
{
    ISegment *segment = NULL;

    vector<ISegment *> retSegments = getSegments( type );
    const size_t size = retSegments.size();
    if( size )
    {
        /* check if that's a template (fixme: find a better way) */
        MediaSegmentTemplate *templ;
        if( size == 1 && (templ = dynamic_cast<MediaSegmentTemplate*>(retSegments[0])) )
        {
            if(templ->segmentTimeline.Get() == NULL ||
               templ->segmentTimeline.Get()->maxElementNumber() > pos)
                return templ;
        }
        else if( pos < size )
        {
            segment = retSegments[pos];
        }
    }

    return segment;
}

bool SegmentInformation::getSegmentNumberByTime(mtime_t time, uint64_t *ret) const
{
    SegmentList *segList;
    MediaSegmentTemplate *mediaTemplate;
    uint64_t timescale = 1;
    mtime_t duration = 0;

    if( (mediaTemplate = inheritSegmentTemplate()) )
    {
        timescale = mediaTemplate->inheritTimescale();
        duration = mediaTemplate->duration.Get();
    }
    else if ( (segList = inheritSegmentList()) )
    {
        timescale = segList->inheritTimescale();
        duration = segList->duration.Get();
    }

    if(duration)
    {
        *ret = time / (CLOCK_FREQ * duration / timescale);
        return true;
    }

    return false;
}

mtime_t SegmentInformation::getPlaybackTimeBySegmentNumber(uint64_t number) const
{
    SegmentList *segList;
    MediaSegmentTemplate *mediaTemplate;
    uint64_t timescale = 1;
    mtime_t time = 0;
    if( (mediaTemplate = inheritSegmentTemplate()) )
    {
        timescale = mediaTemplate->inheritTimescale();
        if(mediaTemplate->segmentTimeline.Get())
        {
            time = mediaTemplate->segmentTimeline.Get()->
                    getScaledPlaybackTimeByElementNumber(number);
        }
        else
        {
            time = number * mediaTemplate->duration.Get();
        }
    }
    else if ( (segList = inheritSegmentList()) )
    {
        timescale = segList->inheritTimescale();
        time = number * segList->duration.Get();
    }

    if(time)
        time = CLOCK_FREQ * time / timescale;

    return time;
}

void SegmentInformation::collectTimelines(std::vector<SegmentTimeline *> *timelines) const
{
    if(mediaSegmentTemplate && mediaSegmentTemplate->segmentTimeline.Get())
        timelines->push_back(mediaSegmentTemplate->segmentTimeline.Get());

    std::vector<SegmentInformation *>::const_iterator it;
    for(it = childs.begin(); it != childs.end(); ++it)
        (*it)->collectTimelines(timelines);
}

bool SegmentInformation::canBitswitch() const
{
    if(bitswitch_policy == BITSWITCH_INHERIT)
        return (parent) ? parent->canBitswitch() : false;
    else
        return (bitswitch_policy == BITSWITCH_YES);
}

mtime_t SegmentInformation::getPeriodStart() const
{
    if(parent)
        return parent->getPeriodStart();
    else
        return 0;
}

void SegmentInformation::setSegmentList(SegmentList *list)
{
    segmentList = list;
}

void SegmentInformation::setSegmentBase(SegmentBase *base)
{
    segmentBase = base;
}

void SegmentInformation::setSegmentTemplate(MediaSegmentTemplate *templ)
{
    mediaSegmentTemplate = templ;
}

static void insertIntoSegment(std::vector<ISegment *> &seglist, size_t start,
                              size_t end, mtime_t time)
{
    std::vector<ISegment *>::iterator segIt;
    for(segIt = seglist.begin(); segIt < seglist.end(); ++segIt)
    {
        ISegment *segment = *segIt;
        if(segment->getClassId() == Segment::CLASSID_SEGMENT &&
           segment->contains(end + segment->getOffset()))
        {
            SubSegment *subsegment = new SubSegment(segment,
                                                    start + segment->getOffset(),
                                                    (end != 0) ? end + segment->getOffset() : 0);
            subsegment->startTime.Set(time);
            segment->addSubSegment(subsegment);
            break;
        }
    }
}

void SegmentInformation::SplitUsingIndex(std::vector<SplitPoint> &splitlist)
{
    std::vector<ISegment *> seglist = getSegments(INFOTYPE_MEDIA);
    std::vector<SplitPoint>::const_iterator splitIt;
    size_t start = 0, end = 0;
    mtime_t time = 0;

    for(splitIt = splitlist.begin(); splitIt < splitlist.end(); ++splitIt)
    {
        start = end;
        SplitPoint split = *splitIt;
        end = split.offset;
        if(splitIt == splitlist.begin() && split.offset == 0)
            continue;
        time = split.time;
        insertIntoSegment(seglist, start, end, time);
        end++;
    }

    if(start != 0)
    {
        start = end;
        end = 0;
        insertIntoSegment(seglist, start, end, time);
    }
}

void SegmentInformation::setBitstreamSwitching(bool bitswitch)
{
    bitswitch_policy = (bitswitch) ? BITSWITCH_YES : BITSWITCH_NO;
}

Url SegmentInformation::getUrlSegment() const
{
    if(baseUrl.Get() && baseUrl.Get()->hasScheme())
    {
        return *(baseUrl.Get());
    }
    else
    {
        Url ret = getParentUrlSegment();
        if (baseUrl.Get())
            ret.append(*(baseUrl.Get()));
        return ret;
    }
}

SegmentBase * SegmentInformation::inheritSegmentBase() const
{
    if(segmentBase)
        return segmentBase;
    else if (parent)
        return parent->inheritSegmentBase();
    else
        return NULL;
}

SegmentList * SegmentInformation::inheritSegmentList() const
{
    if(segmentList)
        return segmentList;
    else if (parent)
        return parent->inheritSegmentList();
    else
        return NULL;
}

MediaSegmentTemplate * SegmentInformation::inheritSegmentTemplate() const
{
    if(mediaSegmentTemplate)
        return mediaSegmentTemplate;
    else if (parent)
        return parent->inheritSegmentTemplate();
    else
        return NULL;
}
<|endoftext|>


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

#ifndef _OSL_TIME_H_
#include <osl/time.h>
#endif

#include <osl/mutex.hxx>
#include <osl/thread.hxx>

#include <cppuhelper/servicefactory.hxx>

#include <com/sun/star/bridge/XBridgeFactory.hpp>
#include <com/sun/star/connection/XAcceptor.hpp>
#include <com/sun/star/connection/XConnector.hpp>

#include <com/sun/star/lang/XComponent.hpp>

#include <cppuhelper/weak.hxx>

#include <test/XTestFactory.hpp>

using namespace ::test;
using namespace ::rtl;
using namespace ::cppu;
using namespace ::osl;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::bridge;
using namespace ::com::sun::star::connection;

#ifdef SAL_W32
#include <conio.h>
#endif

#include "testcomp.h"
#include "osl/mutex.h"

/*********
 *
 ********/

class MyThread :
    public Thread
{
public:
    MyThread( const Reference< XAcceptor > &r ,
              const Reference< XBridgeFactory > &rFactory,
              const OUString &sConnectionDescription) :
        m_rAcceptor( r ),
        m_rBridgeFactory ( rFactory ),
        m_sConnectionDescription( sConnectionDescription )
        {}
    virtual void SAL_CALL run();

private:
    Reference < XAcceptor > m_rAcceptor;
    Reference < XBridgeFactory > m_rBridgeFactory;
    OUString m_sConnectionDescription;
};

void MyThread::run()
{

    while ( sal_True )
    {
        try
        {
            Reference < XConnection > rConnection =
                m_rAcceptor->accept( m_sConnectionDescription );

            if( ! rConnection.is() )
            {
                break;
            }

            Reference < XBridge > rBridge =
                m_rBridgeFactory->createBridge(
                    OUString() ,
                    OUString( RTL_CONSTASCII_USTRINGPARAM("iiop")) ,
                    rConnection ,
                    (XInstanceProvider * ) new OInstanceProvider );

        }
        catch ( ... )
        {
            printf( "Exception was thrown by acceptor thread\n" );
            break;
        }
    }
}

int main( int argc, char *argv[] )
{
    if( argc < 2 )
    {
        printf( "usage : testsamprocess host:port\n" );
        return 0;
    }

    {
        Reference< XMultiServiceFactory > rSMgr = createRegistryServiceFactory(
            OUString( RTL_CONSTASCII_USTRINGPARAM( "client.rdb" )  ) );

        Reference < XConnector > rConnector(
            createComponent( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.connection.Connector")),
                             OUString( RTL_CONSTASCII_USTRINGPARAM("connectr")),
                             rSMgr ),
            UNO_QUERY );

        Reference < XAcceptor > rAcceptor(
            createComponent( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.connection.Acceptor")),
                             OUString( RTL_CONSTASCII_USTRINGPARAM("acceptor")),
                             rSMgr ),
            UNO_QUERY );

        // just ensure that it is registered
        createComponent( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bridge.Bridge.iiop")),
                         OUString( RTL_CONSTASCII_USTRINGPARAM("iiopbrdg")),
                         rSMgr );

        Reference < XBridgeFactory > rFactory(
            createComponent( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bridge.BridgeFactory")),
                             OUString( RTL_CONSTASCII_USTRINGPARAM("brdgfctr")),
                             rSMgr ),
            UNO_QUERY );

        MyThread threadAcceptor( rAcceptor , rFactory , OUString::createFromAscii( argv[1] ) );

        threadAcceptor.create();
        TimeValue value={2,0};
        osl_waitThread( &value );

        try
        {
            Reference < XConnection > rConnection =
                rConnector->connect( OUString::createFromAscii( argv[1] ) );

            printf( "%s\n" , OUStringToOString( rConnection->getDescription(),
                                                RTL_TEXTENCODING_ASCII_US ).pData->buffer );

            if( rFactory.is() )
            {

                Reference < XBridge > rBridge = rFactory->createBridge(
                    OUString( RTL_CONSTASCII_USTRINGPARAM("bla blub")),
                    OUString( RTL_CONSTASCII_USTRINGPARAM("iiop")),
                    rConnection,
                    Reference < XInstanceProvider > () );

                Reference < XInterface > rInitialObject
                    = rBridge->getInstance( OUString( RTL_CONSTASCII_USTRINGPARAM("bla")) );

                if( rInitialObject.is() )
                {
                    printf( "got the remote object\n" );
                    testRemote( rInitialObject );
                }
                printf( "Closing...\n" );
                TimeValue value={2,0};
                osl_waitThread( &value );
            }

            Reference < XBridge > rBridge = rFactory->getBridge(
                OUString( RTL_CONSTASCII_USTRINGPARAM("bla blub")) );
            assert( ! rBridge.is() );

        }
        catch( Exception & )
        {
            printf( "Login failed, got an Exception !\n" );
        }

        rAcceptor->stopAccepting();
        threadAcceptor.join();

        Reference < XComponent > rComp( rFactory , UNO_QUERY );
        rComp->dispose();

        rComp = Reference < XComponent > ( rSMgr , UNO_QUERY );
        rComp->dispose();
    }
    return 0;
}
<commit_msg>INTEGRATION: CWS uno2 (1.7.2); FILE MERGED 2003/04/08 09:48:09 dbo 1.7.2.1: #107991# UNO shared library component renaming<commit_after>/*************************************************************************
 *
 *  $RCSfile: testsameprocess.cxx,v $
 *
 *  $Revision: 1.9 $
 *
 *  last change: $Author: rt $ $Date: 2003-04-23 16:45:38 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/
#if OSL_DEBUG_LEVEL == 0
#define NDEBUG
#endif
#include <assert.h>

#ifndef _OSL_TIME_H_
#include <osl/time.h>
#endif

#include <osl/mutex.hxx>
#include <osl/thread.hxx>

#include <cppuhelper/servicefactory.hxx>

#include <com/sun/star/bridge/XBridgeFactory.hpp>
#include <com/sun/star/connection/XAcceptor.hpp>
#include <com/sun/star/connection/XConnector.hpp>

#include <com/sun/star/lang/XComponent.hpp>

#include <cppuhelper/weak.hxx>

#include <test/XTestFactory.hpp>

using namespace ::test;
using namespace ::rtl;
using namespace ::cppu;
using namespace ::osl;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::bridge;
using namespace ::com::sun::star::connection;

#ifdef SAL_W32
#include <conio.h>
#endif

#include "testcomp.h"
#include "osl/mutex.h"

/*********
 *
 ********/

class MyThread :
    public Thread
{
public:
    MyThread( const Reference< XAcceptor > &r ,
              const Reference< XBridgeFactory > &rFactory,
              const OUString &sConnectionDescription) :
        m_rAcceptor( r ),
        m_rBridgeFactory ( rFactory ),
        m_sConnectionDescription( sConnectionDescription )
        {}
    virtual void SAL_CALL run();

private:
    Reference < XAcceptor > m_rAcceptor;
    Reference < XBridgeFactory > m_rBridgeFactory;
    OUString m_sConnectionDescription;
};

void MyThread::run()
{

    while ( sal_True )
    {
        try
        {
            Reference < XConnection > rConnection =
                m_rAcceptor->accept( m_sConnectionDescription );

            if( ! rConnection.is() )
            {
                break;
            }

            Reference < XBridge > rBridge =
                m_rBridgeFactory->createBridge(
                    OUString() ,
                    OUString( RTL_CONSTASCII_USTRINGPARAM("iiop")) ,
                    rConnection ,
                    (XInstanceProvider * ) new OInstanceProvider );

        }
        catch ( ... )
        {
            printf( "Exception was thrown by acceptor thread\n" );
            break;
        }
    }
}

int main( int argc, char *argv[] )
{
    if( argc < 2 )
    {
        printf( "usage : testsamprocess host:port\n" );
        return 0;
    }

    {
        Reference< XMultiServiceFactory > rSMgr = createRegistryServiceFactory(
            OUString( RTL_CONSTASCII_USTRINGPARAM( "client.rdb" )  ) );

        Reference < XConnector > rConnector(
            createComponent( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.connection.Connector")),
                             OUString( RTL_CONSTASCII_USTRINGPARAM("connector.uno" SAL_DLLEXTENSION)),
                             rSMgr ),
            UNO_QUERY );

        Reference < XAcceptor > rAcceptor(
            createComponent( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.connection.Acceptor")),
                             OUString( RTL_CONSTASCII_USTRINGPARAM("acceptor.uno" SAL_DLLEXTENSION)),
                             rSMgr ),
            UNO_QUERY );

        // just ensure that it is registered
//      createComponent( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bridge.Bridge.iiop")),
//                       OUString( RTL_CONSTASCII_USTRINGPARAM("iiopbrdg" SAL_DLLEXTENSION)),
//                       rSMgr );

        Reference < XBridgeFactory > rFactory(
            createComponent( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bridge.BridgeFactory")),
                             OUString( RTL_CONSTASCII_USTRINGPARAM("bridgefac.uno" SAL_DLLEXTENSION)),
                             rSMgr ),
            UNO_QUERY );

        MyThread threadAcceptor( rAcceptor , rFactory , OUString::createFromAscii( argv[1] ) );

        threadAcceptor.create();
        TimeValue value={2,0};
        osl_waitThread( &value );

        try
        {
            Reference < XConnection > rConnection =
                rConnector->connect( OUString::createFromAscii( argv[1] ) );

            printf( "%s\n" , OUStringToOString( rConnection->getDescription(),
                                                RTL_TEXTENCODING_ASCII_US ).pData->buffer );

            if( rFactory.is() )
            {

                Reference < XBridge > rBridge = rFactory->createBridge(
                    OUString( RTL_CONSTASCII_USTRINGPARAM("bla blub")),
                    OUString( RTL_CONSTASCII_USTRINGPARAM("iiop")),
                    rConnection,
                    Reference < XInstanceProvider > () );

                Reference < XInterface > rInitialObject
                    = rBridge->getInstance( OUString( RTL_CONSTASCII_USTRINGPARAM("bla")) );

                if( rInitialObject.is() )
                {
                    printf( "got the remote object\n" );
                    testRemote( rInitialObject );
                }
                printf( "Closing...\n" );
                TimeValue value={2,0};
                osl_waitThread( &value );
            }

            Reference < XBridge > rBridge = rFactory->getBridge(
                OUString( RTL_CONSTASCII_USTRINGPARAM("bla blub")) );
            assert( ! rBridge.is() );

        }
        catch( Exception & )
        {
            printf( "Login failed, got an Exception !\n" );
        }

        rAcceptor->stopAccepting();
        threadAcceptor.join();

        Reference < XComponent > rComp( rFactory , UNO_QUERY );
        rComp->dispose();

        rComp = Reference < XComponent > ( rSMgr , UNO_QUERY );
        rComp->dispose();
    }
    return 0;
}
<|endoftext|>


<commit_before>#ifndef ARCHIVEMODEL_H
#define ARCHIVEMODEL_H

#include <QAbstractTableModel>
#include <QModelIndex>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QSharedPointer>
#include <QString>
#include <QUrl>
#include <QVariant>
#include <QVector>

enum EntryType {
    Local,
    Downloaded,
    Downloadable
};

struct ArchiveEntry {
    QString name;
    QString path;
    QString url;
    size_t sentences;
    double size;
    QString description;
    QString longDescription;
    QString checksum;
    EntryType type;

    QString filePath() const;
    bool existsLocally() const;
};

class ArchiveModel : public QAbstractTableModel
{
    Q_OBJECT
public:
    ArchiveModel(QObject *parent = 0);
    ArchiveModel(QString const &archiveUrl, QObject *parent = 0);
    QVariant data(QModelIndex const &index, int role = Qt::DisplayRole) const;
    int columnCount(QModelIndex const &parent = QModelIndex()) const;
    ArchiveEntry const &entryAtRow(int row) const;
    QVariant headerData(int column, Qt::Orientation orientation,
        int role) const;
    void refresh();
    int rowCount(QModelIndex const &parent = QModelIndex()) const;
    void setUrl(QString const &archiveUrl);

    void deleteLocalFiles(QModelIndex const &index);

private slots:
    void replyFinished(QNetworkReply *reply);

signals:
    void networkError(QString error);
    void processingError(QString error);
    void retrieving();
    void retrievalFinished();

private:
    void init();

    QString networkErrorToString(QNetworkReply::NetworkError error);
    void addLocalFiles();
    void addRecentFiles();

    void writeLocalArchiveIndex(QByteArray const &data);
    QByteArray readLocalArchiveIndex() const;

    bool parseArchiveIndex(QByteArray const &xmlData, bool listLocalFilesOnly = false);

    QString d_archiveUrl;
    QSharedPointer<QNetworkAccessManager> d_accessManager;
    QVector<ArchiveEntry> d_corpora;

};

inline ArchiveEntry const &ArchiveModel::entryAtRow(int row) const
{
    Q_ASSERT(row >= 0 && row < d_corpora.size());

    return d_corpora.at(row);
}

#endif // DOWNLOADWINDOW_H
<commit_msg>Make EntryType a scoped enumeration.<commit_after>#ifndef ARCHIVEMODEL_H
#define ARCHIVEMODEL_H

#include <QAbstractTableModel>
#include <QModelIndex>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QSharedPointer>
#include <QString>
#include <QUrl>
#include <QVariant>
#include <QVector>

enum class EntryType {
    Local,
    Downloaded,
    Downloadable
};

struct ArchiveEntry {
    QString name;
    QString path;
    QString url;
    size_t sentences;
    double size;
    QString description;
    QString longDescription;
    QString checksum;
    EntryType type;

    QString filePath() const;
    bool existsLocally() const;
};

class ArchiveModel : public QAbstractTableModel
{
    Q_OBJECT
public:
    ArchiveModel(QObject *parent = 0);
    ArchiveModel(QString const &archiveUrl, QObject *parent = 0);
    QVariant data(QModelIndex const &index, int role = Qt::DisplayRole) const;
    int columnCount(QModelIndex const &parent = QModelIndex()) const;
    ArchiveEntry const &entryAtRow(int row) const;
    QVariant headerData(int column, Qt::Orientation orientation,
        int role) const;
    void refresh();
    int rowCount(QModelIndex const &parent = QModelIndex()) const;
    void setUrl(QString const &archiveUrl);

    void deleteLocalFiles(QModelIndex const &index);

private slots:
    void replyFinished(QNetworkReply *reply);

signals:
    void networkError(QString error);
    void processingError(QString error);
    void retrieving();
    void retrievalFinished();

private:
    void init();

    QString networkErrorToString(QNetworkReply::NetworkError error);
    void addLocalFiles();
    void addRecentFiles();

    void writeLocalArchiveIndex(QByteArray const &data);
    QByteArray readLocalArchiveIndex() const;

    bool parseArchiveIndex(QByteArray const &xmlData, bool listLocalFilesOnly = false);

    QString d_archiveUrl;
    QSharedPointer<QNetworkAccessManager> d_accessManager;
    QVector<ArchiveEntry> d_corpora;

};

inline ArchiveEntry const &ArchiveModel::entryAtRow(int row) const
{
    Q_ASSERT(row >= 0 && row < d_corpora.size());

    return d_corpora.at(row);
}

#endif // DOWNLOADWINDOW_H
<|endoftext|>