<commit_before>#include "framework.hpp"

#include "../indexer/classificator.hpp"
#include "../indexer/feature_visibility.hpp"

namespace
{
  class FeatureInfoT
  {
    double m_dist;

  public:
    FeatureInfoT(double d, feature::TypesHolder & types, string & name, m2::PointD const & pt)
      : m_dist(d), m_types(types), m_pt(pt)
    {
      m_name.swap(name);
    }

    bool operator<(FeatureInfoT const & rhs) const
    {
      return (m_dist < rhs.m_dist);
    }

    void Swap(FeatureInfoT & rhs)
    {
      swap(m_dist, rhs.m_dist);
      m_name.swap(rhs.m_name);
      swap(m_types, rhs.m_types);
    }

    string m_name;
    feature::TypesHolder m_types;
    m2::PointD m_pt;
  };

  void swap(FeatureInfoT & i1, FeatureInfoT & i2)
  {
    i1.Swap(i2);
  }

  class DoGetFeatureInfoBase
  {
  protected:
    virtual double GetResultDistance(double d, feature::TypesHolder const & types) const = 0;
    virtual double NeedProcess(feature::TypesHolder const & types) const
    {
      pair<int, int> const r = feature::GetDrawableScaleRange(types);
      return my::between_s(r.first, r.second, m_scale);
    }

    static double GetCompareEpsilonImpl(feature::EGeomType type, double eps)
    {
      using namespace feature;
      switch (type)
      {
      case GEOM_POINT: return 0.0 * eps;
      case GEOM_LINE: return 1.0 * eps;
      case GEOM_AREA: return 2.0 * eps;
      default:
        ASSERT ( false, () );
        return numeric_limits<double>::max();
      }
    }

  public:
    DoGetFeatureInfoBase(m2::PointD const & pt, double eps, int scale)
      : m_pt(pt), m_scale(scale), m_eps(eps)
    {
      m_coastType = classif().GetCoastType();
    }

    void operator() (FeatureType const & f)
    {
      feature::TypesHolder types(f);
      if (!types.Has(m_coastType) && NeedProcess(types))
      {
        double const d = f.GetDistance(m_pt, m_scale);
        ASSERT_GREATER_OR_EQUAL(d, 0.0, ());

        if (d <= m_eps)
        {
          string defName, intName;
          f.GetPreferredDrawableNames(defName, intName);

          // if geom type is not GEOM_POINT, result center point doesn't matter in future use
          m2::PointD const pt = (types.GetGeoType() == feature::GEOM_POINT) ?
                f.GetCenter() : m2::PointD();

          m_cont.push_back(FeatureInfoT(GetResultDistance(d, types), types, defName, pt));
        }
      }
    }

    void SortResults()
    {
      sort(m_cont.begin(), m_cont.end());
    }

  private:
    m2::PointD m_pt;
    int m_scale;
    uint32_t m_coastType;

  protected:
    double m_eps;
    vector<FeatureInfoT> m_cont;
  };

  class DoGetFeatureTypes : public DoGetFeatureInfoBase
  {
  protected:
    virtual double GetResultDistance(double d, feature::TypesHolder const & types) const
    {
      return (d + GetCompareEpsilonImpl(types.GetGeoType(), m_eps));
    }

  public:
    DoGetFeatureTypes(m2::PointD const & pt, double eps, int scale)
      : DoGetFeatureInfoBase(pt, eps, scale)
    {
    }

    void GetFeatureTypes(size_t count, vector<string> & types)
    {
      SortResults();

      Classificator const & c = classif();

      for (size_t i = 0; i < min(count, m_cont.size()); ++i)
        for (size_t j = 0; j < m_cont[i].m_types.Size(); ++j)
          types.push_back(c.GetFullObjectName(m_cont[i].m_types[j]));
    }
  };
}

void Framework::GetFeatureTypes(m2::PointD pt, vector<string> & types) const
{
  pt = m_navigator.ShiftPoint(pt);

  int const sm = 20;
  m2::RectD pixR(m2::PointD(pt.x - sm, pt.y - sm), m2::PointD(pt.x + sm, pt.y + sm));

  m2::RectD glbR;
  m_navigator.Screen().PtoG(pixR, glbR);

  int const scale = GetDrawScale();
  DoGetFeatureTypes getTypes(m_navigator.Screen().PtoG(pt),
                             max(glbR.SizeX(), glbR.SizeY()) / 2.0,
                             scale);

  m_model.ForEachFeature(glbR, getTypes, scale);

  getTypes.GetFeatureTypes(5, types);
}

namespace
{
  class DoGetAddressInfo : public DoGetFeatureInfoBase
  {
  public:
    class TypeChecker
    {
      vector<uint32_t> m_localities, m_streets, m_buildings;

      template <size_t count, size_t ind>
      void FillMatch(char const * (& arr)[count][ind], vector<uint32_t> & vec)
      {
        STATIC_ASSERT ( count > 0 );
        STATIC_ASSERT ( ind > 0 );

        Classificator const & c = classif();

        vec.reserve(count);
        for (size_t i = 0; i < count; ++i)
        {
          vector<string> v(arr[i], arr[i] + ind);
          vec.push_back(c.GetTypeByPath(v));
        }
      }

      static bool IsMatchImpl(vector<uint32_t> const & vec, feature::TypesHolder const & types)
      {
        for (size_t i = 0; i < types.Size(); ++i)
        {
          uint32_t type = types[i];
          ftype::TruncValue(type, 2);

          if (find(vec.begin(), vec.end(), type) != vec.end())
            return true;
        }

        return false;
      }

    public:
      TypeChecker()
      {
        char const * arrLocalities[][2] = {
          { "place", "city" },
          { "place", "town" },
          { "place", "village" },
          { "place", "hamlet" }
        };

        char const * arrStreet[][2] = {
          { "highway", "primary" },
          { "highway", "secondary" },
          { "highway", "residential" },
          { "highway", "tertiary" },
          { "highway", "living_street" },
          { "highway", "service" }
        };

        char const * arrBuilding[][1] = {
          { "building" }
        };

        FillMatch(arrLocalities, m_localities);
        FillMatch(arrStreet, m_streets);
        FillMatch(arrBuilding, m_buildings);
      }

      bool IsLocality(feature::TypesHolder const & types) const
      {
        return IsMatchImpl(m_localities, types);
      }
      bool IsStreet(feature::TypesHolder const & types) const
      {
        return IsMatchImpl(m_streets, types);
      }
      bool IsBuilding(feature::TypesHolder const & types) const
      {
        return IsMatchImpl(m_buildings, types);
      }

      double GetLocalityDivideFactor(feature::TypesHolder const & types) const
      {
        double arrF[] = { 10.0, 10.0, 1.0, 1.0 };
        ASSERT_EQUAL ( ARRAY_SIZE(arrF), m_localities.size(), () );

        for (size_t i = 0; i < types.Size(); ++i)
        {
          uint32_t type = types[i];
          ftype::TruncValue(type, 2);

          vector<uint32_t>::const_iterator j = find(m_localities.begin(), m_localities.end(), type);
          if (j != m_localities.end())
            return arrF[distance(m_localities.begin(), j)];
        }

        return 1.0;
      }
    };

    TypeChecker const & m_checker;
    bool m_doLocalities;

  protected:
    virtual double GetResultDistance(double d, feature::TypesHolder const & types) const
    {
      if (!m_doLocalities)
        return (d + GetCompareEpsilonImpl(types.GetGeoType(), 5.0 * MercatorBounds::degreeInMetres));
      else
      {
        // This routine is needed for quality of locality prediction.
        // Hamlet may be the nearest point, but it's a part of a City. So use the divide factor
        // for distance, according to feature type.
        return (d / m_checker.GetLocalityDivideFactor(types));
      }
    }
    virtual double NeedProcess(feature::TypesHolder const & types) const
    {
      if (!DoGetFeatureInfoBase::NeedProcess(types))
        return false;

      return (!m_doLocalities ||
              (types.GetGeoType() == feature::GEOM_POINT && m_checker.IsLocality(types)));
    }

  public:
    DoGetAddressInfo(m2::PointD const & pt, double eps, int scale,
                     TypeChecker const & checker)
      : DoGetFeatureInfoBase(pt, eps, scale),
        m_checker(checker), m_doLocalities(false)
    {
    }

    void PrepareForLocalities()
    {
      m_doLocalities = true;
      m_cont.clear();
    }

    void FillAddress(Framework::AddressInfo & info)
    {
      SortResults();

      for (size_t i = 0; i < m_cont.size(); ++i)
      {
        bool const isStreet = m_checker.IsStreet(m_cont[i].m_types);
        //bool const isBuilding = m_checker.IsBuilding(m_cont[i].m_types);

        if (info.m_street.empty() && isStreet)
          info.m_street = m_cont[i].m_name;

        //if (info.m_house.empty() && isBuilding)
        //  info.m_house = m_cont[i].m_house;

        if (info.m_name.empty())
        {
          if (m_cont[i].m_types.GetGeoType() != feature::GEOM_LINE)
            info.m_name = m_cont[i].m_name;
        }

        if (!(info.m_street.empty() || info.m_name.empty()))
          break;
      }
    }

    void FillLocality(Framework::AddressInfo & info, Framework const & fm)
    {
      SortResults();

      for (size_t i = 0; i < m_cont.size(); ++i)
      {
        if (!m_cont[i].m_name.empty() && fm.GetCountryName(m_cont[i].m_pt) == info.m_country)
        {
          info.m_city = m_cont[i].m_name;
          break;
        }
      }
    }
  };
}

void Framework::GetAddressInfo(m2::PointD const & pt, AddressInfo & info) const
{
  info.m_country = GetCountryName(pt);
  if (info.m_country.empty())
  {
    LOG(LINFO, ("Can't find region for point ", pt));
    return;
  }

  int const scale = scales::GetUpperScale();
  double const addressR = 200.0;
  double const localityR = 20000;

  static DoGetAddressInfo::TypeChecker checker;

  // first of all - get an address
  {
    DoGetAddressInfo getAddress(pt, addressR, scale, checker);

    m_model.ForEachFeature(
          MercatorBounds::RectByCenterXYAndSizeInMeters(pt, addressR), getAddress, scale);

    getAddress.FillAddress(info);
  }

  // now - get the locality
  {
    DoGetAddressInfo getLocality(pt, localityR, scale, checker);
    getLocality.PrepareForLocalities();

    m_model.ForEachFeature(
          MercatorBounds::RectByCenterXYAndSizeInMeters(pt, localityR), getLocality, scale);

    getLocality.FillLocality(info, *this);
  }
}
<commit_msg>Fix bug with locality detection (do not check on scale visibility).<commit_after>#include "framework.hpp"

