/*******************************************************************************
 *
 * File:     Abstraction.hpp
 *
 * Contents: Definition of Abstraction Module.
 *
 * Copyright 2017
 *
 * Author: David Juhasz (david.juhasz@tuwien.ac.at)
 *
 ******************************************************************************/

#ifndef ROSA_AGENT_ABSTRACTION_HPP
#define ROSA_AGENT_ABSTRACTION_HPP

#include "rosa/agent/Module.h"

#include "rosa/support/debug.hpp"

#include <algorithm>
#include <map>

namespace rosa {
namespace agent {

// Abstracts values from type T to type A.
template <typename T, typename A> class Abstraction : public Module {
protected:
  // Value to be returned if by default.
  const A Default;

public:
  // Ctor.
  Abstraction(const A Default) noexcept : Default(Default) {}

  // Dtor.
  ~Abstraction(void) = default;

  // Abstracts the given value of type T into a value of type A.
  virtual A operator()(const T &) const noexcept { return Default; }
};

// Implements Abstraction as a map from T to A. This implementation is supposed
// to be used to abstract between enumeration types.
template <typename T, typename A>
class MapAbstraction : public Abstraction<T, A>, private std::map<T, A> {
  // Make sure the actual type arguments are enumerations.
  STATIC_ASSERT((std::is_enum<T>::value && std::is_enum<A>::value),
                "mapping not enumerations");

  // Bringing into scope inherited members.
  using Abstraction<T, A>::Default;
  using std::map<T, A>::end;
  using std::map<T, A>::find;

public:
  // Ctor. Initializes the mapping.
  MapAbstraction(const std::map<T, A> &Map, const A Default) noexcept
      : Abstraction<T, A>(Default),
        std::map<T, A>(Map) {}

  // Dtor.
  ~MapAbstraction(void) = default;

  // Gives a value of type A associated by the underlying map to V of type T,
  // or Default if no mapping is defined for V.
  A operator()(const T &V) const noexcept override {
    const auto I = find(V);
    return I == end() ? Default : *I;
  }

};

// Implements Abstraction as a map from ranges of T to A. This implementation
// is supposed to be used to abstract ranges of arithmetic types into
// enumerations.
// INV: The keys in the underlying map define valid ranges (first <= second)
// and there are no overlapping ranges defined by the keys.
template <typename T, typename A>
class RangeAbstraction : public Abstraction<T, A>,
                         private std::map<std::pair<T, T>, A> {
  // Make sure the actual type arguments are matching our expectations.
  STATIC_ASSERT((std::is_arithmetic<T>::value), "abstracting not arithmetic");
  STATIC_ASSERT((std::is_enum<A>::value), "abstracting not to enumeration");

  // Bringing into scope inherited members.
  using Abstraction<T, A>::Default;
  using std::map<std::pair<T, T>, A>::begin;
  using std::map<std::pair<T, T>, A>::end;
  using std::map<std::pair<T, T>, A>::find;

public:
  // Ctor. Initializes and validates the mapping.
  // PRE: Each key defines a valid range (first <= second) and there are no
  // overlapping ranges defined by the keys.
  RangeAbstraction(const std::map<std::pair<T, T>, A> &Map, const A &Default)
      : Abstraction<T, A>(Default), std::map<std::pair<T, T>, A>(Map) {
    // Sanity check.
    ASSERT(std::all_of(
        begin(), end(), [this](const std::pair<std::pair<T, T>, A> &P) {
          return P.first.first <= P.first.second &&
                 std::all_of(++find(P.first), end(),
                             [&P](const std::pair<std::pair<T, T>, A> &R) {
                               // NOTE: Values in Map are sorted.
                               return P.first.first < P.first.second &&
                                          P.first.second <= R.first.first ||
                                      P.first.first == P.first.second &&
                                          P.first.second < R.first.first;
                             });
        }));
  }

  // Dtor.
  ~RangeAbstraction(void) = default;

  // Gives a value of type A associated by the underlying map to V of type T,
  // or Default if no range containing V is defined.
  A operator()(const T &V) const noexcept override {
    auto I = begin();
    bool found = false; // Indicates if I refers to a matching range.
    bool failed = false; // Indicates if it is pointless to continue searching.
    while (!found && !failed && I != end()) {
      if (V < I->first.first) {
        // No match so far and V is below the next range, never will match.
        // NOTE: Keys are sorted in the map.
        failed = true;
      } else if (I->first.first <= V && V < I->first.second) {
        // Matching range found.
        found = true;
      } else {
        // Cannot conclude in this step, move to the next range.
        ++I;
      }
    }
    ASSERT(!found || I != end());
    return found ? I->second : Default;
  }

};

} // End namespace agent
} // End namespace rosa

#endif // ROSA_AGENT_ABSTRACTION_HPP

