text:
/* Copyright 2008, 2009 (C) Nicira, Inc.
 *
 * This file is part of NOX.
 *
 * NOX is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * NOX 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with NOX.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef AUTHENTICATOR_HH
#define AUTHENTICATOR_HH 1

#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/shared_array.hpp>

#include "component.hh"
#include "bindings_storage/bindings_storage.hh"
#include "data/datatypes.hh"
#include "data/datacache.hh"
#include "flow_in.hh"
#include "flow_util.hh"
#include "hash_map.hh"
#include "host_event.hh"
#include "netinet++/cidr.hh"
#include "user_event.hh"
#include "user_event_log/user_event_log.hh"

// enable to have locations added/removed based on topology i.e. so
// hosts wont have duplicate locations with one downstream from
// another.  Requires routing_module library to be loaded as
// dependency.  Often you'll want this enabled, but NOX is set up
// currently such that routing_module auto pulls in LLDP discovery
// component etc which a user may not always want running.

#define AUTH_WITH_ROUTING 1

#if AUTH_WITH_ROUTING
#include "routing/routing.hh"
#endif

namespace vigil {
namespace applications {

/** \ingroup noxcomponents
 *
 * Authenticator
 *
 */

class Authenticator
    : public container::Component {

public:
    Authenticator(const container::Context*, const json_object*);
    Authenticator() : Component(0) { }

    static void getInstance(const container::Context*, Authenticator*&);

    void configure(const container::Configuration*);
    void install();

    struct DLEntry;

    struct DLNWEntry {
        uint32_t nwaddr;
        bool authed;
        boost::shared_ptr<HostNetid> host_netid;
        boost::shared_ptr<GroupList> nwaddr_groups;  // both or just nw?
        DLEntry *dlentry;
        time_t exp_time;
    };

    struct HostNetEntry;

    struct DLBinding {
        int64_t id;
        int64_t priority;
        HostNetEntry *netid;
    };

    typedef hash_map<uint32_t, DLNWEntry> DLNWMap;

    struct DLEntry {
        ethernetaddr dladdr;
        boost::shared_ptr<GroupList> groups;
        DLNWMap dlnwentries;
        DLNWEntry *zero;
        std::list<AuthedLocation> locations;
        std::list<DLBinding> bindings;
        time_t exp_time;
    };

    struct NWBinding {
        int64_t id;
        int64_t priority;
        HostNetEntry *netid;
    };

    struct NWEntry {
        boost::shared_ptr<GroupList> groups;
        std::list<DLNWEntry*> dlnwentries;
        std::list<NWBinding> bindings;
        time_t exp_time;
    };

    struct LocEntry {
        boost::shared_ptr<Location> entry;
        std::list<DLEntry*> dlentries;
        bool active;
    };

    struct LocBinding {
        int64_t priority;
        LocEntry *location;
    };

    struct PortEntry {
        int64_t port;
        LocEntry *active_location;
        std::list<LocBinding> bindings;
    };

    struct SwitchEntry {
        boost::shared_ptr<Switch> entry;
        std::list<PortEntry> locations;
        bool active;
    };

    struct SwitchBinding {
        int64_t priority;
        SwitchEntry *sentry;
    };

    struct DPEntry {
        SwitchEntry *active_switch;
        std::list<SwitchBinding> bindings;
    };

    struct HostNetEntry {
        boost::shared_ptr<HostNetid> entry;
        std::list<DLNWEntry*> dlnwentries;
    };

    struct HostEntry {
        boost::shared_ptr<Host> entry;
        std::list<HostNetEntry*> netids;
    };

    struct UserEntry {
        boost::shared_ptr<User> entry;
        std::list<HostEntry*> hostentries;
    };

    void add_internal_subnet(const cidr_ipaddr&);
    bool remove_internal_subnet(const cidr_ipaddr&);
    void clear_internal_subnets();

    void get_names(const datapathid& dp, uint16_t inport,
                   const ethernetaddr& dlsrc, uint32_t nwsrc,
                   const ethernetaddr& dldst, uint32_t nwdst,
                   PyObject *callable);

    void reset_last_active(const datapathid& dp, uint16_t port,
                           const ethernetaddr& dladdr, uint32_t nwaddr);

    void set_default_hard_timeout(uint32_t to) { default_hard_timeout = to; }
    void set_default_idle_timeout(uint32_t to) { default_idle_timeout = to; }
    uint32_t get_default_hard_timeout() { return default_hard_timeout; }
    uint32_t get_default_idle_timeout() { return default_idle_timeout; }

    const DLEntry *get_dladdr_entry(const ethernetaddr& dladdr) const;
    const NWEntry *get_nwaddr_entry(uint32_t nwaddr) const;
    const DLNWEntry *get_dlnw_entry(const ethernetaddr& dladdr,
                                    uint32_t nwaddr) const;
    const SwitchEntry *get_switch_entry(const datapathid& dp) const;
    const LocEntry *get_location_entry(const datapathid& dp,
                                       uint16_t port) const;
    const SwitchEntry *get_switch_entry(int64_t id) const;
    const LocEntry *get_location_entry(int64_t id) const;
    const HostEntry *get_host_entry(int64_t id) const;
    const HostNetEntry *get_host_netid_entry(int64_t id) const;
    const UserEntry *get_user_entry(int64_t id) const;

    HostEntry* update_host(int64_t hostname);
    HostNetEntry* update_host_netid(int64_t host_netid, int64_t host_id,
                                    bool is_router, bool is_gateway);
    UserEntry* update_user(int64_t username);
    LocEntry* update_location(int64_t locname, int64_t priority,
                              int64_t switch_id, uint16_t port,
                              const std::string& portname);
    SwitchEntry* update_switch(int64_t switchname, int64_t priority,
                               const datapathid& dp);

    void delete_principal(const Principal& principal);
    void delete_host(int64_t hostname);
    void delete_host_netid(int64_t host_netid);
    void delete_user(int64_t username);
    void delete_location(int64_t locname, bool remove_binding = true);
    void delete_switch(int64_t switchname);

    typedef hash_map<int64_t, hash_set<int64_t> > MemberMap; // key = type,
                                                             // value = member IDs
    void all_updated(bool poison) const;
    void principal_updated(const Principal& principal, bool poison,
                           bool refetch_groups);
    void principals_updated(const PrincipalSet& principals, bool poison,
                            bool refetch_groups);
    void groups_updated(const std::list<int64_t>& groups, bool poison);
    void addr_updated(AddressType type, const std::string& addr,
                      bool poison, bool refetch_groups);

    int64_t get_port_number(const datapathid& dp,
                            const std::string& port_name) const;

    typedef boost::function<void(const boost::shared_ptr<Location>&,
                                 const DLNWEntry*, bool)> EndpointUpdatedFn;

    void add_endpoint_updated_fn(const EndpointUpdatedFn& fn);

    /** \brief Option to allow poisoning or not
     * Defaulted to true
     */
    bool poison_allowed;

private:
    typedef hash_map<uint64_t, DLEntry>      DLMap;
    typedef hash_map<uint32_t, NWEntry>      NWMap;
    typedef hash_map<uint64_t, DPEntry>      DPMap;
    typedef hash_map<int64_t, SwitchEntry>   SwitchMap;
    typedef hash_map<int64_t, LocEntry>      LocMap;
    typedef hash_map<int64_t, HostEntry>     HostMap;
    typedef hash_map<int64_t, HostNetEntry>  HostNetMap;
    typedef hash_map<int64_t, UserEntry>     UserMap;

    static const uint32_t NWADDR_TIMEOUT = 300;

    DLMap hosts_by_dladdr;
    NWMap hosts_by_nwaddr;
    DPMap switches_by_dp;

    SwitchMap switches;
    LocMap locations;
    HostMap hosts;
    HostNetMap host_netids;
    UserMap users;

    hash_map<uint64_t, SwitchEntry> dynamic_switches;
    hash_map<uint64_t, LocEntry> dynamic_locations;

    struct Queued_loc {
        datapathid dp;
        uint16_t port;
        std::string portname;
    };

    std::list<Queued_loc> queued_locs;

    bool routing;
    bool auto_auth;
    uint32_t expire_timer;

    boost::shared_array<uint8_t> raw_of;
    ofp_flow_mod *ofm;

    std::vector<cidr_ipaddr> internal_subnets;

    Datatypes *datatypes;
    Data_cache *data_cache;
    Bindings_Storage *bindings;
    User_Event_Log *user_log;
    std::list<EndpointUpdatedFn> updated_fns;
    uint32_t default_hard_timeout;
    uint32_t default_idle_timeout;
    char buf[1024];
#if AUTH_WITH_ROUTING
    Routing_module *routing_mod;
#endif

    const std::string& get_switch_name(int64_t id);
    const std::string& get_location_name(int64_t id);
    const std::string& get_host_name(int64_t id);
    const std::string& get_netid_name(int64_t id);
    const std::string& get_user_name(int64_t id);
    const std::string& get_group_name(int64_t id);

    DLEntry *get_dladdr_entry(const ethernetaddr& dladdr, const time_t& exp_time,
                              bool create=false);
    NWEntry *get_nwaddr_entry(uint32_t nwaddr, const time_t& exp_time,
                              bool create);
    DLNWEntry *get_dlnw_entry(const ethernetaddr& dladdr, uint32_t nwaddr,
                              const time_t& exp_time, bool create);
    DLNWEntry *get_dlnw_entry(DLEntry *dlentry, uint32_t nwaddr,
                              const time_t& exp_time, bool create);

    struct ptrhash {
        std::size_t operator()(const void *ptr) const;
    };

    struct ptreq {
        bool operator()(const void *a, const void *b) const;
    };

    struct sptrhash {
        std::size_t operator()(const boost::shared_ptr<Location>& ptr) const;
    };

    struct sptreq {
        bool operator()(const boost::shared_ptr<Location>& a,
                        const boost::shared_ptr<Location>& b) const;
    };

    typedef hash_set<const DLNWEntry*, ptrhash, ptreq> DLNWHash;
    typedef hash_map<boost::shared_ptr<Location>,
                     DLNWHash, sptrhash, sptreq> EndpointValue;
    typedef hash_map<const DLEntry*, EndpointValue,
                     ptrhash, ptreq> Endpoints;

    void group_updated(int64_t group, Endpoints& endpoints);
    void add_updated_principal(const Principal& principal,
                               Endpoints& endpoints, bool refetch_groups);
    void add_updated_cidr(const cidr_ipaddr& cidr, Endpoints& endpoints,
                          bool refetch_groups);
    void add_updated_entry(const SwitchEntry *entry, Endpoints& endpoints) const;
    void add_updated_entry(const LocEntry *entry, Endpoints& endpoints) const;
    void add_updated_entry(const HostEntry *entry, Endpoints& endpoints) const;
    void add_updated_entry(const HostNetEntry *entry, Endpoints& endpoints) const;
    void add_updated_entry(const UserEntry *entry, Endpoints& endpoints) const;
    void add_updated_entry(const DLEntry *entry, Endpoints& endpoints) const;
    void add_updated_entry(const NWEntry *entry, Endpoints& endpoints) const;
    void add_updated_entry(const DLNWEntry *entry, Endpoints& endpoints) const;
    LocEntry *get_unauth_location();
    HostNetEntry *get_unauth_netid();
    HostNetEntry *get_auth_netid();
    bool auth_flow_host(const Flow_in_event& fi, bool auth_default);
    const HostNetEntry *get_names_from_bindings(const DLEntry *dlentry,
                                          const NWEntry *nwentry) const;
    bool get_location(const datapathid &dp, uint16_t port,
                      std::list<AuthedLocation> &als,
                      std::list<AuthedLocation>::iterator &al) const;
    bool get_on_path_location(const datapathid& dp, uint16_t port,
                              std::list<AuthedLocation>& als,
                              std::list<AuthedLocation>::iterator &al) const;
    bool is_internal_ip(uint32_t nwaddr) const;
    void expire_entities();
    void expire_host_locations(const timeval& curtime);
    void expire_hosts(const timeval& curtime);
    void poison_port(const datapathid& dp, uint16_t port) const;
    void poison_location(const datapathid& dp, const ethernetaddr& dladdr,
                         uint32_t nwaddr, bool wildcard_nw) const;
    void endpoints_updated(const Endpoints& endpoints, bool poison) const;
    void call_updated_fns(const boost::shared_ptr<Location>& lentry,
                          const DLNWEntry *nwentry, bool poisoned) const;
    void call_updated_fns(const boost::shared_ptr<Location>& lentry,
                          const DLEntry *dlentry, bool poisoned) const;
    bool is_internal_mac(uint64_t hb_dladdr) const;

    /* authenticator_modify.cc */

    bool add_host(const Host_auth_event& ha);
    bool add_host2(const Host_auth_event& ha, LocEntry *lentry,
                   HostNetEntry *netid, HostEntry *host,
                   DLNWEntry *dlnwentry, const timeval& curtime);
    bool auth_zero(const Host_auth_event& ha, LocEntry *location,
                   HostNetEntry *netid, HostEntry *host,
                   DLNWEntry *dlnwentry, const timeval& curtime);
    void add_host_location(const Host_auth_event& ha, LocEntry *lentry,
                           HostEntry *host, DLEntry *dlentry,
                           const timeval& curtime);
    void remove_dl_location(DLEntry *dlentry,
                            std::list<AuthedLocation>::iterator& authed,
                            Host_event::Reason reason, bool poison);
    void remove_dl_location(DLEntry *dlentry, const datapathid& dp, uint16_t port,
                            bool ignore_port, Host_event::Reason reason, bool poison);
    void remove_dlnw_host(DLNWEntry *dlnwentry, Host_event::Reason reason,
                          bool poison);
    bool remove_dlnw_location(DLNWEntry *dlnwentry, const datapathid& dp,
                              uint16_t port, bool ignore_port,
                              Host_event::Reason reason);
    void remove_host(HostEntry *hentry, const datapathid& dp,
                     uint16_t port, bool ignore_port,
                     Host_event::Reason reason);
    void remove_host_netid(HostNetEntry *netid, const datapathid& dp,
                             uint16_t port, bool ignore_port,
                             Host_event::Reason reason);
    void remove_location_hosts(LocEntry *lentry, Host_event::Reason reason, bool poison);
    void remove_location_hosts(const datapathid& dp, uint16_t port,
                               bool ignore_port, Host_event::Reason reason,
                               bool poison);
    void remove_host_by_event(const Host_auth_event& ha);
    bool remove_host_by_event(const Host_auth_event& ha, DLNWEntry *dlnwentry);
    bool add_user(const User_auth_event& ua);
    void remove_unauth_user(HostEntry *hentry);
    void remove_user(UserEntry *user, HostEntry *host,
                     User_event::Reason reason, bool add_unauth);
    void remove_user(int64_t username, int64_t hostname,
                     User_event::Reason reason);
    DLEntry* new_dladdr(const ethernetaddr& dladdr, const time_t& exp_time);
    NWEntry* new_nwaddr(uint32_t nwaddr, const time_t& exp_time);
    DLNWEntry* new_dlnw_entry(DLEntry *dlentry, uint32_t nwaddr,
                              const time_t& exp_time);
    bool add_netid_binding(HostNetEntry *netid, int64_t host_id);
    void update_netid_binding(int64_t addr_id, int64_t host_netid,
                              int64_t priority, const std::string& addr,
                              AddressType addr_type, bool deleted);
    void add_dladdr_binding(HostNetEntry *netid, int64_t addr_id,
                            int64_t priority, const ethernetaddr& dladdr);
    void add_nwaddr_binding(HostNetEntry *netid, int64_t addr_id,
                            int64_t priority, const ipaddr& ip);
    void delete_dladdr_binding(HostNetEntry *netid, int64_t addr_id,
                               const ethernetaddr& dladdr,
                               Host_event::Reason reason);
    void delete_nwaddr_binding(HostNetEntry *netid, int64_t addr_id,
                               const ipaddr& ip,
                               Host_event::Reason reason);
    void delete_netid_host_binding(HostNetEntry *netid,
                                   Host_event::Reason reason);
    void delete_host_netid(HostNetEntry *netid);
    bool add_location_binding(LocEntry *lentry, int64_t priority,
                              int64_t switch_id, uint16_t port);
    void delete_location_binding(LocEntry *lentry);
    LocEntry* new_dynamic_location(SwitchEntry *sentry, uint16_t port);
    void activate_location(const datapathid& dp, uint16_t port,
                           const std::string& portname);
    void deactivate_location(const datapathid& dp, uint16_t port,
                             bool poison);
    void deactivate_location(const datapathid& dp, uint16_t port,
                             PortEntry& pentry, bool poison);
    void add_switch_binding(SwitchEntry *sentry, int64_t priority,
                            const datapathid& dp);
    void delete_switch_binding(SwitchEntry *sentry);
    SwitchEntry* new_dynamic_switch(const datapathid& dp);
    void activate_switch(const datapathid& dp);
    void deactivate_switch(const datapathid& dp, bool poison);
    void deactivate_switch(DPEntry& dentry, bool poison);
    void new_location_config(const datapathid& dp, uint16_t port,
                             const std::string& portname, bool is_redo);
    void queue_new_location(const datapathid& dp, uint16_t port,
                            const std::string& portname);
    void process_queue();
    void remove_queued_loc(const datapathid& dp, uint16_t port);
    void mark_as_virtual(LocEntry* location);
    LocEntry* get_active_location(const datapathid& dp, uint16_t port) const;

    Disposition handle_bootstrap(const Event& event);
    Disposition handle_datapath_join(const Event& event);
    Disposition handle_datapath_leave(const Event& event);
    Disposition handle_port_status(const Event& event);
    Disposition handle_link_change(const Event& event);
    Disposition handle_host_auth(const Event& event);
    Disposition handle_user_auth(const Event& event);
    Disposition handle_packet_in(const Event& event);

    bool set_flow_in(Flow_in_event &fi, bool packet_in);
    bool set_flow_src(Flow_in_event& fi, bool packet_in);
    void set_flow_dst(Flow_in_event &fi);
    DLNWEntry *cache_host(DLEntry *dlentry,
                          const NWEntry *nwentry, uint32_t nwaddr,
                          const time_t& exp_time);
    void make_primary(const time_t& curtime, DLEntry *dlentry,
                      std::list<AuthedLocation>::iterator& location);
    bool set_source_route_host(NWEntry *nwentry, Flow_in_event& fi);
    bool set_dest_route_host(NWEntry *nwentry, Flow_in_event& fi);
    void set_destinations(const std::list<AuthedLocation>& als,
                          Flow_in_event& fi);
    void set_empty_destinations(Flow_in_event& fi);
    void set_route_destinations(const std::list<AuthedLocation>& als,
                                Flow_in_event& fi);

#ifdef TWISTED_ENABLED
    PyObject* namelist_to_python(const GroupList& ids);
    PyObject* name_to_python(uint32_t id, PrincipalType t);
    PyObject* users_to_python(const std::list<AuthedUser>& users);
#endif
};

} // namespace applications
} // namespace vigil

#endif // AUTHENTICATOR_HH

ext: hh

max_stars_repo_name: ayjazz/OESS

max_issues_repo_name: ayjazz/OESS

max_forks_repo_name: ayjazz/OESS


text:
//
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/CPPAlliance/http_proto
//

#ifndef BOOST_HTTP_PROTO_BUFFER_HPP
#define BOOST_HTTP_PROTO_BUFFER_HPP

#include <boost/http_proto/detail/config.hpp>
#include <cstdlib>

namespace boost {

#ifndef BOOST_HTTP_PROTO_DOCS
namespace asio {
class const_buffer;
class mutable_buffer;
} // asio
#endif

namespace http_proto {

/** Holds a buffer that can be modified
*/
class mutable_buffer
{
    void* data_ = nullptr;
    std::size_t size_ = 0;

public:
    mutable_buffer() noexcept = default;

    mutable_buffer(
        void* data,
        std::size_t size) noexcept
        : data_(data)
        , size_(size)
    {
    }

    void*
    data() const noexcept
    {
        return data_;
    }

    std::size_t
    size() const noexcept
    {
        return size_;
    }

    mutable_buffer&
    operator+=(std::size_t n) noexcept
    {
        if(n > size_)
            n = size_;
        data_ = static_cast<
            char*>(data_) + n;
        size_ -= n;
        return *this;
    }

#ifndef BOOST_HTTP_PROTO_DOCS
    inline
    operator
    asio::mutable_buffer() const noexcept;
#endif
};

//------------------------------------------------

/** Holds a buffer that cannot be modified
*/
class const_buffer
{
    void const* data_ = nullptr;
    std::size_t size_ = 0;

public:
    const_buffer() noexcept = default;

    const_buffer(
        mutable_buffer const& mb) noexcept
        : data_(mb.data())
        , size_(mb.size())
    {
    }

    const_buffer(
        void const* data,
        std::size_t size) noexcept
        : data_(data)
        , size_(size)
    {
    }

    void const*
    data() const noexcept
    {
        return data_;
    }

    std::size_t
    size() const noexcept
    {
        return size_;
    }

    const_buffer&
    operator+=(std::size_t n) noexcept
    {
        if(n > size_)
            n = size_;
        data_ = static_cast<
            char const*>(data_) + n;
        size_ -= n;
        return *this;
    }

#ifndef BOOST_HTTP_PROTO_DOCS
    inline
    operator
    asio::const_buffer() const noexcept;
#endif
};

//------------------------------------------------

inline
mutable_buffer
operator+(
    mutable_buffer b,
    std::size_t n) noexcept
{
    b += n;
    return b;
}

inline
mutable_buffer
operator+(
    std::size_t n,
    mutable_buffer b) noexcept
{
    b += n;
    return b;
}

inline
const_buffer
operator+(
    const_buffer b,
    std::size_t n) noexcept
{
    b += n;
    return b;
}

inline
const_buffer
operator+(
    std::size_t n,
    const_buffer b) noexcept
{
    b += n;
    return b;
}

//------------------------------------------------

/** Holds a buffer sequence that cannot be modified
*/
class mutable_buffers
{
    mutable_buffer const* begin_ = nullptr;
    mutable_buffer const* end_ = nullptr;

public:
    mutable_buffers() = default;
    mutable_buffers(
        mutable_buffers const&) = default;
    mutable_buffers&
    operator=(mutable_buffers const&) = default;

    mutable_buffers(
        mutable_buffer const* begin,
        mutable_buffer const* end) noexcept
        : begin_(begin)
        , end_(end)
    {
    }

    mutable_buffer const*
    begin() const noexcept
    {
        return begin_;
    }

    mutable_buffer const*
    end() const noexcept
    {
        return end_;
    }
};

//------------------------------------------------

/** Holds a buffer sequence that cannot be modified
*/
class const_buffers
{
    const_buffer const* begin_ = nullptr;
    const_buffer const* end_ = nullptr;

public:
    const_buffers() = default;
    const_buffers(
        const_buffers const&) = default;
    const_buffers&
    operator=(const_buffers const&) = default;

    const_buffers(
        const_buffer const* begin,
        const_buffer const* end) noexcept
        : begin_(begin)
        , end_(end)
    {
    }

    const_buffer const*
    begin() const noexcept
    {
        return begin_;
    }

    const_buffer const*
    end() const noexcept
    {
        return end_;
    }
};

struct const_buffer_pair
{
    char const* data[2];
    std::size_t size[2];
};

} // http_proto
} // boost

#endif

ext: hpp


text:
//=======================================================================
// Copyright 2007 Aaron Windsor
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//=======================================================================
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/boyer_myrvold_planar_test.hpp>

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

  // This program illustrates a simple use of boyer_myrvold_planar_embedding
  // as a simple yes/no test for planarity.

  using namespace boost;

  typedef adjacency_list<vecS,
                         vecS,
                         undirectedS,
                         property<vertex_index_t, int>
                         > graph;

  graph K_4(4);
  add_edge(0, 1, K_4);
  add_edge(0, 2, K_4);
  add_edge(0, 3, K_4);
  add_edge(1, 2, K_4);
  add_edge(1, 3, K_4);
  add_edge(2, 3, K_4);

  if (boyer_myrvold_planarity_test(K_4))
    std::cout << "K_4 is planar." << std::endl;
  else
    std::cout << "ERROR! K_4 should have been recognized as planar!"
          << std::endl;

  graph K_5(5);
  add_edge(0, 1, K_5);
  add_edge(0, 2, K_5);
  add_edge(0, 3, K_5);
  add_edge(0, 4, K_5);
  add_edge(1, 2, K_5);
  add_edge(1, 3, K_5);
  add_edge(1, 4, K_5);
  add_edge(2, 3, K_5);
  add_edge(2, 4, K_5);

  // We've almost created a K_5 - it's missing one edge - so it should still
  // be planar at this point.

  if (boyer_myrvold_planarity_test(K_5))
    std::cout << "K_5 (minus an edge) is planar." << std::endl;
  else
    std::cout << "ERROR! K_5 with one edge missing should"
              << " have been recognized as planar!" << std::endl;

  // Now add the final edge...
  add_edge(3, 4, K_5);

  if (boyer_myrvold_planarity_test(K_5))
    std::cout << "ERROR! K_5 was recognized as planar!" << std::endl;
  else
    std::cout << "K_5 is not planar." << std::endl;

  return 0;
}

ext: cpp

max_stars_repo_name: zyiacas/boost-doc-zh

max_issues_repo_name: sdfict/boost-doc-zh

max_forks_repo_name: sdfict/boost-doc-zh


text:
#define BOOST_TEST_MODULE communicator_reduce_scatter

#include <boost/test/included/unit_test.hpp>
#include <mpl/mpl.hpp>
#include "test_helper.hpp"

template<typename F, typename T>
bool reduce_scatter_test(F f, const T &val) {
  const mpl::communicator &comm_world{mpl::environment::comm_world()};
  T x{val};
  std::vector<T> v_x;
  mpl::contiguous_layouts<T> l;
  for (int i{0}; i < comm_world.size(); ++i) {
    const int block_size{i + 1};
    for (int j{0}; j < block_size; ++j)
      v_x.push_back(x);
    l.push_back(mpl::contiguous_layout<T>(block_size));
    ++x;
  }
  const int block_size{comm_world.rank() + 1};
  std::vector<T> v_y(block_size);
  comm_world.reduce_scatter(f, v_x.data(), v_y.data(), l);
  x = val;
  for (int i{0}; i < comm_world.rank(); ++i)
    ++x;
  T expected{x};
  for (int i{1}; i < comm_world.size(); ++i)
    expected = f(expected, x);
  std::vector<T> v_expected(block_size, expected);
  return v_y == v_expected;
}

template<typename F, typename T>
bool ireduce_scatter_test(F f, const T &val) {
  const mpl::communicator &comm_world{mpl::environment::comm_world()};
  T x{val};
  std::vector<T> v_x;
  mpl::contiguous_layouts<T> l;
  for (int i{0}; i < comm_world.size(); ++i) {
    const int block_size{i + 1};
    for (int j{0}; j < block_size; ++j)
      v_x.push_back(x);
    l.push_back(mpl::contiguous_layout<T>(block_size));
    ++x;
  }
  const int block_size{comm_world.rank() + 1};
  std::vector<T> v_y(block_size);
  auto r{comm_world.ireduce_scatter(f, v_x.data(), v_y.data(), l)};
  x = val;
  for (int i{0}; i < comm_world.rank(); ++i)
    ++x;
  T expected{x};
  for (int i{1}; i < comm_world.size(); ++i)
    expected = f(expected, x);
  std::vector<T> v_expected(block_size, expected);
  r.wait();
  return v_y == v_expected;
}

BOOST_AUTO_TEST_CASE(reduce_scatter) {
  BOOST_TEST(reduce_scatter_test(add<double>(), 1.0));
  BOOST_TEST(reduce_scatter_test(add<tuple>(), tuple{1, 2.0}));
  BOOST_TEST(reduce_scatter_test(mpl::plus<double>(), 1.0));
  BOOST_TEST(reduce_scatter_test(mpl::plus<tuple>(), tuple{1, 2.0}));
  BOOST_TEST(reduce_scatter_test([](auto a, auto b) { return a + b; }, 1.0));
  BOOST_TEST(reduce_scatter_test([](auto a, auto b) { return a + b; }, tuple{1, 2.0}));

  BOOST_TEST(ireduce_scatter_test(add<double>(), 1.0));
  BOOST_TEST(ireduce_scatter_test(add<tuple>(), tuple{1, 2.0}));
  BOOST_TEST(ireduce_scatter_test(mpl::plus<double>(), 1.0));
  BOOST_TEST(ireduce_scatter_test(mpl::plus<tuple>(), tuple{1, 2.0}));
  BOOST_TEST(ireduce_scatter_test([](auto a, auto b) { return a + b; }, 1.0));
  BOOST_TEST(ireduce_scatter_test([](auto a, auto b) { return a + b; }, tuple{1, 2.0}));
}

ext: cc

max_stars_repo_name: mlund/mpl

max_issues_repo_name: mlund/mpl

max_forks_repo_name: mlund/mpl


text: #include <boost/mpl/aux_/contains_impl.hpp>

max_stars_repo_name: miathedev/BoostForArduino

max_issues_repo_name: miathedev/BoostForArduino

max_forks_repo_name: miathedev/BoostForArduino


text:
#pragma once
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

namespace commands {
    const std::string CONSOLE_COMMAND = "ConsoleCommand";
    const std::string MESSAGE_BOX_COMMAND = "MessageBoxCommand";
};

class command {

public:
    command();

    void load( const std::string& json );
    void execute_command();

    boost::property_tree::ptree get_ptree() const;

private:
    boost::property_tree::ptree _pt;
};


text:
#ifndef SUPPORT_FOR_MODEL_GENERATION
#   define SUPPORT_FOR_MODEL_GENERATION
#endif  // SUPPORT_FOR_MODEL_GENERATION
#include "logging.h"
#include "libs/cleanup_operatiosn/dataclean_conf.h"
#include "libs/cleanup_operatiosn/command_processing.h"
#include "libs/cleanup_operatiosn/cleanup_tasks.h"
#include "libs/cleanup_operatiosn/cleanup_results.h"
#include "libs/json_parser/json_ostream.h"
#include "libs/json_parser/json_writer.h"
#include "libs/json_parser/json_istream.h"
#include "libs/system/exceptions.h"
#include "libs/system/error_message.h"
#include <boost/filesystem.hpp>
#include <boost/algorithm/string/join.hpp>
#include <algorithm>
#include <iterator>
#include <string>
#include <iostream>
#include <vector>

struct success_report {
    const std::string msg;
    const std::string details;

    explicit success_report(const std::string& path) :
        msg("finish successfully"),
        details("successfully finish processing data from " + path)
    {
    }
};

#ifdef NDEBUG
    sl::severity_level log_severity = sl::info;
#else
    sl::severity_level log_severity = sl::debug;
#endif // NDEBUG
namespace lookup
{
    json::ostream& operator ^ (json::ostream& js, const table_entry& stat);
}
std::ostream& operator << (std::ostream& os, const schema_type& s);
//std::ostream& operator << (std::ostream& os, const error_report& err);
////json::ostream& operator ^ (json::ostream& js, const error_report& err);
json::ostream& operator ^ (json::ostream& js, const success_report& err);
json::ostream& operator ^ (json::ostream& js, const column_info& stat);
json::ostream& operator ^ (json::ostream& js, const cleanup_results_entry& stat);
void report_success(const success_report& errors, const cleanup_results&, bool no_actions);
int report_error(error_report&& err);
dataclean_conf process_cmd(const std::string& arg);
void setup_log();
void log_print_schema(const schema_type& s);
cleanup_results clean(const dataclean_conf& withconf);

int main(int argc, char** argv)
{
    if (argc != 2) {
        LOG_ERROR<<"invalid command line args for "<<argv[0]<<" should have passed with json";
        return report_error(error_report{"invalid args", "usage: <json input> - either as file or on command line"});
    }

    try {
        setup_log();
        LOG_INFO<<"\n------------------------------------------------------------------------------\n"
            <<"   Starting "<<argv[0]<<" with \n"<<argv[1]
            <<"\n------------------------------------------------------------------------------\n";
        auto conf = process_cmd(argv[1]);
        LOG_INFO<<"finish configuration - starting processing";
        LOG_DEBUG<<"input:\n"<<conf;
        auto report = clean(conf);
        if (report.empty()) {
            report_error(error_report{"failed to execute", "either invalid configuration or failed to run the given configuration"});
        } else {
            report_success(success_report{conf.source_location}, report, not has_actions(conf));
        }
    } catch (const invalid_path& ip) {
        LOG_ERROR<<"invalid path given for processing input at "<<argv[1];
        return report_error(error_report{"invalid input path", ip.what()});
    } catch (const json_error& je) {
        LOG_ERROR<<"failed to process json input "<<argv[1];
        return report_error(error_report{"json parsing error", je.what()});
    } catch (const invalid_conf& e) {
        LOG_ERROR<<"terminating as as result of invalid conf "<<e.what();
        return report_error(error_report{"invalid configuration", "terminated as a result of invalid configuration: " + e.what()});
    } catch (const norm_error& e) {
        LOG_ERROR<<"normalization failed: "<<e.what();
        return report_error(error_report{"normalization failed", e.what()});
    } catch (const std::exception& e) {
        LOG_ERROR<<"run time error: "<<e.what();
        return report_error(error_report{"error", e.what()});
    } catch (...) {
        LOG_ERROR<<"unkown error while running "<<argv[0];
        return report_error(error_report{"unwkown error", "unknown error - terminate data clean application"});
    }
    LOG_INFO<<"\n------------------------------------------------------------------------------\n   "<<
        argv[0]<<" is done\n------------------------------------------------------------------------------\n";
    return 0;
}

int report_error(error_report&& err)
{
#if 0
    using namespace json::literals;
    json::output_stream root;
    auto parser = root ^ json::open;
    auto e = parser ^ "errors"_s;
    e ^ err ^ json::_end;
    auto msg = root ^ json::str_cast;
#endif
    const auto msg = to_json(err);
    std::cerr<<msg<<std::endl;
    return -1;
}

void report_success(const success_report& ok, const cleanup_results& stats, bool no_actions)
{
    using namespace json::literals;
    json::output_stream root;
    auto parser = root ^ json::open;
    auto err = parser ^ "ok"_s;
    err ^ ok ^ json::_end;
    auto d = parser ^ "stats"_s;
    if (no_actions) {
        LOG_INFO<<"we have no action to report results for";
        d ^ "status"_n ^ "no actions, no stats" ^ json::_end;
    } else {
        LOG_INFO<<"report for "<<stats.size()<<" in the output";
        d ^ stats ^ json::_end;
    }
    auto msg = root ^ json::str_cast;
    std::cerr<<msg<<std::endl;
}

json::ostream& operator ^ (json::ostream& js, const column_info& stat)
{
    using namespace json::literals;
    js ^ "minval"_n ^ math::minimum(stat.range) ^ "maxval"_n ^ math::maximum(stat.range) ^
        "mean"_n ^ math::mean(stat.stats) ^ "STD"_n ^ math::standard_dev(stat.stats);
    return js;
}

json::ostream& operator ^ (json::ostream& js, const success_report& ok)
{
    using namespace json::literals;

    js ^ "status"_n ^ ok.msg ^ "msg"_n ^ ok.details;
    return js;
}

json::ostream& operator ^ (json::ostream& js, const cleanup_results_entry& stat)
{
    using namespace json::literals;
    js ^ "colid"_n ^ name(stat);
    auto info = js ^ "info"_s;
    info ^ stats(stat) ^ json::_end;
    return js;
}

dataclean_conf process_cmd(const std::string& arg)
{
    auto actions = process_configuration(arg);
    return actions;
}

void setup_log()
{
    static const std::string LOG_DIR_NAME = "logs";
    static const std::string LOG_FILE_NAME = "data_cleaning";
    sl::default_log(log_severity, LOG_DIR_NAME.c_str(), LOG_FILE_NAME.c_str());
}

std::ostream& operator << (std::ostream& os, const schema_type& s) {
    std::copy(s.begin(), s.end(), std::ostream_iterator<schema_type::value_type>(os, "|"));
    return os;
}

void log_print_schema(const schema_type& s)
{
    std::string output = boost::algorithm::join(s, "|");
    LOG_DEBUG<<"shcema: "<<output;
}

cleanup_results clean(const dataclean_conf& withconf)
{
    LOG_INFO<<"starting data cleaning process for data at "<<withconf.source_location;
    if (empty(withconf)) {
        LOG_ERROR<<"would not run, no valid configuration";
        return cleanup_results{};
    } else {
        cleanup_task cleanup;
        auto results = cleanup(withconf);
        if (results.empty()) {
            LOG_ERROR<<"failed to cleanup data";
        } else {
            LOG_INFO<<"successfully finish cleaning up data and export it to '"<<withconf.export_file<<"'";
        }
        LOG_INFO<<"finish processing all actions";
        return results;
    }
}

namespace lookup
{
    json::ostream& operator ^ (json::ostream& js, const table_entry& stat)
    {
        using namespace json::literals;
        js ^ "source"_n ^ stat.source ^ "target"_n ^ stat.target;
        return js;
    }
}


text:
/// Function call model class definition.
///
/// PlnFunctionCall call function with arguments
/// and get return values.
/// e.g. ) funcion(a, b) -> c, d;
///
/// @file	PlnFunctionCall.cpp
/// @copyright	2017-2021 YAMAGUCHI Toshinobu