#include "../indexer/classificator.hpp"
#include "../indexer/feature_visibility.hpp"

namespace
{
  class FeatureInfoT
  {
  public:
    FeatureInfoT(double d, feature::TypesHolder & types, string & name, m2::PointD const & pt)
      : m_types(types), m_pt(pt), m_dist(d)
    {
      m_name.swap(name);
    }

    bool operator<(FeatureInfoT const & rhs) const
    {
      return (m_dist < rhs.m_dist);
    }

    void Swap(FeatureInfoT & rhs)
    {
      swap(m_dist, rhs.m_dist);
      m_name.swap(rhs.m_name);
      swap(m_types, rhs.m_types);
    }

    string m_name;
    feature::TypesHolder m_types;
    m2::PointD m_pt;
    double m_dist;
  };

  void swap(FeatureInfoT & i1, FeatureInfoT & i2)
  {
    i1.Swap(i2);
  }

  string DebugPrint(FeatureInfoT const & info)
  {
    return ("Name = " + info.m_name + " Distance = " + strings::to_string(info.m_dist));
  }

  class DoGetFeatureInfoBase
  {
  protected:
    virtual double GetResultDistance(double d, feature::TypesHolder const & types) const = 0;
    virtual double NeedProcess(feature::TypesHolder const & types) const
    {
      pair<int, int> const r = feature::GetDrawableScaleRange(types);
      return my::between_s(r.first, r.second, m_scale);
    }

    static double GetCompareEpsilonImpl(feature::EGeomType type, double eps)
    {
      using namespace feature;
      switch (type)
      {
      case GEOM_POINT: return 0.0 * eps;
      case GEOM_LINE: return 1.0 * eps;
      case GEOM_AREA: return 2.0 * eps;
      default:
        ASSERT ( false, () );
        return numeric_limits<double>::max();
      }
    }

  public:
    DoGetFeatureInfoBase(m2::PointD const & pt, double eps, int scale)
      : m_pt(pt), m_scale(scale), m_eps(eps)
    {
      m_coastType = classif().GetCoastType();
    }

    void operator() (FeatureType const & f)
    {
      feature::TypesHolder types(f);
      if (!types.Has(m_coastType) && NeedProcess(types))
      {
        double const d = f.GetDistance(m_pt, m_scale);
        ASSERT_GREATER_OR_EQUAL(d, 0.0, ());

        if (d <= m_eps)
        {
          string defName, intName;
          f.GetPreferredDrawableNames(defName, intName);

          // if geom type is not GEOM_POINT, result center point doesn't matter in future use
          m2::PointD const pt = (types.GetGeoType() == feature::GEOM_POINT) ?
                f.GetCenter() : m2::PointD();

          m_cont.push_back(FeatureInfoT(GetResultDistance(d, types), types, defName, pt));
        }
      }
    }

    void SortResults()
    {
      sort(m_cont.begin(), m_cont.end());
    }

  private:
    m2::PointD m_pt;
    int m_scale;
    uint32_t m_coastType;

  protected:
    double m_eps;
    vector<FeatureInfoT> m_cont;
  };

  class DoGetFeatureTypes : public DoGetFeatureInfoBase
  {
  protected:
    virtual double GetResultDistance(double d, feature::TypesHolder const & types) const
    {
      return (d + GetCompareEpsilonImpl(types.GetGeoType(), m_eps));
    }

  public:
    DoGetFeatureTypes(m2::PointD const & pt, double eps, int scale)
      : DoGetFeatureInfoBase(pt, eps, scale)
    {
    }

    void GetFeatureTypes(size_t count, vector<string> & types)
    {
      SortResults();

      Classificator const & c = classif();

      for (size_t i = 0; i < min(count, m_cont.size()); ++i)
        for (size_t j = 0; j < m_cont[i].m_types.Size(); ++j)
          types.push_back(c.GetFullObjectName(m_cont[i].m_types[j]));
    }
  };
}

void Framework::GetFeatureTypes(m2::PointD pt, vector<string> & types) const
{
  pt = m_navigator.ShiftPoint(pt);

  int const sm = 20;
  m2::RectD pixR(m2::PointD(pt.x - sm, pt.y - sm), m2::PointD(pt.x + sm, pt.y + sm));

  m2::RectD glbR;
  m_navigator.Screen().PtoG(pixR, glbR);

  int const scale = GetDrawScale();
  DoGetFeatureTypes getTypes(m_navigator.Screen().PtoG(pt),
                             max(glbR.SizeX(), glbR.SizeY()) / 2.0,
                             scale);

  m_model.ForEachFeature(glbR, getTypes, scale);

  getTypes.GetFeatureTypes(5, types);
}

namespace
{
  class DoGetAddressInfo : public DoGetFeatureInfoBase
  {
  public:
    class TypeChecker
    {
      vector<uint32_t> m_localities, m_streets, m_buildings;

      template <size_t count, size_t ind>
      void FillMatch(char const * (& arr)[count][ind], vector<uint32_t> & vec)
      {
        STATIC_ASSERT ( count > 0 );
        STATIC_ASSERT ( ind > 0 );

        Classificator const & c = classif();

        vec.reserve(count);
        for (size_t i = 0; i < count; ++i)
        {
          vector<string> v(arr[i], arr[i] + ind);
          vec.push_back(c.GetTypeByPath(v));
        }
      }

      static bool IsMatchImpl(vector<uint32_t> const & vec, feature::TypesHolder const & types)
      {
        for (size_t i = 0; i < types.Size(); ++i)
        {
          uint32_t type = types[i];
          ftype::TruncValue(type, 2);

          if (find(vec.begin(), vec.end(), type) != vec.end())
            return true;
        }

        return false;
      }

    public:
      TypeChecker()
      {
        char const * arrLocalities[][2] = {
          { "place", "city" },
          { "place", "town" },
          { "place", "village" },
          { "place", "hamlet" }
        };

        char const * arrStreet[][2] = {
          { "highway", "primary" },
          { "highway", "secondary" },
          { "highway", "residential" },
          { "highway", "tertiary" },
          { "highway", "living_street" },
          { "highway", "service" }
        };

        char const * arrBuilding[][1] = {
          { "building" }
        };

        FillMatch(arrLocalities, m_localities);
        FillMatch(arrStreet, m_streets);
        FillMatch(arrBuilding, m_buildings);
      }

      bool IsLocality(feature::TypesHolder const & types) const
      {
        return IsMatchImpl(m_localities, types);
      }
      bool IsStreet(feature::TypesHolder const & types) const
      {
        return IsMatchImpl(m_streets, types);
      }
      bool IsBuilding(feature::TypesHolder const & types) const
      {
        return IsMatchImpl(m_buildings, types);
      }

      double GetLocalityDivideFactor(feature::TypesHolder const & types) const
      {
        double arrF[] = { 10.0, 10.0, 1.0, 1.0 };
        ASSERT_EQUAL ( ARRAY_SIZE(arrF), m_localities.size(), () );

        for (size_t i = 0; i < types.Size(); ++i)
        {
          uint32_t type = types[i];
          ftype::TruncValue(type, 2);

          vector<uint32_t>::const_iterator j = find(m_localities.begin(), m_localities.end(), type);
          if (j != m_localities.end())
            return arrF[distance(m_localities.begin(), j)];
        }

        return 1.0;
      }
    };

    TypeChecker const & m_checker;
    bool m_doLocalities;

  protected:
    virtual double GetResultDistance(double d, feature::TypesHolder const & types) const
    {
      if (!m_doLocalities)
        return (d + GetCompareEpsilonImpl(types.GetGeoType(), 5.0 * MercatorBounds::degreeInMetres));
      else
      {
        // This routine is needed for quality of locality prediction.
        // Hamlet may be the nearest point, but it's a part of a City. So use the divide factor
        // for distance, according to feature type.
        return (d / m_checker.GetLocalityDivideFactor(types));
      }
    }
    virtual double NeedProcess(feature::TypesHolder const & types) const
    {
      if (m_doLocalities)
        return (types.GetGeoType() == feature::GEOM_POINT && m_checker.IsLocality(types));
      else
        return DoGetFeatureInfoBase::NeedProcess(types);
    }

  public:
    DoGetAddressInfo(m2::PointD const & pt, double eps, int scale,
                     TypeChecker const & checker)
      : DoGetFeatureInfoBase(pt, eps, scale),
        m_checker(checker), m_doLocalities(false)
    {
    }

    void PrepareForLocalities()
    {
      m_doLocalities = true;
      m_cont.clear();
    }

    void FillAddress(Framework::AddressInfo & info)
    {
      SortResults();

      for (size_t i = 0; i < m_cont.size(); ++i)
      {
        bool const isStreet = m_checker.IsStreet(m_cont[i].m_types);
        //bool const isBuilding = m_checker.IsBuilding(m_cont[i].m_types);

        if (info.m_street.empty() && isStreet)
          info.m_street = m_cont[i].m_name;

        //if (info.m_house.empty() && isBuilding)
        //  info.m_house = m_cont[i].m_house;

        if (info.m_name.empty())
        {
          if (m_cont[i].m_types.GetGeoType() != feature::GEOM_LINE)
            info.m_name = m_cont[i].m_name;
        }

        if (!(info.m_street.empty() || info.m_name.empty()))
          break;
      }
    }

    void FillLocality(Framework::AddressInfo & info, Framework const & fm)
    {
      SortResults();
      //LOG(LDEBUG, (m_cont));

      for (size_t i = 0; i < m_cont.size(); ++i)
      {
        if (!m_cont[i].m_name.empty() && fm.GetCountryName(m_cont[i].m_pt) == info.m_country)
        {
          info.m_city = m_cont[i].m_name;
          break;
        }
      }
    }
  };
}

void Framework::GetAddressInfo(m2::PointD const & pt, AddressInfo & info) const
{
  info.m_country = GetCountryName(pt);
  if (info.m_country.empty())
  {
    LOG(LINFO, ("Can't find region for point ", pt));
    return;
  }

  int const scale = scales::GetUpperScale();
  double const addressR = 200.0;
  double const localityR = 20000;

  static DoGetAddressInfo::TypeChecker checker;

  // first of all - get an address
  {
    DoGetAddressInfo getAddress(pt, addressR, scale, checker);

    m_model.ForEachFeature(
          MercatorBounds::RectByCenterXYAndSizeInMeters(pt, addressR), getAddress, scale);

    getAddress.FillAddress(info);
  }

  // now - get the locality
  {
    DoGetAddressInfo getLocality(pt, localityR, scale, checker);
    getLocality.PrepareForLocalities();

    m_model.ForEachFeature(
          MercatorBounds::RectByCenterXYAndSizeInMeters(pt, localityR), getLocality, scale);

    getLocality.FillLocality(info, *this);
  }
}
<|endoftext|>


<commit_before>/*****************************************************************************
 *
 * This file is part of Mapnik (c++ mapping toolkit)
 *
 * Copyright (C) 2006 Artem Pavlenko, Jean-Francois Doyon
 *
 * This library 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 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *****************************************************************************/
//$Id: mapnik_parameters.cpp 17 2005-03-08 23:58:43Z pavlenko $

#include <boost/python.hpp>
#include <boost/python/detail/api_placeholder.hpp>
#include <mapnik/params.hpp>

using mapnik::parameter;
using mapnik::parameters;

struct parameter_pickle_suite : boost::python::pickle_suite
{
    static boost::python::tuple
    getinitargs(const parameter& p)
    {
        using namespace boost::python;
        return boost::python::make_tuple(p.first,p.second);
    }
};

struct parameters_pickle_suite : boost::python::pickle_suite
{
    static boost::python::tuple
    getstate(const parameters& p)
    {
        using namespace boost::python;
        dict d;
        parameters::const_iterator pos=p.begin();
        while(pos!=p.end())
        {
            d[pos->first]=pos->second;
            ++pos;
        }
        return boost::python::make_tuple(d);
    }

    static void setstate(parameters& p, boost::python::tuple state)
    {
        using namespace boost::python;
        if (len(state) != 1)
        {
            PyErr_SetObject(PyExc_ValueError,
			    ("expected 1-item tuple in call to __setstate__; got %s"
			     % state).ptr()
			    );
            throw_error_already_set();
        }
        dict d = extract<dict>(state[0]);
        boost::python::list keys=d.keys();
        for (int i=0;i<len(keys);++i)
        {
            std::string key=extract<std::string>(keys[i]);
            std::string value=extract<std::string>(d[key]); // FIXME: use boost::variant as a value object. Add Py_Int/Py_Float/Py_String extractors.
            p[key] = value;
        }
    }
};

void export_parameters()
{
    using namespace boost::python;
    class_<parameter>("Parameter",init<std::string,std::string>())
        .def_pickle(parameter_pickle_suite())
        ;

    class_<parameters>("Parameters",init<>())
        .def_pickle(parameters_pickle_suite())
        ;
}
<commit_msg>+expose parameters as python list and dict, and add pickle support by taking advantage of boost visitation - needs further review - see #345<commit_after>/*****************************************************************************
 *
 * This file is part of Mapnik (c++ mapping toolkit)
 *
 * Copyright (C) 2006 Artem Pavlenko, Jean-Francois Doyon
 *
 * This library 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 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *****************************************************************************/
//$Id: mapnik_parameters.cpp 17 2005-03-08 23:58:43Z pavlenko $

// boost
#include <boost/python.hpp>

// mapnik
#include <mapnik/params.hpp>

using mapnik::parameter;
using mapnik::parameters;

struct pickle_value : public boost::static_visitor<>
{
    public:
        pickle_value( boost::python::list vals):
        vals_(vals) {}

        void operator () ( int val )
        {
            vals_.append(val);
        }

        void operator () ( double val )
        {
            vals_.append(val);
        }

        void operator () ( std::string val )
        {
            vals_.append(val);
        }

    private:
        boost::python::list vals_;

};

struct parameter_pickle_suite : boost::python::pickle_suite
{
    static boost::python::tuple
    getinitargs(const parameter& p)
    {
        using namespace boost::python;
        return boost::python::make_tuple(p.first,boost::get<std::string>(p.second));
    }
};

struct parameters_pickle_suite : boost::python::pickle_suite
{
    static boost::python::tuple
    getstate(const parameters& p)
    {
        using namespace boost::python;
        dict d;
        parameters::const_iterator pos=p.begin();
        while(pos!=p.end())
        {
            boost::python::list vals;
            pickle_value serializer( vals );
            mapnik::value_holder val = pos->second;
            boost::apply_visitor( serializer, val );
            d[pos->first] = vals[0];
            ++pos;
        }
        return boost::python::make_tuple(d);
    }

    static void setstate(parameters& p, boost::python::tuple state)
    {
        using namespace boost::python;
        if (len(state) != 1)
        {
            PyErr_SetObject(PyExc_ValueError,
			    ("expected 1-item tuple in call to __setstate__; got %s"
			     % state).ptr()
			    );
            throw_error_already_set();
        }

        dict d = extract<dict>(state[0]);
        boost::python::list keys = d.keys();
        for (int i=0; i<len(keys); ++i)
        {
            std::string key = extract<std::string>(keys[i]);
            object obj = d[key];
            extract<std::string> ex0(obj);
            extract<int> ex1(obj);
            extract<double> ex2(obj);

            if (ex0.check())
            {
               p[key] = ex0();
            }
            else if (ex1.check())
            {
               p[key] = ex1();
            }
            else if (ex2.check())
            {
               p[key] = ex2();
            }

            /*
            extract_value serializer( p, key );
            mapnik::value_holder val = extract<mapnik::value_holder>(d[key]);
            boost::apply_visitor( serializer, val );
            */
        }
    }
};

boost::python::dict dict_params(parameters& p)
{
    boost::python::dict d;
    parameters::const_iterator pos=p.begin();
    while(pos!=p.end())
    {
        boost::python::list vals;
        pickle_value serializer( vals );
        mapnik::value_holder val = pos->second;
        boost::apply_visitor( serializer, val );
        d[pos->first] = vals[0];
        ++pos;
    }
    return d;
}

boost::python::list list_params(parameters& p)
{
    boost::python::list l;
    parameters::const_iterator pos=p.begin();
    while(pos!=p.end())
    {
        boost::python::list vals;
        pickle_value serializer( vals );
        mapnik::value_holder val = pos->second;
        boost::apply_visitor( serializer, val );
        l.append(boost::python::make_tuple(pos->first,vals[0]));
        ++pos;
    }
    return l;
}

boost::python::dict dict_param(parameter& p)
{
    boost::python::dict d;
    d[p.first] = boost::get<std::string>(p.second);
    return d;
}

boost::python::tuple tuple_param(parameter& p)
{
    return boost::python::make_tuple(p.first,boost::get<std::string>(p.second));
}

void export_parameters()
{
    using namespace boost::python;
    class_<parameter>("Parameter",init<std::string,std::string>())
        .def_pickle(parameter_pickle_suite())
        .def("as_dict",dict_param)
        .def("as_tuple",tuple_param)
        ;

    class_<parameters>("Parameters",init<>())
        .def_pickle(parameters_pickle_suite())
        .def("as_dict",dict_params)
        .def("as_list",list_params)
        ;
}<|endoftext|>


<commit_before>/*ckwg +5
 * Copyright 2011 by Kitware, Inc. All Rights Reserved. Please refer to
 * KITWARE_LICENSE.TXT for licensing information, or contact General Counsel,
 * Kitware, Inc., 28 Corporate Drive, Clifton Park, NY 12065.
 */

#include "layered_image_reader_process.h"

#include <processes/helpers/image/format.h>
#include <processes/helpers/image/read.h>

#include <vistk/utilities/path.h>

#include <vistk/pipeline/config.h>
#include <vistk/pipeline/datum.h>
#include <vistk/pipeline/process_exception.h>
#include <vistk/pipeline/stamp.h>

#include <boost/algorithm/string/predicate.hpp>
#include <boost/foreach.hpp>
#include <boost/format.hpp>
#include <boost/make_shared.hpp>

#include <string>

/**
 * \file layered_image_reader_process.cxx
 *
 * \brief Implementation of the layered image reader process.
 */

namespace vistk
{

class layered_image_reader_process::priv
{
  public:
    priv(config::value_t const& fmt, uint64_t offset_, read_func_t func, port_type_t const& port_type);
    ~priv();

    typedef boost::basic_format<config::value_t::value_type> format_t;

    typedef port_t layer_t;
    typedef std::vector<layer_t> layers_t;

    format_t format;
    read_func_t const read;
    port_type_t const port_type_output;

    layers_t layers;
    uint64_t frame;

    static config::key_t const config_pixtype;
    static config::key_t const config_pixfmt;
    static config::key_t const config_format;
    static config::key_t const config_offset;
    static config::value_t const default_pixtype;
    static config::value_t const default_pixfmt;
    static config::value_t const default_format;
    static config::value_t const default_offset;
    static port_t const port_image_prefix;
};

config::key_t const layered_image_reader_process::priv::config_pixtype = config::key_t("pixtype");
config::key_t const layered_image_reader_process::priv::config_pixfmt = config::key_t("pixfmt");
config::key_t const layered_image_reader_process::priv::config_format = config::key_t("format");
config::key_t const layered_image_reader_process::priv::config_offset = config::key_t("offset");
config::value_t const layered_image_reader_process::priv::default_pixtype = config::value_t(pixtypes::pixtype_byte());
config::value_t const layered_image_reader_process::priv::default_pixfmt = config::value_t(pixfmts::pixfmt_rgb());
config::value_t const layered_image_reader_process::priv::default_format = config::value_t("image-%1%-%2%.png");
config::value_t const layered_image_reader_process::priv::default_offset = config::value_t("0");
process::port_t const layered_image_reader_process::priv::port_image_prefix = process::port_t("image/");

layered_image_reader_process
::layered_image_reader_process(config_t const& config)
  : process(config)
{
  /// \todo There should probably be timestamp input rather than manually keeping track of the frame.

  declare_configuration_key(priv::config_pixtype, boost::make_shared<conf_info>(
    priv::default_pixtype,
    config::description_t("The pixel type of the input images.")));
  declare_configuration_key(priv::config_pixfmt, boost::make_shared<conf_info>(
    priv::default_pixfmt,
    config::description_t("The pixel format of the input images.")));
  declare_configuration_key(priv::config_format, boost::make_shared<conf_info>(
    priv::default_format,
    config::description_t("The format string for the input layers.")));
  declare_configuration_key(priv::config_offset, boost::make_shared<conf_info>(
    priv::default_offset,
    config::description_t("The frame number to start reading.")));
}

layered_image_reader_process
::~layered_image_reader_process()
{
}

void
layered_image_reader_process
::_init()
{
  // Configure the process.
  {
    pixtype_t const pixtype = config_value<pixtype_t>(priv::config_pixtype);
    pixfmt_t const pixfmt = config_value<pixfmt_t>(priv::config_pixfmt);
    config::value_t const format = config_value<config::value_t>(priv::config_format);
    uint64_t const offset = config_value<uint64_t>(priv::config_offset);

    port_type_t const port_type = port_type_for_pixtype(pixtype, pixfmt);
    read_func_t const func = read_for_pixtype(pixtype);

    d.reset(new priv(format, offset, func, port_type));
  }

  if (!d->read)
  {
    static std::string const reason = "A read function for the "
                                      "given pixtype could not be found";

    throw invalid_configuration_exception(name(), reason);
  }

  process::_init();
}

void
layered_image_reader_process
::_step()
{
  BOOST_FOREACH (priv::layer_t const& layer, d->layers)
  {
    d->format.clear();

    try
    {
      d->format % layer;
      d->format % d->frame;
    }
    catch (boost::io::format_error&)
    {
    }

    path_t const path = boost::str(d->format);

    datum_t const dat = d->read(path);

    push_datum_to_port(priv::port_image_prefix + layer, dat);
  }

  ++d->frame;

  process::_step();
}

process::port_info_t
layered_image_reader_process
::_output_port_info(port_t const& port)
{
  if (boost::starts_with(port, priv::port_image_prefix))
  {
    priv::layer_t const layer = port.substr(priv::port_image_prefix.size());

    priv::layers_t::const_iterator const i = std::find(d->layers.begin(), d->layers.end(), layer);

    if (i == d->layers.end())
    {
      d->layers.push_back(layer);

      port_flags_t required;

      required.insert(flag_required);

      declare_output_port(port, boost::make_shared<port_info>(
        d->port_type_output,
        required,
        port_description_t("The \'" + layer + "\' layer of the image.")));
    }
  }

  return process::_output_port_info(port);
}

layered_image_reader_process::priv
::priv(config::value_t const& fmt, uint64_t offset_, read_func_t func, port_type_t const& port_type)
  : format(fmt)
  , read(func)
  , port_type_output(port_type)
  , frame(offset_)
{
}

layered_image_reader_process::priv
::~priv()
{
}

}
<commit_msg>Get a timestamp to know what mask to read<commit_after>/*ckwg +5
 * Copyright 2011 by Kitware, Inc. All Rights Reserved. Please refer to
 * KITWARE_LICENSE.TXT for licensing information, or contact General Counsel,
 * Kitware, Inc., 28 Corporate Drive, Clifton Park, NY 12065.
 */

#include "layered_image_reader_process.h"

#include <processes/helpers/image/format.h>
#include <processes/helpers/image/read.h>

#include <vistk/utilities/timestamp.h>
#include <vistk/utilities/path.h>

#include <vistk/pipeline/config.h>
#include <vistk/pipeline/datum.h>
#include <vistk/pipeline/process_exception.h>
#include <vistk/pipeline/stamp.h>

#include <boost/algorithm/string/predicate.hpp>
#include <boost/foreach.hpp>
#include <boost/format.hpp>
#include <boost/make_shared.hpp>

#include <string>

/**
 * \file layered_image_reader_process.cxx
 *
 * \brief Implementation of the layered image reader process.
 */

namespace vistk
{

class layered_image_reader_process::priv
{
  public:
    priv(config::value_t const& fmt, read_func_t func, port_type_t const& port_type);
    ~priv();

    typedef boost::basic_format<config::value_t::value_type> format_t;

    typedef port_t layer_t;
    typedef std::vector<layer_t> layers_t;

    format_t format;
    read_func_t const read;
    port_type_t const port_type_output;

    layers_t layers;

    static config::key_t const config_pixtype;
    static config::key_t const config_pixfmt;
    static config::key_t const config_format;
    static config::value_t const default_pixtype;
    static config::value_t const default_pixfmt;
    static config::value_t const default_format;
    static port_t const port_timestamp;
    static port_t const port_image_prefix;
};

config::key_t const layered_image_reader_process::priv::config_pixtype = config::key_t("pixtype");
config::key_t const layered_image_reader_process::priv::config_pixfmt = config::key_t("pixfmt");
config::key_t const layered_image_reader_process::priv::config_format = config::key_t("format");
config::value_t const layered_image_reader_process::priv::default_pixtype = config::value_t(pixtypes::pixtype_byte());
config::value_t const layered_image_reader_process::priv::default_pixfmt = config::value_t(pixfmts::pixfmt_rgb());
config::value_t const layered_image_reader_process::priv::default_format = config::value_t("image-%1%-%2%.png");
process::port_t const layered_image_reader_process::priv::port_timestamp = process::port_t("timestamp");
process::port_t const layered_image_reader_process::priv::port_image_prefix = process::port_t("image/");

layered_image_reader_process
::layered_image_reader_process(config_t const& config)
  : process(config)
{
  /// \todo There should probably be timestamp input rather than manually keeping track of the frame.

  declare_configuration_key(priv::config_pixtype, boost::make_shared<conf_info>(
    priv::default_pixtype,
    config::description_t("The pixel type of the input images.")));
  declare_configuration_key(priv::config_pixfmt, boost::make_shared<conf_info>(
    priv::default_pixfmt,
    config::description_t("The pixel format of the input images.")));
  declare_configuration_key(priv::config_format, boost::make_shared<conf_info>(
    priv::default_format,
    config::description_t("The format string for the input layers.")));

  port_flags_t required;

  required.insert(flag_required);

  declare_input_port(priv::port_timestamp, boost::make_shared<port_info>(
    "timestamp",
    required,
    port_description_t("The timestamp to use when reading masks.")));
}

layered_image_reader_process
::~layered_image_reader_process()
{
}

void
layered_image_reader_process
::_init()
{
  // Configure the process.
  {
    pixtype_t const pixtype = config_value<pixtype_t>(priv::config_pixtype);
    pixfmt_t const pixfmt = config_value<pixfmt_t>(priv::config_pixfmt);
    config::value_t const format = config_value<config::value_t>(priv::config_format);

    port_type_t const port_type = port_type_for_pixtype(pixtype, pixfmt);
    read_func_t const func = read_for_pixtype(pixtype);

    d.reset(new priv(format, func, port_type));
  }

  if (!d->read)
  {
    static std::string const reason = "A read function for the "
                                      "given pixtype could not be found";

    throw invalid_configuration_exception(name(), reason);
  }

  process::_init();
}

void
layered_image_reader_process
::_step()
{
  timestamp const ts = grab_from_port_as<timestamp>(priv::port_timestamp);

  BOOST_FOREACH (priv::layer_t const& layer, d->layers)
  {
    d->format.clear();

    try
    {
      d->format % layer;
      d->format % ts.frame();
    }
    catch (boost::io::format_error&)
    {
    }

    path_t const path = boost::str(d->format);

    datum_t const dat = d->read(path);

    push_datum_to_port(priv::port_image_prefix + layer, dat);
  }

  process::_step();
}

process::port_info_t
layered_image_reader_process
::_output_port_info(port_t const& port)
{
  if (boost::starts_with(port, priv::port_image_prefix))
  {
    priv::layer_t const layer = port.substr(priv::port_image_prefix.size());

    priv::layers_t::const_iterator const i = std::find(d->layers.begin(), d->layers.end(), layer);

    if (i == d->layers.end())
    {
      d->layers.push_back(layer);

      port_flags_t required;

      required.insert(flag_required);

      declare_output_port(port, boost::make_shared<port_info>(
        d->port_type_output,
        required,
        port_description_t("The \'" + layer + "\' layer of the image.")));
    }
  }

  return process::_output_port_info(port);
}

layered_image_reader_process::priv
::priv(config::value_t const& fmt, read_func_t func, port_type_t const& port_type)
  : format(fmt)
  , read(func)
  , port_type_output(port_type)
{
}

layered_image_reader_process::priv
::~priv()
{
}

}
<|endoftext|>


<commit_before>#include "BleuDocScorer.h"

#include <algorithm>
#include <cassert>
#include <cmath>
#include <climits>
#include <fstream>
#include <iostream>
#include <stdexcept>

#include "util/check.hh"
#include "Ngram.h"
#include "Reference.h"
#include "Util.h"
#include "Vocabulary.h"

using namespace std;

namespace
{

// configure regularisation
const char KEY_REFLEN[] = "reflen";
const char REFLEN_AVERAGE[] = "average";
const char REFLEN_SHORTEST[] = "shortest";
const char REFLEN_CLOSEST[] = "closest";

} // namespace

