<commit_before>#include "router.h"
#include "service.h"
#include "lambda_service.h"
#include <string>
#include <iostream>
#include <algorithm>

namespace REST {
  workers_services_vector Router::workers_services;
  Router* Router::pInstance = NULL;
  lambda_patterns* Router::patterns = new lambda_patterns();
  int Router::WORKERS = 256;
  std::shared_ptr<Router::Node> Router::root = std::make_shared<Router::Node>();

  Router::Router() {
    workers_services.resize(WORKERS);

    for (int i = 0; i < WORKERS; i++)
      workers_services[i] = std::make_shared<names_services_map>();
  }

  Router* Router::Instance() {
    if(pInstance == NULL){
      pInstance = new Router();
    }
    return pInstance;
  }


  std::shared_ptr<Service> Router::getResource(std::shared_ptr<Request> request, int worker_id) {
    std::shared_ptr<Node> node = match(request->path, request->parameters);

    if (node == nullptr)
      return NULL;

    return node->find_service(worker_id);
  }

  void Router::path(std::string const& path, service_lambda lambda) {
    std::shared_ptr<Router::Node> node = Router::Node::from_path(path);
    node->end()->add_service(std::make_shared<LambdaService>(lambda));
    root->merge(node);
  }

  Router::Node::Node(std::string p, std::shared_ptr<Node> const& pr) : path(p), parent(pr) {
  }

  Router::Node::~Node() {
    children.clear();
  }

  std::shared_ptr<Service> Router::Node::find_service(int worker_id) {
    return service[worker_id];
  }

  std::string Router::Node::uri() {
    std::string address;
    std::shared_ptr<Node> previous = shared_from_this();

    while (previous != nullptr) {
      address = (previous->is_root() ? "" : "/" + previous->path) + address;
      previous = previous->parent;
    }
    return address;
  }

  std::shared_ptr<Router::Node> Router::Node::start() {
    std::shared_ptr<Node> previous = shared_from_this();

    while (true) {
      if (previous->parent == nullptr)
        return previous;
      else
        previous = previous->parent;
    }
  }

  std::shared_ptr<Router::Node> Router::Node::end() {
    std::shared_ptr<Node> current = shared_from_this();

    while (true) {
      if (current->is_last())
        return current;
      else
        current = current->next();
    }
  }

  bool Router::Node::is_last() {
    return children.empty();
  }

  std::shared_ptr<Router::Node> Router::Node::next() {
    return *(children.begin());
  }

  void Router::Node::print(int level) {
    std::cout << std::string(level*2, ' ') << "/" << path << std::endl;
    for (auto next : children)
      next->print(level+1);
  }

  bool Router::Node::merge(std::shared_ptr<Router::Node> const path) {
    if (Router::Node::equal(path, shared_from_this())) {
      std::vector< std::shared_ptr<Node> > common_paths(path->children.size());
      auto irfit = std::set_intersection(path->children.begin(), path->children.end(), children.begin(), children.end(), common_paths.begin(), Router::Node::less);
      common_paths.resize(irfit - common_paths.begin());

      for (auto next : common_paths) {
        auto common = std::find_if(children.begin(), children.end(), [&next](const std::shared_ptr<Node> c) { return Router::Node::equal(next, c); });

        (*common)->merge(next);
      }<commit_msg>root path is working too<commit_after>#include "router.h"
#include "service.h"
#include "lambda_service.h"
#include <string>
#include <iostream>
#include <algorithm>

namespace REST {
  workers_services_vector Router::workers_services;
  Router* Router::pInstance = NULL;
  lambda_patterns* Router::patterns = new lambda_patterns();
  int Router::WORKERS = 256;
  std::shared_ptr<Router::Node> Router::root = std::make_shared<Router::Node>();

  Router::Router() {
    workers_services.resize(WORKERS);

    for (int i = 0; i < WORKERS; i++)
      workers_services[i] = std::make_shared<names_services_map>();
  }

  Router* Router::Instance() {
    if(pInstance == NULL){
      pInstance = new Router();
    }
    return pInstance;
  }


  std::shared_ptr<Service> Router::getResource(std::shared_ptr<Request> request, int worker_id) {
    std::shared_ptr<Node> node = match(request->path, request->parameters);

    if (node == nullptr)
      return NULL;

    std::shared_ptr<Service> service = node->find_service(worker_id);

    node.reset();

    return service;
  }

  void Router::path(std::string const& path, service_lambda lambda) {
    std::shared_ptr<Router::Node> node = Router::Node::from_path(path);
    node->end()->add_service(std::make_shared<LambdaService>(lambda));
    root->merge(node);

    if (node->end()->is_root()) {
      std::cout << "to jes root\n";
    }
  }

  Router::Node::Node(std::string p, std::shared_ptr<Node> const& pr) : path(p), parent(pr) {
  }

  Router::Node::~Node() {
    children.clear();
  }

  std::shared_ptr<Service> Router::Node::find_service(int worker_id) {
    return service[worker_id];
  }

  std::string Router::Node::uri() {
    std::string address;
    std::shared_ptr<Node> previous = shared_from_this();

    while (previous != nullptr) {
      address = (previous->is_root() ? "" : "/" + previous->path) + address;
      previous = previous->parent;
    }
    return address;
  }

  std::shared_ptr<Router::Node> Router::Node::start() {
    std::shared_ptr<Node> previous = shared_from_this();

    while (true) {
      if (previous->parent == nullptr)
        return previous;
      else
        previous = previous->parent;
    }
  }

  std::shared_ptr<Router::Node> Router::Node::end() {
    std::shared_ptr<Node> current = shared_from_this();

    while (true) {
      if (current->is_last())
        return current;
      else
        current = current->next();
    }
  }

  bool Router::Node::is_last() {
    return children.empty();
  }

  std::shared_ptr<Router::Node> Router::Node::next() {
    return *(children.begin());
  }

  void Router::Node::print(int level) {
    std::cout << std::string(level*2, ' ') << "/" << path << std::endl;
    for (auto next : children)
      next->print(level+1);
  }

  bool Router::Node::merge(std::shared_ptr<Router::Node> const path) {
    if (Router::Node::equal(path, shared_from_this())) {
      if (path->service.size() > 0) {
        service = path->service;
        std::cout << "kopiuje service\n";
      }

      std::vector< std::shared_ptr<Node> > common_paths(path->children.size());
      auto irfit = std::set_intersection(path->children.begin(), path->children.end(), children.begin(), children.end(), common_paths.begin(), Router::Node::less);
      common_paths.resize(irfit - common_paths.begin());

      for (auto next : common_paths) {
        auto common = std::find_if(children.begin(), children.end(), [&next](const std::shared_ptr<Node> c) { return Router::Node::equal(next, c); });

        (*common)->merge(next);
      }
{'fraction_non_alphanumeric': 0.17421806489330605, 'fraction_numerical': 0.001900029231218942, 'mean_word_length': 3.8948497854077253, 'pattern_counts': {'":': 0, '<': 72, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2182634', 'n_tokens_mistral': 2278, 'n_tokens_neox': 2205, 'n_words': 406}