Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
251reverse_iterator rbegin() noexcept { return reverse_iterator{end()}; }252reverse_iterator rend() noexcept { return reverse_iterator{begin()}; }253const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator{cend()}; }254const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator{cend()}; }255const_reverse_iterator rend() const noexcept { return const_reverse_iterator{cbegin()}; }256const_reverse_iterator crend() const noexcept { return const_reverse_iterator{cbegin()}; }258/** Count the number of bits in the container. */259size_type size() const noexcept { return m_deque.size() * BITS_PER_WORD - m_pad_begin - m_pad_end; }261/** Determine whether the container is empty. */262bool empty() const noexcept263{264return m_deque.size() == 0 || (m_deque.size() == 1 && (m_pad_begin + m_pad_end == BITS_PER_WORD));265}267/** Return the maximum size of the container. */268size_type max_size() const noexcept269{270if (m_deque.max_size() < std::numeric_limits<difference_type>::max() / BITS_PER_WORD) {271return m_deque.max_size() * BITS_PER_WORD;272} else {273return std::numeric_limits<difference_type>::max();274}275}277/** Set the container equal to the bits in [first,last). */278template<typename It>279void assign(It first, It last)280{281size_type count = std::distance(first, last);282assign(count, false);283auto it = begin();284while (first != last) {285*(it++) = *(first++);286}287}289/** Set the container equal to the bits in ilist. */290void assign(std::initializer_list<bool> ilist)291{292assign(ilist.size(), false);293auto it = begin();294auto init = ilist.begin();295while (init != ilist.end()) {296*(it++) = *(init++);297}298}300/** Set the container equal to the bits in ilist. */301bitdeque& operator=(std::initializer_list<bool> ilist)302{303assign(ilist);304return *this;305}307/** Construct a container containing the bits in [first,last). */308template<typename It>309bitdeque(It first, It last) { assign(first, last); }311/** Construct a container containing the bits in ilist. */312bitdeque(std::initializer_list<bool> ilist) { assign(ilist); }314// Access an element of the container, with bounds checking.315reference at(size_type position)316{317if (position >= size()) throw std::out_of_range("bitdeque::at() out of range");318return begin()[position];319}320const_reference at(size_type position) const321{322if (position >= size()) throw std::out_of_range("bitdeque::at() out of range");323return cbegin()[position];324}326// Access elements of the container without bounds checking.327reference operator[](size_type position) { return begin()[position]; }328const_reference operator[](size_type position) const { return cbegin()[position]; }329reference front() { return *begin(); }330const_reference front() const { return *cbegin(); }331reference back() { return end()[-1]; }332const_reference back() const { return cend()[-1]; }334/** Release unused memory. */335void shrink_to_fit()336{337m_deque.shrink_to_fit();338}340/** Empty the container. */341void clear() noexcept342{343m_deque.clear();344m_pad_begin = m_pad_end = 0;345}347// Append an element to the container.348void push_back(bool val)349{350extend_back(1);351back() = val;352}353reference emplace_back(bool val)354{355extend_back(1);356auto ref = back();357ref = val;358return ref;359}361// Prepend an element to the container.362void push_front(bool val)363{364extend_front(1);365front() = val;366}367reference emplace_front(bool val)368{369extend_front(1);370auto ref = front();371ref = val;372return ref;373}375// Remove the last element from the container.376void pop_back()377{378erase_back(1);379}381// Remove the first element from the container.382void pop_front()383{384erase_front(1);385}387/** Resize the container. */388void resize(size_type n)389{390if (n < size()) {391erase_back(size() - n);392} else {393extend_back(n - size());394}395}397// Swap two containers.398void swap(bitdeque& other) noexcept399{400std::swap(m_deque, other.m_deque);401std::swap(m_pad_begin, other.m_pad_begin);402std::swap(m_pad_end, other.m_pad_end);403}404friend void swap(bitdeque& b1, bitdeque& b2) noexcept { b1.swap(b2); }406// Erase elements from the container.407iterator erase(const_iterator first, const_iterator last)408{409size_type before = std::distance(cbegin(), first);410size_type dist = std::distance(first, last);411size_type after = std::distance(last, cend());412if (before < after) {413std::move_backward(begin(), begin() + before, end() - after);414erase_front(dist);415return begin() + before;416} else {417std::move(end() - after, end(), begin() + before);418erase_back(dist);419return end() - after;420}421}423iterator erase(iterator first, iterator last) { return erase(const_iterator{first}, const_iterator{last}); }424iterator erase(const_iterator pos) { return erase(pos, pos + 1); }425iterator erase(iterator pos) { return erase(const_iterator{pos}, const_iterator{pos} + 1); }427// Insert elements into the container.428iterator insert(const_iterator pos, bool val)429{430size_type before = pos - cbegin();431insert_zeroes(before, 1);432auto it = begin() + before;433*it = val;434return it;435}437iterator emplace(const_iterator pos, bool val) { return insert(pos, val); }439iterator insert(const_iterator pos, size_type count, bool val)440{441size_type before = pos - cbegin();442insert_zeroes(before, count);443auto it_begin = begin() + before;444auto it = it_begin;445auto it_end = it + count;446while (it != it_end) *(it++) = val;447return it_begin;448}450template<typename It>451iterator insert(const_iterator pos, It first, It last)452{453size_type before = pos - cbegin();454size_type count = std::distance(first, last);455insert_zeroes(before, count);456auto it_begin = begin() + before;457auto it = it_begin;458while (first != last) {459*(it++) = *(first++);460}461return it_begin;462}463};465#endif // BITCOIN_UTIL_BITDEQUE_H
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.