text:
/*
 * This file is part of the DUDS project. It is subject to the BSD-style
 * license terms in the LICENSE file found in the top-level directory of this
 * distribution and at https://github.com/jjackowski/duds/blob/master/LICENSE.
 * No part of DUDS, including this file, may be copied, modified, propagated,
 * or distributed except according to the terms contained in the LICENSE file.
 *
 * Copyright (C) 2019  Jeff Jackowski
 */
#ifndef GENERICMENUITEM_HPP
#define GENERICMENUITEM_HPP

#include <duds/ui/menu/MenuItem.hpp>
#include <boost/signals2/signal.hpp>

namespace duds { namespace ui { namespace menu {

/**
 * A generic take on MenuItem that invokes a Boost signal when the item is
 * chosen, selected, or deselected.
 * @author  Jeff Jackowski
 */
class GenericMenuItem : public MenuItem {
public:
	/**
	 * The signal type invoked when the item is chosen, selected, or deselected.
	 * @param invokingView  The view used to chose or select the menu item.
	 * @param access        An access object for the menu that may be used
	 *                      to modify the menu.
	 * @param self          The GenericMenuItem that invoked the signal.
	 */
	typedef boost::signals2::signal<
		void(
			MenuView &invokingView,
			const MenuAccess &access,
			GenericMenuItem &self
		)
	> Signal;
private:
	/**
	 * The signal that is invoked when the user choses this menu item.
	 */
	Signal choseSig;
	/**
	 * The signal that is invoked when the user selects this menu item.
	 */
	Signal selSig;
	/**
	 * The signal that is invoked when the user deselects this menu item.
	 */
	Signal deselSig;
public:
	/**
	 * Constructs a new GenericMenuItem.
	 * @note  All MenuItem objects must be managed by std::shared_ptr.
	 * @param label        Short text presented to the user as the menu item.
	 * @param flags        The option flags for the item. The default is zero:
	 *                     enabled, visible, no value, and not a toggle.
	 * @sa make(const std::string &, Flags)
	 */
	GenericMenuItem(
		const std::string &label,
		Flags flags = Flags::Zero()
	) : MenuItem(label, flags) { }
	/**
	 * Constructs a new GenericMenuItem.
	 * @note  All MenuItem objects must be managed by std::shared_ptr.
	 * @param label        Short text presented to the user as the menu item.
	 * @param description  Longer text optionally presented to the user to
	 *                     provide a better idea of what the option does.
	 * @param flags        The option flags for the item. The default is zero:
	 *                     enabled, visible, no value, and not a toggle.
	 * @sa make(const std::string &, const std::string &, Flags)
	 */
	GenericMenuItem(
		const std::string &label,
		const std::string &description,
		Flags flags = Flags::Zero()
	) : MenuItem(label, description, flags) { }
	/**
	 * Constructs a new GenericMenuItem with an associated value.
	 * @note  All MenuItem objects must be managed by std::shared_ptr.
	 * @param label        Short text presented to the user as the menu item.
	 * @param description  Longer text optionally presented to the user to
	 *                     provide a better idea of what the option does.
	 * @param value        The current value associated with the menu item.
	 * @param flags        The option flags for the item. While the default is
	 *                     zero, the MenuItem::HasValue flag will be OR'd with
	 *                     the given value by the constructor.
	 * @sa make(const std::string &, const std::string &, const std::string &, Flags)
	 */
	GenericMenuItem(
		const std::string &label,
		const std::string &description,
		const std::string &value,
		Flags flags = Flags::Zero()
	) : MenuItem(label, description, value, flags) { }
	/**
	 * Convenience function to make a shared pointer holding a new
	 * GenericMenuItem.
	 * @param label        Short text presented to the user as the menu item.
	 * @param flags        The option flags for the item. The default is zero:
	 *                     enabled, visible, no value, and not a toggle.
	 */
	static std::shared_ptr<GenericMenuItem> make(
		const std::string &label,
		Flags flags = Flags::Zero()
	) {
		return std::make_shared<GenericMenuItem>(label, flags);
	}
	/**
	 * Convenience function to make a shared pointer holding a new
	 * GenericMenuItem.
	 * @param label        Short text presented to the user as the menu item.
	 * @param description  Longer text optionally presented to the user to
	 *                     provide a better idea of what the option does.
	 * @param flags        The option flags for the item. The default is zero:
	 *                     enabled, visible, no value, and not a toggle.
	 */
	static std::shared_ptr<GenericMenuItem> make(
		const std::string &label,
		const std::string &description,
		Flags flags = Flags::Zero()
	) {
		return std::make_shared<GenericMenuItem>(label, description, flags);
	}
	/**
	 * Convenience function to make a shared pointer holding a new
	 * GenericMenuItem with an associated value.
	 * @param label        Short text presented to the user as the menu item.
	 * @param description  Longer text optionally presented to the user to
	 *                     provide a better idea of what the option does.
	 * @param value        The current value associated with the menu item.
	 * @param flags        The option flags for the item. While the default is
	 *                     zero, the MenuItem::HasValue flag will be OR'd with
	 *                     the given value by the constructor.
	 */
	static std::shared_ptr<GenericMenuItem> make(
		const std::string &label,
		const std::string &description,
		const std::string &value,
		Flags flags = Flags::Zero()
	) {
		return std::make_shared<GenericMenuItem>(label, description, value, flags);
	}
	/**
	 * Invokes the @a choseSig signal in response to the user chosing this
	 * menu item. Use one of the choseConnect() or choseConnectExtended()
	 * functions to make connections to the signal.
	 */
	virtual void chose(MenuView &invokingView, const MenuAccess &access);
	/**
	 * Invokes the @a selSig signal in response to the user selecting this
	 * menu item.
	 */
	virtual void select(MenuView &invokingView, const MenuAccess &access);
	/**
	 * Invokes the @a deselSig signal in response to the user deselecting this
	 * menu item.
	 */
	virtual void deselect(MenuView &invokingView, const MenuAccess &access);
	/**
	 * Make a connection to the generic menu item chose signal.
	 * See the [Boost reference documentation](https://www.boost.org/doc/libs/1_67_0/doc/html/boost/signals2/signal.html#idp182137616-bb)
	 * for more details, or the [tutorial](https://www.boost.org/doc/libs/1_67_0/doc/html/signals2/tutorial.html)
	 * for an overview of the whole boost::singals2 system.
	 */
	boost::signals2::connection choseConnect(
		const typename Signal::slot_type &slot,
		boost::signals2::connect_position at = boost::signals2::at_back
	) {
		return choseSig.connect(slot, at);
	}
	/**
	 * Make a connection to the generic menu item chose signal.
	 * See the [Boost reference documentation](https://www.boost.org/doc/libs/1_67_0/doc/html/boost/signals2/signal.html#idp182137616-bb)
	 * for more details, or the [tutorial](https://www.boost.org/doc/libs/1_67_0/doc/html/signals2/tutorial.html)
	 * for an overview of the whole boost::singals2 system.
	 */
	boost::signals2::connection choseConnect(
		const typename Signal::group_type &group,
		const typename Signal::slot_type &slot,
		boost::signals2::connect_position at = boost::signals2::at_back
	) {
		return choseSig.connect(group, slot, at);
	}
	/**
	 * Make a connection to the generic menu item chose signal.
	 * See the [Boost reference documentation](https://www.boost.org/doc/libs/1_67_0/doc/html/boost/signals2/signal.html#idp182137616-bb)
	 * for more details, or the [tutorial](https://www.boost.org/doc/libs/1_67_0/doc/html/signals2/tutorial.html)
	 * for an overview of the whole boost::singals2 system.
	 */
	boost::signals2::connection choseConnectExtended(
		const typename Signal::extended_slot_type &slot,
		boost::signals2::connect_position at = boost::signals2::at_back
	) {
		return choseSig.connect_extended(slot, at);
	}
	/**
	 * Make a connection to the generic menu item chose signal.
	 * See the [Boost reference documentation](https://www.boost.org/doc/libs/1_67_0/doc/html/boost/signals2/signal.html#idp182137616-bb)
	 * for more details, or the [tutorial](https://www.boost.org/doc/libs/1_67_0/doc/html/signals2/tutorial.html)
	 * for an overview of the whole boost::singals2 system.
	 */
	boost::signals2::connection choseConnectExtended(
		const typename Signal::group_type &group,
		const typename Signal::extended_slot_type &slot,
		boost::signals2::connect_position at = boost::signals2::at_back
	) {
		return choseSig.connect_extended(group, slot, at);
	}
	/**
	 * Disconnect a group from the generic menu item chose signal.
	 * See the [Boost reference documentation](https://www.boost.org/doc/libs/1_67_0/doc/html/boost/signals2/signal.html#idp182137616-bb)
	 * for more details, or the [tutorial](https://www.boost.org/doc/libs/1_67_0/doc/html/signals2/tutorial.html)
	 * for an overview of the whole boost::singals2 system.
	 */
	void choseDisconnect(
		const typename Signal::group_type &group
	) {
		choseSig.disconnect(group);
	}
	/**
	 * Disconnect from the generic menu item chose signal.
	 * See the [Boost reference documentation](https://www.boost.org/doc/libs/1_67_0/doc/html/boost/signals2/signal.html#idp182137616-bb)
	 * for more details, or the [tutorial](https://www.boost.org/doc/libs/1_67_0/doc/html/signals2/tutorial.html)
	 * for an overview of the whole boost::singals2 system.
	 */
	template<typename Slot>
	void choseDisconnect(const Slot &slotFunc) {
		choseSig.disconnect(slotFunc);
	}
	/**
	 * Make a connection to the generic menu item select signal.
	 * See the [Boost reference documentation](https://www.boost.org/doc/libs/1_67_0/doc/html/boost/signals2/signal.html#idp182137616-bb)
	 * for more details, or the [tutorial](https://www.boost.org/doc/libs/1_67_0/doc/html/signals2/tutorial.html)
	 * for an overview of the whole boost::singals2 system.
	 */
	boost::signals2::connection selectConnect(
		const typename Signal::slot_type &slot,
		boost::signals2::connect_position at = boost::signals2::at_back
	) {
		return selSig.connect(slot, at);
	}
	/**
	 * Make a connection to the generic menu item select signal.
	 * See the [Boost reference documentation](https://www.boost.org/doc/libs/1_67_0/doc/html/boost/signals2/signal.html#idp182137616-bb)
	 * for more details, or the [tutorial](https://www.boost.org/doc/libs/1_67_0/doc/html/signals2/tutorial.html)
	 * for an overview of the whole boost::singals2 system.
	 */
	boost::signals2::connection selectConnect(
		const typename Signal::group_type &group,
		const typename Signal::slot_type &slot,
		boost::signals2::connect_position at = boost::signals2::at_back
	) {
		return selSig.connect(group, slot, at);
	}
	/**
	 * Make a connection to the generic menu item select signal.
	 * See the [Boost reference documentation](https://www.boost.org/doc/libs/1_67_0/doc/html/boost/signals2/signal.html#idp182137616-bb)
	 * for more details, or the [tutorial](https://www.boost.org/doc/libs/1_67_0/doc/html/signals2/tutorial.html)
	 * for an overview of the whole boost::singals2 system.
	 */
	boost::signals2::connection selectConnectExtended(
		const typename Signal::extended_slot_type &slot,
		boost::signals2::connect_position at = boost::signals2::at_back
	) {
		return selSig.connect_extended(slot, at);
	}
	/**
	 * Make a connection to the generic menu item select signal.
	 * See the [Boost reference documentation](https://www.boost.org/doc/libs/1_67_0/doc/html/boost/signals2/signal.html#idp182137616-bb)
	 * for more details, or the [tutorial](https://www.boost.org/doc/libs/1_67_0/doc/html/signals2/tutorial.html)
	 * for an overview of the whole boost::singals2 system.
	 */
	boost::signals2::connection selectConnectExtended(
		const typename Signal::group_type &group,
		const typename Signal::extended_slot_type &slot,
		boost::signals2::connect_position at = boost::signals2::at_back
	) {
		return selSig.connect_extended(group, slot, at);
	}
	/**
	 * Disconnect a group from the generic menu item select signal.
	 * See the [Boost reference documentation](https://www.boost.org/doc/libs/1_67_0/doc/html/boost/signals2/signal.html#idp182137616-bb)
	 * for more details, or the [tutorial](https://www.boost.org/doc/libs/1_67_0/doc/html/signals2/tutorial.html)
	 * for an overview of the whole boost::singals2 system.
	 */
	void selectDisconnect(
		const typename Signal::group_type &group
	) {
		selSig.disconnect(group);
	}
	/**
	 * Disconnect from the generic menu item select signal.
	 * See the [Boost reference documentation](https://www.boost.org/doc/libs/1_67_0/doc/html/boost/signals2/signal.html#idp182137616-bb)
	 * for more details, or the [tutorial](https://www.boost.org/doc/libs/1_67_0/doc/html/signals2/tutorial.html)
	 * for an overview of the whole boost::singals2 system.
	 */
	template<typename Slot>
	void selectDisconnect(const Slot &slotFunc) {
		selSig.disconnect(slotFunc);
	}
	/**
	 * Make a connection to the generic menu item deselect signal.
	 * See the [Boost reference documentation](https://www.boost.org/doc/libs/1_67_0/doc/html/boost/signals2/signal.html#idp182137616-bb)
	 * for more details, or the [tutorial](https://www.boost.org/doc/libs/1_67_0/doc/html/signals2/tutorial.html)
	 * for an overview of the whole boost::singals2 system.
	 */
	boost::signals2::connection deselectConnect(
		const typename Signal::slot_type &slot,
		boost::signals2::connect_position at = boost::signals2::at_back
	) {
		return deselSig.connect(slot, at);
	}
	/**
	 * Make a connection to the generic menu item deselect signal.
	 * See the [Boost reference documentation](https://www.boost.org/doc/libs/1_67_0/doc/html/boost/signals2/signal.html#idp182137616-bb)
	 * for more details, or the [tutorial](https://www.boost.org/doc/libs/1_67_0/doc/html/signals2/tutorial.html)
	 * for an overview of the whole boost::singals2 system.
	 */
	boost::signals2::connection deselectConnect(
		const typename Signal::group_type &group,
		const typename Signal::slot_type &slot,
		boost::signals2::connect_position at = boost::signals2::at_back
	) {
		return deselSig.connect(group, slot, at);
	}
	/**
	 * Make a connection to the generic menu item deselect signal.
	 * See the [Boost reference documentation](https://www.boost.org/doc/libs/1_67_0/doc/html/boost/signals2/signal.html#idp182137616-bb)
	 * for more details, or the [tutorial](https://www.boost.org/doc/libs/1_67_0/doc/html/signals2/tutorial.html)
	 * for an overview of the whole boost::singals2 system.
	 */
	boost::signals2::connection deselectConnectExtended(
		const typename Signal::extended_slot_type &slot,
		boost::signals2::connect_position at = boost::signals2::at_back
	) {
		return deselSig.connect_extended(slot, at);
	}
	/**
	 * Make a connection to the generic menu item deselect signal.
	 * See the [Boost reference documentation](https://www.boost.org/doc/libs/1_67_0/doc/html/boost/signals2/signal.html#idp182137616-bb)
	 * for more details, or the [tutorial](https://www.boost.org/doc/libs/1_67_0/doc/html/signals2/tutorial.html)
	 * for an overview of the whole boost::singals2 system.
	 */
	boost::signals2::connection deselectConnectExtended(
		const typename Signal::group_type &group,
		const typename Signal::extended_slot_type &slot,
		boost::signals2::connect_position at = boost::signals2::at_back
	) {
		return deselSig.connect_extended(group, slot, at);
	}
	/**
	 * Disconnect a group from the generic menu item deselect signal.
	 * See the [Boost reference documentation](https://www.boost.org/doc/libs/1_67_0/doc/html/boost/signals2/signal.html#idp182137616-bb)
	 * for more details, or the [tutorial](https://www.boost.org/doc/libs/1_67_0/doc/html/signals2/tutorial.html)
	 * for an overview of the whole boost::singals2 system.
	 */
	void deselectDisconnect(
		const typename Signal::group_type &group
	) {
		deselSig.disconnect(group);
	}
	/**
	 * Disconnect from the generic menu item deselect signal.
	 * See the [Boost reference documentation](https://www.boost.org/doc/libs/1_67_0/doc/html/boost/signals2/signal.html#idp182137616-bb)
	 * for more details, or the [tutorial](https://www.boost.org/doc/libs/1_67_0/doc/html/signals2/tutorial.html)
	 * for an overview of the whole boost::singals2 system.
	 */
	template<typename Slot>
	void deselectDisconnect(const Slot &slotFunc) {
		deselSig.disconnect(slotFunc);
	}
};

/**
 * A shared pointer to a GenericMenuItem.
 */
typedef std::shared_ptr<GenericMenuItem>  GenericMenuItemSptr;

/**
 * A weak pointer to a GenericMenuItem.
 */
typedef std::weak_ptr<GenericMenuItem>  GenericMenuItemWptr;

} } }