#include <boost/assert.hpp>

#include "../../PlnConstants.h"
#include "PlnFunctionCall.h"
#include "../PlnFunction.h"
#include "../PlnVariable.h"
#include "../PlnType.h"
#include "../../PlnModel.h"
#include "../../PlnDataAllocator.h"
#include "../../PlnGenerator.h"
#include "../../PlnScopeStack.h"
#include "PlnClone.h"
#include "PlnArrayValue.h"
#include "../../PlnMessage.h"
#include "../../PlnException.h"

static PlnFunction* internalFuncs[IFUNC_NUM] = { NULL };
static bool is_init_ifunc = false;

// PlnFunctionCall
PlnFunctionCall::PlnFunctionCall(PlnFunction* f)
	: PlnExpression(ET_FUNCCALL), function(f)
{
	for (auto& rv: f->return_vals) {
		PlnValue val;
		val.type = VL_WORK;
		val.inf.wk_type = rv.var_type;
		values.push_back(val);
	}
}

PlnFunctionCall::PlnFunctionCall(PlnFunction* f, vector<PlnArgument>& args)
	: PlnFunctionCall(f)
{
	arguments = move(args);
}

PlnFunctionCall::PlnFunctionCall(PlnFunction* f, vector<PlnExpression*>& args)
	: PlnFunctionCall(f)
{
	vector<PlnParameter*> &iparams = f->parameters;
	BOOST_ASSERT(args.size() == iparams.size());

	int p_ind = 0;
	for (auto arg_ex: args) {
		BOOST_ASSERT(arg_ex->values.size() == 1);
		arguments.push_back({arg_ex});
		auto &arg = arguments.back();
		arg.inf.push_back({iparams[p_ind], PIO_INPUT});
		p_ind++;
	}
}

PlnFunctionCall::~PlnFunctionCall()
{
	for (auto a: arguments)
		delete a.exp;
	for (auto fv: free_vars)
		delete fv;
	for (auto fex: free_exs)
		delete fex;
}

void PlnFunctionCall::loadArgDps(PlnDataAllocator& da)
{
	PlnFunction* f = function;
	BOOST_ASSERT(!arguments.size());

	arg_dps = f->createArgDps();
	da.setArgDps(f->type, arg_dps, false);
}

static vector<PlnDataPlace*> loadArgs(PlnFunctionCall *fcall, PlnDataAllocator& da, PlnScopeInfo& si)
{
	auto f = fcall->function;
	auto& clones = fcall->clones;
	auto arg_dps = f->createArgDps();

	if (f->has_va_arg) {
		// Add variable arguments data types.
		for (auto &arg: fcall->arguments) {
			int i=0;
			for (auto& inf: arg.inf) {
				if (inf.param->passby == FPM_IN_VARIADIC) {
					PlnDataPlace* dp = new PlnDataPlace(8, arg.exp->getDataType(i));
					dp->status = DS_READY_ASSIGN;
					arg_dps.push_back(dp);

				} else if (inf.param->passby == FPM_OUT_VARIADIC) {
					PlnDataPlace* dp = new PlnDataPlace(8, DT_OBJECT_REF);
					dp->status = DS_READY_ASSIGN;
					arg_dps.push_back(dp);
				}
				i++;
			}
		}
	}

	da.setArgDps(f->type, arg_dps, false);

	int j = 0;
	for (auto &arg: fcall->arguments) {
		int vi = 0;
		for (auto &v: arg.exp->values) {
			PlnArgValueInf& argval = arg.inf[vi];
			int dp_i = argval.param->index;
			if (argval.va_idx >= 0)
				dp_i += argval.va_idx;
			BOOST_ASSERT(arg_dps[dp_i]->data_type != DT_UNKNOWN);

			if (argval.opt == AG_MOVE && v.type == VL_VAR) {
				arg_dps[dp_i]->do_clear_src = true;
			}

			PlnClone* clone = NULL;
			if (argval.param->passby == FPM_IN_BYREF_CLONE) {
				BOOST_ASSERT(v.type == VL_LIT_ARRAY || v.type == VL_VAR || v.type == VL_WORK || v.type == VL_LIT_STR);
				if (v.type == VL_LIT_ARRAY) {
					PlnExpression* tmp = v.inf.arrValue;
					v.inf.arrValue = NULL;	 // == arg.exp->values[vi].inf.arrValue;
					delete arg.exp;
					arg.exp = tmp;
				}

				if (v.type == VL_WORK) {
					if (v.inf.wk_type->mode[ALLOC_MD] != 'h') {
						clone = new PlnClone(da, arg.exp, argval.param->var->var_type, false);
					}
				} else {
					clone = new PlnClone(da, arg.exp, argval.param->var->var_type, false);
				}

			} else if (argval.param->passby == FPM_IN_BYREF) {
				// reference paramater
				if (v.type == VL_WORK && v.inf.wk_type->mode[ALLOC_MD]=='h') { // e.g. return value of function
					BOOST_ASSERT(v.inf.wk_type->mode[ALLOC_MD]=='h');
					// needs to free after call func.
					PlnVariable *tmp_var = PlnVariable::createTempVar(da, argval.param->var->var_type, "free_var");
					da.pushSrc(tmp_var->place, arg_dps[dp_i], false);
					PlnExpression *free_ex = PlnFreer::getFreeEx(tmp_var);

					fcall->free_work_vars.push_back(tmp_var);
					fcall->free_exs.push_back(free_ex);
				}

			}

			if ((argval.param->passby == FPM_IN_BYREF && arg.exp->getDataType(vi) != DT_OBJECT_REF)
				|| (argval.param->passby == FPM_OUT_BYREF && arg.exp->getDataType(vi) != DT_OBJECT_REF)
				|| (argval.param->passby == FPM_OUT_VARIADIC && arg.exp->getDataType(vi) != DT_OBJECT_REF)
				|| (argval.param->passby == FPM_OUT_BYREFADDR_GETOWNER)
				|| (argval.param->passby == FPM_OUT_BYREFADDR)) {
				arg_dps[dp_i]->load_address = true;
			}

			if (clone)
				clone->finishAlloc(da, si);
			else
				arg.exp->data_places.push_back(arg_dps[dp_i]);

			clones.push_back(clone);

			vi++;
		}

		arg.exp->finish(da, si);

		vi = 0;
		for (auto &v: arg.exp->values) {
			PlnArgValueInf& argval = arg.inf[vi];
			int dp_i = argval.param->index;
			if (argval.va_idx >= 0)
				dp_i += argval.va_idx;

			if (arg.inf[vi].opt == AG_MOVE && v.type == VL_VAR) {
				auto var = v.inf.var;
				// Check if variable can write.
				if (var->var_type->mode[IDENTITY_MD] != 'm') {
					PlnCompileError err(E_CantUseMoveOwnershipFrom, var->name);
					err.loc = arg.exp->loc;
					throw err;
				}

				// Mark as freed variable.
				if (!si.exists_current(var))
					si.push_owner_var(var);
				si.set_lifetime(var, VLT_FREED);
			}
			vi++;

			if (clones[j]) {
				clones[j]->data_places.push_back(arg_dps[dp_i]);
				clones[j]->finish(da, si);
			}
			++j;
		}
	}

	return arg_dps;
}

void PlnFunctionCall::finish(PlnDataAllocator& da, PlnScopeInfo& si)
{
	int func_type = function->type;

	if (arguments.size())
		arg_dps = loadArgs(this, da, si);

	for (auto dp: arg_dps)
		da.popSrc(dp);

	for (auto free_work_var: free_work_vars)
		da.popSrc(free_work_var->place);

	da.funcCalled(arg_dps, func_type, function->never_return);

	ret_dps = function->createRetValDps();
	da.setRetValDps(function->type, ret_dps, false);

	int i = 0;
	for (auto r: function->return_vals) {
		ret_dps[i]->data_type = r.local_var->var_type->data_type();
		++i;
	}

	for (i=0; i<ret_dps.size(); ++i) {
		da.allocDp(ret_dps[i]);
		if (i >= data_places.size()) {
			if (function->return_vals[i].var_type->mode[ALLOC_MD] == 'h') {
				PlnVariable *tmp_var = PlnVariable::createTempVar(da, function->return_vals[i].local_var->var_type, "ret" + std::to_string(i));
				PlnExpression *free_ex = PlnFreer::getFreeEx(tmp_var);

				free_vars.push_back(tmp_var);
				free_exs.push_back(free_ex);

				da.pushSrc(tmp_var->place, ret_dps[i]);
			} else {
				da.releaseDp(ret_dps[i]);
			}
		}
	}

	i=0;
	for (auto dp: data_places) {
		da.pushSrc(dp, ret_dps[i]);
		i++;
	}
	for (auto free_var: free_vars) {
		da.popSrc(free_var->place);
	}

	for (auto free_ex: free_exs)
		free_ex->finish(da, si);

	for (auto free_work_var: free_work_vars)
		da.releaseDp(free_work_var->place);

	for (auto free_var: free_vars)
		da.releaseDp(free_var->place);

}

void PlnFunctionCall::gen(PlnGenerator &g)
{
	function->call_count++;
	switch (function->type) {
		case FT_PLN:
		{
			int i=0;
			for (auto arg: arguments) {
				if (clones[i]) clones[i]->genAlloc(g);
				arg.exp->gen(g);
				if (clones[i]) clones[i]->gen(g);
				i++;
			}

			for (auto dp: arg_dps)
				g.genLoadDp(dp, false);

			for (auto dp: arg_dps)
				g.genSaveDp(dp);

			vector<int> arg_dtypes;
			for (auto dp: arg_dps)
				arg_dtypes.push_back(dp->data_type);

			for (auto free_work_var: free_work_vars)
				g.genLoadDp(free_work_var->place);

			g.genCCall(function->asm_name, arg_dtypes, function->has_va_arg);

			for (auto dp: data_places)
				g.genSaveSrc(dp);

			for (auto free_var: free_vars)
				g.genLoadDp(free_var->place);

			for (auto free_ex: free_exs)
				free_ex->gen(g);

			break;
		}
		case FT_SYS:
		{
			for (auto arg: arguments)
				arg.exp->gen(g);

			for (auto dp: arg_dps)
				g.genLoadDp(dp, false);

			for (auto dp: arg_dps)
				g.genSaveDp(dp);

			g.genSysCall(function->inf.syscall.id, function->asm_name);

			break;
		}
		case FT_C:
		{
			int i=0;
			for (auto arg: arguments) {
				if (clones[i]) clones[i]->genAlloc(g);
				arg.exp->gen(g);
				if (clones[i]) clones[i]->gen(g);
				i++;
			}
			//for (auto& arg: arguments)
			//	arg.exp->gen(g);

			for (auto dp: arg_dps)
				g.genLoadDp(dp, false);

			for (auto dp: arg_dps)
				g.genSaveDp(dp);

			vector<int> arg_dtypes;
			for (auto dp: arg_dps)
				arg_dtypes.push_back(dp->data_type);

			g.genCCall(function->asm_name, arg_dtypes, function->has_va_arg);

			for (auto dp: data_places)
				g.genSaveSrc(dp);

			break;
		}
		default:
			BOOST_ASSERT(false);
	}
}

static void initInternalFunctions()
{
	PlnFunction* f;
	string ret_name = "";

	f = new PlnFunction(FT_C, "__malloc");
	f->asm_name = "malloc";
	f->addParam("size", PlnType::getSint()->getVarType(), PIO_INPUT, FPM_IN_BYVAL, NULL);
	f->addRetValue(ret_name, PlnType::getObject()->getVarType());
	internalFuncs[IFUNC_MALLOC] = f;

	f = new PlnFunction(FT_C, "__free");
	f->asm_name = "free";
	f->addParam("ptr", PlnType::getObject()->getVarType(), PIO_INPUT, FPM_IN_BYREF, NULL);
	internalFuncs[IFUNC_FREE] = f;

	f = new PlnFunction(FT_C, "__exit");
	f->asm_name = "exit";
	f->addParam("status", PlnType::getSint()->getVarType(), PIO_INPUT, FPM_IN_BYVAL, NULL);
	f->never_return = true;
	internalFuncs[IFUNC_EXIT] = f;
}

PlnFunction* PlnFunctionCall::getInternalFunc(PlnInternalFuncType func_type)
{
	BOOST_ASSERT(func_type < IFUNC_NUM);
	if (!is_init_ifunc) {
		initInternalFunctions();
		is_init_ifunc = true;
	}

	return internalFuncs[func_type];
}