namespace MosesTuning
{

BleuDocScorer::BleuDocScorer(const string& config)
    : BleuScorer("BLEUDOC", config),
      m_ref_length_type(CLOSEST)
{
  const string reflen = getConfig(KEY_REFLEN, REFLEN_CLOSEST);
  if (reflen == REFLEN_AVERAGE) {
    m_ref_length_type = AVERAGE;
  } else if (reflen == REFLEN_SHORTEST) {
    m_ref_length_type = SHORTEST;
  } else if (reflen == REFLEN_CLOSEST) {
    m_ref_length_type = CLOSEST;
  } else {
    throw runtime_error("Unknown reference length strategy: " + reflen);
  }
}

BleuDocScorer::~BleuDocScorer() {}

bool BleuDocScorer::OpenReferenceStream(istream* is, size_t file_id)
{
  if (is == NULL) return false;

  string line;
  size_t doc_id = -1;
  size_t sid = 0;
  while (getline(*is, line)) {

    if (line.find("<doc docid") != std::string::npos) {  // new document
      doc_id++;
      m_references.push_back(new ScopedVector<Reference>());
      sid = 0;
    }
    else if (line.find("<seg") != std::string::npos) {  //new sentence
      int start = line.find_first_of('>') + 1;
      std::string trans = line.substr(start, line.find_last_of('<')-start);
      trans = preprocessSentence(trans);

      if (file_id == 0) {
		  Reference* ref = new Reference;
		  m_references[doc_id]->push_back(ref);    // Take ownership of the Reference object.
      }

      if (m_references[doc_id]->size() <= sid) {
      	return false;
      }
      NgramCounts counts;
      size_t length = CountNgrams(trans, counts, kBleuNgramOrder);

      //for any counts larger than those already there, merge them in
      for (NgramCounts::const_iterator ci = counts.begin(); ci != counts.end(); ++ci) {
		  const NgramCounts::Key& ngram = ci->first;
		  const NgramCounts::Value newcount = ci->second;

		  NgramCounts::Value oldcount = 0;
		  m_references[doc_id]->get().at(sid)->get_counts()->Lookup(ngram, &oldcount);
		  if (newcount > oldcount) {
			  m_references[doc_id]->get().at(sid)->get_counts()->operator[](ngram) = newcount;
		  }
      }
      //add in the length

	  m_references[doc_id]->get().at(sid)->push_back(length);
      if (sid > 0 && sid % 100 == 0) {
		  TRACE_ERR(".");
      }
      ++sid;
    }
  }
  return true;
}

void BleuDocScorer::prepareStats(size_t sid, const string& text, ScoreStats& entry)
{
  if (sid >= m_references.size()) {
    stringstream msg;
    msg << "Sentence id (" << sid << ") not found in reference set";
    throw runtime_error(msg.str());
  }

  std::vector<std::string> sentences = splitDoc(text);

  vector<ScoreStatsType> totStats(kBleuNgramOrder * 2 + 1);

  for (uint i=0; i<sentences.size(); ++i) {

    NgramCounts testcounts;
    // stats for this line
    vector<ScoreStatsType> stats(kBleuNgramOrder * 2);
    string sentence = preprocessSentence(sentences[i]);
    const size_t length = CountNgrams(sentence, testcounts, kBleuNgramOrder);

    //precision on each ngram type
    for (NgramCounts::const_iterator testcounts_it = testcounts.begin();
	 testcounts_it != testcounts.end(); ++testcounts_it) {
      const NgramCounts::Value guess = testcounts_it->second;
      const size_t len = testcounts_it->first.size();
      NgramCounts::Value correct = 0;

      NgramCounts::Value v = 0;
      if (m_references[sid]->get().at(i)->get_counts()->Lookup(testcounts_it->first, &v)) {
	correct = min(v, guess);
      }
      stats[len * 2 - 2] += correct;
      stats[len * 2 - 1] += guess;
    }

    const int reference_len = CalcReferenceLength(sid, i, length);
    stats.push_back(reference_len);

    //ADD stats to totStats
    std::transform(stats.begin(), stats.end(), totStats.begin(),
		   totStats.begin(), std::plus<int>());
  }
  entry.set(totStats);
}

std::vector<std::string> BleuDocScorer::splitDoc(const std::string& text)
{
  std::vector<std::string> res;

  uint index = 0;
  std::string::size_type end;

  while ((end = text.find(" \\n ", index)) != std::string::npos) {
    res.push_back(text.substr(index,end-index));
    index = end + 4;
  }
  return res;
}

statscore_t BleuDocScorer::calculateScore(const vector<int>& comps) const
{
  CHECK(comps.size() == kBleuNgramOrder * 2 + 1);

  float logbleu = 0.0;
  for (int i = 0; i < kBleuNgramOrder; ++i) {
    if (comps[2*i] == 0) {
      return 0.0;
    }
    logbleu += log(comps[2*i]) - log(comps[2*i+1]);

  }
  logbleu /= kBleuNgramOrder;
  // reflength divided by test length
  const float brevity = 1.0 - static_cast<float>(comps[kBleuNgramOrder * 2]) / comps[1];
  if (brevity < 0.0) {
    logbleu += brevity;
  }
  return exp(logbleu);
}

int BleuDocScorer::CalcReferenceLength(size_t doc_id, size_t sentence_id, size_t length)
{
  switch (m_ref_length_type) {
    case AVERAGE:
      return m_references[doc_id]->get().at(sentence_id)->CalcAverage();
      break;
    case CLOSEST:
      return m_references[doc_id]->get().at(sentence_id)->CalcClosest(length);
      break;
    case SHORTEST:
      return m_references[doc_id]->get().at(sentence_id)->CalcShortest();
      break;
    default:
      cerr << "unknown reference types." << endl;
      exit(1);
  }
}

}

<commit_msg>mac compile<commit_after>#include "BleuDocScorer.h"

#include <sys/types.h>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <climits>
#include <fstream>
#include <iostream>
#include <stdexcept>

#include "util/check.hh"
#include "Ngram.h"
#include "Reference.h"
#include "Util.h"
#include "Vocabulary.h"

using namespace std;

namespace
{

// configure regularisation
const char KEY_REFLEN[] = "reflen";
const char REFLEN_AVERAGE[] = "average";
const char REFLEN_SHORTEST[] = "shortest";
const char REFLEN_CLOSEST[] = "closest";

} // namespace

namespace MosesTuning
{

BleuDocScorer::BleuDocScorer(const string& config)
    : BleuScorer("BLEUDOC", config),
      m_ref_length_type(CLOSEST)
{
  const string reflen = getConfig(KEY_REFLEN, REFLEN_CLOSEST);
  if (reflen == REFLEN_AVERAGE) {
    m_ref_length_type = AVERAGE;
  } else if (reflen == REFLEN_SHORTEST) {
    m_ref_length_type = SHORTEST;
  } else if (reflen == REFLEN_CLOSEST) {
    m_ref_length_type = CLOSEST;
  } else {
    throw runtime_error("Unknown reference length strategy: " + reflen);
  }
}

BleuDocScorer::~BleuDocScorer() {}

bool BleuDocScorer::OpenReferenceStream(istream* is, size_t file_id)
{
  if (is == NULL) return false;

  string line;
  size_t doc_id = -1;
  size_t sid = 0;
  while (getline(*is, line)) {

    if (line.find("<doc docid") != std::string::npos) {  // new document
      doc_id++;
      m_references.push_back(new ScopedVector<Reference>());
      sid = 0;
    }
    else if (line.find("<seg") != std::string::npos) {  //new sentence
      int start = line.find_first_of('>') + 1;
      std::string trans = line.substr(start, line.find_last_of('<')-start);
      trans = preprocessSentence(trans);

      if (file_id == 0) {
		  Reference* ref = new Reference;
		  m_references[doc_id]->push_back(ref);    // Take ownership of the Reference object.
      }

      if (m_references[doc_id]->size() <= sid) {
      	return false;
      }
      NgramCounts counts;
      size_t length = CountNgrams(trans, counts, kBleuNgramOrder);

      //for any counts larger than those already there, merge them in
      for (NgramCounts::const_iterator ci = counts.begin(); ci != counts.end(); ++ci) {
		  const NgramCounts::Key& ngram = ci->first;
		  const NgramCounts::Value newcount = ci->second;

		  NgramCounts::Value oldcount = 0;
		  m_references[doc_id]->get().at(sid)->get_counts()->Lookup(ngram, &oldcount);
		  if (newcount > oldcount) {
			  m_references[doc_id]->get().at(sid)->get_counts()->operator[](ngram) = newcount;
		  }
      }
      //add in the length

	  m_references[doc_id]->get().at(sid)->push_back(length);
      if (sid > 0 && sid % 100 == 0) {
		  TRACE_ERR(".");
      }
      ++sid;
    }
  }
  return true;
}

void BleuDocScorer::prepareStats(size_t sid, const string& text, ScoreStats& entry)
{
  if (sid >= m_references.size()) {
    stringstream msg;
    msg << "Sentence id (" << sid << ") not found in reference set";
    throw runtime_error(msg.str());
  }

  std::vector<std::string> sentences = splitDoc(text);

  vector<ScoreStatsType> totStats(kBleuNgramOrder * 2 + 1);

  for (uint i=0; i<sentences.size(); ++i) {

    NgramCounts testcounts;
    // stats for this line
    vector<ScoreStatsType> stats(kBleuNgramOrder * 2);
    string sentence = preprocessSentence(sentences[i]);
    const size_t length = CountNgrams(sentence, testcounts, kBleuNgramOrder);

    //precision on each ngram type
    for (NgramCounts::const_iterator testcounts_it = testcounts.begin();
	 testcounts_it != testcounts.end(); ++testcounts_it) {
      const NgramCounts::Value guess = testcounts_it->second;
      const size_t len = testcounts_it->first.size();
      NgramCounts::Value correct = 0;

      NgramCounts::Value v = 0;
      if (m_references[sid]->get().at(i)->get_counts()->Lookup(testcounts_it->first, &v)) {
	correct = min(v, guess);
      }
      stats[len * 2 - 2] += correct;
      stats[len * 2 - 1] += guess;
    }

    const int reference_len = CalcReferenceLength(sid, i, length);
    stats.push_back(reference_len);

    //ADD stats to totStats
    std::transform(stats.begin(), stats.end(), totStats.begin(),
		   totStats.begin(), std::plus<int>());
  }
  entry.set(totStats);
}

std::vector<std::string> BleuDocScorer::splitDoc(const std::string& text)
{
  std::vector<std::string> res;

  uint index = 0;
  std::string::size_type end;

  while ((end = text.find(" \\n ", index)) != std::string::npos) {
    res.push_back(text.substr(index,end-index));
    index = end + 4;
  }
  return res;
}

statscore_t BleuDocScorer::calculateScore(const vector<int>& comps) const
{
  CHECK(comps.size() == kBleuNgramOrder * 2 + 1);

  float logbleu = 0.0;
  for (int i = 0; i < kBleuNgramOrder; ++i) {
    if (comps[2*i] == 0) {
      return 0.0;
    }
    logbleu += log(comps[2*i]) - log(comps[2*i+1]);

  }
  logbleu /= kBleuNgramOrder;
  // reflength divided by test length
  const float brevity = 1.0 - static_cast<float>(comps[kBleuNgramOrder * 2]) / comps[1];
  if (brevity < 0.0) {
    logbleu += brevity;
  }
  return exp(logbleu);
}

int BleuDocScorer::CalcReferenceLength(size_t doc_id, size_t sentence_id, size_t length)
{
  switch (m_ref_length_type) {
    case AVERAGE:
      return m_references[doc_id]->get().at(sentence_id)->CalcAverage();
      break;
    case CLOSEST:
      return m_references[doc_id]->get().at(sentence_id)->CalcClosest(length);
      break;
    case SHORTEST:
      return m_references[doc_id]->get().at(sentence_id)->CalcShortest();
      break;
    default:
      cerr << "unknown reference types." << endl;
      exit(1);
  }
}

}

<|endoftext|>


<commit_before>
<commit_msg>Delete 1.cpp<commit_after><|endoftext|>


<commit_before>// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2015-2016 Oslo and Akershus University College of Applied Sciences
// and Alfred Bratterud
//
// Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef MIDDLEWARE_PARSLEY_HPP
#define MIDDLEWARE_PARSLEY_HPP

#include "json.hpp"
#include "middleware.hpp"

namespace middleware {

/**
 * @brief A vegan way to parse JSON Content in a response
 * @details TBC..
 *
 */
class Parsley : public server::Middleware {
public:

  virtual void process(server::Request_ptr req, server::Response_ptr, server::Next next) override;

private:

  bool has_json(server::Request_ptr req) const;
};

inline void process(server::Request_ptr req, server::Response_ptr, server::Next next) {
  using namespace json;

  if(not has_json(req)) return (*next)();

  // Request doesn't have JSON attribute
  if(not req->has_attribute<JsonDoc>()) {
    // Create attribute
    auto json = std::make_shared<JsonDoc>();

    // Access the document and parse the body
    json->doc().Parse(req->get_body().c_str());
    printf("<Parsley> Parsed JSON data.\n");

    // Add the json attribute to the request
    req->set_attribute(json);
  }

  return (*next)();
}

inline bool has_json(server::Request_ptr req) const {
  auto c_type = http::header_fields::Entity::Content_Type;
  if(not req->has_header(c_type)) return false;
  return (req->header_value(c_type).find("application/json") != std::string::npos);
}

} //< namespace middleware

#endif
<commit_msg>Added API DOC comment blocks<commit_after>// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2015-2016 Oslo and Akershus University College of Applied Sciences
// and Alfred Bratterud
//
// Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef MIDDLEWARE_PARSLEY_HPP
#define MIDDLEWARE_PARSLEY_HPP

#include "json.hpp"
#include "middleware.hpp"

namespace middleware {

/**
 * @brief A vegan way to parse JSON Content in a response
 * @details TBC..
 *
 */
class Parsley : public server::Middleware {
public:

  /**
   *
   */
  virtual void process(server::Request_ptr req, server::Response_ptr, server::Next next) override;

private:

  /**
   *
   */
  bool has_json(server::Request_ptr req) const;
};

inline void process(server::Request_ptr req, server::Response_ptr, server::Next next) {
  using namespace json;

  if(not has_json(req)) return (*next)();

  // Request doesn't have JSON attribute
  if(not req->has_attribute<JsonDoc>()) {
    // Create attribute
    auto json = std::make_shared<JsonDoc>();

    // Access the document and parse the body
    json->doc().Parse(req->get_body().c_str());
    printf("<Parsley> Parsed JSON data.\n");

    // Add the json attribute to the request
    req->set_attribute(json);
  }

  return (*next)();
}

inline bool has_json(server::Request_ptr req) const {
  auto c_type = http::header_fields::Entity::Content_Type;
  if(not req->has_header(c_type)) return false;
  return (req->header_value(c_type).find("application/json") != std::string::npos);
}

} //< namespace middleware

#endif
<|endoftext|>


<commit_before>/*************************************************************************
 *
 *  $RCSfile: sbintern.hxx,v $
 *
 *  $Revision: 1.4 $
 *
 *  last change: $Author: vg $ $Date: 2004-01-06 19:41:45 $
 *
 *  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): _______________________________________
 *
 *
 ************************************************************************/

#ifndef _SB_INTERN_HXX
#define _SB_INTERN_HXX

#include <svtools/sbxfac.hxx>
#ifndef _UNOTOOLS_TRANSLITERATIONWRAPPER_HXX
#include <unotools/transliterationwrapper.hxx>
#endif
#include "sb.hxx"

class ::utl::TransliterationWrapper;
class SbUnoFactory;
class SbiInstance;
class SbModule;

class SbiFactory : public SbxFactory
{
public:
    virtual SbxBase* Create( UINT16 nSbxId, UINT32 = SBXCR_SBX );
    virtual SbxObject* CreateObject( const String& );
};

// Stack fuer die im Fehlerfall abgebaute SbiRuntime Kette
class SbErrorStackEntry
{
public:
    SbErrorStackEntry(SbMethodRef aM, xub_StrLen nL, xub_StrLen nC1, xub_StrLen nC2)
        : aMethod(aM), nLine(nL), nCol1(nC1), nCol2(nC2) {}
    SbMethodRef aMethod;
    xub_StrLen nLine;
    xub_StrLen nCol1, nCol2;
};

SV_DECL_PTRARR_DEL(SbErrorStack, SbErrorStackEntry*, 1, 1)

struct SbiGlobals
{
    SbiInstance*    pInst;          // alle aktiven Runtime-Instanzen
    SbiFactory*     pSbFac;         // StarBASIC-Factory
    SbUnoFactory*   pUnoFac;        // Factory fuer Uno-Structs bei DIM AS NEW
    SbModule*       pMod;           // aktuell aktives Modul
    SbModule*       pCompMod;       // aktuell compiliertes Modul
    short           nInst;          // Anzahl BASICs
    Link            aErrHdl;        // globaler Error-Handler
    Link            aBreakHdl;      // globaler Break-Handler
    SbError         nCode;          // aktueller Fehlercode
    xub_StrLen      nLine;          // aktuelle Zeile
    xub_StrLen      nCol1,nCol2;    // aktuelle Spalten (von,bis)
    BOOL            bCompiler;      // Flag fuer Compiler-Error
    BOOL            bGlobalInitErr; // Beim GlobalInit trat ein Compiler-Fehler auf
    BOOL            bRunInit;       // TRUE, wenn RunInit vom Basic aktiv ist
    String          aErrMsg;        // Puffer fuer GetErrorText()
    SbLanguageMode  eLanguageMode;  // Flag fuer Visual-Basic-Script-Modus
    SbErrorStack*   pErrStack;      // Stack fuer die im Fehlerfall abgebaute SbiRuntime Kette
    ::utl::TransliterationWrapper* pTransliterationWrapper;    // For StrComp
    BOOL            bBlockCompilerError;

    SbiGlobals();
    ~SbiGlobals();
};

// Utility-Makros und -Routinen

SbiGlobals* GetSbData();

#define pINST       GetSbData()->pInst
#define pMOD        GetSbData()->pMod
#define pCMOD       GetSbData()->pCompMod
#define pSBFAC      GetSbData()->pSbFac
#define pUNOFAC     GetSbData()->pUnoFac

#endif

<commit_msg>INTEGRATION: CWS jl5vba (1.4.6); FILE MERGED 2004/01/21 09:24:39 ab 1.4.6.1: #111934# Merge to src680, for tasks see message INTEGRATION: CWS ab02vba (1.3.22)<commit_after>/*************************************************************************
 *
 *  $RCSfile: sbintern.hxx,v $
 *
 *  $Revision: 1.5 $
 *
 *  last change: $Author: obo $ $Date: 2004-03-17 13:34:46 $
 *
 *  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): _______________________________________
 *
 *
 ************************************************************************/

#ifndef _SB_INTERN_HXX
#define _SB_INTERN_HXX

#include <svtools/sbxfac.hxx>
#ifndef _UNOTOOLS_TRANSLITERATIONWRAPPER_HXX
#include <unotools/transliterationwrapper.hxx>
#endif
#include "sb.hxx"

class ::utl::TransliterationWrapper;
class SbUnoFactory;
class SbTypeFactory;
class SbOLEFactory;
class SbiInstance;
class SbModule;

class SbiFactory : public SbxFactory
{
public:
    virtual SbxBase* Create( UINT16 nSbxId, UINT32 = SBXCR_SBX );
    virtual SbxObject* CreateObject( const String& );
};

// Stack fuer die im Fehlerfall abgebaute SbiRuntime Kette
class SbErrorStackEntry
{
public:
    SbErrorStackEntry(SbMethodRef aM, xub_StrLen nL, xub_StrLen nC1, xub_StrLen nC2)
        : aMethod(aM), nLine(nL), nCol1(nC1), nCol2(nC2) {}
    SbMethodRef aMethod;
    xub_StrLen nLine;
    xub_StrLen nCol1, nCol2;
};

SV_DECL_PTRARR_DEL(SbErrorStack, SbErrorStackEntry*, 1, 1)

struct SbiGlobals
{
    SbiInstance*    pInst;          // alle aktiven Runtime-Instanzen
    SbiFactory*     pSbFac;         // StarBASIC-Factory
    SbUnoFactory*   pUnoFac;        // Factory fuer Uno-Structs bei DIM AS NEW
    SbTypeFactory*  pTypeFac;       // Factory for user defined types
    SbOLEFactory*   pOLEFac;        // Factory for OLE types
    SbModule*       pMod;           // aktuell aktives Modul
    SbModule*       pCompMod;       // aktuell compiliertes Modul
    short           nInst;          // Anzahl BASICs
    Link            aErrHdl;        // globaler Error-Handler
    Link            aBreakHdl;      // globaler Break-Handler
    SbError         nCode;          // aktueller Fehlercode
    xub_StrLen      nLine;          // aktuelle Zeile
    xub_StrLen      nCol1,nCol2;    // aktuelle Spalten (von,bis)
    BOOL            bCompiler;      // Flag fuer Compiler-Error
    BOOL            bGlobalInitErr; // Beim GlobalInit trat ein Compiler-Fehler auf
    BOOL            bRunInit;       // TRUE, wenn RunInit vom Basic aktiv ist
    String          aErrMsg;        // Puffer fuer GetErrorText()
    SbLanguageMode  eLanguageMode;  // Flag fuer Visual-Basic-Script-Modus
    SbErrorStack*   pErrStack;      // Stack fuer die im Fehlerfall abgebaute SbiRuntime Kette
    ::utl::TransliterationWrapper* pTransliterationWrapper;    // For StrComp
    BOOL            bBlockCompilerError;

    SbiGlobals();
    ~SbiGlobals();
};

// Utility-Makros und -Routinen

SbiGlobals* GetSbData();

#define pINST       GetSbData()->pInst
#define pMOD        GetSbData()->pMod
#define pCMOD       GetSbData()->pCompMod
#define pSBFAC      GetSbData()->pSbFac
#define pUNOFAC     GetSbData()->pUnoFac
#define pTYPEFAC    GetSbData()->pTypeFac
#define pOLEFAC     GetSbData()->pOLEFac

#endif

<|endoftext|>


<commit_before>#include <cassert>
#include <iostream>
#include <memory>

#include "sodium.h"

#include "samplePacket.hpp"
#include "stateMachine.hpp"

union tsc_t {
	uint64_t tsc_64;
	struct {
		uint32_t lo_32;
		uint32_t hi_32;
	};
};

inline static uint64_t read_rdtsc() {
	union tsc_t tsc;
	asm volatile("RDTSC\n\t"
				 "mov %%edx, %0\n\t"
				 "mov %%eax, %1\n\t"
				 : "=r"(tsc.hi_32), "=r"(tsc.lo_32)::"%rax", "%rbx", "%rcx", "%rdx");
	return tsc.tsc_64;
}

using namespace std;

class Identifier;
using SM = StateMachine<Identifier, SamplePacket>;

// This is a very elemental Identifier class
class Identifier {
public:
	struct ConnectionID {
		uint64_t val;

		bool operator==(const ConnectionID &c) const { return val == c.val; }

		bool operator<(const ConnectionID &c) const { return val < c.val; }

		operator std::string() const { return ""; }

		ConnectionID(const ConnectionID &c) : val(c.val){};
		ConnectionID() : val(0){};
	};