#endif        //  #ifndef GENERICMENUITEM_HPP

ext: hpp

max_stars_repo_name: jjackowski/duds

max_issues_repo_name: jjackowski/duds

max_forks_repo_name: jjackowski/duds


text:
//==============================================================================
//         Copyright 2003 - 2014   LASMEA UMR 6602 CNRS/Univ. Clermont II
//         Copyright 2009 - 2014   LRI    UMR 8623 CNRS/Univ Paris Sud XI
//
//          Distributed under the Boost Software License, Version 1.0.
//                 See accompanying file LICENSE.txt or copy at
//                     http://www.boost.org/LICENSE_1_0.txt
//==============================================================================
#ifndef NT2_DSL_FUNCTIONS_REDUCTION_RUN_HPP_INCLUDED
#define NT2_DSL_FUNCTIONS_REDUCTION_RUN_HPP_INCLUDED

#include <nt2/dsl/functions/run.hpp>
#include <nt2/include/functions/of_size.hpp>
#include <nt2/include/functions/inner_fold.hpp>
#include <nt2/include/functions/outer_fold.hpp>
#include <nt2/include/functions/reshape.hpp>
#include <nt2/include/functions/ndims.hpp>
#include <nt2/include/functions/firstnonsingleton.hpp>
#include <nt2/sdk/memory/category.hpp>
#include <boost/proto/traits.hpp>
#include <numeric>

namespace nt2 { namespace ext
{
  //============================================================================
  // Reductions operations go to inner/outer fold
  // Note that MATLAB reduction functions have a f(x) and a f(x,i) form
  // that we handle with reduction_dim
  //============================================================================
  BOOST_DISPATCH_IMPLEMENT  ( run_assign_, tag::cpu_
                            , (A0)(T0)(N0)(A1)(T1)(O1)(Neutral1)(N1)
                            , ((node_ < A0
                                      , elementwise_<T0>, N0
                                      , nt2::container::domain
                                      >
                              ))
                              ((node_ < A1
                                      , reduction_<T1,O1,Neutral1>, N1
                                      , nt2::container::domain
                                      >
                              ))
                            )
  {
    typedef A0&                                                                result_type;
    typedef typename boost::proto::result_of::child_c<A1&, 0>::type            input_type;
    typedef typename boost::remove_reference<input_type>::type::extent_type    extent_type;

    BOOST_FORCEINLINE result_type
    operator()(A0& a0, A1& a1) const
    {
      a0.resize(a1.extent());

      input_type input = boost::proto::child_c<0>(a1);
      extent_type ext = input.extent();
      std::size_t dim = nt2::ndims(ext);
      std::size_t lo_dim = nt2::firstnonsingleton(ext);
      std::size_t red = reduction_dim(a1, boost::mpl::bool_<!(boost::proto::arity_of<A1>::value <= 1)>());

      std::size_t inner = red-1 < ext.size() ? ext[red-1] : 1;

      std::size_t lo = std::accumulate( ext.begin()
                                      , ext.begin()+std::min(red-1, dim)
                                      , std::size_t(1)
                                      , std::multiplies<std::size_t>()
                                      );

      std::size_t hi = std::accumulate( ext.begin()+std::min(red, dim)
                                      , ext.begin()+dim
                                      , std::size_t(1)
                                      , std::multiplies<std::size_t>()
                                      );

      if(red <= lo_dim)
      {
        nt2::inner_fold( container::as_view(a0)
                       , reshape(input, of_size(inner, hi))
                       , typename nt2::make_functor<Neutral1, A0>::type()
                       , typename nt2::make_functor<O1, A0>::type()
                       , typename nt2::make_functor<T1, A0>::type()
                       );
      }
      else
      {
        nt2::outer_fold( reshape(a0, of_size(lo, hi))
                       , reshape(input, of_size(lo, inner, hi))
                       , typename nt2::make_functor<Neutral1, A0>::type()
                       , typename nt2::make_functor<O1, A0>::type()
                       , typename nt2::make_functor<T1, A0>::type()
                       );
      }

      return a0;
    }

    inline std::size_t reduction_dim(A1& a1, boost::mpl::false_) const
    {
      return nt2::firstnonsingleton(boost::proto::child_c<0>(a1).extent());
    }

    inline std::size_t reduction_dim(A1& a1, boost::mpl::true_) const
    {
      return nt2::run(boost::proto::child_c<1>(a1));
    }
  };
} }

#endif


text:
// Copyright (c) 2015-2016, FlightAware LLC.
// Copyright (c) 2015, Oliver Jowett <oliver@mutability.co.uk>
// All rights reserved.

// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:

// * Redistributions of source code must retain the above copyright notice, this
//   list of conditions and the following disclaimer.

// * Redistributions in binary form must reproduce the above copyright notice,
//   this list of conditions and the following disclaimer in the documentation
//   and/or other materials provided with the distribution.

// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <algorithm>
#include <iomanip>
#include <iostream>

#include <boost/asio.hpp>
#include <boost/asio/ip/v6_only.hpp>
#include <boost/asio/steady_timer.hpp>

#include "beast_input_net.h"
#include "modes_message.h"

using namespace beast;
using boost::asio::ip::tcp;

NetInput::NetInput(boost::asio::io_service &service_, const std::string &host_, const std::string &port_or_service_, const Settings &fixed_settings_, const modes::Filter &filter_) : BeastInput(service_, fixed_settings_, filter_), host(host_), port_or_service(port_or_service_), resolver(service_), socket(service_), reconnect_timer(service_), readbuf(std::make_shared<helpers::bytebuf>(read_buffer_size)), warned_about_framing(false) {}

std::string NetInput::what() const { return std::string("net(") + host + std::string(":") + port_or_service + std::string(")"); }

void NetInput::try_to_connect(void) {
    auto self(shared_from_this());

    tcp::resolver::query query(host, port_or_service);
    resolver.async_resolve(query, [this, self](const boost::system::error_code &ec, tcp::resolver::iterator it) {
        if (!ec) {
            next_endpoint = it;
            try_next_endpoint();
        } else if (ec == boost::asio::error::operation_aborted) {
            return;
        } else {
            std::cerr << what() << ": could not resolve address: " << ec.message() << std::endl;
            connection_failed();
            return;
        }
    });
}

void NetInput::try_next_endpoint() {
    if (next_endpoint == tcp::resolver::iterator()) {
        // No more addresses to try
        connection_failed();
        return;
    }

    tcp::endpoint endpoint = *next_endpoint++;

    auto self(shared_from_this());
    socket.async_connect(endpoint, [this, self, endpoint](const boost::system::error_code &ec) {
        if (!ec) {
            connection_established(endpoint);
        } else if (ec == boost::asio::error::operation_aborted) {
            return;
        } else {
            std::cerr << what() << ": connection to " << endpoint << " failed: " << ec.message() << std::endl;
            socket.close();
            try_next_endpoint();
        }
    });
}

