text:
/*

Copyright (c) 2003, Arvid Norberg
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.
    * Neither the name of the author 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 THE COPYRIGHT OWNER 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.

*/

#ifndef TORRENT_PEER_CONNECTION_HPP_INCLUDED
#define TORRENT_PEER_CONNECTION_HPP_INCLUDED

#include <ctime>
#include <algorithm>
#include <vector>
#include <string>

#include "libtorrent/debug.hpp"

#ifdef _MSC_VER
#pragma warning(push, 1)
#endif

#include <boost/smart_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <boost/noncopyable.hpp>
#include <boost/array.hpp>
#include <boost/optional.hpp>
#include <boost/cstdint.hpp>
#include <boost/pool/pool.hpp>
#include <boost/aligned_storage.hpp>

#ifdef _MSC_VER
#pragma warning(pop)
#endif

#include "libtorrent/buffer.hpp"
#include "libtorrent/socket.hpp"
#include "libtorrent/peer_id.hpp"
#include "libtorrent/storage.hpp"
#include "libtorrent/stat.hpp"
#include "libtorrent/alert.hpp"
#include "libtorrent/torrent_handle.hpp"
#include "libtorrent/torrent.hpp"
#include "libtorrent/peer_request.hpp"
#include "libtorrent/piece_block_progress.hpp"
#include "libtorrent/config.hpp"
#include "libtorrent/session.hpp"
#include "libtorrent/bandwidth_limit.hpp"
#include "libtorrent/policy.hpp"
#include "libtorrent/socket_type.hpp"
#include "libtorrent/intrusive_ptr_base.hpp"
#include "libtorrent/assert.hpp"
#include "libtorrent/chained_buffer.hpp"
#include "libtorrent/disk_buffer_holder.hpp"
#include "libtorrent/bitfield.hpp"

#ifdef TORRENT_STATS
#include "libtorrent/aux_/session_impl.hpp"
#endif

namespace libtorrent
{
	class torrent;
	struct peer_plugin;

	namespace detail
	{
		struct session_impl;
	}

	struct pending_block
	{
		pending_block(piece_block const& b)
			: skipped(0), not_wanted(false), timed_out(false)
			, busy(false), block(b) {}

		// the number of times the request
		// has been skipped by out of order blocks
		boost::uint16_t skipped;

		// if any of these are set to true, this block
		// is not allocated
		// in the piece picker anymore, and open for
		// other peers to pick. This may be caused by
		// it either timing out or being received
		// unexpectedly from the peer
		bool not_wanted:1;
		bool timed_out:1;

		// the busy flag is set if the block was
		// requested from another peer when this
		// request was queued. We only allow a single
		// busy request at a time in each peer's queue
		bool busy:1;

		piece_block block;

		bool operator==(pending_block const& b)
		{
			return b.skipped == skipped && b.block == block
				&& b.not_wanted == not_wanted && b.timed_out == timed_out;
		}
	};

	struct has_block
	{
		has_block(piece_block const& b): block(b) {}
		piece_block const& block;
		bool operator()(pending_block const& pb) const
		{ return pb.block == block; }
	};

