forked from premiere/premiere-libtorrent
added custom resize methods to aux::vector (#1514)
added custom resize methods to aux::vector, code refactor
This commit is contained in:
parent
3d06371f31
commit
7ac2805c2c
|
@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||||
#define TORRENT_VECTOR_HPP
|
#define TORRENT_VECTOR_HPP
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
#include "libtorrent/units.hpp"
|
#include "libtorrent/units.hpp"
|
||||||
#include "libtorrent/assert.hpp"
|
#include "libtorrent/assert.hpp"
|
||||||
|
@ -70,7 +71,38 @@ namespace libtorrent { namespace aux {
|
||||||
}
|
}
|
||||||
|
|
||||||
IndexType end_index() const
|
IndexType end_index() const
|
||||||
{ return IndexType(static_cast<underlying_index>(this->size())); }
|
{
|
||||||
|
TORRENT_ASSERT(this->size() <= std::size_t(std::numeric_limits<underlying_index>::max()));
|
||||||
|
return IndexType(static_cast<underlying_index>(this->size()));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename U = underlying_index, typename Cond
|
||||||
|
= typename std::enable_if<std::is_signed<U>::value>::type>
|
||||||
|
void resize(underlying_index s)
|
||||||
|
{
|
||||||
|
TORRENT_ASSERT(s >= 0);
|
||||||
|
this->base::resize(std::size_t(s));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename U = underlying_index, typename Cond
|
||||||
|
= typename std::enable_if<std::is_signed<U>::value>::type>
|
||||||
|
void resize(underlying_index s, T const& v)
|
||||||
|
{
|
||||||
|
TORRENT_ASSERT(s >= 0);
|
||||||
|
this->base::resize(std::size_t(s), v);
|
||||||
|
}
|
||||||
|
|
||||||
|
void resize(std::size_t s)
|
||||||
|
{
|
||||||
|
TORRENT_ASSERT(s <= std::size_t(std::numeric_limits<underlying_index>::max()));
|
||||||
|
this->base::resize(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
void resize(std::size_t s, T const& v)
|
||||||
|
{
|
||||||
|
TORRENT_ASSERT(s <= std::size_t(std::numeric_limits<underlying_index>::max()));
|
||||||
|
this->base::resize(s, v);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename Iter>
|
template <typename Iter>
|
||||||
|
|
|
@ -363,7 +363,7 @@ namespace libtorrent
|
||||||
|
|
||||||
m_files.set_num_pieces(static_cast<int>(
|
m_files.set_num_pieces(static_cast<int>(
|
||||||
(m_files.total_size() + m_files.piece_length() - 1) / m_files.piece_length()));
|
(m_files.total_size() + m_files.piece_length() - 1) / m_files.piece_length()));
|
||||||
m_piece_hash.resize(std::size_t(m_files.num_pieces()));
|
m_piece_hash.resize(m_files.num_pieces());
|
||||||
}
|
}
|
||||||
|
|
||||||
create_torrent::create_torrent(torrent_info const& ti)
|
create_torrent::create_torrent(torrent_info const& ti)
|
||||||
|
@ -399,7 +399,7 @@ namespace libtorrent
|
||||||
add_http_seed(s.url);
|
add_http_seed(s.url);
|
||||||
}
|
}
|
||||||
|
|
||||||
m_piece_hash.resize(std::size_t(m_files.num_pieces()));
|
m_piece_hash.resize(m_files.num_pieces());
|
||||||
for (piece_index_t i(0); i != m_files.end_piece(); ++i)
|
for (piece_index_t i(0); i != m_files.end_piece(); ++i)
|
||||||
set_hash(i, ti.hash_for_piece(i));
|
set_hash(i, ti.hash_for_piece(i));
|
||||||
|
|
||||||
|
@ -614,7 +614,7 @@ namespace libtorrent
|
||||||
int const num_leafs = merkle_num_leafs(m_files.num_pieces());
|
int const num_leafs = merkle_num_leafs(m_files.num_pieces());
|
||||||
int const num_nodes = merkle_num_nodes(num_leafs);
|
int const num_nodes = merkle_num_nodes(num_leafs);
|
||||||
int const first_leaf = num_nodes - num_leafs;
|
int const first_leaf = num_nodes - num_leafs;
|
||||||
m_merkle_tree.resize(std::size_t(num_nodes));
|
m_merkle_tree.resize(num_nodes);
|
||||||
int const num_pieces = int(m_piece_hash.size());
|
int const num_pieces = int(m_piece_hash.size());
|
||||||
for (int i = 0; i < num_pieces; ++i)
|
for (int i = 0; i < num_pieces; ++i)
|
||||||
m_merkle_tree[first_leaf + i] = m_piece_hash[piece_index_t(i)];
|
m_merkle_tree[first_leaf + i] = m_piece_hash[piece_index_t(i)];
|
||||||
|
@ -695,7 +695,7 @@ namespace libtorrent
|
||||||
{
|
{
|
||||||
TORRENT_ASSERT(index >= file_index_t(0));
|
TORRENT_ASSERT(index >= file_index_t(0));
|
||||||
TORRENT_ASSERT(index < m_files.end_file());
|
TORRENT_ASSERT(index < m_files.end_file());
|
||||||
if (m_filehashes.empty()) m_filehashes.resize(std::size_t(m_files.num_files()));
|
if (m_filehashes.empty()) m_filehashes.resize(m_files.num_files());
|
||||||
m_filehashes[index] = h;
|
m_filehashes[index] = h;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue