2008-05-28 04:35:02 +02:00
|
|
|
/*
|
|
|
|
|
2018-04-09 09:04:33 +02:00
|
|
|
Copyright (c) 2008-2018, Arvid Norberg
|
2008-05-28 04:35:02 +02:00
|
|
|
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_BITFIELD_HPP_INCLUDED
|
|
|
|
#define TORRENT_BITFIELD_HPP_INCLUDED
|
|
|
|
|
|
|
|
#include "libtorrent/assert.hpp"
|
|
|
|
#include "libtorrent/config.hpp"
|
2018-06-02 19:29:40 +02:00
|
|
|
#include "libtorrent/index_range.hpp"
|
2017-01-26 17:13:40 +01:00
|
|
|
#include "libtorrent/aux_/unique_ptr.hpp"
|
2015-09-07 23:00:27 +02:00
|
|
|
#include "libtorrent/aux_/byteswap.hpp"
|
2016-08-07 22:21:08 +02:00
|
|
|
#include "libtorrent/aux_/ffs.hpp"
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2009-02-22 22:01:51 +01:00
|
|
|
#include <cstring> // for memset and memcpy
|
2016-06-18 20:01:38 +02:00
|
|
|
#include <cstdint> // uint32_t
|
2008-05-28 04:35:02 +02:00
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent {
|
|
|
|
|
2016-04-26 06:37:47 +02:00
|
|
|
// The bitfield type stores any number of bits as a bitfield
|
2014-07-28 06:41:40 +02:00
|
|
|
// in a heap allocated array.
|
2008-05-28 04:35:02 +02:00
|
|
|
struct TORRENT_EXPORT bitfield
|
|
|
|
{
|
2013-09-06 04:29:21 +02:00
|
|
|
// constructs a new bitfield. The default constructor creates an empty
|
|
|
|
// bitfield. ``bits`` is the size of the bitfield (specified in bits).
|
2014-04-28 06:46:33 +02:00
|
|
|
// ``val`` is the value to initialize the bits to. If not specified
|
2013-09-06 04:29:21 +02:00
|
|
|
// all bits are initialized to 0.
|
|
|
|
//
|
|
|
|
// The constructor taking a pointer ``b`` and ``bits`` copies a bitfield
|
|
|
|
// from the specified buffer, and ``bits`` number of bits (rounded up to
|
2016-04-26 06:37:47 +02:00
|
|
|
// the nearest byte boundary).
|
2017-05-28 13:14:58 +02:00
|
|
|
bitfield() noexcept = default;
|
2016-10-08 20:07:11 +02:00
|
|
|
explicit bitfield(int bits) { resize(bits); }
|
2016-07-16 23:31:25 +02:00
|
|
|
bitfield(int bits, bool val) { resize(bits, val); }
|
|
|
|
bitfield(char const* b, int bits) { assign(b, bits); }
|
|
|
|
bitfield(bitfield const& rhs) { assign(rhs.data(), rhs.size()); }
|
2017-05-28 13:14:58 +02:00
|
|
|
bitfield(bitfield&& rhs) noexcept = default;
|
2008-05-28 04:35:02 +02:00
|
|
|
|
2013-09-06 04:29:21 +02:00
|
|
|
// copy bitfield from buffer ``b`` of ``bits`` number of bits, rounded up to
|
|
|
|
// the nearest byte boundary.
|
2017-11-27 20:13:37 +01:00
|
|
|
void assign(char const* b, int const bits)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
|
|
|
resize(bits);
|
|
|
|
if (bits > 0)
|
|
|
|
{
|
2017-04-23 03:33:38 +02:00
|
|
|
std::memcpy(buf(), b, std::size_t((bits + 7) / 8));
|
2014-07-06 21:18:00 +02:00
|
|
|
clear_trailing_bits();
|
|
|
|
}
|
|
|
|
}
|
2008-05-28 04:35:02 +02:00
|
|
|
|
2013-09-06 04:29:21 +02:00
|
|
|
// query bit at ``index``. Returns true if bit is 1, otherwise false.
|
2018-08-20 15:20:16 +02:00
|
|
|
bool operator[](int index) const noexcept
|
2008-05-28 04:35:02 +02:00
|
|
|
{ return get_bit(index); }
|
2018-08-20 15:20:16 +02:00
|
|
|
bool get_bit(int index) const noexcept
|
2008-05-28 04:35:02 +02:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(index >= 0);
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(index < size());
|
2017-09-17 23:50:33 +02:00
|
|
|
return (buf()[index / 32] & aux::host_to_network(0x80000000 >> (index & 31))) != 0;
|
2008-05-28 04:35:02 +02:00
|
|
|
}
|
2015-05-18 03:30:32 +02:00
|
|
|
|
2013-09-06 04:29:21 +02:00
|
|
|
// set bit at ``index`` to 0 (clear_bit) or 1 (set_bit).
|
2018-08-20 15:20:16 +02:00
|
|
|
void clear_bit(int index) noexcept
|
2008-05-28 04:35:02 +02:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(index >= 0);
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(index < size());
|
2016-07-16 23:31:25 +02:00
|
|
|
buf()[index / 32] &= aux::host_to_network(~(0x80000000 >> (index & 31)));
|
2008-05-28 04:35:02 +02:00
|
|
|
}
|
2018-08-20 15:20:16 +02:00
|
|
|
void set_bit(int index) noexcept
|
2013-09-06 04:29:21 +02:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(index >= 0);
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(index < size());
|
2017-09-17 23:50:33 +02:00
|
|
|
buf()[index / 32] |= aux::host_to_network(0x80000000 >> (index & 31));
|
2013-09-06 04:29:21 +02:00
|
|
|
}
|
2008-05-28 04:35:02 +02:00
|
|
|
|
2012-05-02 20:55:58 +02:00
|
|
|
// returns true if all bits in the bitfield are set
|
2018-06-02 19:29:40 +02:00
|
|
|
bool all_set() const noexcept;
|
2012-05-02 20:55:58 +02:00
|
|
|
|
2019-09-07 11:20:24 +02:00
|
|
|
// returns true if no bit in the bitfield is set
|
2018-06-02 19:29:40 +02:00
|
|
|
bool none_set() const noexcept
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
2017-01-13 23:08:54 +01:00
|
|
|
if(size() == 0) return true;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
const int words = num_words();
|
2016-07-16 23:31:25 +02:00
|
|
|
std::uint32_t const* b = buf();
|
2014-07-06 21:18:00 +02:00
|
|
|
for (int i = 0; i < words; ++i)
|
2012-05-02 20:55:58 +02:00
|
|
|
{
|
2016-07-16 23:31:25 +02:00
|
|
|
if (b[i] != 0) return false;
|
2012-05-02 20:55:58 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-06 04:29:21 +02:00
|
|
|
// returns the size of the bitfield in bits.
|
2018-06-02 19:29:40 +02:00
|
|
|
int size() const noexcept
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
2017-11-27 20:13:37 +01:00
|
|
|
int const bits = m_buf == nullptr ? 0 : int(m_buf[0]);
|
|
|
|
TORRENT_ASSERT(bits >= 0);
|
|
|
|
return bits;
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
|
|
|
|
2019-09-07 11:20:24 +02:00
|
|
|
// returns the number of 32 bit words are needed to represent all bits in
|
|
|
|
// this bitfield.
|
2018-08-20 15:20:16 +02:00
|
|
|
int num_words() const noexcept
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
|
|
|
return (size() + 31) / 32;
|
|
|
|
}
|
2013-09-06 04:29:21 +02:00
|
|
|
|
|
|
|
// returns true if the bitfield has zero size.
|
2018-08-20 15:20:16 +02:00
|
|
|
bool empty() const noexcept { return size() == 0; }
|
2008-05-28 04:35:02 +02:00
|
|
|
|
2019-09-07 11:20:24 +02:00
|
|
|
// returns a pointer to the internal buffer of the bitfield, or
|
|
|
|
// ``nullptr`` if it's empty.
|
2018-08-20 15:20:16 +02:00
|
|
|
char const* data() const noexcept { return m_buf ? reinterpret_cast<char const*>(&m_buf[1]) : nullptr; }
|
|
|
|
char* data() noexcept { return m_buf ? reinterpret_cast<char*>(&m_buf[1]) : nullptr; }
|
2008-05-28 04:35:02 +02:00
|
|
|
|
2018-04-26 09:01:14 +02:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2015-05-18 03:30:32 +02:00
|
|
|
TORRENT_DEPRECATED
|
|
|
|
char const* bytes() const { return data(); }
|
|
|
|
#endif
|
|
|
|
|
2019-09-07 11:20:24 +02:00
|
|
|
// hidden
|
2008-05-28 04:35:02 +02:00
|
|
|
bitfield& operator=(bitfield const& rhs)
|
|
|
|
{
|
2018-11-16 02:44:11 +01:00
|
|
|
if (&rhs == this) return *this;
|
2015-05-09 21:07:33 +02:00
|
|
|
assign(rhs.data(), rhs.size());
|
2008-05-28 04:35:02 +02:00
|
|
|
return *this;
|
|
|
|
}
|
2017-05-28 13:14:58 +02:00
|
|
|
bitfield& operator=(bitfield&& rhs) noexcept = default;
|
2016-07-16 23:31:25 +02:00
|
|
|
|
2019-09-07 11:20:24 +02:00
|
|
|
// swaps the bit-fields two variables refer to
|
2018-08-20 15:20:16 +02:00
|
|
|
void swap(bitfield& rhs) noexcept
|
2016-02-12 23:50:29 +01:00
|
|
|
{
|
2016-07-16 23:31:25 +02:00
|
|
|
std::swap(m_buf, rhs.m_buf);
|
2016-02-12 23:50:29 +01:00
|
|
|
}
|
|
|
|
|
2013-09-06 04:29:21 +02:00
|
|
|
// count the number of bits in the bitfield that are set to 1.
|
2018-06-02 19:29:40 +02:00
|
|
|
int count() const noexcept;
|
2019-09-07 11:20:24 +02:00
|
|
|
|
|
|
|
// returns the index of the first set bit in the bitfield, i.e. 1 bit.
|
2018-06-02 19:29:40 +02:00
|
|
|
int find_first_set() const noexcept;
|
2019-09-07 11:20:24 +02:00
|
|
|
|
|
|
|
// returns the index to the last cleared bit in the bitfield, i.e. 0 bit.
|
2018-06-02 19:29:40 +02:00
|
|
|
int find_last_clear() const noexcept;
|
2008-05-28 04:35:02 +02:00
|
|
|
|
2019-09-07 11:20:24 +02:00
|
|
|
// internal
|
2008-05-28 04:35:02 +02:00
|
|
|
struct const_iterator
|
|
|
|
{
|
|
|
|
friend struct bitfield;
|
|
|
|
|
2018-03-22 17:01:38 +01:00
|
|
|
using value_type = bool;
|
|
|
|
using difference_type = ptrdiff_t;
|
|
|
|
using pointer = bool const*;
|
|
|
|
using reference = bool&;
|
|
|
|
using iterator_category = std::forward_iterator_tag;
|
2008-05-28 04:35:02 +02:00
|
|
|
|
2018-08-20 15:20:16 +02:00
|
|
|
bool operator*() noexcept { return (*buf & aux::host_to_network(bit)) != 0; }
|
|
|
|
const_iterator& operator++() noexcept { inc(); return *this; }
|
|
|
|
const_iterator operator++(int) noexcept
|
2008-05-28 04:35:02 +02:00
|
|
|
{ const_iterator ret(*this); inc(); return ret; }
|
2018-08-20 15:20:16 +02:00
|
|
|
const_iterator& operator--() noexcept { dec(); return *this; }
|
|
|
|
const_iterator operator--(int) noexcept
|
2008-05-28 04:35:02 +02:00
|
|
|
{ const_iterator ret(*this); dec(); return ret; }
|
|
|
|
|
2018-08-20 15:20:16 +02:00
|
|
|
const_iterator() noexcept {}
|
|
|
|
bool operator==(const_iterator const& rhs) const noexcept
|
2014-07-06 21:18:00 +02:00
|
|
|
{ return buf == rhs.buf && bit == rhs.bit; }
|
2008-05-28 04:35:02 +02:00
|
|
|
|
2018-08-20 15:20:16 +02:00
|
|
|
bool operator!=(const_iterator const& rhs) const noexcept
|
2014-07-06 21:18:00 +02:00
|
|
|
{ return buf != rhs.buf || bit != rhs.bit; }
|
2008-05-28 04:35:02 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
void inc()
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(buf);
|
2008-05-28 04:35:02 +02:00
|
|
|
if (bit == 0x01)
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
bit = 0x80000000;
|
|
|
|
++buf;
|
2008-05-28 04:35:02 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bit >>= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void dec()
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(buf);
|
|
|
|
if (bit == 0x80000000)
|
2008-05-28 04:35:02 +02:00
|
|
|
{
|
|
|
|
bit = 0x01;
|
2014-07-06 21:18:00 +02:00
|
|
|
--buf;
|
2008-05-28 04:35:02 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bit <<= 1;
|
|
|
|
}
|
|
|
|
}
|
2016-06-18 20:01:38 +02:00
|
|
|
const_iterator(std::uint32_t const* ptr, int offset)
|
2014-07-06 21:18:00 +02:00
|
|
|
: buf(ptr), bit(0x80000000 >> offset) {}
|
2017-09-28 10:11:20 +02:00
|
|
|
std::uint32_t const* buf = nullptr;
|
|
|
|
std::uint32_t bit = 0x80000000;
|
2008-05-28 04:35:02 +02:00
|
|
|
};
|
|
|
|
|
2019-09-07 11:20:24 +02:00
|
|
|
// internal
|
2018-08-20 15:20:16 +02:00
|
|
|
const_iterator begin() const noexcept { return const_iterator(m_buf ? buf() : nullptr, 0); }
|
2018-09-04 13:46:09 +02:00
|
|
|
const_iterator end() const noexcept
|
|
|
|
{
|
|
|
|
if (m_buf)
|
|
|
|
return const_iterator(buf() + num_words() - (((size() & 31) == 0) ? 0 : 1), size() & 31);
|
|
|
|
else
|
|
|
|
return const_iterator(nullptr, size() & 31);
|
|
|
|
}
|
2008-05-28 04:35:02 +02:00
|
|
|
|
2013-09-06 04:29:21 +02:00
|
|
|
// set the size of the bitfield to ``bits`` length. If the bitfield is extended,
|
|
|
|
// the new bits are initialized to ``val``.
|
2016-04-20 13:50:57 +02:00
|
|
|
void resize(int bits, bool val);
|
|
|
|
void resize(int bits);
|
2008-05-28 04:35:02 +02:00
|
|
|
|
2013-09-06 04:29:21 +02:00
|
|
|
// set all bits in the bitfield to 1 (set_all) or 0 (clear_all).
|
2018-08-20 15:20:16 +02:00
|
|
|
void set_all() noexcept
|
2013-09-06 04:29:21 +02:00
|
|
|
{
|
2017-01-13 23:08:54 +01:00
|
|
|
if (size() == 0) return;
|
2019-11-05 20:14:46 +01:00
|
|
|
std::memset(buf(), 0xff, std::size_t(num_words()) * 4);
|
2013-09-06 04:29:21 +02:00
|
|
|
clear_trailing_bits();
|
|
|
|
}
|
2018-08-20 15:20:16 +02:00
|
|
|
void clear_all() noexcept
|
2013-09-06 04:29:21 +02:00
|
|
|
{
|
2017-01-13 23:08:54 +01:00
|
|
|
if (size() == 0) return;
|
2019-11-05 20:14:46 +01:00
|
|
|
std::memset(buf(), 0x00, std::size_t(num_words()) * 4);
|
2013-09-06 04:29:21 +02:00
|
|
|
}
|
2015-05-18 07:06:07 +02:00
|
|
|
|
2013-09-06 04:29:21 +02:00
|
|
|
// make the bitfield empty, of zero size.
|
2018-08-20 15:20:16 +02:00
|
|
|
void clear() noexcept { m_buf.reset(); }
|
2009-02-03 08:46:24 +01:00
|
|
|
|
2008-05-28 04:35:02 +02:00
|
|
|
private:
|
|
|
|
|
2018-08-20 15:20:16 +02:00
|
|
|
std::uint32_t const* buf() const noexcept { TORRENT_ASSERT(m_buf); return &m_buf[1]; }
|
|
|
|
std::uint32_t* buf() noexcept { TORRENT_ASSERT(m_buf); return &m_buf[1]; }
|
|
|
|
void clear_trailing_bits() noexcept
|
2009-02-06 09:51:25 +01:00
|
|
|
{
|
|
|
|
// clear the tail bits in the last byte
|
2016-07-16 23:31:25 +02:00
|
|
|
if (size() & 31) buf()[num_words() - 1] &= aux::host_to_network(0xffffffff << (32 - (size() & 31)));
|
2009-02-06 09:51:25 +01:00
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// the first element is not part of the bitfield, it's the
|
2016-07-16 23:31:25 +02:00
|
|
|
// number of bits.
|
2017-01-26 17:13:40 +01:00
|
|
|
aux::unique_ptr<std::uint32_t[]> m_buf;
|
2008-05-28 04:35:02 +02:00
|
|
|
};
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
template <typename IndexType>
|
|
|
|
struct typed_bitfield : bitfield
|
|
|
|
{
|
2017-05-28 13:14:58 +02:00
|
|
|
typed_bitfield() noexcept {}
|
|
|
|
typed_bitfield(typed_bitfield&& rhs) noexcept
|
|
|
|
: bitfield(std::forward<bitfield>(rhs))
|
|
|
|
{}
|
|
|
|
typed_bitfield(typed_bitfield const& rhs)
|
|
|
|
: bitfield(static_cast<bitfield const&>(rhs))
|
|
|
|
{}
|
|
|
|
typed_bitfield(bitfield&& rhs) noexcept : bitfield(std::forward<bitfield>(rhs)) {} // NOLINT
|
|
|
|
typed_bitfield(bitfield const& rhs) : bitfield(rhs) {} // NOLINT
|
|
|
|
typed_bitfield& operator=(typed_bitfield&& rhs) noexcept
|
|
|
|
{
|
|
|
|
this->bitfield::operator=(std::forward<bitfield>(rhs));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
typed_bitfield& operator=(typed_bitfield const& rhs)
|
|
|
|
{
|
|
|
|
this->bitfield::operator=(rhs);
|
|
|
|
return *this;
|
|
|
|
}
|
2016-12-22 16:42:33 +01:00
|
|
|
using bitfield::bitfield;
|
|
|
|
|
2018-06-02 19:29:40 +02:00
|
|
|
// returns an object that can be used in a range-for to iterate over all
|
|
|
|
// indices in the bitfield
|
|
|
|
index_range<IndexType> range() const noexcept
|
|
|
|
{
|
2018-07-07 20:47:50 +02:00
|
|
|
return {IndexType{0}, end_index()};
|
2018-06-02 19:29:40 +02:00
|
|
|
}
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
bool operator[](IndexType const index) const
|
|
|
|
{ return this->bitfield::get_bit(static_cast<int>(index)); }
|
|
|
|
|
|
|
|
bool get_bit(IndexType const index) const
|
|
|
|
{ return this->bitfield::get_bit(static_cast<int>(index)); }
|
|
|
|
|
|
|
|
void clear_bit(IndexType const index)
|
|
|
|
{ this->bitfield::clear_bit(static_cast<int>(index)); }
|
|
|
|
|
|
|
|
void set_bit(IndexType const index)
|
|
|
|
{ this->bitfield::set_bit(static_cast<int>(index)); }
|
|
|
|
|
2018-06-02 19:29:40 +02:00
|
|
|
IndexType end_index() const noexcept { return IndexType(this->size()); }
|
2016-12-22 16:42:33 +01:00
|
|
|
};
|
2008-05-28 04:35:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TORRENT_BITFIELD_HPP_INCLUDED
|