	class TORRENT_EXPORT peer_connection
		: public intrusive_ptr_base<peer_connection>
		, public boost::noncopyable
	{
	friend class invariant_access;
	public:

		enum channels
		{
			upload_channel,
			download_channel,
			num_channels
		};

		// this is the constructor where the we are the active part.
		// The peer_conenction should handshake and verify that the
		// other end has the correct id
		peer_connection(
			aux::session_impl& ses
			, boost::weak_ptr<torrent> t
			, boost::shared_ptr<socket_type> s
			, tcp::endpoint const& remote
			, policy::peer* peerinfo);

		// with this constructor we have been contacted and we still don't
		// know which torrent the connection belongs to
		peer_connection(
			aux::session_impl& ses
			, boost::shared_ptr<socket_type> s
			, tcp::endpoint const& remote
			, policy::peer* peerinfo);

		// this function is called after it has been constructed and properly
		// reference counted. It is safe to call self() in this function
		// and schedule events with references to itself (that is not safe to
		// do in the constructor).
		virtual void start();

		virtual ~peer_connection();

		void set_peer_info(policy::peer* pi)
		{
			TORRENT_ASSERT(m_peer_info == 0 || pi == 0 );
			m_peer_info = pi;
		}

		policy::peer* peer_info_struct() const
		{ return m_peer_info; }

		enum peer_speed_t { slow = 1, medium, fast };
		peer_speed_t peer_speed();

		void send_allowed_set();

#ifndef TORRENT_DISABLE_EXTENSIONS
		void add_extension(boost::shared_ptr<peer_plugin>);
#endif

		// this function is called once the torrent associated
		// with this peer connection has retrieved the meta-
		// data. If the torrent was spawned with metadata
		// this is called from the constructor.
		void init();

		// this is called when the metadata is retrieved
		// and the files has been checked
		virtual void on_metadata() {};

		void on_metadata_impl();

		void set_upload_limit(int limit);
		void set_download_limit(int limit);

		int upload_limit() const { return m_upload_limit; }
		int download_limit() const { return m_download_limit; }

		int prefer_whole_pieces() const
		{
			if (on_parole()) return 1;
			return m_prefer_whole_pieces;
		}

		bool on_parole() const
		{ return peer_info_struct() && peer_info_struct()->on_parole; }

		int picker_options() const;

		void prefer_whole_pieces(int num)
		{ m_prefer_whole_pieces = num; }

		bool request_large_blocks() const
		{ return m_request_large_blocks; }

		void request_large_blocks(bool b)
		{ m_request_large_blocks = b; }

		void set_endgame(bool b) { m_endgame_mode = b; }
		bool endgame() const { return m_endgame_mode; }

		bool no_download() const { return m_no_download; }
		void no_download(bool b) { m_no_download = b; }

		void set_priority(int p)
		{
			TORRENT_ASSERT(p > 0);
			TORRENT_ASSERT(m_priority <= 255);
			if (p > 255) p = 255;
			m_priority = p;
		}

		void fast_reconnect(bool r);
		bool fast_reconnect() const { return m_fast_reconnect; }

		// this adds an announcement in the announcement queue
		// it will let the peer know that we have the given piece
		void announce_piece(int index);

		// this will tell the peer to announce the given piece
		// and only allow it to request that piece
		void superseed_piece(int index);
		int superseed_piece() const { return m_superseed_piece; }

		// tells if this connection has data it want to send
		// and has enough upload bandwidth quota left to send it.
		bool can_write() const;
		bool can_read(char* state = 0) const;

		bool is_seed() const;

		void set_upload_only(bool u);
		bool upload_only() const { return m_upload_only; }

		// will send a keep-alive message to the peer
		void keep_alive();

		peer_id const& pid() const { return m_peer_id; }
		void set_pid(const peer_id& peer_id) { m_peer_id = peer_id; }
		bool has_piece(int i) const;

		std::vector<pending_block> const& download_queue() const;
		std::vector<pending_block> const& request_queue() const;
		std::vector<peer_request> const& upload_queue() const;

		// estimate of how long it will take until we have
		// received all piece requests that we have sent
		// if extra_bytes is specified, it will include those
		// bytes as if they've been requested
		time_duration download_queue_time(int extra_bytes = 0) const;

		bool is_interesting() const { return m_interesting; }
		bool is_choked() const { return m_choked; }

		bool is_peer_interested() const { return m_peer_interested; }
		bool has_peer_choked() const { return m_peer_choked; }

		void update_interest();

		virtual void get_peer_info(peer_info& p) const;

		// returns the torrent this connection is a part of
		// may be zero if the connection is an incoming connection
		// and it hasn't received enough information to determine
		// which torrent it should be associated with
		boost::weak_ptr<torrent> associated_torrent() const
		{ return m_torrent; }

		const stat& statistics() const { return m_statistics; }
		void add_stat(size_type downloaded, size_type uploaded);

		// is called once every second by the main loop
		void second_tick(int tick_interval_ms);

		void timeout_requests();

		boost::shared_ptr<socket_type> get_socket() const { return m_socket; }
		tcp::endpoint const& remote() const { return m_remote; }

		bitfield const& get_bitfield() const;
		std::vector<int> const& allowed_fast();
		std::vector<int> const& suggested_pieces() const { return m_suggested_pieces; }

		ptime connected_time() const { return m_connect; }
		ptime last_received() const { return m_last_receive; }

		void on_timeout();
		// this will cause this peer_connection to be disconnected.
		void disconnect(error_code const& ec, int error = 0);
		bool is_disconnecting() const { return m_disconnecting; }

		// this is called when the connection attempt has succeeded
		// and the peer_connection is supposed to set m_connecting
		// to false, and stop monitor writability
		void on_connection_complete(error_code const& e);

		// returns true if this connection is still waiting to
		// finish the connection attempt
		bool is_connecting() const { return m_connecting; }

		// returns true if the socket of this peer hasn't been
		// attempted to connect yet (i.e. it's queued for
		// connection attempt).
		bool is_queued() const { return m_queued; }

		// called when it's time for this peer_conncetion to actually
		// initiate the tcp connection. This may be postponed until
		// the library isn't using up the limitation of half-open
		// tcp connections.
		void on_connect(int ticket);

		// This is called for every peer right after the upload
		// bandwidth has been distributed among them
		// It will reset the used bandwidth to 0.
		void reset_upload_quota();

		// free upload.
		size_type total_free_upload() const;
		void add_free_upload(size_type free_upload);

		// trust management.
		void received_valid_data(int index);
		void received_invalid_data(int index);

		size_type share_diff() const;

		// a connection is local if it was initiated by us.
		// if it was an incoming connection, it is remote
		bool is_local() const { return m_active; }

		bool on_local_network() const;
		bool ignore_bandwidth_limits() const
		{ return m_ignore_bandwidth_limits; }
		void ignore_bandwidth_limits(bool i)
		{ m_ignore_bandwidth_limits = i; }

		bool ignore_unchoke_slots() const;
		void ignore_unchoke_slots(bool i)
		{ m_ignore_unchoke_slots = i; }

		bool failed() const { return m_failed; }

		int desired_queue_size() const
		{
			// this peer is in end-game mode we only want
			// one outstanding request
			return m_endgame_mode ? 1: m_desired_queue_size;
		}

		// compares this connection against the given connection
		// for which one is more eligible for an unchoke.
		// returns true if this is more eligible
		bool unchoke_compare(boost::intrusive_ptr<peer_connection const> const& p) const;
		bool upload_rate_compare(peer_connection const* p) const;

		// resets the byte counters that are used to measure
		// the number of bytes transferred within unchoke cycles
		void reset_choke_counters();

		// if this peer connection is useless (neither party is
		// interested in the other), disconnect it
		void disconnect_if_redundant();

#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
		boost::shared_ptr<logger> m_logger;
#endif

		// the message handlers are called
		// each time a recv() returns some new
		// data, the last time it will be called
		// is when the entire packet has been
		// received, then it will no longer
		// be called. i.e. most handlers need
		// to check how much of the packet they
		// have received before any processing
		void incoming_keepalive();
		void incoming_choke();
		void incoming_unchoke();
		void incoming_interested();
		void incoming_not_interested();
		void incoming_have(int piece_index);
		void incoming_bitfield(bitfield const& bits);
		void incoming_request(peer_request const& r);
		void incoming_piece(peer_request const& p, disk_buffer_holder& data);
		void incoming_piece(peer_request const& p, char const* data);
		void incoming_piece_fragment(int bytes);
		void start_receive_piece(peer_request const& r);
		void incoming_cancel(peer_request const& r);

		void incoming_dht_port(int listen_port);

		void incoming_reject_request(peer_request const& r);
		void incoming_have_all();
		void incoming_have_none();
		void incoming_allowed_fast(int index);
		void incoming_suggest(int index);

		// the following functions appends messages
		// to the send buffer
		bool send_choke();
		bool send_unchoke();
		void send_interested();
		void send_not_interested();

		void snub_peer();

		bool can_request_time_critical() const;

		void make_time_critical(piece_block const& block);

		// adds a block to the request queue
		// returns true if successful, false otherwise
		enum flags_t { req_time_critical = 1, req_busy = 2 };
		bool add_request(piece_block const& b, int flags = 0);

		// clears the request queue and sends cancels for all messages
		// in the download queue
		void cancel_all_requests();

		// removes a block from the request queue or download queue
		// sends a cancel message if appropriate
		// refills the request queue, and possibly ignoring pieces requested
		// by peers in the ignore list (to avoid recursion)
		void cancel_request(piece_block const& b);
		void send_block_requests();

		int bandwidth_throttle(int channel) const
		{ return m_bandwidth_channel[channel].throttle(); }

		void assign_bandwidth(int channel, int amount);

#ifdef TORRENT_DEBUG
		void check_invariant() const;
		ptime m_last_choke;
#endif

		// is true until we can be sure that the other end
		// speaks our protocol (be it bittorrent or http).
		virtual bool in_handshake() const = 0;

		// returns the block currently being
		// downloaded. And the progress of that
		// block. If the peer isn't downloading
		// a piece for the moment, the boost::optional
		// will be invalid.
		virtual boost::optional<piece_block_progress>
		downloading_piece_progress() const
		{
#ifdef TORRENT_VERBOSE_LOGGING
			(*m_logger) << "downloading_piece_progress() dispatched to the base class!\n";
#endif
			return boost::optional<piece_block_progress>();
		}

		// these functions are virtual to let bt_peer_connection hook into them
		// and encrypt the content
		enum message_type_flags { message_type_request = 1 };
		virtual void send_buffer(char const* begin, int size, int flags = 0);
		virtual buffer::interval allocate_send_buffer(int size);
		virtual void setup_send();

		template <class Destructor>
		void append_send_buffer(char* buffer, int size, Destructor const& destructor)
		{
#if TORRENT_DISK_STATS
			if (m_ses.m_disk_thread.is_disk_buffer(buffer))
				m_ses.m_disk_thread.rename_buffer(buffer, "queued send buffer");
#endif
			m_send_buffer.append_buffer(buffer, size, size, destructor);
#if defined TORRENT_STATS && defined TORRENT_DISK_STATS
			m_ses.m_buffer_usage_logger << log_time() << " append_send_buffer: " << size << std::endl;
			m_ses.log_buffer_usage();
#endif
		}

		virtual void append_const_send_buffer(char const* buffer, int size);

#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
		void set_country(char const* c)
		{
			TORRENT_ASSERT(strlen(c) == 2);
			m_country[0] = c[0];
			m_country[1] = c[1];
		}
		bool has_country() const { return m_country[0] != 0; }
#endif

		int send_buffer_size() const
		{ return m_send_buffer.size(); }

		int send_buffer_capacity() const
		{ return m_send_buffer.capacity(); }

		int packet_size() const { return m_packet_size; }

		bool packet_finished() const
		{ return m_packet_size <= m_recv_pos; }

#ifdef TORRENT_DEBUG
		bool piece_failed;
#endif

		// upload and download channel state
		// enum from peer_info::bw_state
		char m_channel_state[2];

		size_type uploaded_since_unchoke() const
		{ return m_statistics.total_payload_upload() - m_uploaded_at_last_unchoke; }

		size_type downloaded_since_unchoke() const
		{ return m_statistics.total_payload_download() - m_downloaded_at_last_unchoke; }

		enum sync_t { read_async, read_sync };
		void setup_receive(sync_t sync = read_sync);

	protected:

		size_t try_read(sync_t s, error_code& ec);

		virtual void get_specific_peer_info(peer_info& p) const = 0;

		virtual void write_choke() = 0;
		virtual void write_unchoke() = 0;
		virtual void write_interested() = 0;
		virtual void write_not_interested() = 0;
		virtual void write_request(peer_request const& r) = 0;
		virtual void write_cancel(peer_request const& r) = 0;
		virtual void write_have(int index) = 0;
		virtual void write_keepalive() = 0;
		virtual void write_piece(peer_request const& r, disk_buffer_holder& buffer) = 0;

		virtual void write_reject_request(peer_request const& r) = 0;
		virtual void write_allow_fast(int piece) = 0;

		virtual void on_connected() = 0;
		virtual void on_tick() {}

		virtual void on_receive(error_code const& error
			, std::size_t bytes_transferred) = 0;
		virtual void on_sent(error_code const& error
			, std::size_t bytes_transferred) = 0;

#ifndef TORRENT_DISABLE_ENCRYPTION
		buffer::interval wr_recv_buffer()
		{
			TORRENT_ASSERT(!m_disk_recv_buffer);
			TORRENT_ASSERT(m_disk_recv_buffer_size == 0);
			if (m_recv_buffer.empty()) return buffer::interval(0,0);
			return buffer::interval(&m_recv_buffer[0]
				, &m_recv_buffer[0] + m_recv_pos);
		}

		std::pair<buffer::interval, buffer::interval> wr_recv_buffers(int bytes);
#endif

		buffer::const_interval receive_buffer() const
		{
			if (m_recv_buffer.empty()) return buffer::const_interval(0,0);
			return buffer::const_interval(&m_recv_buffer[0]
				, &m_recv_buffer[0] + m_recv_pos);
		}

		bool allocate_disk_receive_buffer(int disk_buffer_size);
		char* release_disk_receive_buffer();
		bool has_disk_receive_buffer() const { return m_disk_recv_buffer; }
		void cut_receive_buffer(int size, int packet_size);
		void reset_recv_buffer(int packet_size);
		void set_soft_packet_size(int size) { m_soft_packet_size = size; }

		void attach_to_torrent(sha1_hash const& ih);

		bool verify_piece(peer_request const& p) const;

		// the bandwidth channels, upload and download
		// keeps track of the current quotas
		bandwidth_channel m_bandwidth_channel[num_channels];

		// number of bytes this peer can send and receive
		int m_quota[2];

		// statistics about upload and download speeds
		// and total amount of uploads and downloads for
		// this peer
		stat m_statistics;

		// a back reference to the session
		// the peer belongs to.
		aux::session_impl& m_ses;

		// called from the main loop when this connection has any
		// work to do.
		void on_send_data(error_code const& error
			, std::size_t bytes_transferred);
		void on_receive_data(error_code const& error
			, std::size_t bytes_transferred);
		void on_receive_data_nolock(error_code const& error
			, std::size_t bytes_transferred);

		// this is the limit on the number of outstanding requests
		// we have to this peer. This is initialized to the settings
		// in the session_settings structure. But it may be lowered
		// if the peer is known to require a smaller limit (like BitComet).
		// or if the extended handshake sets a limit.
		// web seeds also has a limit on the queue size.
		int m_max_out_request_queue;

		void set_timeout(int s) { m_timeout = s; }

#ifndef TORRENT_DISABLE_EXTENSIONS
		typedef std::list<boost::shared_ptr<peer_plugin> > extension_list_t;
		extension_list_t m_extensions;
#endif

#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
		// in case the session settings is set
		// to resolve countries, this is set to
		// the two character country code this
		// peer resides in.
		char m_country[2];
#endif

#ifdef TORRENT_DEBUG
		boost::intrusive_ptr<peer_connection> self()
		{
			TORRENT_ASSERT(!m_in_constructor);
			return intrusive_ptr_base<peer_connection>::self();
		}
#endif

	private:

		void fill_send_buffer();
		void on_disk_read_complete(int ret, disk_io_job const& j, peer_request r);
		void on_disk_write_complete(int ret, disk_io_job const& j
			, peer_request r, boost::shared_ptr<torrent> t);
		void request_upload_bandwidth(
			bandwidth_channel* bwc1
			, bandwidth_channel* bwc2 = 0
			, bandwidth_channel* bwc3 = 0
			, bandwidth_channel* bwc4 = 0);
		void request_download_bandwidth(
			bandwidth_channel* bwc1
			, bandwidth_channel* bwc2 = 0
			, bandwidth_channel* bwc3 = 0
			, bandwidth_channel* bwc4 = 0);

		// keep the io_service running as long as we
		// have peer connections
		io_service::work m_work;

		// the time when we last got a part of a
		// piece packet from this peer
		ptime m_last_piece;
		// the time we sent a request to
		// this peer the last time
		ptime m_last_request;
		// the time we received the last
		// piece request from the peer
		ptime m_last_incoming_request;
		// the time when we unchoked this peer
		ptime m_last_unchoke;

		// timeouts
		ptime m_last_receive;
		ptime m_last_sent;

		// the time when the first entry in the
		// request queue was requested, increased
		// for each entry that is popped from the
		// download queue. Used for request timeout
		ptime m_requested;

		// if the timeout is extended for the outstanding
		// requests, this is the number of seconds it was
		// extended.
		int m_timeout_extend;

		// a timestamp when the remote download rate
		// was last updated
		ptime m_remote_dl_update;

		// the time when async_connect was called
		// or when the incoming connection was established
		ptime m_connect;

		// the time when this peer sent us a not_interested message
		// the last time.
		ptime m_became_uninterested;

		// the time when we sent a not_interested message to
		// this peer the last time.
		ptime m_became_uninteresting;

		// the amount of data this peer has been given
		// as free upload. This is distributed from
		// peers from which we get free download
		// this will be negative on a peer from which
		// we get free download, and positive on peers
		// that we give the free upload, to keep the balance.
		size_type m_free_upload;

		// the total payload download bytes
		// at the last unchoke cycle. This is used to
		// measure the number of bytes transferred during
		// an unchoke cycle, to unchoke peers the more bytes
		// they sent us
		size_type m_downloaded_at_last_unchoke;
		size_type m_uploaded_at_last_unchoke;

#ifndef TORRENT_DISABLE_GEO_IP
		std::string m_inet_as_name;
#endif

		buffer m_recv_buffer;

		// if this peer is receiving a piece, this
		// points to a disk buffer that the data is
		// read into. This eliminates a memcopy from
		// the receive buffer into the disk buffer
		disk_buffer_holder m_disk_recv_buffer;

		chained_buffer m_send_buffer;

		boost::shared_ptr<socket_type> m_socket;
		// this is the peer we're actually talking to
		// it may not necessarily be the peer we're
		// connected to, in case we use a proxy
		tcp::endpoint m_remote;

		// this is the torrent this connection is
		// associated with. If the connection is an
		// incoming connection, this is set to zero
		// until the info_hash is received. Then it's
		// set to the torrent it belongs to.
		boost::weak_ptr<torrent> m_torrent;

		// remote peer's id
		peer_id m_peer_id;

		// the pieces the other end have
		bitfield m_have_piece;

		// the queue of requests we have got
		// from this peer
		std::vector<peer_request> m_requests;

		// the blocks we have reserved in the piece
		// picker and will request from this peer.
		std::vector<pending_block> m_request_queue;

		// the queue of blocks we have requested
		// from this peer
		std::vector<pending_block> m_download_queue;

		// the pieces we will send to the peer
		// if requested (regardless of choke state)
		std::vector<int> m_accept_fast;

		// the pieces the peer will send us if
		// requested (regardless of choke state)
		std::vector<int> m_allowed_fast;

		// pieces that has been suggested to be
		// downloaded from this peer
		std::vector<int> m_suggested_pieces;

		// a list of byte offsets inside the send buffer
		// the piece requests
		std::vector<int> m_requests_in_buffer;

		// the block we're currently receiving. Or
		// (-1, -1) if we're not receiving one
		piece_block m_receiving_block;

		// the number of bytes that the other
		// end has to send us in order to respond
		// to all outstanding piece requests we
		// have sent to it
		int m_outstanding_bytes;

		// the number of outstanding bytes expected
		// to be received by extensions
		int m_extension_outstanding_bytes;

		// the number of time critical requests
		// queued up in the m_request_queue that
		// soon will be committed to the download
		// queue. This is included in download_queue_time()
		// so that it can be used while adding more
		// requests and take the previous requests
		// into account without submitting it all
		// immediately
		int m_queued_time_critical;

		// the number of pieces this peer
		// has. Must be the same as
		// std::count(m_have_piece.begin(),
		// m_have_piece.end(), true)
		int m_num_pieces;

		// the timeout in seconds
		int m_timeout;

		// the size (in bytes) of the bittorrent message
		// we're currently receiving
		int m_packet_size;

		// some messages needs to be read from the socket
		// buffer in multiple stages. This soft packet
		// size limits the read size between message handler
		// dispatch. Ignored when set to 0
		int m_soft_packet_size;

		// the number of bytes of the bittorrent payload
		// we've received so far
		int m_recv_pos;

		int m_disk_recv_buffer_size;

		// the number of bytes we are currently reading
		// from disk, that will be added to the send
		// buffer as soon as they complete
		int m_reading_bytes;

		// the number of invalid piece-requests
		// we have got from this peer. If the request
		// queue gets empty, and there have been
		// invalid requests, we can assume the
		// peer is waiting for those pieces.
		// we can then clear its download queue
		// by sending choke, unchoke.
		int m_num_invalid_requests;

		// this is the priority with which this peer gets
		// download bandwidth quota assigned to it.
		int m_priority;

		int m_upload_limit;
		int m_download_limit;

		// this peer's peer info struct. This may
		// be 0, in case the connection is incoming
		// and hasn't been added to a torrent yet.
		policy::peer* m_peer_info;

		// this is a measurement of how fast the peer
		// it allows some variance without changing
		// back and forth between states
		peer_speed_t m_speed;

		// the ticket id from the connection queue.
		// This is used to identify the connection
		// so that it can be removed from the queue
		// once the connection completes
		int m_connection_ticket;

		// if this is -1, superseeding is not active. If it is >= 0
		// this is the piece that is available to this peer. Only
		// this piece can be downloaded from us by this peer.
		// This will remain the current piece for this peer until
		// another peer sends us a have message for this piece
		int m_superseed_piece;

		// bytes downloaded since last second
		// timer timeout; used for determining
		// approx download rate
		int m_remote_bytes_dled;

		// approximate peer download rate
		int m_remote_dl_rate;

		// the number of bytes send to the disk-io
		// thread that hasn't yet been completely written.
		int m_outstanding_writing_bytes;

		// max transfer rates seen on this peer
		int m_download_rate_peak;
		int m_upload_rate_peak;

		// estimated round trip time to this peer
		// based on the time from when async_connect
		// was called to when on_connection_complete
		// was called. The rtt is specified in milliseconds
		boost::uint16_t m_rtt;

		// if set to non-zero, this peer will always prefer
		// to request entire n pieces, rather than blocks.
		// where n is the value of this variable.
		// if it is 0, the download rate limit setting
		// will be used to determine if whole pieces
		// are preferred.
		boost::uint8_t m_prefer_whole_pieces;

		// the number of request we should queue up
		// at the remote end.
		boost::uint8_t m_desired_queue_size;

		// the number of piece requests we have rejected
		// in a row because the peer is choked. This is
		// used to re-send the choked message in case the
		// other end keeps requesting pieces while being
		// choked, and eventuelly disconnect if it keeps
		// requesting too many pieces while being choked
		boost::uint8_t m_choke_rejects;

		// counts the number of recursive calls to on_receive_data
		// used to limit recursion
		boost::uint8_t m_read_recurse:5;

		// if this is true, the disconnection
		// timestamp is not updated when the connection
		// is closed. This means the time until we can
		// reconnect to this peer is shorter, and likely
		// immediate.
		bool m_fast_reconnect:1;

		// is true if it was we that connected to the peer
		// and false if we got an incoming connection
		// could be considered: true = local, false = remote
		bool m_active:1;

		// other side says that it's interested in downloading
		// from us.
		bool m_peer_interested:1;

		// the other side has told us that it won't send anymore
		// data to us for a while
		bool m_peer_choked:1;

		// the peer has pieces we are interested in
		bool m_interesting:1;

		// we have choked the upload to the peer
		bool m_choked:1;

		// this is set to true if the connection timed
		// out or closed the connection. In that
		// case we will not try to reconnect to
		// this peer
		bool m_failed:1;

		// if this is set to true, the peer will not
		// request bandwidth from the limiter, but instead
		// just send and receive as much as possible.
		bool m_ignore_bandwidth_limits:1;

		// set to true if this peer controls its unchoke
		// state individually, regardless of the global
		// unchoker
		bool m_ignore_unchoke_slots:1;

		// this is set to true when a have_all
		// message is received. This information
		// is used to fill the bitmask in init()
		bool m_have_all:1;

		// this is true if this connection has been added
		// to the list of connections that will be closed.
		bool m_disconnecting:1;

		// this is true until this socket has become
		// writable for the first time (i.e. the
		// connection completed). While connecting
		// the timeout will not be triggered. This is
		// because windows XP SP2 may delay connection
		// attempts, which means that the connection
		// may not even have been attempted when the
		// time out is reached.
		bool m_connecting:1;

		// This is true until connect is called on the
		// peer_connection's socket. It is false on incoming
		// connections.
		bool m_queued:1;

		// if this is true, the blocks picked by the piece
		// picker will be merged before passed to the
		// request function. i.e. subsequent blocks are
		// merged into larger blocks. This is used by
		// the http-downloader, to request whole pieces
		// at a time.
		bool m_request_large_blocks:1;

		// set to true when this peer is only uploading
		bool m_upload_only:1;

		// set to true when a piece request times out. The
		// result is that the desired pending queue size
		// is set to 1
		bool m_snubbed:1;

		// this is set to true once the bitfield is received
		bool m_bitfield_received:1;

		// if this is set to true, the client will not
		// pick any pieces from this peer
		bool m_no_download:1;

		// this is set to true if the last time we tried to
		// pick a piece to download, we could only find
		// blocks that were already requested from other
		// peers. In this case, we should not try to pick
		// another piece until the last one we requested is done
		bool m_endgame_mode:1;

		template <std::size_t Size>
		struct handler_storage
		{
#ifdef TORRENT_DEBUG
			handler_storage()
			  : used(false)
			{}

			bool used;
#endif
			boost::aligned_storage<Size> bytes;
		};

		handler_storage<TORRENT_READ_HANDLER_MAX_SIZE> m_read_handler_storage;
		handler_storage<TORRENT_WRITE_HANDLER_MAX_SIZE> m_write_handler_storage;

		template <class Handler, std::size_t Size>
		struct allocating_handler
		{
			allocating_handler(
				Handler const& h, handler_storage<Size>& s
			)
			  : handler(h)
			  , storage(s)
			{}

			template <class A0>
			void operator()(A0 const& a0) const
			{
				handler(a0);
			}

			template <class A0, class A1>
			void operator()(A0 const& a0, A1 const& a1) const
			{
				handler(a0, a1);
			}

			template <class A0, class A1, class A2>
			void operator()(A0 const& a0, A1 const& a1, A2 const& a2) const
			{
				handler(a0, a1, a2);
			}

			friend void* asio_handler_allocate(
			    std::size_t size, allocating_handler<Handler, Size>* ctx)
			{
				TORRENT_ASSERT(size <= Size);
#ifdef TORRENT_DEBUG
				TORRENT_ASSERT(!ctx->storage.used);
				ctx->storage.used = true;
#endif
				return &ctx->storage.bytes;
			}

			friend void asio_handler_deallocate(
				void*, std::size_t, allocating_handler<Handler, Size>* ctx)
			{
#ifdef TORRENT_DEBUG
				ctx->storage.used = false;
#endif
			}

			Handler handler;
			handler_storage<Size>& storage;
		};

		template <class Handler>
		allocating_handler<Handler, TORRENT_READ_HANDLER_MAX_SIZE>
			make_read_handler(Handler const& handler)
		{
			return allocating_handler<Handler, TORRENT_READ_HANDLER_MAX_SIZE>(
				handler, m_read_handler_storage
			);
		}

		template <class Handler>
		allocating_handler<Handler, TORRENT_WRITE_HANDLER_MAX_SIZE>
			make_write_handler(Handler const& handler)
		{
			return allocating_handler<Handler, TORRENT_WRITE_HANDLER_MAX_SIZE>(
				handler, m_write_handler_storage
			);
		}

#ifdef TORRENT_DEBUG
	public:
		bool m_in_constructor:1;
		bool m_disconnect_started:1;
		bool m_initialized:1;
		int m_received_in_piece;
#endif
	};
}