void NetInput::connection_established(const tcp::endpoint &endpoint) {
    std::cerr << what() << ": connected to " << endpoint << std::endl;

    BeastInput::connection_established();
    warned_about_framing = false;
    start_reading();
}

void NetInput::disconnect() {
    if (socket.is_open()) {
        boost::system::error_code ignored;
        socket.close(ignored);
    }
}

bool NetInput::low_level_write(std::shared_ptr<helpers::bytebuf> message) {
    if (!socket.is_open())
        return false;

    auto self(shared_from_this());
    boost::asio::async_write(socket, boost::asio::buffer(*message), [this, self, message](boost::system::error_code ec, std::size_t len) {
        if (ec)
            handle_error(ec);
    });
    return true;
}

void NetInput::handle_error(const boost::system::error_code &ec) {
    if (ec == boost::asio::error::operation_aborted)
        return;

    std::cerr << what() << ": i/o error: " << ec.message() << std::endl;
    connection_failed();

    if (socket.is_open()) {
        boost::system::error_code ignored;
        socket.close(ignored);
    }
}

void NetInput::check_framing_errors(void) {
    if (!have_good_sync() && bad_bytes() > 20) {
        if (!warned_about_framing) {
            std::cerr << what() << ": framing errors seen, is the peer sending Beast binary data?" << std::endl;
            warned_about_framing = true;
        }
    }
}

void NetInput::start_reading(const boost::system::error_code &ec) {
    if (ec) {
        assert(ec == boost::asio::error::operation_aborted);
        return;
    }

    auto self(std::static_pointer_cast<NetInput>(shared_from_this()));
    std::shared_ptr<helpers::bytebuf> buf;

    buf.swap(readbuf);
    if (buf) {
        buf->resize(read_buffer_size);
    } else {
        buf = std::make_shared<helpers::bytebuf>(read_buffer_size);
    }

    socket.async_read_some(boost::asio::buffer(*buf), [this, self, buf](const boost::system::error_code &ec, std::size_t len) {
        if (ec) {
            readbuf = buf;
            handle_error(ec);
        } else {
            buf->resize(len);
            parse_input(*buf);
            check_framing_errors();
            readbuf = buf;

            start_reading();
        }
    });
}

ext: cc


text:
//
// An enum-matched-to-names class used to indicate the level of compression
// requested. The class contains enum entries for the different possible
// levels of compression.
//
//
// Original author Rob Kutschke
// Modified for Compression: Andy Edmonds, Dec 2020
//

#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdexcept>

#include "Offline/Compression/inc/CompressionLevel.hh"

#include <boost/static_assert.hpp>

using namespace std;

namespace mu2e {

  const char* CompressionLevel::_name[] = { COMPRESSIONLEVEL_NAMES };

  BOOST_STATIC_ASSERT(sizeof(CompressionLevel::_name)/sizeof(char*) == CompressionLevel::lastEnum);

  void CompressionLevel::printAll( std::ostream& ost){
    ost << "List of allowed compression levels: " << endl;
    for ( int i=0; i<lastEnum; ++i){
      ost << setw(3) << i << " " << _name[i] << std::endl;
    }
  }

  CompressionLevel CompressionLevel::findByName ( std::string const& name){

    // Size must be at least 2 (for unknown and lastEnum).
    for ( size_t i=0; i<size(); ++i ){
      if ( _name[i] == name ){
        return CompressionLevel(enum_type(i));
      }
    }
    return CompressionLevel(unknown);
  }
}

max_stars_repo_name: resnegfk/Offline

max_issues_repo_name: resnegfk/Offline

max_forks_repo_name: resnegfk/Offline