max_stars_repo_name: tosyama/palan

max_issues_repo_name: tosyama/palan

max_forks_repo_name: tosyama/palan


text:
#pragma once

#include "interfaces/json_storage.hpp"
#include "interfaces/metric.hpp"
#include "interfaces/report.hpp"
#include "interfaces/report_manager.hpp"
#include "types/report_types.hpp"

#include <boost/asio/io_context.hpp>
#include <boost/asio/steady_timer.hpp>
#include <sdbusplus/asio/object_server.hpp>

#include <chrono>
#include <memory>

class Report : public interfaces::Report
{
  public:
    Report(boost::asio::io_context& ioc,
           const std::shared_ptr<sdbusplus::asio::object_server>& objServer,
           const std::string& reportName, const std::string& reportingType,
           const bool emitsReadingsSignal,
           const bool logToMetricReportsCollection, const Milliseconds period,
           interfaces::ReportManager& reportManager,
           interfaces::JsonStorage& reportStorage,
           std::vector<std::shared_ptr<interfaces::Metric>> metrics);
    ~Report() = default;

    Report(const Report&) = delete;
    Report(Report&&) = delete;
    Report& operator=(const Report&) = delete;
    Report& operator=(Report&&) = delete;

    std::string getName() const override
    {
        return name;
    }

    std::string getPath() const override
    {
        return path;
    }

    void updateReadings() override;
    bool storeConfiguration() const;

  private:
    std::unique_ptr<sdbusplus::asio::dbus_interface> makeReportInterface();
    static void timerProc(boost::system::error_code, Report& self);
    void scheduleTimer(Milliseconds interval);

    const std::string name;
    const std::string path;
    std::string reportingType;
    Milliseconds interval;
    bool emitsReadingsUpdate;
    bool logToMetricReportsCollection;
    ReadingParametersPastVersion readingParametersPastVersion;
    ReadingParameters readingParameters;
    bool persistency = false;
    Readings cachedReadings = {};
    Readings readings = {};
    std::shared_ptr<sdbusplus::asio::object_server> objServer;
    std::unique_ptr<sdbusplus::asio::dbus_interface> reportIface;
    std::unique_ptr<sdbusplus::asio::dbus_interface> deleteIface;
    std::vector<std::shared_ptr<interfaces::Metric>> metrics;
    boost::asio::steady_timer timer;

    interfaces::JsonStorage::FilePath fileName;
    interfaces::JsonStorage& reportStorage;

  public:
    static constexpr const char* reportIfaceName =
        "xyz.openbmc_project.Telemetry.Report";
    static constexpr const char* reportDir =
        "/xyz/openbmc_project/Telemetry/Reports/";
    static constexpr const char* deleteIfaceName =
        "xyz.openbmc_project.Object.Delete";
    static constexpr size_t reportVersion = 4;
};

max_stars_repo_name: lxwinspur/telemetry

max_issues_repo_name: lxwinspur/telemetry

max_forks_repo_name: lxwinspur/telemetry


text:
// VMDモーションを間引く

#include <vector>
#include <Eigen/Core>
#include "VMD.h"
#include "interpolate.h"
#include "reducevmd.h"

#define _USE_MATH_DEFINES
#include <math.h>

// head番目からtail番目のボーンキーフレームのうち、残すべきものを再帰的に探して返す。
// ただし返値にtail番目のボーンは含まれないので、コール元で追加する必要がある。
vector<VMD_Frame> reduce_bone_frame_recursive(const vector<VMD_Frame>& v, int head, int tail, float threshold_pos, float threshold_rot, bool bezier)
{
  float max_pos_err = 0.0;
  float max_rot_err = 0.0;
  int max_idx_pos = 0;
  int max_idx_rot = 0;
  VMD_Frame head_frame = v[head];
  VMD_Frame tail_frame = v[tail];
  const int bezier_interpolation_limit = 60;
  if (bezier && tail - head < bezier_interpolation_limit) {
    optimize_bezier_parameter(tail_frame, v, head, tail);
  }

  // headフレームの値とtailフレームの値によって決まる補完曲線(直線)から最も離れた(誤差の大きい)フレームを探す
  for (int i = head + 1; i < tail; i++) {
    VMD_Frame f = interpolate_frame(head_frame, tail_frame, i, bezier);
    float pos_err = (f.position - v[i].position).norm();
    if (pos_err > max_pos_err) {
      max_idx_pos = i;
      max_pos_err = pos_err;
    }
    float rot_err = fabs(f.rotation.angularDistance(v[i].rotation) * 180 / M_PI);
    if (rot_err > max_rot_err) {
      max_idx_rot = i;
      max_rot_err = rot_err;
    }
  }

  // 補間曲線から最も離れたフレームの誤差が閾値を超えていたら、そのフレーム(max_idx_*)を残し、
  // [head, max_idx_*] と [max_idx_*, tail] のそれぞれの区間を再帰的に探す。
  vector<VMD_Frame> v1;
  if (max_pos_err > threshold_pos) {
    v1 = reduce_bone_frame_recursive(v, head, max_idx_pos, threshold_pos, threshold_rot, bezier);
    vector<VMD_Frame> v2 = reduce_bone_frame_recursive(v, max_idx_pos, tail, threshold_pos, threshold_rot, bezier);
    v1.insert(v1.end(), v2.begin(), v2.end());
  } else {
    if (max_rot_err > threshold_rot) {
      v1 = reduce_bone_frame_recursive(v, head, max_idx_rot, threshold_pos, threshold_rot, bezier);
      vector<VMD_Frame> v2 = reduce_bone_frame_recursive(v, max_idx_rot, tail, threshold_pos, threshold_rot, bezier);
      v1.insert(v1.end(), v2.begin(), v2.end());
    } else {
      v1.push_back(tail_frame);
    }
  }
  return v1;
}

// head番目からtail番目のボーンキーフレームのうち、残すべきものを探して返す。
vector<VMD_Frame> reduce_bone_frame(const vector<VMD_Frame>& v, int head, int tail, float threshold_pos, float threshold_rot, bool bezier)
{
  if (threshold_pos < 0 || threshold_rot < 0) {
    vector<VMD_Frame> v1(v);
    return v1;
  }

  vector<VMD_Frame> v1 = reduce_bone_frame_recursive(v, head, tail, threshold_pos, threshold_rot, bezier);
  v1.insert(v1.begin(), v.front());

  return v1;
}

// head番目からtail番目の表情キーフレームのうち、残すべきものを再帰的に探して返す。
// ただし返値にtail番目のフレームは含まれないので、コール元で追加する必要がある。
vector<VMD_Morph> reduce_morph_frame_recursive(const vector<VMD_Morph>& v, int head, int tail, float threshold)
{
  float max = 0.0;
  int max_idx = 0;
  int total = tail - head;
  for (int i = head + 1; i < tail; i++) {
    float iv = v[head].weight + (v[tail].weight - v[head].weight) * (i - head) / total;
    float e = abs(iv - v[i].weight);
    if (e > max) {
      max_idx = i;
      max = e;
    }
  }

  vector<VMD_Morph> v1;
  if (max > threshold) {
    v1 = reduce_morph_frame_recursive(v, head, max_idx, threshold);
    vector<VMD_Morph> v2 = reduce_morph_frame_recursive(v, max_idx, tail, threshold);
    v1.insert(v1.end(), v2.begin(), v2.end());
  } else {
    v1.push_back(v[head]);
  }
  return v1;
}

// head番目からtail番目の表情キーフレームのうち、残すべきものを探して返す。
vector<VMD_Morph> reduce_morph_frame(const vector<VMD_Morph>& v, int head, int tail, float threshold)
{
  if (threshold < 0) {
    vector<VMD_Morph> v1(v);
    return v1;
  }

  vector<VMD_Morph> v1 = reduce_morph_frame_recursive(v, head, tail, threshold);
  v1.push_back(v.back());
  return v1;
}

max_stars_repo_name: ikeno-ikeo/readfacevmd

max_issues_repo_name: ikeno-ikeo/readfacevmd

max_forks_repo_name: ikeno-ikeo/readfacevmd


text:
#include <boost/tokenizer.hpp>
#include <string>
#include <iostream>

int main()
{
  typedef boost::tokenizer<boost::char_separator<char>> tokenizer;
  std::string s = "Boost C++ Libraries";
  tokenizer tok{s};
  for (tokenizer::iterator it = tok.begin(); it != tok.end(); ++it)
    std::cout << *it << '\n';
}

max_stars_repo_name: KwangjoJeong/Boost

max_issues_repo_name: KwangjoJeong/Boost

max_forks_repo_name: KwangjoJeong/Boost


text:
/**
 ** Isaac Genome Alignment Software
 ** Copyright (c) 2010-2017 Illumina, Inc.
 ** All rights reserved.
 **
 ** This software is provided under the terms and conditions of the
 ** GNU GENERAL PUBLIC LICENSE Version 3
 **
 ** You should have received a copy of the GNU GENERAL PUBLIC LICENSE Version 3
 ** along with this program. If not, see
 ** <https://github.com/illumina/licenses/>.
 **
 ** \file FiniteCapacityVector.hh
 **
 ** Vector that passes its allocator to contained objects.
 **
 ** \author Roman Petrovski
 **/

#ifndef iSAAC_COMMON_SAME_ALLOCATOR_VECTOR_HH
#define iSAAC_COMMON_SAME_ALLOCATOR_VECTOR_HH

#include <initializer_list>
#include <boost/foreach.hpp>

namespace isaac
{
namespace common
{

template <typename ValueT, typename AllocatorT>
class SameAllocatorVector : std::vector<ValueT, typename AllocatorT::template rebind<ValueT>::other>
{
public:
    typedef std::vector<ValueT, typename AllocatorT::template rebind<ValueT>::other> BaseT;

public:
    using typename BaseT::value_type;
    typedef typename BaseT::allocator_type allocator_type;

    explicit SameAllocatorVector(const std::size_t s)
        : BaseT(s)
    {
//        ISAAC_THREAD_CERR << "SameAllocatorVector<" <<  demangle(typeid(ValueT).name()) <<
//            ">(const std::size_t s)" << std::endl;
    }

    SameAllocatorVector(const std::size_t s, const value_type &v)
        : BaseT(s, v)
    {
//        ISAAC_THREAD_CERR << "SameAllocatorVector<" <<  demangle(typeid(ValueT).name()) <<
//            ">(const std::size_t s)" << std::endl;
    }

    ~SameAllocatorVector()
    {
//        ISAAC_THREAD_CERR << "SameAllocatorVector<" <<  demangle(typeid(ValueT).name()) <<
//            ">~SameAllocatorVector()" << std::endl;
    }

    SameAllocatorVector()
    {
//        ISAAC_THREAD_CERR << "SameAllocatorVector<" <<  demangle(typeid(ValueT).name()) <<
//            ">()" << std::endl;
    }

    SameAllocatorVector(const SameAllocatorVector &that)
        : BaseT(that)
    {
//        ISAAC_THREAD_CERR << "SameAllocatorVector<" <<  demangle(typeid(ValueT).name()) <<
//            ">(const SameAllocatorVector &that)" << std::endl;
    }

    SameAllocatorVector(const SameAllocatorVector &that, const AllocatorT &allocator)
        : BaseT(allocator)
    {
        BaseT::resize(that.size());
        int i = 0;
        BOOST_FOREACH(const value_type & v, that)
        {
            (*this)[i++] = value_type(v, typename BaseT::value_type::allocator_type(allocator));
        }
//        ISAAC_THREAD_CERR << "SameAllocatorVector<" <<  demangle(typeid(ValueT).name()) <<
//            ">(const SameAllocatorVector &that, const AllocatorT &allocator)" << std::endl;
    }

    SameAllocatorVector(SameAllocatorVector &&that)
    {
        swap(that);
//        ISAAC_THREAD_CERR << "SameAllocatorVector<" <<  demangle(typeid(ValueT).name()) <<
//            ">(SameAllocatorVector &&that)" << std::endl;
    }

    template<typename InputIterator>
    SameAllocatorVector(InputIterator first, InputIterator last)
    : BaseT(first, last)
    {
//        ISAAC_THREAD_CERR << "SameAllocatorVector<" <<  demangle(typeid(ValueT).name()) <<
//            ">(InputIterator first, InputIterator last)" << std::endl;
    }

    template<typename InputIterator>
    SameAllocatorVector(InputIterator first, InputIterator last, const AllocatorT& a)
    : BaseT(first, last, a)
    {
//        ISAAC_THREAD_CERR << "SameAllocatorVector<" <<  demangle(typeid(ValueT).name()) <<
//            ">(InputIterator first, InputIterator last, const AllocatorT& a))" << std::endl;
    }