#endif // TORRENT_PEER_CONNECTION_HPP_INCLUDED

max_stars_repo_name: CarysT/medusa

max_issues_repo_name: CarysT/medusa

max_forks_repo_name: CarysT/medusa


text:
#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <string>
#include <thread>
#include <vector>
using namespace std;

#include "ctrlclaw.hpp"
#include "log.hpp"
#include "parrotword.hpp"
#include "textpump.hpp"

vector<string> parseUTF8(const string &words);

void parseBytes(const vector<string> &Bytes, const string &text);

static string longTxt(
    u8"它是由从地方到全球范围内几百万个私人的、学术界的、"
    u8"企业的和政府的网络所构成，通过电子，"
    u8"企业的和政府的网络所构成，通过<>"
    u8"无线和光纤网络技术等等一系列广泛的技术联系在一起。"
    u8"这种将计算机网络互相联接在一起的方法可称作“网络互联”，"
    u8"在这基础上发展出覆盖全世界的全球性互联网络称互联网，"
    u8"即是互相连接一起的网络。");

static string shortTxt(
    u8"人造卫星能够成功执行预定任务，单凭卫星本身是不行的，"
    u8"而需要完整的卫星工程系统");

/*
int main(int ac, char *av[]) {
  ParrotWord parrot("./db/ostrich/cn");
  if (parrot.loadMaster(false) == false) {
    return 0;
  }
  auto learnParrot = [&](string wordStr, vector<string> word) {
    parrot.learn(word, wordStr);
  };

  CtrlClaw claw;
  claw.claw(longTxt);
  claw.eachSentence(learnParrot);

  parrot.unloadMaster();
  return 0;
}
*/