text:
/*
Copyright (c) 2015, Sigurd Storve
All rights reserved.

Licensed under the BSD license.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the <organization> nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once
#include <stdexcept>
#include <vector>
#ifdef _MSC_VER
#   pragma warning(push)
#   pragma warning(disable:4251) // needs to have dll-interface...
#endif
#include <H5Cpp.h>
#ifdef _MSC_VER
#   pragma warning(pop)
#endif
#include <boost/multi_array.hpp>

namespace SimpleHDF {

namespace detail {
    // Template trick for typemapping from C++ data types to HDF5 data type
    // through templates:
    // http://stackoverflow.com/questions/9250237/write-a-boostmulti-array-to-hdf5-dataset
    template <typename T>
    inline H5::DataType EquivH5Type() {
        throw std::runtime_error("Illegal type.");
    }
    template<>
    inline H5::DataType EquivH5Type<unsigned char>() {
        return H5::PredType::NATIVE_UCHAR;
    }
    template<>
    inline H5::DataType EquivH5Type<int>() {
        return H5::PredType::NATIVE_INT;
    }
    template<>
    inline H5::DataType EquivH5Type<float>() {
        return H5::PredType::NATIVE_FLOAT;
    }
    template<>
    inline H5::DataType EquivH5Type<double>() {
        return H5::PredType::NATIVE_DOUBLE;
    }
}

// Convenice class for using HDF5 functionality.
class SimpleHDF5Reader {
public:
    // Open a HDF5 file for reading.
    SimpleHDF5Reader(const std::string& filename) {
        H5::Exception::dontPrint();
        try {
            hdf5_file = H5::H5File(filename, H5F_ACC_RDONLY);
        } catch (...) {
            throwRuntimeError(std::string("Unable to load file: ") + filename);
        }
    }

    // Read a scalar value.
    template <typename T>
    T readScalar(const std::string & dataSetName) {
        const H5::DataSet & dataSet = hdf5_file.openDataSet(dataSetName);
        T res;
        try {
            dataSet.read(&res, detail::EquivH5Type<T>());
            return res;
        } catch (...) {
            throwRuntimeError("Error reading scalar.");
        }
    }

    // Read a std::vector of elements, uses readMultiArray internally.
    template <typename T>
    std::vector<T> readStdVector(const std::string & dataSetName) {
        auto v = readMultiArray<T, 1>(dataSetName);
        auto size = v.shape()[0];
        std::vector<T> res(size);
        for (size_t i = 0; i < size; i++) {
            res[i] = v[i];
        }
        return res;
    }

    template<typename T, int N>
    boost::multi_array<T, N> readMultiArray(const std::string& dataset_name) {
        H5::DataSet dataset = hdf5_file.openDataSet(dataset_name);
        typedef boost::multi_array<T, N> array_type;
        array_type res;
        try {
            std::vector<int> temp_dimensions = getDimensions(dataset);
            int rank = static_cast<int>(temp_dimensions.size());
            if (rank != N) {
                throw std::runtime_error("Rank mistmatch");
            }

            boost::array<int, N> dimensions;
            for (int dim = 0; dim < N; dim++) {
                dimensions[dim] = temp_dimensions[dim];
            }

            //res.resize(boost::array<array_type::index, N>(dimensions));
            res.resize(dimensions);
            dataset.read(res.data(), detail::EquivH5Type<T>());
        } catch (...) {
            throwRuntimeError("Unable to read N-dim array");
        }
        return res;
    }

    // Get the dimensions of a data set as a a std::vector of ints.
    static std::vector<int> getDimensions(const H5::DataSet & dataSet) {
        H5::DataSpace dataSpace = dataSet.getSpace();
        if (!dataSpace.isSimple()) {
            throwRuntimeError("Complex data space is not supported.");
        }
        int ndim = (int)dataSpace.getSimpleExtentNdims();
        // Get a vector of dimensions
        hsize_t *dims = new hsize_t[ndim];
        dataSpace.getSimpleExtentDims(dims, NULL);
        // Copy over to a std::vector
        std::vector<int> res;
        res.resize(ndim);
        for (int dim=0; dim < ndim; dim++) {
            res[dim] = static_cast<int>(dims[dim]);
        }
        delete [] dims;
        return res;
    }

    std::vector<int> getDimensions(const std::string& dataset_name) {
        H5::DataSet dataset = hdf5_file.openDataSet(dataset_name);
        return getDimensions(dataset);
    }

    ~SimpleHDF5Reader() {
        hdf5_file.close();
    }

protected:
    // Convenience function for appending class name to exception message.
    static void throwRuntimeError(const std::string& msg) {
        std::string func_str;
#ifdef _MSC_VER
        func_str = std::string(__FUNCTION__) + msg;
#else
        func_str = std::string(__func__);
#endif
        throw std::runtime_error(func_str + std::string(" : ") + msg);
    }

protected:
    H5::H5File hdf5_file;
};

} // end namespace SimpleHDF

max_stars_repo_name: sigurdstorve/OpenBCSim

max_issues_repo_name: rojsc/OpenBCSim

max_forks_repo_name: rojsc/OpenBCSim


text:
#include <armadillo>
#include <cstring>

#include "matrix/utility.hh"

#include "gyro/gyro_rocket6g.hh"

#include "stochastic.hh"

#include <ctime>

GyroRocket6G::GyroRocket6G(Data_exchang &input)
    : VECTOR_INIT(EUG, 3),
      VECTOR_INIT(EWG, 3),
      VECTOR_INIT(EWALKG, 3),
      VECTOR_INIT(EUNBG, 3),
      VECTOR_INIT(EMISG, 3),
      VECTOR_INIT(ESCALG, 3),
      VECTOR_INIT(EBIASG, 3),
      VECTOR_INIT(ITA1, 3),
      VECTOR_INIT(ITA2, 3),
      VECTOR_INIT(BETA, 3) {
  snprintf(name, sizeof(name), "Rocket6G Gyro Sensor Model");
  srand(static_cast<unsigned int>(time(NULL)));
  data_exchang = &input;
}

GyroRocket6G::GyroRocket6G(const GyroRocket6G &other) {
  this->WBICB = other.WBICB;
  this->EWBIB = other.EWBIB;
  this->EUG = other.EUG;
  this->EWG = other.EWG;
  this->EWALKG = other.EWALKG;
  this->EUNBG = other.EUNBG;
  this->EMISG = other.EMISG;
  this->ESCALG = other.ESCALG;
  this->EBIASG = other.EBIASG;
  this->ITA1 = other.ITA1;
  this->ITA2 = other.ITA2;
  this->BETA = other.BETA;
  this->data_exchang = other.data_exchang;
}

GyroRocket6G &GyroRocket6G::operator=(const GyroRocket6G &other) {
  if (&other == this) return *this;

  this->WBICB = other.WBICB;
  this->EWBIB = other.EWBIB;
  this->EUG = other.EUG;
  this->EWG = other.EWG;
  this->EWALKG = other.EWALKG;
  this->EUNBG = other.EUNBG;
  this->EMISG = other.EMISG;
  this->ESCALG = other.ESCALG;
  this->EBIASG = other.EBIASG;
  this->ITA1 = other.ITA1;
  this->ITA2 = other.ITA2;
  this->BETA = other.BETA;
  this->data_exchang = other.data_exchang;

  return *this;
}

void GyroRocket6G::algorithm(double int_step) {
  arma::vec3 WBIB = grab_WBIB();
  arma::vec3 FSPB = grab_FSPB();

  //-------------------------------------------------------------------------
  // ARW RRW
  double sig(1.0);
  double RRW(0.0130848811);  // 0.4422689813  7.6072577e-3
  double ARW(0.2828427125);  // 0.07071067812  7.90569415e-3
  double Freq(200.0);

  for (int i = 0; i < 3; i++) {
    ITA2(i) =
        gauss(0, 1.0) * RRW * RAD;  // distribution(generator) * RRW * RAD;
    BETA(i) = 0.9999 * BETA(i) + ITA2(i) * int_step;
    ITA1(i) = gauss(0, 1.0) * (ARW * sqrt(Freq) / 60 * (1 / sig)) *
              RAD;  // distribution(generator) * (ARW * sqrt(Freq) / 60 * (1 /
                    // sig)) * RAD;
  }

  // combining all uncertainties
  this->EWBIB = ITA1 + BETA;  // EMSBG + EUG + EWG;

  this->WBICB = WBIB + EWBIB;

  data_exchang->hset("WBICB", WBICB);
  return;
}

int GyroRocket6G::write_to_(const char *bus_name) {
  int rc = 0;
  rc |= nxbus_mset(NXBUS_DOUBLE, "Gyro:_WBICB", 3, _WBICB);
  rc |= nxbus_mset(NXBUS_DOUBLE, "Gyro:_EWBIB", 3, _EWBIB);
  return rc;
}

ext: cpp


text:
#include "FilterManager.h"
#include "../group-manager/PropValueTable.h" // pvid_t
#include "../group-manager/GroupManager.h"
#include "../attr-manager/AttrManager.h"
#include <search-manager/NumericPropertyTableBuilder.h>
#include <document-manager/Document.h>
#include <document-manager/DocumentManager.h>
#include <boost/filesystem.hpp>
#include <fstream>
#include <iostream>

using namespace izenelib::util;
namespace
{
struct IsDeleted
{
    IsDeleted(const boost::shared_ptr<sf1r::DocumentManager>& doc_manager)
        :inner_doc_manager_(doc_manager)
    {
    }
    bool operator()(uint32_t single_res) const
    {
        return inner_doc_manager_->isDeleted(single_res);
    }
private:
    const boost::shared_ptr<sf1r::DocumentManager>& inner_doc_manager_;
};
}

namespace sf1r
{

FilterManager::FilterManager(
        const boost::shared_ptr<DocumentManager>& d,
        faceted::GroupManager* g,
        const std::string& rootpath,
        faceted::AttrManager* attr,
        NumericPropertyTableBuilder* builder)
    : document_manager_(d)
    , groupManager_(g)
    , attrManager_(attr)
    , numericTableBuilder_(builder)
    , data_root_path_(rootpath)
    , rebuild_all_(false)
{
}

FilterManager::~FilterManager()
{
}

faceted::GroupManager* FilterManager::getGroupManager() const
{
    return groupManager_;
}

faceted::AttrManager* FilterManager::getAttrManager() const
{
    return attrManager_;
}

NumericPropertyTableBuilder* FilterManager::getNumericTableBuilder() const
{
    return numericTableBuilder_;
}

void FilterManager::loadStrFilterInvertedData(
        const std::vector<std::string>& property_list,
        std::vector<StrFilterItemMapT>& str_filter_data,
        std::vector<uint32_t>& last_docid_list)
{
    str_filter_data.resize(property_list.size());
    last_docid_list.clear();
    last_docid_list.resize(property_list.size(), 0);

    for (size_t prop_num = 0; prop_num < property_list.size(); ++prop_num)
    {
        const std::string& property = property_list[prop_num];
        std::ifstream ifs((data_root_path_ + "/str_filter_inverted.data." + property).c_str());
        if (ifs)
        {
            LOG(INFO) << "loading filter data ";
            size_t filter_num = 0;
            ifs.read((char*)&filter_num, sizeof(filter_num));
            LOG(INFO) << "filter total num: " << filter_num;
            for (size_t i = 0; i < filter_num; ++i)
            {
                size_t key_len;
                size_t docid_len;
                ifs.read((char*)&key_len, sizeof(key_len));
                std::string key(key_len, '\0');
                ifs.read((char*)&key[0], key_len);
                ifs.read((char*)&docid_len, sizeof(docid_len));
                std::vector<uint32_t> docid_list(docid_len);
                ifs.read((char*)&docid_list[0], sizeof(docid_list[0]) * docid_len);

                LOG(INFO) << "filter num: " << i << ", key=" << key << ", docnum: " << docid_len;
                if (!docid_list.empty())
                {
                    last_docid_list[prop_num] = std::max(last_docid_list[prop_num], docid_list.back());
                }
                str_filter_data[prop_num].insert(std::make_pair(UString(key, UString::UTF_8), docid_list));
            }
        }
        else
        {
            LOG(INFO) << "propterty: " << property << ", no inverted file found!";
        }
    }
}

void FilterManager::saveStrFilterInvertedData(
        const std::vector<std::string>& property_list,
        const std::vector<StrFilterItemMapT>& str_filter_data) const
{
    assert(str_filter_data.size() == property_list.size());
    for (size_t prop_num = 0; prop_num < property_list.size(); ++prop_num)
    {
        const std::string& property = property_list[prop_num];
        std::ofstream ofs((data_root_path_ + "/str_filter_inverted.data." + property).c_str());
        if (ofs)
        {
            LOG(INFO) << "saveing string filter data for: " << property;
            const size_t filter_num = str_filter_data[prop_num].size();
            ofs.write((const char*)&filter_num, sizeof(filter_num));
            for (StrFilterItemMapT::const_iterator cit = str_filter_data[prop_num].begin();
                    cit != str_filter_data[prop_num].end(); ++cit)
            {
                std::string keystr;
                cit->first.convertString(keystr, UString::UTF_8);
                const size_t key_len = keystr.size();
                const size_t docid_len = cit->second.size();
                ofs.write((const char*)&key_len, sizeof(key_len));
                ofs.write(keystr.data(), key_len);
                ofs.write((const char*)&docid_len, sizeof(docid_len));
                ofs.write((const char*)&cit->second[0], sizeof(cit->second[0]) * docid_len);
            }
        }
    }
}

void FilterManager::setGroupFilterProperties(std::vector<std::string>& property_list)
{
    group_prop_list_ = property_list;
}

void FilterManager::setAttrFilterProperties(std::vector<std::string>& property_list)
{
    attr_prop_list_ = property_list;
}

void FilterManager::setStrFilterProperties(std::vector<std::string>& property_list)
{
    str_prop_list_ = property_list;
}

void FilterManager::setDateFilterProperties(std::vector<std::string>& property_list)
{
    date_prop_list_ = property_list;
}

void FilterManager::setNumFilterProperties(std::vector<std::string>& property_list, std::vector<int32_t>& amp_list)
{
    assert(property_list.size() == amp_list.size());
    num_prop_list_ = property_list;
    std::map<std::string, int32_t> num_amp_map;
    for (size_t i = 0; i < property_list.size(); ++i)
    {
        num_amp_map[property_list[i]] = amp_list[i];
    }
    setNumericAmp(num_amp_map);
}

void FilterManager::copyPropertyInfo(const boost::shared_ptr<FilterManager>& other)
{
    group_prop_list_ = other->group_prop_list_;
    attr_prop_list_ = other->attr_prop_list_;
    str_prop_list_ = other->str_prop_list_;
    date_prop_list_ = other->date_prop_list_;
    num_prop_list_ = other->num_prop_list_;

    prop_id_map_ = other->prop_id_map_;
    prop_list_ = other->prop_list_;
    num_amp_list_ = other->num_amp_list_;

    num_amp_map_ = other->num_amp_map_;
}

void FilterManager::generatePropertyId()
{
    generatePropertyIdForList(group_prop_list_, GROUP_ATTR_FILTER);
    generatePropertyIdForList(attr_prop_list_, GROUP_ATTR_FILTER);
    generatePropertyIdForList(str_prop_list_, STR_FILTER);
    generatePropertyIdForList(date_prop_list_, NUM_FILTER);
    generatePropertyIdForList(num_prop_list_, NUM_FILTER);

    str_filter_ids_.resize(prop_list_.size());
    num_filter_ids_.resize(prop_list_.size());
    prop_filter_str_list_.resize(prop_list_.size());
    str_key_sets_.resize(prop_list_.size());
    num_key_sets_.resize(prop_list_.size());

    filter_list_.resize(prop_list_.size());////xxxx
    str_filter_map_.resize(str_prop_list_.size());
}

void FilterManager::generatePropertyIdForList(const std::vector<std::string>& prop_list, FilterType type)
{
    for (std::vector<std::string>::const_iterator it = prop_list.begin();
            it != prop_list.end(); ++it)
    {
        size_t prop_id = prop_id_map_.size();
        if (prop_id_map_.find(*it) == prop_id_map_.end())
        {
            prop_id_map_[*it] = prop_id;
            prop_list_.resize(prop_id + 1);
            prop_list_[prop_id].first = type;
            prop_list_[prop_id].second = *it;
            if (type == NUM_FILTER)
            {
                num_amp_list_.resize(prop_id + 1, 1);
                std::map<std::string, int32_t>::const_iterator mit = num_amp_map_.find(*it);
                if (mit != num_amp_map_.end())
                {
                    num_amp_list_[prop_id] = mit->second;
                }
            }
        }
    }
}

void FilterManager::buildFilters(uint32_t last_docid, uint32_t max_docid, bool isIncre)
{
    std::vector<StrFilterItemMapT> group_filter_map;
    std::vector<StrFilterItemMapT> attr_filter_map;
    std::vector<NumFilterItemMapT> num_filter_map;
    std::vector<NumFilterItemMapT> date_filter_map;

    std::vector<uint32_t> group_last_docid_list;
    std::vector<uint32_t> attr_last_docid_list;

    if (last_docid > 0 && !isIncre)// for increase data
    {
        loadStrFilterInvertedData(group_prop_list_, group_filter_map, group_last_docid_list);
        loadStrFilterInvertedData(attr_prop_list_, attr_filter_map, attr_last_docid_list);
    }
    else
    {
        group_last_docid_list.resize(group_prop_list_.size());
        attr_last_docid_list.resize(attr_prop_list_.size());
    }

    if (isIncre)
    {
        str_filter_ids_.clear();
        str_filter_ids_.resize(prop_list_.size());

        num_filter_ids_.clear();
        num_filter_ids_.resize(prop_list_.size());

        filter_list_.clear();
        filter_list_.resize(prop_list_.size());

        num_key_sets_.clear();
        num_key_sets_.resize(prop_list_.size());

        for (std::vector<uint32_t>::iterator i = group_last_docid_list.begin(); i != group_last_docid_list.end(); ++i)
        {
            if (*i == 0)
            {
                *i = last_docid;
            }
        }
    }

    buildGroupFilters(group_last_docid_list, max_docid, group_prop_list_, group_filter_map);
    saveStrFilterInvertedData(group_prop_list_, group_filter_map);

    buildAttrFilters(attr_last_docid_list, max_docid, attr_prop_list_, attr_filter_map);
    saveStrFilterInvertedData(attr_prop_list_, attr_filter_map);

    if (isIncre)
    {
        buildDateFilters(last_docid, max_docid, date_prop_list_, date_filter_map);
        buildNumFilters(last_docid, max_docid, num_prop_list_, num_filter_map);
    }
    else
    {
        buildDateFilters(0, max_docid, date_prop_list_, date_filter_map);
        buildNumFilters(0, max_docid, num_prop_list_, num_filter_map);
    }
}

void FilterManager::buildStringFiltersForDoc(docid_t doc_id, const Document& doc)
{
    if (str_prop_list_.empty()) return;

    for (size_t i = 0; i < str_prop_list_.size(); ++i)
    {
        const std::string& property = str_prop_list_[i];
        Document::property_const_iterator dit = doc.findProperty(property);
        if (dit == doc.propertyEnd())
        {
            continue;
        }
        size_t prop_id = prop_id_map_[property];
        size_t n = 0, nOld = 0;
        UString value = propstr_to_ustr(dit->second.getPropertyStrValue());
        if (value.empty())
        {
            continue;
        }
        if ((n = value.find((UString::CharT)',', n)) != UString::npos)
        {
            do
            {
                if (n != nOld)
                {
                    UString sub_value = value.substr(nOld, n - nOld);
                    FilterDocListT& item = str_filter_map_[i][sub_value];
                    if (item.empty())
                    {
                        str_key_sets_[prop_id].push_back(sub_value);
                    }
                    item.push_back(doc_id);
                }
                n = value.find((UString::CharT)',', ++n);
            } while (n != UString::npos);
        }
        UString sub_value = value.substr(nOld);
        FilterDocListT& item = str_filter_map_[i][sub_value];
        if (item.empty())
        {
            str_key_sets_[prop_id].push_back(sub_value);
        }
        item.push_back(doc_id);
    }
}

void FilterManager::finishBuildStringFilters()
{
    for (size_t i = 0; i < str_filter_map_.size(); ++i)
    {
        StrFilterItemMapT::iterator it = str_filter_map_[i].begin();
        while(it != str_filter_map_[i].end())
        {
            it->second.erase(std::remove_if (it->second.begin(), it->second.end(), IsDeleted(document_manager_)), it->second.end());
            if (it->second.empty())
            {
                str_filter_map_[i].erase(it++);
                continue;
            }
            ++it;
        }
    }

    for (size_t i = 0; i < str_prop_list_.size(); ++i)
    {
        size_t prop_id = prop_id_map_[str_prop_list_[i]];
        std::sort(str_key_sets_[prop_id].begin(), str_key_sets_[prop_id].end());
        mapAttrFilterToFilterId(str_filter_map_[i], str_filter_ids_[prop_id], filter_list_[prop_id]);
    }
    std::vector<StrFilterItemMapT>().swap(str_filter_map_);
}

// the last_docid means where the last indexing end for. We just build the
// inverted data begin from last_docid + 1.
void FilterManager::buildGroupFilters(
        const std::vector<uint32_t>& last_docid_list, uint32_t max_docid,
        const std::vector<std::string>& property_list,
        std::vector<StrFilterItemMapT>& group_filter_data)
{
    if (!groupManager_) return;

    group_filter_data.resize(property_list.size());
    // the relationship between group node need rebuild from docid = 1.
    GroupNode* group_root = new GroupNode(UString("root", UString::UTF_8));
    std::vector<GroupNode*> property_root_nodes;
    for (size_t j = 0; j < property_list.size(); ++j)
    {
        uint32_t last_docid_forproperty = last_docid_list[j];
        const std::string& property = property_list[j];
        size_t prop_id = prop_id_map_[property];

        group_root->appendChild(UString(property, UString::UTF_8));
        property_root_nodes.push_back(group_root->getChild(UString(property, UString::UTF_8)));

        if (isUnchangedProperty(property))
        {
            LOG(INFO) << "the filter property : " << property << " do not need rebuild since no change.";
            continue;
        }

        faceted::PropValueTable* pvt = groupManager_->getPropValueTable(property);
        if (!pvt)
        {
            LOG(INFO) << "property: " << property_list[j] << " not in group manager!";
            continue;
        }
        if (groupManager_->isRebuildProp_(property))
        {
            LOG(INFO) << "clear old group data to rebuild filter data for property: " << property;
            group_filter_data[j].clear();
            last_docid_forproperty = 0;
        }
        LOG(INFO) << "building filter data, start from:" << last_docid_forproperty <<
            ", property: " << property << ", pid: " << prop_id;
        LOG(INFO) << "building filter data, end at:" << max_docid;

        // here is all build ....
        for (uint32_t docid = 1; docid <= max_docid; ++docid)
        {
            faceted::PropValueTable::PropIdList propids;
            // get all leaf group path
            pvt->getPropIdList(docid, propids);
            for (size_t k = 0; k < propids.size(); ++k)
            {
                std::vector<izenelib::util::UString> groupPath;
                pvt->propValuePath(propids[k], groupPath);
                if (groupPath.empty()) continue;
                GroupNode* curgroup = property_root_nodes[j];
                StrFilterKeyT groupstr = groupPath[0];
                curgroup->appendChild(groupstr);
                curgroup = curgroup->getChild(groupstr);
                assert(curgroup);
                for (size_t k = 1; k < groupPath.size(); ++k)
                {
                    groupstr.append(UString(">", UString::UTF_8)).append(groupPath[k]);
                    curgroup->appendChild(groupstr);
                    curgroup = curgroup->getChild(groupstr);
                    assert(curgroup);
                }

                if (docid <= last_docid_forproperty) continue;

                group_filter_data[j][groupstr].push_back(docid);
            }
        }

        for (size_t i = 0; i < group_filter_data.size(); ++i)
        {
            StrFilterItemMapT::iterator it = group_filter_data[i].begin();
            while(it != group_filter_data[i].end())
            {
                it->second.erase(std::remove_if (it->second.begin(), it->second.end(), IsDeleted(document_manager_)), it->second.end());
                if (it->second.empty())
                {
                    group_filter_data[i].erase(it++);
                    continue;
                }
                ++it;
            }
        }

        // map the group filter string to filter id.
        mapGroupFilterToFilterId(
                property_root_nodes[j],
                group_filter_data[j],
                str_filter_ids_[prop_id],
                filter_list_[prop_id]);
        std::vector<StrFilterKeyT>().swap(prop_filter_str_list_[prop_id]);
        prop_filter_str_list_[prop_id].reserve(str_filter_ids_[prop_id].size() + 1);

        prop_filter_str_list_[prop_id].push_back(UString("", UString::UTF_8));
        for (StrIdMapT::const_iterator filterstr_it = str_filter_ids_[prop_id].begin();
                filterstr_it != str_filter_ids_[prop_id].end(); ++filterstr_it)
        {
            prop_filter_str_list_[prop_id].push_back(filterstr_it->first);
        }

        //printNode(property_root_nodes[j], 0, str_filter_ids_[prop_id], filter_list_[prop_id]);
    }
    delete group_root;
    LOG(INFO) << "finish building group filter data.";
}

void FilterManager::mapGroupFilterToFilterId(
        GroupNode* node,
        const StrFilterItemMapT& group_filter_data,
        StrIdMapT& filterids,
        std::vector<FilterDocListT>& filter_list)
{
    if (node)
    {
        filterids[node->node_name_].start = filter_list.size();
        string groupstr1;
        node->node_name_.convertString(groupstr1, UString::UTF_8);

        StrFilterItemMapT::const_iterator cit = group_filter_data.find(node->node_name_);
        if (cit != group_filter_data.end())
            filter_list.push_back(cit->second);
        for (GroupNode::constIter group_cit = node->beginChild();
                group_cit != node->endChild(); ++group_cit)
        {
            mapGroupFilterToFilterId(group_cit->second, group_filter_data, filterids, filter_list);
        }
        filterids[node->node_name_].end = filter_list.size();
    }
}

void FilterManager::buildAttrFilters(
        const std::vector<uint32_t>& last_docid_list, uint32_t max_docid,
        const std::vector<std::string>& property_list,
        std::vector<StrFilterItemMapT>& attr_filter_data)
{
    if (!attrManager_ || property_list.empty())
        return;

    uint32_t last_docid = last_docid_list[0];

    LOG(INFO) << "begin building attribute filter data.";

    attr_filter_data.resize(1);

    const std::string& property = property_list[0];
    const faceted::AttrTable& attr_table = attrManager_->getAttrTable();
    size_t prop_id = prop_id_map_[property];

    if (isUnchangedProperty(property))
    {
        LOG(INFO) << "the filter property : " << property << " do not need rebuild since no change.";
        return;
    }

    for (uint32_t docid = last_docid + 1; docid <= max_docid; ++docid)
    {
        faceted::AttrTable::ValueIdList attrvids;
        attr_table.getValueIdList(docid, attrvids);
        for (size_t k = 0; k < attrvids.size(); ++k)
        {
            StrFilterKeyT attrstr = formatAttrPath(
                    attr_table.nameStr(attr_table.valueId2NameId(attrvids[k])),
                    attr_table.valueStr(attrvids[k]));
            attr_filter_data[0][attrstr].push_back(docid);
        }
    }

    for (size_t i = 0; i < attr_filter_data.size(); ++i)
    {
        StrFilterItemMapT::iterator it = attr_filter_data[i].begin();
        while(it != attr_filter_data[i].end())
        {
            it->second.erase(std::remove_if (it->second.begin(), it->second.end(), IsDeleted(document_manager_)), it->second.end());
            if (it->second.empty())
            {
                attr_filter_data[i].erase(it++);
                continue;
            }
            ++it;
        }
    }
    // map the attribute filter string to filter id.
    mapAttrFilterToFilterId(attr_filter_data[0], str_filter_ids_[prop_id], filter_list_[prop_id]);

    std::vector<StrFilterKeyT>().swap(prop_filter_str_list_[prop_id]);
    prop_filter_str_list_[prop_id].reserve(str_filter_ids_[prop_id].size() + 1);
    prop_filter_str_list_[prop_id].push_back(UString("", UString::UTF_8));
    for (StrIdMapT::const_iterator filterstr_it = str_filter_ids_[prop_id].begin();
            filterstr_it != str_filter_ids_[prop_id].end(); ++filterstr_it)
    {
        prop_filter_str_list_[prop_id].push_back(filterstr_it->first);
    }

    LOG(INFO) << "finish building attribute filter data.";
}

void FilterManager::mapAttrFilterToFilterId(const StrFilterItemMapT& attr_filter_data, StrIdMapT& filterids, std::vector<FilterDocListT>& filter_list)
{
    for (StrFilterItemMapT::const_iterator cit = attr_filter_data.begin();
            cit != attr_filter_data.end(); ++cit)
    {
        filterids[cit->first].start = filter_list.size();
        if (!cit->second.empty())
            filter_list.push_back(cit->second);
        filterids[cit->first].end = filter_list.size();
#ifndef NDEBUG
        std::string outstr;
        cit->first.convertString(outstr, UString::UTF_8);
        LOG(INFO) << "attribute: " << outstr << ", id range: " << filterids[cit->first].start << ", "
            << filterids[cit->first].end << ", total docid size: " << cit->second.size();
#endif
    }
}

void FilterManager::buildDateFilters( // all rebuild ....
        uint32_t last_docid, uint32_t max_docid,
        const std::vector< std::string >& property_list,
        std::vector<NumFilterItemMapT>& date_filter_data)
{
    if (!groupManager_) return;

    LOG(INFO) << "begin build date filter ... ";

    date_filter_data.resize(property_list.size());

    for (size_t j = 0; j < property_list.size(); ++j)
    {
        const std::string& property = property_list[j];
        size_t prop_id = prop_id_map_[property];

        faceted::DateGroupTable* dgt = groupManager_->getDateGroupTable(property);
        if (!dgt)
        {
            LOG(INFO) << "property: " << property << " is not found in date property table!";
            continue;
        }
        std::vector<NumFilterKeyT>& num_key_set = num_key_sets_[prop_id];
        for (uint32_t docid = last_docid + 1; docid <= max_docid; ++docid)
        {
            faceted::DateGroupTable::DateSet original_date;
            dgt->getDateSet(docid, faceted::MASK_YEAR_MONTH_DAY, original_date);
            for (faceted::DateGroupTable::DateSet::const_iterator date_it = original_date.begin();
                    date_it != original_date.end(); ++date_it)
            {
                const NumFilterKeyT& num_key = (NumFilterKeyT)(*date_it);
                FilterDocListT& item = date_filter_data[j][num_key];
                if (item.empty())
                {
                    num_key_set.push_back(num_key);
                }
                item.push_back(docid);
            }
        }
        // sort num_key_set in asc order
        std::sort(num_key_set.begin(), num_key_set.end());
        // map the numeric filter to filter id.
        mapNumericFilterToFilterId(date_filter_data[j], num_filter_ids_[prop_id], filter_list_[prop_id]);
    }
    LOG(INFO) << "finish building date filter data.";
}

void FilterManager::buildNumFilters(
        uint32_t last_docid, uint32_t max_docid,
        const std::vector<std::string>& property_list,
        std::vector<NumFilterItemMapT>& num_filter_data)
{
    if (!numericTableBuilder_)
    {
        LOG(INFO) << "no numericPropertyTable builder, numeric filter will not build.";
        return;
    }

    LOG(INFO) << "begin building numeric filter data.";

    num_filter_data.resize(property_list.size());

    NumFilterKeyT num_key_low;
    NumFilterKeyT num_key_high;
    std::pair<double, double> original_key;
    NumFilterKeyT inc = 1;
    for (size_t j = 0; j < property_list.size(); ++j)
    {
        const std::string& property = property_list[j];
        size_t prop_id = prop_id_map_[property];

        LOG(INFO) << "building numeric property: " << property;
        std::map<std::string, int32_t>::const_iterator nit = num_amp_map_.find(property);
        if (nit != num_amp_map_.end())
        {
            num_amp_list_[prop_id] = nit->second;
        }
        boost::shared_ptr<NumericPropertyTableBase>& numericPropertyTable = numericTableBuilder_->createPropertyTable(property);
        if (!numericPropertyTable)
        {
            LOG(INFO) << "property: " << property << " is not found in numeric property table!";
            continue;
        }
        std::vector<NumFilterKeyT>& num_key_set = num_key_sets_[prop_id];
        for (uint32_t docid = last_docid + 1; docid <= max_docid; ++docid)
        {
            if (!numericPropertyTable->getDoublePairValue(docid, original_key))
                continue;
            num_key_low = formatNumericFilter(prop_id, original_key.first);
            num_key_high = formatNumericFilter(prop_id, original_key.second);
            inc = 1;
            if (num_key_high - num_key_low > 100)
            {
                // too larger interval. just pick some.
                inc = (num_key_high - num_key_low) / 100;
            }
            for (NumFilterKeyT num_key = num_key_low; num_key <= num_key_high;)
            {
                FilterDocListT& item = num_filter_data[j][num_key];
                if (item.empty())
                {
                    num_key_set.push_back(num_key);
                }
                item.push_back(docid);
                // make sure the num_key_high is added to index.
                if (num_key == num_key_high || num_key + inc <= num_key_high)
                    num_key += inc;
                else
                    num_key = num_key_high;
            }
        }
        // sort num_key_set in asc order
        std::sort(num_key_set.begin(), num_key_set.end());
        // map the numeric filter to filter id.
        mapNumericFilterToFilterId(num_filter_data[j], num_filter_ids_[prop_id], filter_list_[prop_id]);
    }
    LOG(INFO) << "finish building numeric filter data.";
}

void FilterManager::mapNumericFilterToFilterId(const NumFilterItemMapT& num_filter_data, NumIdMapT& filterids, std::vector<FilterDocListT>& filter_list)
{
    for (NumFilterItemMapT::const_iterator cit = num_filter_data.begin();
            cit != num_filter_data.end(); ++cit)
    {
        filterids[cit->first].start = filter_list.size();
        if (!cit->second.empty())
            filter_list.push_back(cit->second);
        filterids[cit->first].end = filter_list.size();
#ifndef NDEBUG
        LOG(INFO) << "num: " << cit->first << ", id range: " << filterids[cit->first].start << ", "
            << filterids[cit->first].end << ", total docid size: " << cit->second.size();
#endif
    }
}

std::vector<std::vector<FilterManager::FilterDocListT> >& FilterManager::getFilterList()
{
    return filter_list_;
}

void FilterManager::clearFilterList()
{
    std::vector<std::vector<FilterDocListT> >().swap(filter_list_);
}

void FilterManager::clearFilterId()
{
    std::map<std::string, size_t>().swap(prop_id_map_);
    std::vector<std::pair<int32_t, std::string> >().swap(prop_list_);
    StrPropIdVecT().swap(str_filter_ids_);
    NumPropIdVecT().swap(num_filter_ids_);
    PropFilterStrVecT().swap(prop_filter_str_list_);
}

void FilterManager::saveFilterId()
{
    if (prop_list_.empty())
    {
        LOG(WARNING) << "no filter ids to be save.";
        return;
    }

    std::ofstream ofs((data_root_path_ + "/prop_list.txt").c_str());
    ofs << prop_list_.size() << std::endl;
    for (size_t i = 0; i < prop_list_.size(); ++i)
    {
        ofs << prop_list_[i].first << " " << prop_list_[i].second << std::endl;
    }

    for (size_t i = 0; i < prop_list_.size(); ++i)
    {
        std::ofstream ofs((data_root_path_ + "/filterid." + prop_list_[i].second).c_str());
        if (!ofs) continue;

        switch (prop_list_[i].first)
        {
        case GROUP_ATTR_FILTER:
            {
                size_t num = str_filter_ids_[i].size();
                ofs.write((const char*)&num, sizeof(num));
                for (StrIdMapT::const_iterator it = str_filter_ids_[i].begin();
                        it != str_filter_ids_[i].end(); ++it)
                {
                    std::string str_key;
                    it->first.convertString(str_key, UString::UTF_8);
                    saveArray_(ofs, str_key);
                    ofs.write((const char*)&(it->second), sizeof(it->second));
                }
            }
            break;

        case STR_FILTER:
            {
                size_t num = str_filter_ids_[i].size();
                ofs.write((const char*)&num, sizeof(num));
                for (StrIdMapT::const_iterator it = str_filter_ids_[i].begin();
                        it != str_filter_ids_[i].end(); ++it)
                {
                    std::string str_key;
                    it->first.convertString(str_key, UString::UTF_8);
                    saveArray_(ofs, str_key);
                    ofs.write((const char*)&(it->second), sizeof(it->second));
                }
            }
            break;

        case NUM_FILTER:
            {
                size_t num = num_filter_ids_[i].size();
                ofs.write((const char*)&num, sizeof(num));
                for (NumIdMapT::const_iterator it = num_filter_ids_[i].begin();
                        it != num_filter_ids_[i].end(); ++it)
                {
                    ofs.write((const char*)&(*it), sizeof(*it));
                }
            }
            break;

        default:
            break;
        }
    }
}

void FilterManager::loadFilterId()
{
    std::ifstream ifs((data_root_path_ + "/prop_list.txt").c_str());
    if (!ifs)
    {
        LOG(WARNING) << "no filter ids to be load.";
        return;
    }

    size_t prop_count = 0;
    ifs >> prop_count;
    std::vector<std::pair<int32_t, std::string> > tmp_prop_list(prop_count);
    for (size_t i = 0; i < prop_count; ++i)
    {
        ifs >> tmp_prop_list[i].first >> tmp_prop_list[i].second;
    }

    for (size_t i = 0; i < prop_count; ++i)
    {
        const std::string& property = tmp_prop_list[i].second;
        std::string loadpath = data_root_path_ + "/filterid." + property;
        std::ifstream ifs(loadpath.c_str());
        if (!ifs)
        {
            LOG(WARNING) << "the property filter id not found: " << property;
            rebuild_all_ = true;
            continue;
        }
        LOG(INFO) << "loading filter id map for property: " << property;
        prop_list_.push_back(tmp_prop_list[i]);
        prop_id_map_[property] = prop_list_.size() - 1;

        size_t num = 0;
        ifs.read((char*)&num, sizeof(num));
        LOG(INFO) << "filter id num: " << num;

        switch (prop_list_.back().first)
        {
        case GROUP_ATTR_FILTER:
            {
                str_filter_ids_.resize(i + 1);
                prop_filter_str_list_.resize(i + 1);
                StrIdMapT& strid_map = str_filter_ids_[i];
                // start from 1. so reserve the first.
                std::vector<StrFilterKeyT>().swap(prop_filter_str_list_[i]);
                prop_filter_str_list_[i].reserve(prop_filter_str_list_.size() + num + 1);
                prop_filter_str_list_[i].push_back(UString("", UString::UTF_8));
                for (size_t j = 0; j < num; ++j)
                {
                    std::string str_key;
                    loadArray_(ifs, str_key);
                    FilterIdRange& idrange = strid_map[UString(str_key, UString::UTF_8)];
                    ifs.read((char*)&idrange, sizeof(idrange));
                    prop_filter_str_list_[i].push_back(UString(str_key, UString::UTF_8));
                }
            }
            break;

        case STR_FILTER:
            {
                str_filter_ids_.resize(i + 1);
                str_key_sets_.resize(i + 1);
                std::vector<StrFilterKeyT>& str_key_set = str_key_sets_[i];
                str_key_set.resize(num);
                StrIdMapT& filter_ids = str_filter_ids_[i];
                for (size_t j = 0; j < num; ++j)
                {
                    std::string str_key;
                    loadArray_(ifs, str_key);
                    FilterIdRange& idrange = filter_ids[UString(str_key, UString::UTF_8)];
                    ifs.read((char*)&idrange, sizeof(idrange));
                    str_key_set[j] = UString(str_key, UString::UTF_8);
                }
            }
            break;

        case NUM_FILTER:
            {
                num_filter_ids_.resize(i + 1);
                num_key_sets_.resize(i + 1);
                num_amp_list_.resize(i + 1, 1);
                std::map<std::string, int32_t>::const_iterator mit = num_amp_map_.find(property);
                if (mit != num_amp_map_.end())
                {
                    num_amp_list_[i] = mit->second;
                }
                std::vector<NumFilterKeyT>& num_key_set = num_key_sets_[i];
                num_key_set.resize(num);
                NumIdMapT& filter_ids = num_filter_ids_[i];
                NumFilterKeyT num_key;
                for (size_t j = 0; j < num; ++j)
                {
                    ifs.read((char*)&num_key, sizeof(num_key));
                    FilterIdRange& idrange = filter_ids[num_key];
                    ifs.read((char*)&idrange, sizeof(idrange));
                    filter_ids.insert(std::make_pair(num_key, idrange));
                    num_key_set[j] = num_key;
                }
            }
            break;

        default:
            break;
        }
    }
}
void FilterManager::saveFilterList()
{
    string docid_path = data_root_path_ + "/filter.list";
    FILE* file;
    if ((file = fopen(docid_path.c_str(), "wb")) == NULL)
    {
        LOG(INFO) << "Cannot open output file"<<endl;
        return;
    }
    unsigned int size_1 = filter_list_.size();
    fwrite(&size_1, sizeof(size_1), 1, file);

    for (std::vector<std::vector<FilterDocListT> >::iterator i = filter_list_.begin(); i != filter_list_.end(); ++i)
    {
        uint32_t size_2 = i->size();
        fwrite(&size_2, sizeof(size_2), 1, file);

        for (std::vector<FilterDocListT>::iterator j = (*i).begin(); j != (*i).end(); ++j)
        {
            uint32_t size_3 = j->size();
            fwrite(&size_3, sizeof(size_3), 1, file);

            for (FilterDocListT::iterator k = (*j).begin(); k != (*j).end(); ++k)
            {
                uint32_t value = *k;
                fwrite(&value, sizeof(value), 1, file);
            }
        }
    }
}

bool FilterManager::loadFilterList()
{
    string docid_path = data_root_path_ + "/filter.list";
    FILE* file;
    if ((file = fopen(docid_path.c_str(), "rb")) == NULL)
    {
        LOG(INFO) << "Cannot open output file"<<endl;
        return false;
    }

    unsigned int size = 0;
    if (1 != fread(&size, sizeof(size), 1, file) ) return false;

    for (unsigned int i = 0; i < size; ++i)
    {
        unsigned int count = 0;
        if (1 != fread(&count, sizeof(count), 1, file) ) return false;
        std::vector<FilterDocListT> v;
        for (unsigned int j = 0; j < count; ++j)
        {
            unsigned int count_1 = 0;
            if (1 != fread(&count_1, sizeof(count_1), 1, file) ) return false;
            FilterDocListT filterDocList;
            for (unsigned int i = 0; i < count_1; ++i)
            {
                uint32_t value = 0;
                if (1 != fread(&value, sizeof(value), 1, file) ) return false;
                filterDocList.push_back(value);
            }
            v.push_back(filterDocList);
        }
        filter_list_.push_back(v);
    }
    return true;
}

izenelib::util::UString FilterManager::formatGroupPath(const std::vector<std::string>& groupPath) const
{
    izenelib::util::UString groupstr;
    if (groupPath.empty()) return groupstr;

    std::string group_tmpstr = groupPath[0];
    for (size_t k = 1; k < groupPath.size(); ++k)
    {
        if (!groupPath[k].empty())
            group_tmpstr += ">" + groupPath[k];
    }
    return UString(group_tmpstr, UString::UTF_8);
}

izenelib::util::UString FilterManager::formatGroupPath(const std::vector<izenelib::util::UString>& groupPath) const
{
    izenelib::util::UString groupstr;
    if (groupPath.empty()) return groupstr;

    groupstr = groupPath[0];
    for (size_t k = 1; k < groupPath.size(); ++k)
    {
        if (!groupPath[k].empty())
            groupstr.append(UString(">", UString::UTF_8)).append(groupPath[k]);
    }
    return groupstr;
}

izenelib::util::UString FilterManager::formatAttrPath(const std::string& attrname, const std::string& attrvalue) const
{
    return UString(attrname + ": " + attrvalue, UString::UTF_8);
}

izenelib::util::UString FilterManager::formatAttrPath(const izenelib::util::UString& attrname, const izenelib::util::UString& attrvalue) const
{
    izenelib::util::UString retstr = attrname;
    retstr.push_back(':');
    return retstr.append(attrvalue);
}

FilterManager::FilterIdRange FilterManager::getStrFilterIdRangeExact(size_t prop_id, const StrFilterKeyT& str_filter)
{
    static const FilterIdRange empty_range;
    StrIdMapT::const_iterator it = str_filter_ids_[prop_id].find(str_filter);
    std::string outstr;
    str_filter.convertString(outstr, UString::UTF_8);
    if (it == str_filter_ids_[prop_id].end())
    {
        // no such group
        LOG(INFO) << "string filter key: " << outstr << " not found in pid:" << prop_id;
        return empty_range;
    }
    LOG(INFO) << "string filter key: " << outstr << ", filter id range is: " << it->second.start << ", " << it->second.end;
    return it->second;
}

FilterManager::FilterIdRange FilterManager::getStrFilterIdRangeGreater(size_t prop_id, const StrFilterKeyT& str_filter, bool include)
{
    FilterIdRange empty_range;
    if (prop_id >= str_key_sets_.size())
        return empty_range;
    const std::vector<StrFilterKeyT>& str_key_set = str_key_sets_[prop_id];

    std::vector<StrFilterKeyT>::const_iterator it = include
        ? std::lower_bound(str_key_set.begin(), str_key_set.end(), str_filter)
        : std::upper_bound(str_key_set.begin(), str_key_set.end(), str_filter);
    if (it == str_key_set.end())
    {
        return empty_range;
    }

    StrIdMapT::const_iterator sit = str_filter_ids_[prop_id].find(*it);
    if (sit == str_filter_ids_[prop_id].end())
    {
        return empty_range;
    }

    empty_range.start = sit->second.start;
    empty_range.end = str_filter_ids_[prop_id].find(str_key_set.back())->second.end;

    std::string outstr;
    str_filter.convertString(outstr, UString::UTF_8);
    LOG(INFO) << "string filter larger map to key: " << outstr << ", filter id range is: " << empty_range.start << ", " << empty_range.end;

    return empty_range;
}

FilterManager::FilterIdRange FilterManager::getStrFilterIdRangeLess(size_t prop_id, const StrFilterKeyT& str_filter, bool include)
{
    FilterIdRange empty_range;
    if (prop_id >= str_key_sets_.size())
        return empty_range;
    const std::vector<StrFilterKeyT>& str_key_set = str_key_sets_[prop_id];

    std::vector<StrFilterKeyT>::const_iterator it = std::lower_bound(str_key_set.begin(), str_key_set.end(), str_filter);
    if (it == str_key_set.begin() && (!include || it->compare(str_filter) > 0))
    {
        return empty_range;
    }
    if (!include || it == str_key_set.end())
    {
        --it;
    }

    StrIdMapT::const_iterator sit = str_filter_ids_[prop_id].find(*it);
    if (sit == str_filter_ids_[prop_id].end())
    {
        return empty_range;
    }

    empty_range.start = str_filter_ids_[prop_id].find(str_key_set[0])->second.start;
    empty_range.end = sit->second.end;

    std::string outstr;
    str_filter.convertString(outstr, UString::UTF_8);
    LOG(INFO) << "string filter smaller map to key: " << outstr << ", filter id range is: " << empty_range.start << ", " << empty_range.end;

    return empty_range;
}

FilterManager::FilterIdRange FilterManager::getStrFilterIdRangePrefix(size_t prop_id, const StrFilterKeyT& str_filter)
{
    FilterIdRange empty_range;
    if (prop_id >= str_key_sets_.size())
        return empty_range;
    const std::vector<StrFilterKeyT>& str_key_set = str_key_sets_[prop_id];

    std::vector<StrFilterKeyT>::const_iterator it0 = std::lower_bound(str_key_set.begin(), str_key_set.end(), str_filter, PrefixCompare(str_filter.length()));
    if (it0 == str_key_set.end())
    {
        return empty_range;
    }
    std::vector<StrFilterKeyT>::const_iterator it1 = std::upper_bound(str_key_set.begin(), str_key_set.end(), str_filter, PrefixCompare(str_filter.length()));
    if (it1 == str_key_set.begin())
    {
        return empty_range;
    }
    --it1;

    StrIdMapT::const_iterator sit0 = str_filter_ids_[prop_id].find(*it0);
    if (sit0 == str_filter_ids_[prop_id].end())
    {
        return empty_range;
    }
    StrIdMapT::const_iterator sit1 = str_filter_ids_[prop_id].find(*it1);
    if (sit1 == str_filter_ids_[prop_id].end())
    {
        return empty_range;
    }

    empty_range.start = sit0->second.start;
    empty_range.end = sit1->second.end;

    std::string outstr;
    str_filter.convertString(outstr, UString::UTF_8);
    LOG(INFO) << "string filter smaller map to key: " << outstr << ", filter id range is: " << empty_range.start << ", " << empty_range.end;

    return empty_range;
}

FilterManager::NumFilterKeyT FilterManager::formatNumericFilter(size_t prop_id, double num_filter, bool tofloor) const
{
    int32_t amplifier = num_amp_list_[prop_id];
    return tofloor ? (NumFilterKeyT)std::floor(num_filter * amplifier) : (NumFilterKeyT)std::ceil(num_filter * amplifier);
}

FilterManager::FilterIdRange FilterManager::getNumFilterIdRangeExact(size_t prop_id, double num_filter) const
{
    static const FilterIdRange empty_range;
    NumFilterKeyT numkey = formatNumericFilter(prop_id, num_filter);
    NumIdMapT::const_iterator it = num_filter_ids_[prop_id].find(numkey);
    if (it == num_filter_ids_[prop_id].end())
    {
        // no such group
        return empty_range;
    }

    LOG(INFO) << "numeric filter map to key: " << numkey << ", filter id range is: " << it->second.start << "," << it->second.end;

    return it->second;
}

FilterManager::FilterIdRange FilterManager::getNumFilterIdRangeGreater(size_t prop_id, double num_filter, bool include) const
{
    FilterIdRange empty_range;
    NumFilterKeyT num_key = formatNumericFilter(prop_id, num_filter, true);
    const std::vector<NumFilterKeyT>& num_key_set = num_key_sets_[prop_id];
    if (num_key_set.size() == 0)
    {
        return empty_range;
    }

    std::vector<NumFilterKeyT>::const_iterator it = include
        ? std::lower_bound(num_key_set.begin(), num_key_set.end(), num_key)
        : std::upper_bound(num_key_set.begin(), num_key_set.end(), num_key);
    if (it == num_key_set.end())
    {
        return empty_range;
    }
    NumIdMapT::const_iterator nit = num_filter_ids_[prop_id].find(*it);
    if (nit == num_filter_ids_[prop_id].end())
    {
        return empty_range;
    }

    empty_range.start = nit->second.start;
    empty_range.end = num_filter_ids_[prop_id].find(num_key_set.back())->second.end;

    LOG(INFO) << "numeric filter larger map to key: " << *it << ", filter id range is: " << empty_range.start << "," << empty_range.end;

    return empty_range;
}

FilterManager::FilterIdRange FilterManager::getNumFilterIdRangeLess(size_t prop_id, double num_filter, bool include) const
{
    FilterIdRange empty_range;
    NumFilterKeyT num_key = formatNumericFilter(prop_id, num_filter, false);
    const std::vector<NumFilterKeyT>& num_key_set = num_key_sets_[prop_id];

    if (num_key_set.size() == 0)
    {
        return empty_range;
    }

    std::vector<NumFilterKeyT>::const_iterator it = include
        ? std::upper_bound(num_key_set.begin(), num_key_set.end(), num_key)
        : std::lower_bound(num_key_set.begin(), num_key_set.end(), num_key);
    if (it == num_key_set.begin() && (!include || *it > num_filter))
    {
        return empty_range;
    }
    if (it != num_key_set.begin())
    {
        --it;
    }
    NumIdMapT::const_iterator nit = num_filter_ids_[prop_id].find(*it);
    if (nit == num_filter_ids_[prop_id].end())
    {
        return empty_range;
    }

    empty_range.start = num_filter_ids_[prop_id].find(num_key_set[0])->second.start;
    empty_range.end = nit->second.end;

    LOG(INFO) << "numeric filter smaller map to key: " << *it << ", filter id range is: " << empty_range.start << "," << empty_range.end;

    return empty_range;
}

void FilterManager::setNumericAmp(const std::map<std::string, int32_t>& num_amp_map)
{
    num_amp_map_ = num_amp_map;
    num_amp_list_.resize(num_amp_map_.size(), 1);
    std::map<std::string, int32_t>::const_iterator mit = num_amp_map_.begin();
    while (mit != num_amp_map_.end())
    {
        size_t prop_id = getPropertyId(mit->first);
        if (prop_id != (size_t)-1)
        {
            num_amp_list_[prop_id] = mit->second;
        }
        ++mit;
    }
}

const std::map<std::string, int32_t>& FilterManager::getNumericAmp() const
{
    return num_amp_map_;
}

size_t FilterManager::getPropertyId(const std::string& property) const
{
    std::map<std::string, size_t>::const_iterator it = prop_id_map_.find(property);
    if (it == prop_id_map_.end())
    {
        return -1;
    }
    else
    {
        return it->second;
    }
}

size_t FilterManager::getAttrPropertyId() const
{
    if (attr_prop_list_.empty())
    {
        return -1;
    }
    else
    {
        return getPropertyId(attr_prop_list_[0]);
    }
}

size_t FilterManager::propertyCount() const
{
    return prop_id_map_.size();
}

izenelib::util::UString FilterManager::getPropFilterString(size_t prop_id, size_t filter_strid) const
{
    izenelib::util::UString result;
    if (prop_id >= prop_filter_str_list_.size())
        return result;
    // filter_strid start from 1. just like the document id.
    assert(filter_strid >= 1);
    if (filter_strid >= prop_filter_str_list_[prop_id].size())
        return result;
    return prop_filter_str_list_[prop_id][filter_strid];
}

size_t FilterManager::getMaxPropFilterStrId(size_t prop_id) const
{
    if (prop_id >= prop_filter_str_list_.size())
        return 0;
    return prop_filter_str_list_[prop_id].size() - 1;
}

void FilterManager::addUnchangedProperty(const std::string& property)
{
    if (rebuild_all_ || (groupManager_ && groupManager_->isRebuildProp_(property)))
    {
        // rebuild property always changed.
        return;
    }
    unchanged_prop_list_.insert(property);
}

void FilterManager::clearUnchangedProperties()
{
    unchanged_prop_list_.clear();
}

bool FilterManager::isUnchangedProperty(const std::string& property) const
{
    return unchanged_prop_list_.find(property) != unchanged_prop_list_.end();
}

void FilterManager::swapUnchangedFilter(FilterManager* old_filter)
{
    for (std::set<std::string>::const_iterator cit = unchanged_prop_list_.begin();
            cit != unchanged_prop_list_.end(); ++cit)
    {
        size_t prop_id = getPropertyId(*cit);
        if (prop_id == (size_t)-1)
        {
            LOG(ERROR) << "swap filter string id failed for non-exist property: " << *cit;
            continue;
        }
        str_filter_ids_[prop_id].swap(old_filter->str_filter_ids_[prop_id]);
        prop_filter_str_list_[prop_id].swap(old_filter->prop_filter_str_list_[prop_id]);
        LOG(INFO) << "property filter data swapped: " << *cit;
    }
}

void FilterManager::setRebuildFlag(const FilterManager* old_filter)
{
    rebuild_all_ = !old_filter || old_filter->rebuild_all_;
}

void FilterManager::clearRebuildFlag()
{
    rebuild_all_ = false;
}

}


text:
/*
	This file is part of cpp-ethereum.

	cpp-ethereum 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.

	cpp-ethereum 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 cpp-ethereum.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
 * @author Christian <c@ethdev.com>
 * @date 2015
 * Tests for high level features like import.
 */