	struct Hasher {
		size_t operator()(const ConnectionID &id) const {
			uint64_t res;
			assert(crypto_shorthash_BYTES == 8);
			uint8_t key[crypto_shorthash_KEYBYTES];
			memset(key, 0, crypto_shorthash_KEYBYTES);
			crypto_shorthash(reinterpret_cast<uint8_t *>(&res),
				reinterpret_cast<const uint8_t *>(&id.val), sizeof(id.val), key);

			return res;
		}
	};

	static ConnectionID identify(SamplePacket *pkt) {
		ConnectionID id;
		id.val = (uint64_t)pkt->getData();
		return id;
	};
};

// This just mallocs one SamplePacket
SamplePacket *getPkt() {
	uint32_t dataLen = 64;
	void *data = malloc(dataLen);
	SamplePacket *p = new SamplePacket(data, dataLen);
	return p;
}

// This is the function for state 1
void fun1(SM::State &state, SamplePacket *pktIn, SM::FunIface &fi) {

	DEBUG_ENABLED(cout << endl << "running fun1" << endl;)

	// Sanity check -> this should never to true
	DEBUG_ENABLED(if (state.state != 1) { cout << "fun1: state not 1" << endl; })

	// if pktIn == 2 -> transition to state 2 and fill packet with 12
	if (*(reinterpret_cast<uint32_t *>(pktIn->getData())) == 2) {
		DEBUG_ENABLED(cout << "fun1: transision to state 2" << endl;)
		fi.transition(2);
		*(reinterpret_cast<uint32_t *>(pktIn->getData())) = 12;
		return;
	}

	// if pktIn == 3 -> transition to state 3 and fill packet with 13
	if (*(reinterpret_cast<uint32_t *>(pktIn->getData())) == 3) {
		DEBUG_ENABLED(cout << "fun1: transision to state 3" << endl;)
		fi.transition(3);
		*(reinterpret_cast<uint32_t *>(pktIn->getData())) = 13;
		return;
	}

	DEBUG_ENABLED(cout << "fun1: no transision" << endl;)

	*(reinterpret_cast<uint32_t *>(pktIn->getData())) = 10;
}

// This is the function for state 2
// Pretty much the same as state 1...
void fun2(SM::State &state, SamplePacket *pktIn, SM::FunIface &fi) {
	DEBUG_ENABLED(cout << endl << "running fun2" << endl;)

	DEBUG_ENABLED(if (state.state != 2) { cout << "fun2: state not 2" << endl; })

	if (*(reinterpret_cast<uint32_t *>(pktIn->getData())) == 3) {
		DEBUG_ENABLED(cout << "fun2: transision to state 3" << endl;)
		fi.transition(3);
		*(reinterpret_cast<uint32_t *>(pktIn->getData())) = 23;
		return;
	}

	DEBUG_ENABLED(cout << "fun2: no transision" << endl;)

	*(reinterpret_cast<uint32_t *>(pktIn->getData())) = 24;
}

void usage(std::string progName) {
	std::cout << "Usage: " << progName << " <Num States>" << std::endl;
	std::exit(0);
}

int main(int argc, char **argv) {

	if (argc < 2) {
		usage(std::string(argv[0]));
	}

	unsigned int numStates = atoi(argv[1]);
	uint64_t startTimer, stopTimer;

	try {

		SM sm;

		// All incoming connections start at state 1, and don't require preparation
		sm.registerStartStateID(1, nullptr);

		// Every connection is state 3 should be deleted
		sm.registerEndStateID(3);

		// Register the functions to handle packets for these states
		sm.registerFunction(1, fun1);
		sm.registerFunction(2, fun2);

		// Setup sanity check
		assert(sm.getStateTableSize() == 0);

		// Prepare the packet array
		SamplePacket **spArray =
			reinterpret_cast<SamplePacket **>(malloc(numStates * sizeof(void *)));
		for (unsigned int i = 0; i < numStates; i++) {
			spArray[i] = getPkt();
		}

		// Prepare one BufArray
		BufArray<SamplePacket> pktsIn(spArray, numStates);

		// Set data in input packets to 0
		for (unsigned int i = 0; i < numStates; i++) {
			*(reinterpret_cast<uint32_t *>(pktsIn[i]->getData())) = 0;
		}

		// Run the packet through the state machine
		startTimer = read_rdtsc();
		sm.runPktBatch(pktsIn);
		stopTimer = read_rdtsc();
		std::cout << "Insertion: " << stopTimer - startTimer << std::endl;

		// Run packets through SM
		startTimer = read_rdtsc();
		sm.runPktBatch(pktsIn);
		stopTimer = read_rdtsc();
		std::cout << "Run: " << stopTimer - startTimer << std::endl;

	} catch (exception *e) {
		// Just catch whatever fails there may be
		cout << endl << "FATAL:" << endl;
		cout << e->what() << endl;

		return 1;
	}

	return 0;
}
<commit_msg>fix benchmarks/stateTableSize.cpp for sparsehash<commit_after>#include <cassert>
#include <iostream>
#include <limits>
#include <memory>

#include "sodium.h"

#include "samplePacket.hpp"
#include "stateMachine.hpp"

union tsc_t {
	uint64_t tsc_64;
	struct {
		uint32_t lo_32;
		uint32_t hi_32;
	};
};

inline static uint64_t read_rdtsc() {
	union tsc_t tsc;
	asm volatile("RDTSC\n\t"
				 "mov %%edx, %0\n\t"
				 "mov %%eax, %1\n\t"
				 : "=r"(tsc.hi_32), "=r"(tsc.lo_32)::"%rax", "%rbx", "%rcx", "%rdx");
	return tsc.tsc_64;
}

using namespace std;

class Identifier;
using SM = StateMachine<Identifier, SamplePacket>;

// This is a very elemental Identifier class
class Identifier {
public:
	struct ConnectionID {
		uint64_t val;

		bool operator==(const ConnectionID &c) const { return val == c.val; }

		bool operator<(const ConnectionID &c) const { return val < c.val; }

		operator std::string() const { return ""; }

		ConnectionID(const ConnectionID &c) : val(c.val){};
		ConnectionID() : val(0){};
		ConnectionID(uint64_t val) : val(val){};
	};

	static struct ConnectionID getDelKey() {
		return ConnectionID(std::numeric_limits<uint64_t>::max());
	};

	static struct ConnectionID getEmptyKey() {
		return ConnectionID(std::numeric_limits<uint64_t>::max() - 1);
	};

	struct Hasher {
		size_t operator()(const ConnectionID &id) const {
			uint64_t res;
			assert(crypto_shorthash_BYTES == 8);
			uint8_t key[crypto_shorthash_KEYBYTES];
			memset(key, 0, crypto_shorthash_KEYBYTES);
			crypto_shorthash(reinterpret_cast<uint8_t *>(&res),
				reinterpret_cast<const uint8_t *>(&id.val), sizeof(id.val), key);

			return res;
		}
	};

	static ConnectionID identify(SamplePacket *pkt) {
		ConnectionID id;
		id.val = (uint64_t)pkt->getData();
		return id;
	};
};

// This just mallocs one SamplePacket
SamplePacket *getPkt() {
	uint32_t dataLen = 64;
	void *data = malloc(dataLen);
	SamplePacket *p = new SamplePacket(data, dataLen);
	return p;
}

// This is the function for state 1
void fun1(SM::State &state, SamplePacket *pktIn, SM::FunIface &fi) {

	DEBUG_ENABLED(cout << endl << "running fun1" << endl;)

	// Sanity check -> this should never to true
	DEBUG_ENABLED(if (state.state != 1) { cout << "fun1: state not 1" << endl; })

	// if pktIn == 2 -> transition to state 2 and fill packet with 12
	if (*(reinterpret_cast<uint32_t *>(pktIn->getData())) == 2) {
		DEBUG_ENABLED(cout << "fun1: transision to state 2" << endl;)
		fi.transition(2);
		*(reinterpret_cast<uint32_t *>(pktIn->getData())) = 12;
		return;
	}

	// if pktIn == 3 -> transition to state 3 and fill packet with 13
	if (*(reinterpret_cast<uint32_t *>(pktIn->getData())) == 3) {
		DEBUG_ENABLED(cout << "fun1: transision to state 3" << endl;)
		fi.transition(3);
		*(reinterpret_cast<uint32_t *>(pktIn->getData())) = 13;
		return;
	}

	DEBUG_ENABLED(cout << "fun1: no transision" << endl;)

	*(reinterpret_cast<uint32_t *>(pktIn->getData())) = 10;
}

// This is the function for state 2
// Pretty much the same as state 1...
void fun2(SM::State &state, SamplePacket *pktIn, SM::FunIface &fi) {
	DEBUG_ENABLED(cout << endl << "running fun2" << endl;)

	DEBUG_ENABLED(if (state.state != 2) { cout << "fun2: state not 2" << endl; })

	if (*(reinterpret_cast<uint32_t *>(pktIn->getData())) == 3) {
		DEBUG_ENABLED(cout << "fun2: transision to state 3" << endl;)
		fi.transition(3);
		*(reinterpret_cast<uint32_t *>(pktIn->getData())) = 23;
		return;
	}

	DEBUG_ENABLED(cout << "fun2: no transision" << endl;)

	*(reinterpret_cast<uint32_t *>(pktIn->getData())) = 24;
}

void usage(std::string progName) {
	std::cout << "Usage: " << progName << " <Num States>" << std::endl;
	std::exit(0);
}

int main(int argc, char **argv) {

	if (argc < 2) {
		usage(std::string(argv[0]));
	}

	unsigned int numStates = atoi(argv[1]);
	uint64_t startTimer, stopTimer;

	try {

		SM sm;

		// All incoming connections start at state 1, and don't require preparation
		sm.registerStartStateID(1, nullptr);

		// Every connection is state 3 should be deleted
		sm.registerEndStateID(3);

		// Register the functions to handle packets for these states
		sm.registerFunction(1, fun1);
		sm.registerFunction(2, fun2);

		// Setup sanity check
		assert(sm.getStateTableSize() == 0);

		// Prepare the packet array
		SamplePacket **spArray =
			reinterpret_cast<SamplePacket **>(malloc(numStates * sizeof(void *)));
		for (unsigned int i = 0; i < numStates; i++) {
			spArray[i] = getPkt();
		}

		// Prepare one BufArray
		BufArray<SamplePacket> pktsIn(spArray, numStates);

		// Set data in input packets to 0
		for (unsigned int i = 0; i < numStates; i++) {
			*(reinterpret_cast<uint32_t *>(pktsIn[i]->getData())) = 0;
		}

		// Run the packet through the state machine
		startTimer = read_rdtsc();
		sm.runPktBatch(pktsIn);
		stopTimer = read_rdtsc();
		std::cout << "Insertion: " << stopTimer - startTimer << std::endl;

		// Run packets through SM
		startTimer = read_rdtsc();
		sm.runPktBatch(pktsIn);
		stopTimer = read_rdtsc();
		std::cout << "Run: " << stopTimer - startTimer << std::endl;

	} catch (exception *e) {
		// Just catch whatever fails there may be
		cout << endl << "FATAL:" << endl;
		cout << e->what() << endl;

		return 1;
	}

	return 0;
}
<|endoftext|>