#include <boost/foreach.hpp>
#include <boost/optional.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
namespace pt = boost::property_tree;

int main(int ac, char *av[]) {
  CtrlClaw claw;
  ParrotWord parrot("./db/ostrich");
  if (parrot.loadMaster() == false) {
    return 0;
  }
  auto clawText = [&](const pt::ptree &task, string &content,string &ws) {
    std::stringstream ssTask;
    pt::write_json(ssTask, task);
    DUMP_VAR(ssTask.str());
    // DUMP_VAR(content);
    auto langOpt = task.get_optional<string>("lang");
    if(langOpt) {
        auto lang = langOpt.get();
        claw.claw(content);
          auto learnParrot = [&](string wordStr, vector<string> word) {
            parrot.learn(word, wordStr,lang);
          };
        claw.eachSentence(learnParrot);
    }
    parrot.commitArticle(task,ws);
  };
  TextPump txtPump("https://www.wator.xyz/wai/text/train/parrot", "tain.one");
  txtPump.eachTextFromMaster(clawText);
  parrot.unloadMaster();
  return 0;
}

ext: cpp


text:
/* Copyright (C) 2014 InfiniDB, Inc.

   This program 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; version 2 of
   the License.

   This program 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 this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301, USA. */

/*******************************************************************************
* $Id$
*
*******************************************************************************/

/*
 * we_respreadthread.cpp
 *
 *  Created on: Oct 18, 2011
 *      Author: bpaul
 */

#include <boost/thread/condition.hpp>
#include <boost/scoped_array.hpp>
#include <boost/thread.hpp>
using namespace boost;

#include "we_sdhandler.h"
#include "we_respreadthread.h"

namespace WriteEngine
{

WERespReadThread::WERespReadThread(WESDHandler& aSdh):
    fSdh(aSdh)
{
    // ctor
}
WERespReadThread::WERespReadThread(const WERespReadThread& rhs):
    fSdh(rhs.fSdh)
{
    // copy ctor
}
WERespReadThread::~WERespReadThread()
{
    // dtor
}

void WERespReadThread::operator()()
{
    //call datahandler checkForAllPmMsgs()
    fSdh.checkForRespMsgs();
}

} /* namespace WriteEngine */

max_stars_repo_name: zettadb/zettalib

max_issues_repo_name: zettadb/zettalib

max_forks_repo_name: zettadb/zettalib


text:
#ifndef RICPAD_SOLVER
#define RICPAD_SOLVER

#include <iostream>
#include <vector>
#include <math.h>

#include <Eigen/Dense>

#include <differentiate.hpp>

using std::cout;
using std::endl;

namespace solver {
template<typename num_t>
class Solver {
    private:
        // Function
        const std::function<num_t(num_t)> f_;
        // Derivative of the function
        const std::function<num_t(num_t)> df_;
        // Step size
        num_t h_;
        // Tolerance of the result
        num_t tol_;

    public:
        // Construct a Solver object from the function and its derivative
        Solver(
            const std::function<num_t(num_t)> f,
            const std::function<num_t(num_t)> df
            ) :

            f_(f), df_(df)
            {
                h_   = std::sqrt(std::numeric_limits<num_t>::epsilon());
                tol_ = std::sqrt(h_);
            }

        // Solve f = 0 for the initial value of x0
        num_t solve(num_t x0) {
            num_t desv, fx, dfx, x = x0, xold;

            desv = 2*tol_;

            while ( desv > tol_ ) {
                fx  = f_(x);
                dfx = df_(x);

                xold = x;
                x = x - fx/dfx;

                std::cout << x << "\n";

                desv = std::abs(x - xold);
            }

            return x;
        }
};

// Solver for N equations using a real type R
template <
    typename C, // complex number type
    typename R, // real number type (for tolerance parameters, etc.)
    int N       // number of equations and unknowns
>
class Solver2 {
    private:
        // Vector with the functions
        const std::vector<std::function<C(Eigen::Matrix<C,N,1>&)>> f_;
        //Eigen::Matrix<C,N,1> x0;
        // Accepted difference between two iterations
        R tol_;
        // Numerical value for the differentiation step (only relevant when
        // using numerical differentiation)
        R h_;
        // Maximum number of iterations
        int maxiter_ = 100;

    public:
        //----------------------------------------------------------------------
        // Construct a Solver2 object from a vector of functions.
        // Differentiation is performed numerically.
        Solver2(
            const std::vector<std::function<C(Eigen::Matrix<C,N,1>&)>> &f
            ) :

            f_(f) {
                //h_   = std::sqrt(std::numeric_limits<R>::epsilon());
                h_   = 1e-12;
                tol_ = 1e-8;
            };

        //----------------------------------------------------------------------
        // Setters
        void set_h(R h) {h_ = h;};
        void set_tol(R tol) {tol_ = tol;};
        void set_maxiter(int maxiter) {maxiter_ = maxiter;};

        //----------------------------------------------------------------------
        // Solve for f using x0 as initial value
        Eigen::Matrix<C,N,1> solve(
            Eigen::Matrix<C,N,1> x0
            )
        {

            std::cout << "h = " << h_ << "\n"
                << "tol = " << tol_ << "\n\n";

            using solver::differentiate;
            Eigen::Matrix<C, N, N> jacobian, inv_jacobian;
            Eigen::Matrix<C, N, 1> x(x0), xold;
            R desv = tol_ + 1;

            int niter = 0;

            C val;

            while ( desv > tol_ ) {
                for ( int i = 0; i < x.size(); i++ ) {
                    for ( int j = 0; j < x.size(); j++ ) {
                        val = differentiate<C, N>(f_[i], x, j, h_);
                        jacobian(i, j) = std::move(val);
                    }
                }

                inv_jacobian = jacobian.inverse();

                Eigen::Matrix<C, N, 1> F;

                for ( int i = 0; i < N; i++ ) F(i) = f_[i](x);
                /*
                for ( int i = 0; i < N; i++ ) {
                    std::cout << std::setprecision(15) << x(i) << " ";
                }
                std::cout << "\n";
                */
                xold = x;
                x = x - inv_jacobian * F;

                desv = (x - xold).norm();
                std::cout << "desv = " << desv << "\n";

                if ( niter++ > maxiter_ ) {
                    std::cout << "Maximum number of iterations reached.\n";
                    return x;
                }
            }

            return x;
        };

        /* Construct a Solver2 object from a function and its derivative
        Solver2(
            const std::function<R(R)> f,
            const std::function<R(R)> df
            )
        */
};

}; // namespace solver

#endif

ext: hpp

max_stars_repo_name: javierelpianista/solver

max_issues_repo_name: javierelpianista/solver

max_forks_repo_name: javierelpianista/solver


text:
// Copyright (c) 2017-2019 The Multiverse developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "walletdb.h"
#include "leveldbeng.h"

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

using namespace std;
using namespace walleve;
using namespace multiverse::storage;

//////////////////////////////
// CWalletAddrDB

bool CWalletAddrDB::Initialize(const boost::filesystem::path& pathWallet)
{
    CLevelDBArguments args;
    args.path = (pathWallet / "addr").string();
    args.syncwrite = true;
    args.files = 8;
    args.cache = 1 << 20;

    CLevelDBEngine *engine = new CLevelDBEngine(args);

    if (!Open(engine))
    {
        delete engine;
        return false;
    }

    return true;
}

void CWalletAddrDB::Deinitialize()
{
    Close();
}

bool CWalletAddrDB::UpdateKey(const crypto::CPubKey& pubkey,int version,const crypto::CCryptoCipher& cipher)
{
    vector<unsigned char> vch;
    vch.resize( 4 + 48 + 8);
    memcpy(&vch[0],&version,4);
    memcpy(&vch[4],cipher.encrypted,48);
    memcpy(&vch[52],&cipher.nonce,8);

    return Write(CDestination(pubkey),vch);
}

bool CWalletAddrDB::UpdateTemplate(const CTemplateId& tid,const vector<unsigned char>& vchData)
{
    return Write(CDestination(tid),vchData);
}