#include <string>
#include <boost/test/unit_test.hpp>
#include <libsolidity/interface/Exceptions.h>
#include <libsolidity/interface/CompilerStack.h>

using namespace std;

namespace dev
{
namespace solidity
{
namespace test
{

BOOST_AUTO_TEST_SUITE(SolidityImports)

BOOST_AUTO_TEST_CASE(smoke_test)
{
	CompilerStack c;
	c.addSource("a", "contract C {}");
	BOOST_CHECK(c.compile());
}

BOOST_AUTO_TEST_CASE(regular_import)
{
	CompilerStack c;
	c.addSource("a", "contract C {}");
	c.addSource("b", "import \"a\"; contract D is C {}");
	BOOST_CHECK(c.compile());
}

BOOST_AUTO_TEST_CASE(import_does_not_clutter_importee)
{
	CompilerStack c;
	c.addSource("a", "contract C { D d; }");
	c.addSource("b", "import \"a\"; contract D is C {}");
	BOOST_CHECK(!c.compile());
}

BOOST_AUTO_TEST_CASE(import_is_transitive)
{
	CompilerStack c;
	c.addSource("a", "contract C { }");
	c.addSource("b", "import \"a\";");
	c.addSource("c", "import \"b\"; contract D is C {}");
	BOOST_CHECK(c.compile());
}

BOOST_AUTO_TEST_CASE(circular_import)
{
	CompilerStack c;
	c.addSource("a", "import \"b\"; contract C { D d; }");
	c.addSource("b", "import \"a\"; contract D { C c; }");
	BOOST_CHECK(c.compile());
}

BOOST_AUTO_TEST_CASE(relative_import)
{
	CompilerStack c;
	c.addSource("a", "import \"./dir/b\"; contract A is B {}");
	c.addSource("dir/b", "contract B {}");
	c.addSource("dir/c", "import \"../a\"; contract C is A {}");
	BOOST_CHECK(c.compile());
}

BOOST_AUTO_TEST_CASE(relative_import_multiplex)
{
	CompilerStack c;
	c.addSource("a", "contract A {}");
	c.addSource("dir/a/b/c", "import \"../../.././a\"; contract B is A {}");
	BOOST_CHECK(c.compile());
}

BOOST_AUTO_TEST_CASE(simple_alias)
{
	CompilerStack c;
	c.addSource("a", "contract A {}");
	c.addSource("dir/a/b/c", "import \"../../.././a\" as x; contract B is x.A { function() { x.A r = x.A(20); } }");
	BOOST_CHECK(c.compile());
}

BOOST_AUTO_TEST_CASE(library_name_clash)
{
	CompilerStack c;
	c.addSource("a", "library A {}");
	c.addSource("b", "library A {}");
	BOOST_CHECK(!c.compile());
}

BOOST_AUTO_TEST_CASE(library_name_clash_with_contract)
{
	CompilerStack c;
	c.addSource("a", "contract A {}");
	c.addSource("b", "library A {}");
	BOOST_CHECK(c.compile());
}

BOOST_AUTO_TEST_CASE(complex_import)
{
	CompilerStack c;
	c.addSource("a", "contract A {} contract B {} contract C { struct S { uint a; } }");
	c.addSource("b", "import \"a\" as x; import {B as b, C as c, C} from \"a\"; "
				"contract D is b { function f(c.S var1, x.C.S var2, C.S var3) internal {} }");
	BOOST_CHECK(c.compile());
}

BOOST_AUTO_TEST_CASE(name_clash_in_import)
{
	CompilerStack c;
	c.addSource("a", "contract A {}");
	c.addSource("b", "import \"a\"; contract A {} ");
	BOOST_CHECK(!c.compile());
	c.addSource("b", "import \"a\" as A; contract A {} ");
	BOOST_CHECK(!c.compile());
	c.addSource("b", "import {A as b} from \"a\"; contract b {} ");
	BOOST_CHECK(!c.compile());
	c.addSource("b", "import {A} from \"a\"; contract A {} ");
	BOOST_CHECK(!c.compile());
	c.addSource("b", "import {A} from \"a\"; contract B {} ");
	BOOST_CHECK(c.compile());
}

BOOST_AUTO_TEST_SUITE_END()

}
}
} // end namespaces