    SameAllocatorVector & operator=(SameAllocatorVector &&that)
    {
        swap(that);
//        ISAAC_THREAD_CERR << "SameAllocatorVector<" <<  demangle(typeid(ValueT).name()) <<
//            ">::operator=(SameAllocatorVector &&that)" << std::endl;
        return *this;
    }

    void swap(SameAllocatorVector &that)
    {
        BaseT::swap(that);
    }

    // for some reason vector::push_back(&&) causes copy constructor call
    void push_back(value_type&& __x)
    {
        this->resize(this->size() + 1);
        using std::swap; swap(this->back(), __x);
    }

    typedef typename BaseT::iterator iterator;
    typedef typename BaseT::const_iterator const_iterator;

    using BaseT::operator [];
    using BaseT::at;
    using BaseT::push_back;
    using BaseT::size;
    using BaseT::begin;
    using BaseT::end;
    using BaseT::front;
    using BaseT::reserve;
    using BaseT::resize;
    using BaseT::empty;
    using BaseT::clear;
};

template <typename ValueT, typename AllocatorT>
class SameAllocatorVectorEnd : std::vector<ValueT, typename AllocatorT::template rebind<ValueT>::other>
{
public:
    typedef std::vector<ValueT, typename AllocatorT::template rebind<ValueT>::other> BaseT;

public:
    using typename BaseT::value_type;
    typedef typename BaseT::allocator_type allocator_type;

    explicit SameAllocatorVectorEnd(const std::size_t s)
        : BaseT(s)
    {
//        ISAAC_THREAD_CERR << "SameAllocatorVectorEnd<" <<  demangle(typeid(ValueT).name()) <<
//            ">(const std::size_t s)" << std::endl;
    }

    SameAllocatorVectorEnd(const std::size_t s, const value_type v)
        : BaseT(s, v)
    {
//        ISAAC_THREAD_CERR << "SameAllocatorVectorEnd<" <<  demangle(typeid(ValueT).name()) <<
//            ">(const std::size_t s, const value_type v)" << std::endl;
    }

    ~SameAllocatorVectorEnd()
    {
//        ISAAC_THREAD_CERR << "SameAllocatorVectorEnd<" <<  demangle(typeid(ValueT).name()) <<
//            ">~SameAllocatorVectorEnd()" << std::endl;
    }

    SameAllocatorVectorEnd()
    {
//        ISAAC_THREAD_CERR << "SameAllocatorVectorEnd<" <<  demangle(typeid(ValueT).name()) <<
//            ">()" << std::endl;
    }

    SameAllocatorVectorEnd(const SameAllocatorVectorEnd &that)
        : BaseT(that)
    {
//        ISAAC_THREAD_CERR << "SameAllocatorVectorEnd<" <<  demangle(typeid(ValueT).name()) <<
//            ">(const SameAllocatorVectorEnd &that)" << std::endl;
    }

    SameAllocatorVectorEnd(const SameAllocatorVectorEnd &that, const AllocatorT &allocator)
        : BaseT(allocator)
    {
//        static_cast<BaseT&>(*this) = that;
        BaseT::resize(that.size());
        int i = 0;
        BOOST_FOREACH(const value_type & v, that)
        {
            (*this)[i++] = v;//value_type(v, typename BaseT::value_type::allocator_type(allocator));
        }
//        ISAAC_THREAD_CERR << "SameAllocatorVectorEnd<" <<  demangle(typeid(ValueT).name()) <<
//            ">(const SameAllocatorVectorEnd &that, const AllocatorT &allocator)" << std::endl;
    }

    SameAllocatorVectorEnd(SameAllocatorVectorEnd &&that)
    {
        swap(that);
//        ISAAC_THREAD_CERR << "SameAllocatorVectorEnd<" <<  demangle(typeid(ValueT).name()) <<
//            ">(SameAllocatorVectorEnd &&that)" << std::endl;
    }

    SameAllocatorVectorEnd & operator=(SameAllocatorVectorEnd &&that)
    {
        swap(that);
//        ISAAC_THREAD_CERR << "SameAllocatorVectorEnd<" <<  demangle(typeid(ValueT).name()) <<
//            ">::operator=(SameAllocatorVectorEnd &&that)" << std::endl;
        return *this;
    }

    void swap(SameAllocatorVectorEnd &that)
    {
        BaseT::swap(that);
    }

    typedef typename BaseT::iterator iterator;
    typedef typename BaseT::const_iterator const_iterator;

    using BaseT::operator [];
    using BaseT::at;
    using BaseT::push_back;
    using BaseT::size;
    using BaseT::begin;
    using BaseT::end;
    using BaseT::front;
    using BaseT::reserve;
    using BaseT::resize;
    using BaseT::empty;
};

} //namespace common
} //namespace isaac

#endif // #ifndef iSAAC_COMMON_SAME_ALLOCATOR_VECTOR_HH


text:
// Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
// Use of this source code is governed by a apache 2.0 license that can be
// found in the LICENSE file.

#include "common/step/backup/step_backup_manifest.h"

#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/system/error_code.hpp>

#include <pkgmgr-info.h>
#include <pkgmgr_installer.h>

#include <algorithm>
#include <string>

#include "common/paths.h"
#include "common/utils/file_util.h"

namespace bf = boost::filesystem;
namespace bs = boost::system;

namespace common_installer {
namespace backup {

Step::Status StepBackupManifest::precheck() {
  if (!bf::exists(context_->xml_path.get())) {
    LOG(ERROR) << "Xml manifest file does not exist";
    return Status::MANIFEST_NOT_FOUND;
  }
  return Status::OK;
}

Step::Status StepBackupManifest::process() {
  // set backup file path
  bf::path backup_xml_path =
      GetBackupPathForManifestFile(context_->xml_path.get());
  context_->backup_xml_path.set(backup_xml_path);
  bs::error_code error;
  bf::copy(context_->xml_path.get(), context_->backup_xml_path.get(), error);
  if (error) {
    LOG(ERROR) << "Failed to make a copy of xml manifest file";
    return Status::MANIFEST_ERROR;
  }
  LOG(DEBUG) << "Manifest backup created";
  return Status::OK;
}

Step::Status StepBackupManifest::clean() {
  bs::error_code error;
  bf::remove(context_->backup_xml_path.get(), error);
  if (error) {
    LOG(WARNING) << "Cannot remove backup manifest file";
    return Status::MANIFEST_ERROR;
  }
  LOG(DEBUG) << "Manifest backup removed";
  return Status::OK;
}

Step::Status StepBackupManifest::undo() {
  if (bf::exists(context_->backup_xml_path.get())) {
    bs::error_code error;
    bf::remove(context_->xml_path.get(), error);
    if (error) {
      LOG(ERROR) << "Failed to remove newly generated xml file in revert";
      return Status::MANIFEST_ERROR;
    }
    if (!MoveFile(context_->backup_xml_path.get(),
        context_->xml_path.get())) {
      LOG(ERROR) << "Failed to revert a content of xml manifest file";
      return Status::MANIFEST_ERROR;
    }
    LOG(DEBUG) << "Manifest reverted from backup";
  }
  return Status::OK;
}

}  // namespace backup
}  // namespace common_installer


text:
//=======================================================================
// Copyright 2015 - 2020 Jeff Linahan
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//=======================================================================
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/properties.hpp>
#include <boost/graph/breadth_first_search.hpp>
#include <iostream>
using namespace std;

typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::property<boost::vertex_index_t, uint>, boost::property<boost::edge_index_t, uint>> Graph;
typedef boost::graph_traits<Graph>::vertex_descriptor                       vertex_t;
typedef boost::graph_traits<Graph>::edge_descriptor                         edge_t;
typedef boost::graph_traits<Graph>::vertex_descriptor                       vertex_t;

struct CollectBFSVisitor : public boost::default_bfs_visitor
{
	virtual void initialize_vertex(vertex_t v, Graph const& g) {cout << "init vertex\n";};
	virtual void discover_vertex(vertex_t v, Graph const& g) {};
	virtual void examine_vertex(vertex_t v, Graph const& g) {};
	virtual void finish_vertex(vertex_t v, Graph const& g) {};
	virtual void black_target(edge_t, Graph const& g) {};
	virtual void gray_target(edge_t, Graph const& g) {};
	virtual void tree_target(edge_t, Graph const& g) {};
	virtual void tree_edge(edge_t, Graph const& g) {};
	virtual void non_tree_edge(edge_t, Graph const& g) {};
};

struct VertBuffer : boost::queue<vertex_t>
{
};

int main()
{
        Graph g;
        vertex_t x = add_vertex(g);
	auto indexmap = boost::get(boost::vertex_index, g);
	auto colormap = boost::make_vector_property_map<boost::default_color_type>(indexmap);

	VertBuffer q;

        CollectBFSVisitor v;
        breadth_first_search(g, x, q, v, colormap);
}

max_stars_repo_name: jeffythedragonslayer/lipton-tarjan

max_issues_repo_name: jeffythedragonslayer/lipton-tarjan

max_forks_repo_name: jeffythedragonslayer/lipton-tarjan


text:
#include <benchmark/benchmark.h>

#include <boost/container/pmr/monotonic_buffer_resource.hpp>
#include <boost/container/pmr/unsynchronized_pool_resource.hpp>
#include <boost/container/pmr/vector.hpp>

constexpr size_t vector_elements_1{4 * 1024};
constexpr size_t vector_elements_2{1024 * 1024};

static void allocations_boost_pmr_pure_vector_pb(benchmark::State& state) {
  const auto vector_elements{state.range(0)};
  for (auto _ : state) {
    std::vector<size_t> v;
    for (size_t i{0}; i < vector_elements; ++i) {
      v.push_back(i);
    }
    state.SetItemsProcessed(vector_elements);
    benchmark::DoNotOptimize(v);
  }
}
BENCHMARK(allocations_boost_pmr_pure_vector_pb)
    ->Arg(vector_elements_1)
    ->Arg(vector_elements_2);

static void allocations_boost_pmr_pure_vector_eb(benchmark::State& state) {
  const auto vector_elements{state.range(0)};
  for (auto _ : state) {
    std::vector<size_t> v;
    for (size_t i{0}; i < vector_elements; ++i) {
      v.emplace_back(i);
    }
    state.SetItemsProcessed(vector_elements);
    benchmark::DoNotOptimize(v);
  }
}
BENCHMARK(allocations_boost_pmr_pure_vector_eb)
    ->Arg(vector_elements_1)
    ->Arg(vector_elements_2);

static void allocations_boost_pmr_reserve_vector_pb(benchmark::State& state) {
  const auto vector_elements{state.range(0)};
  for (auto _ : state) {
    std::vector<size_t> v;
    v.reserve(vector_elements);
    for (size_t i{0}; i < vector_elements; ++i) {
      v.push_back(i);
    }
    state.SetItemsProcessed(vector_elements);
    benchmark::DoNotOptimize(v);
  }
}
BENCHMARK(allocations_boost_pmr_reserve_vector_pb)
    ->Arg(vector_elements_1)
    ->Arg(vector_elements_2);

static void allocations_boost_pmr_reserve_vector_eb(benchmark::State& state) {
  const auto vector_elements{state.range(0)};
  for (auto _ : state) {
    std::vector<size_t> v;
    v.reserve(vector_elements);
    for (size_t i{0}; i < vector_elements; ++i) {
      v.emplace_back(i);
    }
    state.SetItemsProcessed(vector_elements);
    benchmark::DoNotOptimize(v);
  }
}
BENCHMARK(allocations_boost_pmr_reserve_vector_eb)
    ->Arg(vector_elements_1)
    ->Arg(vector_elements_2);

static void allocations_boost_pmr_pmr_vector_pb(benchmark::State& state) {
  const auto vector_elements{state.range(0)};
  for (auto _ : state) {
    auto pool_options{boost::container::pmr::pool_options{}};
    pool_options.max_blocks_per_chunk = 1024;
    pool_options.largest_required_pool_block = 1024 * 1024 * 8;
    const size_t resource_size = 20 * vector_elements * sizeof(int) + 1024;
    char buffer[resource_size];
    boost::container::pmr::monotonic_buffer_resource monotonic_resource{
        &buffer, resource_size};
    boost::container::pmr::unsynchronized_pool_resource resource{
        pool_options, &monotonic_resource};
    boost::container::pmr::polymorphic_allocator<int> alloc{&resource};
    boost::container::pmr::vector<int> v;
    for (size_t i{0}; i < vector_elements; ++i) {
      v.push_back(i);
    }
    state.SetItemsProcessed(vector_elements);
    benchmark::DoNotOptimize(v);
  }
}
BENCHMARK(allocations_boost_pmr_pmr_vector_pb)
    ->Arg(vector_elements_1)
    ->Arg(vector_elements_2);

static void allocations_boost_pmr_pmr_vector_eb(benchmark::State& state) {
  const auto vector_elements{state.range(0)};
  for (auto _ : state) {
    const size_t resource_size = 20 * vector_elements * sizeof(int) + 1024;
    char buffer[resource_size];
    boost::container::pmr::monotonic_buffer_resource monotonic_resource{
        &buffer, resource_size};
    boost::container::pmr::unsynchronized_pool_resource resource{
        &monotonic_resource};
    boost::container::pmr::polymorphic_allocator<int> alloc{&resource};
    boost::container::pmr::vector<int> v;
    for (size_t i{0}; i < vector_elements; ++i) {
      v.emplace_back(i);
    }
    state.SetItemsProcessed(vector_elements);
    benchmark::DoNotOptimize(v);
  }
}
BENCHMARK(allocations_boost_pmr_pmr_vector_eb)
    ->Arg(vector_elements_1)
    ->Arg(vector_elements_2);

static void allocations_boost_pmr_pmr_reserve_vector_pb(
    benchmark::State& state) {
  const auto vector_elements{state.range(0)};
  for (auto _ : state) {
    const size_t resource_size = 20 * vector_elements * sizeof(int) + 1024;
    char buffer[resource_size];
    boost::container::pmr::monotonic_buffer_resource monotonic_resource{
        &buffer, resource_size};
    boost::container::pmr::unsynchronized_pool_resource resource{
        &monotonic_resource};
    boost::container::pmr::polymorphic_allocator<int> alloc{&resource};
    boost::container::pmr::vector<int> v;
    v.reserve(vector_elements);
    for (size_t i{0}; i < vector_elements; ++i) {
      v.push_back(i);
    }
    state.SetItemsProcessed(vector_elements);
    benchmark::DoNotOptimize(v);
  }
}
BENCHMARK(allocations_boost_pmr_pmr_reserve_vector_pb)
    ->Arg(vector_elements_1)
    ->Arg(vector_elements_2);

static void allocations_boost_pmr_pmr_reserve_vector_eb(
    benchmark::State& state) {
  const auto vector_elements{state.range(0)};
  for (auto _ : state) {
    const size_t resource_size = vector_elements * sizeof(int) + 1024;
    char buffer[resource_size];
    boost::container::pmr::monotonic_buffer_resource monotonic_resource{
        &buffer, resource_size};
    boost::container::pmr::unsynchronized_pool_resource resource{
        &monotonic_resource};
    boost::container::pmr::polymorphic_allocator<int> alloc{&resource};
    boost::container::pmr::vector<int> v;
    v.reserve(vector_elements);
    for (size_t i{0}; i < vector_elements; ++i) {
      v.emplace_back(i);
    }
    state.SetItemsProcessed(vector_elements);
    benchmark::DoNotOptimize(v);
  }
}
BENCHMARK(allocations_boost_pmr_pmr_reserve_vector_eb)
    ->Arg(vector_elements_1)
    ->Arg(vector_elements_2);

max_stars_repo_name: noqqaqq/benchmarks

max_issues_repo_name: noqqaqq/benchmarks

max_forks_repo_name: noqqaqq/benchmarks


text:
#include <vector>
#include <sstream>
#include <boost/range/adaptor/map.hpp>
#include <boost/timer/timer.hpp>

#include <yaml-cpp/yaml.h>

#include "common/god.h"
#include "common/vocab.h"
#include "common/factor_vocab.h"
#include "common/config.h"
#include "common/threadpool.h"
#include "common/file_stream.h"
#include "common/filter.h"
#include "common/processor/bpe.h"
#include "common/utils.h"
#include "common/search.h"
#include "common/sentences.h"
#include "common/translation_task.h"
#include "common/logging.h"

#include "scorer.h"
#include "loader_factory.h"

using namespace std;

namespace amunmt {

God::God()
 : threadIncr_(0)
{
}

God::~God()
{
  Cleanup();
}

God& God::Init(const std::string& options) {
  std::vector<std::string> args = boost::program_options::split_unix(options);
  int argc = args.size() + 1;
  char* argv[argc];
  argv[0] = const_cast<char*>("bogus");
  for (int i = 1; i < argc; ++i) {
    argv[i] = const_cast<char*>(args[i-1].c_str());
  }
  return Init(argc, argv);
}

God& God::Init(int argc, char** argv) {

  config_.AddOptions(argc, argv);
  info_ = spdlog::stderr_logger_mt("info");
  info_->set_pattern("[%c] (%L) %v");
  set_loglevel(*info_, config_.Get<string>("log-info"));

  progress_ = spdlog::stderr_logger_mt("progress");
  progress_->set_pattern("%v");
  set_loglevel(*progress_, config_.Get<string>("log-progress"));

  config_.LogOptions();

  if (Get("source-vocab").IsSequence()) {
    YAML::Node tabVocabs = Get("source-vocab");
    for (unsigned i = 0; i < tabVocabs.size(); i++) {
      if (tabVocabs[i].IsSequence()) {
        auto paths = tabVocabs[i].as<std::vector<std::string>>();
        sourceVocabs_.emplace_back(FactorVocab(paths));
      } else {
        std::string path = tabVocabs[i].as<std::string>();
        sourceVocabs_.emplace_back(FactorVocab(path));
      }
    }
  } else {
    sourceVocabs_.emplace_back(FactorVocab(Get<std::string>("source-vocab")));
  }
  targetVocab_.reset(new Vocab(Get<std::string>("target-vocab")));

  weights_ = Get<std::map<std::string, float>>("weights");

  if(Get<bool>("show-weights")) {
    LOG(info)->info("Outputting weights and exiting");
    for(auto && pair : weights_) {
      std::cout << pair.first << "= " << pair.second << std::endl;
    }
    exit(0);
  }

  LoadScorers();
  LoadFiltering();

  returnNBestList_ = Get<bool>("n-best");

  if (Get<bool>("use-fused-softmax")) {
    useFusedSoftmax_ = true;
    if (gpuLoaders_.size() != 1 || // more than 1 scorer
        God::Get<unsigned>("beam-size") > 11 // beam size affect shared mem alloc in gLogSoftMax()
        ) {
      useFusedSoftmax_ = false;
    }
  }
  else {
    useFusedSoftmax_ = false;
  }
  //cerr << "useFusedSoftmax_=" << useFusedSoftmax_ << endl;

  useTensorCores_ = Get<bool>("tensor-cores");
  cerr << "useTensorCores_=" << useTensorCores_ << endl;

  if (Has("input-file")) {
    LOG(info)->info("Reading from {}", Get<std::string>("input-file"));
    inputStream_.reset(new InputFileStream(Get<std::string>("input-file")));
  }
  else {
    LOG(info)->info("Reading from stdin");
    inputStream_.reset(new InputFileStream(std::cin));
  }

  LoadPrePostProcessing();

  unsigned totalThreads = GetTotalThreads();
  LOG(info)->info("Total number of threads: {}", totalThreads);
  amunmt_UTIL_THROW_IF2(totalThreads == 0, "Total number of threads is 0");

  pool_.reset(new ThreadPool(totalThreads, totalThreads));

  return *this;
}

void God::Cleanup()
{
  pool_.reset();
  cpuLoaders_.clear();
  gpuLoaders_.clear();
  fpgaLoaders_.clear();
}

void God::LoadScorers() {
  LOG(info)->info("Loading scorers...");
#ifdef CUDA
  unsigned gpuThreads = God::Get<unsigned>("gpu-threads");
  auto devices = God::Get<std::vector<unsigned>>("devices");
  if (gpuThreads > 0 && devices.size() > 0) {
    for (auto&& pair : config_.Get()["scorers"]) {
      std::string name = pair.first.as<std::string>();
      gpuLoaders_.emplace(name, LoaderFactory::Create(*this, name, pair.second, GPUDevice));
    }
  }
#endif
#ifdef HAS_CPU
  unsigned cpuThreads = God::Get<unsigned>("cpu-threads");
  if (cpuThreads) {
    for (auto&& pair : config_.Get()["scorers"]) {
      std::string name = pair.first.as<std::string>();
      cpuLoaders_.emplace(name, LoaderFactory::Create(*this, name, pair.second, CPUDevice));
    }
  }
#endif
#ifdef HAS_FPGA
  unsigned fpgaThreads = God::Get<unsigned>("fpga-threads");
  if (fpgaThreads) {
    for (auto&& pair : config_.Get()["scorers"]) {
      std::string name = pair.first.as<std::string>();
      fpgaLoaders_.emplace(name, LoaderFactory::Create(*this, name, pair.second, FPGADevice));
    }
  }
#endif

}

void God::LoadFiltering() {
  if (!Get<std::vector<std::string>>("softmax-filter").empty()) {
    auto filterOptions = Get<std::vector<std::string>>("softmax-filter");
    std::string alignmentFile = filterOptions[0];
    LOG(info)->info("Reading target softmax filter file from {}", alignmentFile);
    Filter* filter = nullptr;
    if (filterOptions.size() >= 3) {
      const unsigned numNFirst = stoi(filterOptions[1]);
      const unsigned maxNumTranslation = stoi(filterOptions[2]);
      filter = new Filter(GetSourceVocab(0, 0),
                          GetTargetVocab(),
                          alignmentFile,
                          numNFirst,
                          maxNumTranslation);
    } else if (filterOptions.size() == 2) {
      const unsigned numNFirst = stoi(filterOptions[1]);
      filter = new Filter(GetSourceVocab(0, 0),
                          GetTargetVocab(),
                          alignmentFile,
                          numNFirst);
    } else {
      filter = new Filter(GetSourceVocab(0, 0),
                          GetTargetVocab(),
                          alignmentFile);
    }
    filter_.reset(filter);
  }
}

void God::LoadPrePostProcessing() {
  if (Has("bpe")) {
    if(Get("bpe").IsSequence()) {
      unsigned i = 0;
      for(auto bpePath : Get<std::vector<std::string>>("bpe")) {
        LOG(info)->info("using bpe: {}", bpePath);
        preprocessors_.push_back(std::vector<PreprocessorPtr>());
        preprocessors_[i++].emplace_back(new BPE(bpePath));
      }
    }
    else {
      LOG(info)->info("using bpe: {}", Get<std::string>("bpe"));
        preprocessors_.push_back(std::vector<PreprocessorPtr>());
      if (Get<std::string>("bpe") != "") {
        preprocessors_[0].emplace_back(new BPE(Get<std::string>("bpe")));
      }
    }
  }

  if (Has("bpe") && !Get<bool>("no-debpe")) {
    LOG(info)->info("De-BPE output");
    postprocessors_.emplace_back(new BPE());
  }
}

Vocab& God::GetSourceVocab(unsigned tab, unsigned factor) const {
  return sourceVocabs_[tab].GetVocab(factor);
}

FactorVocab& God::GetSourceVocabs(unsigned tab) const {
  return sourceVocabs_[tab];
}

Vocab& God::GetTargetVocab() const {
  return *targetVocab_;
}

std::shared_ptr<const Filter> God::GetFilter() const {
  return filter_;
}

std::istream& God::GetInputStream() const {
  return *inputStream_;
}

OutputCollector& God::GetOutputCollector() const {
  return outputCollector_;
}

std::vector<ScorerPtr> God::GetScorers(const DeviceInfo &deviceInfo) const {
  std::vector<ScorerPtr> scorers;

  if (deviceInfo.deviceType == CPUDevice) {
    for (auto&& loader : cpuLoaders_ | boost::adaptors::map_values)
      scorers.emplace_back(loader->NewScorer(*this, deviceInfo));
  }
  else if (deviceInfo.deviceType == GPUDevice) {
    for (auto&& loader : gpuLoaders_ | boost::adaptors::map_values)
      scorers.emplace_back(loader->NewScorer(*this, deviceInfo));
  }
  else if (deviceInfo.deviceType == FPGADevice) {
    for (auto&& loader : fpgaLoaders_ | boost::adaptors::map_values)
      scorers.emplace_back(loader->NewScorer(*this, deviceInfo));
  }
  else {
	amunmt_UTIL_THROW2("Unknown device type:" << deviceInfo);
  }

  return scorers;
}

BestHypsBasePtr God::GetBestHyps(const DeviceInfo &deviceInfo) const {
  if (deviceInfo.deviceType == CPUDevice) {
    return cpuLoaders_.begin()->second->GetBestHyps(*this, deviceInfo);
  }
  else if (deviceInfo.deviceType == GPUDevice) {
    return gpuLoaders_.begin()->second->GetBestHyps(*this, deviceInfo);
  }
  else if (deviceInfo.deviceType == FPGADevice) {
    return fpgaLoaders_.begin()->second->GetBestHyps(*this, deviceInfo);
  }
  else {
	amunmt_UTIL_THROW2("Unknown device type:" << deviceInfo);
  }
}

std::vector<std::string> God::GetScorerNames() const {
  std::vector<std::string> scorerNames;
  for(auto&& name : cpuLoaders_ | boost::adaptors::map_keys)
    scorerNames.push_back(name);
  for(auto&& name : gpuLoaders_ | boost::adaptors::map_keys)
    scorerNames.push_back(name);
  for(auto&& name : fpgaLoaders_ | boost::adaptors::map_keys)
    scorerNames.push_back(name);

  return scorerNames;
}

const std::map<std::string, float>& God::GetScorerWeights() const {
  return weights_;
}

std::vector<std::vector<std::string>> God::Preprocess (
  unsigned i, const std::vector<std::vector<std::string>>& input
) const {
  std::vector<std::vector<std::string>> processed = input;
  if (preprocessors_.size() >= i + 1) {
    for (const auto& processor : preprocessors_[i]) {
      processed = processor->Preprocess(processed);
    }
  }
  return processed;
}

std::vector<std::string> God::Preprocess(unsigned i, const std::vector<std::string>& input) const {
  std::vector<std::string> processed = input;
  if (preprocessors_.size() >= i + 1) {
    for (const auto& processor : preprocessors_[i]) {
      processed = processor->Preprocess(processed);
    }
  }
  return processed;
}

std::vector<std::string> God::Postprocess(const std::vector<std::string>& input) const {
  std::vector<std::string> processed = input;
  for (const auto& processor : postprocessors_) {
    processed = processor->Postprocess(processed);
  }
  return processed;
}

DeviceInfo God::GetNextDevice() const
{
  DeviceInfo ret;

  unsigned cpuThreads = 0, gpuThreads = 0, fpgaThreads = 0;
  std::vector<unsigned> gpuDevices, fpgaDevices;

#ifdef CUDA
  gpuThreads = Get<unsigned>("gpu-threads");
  gpuDevices = Get<std::vector<unsigned>>("devices");
#endif

#ifdef HAS_CPU
  cpuThreads = God::Get<unsigned>("cpu-threads");
#endif

#ifdef HAS_FPGA
  fpgaThreads = Get<unsigned>("fpga-threads");
  fpgaDevices = Get<std::vector<unsigned>>("fpga-devices");
#endif

  unsigned totGPUThreads = gpuThreads * gpuDevices.size();
  unsigned totFPGAThreads = fpgaThreads * fpgaDevices.size();

  // start locking
  boost::unique_lock<boost::shared_mutex> lock(accessLock_);

  if (threadIncr_ < cpuThreads) {
    ret.deviceType = CPUDevice;
    ret.threadInd = threadIncr_;
  }
  else if (threadIncr_ < cpuThreads + totGPUThreads) {
    ret.deviceType = GPUDevice;
    unsigned threadIncr = threadIncr_ - cpuThreads;

    ret.threadInd = threadIncr / gpuDevices.size();

    unsigned deviceInd = threadIncr % gpuDevices.size();
    assert(deviceInd < gpuDevices.size());
    ret.deviceId = gpuDevices[deviceInd];
  }
  else if (threadIncr_ < cpuThreads + totGPUThreads + totFPGAThreads) {
    ret.deviceType = FPGADevice;
    unsigned threadIncr = threadIncr_ - cpuThreads - totGPUThreads;

    ret.threadInd = threadIncr / fpgaDevices.size();

    unsigned deviceInd = threadIncr % fpgaDevices.size();
    assert(deviceInd < fpgaDevices.size());
    ret.deviceId = fpgaDevices[deviceInd];
  }
  else {
    amunmt_UTIL_THROW2("Too many threads");
  }

  ++threadIncr_;

  return ret;
}

Search &God::GetSearch() const
{
  thread_local Search obj(*this);
  return obj;
}

unsigned God::GetTotalThreads() const
{
  unsigned totalThreads = 0;

#ifdef CUDA
  unsigned gpuThreads = Get<unsigned>("gpu-threads");
  auto devices = Get<std::vector<unsigned>>("devices");
  LOG(info)->info("Setting GPU thread count to {}", gpuThreads);
  totalThreads += gpuThreads * devices.size();
#endif

#ifdef HAS_CPU
  unsigned cpuThreads = Get<unsigned>("cpu-threads");
  LOG(info->info("Setting CPU thread count to {}", cpuThreads));
  totalThreads += cpuThreads;
#endif

#ifdef HAS_FPGA
  unsigned fpgaThreads = Get<unsigned>("fpga-threads");
  auto fpgaDevices = Get<std::vector<unsigned>>("fpga-devices");
  LOG(info->info("Setting FPGA thread count to {}", fpgaThreads));
  totalThreads += fpgaThreads * fpgaDevices.size();
#endif

  return totalThreads;
}

}