bool CWalletAddrDB::EraseAddress(const CDestination& dest)
{
    return Erase(dest);
}

bool CWalletAddrDB::WalkThroughAddress(CWalletDBAddrWalker& walker)
{
    return WalkThrough(boost::bind(&CWalletAddrDB::AddressDBWalker,this,_1,_2,boost::ref(walker)));
}

bool CWalletAddrDB::AddressDBWalker(CWalleveBufStream& ssKey,CWalleveBufStream& ssValue,CWalletDBAddrWalker& walker)
{
    CDestination dest;
    vector<unsigned char> vch;
    ssKey >> dest;
    ssValue >> vch;

    if (dest.IsTemplate())
    {
        return walker.WalkTemplate(dest.GetTemplateId(),vch);
    }

    crypto::CPubKey pubkey;
    if (dest.GetPubKey(pubkey) && vch.size() == 4 + 48 + 8)
    {
        int version;
        crypto::CCryptoCipher cipher;

        memcpy(&version,&vch[0],4);
        memcpy(cipher.encrypted,&vch[4],48);
        memcpy(&cipher.nonce,&vch[52],8);

        return walker.WalkPubkey(pubkey,version,cipher);
    }

    return false;
}

//////////////////////////////
// CWalletTxDB

bool CWalletTxDB::Initialize(const boost::filesystem::path& pathWallet)
{
    CLevelDBArguments args;
    args.path = (pathWallet / "wtx").string();

    CLevelDBEngine *engine = new CLevelDBEngine(args);

    if (!Open(engine))
    {
        delete engine;
        return false;
    }

    if (!Read(string("txcount"),nTxCount) || !Read(string("sequence"),nSequence))
    {
        return Reset();
    }

    return true;
}

void CWalletTxDB::Deinitialize()
{
    Close();
}

bool CWalletTxDB::Clear()
{
    RemoveAll();
    return Reset();
}

bool CWalletTxDB::AddNewTx(const CWalletTx& wtx)
{
    pair<uint64,CWalletTx> pairWalletTx;
    if (Read(make_pair(string("wtx"),wtx.txid),pairWalletTx))
    {
        pairWalletTx.second = wtx;
        return Write(make_pair(string("wtx"),wtx.txid),pairWalletTx);
    }

    pairWalletTx.first = nSequence++;
    pairWalletTx.second = wtx;

    if (!TxnBegin())
    {
        return false;
    }

    if (!Write(make_pair(string("wtx"),wtx.txid),pairWalletTx)
        || !Write(make_pair(string("seq"),pairWalletTx.first),CWalletTxSeq(wtx))
        || !Write(string("txcount"),nTxCount + 1)
        || !Write(string("sequence"),nSequence))
    {
        TxnAbort();
        return false;
    }

    if (!TxnCommit())
    {
        return false;
    }

    ++nTxCount;

    return true;
}

bool CWalletTxDB::UpdateTx(const vector<CWalletTx>& vWalletTx,const vector<uint256>& vRemove)
{
    int nTxAddNew = 0;
    vector<pair<uint64,CWalletTx> > vTxUpdate;
    vTxUpdate.reserve(vWalletTx.size());

    BOOST_FOREACH(const CWalletTx& wtx,vWalletTx)
    {
        pair<uint64,CWalletTx> pairWalletTx;
        if (Read(make_pair(string("wtx"),wtx.txid),pairWalletTx))
        {
            vTxUpdate.push_back(make_pair(pairWalletTx.first,wtx));
        }
        else
        {
            vTxUpdate.push_back(make_pair(nSequence++,wtx));
            ++nTxAddNew;
        }
    }

    vector<pair<uint64,uint256> > vTxRemove;
    vTxRemove.reserve(vRemove.size());

    BOOST_FOREACH(const uint256& txid,vRemove)
    {
        pair<uint64,CWalletTx> pairWalletTx;
        if (Read(make_pair(string("wtx"),txid),pairWalletTx))
        {
            vTxRemove.push_back(make_pair(pairWalletTx.first,txid));
        }
    }

    if (!TxnBegin())
    {
        return false;
    }

    for (int i = 0;i < vTxUpdate.size();i++)
    {
        pair<uint64,CWalletTx>& pairWalletTx = vTxUpdate[i];
        if (!Write(make_pair(string("wtx"),pairWalletTx.second.txid),pairWalletTx)
            || !Write(make_pair(string("seq"),pairWalletTx.first),CWalletTxSeq(pairWalletTx.second)))
        {
            TxnAbort();
            return false;
        }
    }

    for (int i = 0;i < vTxRemove.size();i++)
    {
        if (!Erase(make_pair(string("wtx"),vTxRemove[i].second))
            || !Erase(make_pair(string("seq"),vTxRemove[i].first)))
        {
            TxnAbort();
            return false;
        }
    }

    if (!Write(string("txcount"),nTxCount + nTxAddNew - vTxRemove.size())
        || !Write(string("sequence"),nSequence))
    {
        TxnAbort();
        return false;
    }

    if (!TxnCommit())
    {
        return false;
    }

    nTxCount += nTxAddNew - vTxRemove.size();

    return true;
}

bool CWalletTxDB::RetrieveTx(const uint256& txid,CWalletTx& wtx)
{
    pair<uint64,CWalletTx> pairWalletTx;
    if (!Read(make_pair(string("wtx"),txid),pairWalletTx))
    {
        return false;
    }
    wtx = pairWalletTx.second;
    return true;
}

bool CWalletTxDB::ExistsTx(const uint256& txid)
{
    pair<uint64,CWalletTx> pairWalletTx;
    return Read(make_pair(string("wtx"),txid),pairWalletTx);
}

size_t CWalletTxDB::GetTxCount()
{
    return nTxCount;
}

bool CWalletTxDB::WalkThroughTxSeq(CWalletDBTxSeqWalker& walker)
{
    return WalkThrough(boost::bind(&CWalletTxDB::TxSeqWalker,this,_1,_2,boost::ref(walker)),
                       make_pair(string("seq"),uint64(0)));
}

bool CWalletTxDB::WalkThroughTx(CWalletDBTxWalker& walker)
{
    return WalkThrough(boost::bind(&CWalletTxDB::TxWalker,this,_1,_2,boost::ref(walker)),
                       make_pair(string("seq"),uint64(0)));
}

bool CWalletTxDB::TxSeqWalker(CWalleveBufStream& ssKey,CWalleveBufStream& ssValue,CWalletDBTxSeqWalker& walker)
{
    string strPrefix;
    uint64 nSeqNum;
    CWalletTxSeq txSeq;

    ssKey >> strPrefix;
    if (strPrefix != "seq")
    {
        return false;
    }

    ssKey >> nSeqNum;
    ssValue >> txSeq;

    return walker.Walk(txSeq.txid,txSeq.hashFork,txSeq.nBlockHeight);
}

bool CWalletTxDB::TxWalker(CWalleveBufStream& ssKey,CWalleveBufStream& ssValue,CWalletDBTxWalker& walker)
{
    string strPrefix;
    uint64 nSeqNum;
    CWalletTxSeq txSeq;

    ssKey >> strPrefix;
    if (strPrefix != "seq")
    {
        return false;
    }

    ssKey >> nSeqNum;
    ssValue >> txSeq;

    CWalletTx wtx;
    if (!RetrieveTx(txSeq.txid,wtx))
    {
        return false;
    }
    return walker.Walk(wtx);
}

bool CWalletTxDB::Reset()
{
    nSequence = 0;
    nTxCount  = 0;

    if (!TxnBegin())
    {
        return false;
    }

    if (!Write(string("txcount"),size_t(0)))
    {
        TxnAbort();
        return false;
    }

    if (!Write(string("sequence"),nSequence))
    {
        TxnAbort();
        return false;
    }

    return TxnCommit();
}

//////////////////////////////
// CWalletDBListTxSeqWalker

class CWalletDBListTxSeqWalker : public CWalletDBTxSeqWalker
{
public:
    CWalletDBListTxSeqWalker(int nOffsetIn,int nMaxCountIn)
    : nOffset(nOffsetIn), nMaxCount(nMaxCountIn), nIndex(0)
    {
    }
    bool Walk(const uint256& txid,const uint256& hashFork,const int nBlockHeight)
    {
        if (nIndex++ < nOffset)
        {
            return true;
        }
        vWalletTxid.push_back(txid);
        return (vWalletTxid.size() < nMaxCount);
    }
public:
    int nOffset;
    int nMaxCount;
    int nIndex;
    vector<uint256> vWalletTxid;
};

//////////////////////////////
// CWalletDBRollBackTxSeqWalker

class CWalletDBRollBackTxSeqWalker : public CWalletDBTxSeqWalker
{
public:
    CWalletDBRollBackTxSeqWalker(const uint256& hashForkIn,int nMinHeightIn,vector<uint256>& vForkTxIn)
    : hashFork(hashForkIn), nMinHeight(nMinHeightIn), vForkTx(vForkTxIn)
    {
    }
    bool Walk(const uint256& txidIn,const uint256& hashForkIn,const int nBlockHeightIn)
    {
        if (hashForkIn == hashFork && (nBlockHeightIn < 0 || nBlockHeightIn >= nMinHeight))
        {
            vForkTx.push_back(txidIn);
        }
        return true;
    }
public:
    uint256 hashFork;
    int nMinHeight;
    vector<uint256>& vForkTx;
};

//////////////////////////////
// CWalletDB

CWalletDB::CWalletDB()
{
}

CWalletDB::~CWalletDB()
{
    Deinitialize();
}

bool CWalletDB::Initialize(const boost::filesystem::path& pathWallet)
{
    if (!boost::filesystem::exists(pathWallet))
    {
        boost::filesystem::create_directories(pathWallet);
    }

    if (!boost::filesystem::is_directory(pathWallet))
    {
        return false;
    }

    if (!dbAddr.Initialize(pathWallet))
    {
        return false;
    }

    if (!dbWtx.Initialize(pathWallet))
    {
        return false;
    }

    return true;
}

void CWalletDB::Deinitialize()
{
    vector<CWalletTx> vWalletTx;
    txCache.ListTx(0,-1,vWalletTx);
    if (!vWalletTx.empty())
    {
        UpdateTx(vWalletTx);
    }

    txCache.Clear();

    dbWtx.Deinitialize();
    dbAddr.Deinitialize();
}

bool CWalletDB::UpdateKey(const crypto::CPubKey& pubkey,int version,const crypto::CCryptoCipher& cipher)
{
    return dbAddr.UpdateKey(pubkey,version,cipher);
}