max_stars_repo_name: miohtama/solidity

max_issues_repo_name: miohtama/solidity

max_forks_repo_name: miohtama/solidity


text:
//-----------------------------------------------------------------------------
// Copyright (c) 2012-2018 Benjamin Buch
//
// https://github.com/bebuch/http
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
//-----------------------------------------------------------------------------
#include <http/websocket_server_request_handler.hpp>

#include <http/websocket_server_service.hpp>
#include <http/mime_types.hpp>
#include <http/reply.hpp>
#include <http/request.hpp>

#include <boost/uuid/sha1.hpp>
#include <boost/archive/iterators/insert_linebreaks.hpp>
#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/transform_width.hpp>
#include <boost/algorithm/string.hpp>

#include <algorithm>

namespace http::websocket::server{

	namespace impl{

		inline std::string base64_encoded(std::string const& text){
			using namespace boost::archive::iterators;
			using base64_text = insert_linebreaks<
				base64_from_binary< transform_width< char const*, 6, 8 > >,
				72 >;

			std::ostringstream os;
			std::copy(
				base64_text(text.c_str()),
				base64_text(text.c_str() + text.size()),
				std::ostream_iterator< char >(os)
			);
			return os.str();
		}

		std::string sha1_hash(std::string const& text){
			boost::uuids::detail::sha1 hasher;
			std::string hash(20, '\0');
			hasher.process_bytes(text.c_str(), text.size());
			unsigned int digest[5];
			hasher.get_digest(digest);
			for(int i = 0; i < 5; ++i){
				char const* tmp = reinterpret_cast< char* >(digest);
				hash[i * 4]     = tmp[i * 4 + 3];
				hash[i * 4 + 1] = tmp[i * 4 + 2];
				hash[i * 4 + 2] = tmp[i * 4 + 1];
				hash[i * 4 + 3] = tmp[i * 4];
			}
			return hash;
		}

	}

