refactor to use std unordered containers and minor cleanup (#1070)

This commit is contained in:
Alden Torres 2016-09-06 23:37:36 -04:00 committed by Arvid Norberg
parent 5e69dc700c
commit 4c53d7b2da
19 changed files with 67 additions and 89 deletions

View File

@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <cstdlib> // for atoi
#include <cstring>
#include <utility>
#include <deque>
#include "libtorrent/config.hpp"

View File

@ -103,7 +103,7 @@ void torrent_view::update_torrents(std::vector<lt::torrent_status> const& st)
for (std::vector<lt::torrent_status>::const_iterator i = st.begin();
i != st.end(); ++i)
{
boost::unordered_set<lt::torrent_status>::iterator j = m_all_handles.find(*i);
auto j = m_all_handles.find(*i);
// add new entries here
if (j == m_all_handles.end())
{
@ -370,11 +370,10 @@ void torrent_view::update_filtered_torrents()
{
m_scroll_position = 0;
m_filtered_handles.clear();
for (boost::unordered_set<lt::torrent_status>::iterator i = m_all_handles.begin()
, end(m_all_handles.end()); i != end; ++i)
for (auto const& h : m_all_handles)
{
if (!show_torrent(*i)) continue;
m_filtered_handles.push_back(&*i);
if (!show_torrent(h)) continue;
m_filtered_handles.push_back(&h);
}
if (m_active_torrent >= int(m_filtered_handles.size())) m_active_torrent = int(m_filtered_handles.size()) - 1;
if (m_active_torrent < 0) m_active_torrent = 0;

View File

@ -3,9 +3,10 @@
#include <set>
#include <vector>
#include <boost/unordered_set.hpp>
#include <unordered_set>
#include "libtorrent/torrent_handle.hpp"
#include "libtorrent/torrent_status.hpp"
namespace lt = libtorrent;
@ -62,7 +63,7 @@ private:
void update_filtered_torrents();
// all torrents
boost::unordered_set<lt::torrent_status> m_all_handles;
std::unordered_set<lt::torrent_status> m_all_handles;
// pointers into m_all_handles of the remaining torrents after filtering
std::vector<lt::torrent_status const*> m_filtered_handles;

View File

@ -56,4 +56,3 @@ namespace libtorrent
}
#endif

View File

@ -34,15 +34,13 @@ POSSIBILITY OF SUCH DAMAGE.
#define TORRENT_BLOCK_CACHE
#include "libtorrent/aux_/disable_warnings_push.hpp"
#include <boost/unordered_set.hpp>
#include <boost/shared_array.hpp>
#include "libtorrent/aux_/disable_warnings_pop.hpp"
#include <cstdint>
#include <list>
#include <vector>
#include <unordered_set>
#include "libtorrent/time.hpp"
#include "libtorrent/error_code.hpp"
@ -329,12 +327,6 @@ namespace libtorrent
#endif
};
// internal
inline std::size_t hash_value(cached_piece_entry const& p)
{
return std::size_t(p.storage.get()) + std::size_t(p.piece);
}
struct TORRENT_EXTRA_EXPORT block_cache : disk_buffer_pool
{
block_cache(int block_size, io_service& ios
@ -342,11 +334,15 @@ namespace libtorrent
private:
typedef boost::unordered_set<cached_piece_entry> cache_t;
struct hash_value
{
std::size_t operator()(cached_piece_entry const& p) const
{ return std::size_t(p.storage.get()) + std::size_t(p.piece); }
};
typedef std::unordered_set<cached_piece_entry, hash_value> cache_t;
public:
typedef cache_t::iterator iterator;
typedef cache_t::const_iterator const_iterator;
// returns the number of blocks this job would cause to be read in
@ -357,7 +353,7 @@ namespace libtorrent
// returns a range of all pieces. This might be a very
// long list, use carefully
std::pair<iterator, iterator> all_pieces() const;
std::pair<const_iterator, const_iterator> all_pieces() const;
int num_pieces() const { return int(m_pieces.size()); }
list_iterator<cached_piece_entry> write_lru_pieces() const

View File

@ -40,14 +40,12 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/hasher.hpp"
#include "libtorrent/file.hpp" // for combine_path etc.
#include "libtorrent/aux_/disable_warnings_push.hpp"
#include <vector>
#include <string>
#include <utility>
#include "libtorrent/aux_/disable_warnings_push.hpp"
#include <boost/config.hpp>
#include "libtorrent/aux_/disable_warnings_pop.hpp"
// OVERVIEW

View File

@ -36,7 +36,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/config.hpp"
#include "libtorrent/aux_/disable_warnings_push.hpp"
#include <boost/utility.hpp>
#ifndef TORRENT_DISABLE_POOL_ALLOCATOR
#include "libtorrent/allocator.hpp" // for page_aligned_allocator

View File

@ -37,9 +37,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <mutex>
#include "libtorrent/aux_/disable_warnings_push.hpp"
#include <boost/pool/object_pool.hpp>
#include <boost/pool/pool.hpp>
#include "libtorrent/aux_/disable_warnings_pop.hpp"
namespace libtorrent

View File

@ -33,29 +33,17 @@ POSSIBILITY OF SUCH DAMAGE.
#ifndef TORRENT_IO_SERVICE_FWD_HPP_INCLUDED
#define TORRENT_IO_SERVICE_FWD_HPP_INCLUDED
#ifdef __OBJC__
#define Protocol Protocol_
#endif
#include "libtorrent/aux_/disable_warnings_push.hpp"
#include <boost/version.hpp>
#include "libtorrent/aux_/disable_warnings_pop.hpp"
#ifdef __OBJC__
#undef Protocol
#endif
#include "libtorrent/config.hpp"
#if defined TORRENT_BUILD_SIMULATOR
namespace sim { namespace asio {
struct io_service;
}}
#endif
#else
namespace boost { namespace asio {
class io_service;
}}
#endif
namespace libtorrent
{
@ -67,4 +55,3 @@ namespace libtorrent
}
#endif

View File

@ -277,14 +277,7 @@ namespace libtorrent
// peer IDs, node IDs etc.
using sha1_hash = digest32<160>;
// this is here to support usage of sha1_hash in boost unordered containers
typedef sha1_hash peer_id;
inline std::size_t hash_value(sha1_hash const& b)
{
std::size_t ret;
std::memcpy(&ret, &b[0], sizeof(ret));
return ret;
}
#if TORRENT_USE_IOSTREAM
@ -297,8 +290,8 @@ namespace libtorrent
#endif // TORRENT_USE_IOSTREAM
}
namespace std {
namespace std
{
template <>
struct hash<libtorrent::sha1_hash>
{
@ -306,7 +299,7 @@ namespace std {
{
std::size_t ret;
// this is OK because sha1_hash is already a hash
std::memcpy(&ret, k.data(), sizeof(ret));
std::memcpy(&ret, &k[0], sizeof(ret));
return ret;
}
};

View File

@ -71,9 +71,6 @@ namespace libtorrent
struct storage_interface;
class torrent;
// allows torrent_handle to be used in unordered_map and unordered_set.
TORRENT_EXPORT std::size_t hash_value(torrent_status const& ts);
#ifndef BOOST_NO_EXCEPTIONS
void throw_invalid_handle() TORRENT_NO_RETURN;
#endif
@ -199,8 +196,8 @@ namespace libtorrent
state_t piece_state;
};
// for boost::hash (and to support using this type in unordered_map etc.)
std::size_t hash_value(torrent_handle const& h);
// for std::hash (and to support using this type in unordered_map etc.)
TORRENT_EXPORT std::size_t hash_value(torrent_handle const& h);
// You will usually have to store your torrent handles somewhere, since it's
// the object through which you retrieve information about the torrent and
@ -241,7 +238,7 @@ namespace libtorrent
friend struct aux::session_impl;
friend struct session_handle;
friend class torrent;
friend std::size_t hash_value(torrent_handle const& th);
friend TORRENT_EXPORT std::size_t hash_value(torrent_handle const& th);
// constructs a torrent handle that does not refer to a torrent.
// i.e. is_valid() will return false.
@ -1305,4 +1302,16 @@ namespace libtorrent
}
namespace std
{
template <>
struct hash<libtorrent::torrent_handle>
{
std::size_t operator()(libtorrent::torrent_handle const& th) const
{
return libtorrent::hash_value(th);
}
};
}
#endif // TORRENT_TORRENT_HANDLE_HPP_INCLUDED

View File

@ -521,4 +521,16 @@ namespace libtorrent
}
namespace std
{
template <>
struct hash<libtorrent::torrent_status>
{
std::size_t operator()(libtorrent::torrent_status const& ts) const
{
return libtorrent::hash_value(ts.handle);
}
};
}
#endif // TORRENT_TORRENT_STATUS_HPP_INCLUDED

View File

@ -43,16 +43,13 @@ POSSIBILITY OF SUCH DAMAGE.
#include <tuple>
#include <functional>
#include <memory>
#include "libtorrent/aux_/disable_warnings_push.hpp"
#include <boost/unordered_map.hpp>
#include <unordered_map>
#ifdef TORRENT_USE_OPENSSL
#include "libtorrent/aux_/disable_warnings_push.hpp"
#include <boost/asio/ssl/context.hpp>
#endif
#include "libtorrent/aux_/disable_warnings_pop.hpp"
#endif
#include "libtorrent/socket.hpp"
#include "libtorrent/address.hpp"
@ -395,11 +392,11 @@ namespace libtorrent
// maps transactionid to the udp_tracker_connection
// These must use shared_ptr to avoid a dangling reference
// if a connection is erased while a timeout event is in the queue
typedef boost::unordered_map<std::uint32_t
, std::shared_ptr<udp_tracker_connection>> udp_conns_t;
using udp_conns_t = std::unordered_map<std::uint32_t
, std::shared_ptr<udp_tracker_connection>> ;
udp_conns_t m_udp_conns;
typedef std::vector<std::shared_ptr<http_tracker_connection>> http_conns_t;
using http_conns_t = std::vector<std::shared_ptr<http_tracker_connection>>;
http_conns_t m_http_conns;
send_fun_t m_send_fun;

View File

@ -853,7 +853,7 @@ void block_cache::blocks_flushed(cached_piece_entry* pe, int const* flushed, int
update_cache_state(pe);
}
std::pair<block_cache::iterator, block_cache::iterator> block_cache::all_pieces() const
std::pair<block_cache::const_iterator, block_cache::const_iterator> block_cache::all_pieces() const
{
return std::make_pair(m_pieces.begin(), m_pieces.end());
}
@ -1662,7 +1662,7 @@ void block_cache::check_invariant() const
}
}
boost::unordered_set<char*> buffers;
std::unordered_set<char*> buffers;
for (auto const& p :m_pieces)
{
TORRENT_PIECE_ASSERT(p.blocks, &p);

View File

@ -47,7 +47,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include <boost/asio/ip/host_name.hpp>
#include <boost/asio/ip/multicast.hpp>
#include <boost/config.hpp>
#include "libtorrent/aux_/disable_warnings_pop.hpp"

View File

@ -36,7 +36,7 @@ namespace libtorrent { namespace aux {
#ifdef TORRENT_PROFILE_CALLS
static std::mutex g_calls_mutex;
static boost::unordered_map<std::string, int> g_blocking_calls;
static std::unordered_map<std::string, int> g_blocking_calls;
#endif
void blocking_call()
@ -57,10 +57,9 @@ void dump_call_profile()
std::map<int, std::string> profile;
std::unique_lock<std::mutex> l(g_calls_mutex);
for (boost::unordered_map<std::string, int>::const_iterator i = g_blocking_calls.begin()
, end(g_blocking_calls.end()); i != end; ++i)
for (auto const& c : g_blocking_calls)
{
profile[i->second] = i->first;
profile[c.second] = c.first;
}
for (std::map<int, std::string>::const_reverse_iterator i = profile.rbegin()
, end(profile.rend()); i != end; ++i)

View File

@ -3781,8 +3781,7 @@ namespace libtorrent
TORRENT_ASSERT(i->finished <= m_picker->blocks_in_piece(index));
#if TORRENT_USE_ASSERTS
for (std::vector<piece_picker::downloading_piece>::const_iterator j = boost::next(i);
j != dl_queue.end(); ++j)
for (auto j = std::next(i); j != dl_queue.end(); ++j)
{
TORRENT_ASSERT(j->index != index);
}
@ -4800,14 +4799,14 @@ namespace libtorrent
i->flags = flags;
// resort i since deadline might have changed
while (boost::next(i) != m_time_critical_pieces.end() && i->deadline > boost::next(i)->deadline)
while (std::next(i) != m_time_critical_pieces.end() && i->deadline > std::next(i)->deadline)
{
std::iter_swap(i, boost::next(i));
std::iter_swap(i, std::next(i));
++i;
}
while (i != m_time_critical_pieces.begin() && i->deadline < boost::prior(i)->deadline)
while (i != m_time_critical_pieces.begin() && i->deadline < std::prev(i)->deadline)
{
std::iter_swap(i, boost::prior(i));
std::iter_swap(i, std::prev(i));
--i;
}
// just in case this piece had priority 0

View File

@ -727,16 +727,10 @@ namespace libtorrent
return m_torrent.lock();
}
std::size_t hash_value(torrent_status const& ts)
{
return hash_value(ts.handle);
}
std::size_t hash_value(torrent_handle const& th)
{
// using the locked shared_ptr value as hash doesn't work
// for expired weak_ptrs. So, we're left with a hack
return std::size_t(*reinterpret_cast<void* const*>(&th.m_torrent));
}
}

View File

@ -45,8 +45,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/torrent_info.hpp"
#include "libtorrent/read_resume_data.hpp"
#include <boost/utility.hpp>
#include <iostream>
#include <fstream>