bool CWalletDB::UpdateTemplate(const CTemplateId& tid,const vector<unsigned char>& vchData)
{
    return dbAddr.UpdateTemplate(tid,vchData);
}

bool CWalletDB::WalkThroughAddress(CWalletDBAddrWalker& walker)
{
    return dbAddr.WalkThroughAddress(walker);
}

bool CWalletDB::AddNewTx(const CWalletTx& wtx)
{
    if (wtx.nBlockHeight < 0)
    {
        txCache.AddNew(wtx);
        return true;
    }

    return dbWtx.AddNewTx(wtx);
}

bool CWalletDB::UpdateTx(const vector<CWalletTx>& vWalletTx,const vector<uint256>& vRemove)
{
    if (!dbWtx.UpdateTx(vWalletTx,vRemove))
    {
        return false;
    }
    BOOST_FOREACH(const CWalletTx& wtx,vWalletTx)
    {
        txCache.Remove(wtx.txid);
    }
    BOOST_FOREACH(const uint256& txid,vRemove)
    {
        txCache.Remove(txid);
    }
    return true;
}

bool CWalletDB::RetrieveTx(const uint256& txid,CWalletTx& wtx)
{
    if (txCache.Get(txid,wtx))
    {
        return true;
    }

    return dbWtx.RetrieveTx(txid,wtx);
}

bool CWalletDB::ExistsTx(const uint256& txid)
{
    if (txCache.Exists(txid))
    {
        return true;
    }

    return dbWtx.ExistsTx(txid);
}

size_t CWalletDB::GetTxCount()
{
    return dbWtx.GetTxCount() + txCache.Count();
}

bool CWalletDB::ListTx(int nOffset,int nCount,vector<CWalletTx>& vWalletTx)
{
    size_t nDBTx = dbWtx.GetTxCount();

    if (nOffset < nDBTx)
    {
        if (!ListDBTx(nOffset, nCount, vWalletTx))
        {
            return false;
        }
        if (vWalletTx.size() < nCount)
        {
            txCache.ListTx(0, nCount - vWalletTx.size(), vWalletTx);
        }
    }
    else
    {
        txCache.ListTx(nOffset - nDBTx, nCount, vWalletTx);
    }
    return true;
}

bool CWalletDB::ListDBTx(int nOffset,int nCount,vector<CWalletTx>& vWalletTx)
{
    CWalletDBListTxSeqWalker walker(nOffset,nCount);
    if (!dbWtx.WalkThroughTxSeq(walker))
    {
        return false;
    }

    BOOST_FOREACH(const uint256& txid,walker.vWalletTxid)
    {
        CWalletTx wtx;
        if (!dbWtx.RetrieveTx(txid,wtx))
        {
            return false;
        }
        vWalletTx.push_back(wtx);
    }

    return true;
}

bool CWalletDB::ListRollBackTx(const uint256& hashFork,int nMinHeight,vector<uint256>& vForkTx)
{
    CWalletDBRollBackTxSeqWalker walker(hashFork,nMinHeight,vForkTx);
    if (!dbWtx.WalkThroughTxSeq(walker))
    {
        return false;
    }
    txCache.ListForkTx(hashFork,vForkTx);
    return true;
}

bool CWalletDB::WalkThroughTx(CWalletDBTxWalker& walker)
{
    return dbWtx.WalkThroughTx(walker);
}

bool CWalletDB::ClearTx()
{
    txCache.Clear();
    return dbWtx.Clear();
}

max_stars_repo_name: ouyun/FnFnCoreWallet

max_issues_repo_name: ouyun/FnFnCoreWallet

max_forks_repo_name: ouyun/FnFnCoreWallet


text:
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <cstdlib>
#include <cassert>
#include <limits>

#include <boost/foreach.hpp>
#include <boost/thread.hpp>
#include <boost/random.hpp>
#include <boost/random/uniform_real.hpp>
#include <boost/random/normal_distribution.hpp>

#include <ros/ros.h>
#include <geometry_msgs/Point.h>
#include <geometry_msgs/PoseArray.h>
#include <geometry_msgs/Vector3.h>
#include <tf/transform_listener.h>
#include <sensor_msgs/PointCloud2.h>
#include <nav_msgs/Path.h>

#include <octomap/octomap.h>
#include <octomap_ros/conversions.h>
#include <octomap_msgs/Octomap.h>
#include <octomap_msgs/conversions.h>

#include <pcl/kdtree/kdtree_flann.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/features/normal_3d.h>
#include <pcl/features/boundary.h>
#include <pcl/segmentation/extract_clusters.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/filters/passthrough.h>

#include <pcl_conversions/pcl_conversions.h>

class NextBestView
{
public:
    NextBestView();
    ~NextBestView();
    bool isNearVoid(const octomap::point3d& p1, const unsigned char depth, const double res);
    void computeNextBestViews();
    void onOctomap(const octomap_msgs::Octomap::ConstPtr& m);
protected:
    std::string frame_id_;
    std::string robot_frame_id_;
    int num_clusters_;
    int min_computation_interval_;
    double normal_search_radius_;
    int min_pts_per_cluster_;
    double eps_angle_;
    double tolerance_;
    double boundary_angle_threshold_;
    ros::NodeHandle nh_;
    ros::NodeHandle private_node_handle_;
    octomap::OcTree *octree_ptr_;
    ros::Time last_computation_time_;
    ros::Publisher void_frontier_pub_;
    ros::Publisher posearray_pub_;
    std::vector<ros::Publisher> cluster_pub_;
    ros::Subscriber octree_sub_;
};

NextBestView::NextBestView() :
    frame_id_("/map"),
    robot_frame_id_("/base_link"),
    num_clusters_(3),
    min_computation_interval_(5 /*seconds*/),
    normal_search_radius_(0.4),
    min_pts_per_cluster_(5),
    eps_angle_(0.25),
    tolerance_(0.3),
    boundary_angle_threshold_(2.5),
    private_node_handle_("~"),
    octree_ptr_(0L)
{
    private_node_handle_.param("frame_id", frame_id_, frame_id_);
    private_node_handle_.param("robot_frame_id", robot_frame_id_, robot_frame_id_);
    private_node_handle_.param("num_clusters", num_clusters_, num_clusters_);
    private_node_handle_.param("min_computation_interval", min_computation_interval_, min_computation_interval_);
    private_node_handle_.param("normal_search_radius", normal_search_radius_, normal_search_radius_);
    private_node_handle_.param("min_pts_per_cluster", min_pts_per_cluster_, min_pts_per_cluster_);
    private_node_handle_.param("eps_angle", eps_angle_, eps_angle_);
    private_node_handle_.param("tolerance", tolerance_, tolerance_);
    private_node_handle_.param("boundary_angle_threshold", boundary_angle_threshold_, boundary_angle_threshold_);

    octree_sub_ = nh_.subscribe<octomap_msgs::Octomap>("octree_in", 1, &NextBestView::onOctomap, this);
    void_frontier_pub_ = nh_.advertise<sensor_msgs::PointCloud2>("void_frontier", 1, false);
    posearray_pub_ = nh_.advertise<geometry_msgs::PoseArray>("poses", 1, false);
    for(int i = 0; i < num_clusters_; i++)
    {
        std::stringstream ss; ss << "cluster_pcl_" << (i+1);
        cluster_pub_.push_back(nh_.advertise<sensor_msgs::PointCloud2>(ss.str(), 1, false));
    }
}

NextBestView::~NextBestView()
{
    if(octree_ptr_) delete octree_ptr_;
}

/**
 * Check if a point is near the unknown space (by 1 voxel).
 * Note: this assumes the point is contained by a cell in the octree.
 */
bool NextBestView::isNearVoid(const octomap::point3d& p1, const unsigned char depth, const double res)
{
    for(int dz = 0; dz <= 0; dz += 2)
    {
        for(int dy = -1; dy <= 1; dy += 2)
        {
            for(int dx = -1; dx <= 1; dx += 2)
            {
                octomap::OcTreeNode *pNode = octree_ptr_->search(p1.x() + res * dx, p1.y() + res * dy, p1.z() + res * dz, depth);
                if(!pNode) return true;
            }
        }
    }
    return false;
}

static bool compareClusters(pcl::PointIndices c1, pcl::PointIndices c2)
{
    return (c1.indices.size() < c2.indices.size());
}

/**
 * Compute void frontier points (leaf points in free space adjacent to unknown space)
 */