max_stars_repo_name: Targoman/TargomanNMT

max_issues_repo_name: Targoman/TargomanNMT

max_forks_repo_name: Targoman/TargomanNMT


text:
#include <scitbx/array_family/boost_python/flex_fwd.h>

#include <boost/python/class.hpp>
#include <boost/python/args.hpp>
#include <boost/python/return_value_policy.hpp>
#include <boost/python/return_by_value.hpp>
#include <boost/python/copy_const_reference.hpp>

#include <scitbx/math/chebyshev.h>

namespace scitbx { namespace math {

namespace {

  struct chebyshev_base_wrappers
  {
    typedef chebyshev::chebyshev_base<> w_t;

    static void
    wrap()
    {
      using namespace boost::python;

      class_<w_t>("chebyshev_base", no_init)
        .def(init<std::size_t const&,
                  double const&,
                  double const&>((arg("n_terms"),
                                  arg("low_limit"),
                                  arg("high_limit") )))
        .def(init<std::size_t const& ,
                  double const&,
                  double const&,
                  scitbx::af::const_ref<double> const&>
             ((arg("n_terms"),
               arg("low_limit"),
               arg("high_limit"),
               arg("cheb_coefs") )))
        .def("f", (double(w_t::*)(double const&)) &w_t::f)
        .def("f", (scitbx::af::shared<double>(w_t::*)
                   (scitbx::af::const_ref<double> const&))
                    &w_t::f )
        .def("coefs", &w_t::coefs )
        .def("replace", &w_t::replace )
        ;

    }

  };

  struct chebyshev_polynome_wrappers
  {
    typedef chebyshev::chebyshev_polynome<> w_t;

    static void
    wrap()
    {
      using namespace boost::python;

      class_<w_t>("chebyshev_polynome", no_init)
        .def(init<std::size_t const& ,
                  double const&,
                  double const&,
                  scitbx::af::const_ref<double> const&>
             ((arg("n_terms"),
               arg("low_limit"),
               arg("high_limit"),
               arg("cheb_coefs") )))
        .def("f", (double(w_t::*)(double const&)) &w_t::f)
        .def("f", (scitbx::af::shared<double>(w_t::*)
                   (scitbx::af::const_ref<double> const&))
                    &w_t::f )
        .def("coefs", &w_t::coefs )

        .def("dfdx", (double(w_t::*)(double const&)) &w_t::dfdx)
        .def("dfdx", (scitbx::af::shared<double>(w_t::*)
                      (scitbx::af::const_ref<double> const&))
             &w_t::dfdx )
        .def("dfdx_coefs", &w_t::dfdx_coefs)
        ;

    }

  };

  struct chebyshev_fitter_wrappers
  {
    typedef chebyshev::chebyshev_fitter<> w_t;

    static void
    wrap()
    {
      using namespace boost::python;

      class_<w_t>("chebyshev_fitter", no_init)
        .def(init<std::size_t const& ,
                  double const&,
                  double const& >
             ((arg("n_terms"),
               arg("low_limit"),
               arg("high_limit") )))
        .def(init<std::size_t const& ,
                  double const&,
                  double const&,
                  scitbx::af::const_ref<double> const&>
             ((arg("n_terms"),
               arg("low_limit"),
               arg("high_limit"),
               arg("cheb_coefs") )))

        .def("f", (double(w_t::*)(double const&)) &w_t::f)
        .def("f", (scitbx::af::shared<double>(w_t::*)
                   (scitbx::af::const_ref<double> const&))
                    &w_t::f )
        .def("coefs", &w_t::coefs )
        .def("replace", &w_t::replace)
        .def("dfdcoefs", &w_t::dfdcoefs)
        ;

    }

  };

  struct chebyshev_smooth_wrappers
  {
    typedef chebyshev::chebyshev_smooth<> w_t;

    static void
    wrap()
    {
      using namespace boost::python;

      class_<w_t>("chebyshev_smooth", no_init)
        .def(init<std::size_t const&,
                  double const&,
                  double const&>((arg("n_terms"),
                                  arg("low_limit"),
                                  arg("high_limit") )))
        .def(init<std::size_t const& ,
                  double const&,
                  double const&,
                  scitbx::af::const_ref<double> const&>
             ((arg("n_terms"),
               arg("low_limit"),
               arg("high_limit"),
               arg("cheb_coefs") )))
        .def("f", (double(w_t::*)(double const&)) &w_t::f)
        .def("f", (scitbx::af::shared<double>(w_t::*)
                   (scitbx::af::const_ref<double> const&))
                    &w_t::f )
        .def("coefs", &w_t::smooth_coefs )
        .def("replace", &w_t::replace_and_smooth )
        ;

    }

  };

  struct chebyshev_smooth_fitter_wrappers
  {
    typedef chebyshev::chebyshev_smooth_fitter<> w_t;

    static void
    wrap()
    {
      using namespace boost::python;

      class_<w_t>("chebyshev_smooth_fitter", no_init)
        .def(init<std::size_t const&,
                  double const&,
                  double const&>((arg("n_terms"),
                                  arg("low_limit"),
                                  arg("high_limit") )))
        .def(init<std::size_t const& ,
                  double const&,
                  double const&,
                  scitbx::af::const_ref<double> const&>
             ((arg("n_terms"),
               arg("low_limit"),
               arg("high_limit"),
               arg("cheb_coefs") )))
        .def("f", (double(w_t::*)(double const&)) &w_t::f)
        .def("f", (scitbx::af::shared<double>(w_t::*)
                   (scitbx::af::const_ref<double> const&))
                    &w_t::f )
        .def("coefs", &w_t::smooth_coefs )
        .def("replace", &w_t::replace_and_smooth )
        .def("dfdcoefs", &w_t::dfdcoefs)
        ;

    }

  };

  struct chebyshev_lsq_wrappers
  {
    typedef chebyshev::chebyshev_lsq<> w_t;

    static void
    wrap()
    {
      using namespace boost::python;

      class_<w_t>("chebyshev_lsq", no_init)
        .def(init<std::size_t const&,
                  double const&,
                  double const&,
                  scitbx::af::const_ref<double> const&,
                  scitbx::af::const_ref<double> const&,
                  scitbx::af::const_ref<double> const&,
                  scitbx::af::const_ref<bool> const&
                  >((arg("n_terms"),
                     arg("low_limit"),
                     arg("high_limit"),
                     arg("x_obs"),
                     arg("y_obs"),
                     arg("w_obs"),
                     arg("free_flags") )))

        .def("residual", &w_t::residual)
        .def("free_residual", &w_t::free_residual)
        .def("gradient", &w_t::gradient )
        .def("replace", &w_t::replace )
        .def("coefs", &w_t::coefs)
        ;

    }

  };

} // namespace <anonymous>

namespace boost_python {

  void wrap_chebyshev_base()
  {
    chebyshev_base_wrappers::wrap();
  }
  void wrap_chebyshev_polynome()
  {
    chebyshev_polynome_wrappers::wrap();
  }
  void wrap_chebyshev_fitter()
  {
    chebyshev_fitter_wrappers::wrap();
  }

  void wrap_chebyshev_smooth()
  {
    chebyshev_smooth_wrappers::wrap();
  }
  void wrap_chebyshev_smooth_fitter()
  {
    chebyshev_smooth_fitter_wrappers::wrap();
  }

  void wrap_chebyshev_lsq()
  {
    chebyshev_lsq_wrappers::wrap();
  }

}}} // namespace scitbx::math::boost_python