<commit_before>// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/common/notification_service.h"

#include "base/lazy_instance.h"
#include "base/thread_local.h"

static base::LazyInstance<base::ThreadLocalPointer<NotificationService> >
    lazy_tls_ptr(base::LINKER_INITIALIZED);

// static
NotificationService* NotificationService::current() {
  return lazy_tls_ptr.Pointer()->Get();
}

// static
bool NotificationService::HasKey(const NotificationSourceMap& map,
                                 const NotificationSource& source) {
  return map.find(source.map_key()) != map.end();
}

NotificationService::NotificationService() {
  DCHECK(current() == NULL);
#ifndef NDEBUG
  memset(observer_counts_, 0, sizeof(observer_counts_));
#endif

  lazy_tls_ptr.Pointer()->Set(this);
}

void NotificationService::AddObserver(NotificationObserver* observer,
                                      NotificationType type,
                                      const NotificationSource& source) {
  DCHECK(type.value < NotificationType::NOTIFICATION_TYPE_COUNT);

  // We have gotten some crashes where the observer pointer is NULL. The problem
  // is that this happens when we actually execute a notification, so have no
  // way of knowing who the bad observer was. We want to know when this happens
  // in release mode so we know what code to blame the crash on (since this is
  // guaranteed to crash later).
  CHECK(observer);

  NotificationObserverList* observer_list;
  if (HasKey(observers_[type.value], source)) {
    observer_list = observers_[type.value][source.map_key()];
  } else {
    observer_list = new NotificationObserverList;
    observers_[type.value][source.map_key()] = observer_list;
  }

  observer_list->AddObserver(observer);
#ifndef NDEBUG
  ++observer_counts_[type.value];
#endif
}

void NotificationService::RemoveObserver(NotificationObserver* observer,
                                         NotificationType type,
                                         const NotificationSource& source) {
  DCHECK(type.value < NotificationType::NOTIFICATION_TYPE_COUNT);
  DCHECK(HasKey(observers_[type.value], source));

  NotificationObserverList* observer_list =
      observers_[type.value][source.map_key()];
  if (observer_list) {
    observer_list->RemoveObserver(observer);
#ifndef NDEBUG
    --observer_counts_[type.value];
#endif
  }

  // TODO(jhughes): Remove observer list from map if empty?
}

void NotificationService::Notify(NotificationType type,
                                 const NotificationSource& source,
                                 const NotificationDetails& details) {
  DCHECK(type.value > NotificationType::ALL) <<
      "Allowed for observing, but not posting.";
  DCHECK(type.value < NotificationType::NOTIFICATION_TYPE_COUNT);

  // There's no particular reason for the order in which the different
  // classes of observers get notified here.

  // Notify observers of all types and all sources
  if (HasKey(observers_[NotificationType::ALL], AllSources()) &&
      source != AllSources()) {
    FOR_EACH_OBSERVER(NotificationObserver,
       *observers_[NotificationType::ALL][AllSources().map_key()],
       Observe(type, source, details));
  }

  // Notify observers of all types and the given source
  if (HasKey(observers_[NotificationType::ALL], source)) {
    FOR_EACH_OBSERVER(NotificationObserver,
        *observers_[NotificationType::ALL][source.map_key()],
        Observe(type, source, details));
  }

  // Notify observers of the given type and all sources
  if (HasKey(observers_[type.value], AllSources()) &&
      source != AllSources()) {
    FOR_EACH_OBSERVER(NotificationObserver,
                      *observers_[type.value][AllSources().map_key()],
                      Observe(type, source, details));
  }

  // Notify observers of the given type and the given source
  if (HasKey(observers_[type.value], source)) {
    FOR_EACH_OBSERVER(NotificationObserver,
                      *observers_[type.value][source.map_key()],
                      Observe(type, source, details));
  }
}

NotificationService::~NotificationService() {
  lazy_tls_ptr.Pointer()->Set(NULL);

#ifndef NDEBUG
  for (int i = 0; i < NotificationType::NOTIFICATION_TYPE_COUNT; i++) {
    if (observer_counts_[i] > 0) {
      // This may not be completely fixable -- see
      // http://code.google.com/p/chromium/issues/detail?id=11010 .
      // But any new leaks should be fixed.
      LOG(WARNING) << observer_counts_[i] << " notification observer(s) leaked"
          << " of notification type " << i;
    }
  }
#endif

  for (int i = 0; i < NotificationType::NOTIFICATION_TYPE_COUNT; i++) {
    NotificationSourceMap omap = observers_[i];
    for (NotificationSourceMap::iterator it = omap.begin();
         it != omap.end(); ++it) {
      delete it->second;
    }
  }
}

NotificationObserver::~NotificationObserver() {}
<commit_msg>Make NotificationObserver leaks into LOG(INFO).<commit_after>// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/common/notification_service.h"

#include "base/lazy_instance.h"
#include "base/thread_local.h"

static base::LazyInstance<base::ThreadLocalPointer<NotificationService> >
    lazy_tls_ptr(base::LINKER_INITIALIZED);

// static
NotificationService* NotificationService::current() {
  return lazy_tls_ptr.Pointer()->Get();
}

// static
bool NotificationService::HasKey(const NotificationSourceMap& map,
                                 const NotificationSource& source) {
  return map.find(source.map_key()) != map.end();
}

NotificationService::NotificationService() {
  DCHECK(current() == NULL);
#ifndef NDEBUG
  memset(observer_counts_, 0, sizeof(observer_counts_));
#endif

  lazy_tls_ptr.Pointer()->Set(this);
}

void NotificationService::AddObserver(NotificationObserver* observer,
                                      NotificationType type,
                                      const NotificationSource& source) {
  DCHECK(type.value < NotificationType::NOTIFICATION_TYPE_COUNT);

  // We have gotten some crashes where the observer pointer is NULL. The problem
  // is that this happens when we actually execute a notification, so have no
  // way of knowing who the bad observer was. We want to know when this happens
  // in release mode so we know what code to blame the crash on (since this is
  // guaranteed to crash later).
  CHECK(observer);

  NotificationObserverList* observer_list;
  if (HasKey(observers_[type.value], source)) {
    observer_list = observers_[type.value][source.map_key()];
  } else {
    observer_list = new NotificationObserverList;
    observers_[type.value][source.map_key()] = observer_list;
  }

  observer_list->AddObserver(observer);
#ifndef NDEBUG
  ++observer_counts_[type.value];
#endif
}

void NotificationService::RemoveObserver(NotificationObserver* observer,
                                         NotificationType type,
                                         const NotificationSource& source) {
  DCHECK(type.value < NotificationType::NOTIFICATION_TYPE_COUNT);
  DCHECK(HasKey(observers_[type.value], source));

  NotificationObserverList* observer_list =
      observers_[type.value][source.map_key()];
  if (observer_list) {
    observer_list->RemoveObserver(observer);
#ifndef NDEBUG
    --observer_counts_[type.value];
#endif
  }

  // TODO(jhughes): Remove observer list from map if empty?
}

void NotificationService::Notify(NotificationType type,
                                 const NotificationSource& source,
                                 const NotificationDetails& details) {
  DCHECK(type.value > NotificationType::ALL) <<
      "Allowed for observing, but not posting.";
  DCHECK(type.value < NotificationType::NOTIFICATION_TYPE_COUNT);

  // There's no particular reason for the order in which the different
  // classes of observers get notified here.

  // Notify observers of all types and all sources
  if (HasKey(observers_[NotificationType::ALL], AllSources()) &&
      source != AllSources()) {
    FOR_EACH_OBSERVER(NotificationObserver,
       *observers_[NotificationType::ALL][AllSources().map_key()],
       Observe(type, source, details));
  }

  // Notify observers of all types and the given source
  if (HasKey(observers_[NotificationType::ALL], source)) {
    FOR_EACH_OBSERVER(NotificationObserver,
        *observers_[NotificationType::ALL][source.map_key()],
        Observe(type, source, details));
  }

  // Notify observers of the given type and all sources
  if (HasKey(observers_[type.value], AllSources()) &&
      source != AllSources()) {
    FOR_EACH_OBSERVER(NotificationObserver,
                      *observers_[type.value][AllSources().map_key()],
                      Observe(type, source, details));
  }

  // Notify observers of the given type and the given source
  if (HasKey(observers_[type.value], source)) {
    FOR_EACH_OBSERVER(NotificationObserver,
                      *observers_[type.value][source.map_key()],
                      Observe(type, source, details));
  }
}

NotificationService::~NotificationService() {
  lazy_tls_ptr.Pointer()->Set(NULL);

#ifndef NDEBUG
  for (int i = 0; i < NotificationType::NOTIFICATION_TYPE_COUNT; i++) {
    if (observer_counts_[i] > 0) {
      // This may not be completely fixable -- see
      // http://code.google.com/p/chromium/issues/detail?id=11010 .
      // But any new leaks should be fixed.
      LOG(INFO) << observer_counts_[i] << " notification observer(s) leaked"
          << " of notification type " << i;
    }
  }
#endif

  for (int i = 0; i < NotificationType::NOTIFICATION_TYPE_COUNT; i++) {
    NotificationSourceMap omap = observers_[i];
    for (NotificationSourceMap::iterator it = omap.begin();
         it != omap.end(); ++it) {
      delete it->second;
    }
  }
}

NotificationObserver::~NotificationObserver() {}
<|endoftext|>


<commit_before>/*
Copyright (c) 2017 InversePalindrome
VariadicPoker - Deck.hpp
InversePalindrome.com
*/

#pragma once
#include <list>
#include <algorithm>
#include "Card.hpp"

class Deck
{
public:
	Deck();
	Deck(const std::list<Card>& deck);

	std::list<Card> getDeck() const;

	void setDeck(const std::list<Card>& deck);

	void shuffle() const;
	Card removeCard() const;
	Card removeCard(const Card& card) const;
	void addCard(const Card& card) const;

private:
	std::list<Card> deck;
};<commit_msg>Delete Deck.hpp<commit_after><|endoftext|>