void NextBestView::computeNextBestViews()
{
    const unsigned char depth = 16;
    const double res = octree_ptr_->getResolution();

    // compute void frontier:
    octomap::point3d_list pl;
    for(octomap::OcTree::leaf_iterator it = octree_ptr_->begin_leafs(depth); it != octree_ptr_->end_leafs(); it++)
    {
        if(octree_ptr_->isNodeOccupied(*it))
            continue;

        if(isNearVoid(it.getCoordinate(), depth, res))
            pl.push_back(it.getCoordinate());
    }
    if(!pl.size())
    {
        ROS_ERROR("Found no frontier points at depth %d!", depth);
        return;
    }

    pcl::PointCloud<pcl::PointXYZ> border_pcl;
    border_pcl.resize(pl.size());
    border_pcl.header.frame_id = frame_id_;
    size_t i = 0;
    for(octomap::point3d_list::iterator it = pl.begin(); it != pl.end(); ++it)
    {
        border_pcl[i].x = it->x();
        border_pcl[i].y = it->y();
        border_pcl[i].z = it->z();
        i++;
    }

    sensor_msgs::PointCloud2 void_frontier_msg;
    pcl::toROSMsg(border_pcl, void_frontier_msg);
    void_frontier_pub_.publish(void_frontier_msg);

    // estimate normals:
    pcl::PointCloud<pcl::Normal> border_normals;
    pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> norm_estim;
    pcl::search::KdTree<pcl::PointXYZ>::Ptr tree = boost::make_shared<pcl::search::KdTree<pcl::PointXYZ> > ();
    tree->setInputCloud(boost::make_shared<pcl::PointCloud<pcl::PointXYZ> > (border_pcl));
    norm_estim.setSearchMethod(tree);
    norm_estim.setInputCloud(boost::make_shared<pcl::PointCloud<pcl::PointXYZ> > (border_pcl));
    norm_estim.setRadiusSearch(normal_search_radius_);
    norm_estim.compute(border_normals);

    // filter NaNs:
    pcl::PointIndices nan_indices;
    for (unsigned int i = 0; i < border_normals.points.size(); i++) {
        if (isnan(border_normals.points[i].normal[0]))
            nan_indices.indices.push_back(i);
    }
    pcl::ExtractIndices<pcl::PointXYZ> extract;
    extract.setInputCloud(boost::make_shared<pcl::PointCloud<pcl::PointXYZ> >(border_pcl));
    extract.setIndices(boost::make_shared<const pcl::PointIndices>(nan_indices));
    extract.setNegative(true);
    extract.filter(border_pcl);
    pcl::ExtractIndices<pcl::Normal> nextract;
    nextract.setInputCloud(boost::make_shared<pcl::PointCloud<pcl::Normal> >(border_normals));
    nextract.setIndices(boost::make_shared<const pcl::PointIndices>(nan_indices));
    nextract.setNegative(true);
    nextract.filter(border_normals);

    // tree object used for search
    pcl::KdTreeFLANN<pcl::PointXYZ>::Ptr tree2 = boost::make_shared<pcl::KdTreeFLANN<pcl::PointXYZ> >();
    tree2->setInputCloud(boost::make_shared<pcl::PointCloud<pcl::PointXYZ> >(border_pcl));
    // Decompose a region of space into clusters based on the euclidean distance between points, and the normal
    std::vector<pcl::PointIndices> clusters;
    pcl::extractEuclideanClusters<pcl::PointXYZ, pcl::Normal>(border_pcl, border_normals, tolerance_, tree2, clusters, eps_angle_, min_pts_per_cluster_);

    if(clusters.size() > 0)
    {
        std::sort(clusters.begin(), clusters.end(), compareClusters);
        std::vector<pcl::PointCloud<pcl::PointXYZ> > cluster_clouds;
        cluster_clouds.reserve(num_clusters_);

        geometry_msgs::PoseArray nbv_pose_array;

        for(unsigned int nc = 0; nc < clusters.size(); nc++) {
            if(nc == num_clusters_)
                break;

            // extract a cluster:
            pcl::PointCloud<pcl::PointXYZ> cluster_pcl;
            cluster_pcl.header = border_pcl.header;
            extract.setInputCloud(boost::make_shared<pcl::PointCloud<pcl::PointXYZ> >(border_pcl));
            extract.setIndices(boost::make_shared<const pcl::PointIndices>(clusters.back()));
            extract.setNegative(false);
            extract.filter(cluster_pcl);
            // extract normals of cluster:
            pcl::PointCloud<pcl::Normal> cluster_normals;
            cluster_normals.header = border_pcl.header;
            nextract.setInputCloud(boost::make_shared<pcl::PointCloud<pcl::Normal> >(border_normals));
            nextract.setIndices(boost::make_shared<const pcl::PointIndices>(clusters.back()));
            nextract.setNegative(false);
            nextract.filter(cluster_normals);
            // find boundary points of cluster:
            pcl::search::KdTree<pcl::PointXYZ>::Ptr tree3 = boost::make_shared<pcl::search::KdTree<pcl::PointXYZ> >();
            tree3->setInputCloud(boost::make_shared<pcl::PointCloud<pcl::PointXYZ> >(cluster_pcl));
            pcl::PointCloud<pcl::Boundary> boundary_pcl;
            pcl::BoundaryEstimation<pcl::PointXYZ, pcl::Normal, pcl::Boundary> be;
            be.setSearchMethod(tree3);
            be.setInputCloud(boost::make_shared<pcl::PointCloud<pcl::PointXYZ> >(cluster_pcl));
            be.setInputNormals(boost::make_shared<pcl::PointCloud<pcl::Normal> >(cluster_normals));
            be.setRadiusSearch(0.5);
            be.setAngleThreshold(boundary_angle_threshold_);
            be.compute(boundary_pcl);

            geometry_msgs::Pose nbv_pose;
            for (unsigned int i = 0; i < boundary_pcl.points.size(); ++i) {
                if (boundary_pcl.points[i].boundary_point) {
                    nbv_pose.position.x = cluster_pcl.points[i].x;
                    nbv_pose.position.y = cluster_pcl.points[i].y;
                    nbv_pose.position.z = cluster_pcl.points[i].z;
                    tf::Vector3 axis(0, -cluster_normals.points[i].normal[2],
                            cluster_normals.points[i].normal[1]);
                    tf::Quaternion quat(axis, axis.length());
                    geometry_msgs::Quaternion quat_msg;
                    tf::quaternionTFToMsg(quat, quat_msg);
                    nbv_pose.orientation = quat_msg;
                    nbv_pose_array.poses.push_back(nbv_pose);
                }
            }

            cluster_clouds.push_back(cluster_pcl);

            // pop the just used cluster from indices:
            clusters.pop_back();
        }

        // visualize pose array:
        nbv_pose_array.header.frame_id = border_pcl.header.frame_id;
        nbv_pose_array.header.stamp = ros::Time::now();
        posearray_pub_.publish(nbv_pose_array);

        // visualize cluster pcls:
        for(int i = 0; i < num_clusters_; i++)
        {
            sensor_msgs::PointCloud2 cluster_pcl_msg;
            pcl::toROSMsg(cluster_clouds[i], cluster_pcl_msg);
            cluster_pub_[i].publish(cluster_pcl_msg);
        }
    }
}

/**
 * Octomap callback.
 *
 * It will skip if trying to compute poses more frequently than min_goal_interval.
 */
void NextBestView::onOctomap(const octomap_msgs::Octomap::ConstPtr& map)
{
    if((last_computation_time_ + ros::Duration(min_computation_interval_, 0)) > ros::Time::now())
        return;

    if(octree_ptr_) delete octree_ptr_;
    octree_ptr_ = octomap_msgs::binaryMsgToMap(*map);

    last_computation_time_ = ros::Time::now();
    computeNextBestViews();
}

int main(int argc, char **argv)
{
    ros::init(argc, argv, "next_best_view_node");

    NextBestView nbv;
    ros::spin();

    return 0;
}


text:
// Copyright Stephan T. Lavavej, http://nuwen.net .
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at
// http://boost.org/LICENSE_1_0.txt .

#ifndef PHAM_FILE_HH
#define PHAM_FILE_HH

#include "compiler.hh"

#ifdef NUWEN_PLATFORM_MSVC
    #pragma once
#endif

#include "gluon.hh"
#include "typedef.hh"

#include "external_begin.hh"
    #include <cstdio>
    #include <stdexcept>
    #include <string>
    #include <boost/format.hpp>
    #include <boost/shared_ptr.hpp>
    #include <boost/utility.hpp>
#include "external_end.hh"

namespace pham {
    namespace file {
        class proto_file;
    }
}

namespace nuwen {
    namespace file {
        enum output_mode {
            create,
            overwrite,
            append
        };

        inline vuc_t read_file(const std::string& filename);
        inline void write_file(const vuc_t& v, const std::string& filename, output_mode mode = create);

        inline void remove_file(const std::string& filename);

        class input_file {
        public:
            inline explicit input_file(const std::string& filename);

            inline vuc_t read_at_most(vuc_s_t n);
            inline vuc_t read_rest();

            inline void close();

        private:
            boost::shared_ptr<pham::file::proto_file> m_p;
        };

        class output_file {
        public:
            inline explicit output_file(const std::string& filename, output_mode mode = create);

            inline void write(const vuc_t& v);

            inline void close();

        private:
            boost::shared_ptr<pham::file::proto_file> m_p;
        };
    }
}

namespace pham {
    namespace file {
        using namespace std;

        const nuwen::vuc_s_t BLOCK_SIZE = 65536;

        inline void throw_file_error(const string& fxn, const string& msg, const string& filename) {
            throw runtime_error(str(boost::format("RUNTIME ERROR: %1% - %2% (Filename: \"%3%\")") % fxn % msg % filename));
        }

        inline void throw_file_logic_error(const string& fxn, const string& msg, const string& filename) {
            throw logic_error(str(boost::format("LOGIC ERROR: %1% - %2% (Filename: \"%3%\")") % fxn % msg % filename));
        }

        class proto_file : public boost::noncopyable {
        public:
            proto_file(FILE * const p, const string& s) : m_fp(p), m_name(s) {
                if (m_fp == NULL) {
                    throw_file_logic_error("pham::file::proto_file::proto_file()", "Invalid p.", m_name);
                }
            }

            void close() {
                if (m_fp == NULL) {
                    throw_file_logic_error("pham::file::proto_file::close()", "Called twice.", m_name);
                }

                FILE * const p = m_fp;

                m_fp = NULL;

                if (fclose(p) == EOF) {
                    throw_file_error("pham::file::proto_file::close()", "std::fclose() failed.", m_name);
                }
            }

            ~proto_file() {
                if (m_fp) {
                    fclose(m_fp); // Ignore errors.
                }
            }

            FILE * raw() {
                if (m_fp == NULL) {
                    throw_file_logic_error("pham::file::proto_file::raw()", "File already closed.", m_name);
                }

                return m_fp;
            }

            string name() const {
                return m_name;
            }

        private:
            FILE *       m_fp;
            const string m_name;
        };

        inline FILE * open_read_file(const string& filename) {
            FILE * const r = fopen(filename.c_str(), "rb");

            if (r == NULL) {
                throw_file_error("pham::file::open_read_file()", "std::fopen() failed.", filename);
            }

            return r;
        }

        inline FILE * open_write_file(const string& filename, const nuwen::file::output_mode mode) {
            if (mode == nuwen::file::create) {
                FILE * const r = fopen(filename.c_str(), "rb");

                if (r) {
                    if (fclose(r) == EOF) {
                        throw_file_error("pham::file::open_write_file()", "File already exists and std::fclose() failed.", filename);
                    } else {
                        throw_file_error("pham::file::open_write_file()", "File already exists.", filename);
                    }
                }
            }

            FILE * const w = fopen(filename.c_str(), mode == nuwen::file::append ? "ab" : "wb");

            if (w == NULL) {
                throw_file_error("pham::file::open_write_file()", "std::fopen() failed.", filename);
            }

            return w;
        }
    }
}

inline nuwen::vuc_t nuwen::file::read_file(const std::string& filename) {
    input_file f(filename);

    const vuc_t ret = f.read_rest();

    f.close();

    return ret;
}

inline void nuwen::file::write_file(const vuc_t& v, const std::string& filename, const output_mode mode) {
    output_file f(filename, mode);

    f.write(v);

    f.close();
}

inline void nuwen::file::remove_file(const std::string& filename) {
    using namespace std;

    if (remove(filename.c_str()) != 0) {
        pham::file::throw_file_error("nuwen::file::remove_file()", "std::remove() failed.", filename);
    }
}