	bool request_handler::handle_request(
		http::server::connection_ptr const& connection_ptr,
		http::request const& req, http::reply& rep
	){
		// Request path must be absolute.
		if(req.uri.empty() || req.uri[0] != '/'){
			rep = http::reply::stock_reply(http::reply::bad_request);
			return false;
		}

		// Check for the required header field "Connection"
		auto connection_header = req.headers.find("Connection");
		if(connection_header == req.headers.end()){
			rep = http::reply::stock_reply(http::reply::bad_request);
			return false;
		}
		std::vector< std::string > tokens;
		boost::algorithm::split(tokens, connection_header->second,
			boost::algorithm::is_any_of(","));
		for(std::string& token: tokens) boost::algorithm::trim(token);
		if(find(tokens.begin(), tokens.end(), "Upgrade") == tokens.end()){
			rep = http::reply::stock_reply(http::reply::bad_request);
			return false;
		}

		// Check for the required header field "Upgrade"
		auto upgrade_header = req.headers.find("Upgrade");
		if(upgrade_header == req.headers.end()
			|| upgrade_header->second != "websocket"
		){
			rep = http::reply::stock_reply(http::reply::bad_request);
			return false;
		}

		// Check for correct protocol version.
		auto version = req.headers.find("Sec-WebSocket-Version");
		if(version == req.headers.end() || version->second != "13"){
			rep = http::reply::stock_reply(http::reply::bad_request);
			rep.headers.insert(std::make_pair("Sec-WebSocket-Version", "13"));
			return false;
		}

		// Check if a key is available.
		auto key = req.headers.find("Sec-WebSocket-Key");
		if(key == req.headers.end()){
			rep = http::reply::stock_reply(http::reply::bad_request);
			return false;
		}

		// Erase the beginning '/' from resource name
		std::string name = req.uri;
		name.erase(0, 1);

		// Cut parameters
		// TODO: Get parameter to the service
		auto parameter = name.find("?");
		if(parameter != std::string::npos){
			name = name.substr(0, parameter);
		}

		// Check if there is a websocket service with this name
		auto ws_service = services_.find(name);
		if(ws_service == services_.end()){
			rep = http::reply::stock_reply(http::reply::not_found);
			return false;
		}

		// Add the connection to websocket service
		ws_service->second.get().add_connection(connection_ptr);

		// Accept the protocol switching.
		rep = http::reply::stock_reply(http::reply::switching_protocols);
		rep.headers.insert(std::make_pair("Connection", "Upgrade"));
		rep.headers.insert(*upgrade_header);

		// Create and add the respond key.
		std::string magic_key = key->second
			+ "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
		rep.headers.insert(make_pair("Sec-WebSocket-Accept",
			impl::base64_encoded(impl::sha1_hash(magic_key)) + "="));

		return true;
	}