inline nuwen::file::input_file::input_file(const std::string& filename)
    : m_p(new pham::file::proto_file(pham::file::open_read_file(filename), filename)) { }

inline nuwen::vuc_t nuwen::file::input_file::read_at_most(const vuc_s_t n) {
    using namespace std;

    if (n == 0) {
        pham::file::throw_file_logic_error("nuwen::file::input_file::read_at_most()", "Invalid n.", m_p->name());
    }

    vuc_t ret(n);

    ret.resize(fread(&ret[0], 1, n, m_p->raw()));

    if (ferror(m_p->raw())) {
        pham::file::throw_file_error("nuwen::file::input_file::read_at_most()", "std::fread() failed.", m_p->name());
    }

    return ret;
}

inline nuwen::vuc_t nuwen::file::input_file::read_rest() {
    using namespace std;

    vuc_t ret;

    vuc_t temp(pham::file::BLOCK_SIZE);

    while (true) {
        temp.resize(fread(&temp[0], 1, pham::file::BLOCK_SIZE, m_p->raw()));

        if (ferror(m_p->raw())) {
            pham::file::throw_file_error("nuwen::file::input_file::read_rest()", "std::fread() failed.", m_p->name());
        }

        ret += cat(temp);

        if (temp.size() != pham::file::BLOCK_SIZE) {
            break;
        }
    }

    return ret;
}

inline void nuwen::file::input_file::close() {
    m_p->close();
}

inline nuwen::file::output_file::output_file(const std::string& filename, const output_mode mode)
    : m_p(new pham::file::proto_file(pham::file::open_write_file(filename, mode), filename)) { }

inline void nuwen::file::output_file::write(const vuc_t& v) {
    using namespace std;

    if (v.empty()) {
        return;
    }

    if (fwrite(&v[0], 1, v.size(), m_p->raw()) != v.size()) {
        pham::file::throw_file_error("nuwen::file::output_file::write()", "std::fwrite() failed.", m_p->name());
    }

    if (fflush(m_p->raw()) != 0) {
        pham::file::throw_file_error("nuwen::file::output_file::write()", "std::fflush() failed.", m_p->name());
    }
}

inline void nuwen::file::output_file::close() {
    m_p->close();
}

#endif // Idempotency

ext: hh

max_stars_repo_name: nurettin/libnuwen

max_issues_repo_name: nurettin/libnuwen

max_forks_repo_name: nurettin/libnuwen


text:
// Copyright (c) 2021 Graphcore Ltd. All rights reserved.
#define BOOST_TEST_MODULE pipeline_unittest

#include <boost/test/unit_test.hpp>

#include <popart/error.hpp>
#include <popart/graph.hpp>
#include <popart/ir.hpp>
#include <popart/op.hpp>
#include <popart/op/call.hpp>
#include <popart/tensornames.hpp>
#include <popart/transforms/pipeline.hpp>

using namespace popart;

BOOST_AUTO_TEST_CASE(remove_all) {
  Ir ir;

  BOOST_REQUIRE(ir.getAllGraphs().size() == 1);

  auto &g = ir.getMainGraph();

  ir.createGraph(GraphId("foo"));
  ir.createGraph(GraphId("bar"));

  BOOST_REQUIRE(ir.getAllGraphs().size() == 3);

  ir.removeIsolatedGraphs();

  BOOST_REQUIRE(ir.getAllGraphs().size() == 1);
}

BOOST_AUTO_TEST_CASE(doesnt_remove_one) {
  Ir ir;

  BOOST_REQUIRE(ir.getAllGraphs().size() == 1);

  auto &g = ir.getMainGraph();

  ir.createGraph(GraphId("foo"));
  auto &sg = ir.createGraph(GraphId("bar"));

  BOOST_REQUIRE(ir.getAllGraphs().size() == 3);

  Op::Settings settings = Op::Settings{g, "testOp"};
  g.createConnectedOp<CallOp>(
      {}, {}, Onnx::CustomOperators::Call_1, sg, settings);

  ir.removeIsolatedGraphs();

  BOOST_REQUIRE(ir.getAllGraphs().size() == 2);
}

max_stars_repo_name: graphcore/popart

max_issues_repo_name: graphcore/popart

max_forks_repo_name: graphcore/popart


text:
// Copyright (c) 2019-2020, The Monero Project
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
//    conditions and the following disclaimer.
//
// 2. 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.
//
// 3. Neither the name of the copyright holder 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
// 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 "dandelionpp.h"

#include <boost/container/small_vector.hpp>
#include <boost/uuid/nil_generator.hpp>
#include <chrono>

#include "src/common/expect.h"
#include "src/cryptonote_config.h"
#include "src/crypto/crypto.h"

namespace net
{
namespace dandelionpp
{
    namespace
    {
        constexpr const std::size_t expected_max_channels = CRYPTONOTE_NOISE_CHANNELS;

        // could be in util somewhere
        struct key_less
        {
            template<typename K, typename V>
            bool operator()(const std::pair<K, V>& left, const K& right) const
            {
                return left.first < right;
            }

            template<typename K, typename V>
            bool operator()(const K& left, const std::pair<K, V>& right) const
            {
                return left < right.first;
            }
        };

        std::size_t select_stem(epee::span<const std::size_t> usage, epee::span<const boost::uuids::uuid> out_map)
        {
            assert(usage.size() < std::numeric_limits<std::size_t>::max()); // prevented in constructor
            if (usage.size() < out_map.size())
                return std::numeric_limits<std::size_t>::max();

            // small_vector uses stack space if `expected_max_channels < capacity()`
            std::size_t lowest = std::numeric_limits<std::size_t>::max();
            boost::container::small_vector<std::size_t, expected_max_channels> choices;
            static_assert(sizeof(choices) < 256, "choices is too large based on current configuration");

            for (const boost::uuids::uuid& out : out_map)
            {
                if (!out.is_nil())
                {
                    const std::size_t location = std::addressof(out) - out_map.begin();
                    if (usage[location] < lowest)
                    {
                        lowest = usage[location];
                        choices = {location};
                    }
                    else if (usage[location] == lowest)
                        choices.push_back(location);
                }
            }

            switch (choices.size())
            {
            case 0:
                return std::numeric_limits<std::size_t>::max();
            case 1:
                return choices[0];
            default:
                break;
            }

            return choices[crypto::rand_idx(choices.size())];
        }
    } // anonymous

    connection_map::connection_map(std::vector<boost::uuids::uuid> out_connections, const std::size_t stems)
      : out_mapping_(std::move(out_connections)),
        in_mapping_(),
        usage_count_()
    {
        // max value is used by `select_stem` as error case
        if (stems == std::numeric_limits<std::size_t>::max())
            MONERO_THROW(common_error::kInvalidArgument, "stems value cannot be max size_t");

        usage_count_.resize(stems);
        if (stems < out_mapping_.size())
        {
            for (unsigned i = 0; i < stems; ++i)
                std::swap(out_mapping_[i], out_mapping_.at(i + crypto::rand_idx(out_mapping_.size() - i)));

            out_mapping_.resize(stems);
        }
        else
        {
            std::shuffle(out_mapping_.begin(), out_mapping_.end(), crypto::random_device{});
        }
    }

    connection_map::~connection_map() noexcept
    {}

    connection_map connection_map::clone() const
    {
        return {*this};
    }

    bool connection_map::update(std::vector<boost::uuids::uuid> current)
    {
        std::sort(current.begin(), current.end());

        bool replace = false;
        for (auto& existing_out : out_mapping_)
        {
            const auto elem = std::lower_bound(current.begin(), current.end(), existing_out);
            if (elem == current.end() || *elem != existing_out)
            {
                existing_out = boost::uuids::nil_uuid();
                replace = true;
            }
            else // already using connection, remove it from candidate list
                current.erase(elem);
        }

        if (!replace && out_mapping_.size() == usage_count_.size())
            return false;

        const std::size_t existing_outs = out_mapping_.size();
        for (std::size_t i = 0; i < usage_count_.size() && !current.empty(); ++i)
        {
            const bool increase_stems = out_mapping_.size() <= i;
            if (increase_stems || out_mapping_[i].is_nil())
            {
                std::swap(current.back(), current.at(crypto::rand_idx(current.size())));
                if (increase_stems)
                    out_mapping_.push_back(current.back());
                else
                    out_mapping_[i] = current.back();
                current.pop_back();
            }
        }

        return replace || existing_outs < out_mapping_.size();
    }

    std::size_t connection_map::size() const noexcept
    {
        std::size_t count = 0;
        for (const boost::uuids::uuid& connection : out_mapping_)
        {
            if (!connection.is_nil())
                ++count;
        }
        return count;
    }

    boost::uuids::uuid connection_map::get_stem(const boost::uuids::uuid& source)
    {
        auto elem = std::lower_bound(in_mapping_.begin(), in_mapping_.end(), source, key_less{});
        if (elem == in_mapping_.end() || elem->first != source)
        {
            const std::size_t index = select_stem(epee::to_span(usage_count_), epee::to_span(out_mapping_));
            if (out_mapping_.size() < index)
                return boost::uuids::nil_uuid();

            elem = in_mapping_.emplace(elem, source, index);
            usage_count_[index]++;
        }
        else if (out_mapping_.at(elem->second).is_nil()) // stem connection disconnected after mapping
        {
            usage_count_.at(elem->second)--;
            const std::size_t index = select_stem(epee::to_span(usage_count_), epee::to_span(out_mapping_));
            if (out_mapping_.size() < index)
            {
                in_mapping_.erase(elem);
                return boost::uuids::nil_uuid();
            }

            elem->second = index;
            usage_count_[index]++;
        }

        return out_mapping_[elem->second];
    }
} // dandelionpp
} // net

max_stars_repo_name: andrewkatson/denarii

max_issues_repo_name: andrewkatson/denarii

max_forks_repo_name: andrewkatson/denarii


text:
#include <iostream>
#include <string>
#include <boost/fusion/include/vector.hpp>
#include <boost/fusion/include/make_vector.hpp>
#include <boost/fusion/include/at_c.hpp>

namespace fusion = boost::fusion;

int main()
{
	const auto v = fusion::make_vector(123, "Hello!", 4.56f);

	std::cout << fusion::at_c<0>(v) << std::endl;
	std::cout << fusion::at_c<1>(v) << std::endl;
	std::cout << fusion::at_c<2>(v) << std::endl;

	return 0;
}