	void request_handler::shutdown(){
		for(auto& service: services_){
			service.second.get().shutdown(1001, "Server shutdown");
		}
	}

	bool request_handler::register_service(
		std::string const& name,
		service& reg
	){
		return services_.emplace(name, std::ref(reg)).second;
	}

	bool request_handler::shutdown_service(std::string const& name){
		auto iter = services_.find(name);

		if(iter == services_.end()) return false;

		iter->second.get().shutdown(1001, "Websocket shutdown");

		services_.erase(iter);
		return true;
	}

}

max_stars_repo_name: bebuch/http

max_issues_repo_name: bebuch/http

max_forks_repo_name: bebuch/http


text:
//==================================================================================================
/*!
  @file

  @copyright 2016 NumScale SAS

  Distributed under the Boost Software License, Version 1.0.
  (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
*/
//==================================================================================================
#ifndef BOOST_SIMD_FUNCTION_SUM_HPP_INCLUDED
#define BOOST_SIMD_FUNCTION_SUM_HPP_INCLUDED

#if defined(DOXYGEN_ONLY)
namespace boost { namespace simd
{

 /*!

    @ingroup group-reduction
    Function object implementing sum capabilities

    Returns the sum of the elements of the SIMD vector

    @par Semantic:

    For every parameter of pack type T

    @code
    scalar_of_t<T> r = sum(x);
    @endcode

    is similar to:

    @code
    scalar_of_t<T> r = Zero;
    for(result_type i = 0; i != T::static_size; ++i)
      r += x[i];
    @endcode

  **/
  scalar_of_t<Value> sum(Value const & v0);
} }
#endif

#include <boost/simd/function/scalar/sum.hpp>
#include <boost/simd/function/simd/sum.hpp>

#endif

max_stars_repo_name: xmar/pythran

max_issues_repo_name: xmar/pythran

max_forks_repo_name: xmar/pythran