Inside Bitcoin
Opening the selected file…
Inside Bitcoin
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.
251bool empty() const {252return size() == 0;253}255iterator begin() { return iterator(item_ptr(0)); }256const_iterator begin() const { return const_iterator(item_ptr(0)); }257iterator end() { return iterator(item_ptr(size())); }258const_iterator end() const { return const_iterator(item_ptr(size())); }260size_t capacity() const {261if (is_direct()) {262return N;263} else {264return _union.indirect_contents.capacity;265}266}268T& operator[](size_type pos) {269return *item_ptr(pos);270}272const T& operator[](size_type pos) const {273return *item_ptr(pos);274}276void resize(size_type new_size) {277size_type cur_size = size();278if (cur_size == new_size) {279return;280}281if (cur_size > new_size) {282erase(item_ptr(new_size), end());283return;284}285if (new_size > capacity()) {286change_capacity(new_size);287}288ptrdiff_t increase = new_size - cur_size;289fill(item_ptr(cur_size), increase);290_size += increase;291}293void reserve(size_type new_capacity) {294if (new_capacity > capacity()) {295change_capacity(new_capacity);296}297}299void shrink_to_fit() {300change_capacity(size());301}303void clear() {304resize(0);305}307iterator insert(iterator pos, const T& value) {308size_type p = pos - begin();309size_type new_size = size() + 1;310if (capacity() < new_size) {311change_capacity(new_size + (new_size >> 1));312}313T* ptr = item_ptr(p);314T* dst = ptr + 1;315memmove(dst, ptr, (size() - p) * sizeof(T));316_size++;317new(static_cast<void*>(ptr)) T(value);318return iterator(ptr);319}321void insert(iterator pos, size_type count, const T& value) {322size_type p = pos - begin();323size_type new_size = size() + count;324if (capacity() < new_size) {325change_capacity(new_size + (new_size >> 1));326}327T* ptr = item_ptr(p);328T* dst = ptr + count;329memmove(dst, ptr, (size() - p) * sizeof(T));330_size += count;331fill(item_ptr(p), count, value);332}334template <std::input_iterator InputIterator>335void insert(iterator pos, InputIterator first, InputIterator last) {336size_type p = pos - begin();337difference_type count = last - first;338size_type new_size = size() + count;339if (capacity() < new_size) {340change_capacity(new_size + (new_size >> 1));341}342T* ptr = item_ptr(p);343T* dst = ptr + count;344memmove(dst, ptr, (size() - p) * sizeof(T));345_size += count;346fill(ptr, first, last);347}349inline void resize_uninitialized(size_type new_size) {350// resize_uninitialized changes the size of the prevector but does not initialize it.351// If size < new_size, the added elements must be initialized explicitly.352if (capacity() < new_size) {353change_capacity(new_size);354_size += new_size - size();355return;356}357if (new_size < size()) {358erase(item_ptr(new_size), end());359} else {360_size += new_size - size();361}362}364iterator erase(iterator pos) {365return erase(pos, pos + 1);366}368iterator erase(iterator first, iterator last) {369// Erase is not allowed to the change the object's capacity. That means370// that when starting with an indirectly allocated prevector with371// size and capacity > N, the result may be a still indirectly allocated372// prevector with size <= N and capacity > N. A shrink_to_fit() call is373// necessary to switch to the (more efficient) directly allocated374// representation (with capacity N and size <= N).375iterator p = first;376char* endp = (char*)&(*end());377_size -= last - p;378memmove(&(*first), &(*last), endp - ((char*)(&(*last))));379return first;380}382template<typename... Args>383void emplace_back(Args&&... args) {384size_type new_size = size() + 1;385if (capacity() < new_size) {386change_capacity(new_size + (new_size >> 1));387}388new(item_ptr(size())) T(std::forward<Args>(args)...);389_size++;390}392void push_back(const T& value) {393emplace_back(value);394}396void pop_back() {397erase(end() - 1, end());398}400T& front() {401return *item_ptr(0);402}404const T& front() const {405return *item_ptr(0);406}408T& back() {409return *item_ptr(size() - 1);410}412const T& back() const {413return *item_ptr(size() - 1);414}416void swap(prevector<N, T, Size, Diff>& other) noexcept417{418std::swap(_union, other._union);419std::swap(_size, other._size);420}422~prevector() {423if (!is_direct()) {424free(_union.indirect_contents.indirect);425_union.indirect_contents.indirect = nullptr;426}427}429constexpr bool operator==(const prevector& other) const {430return std::ranges::equal(*this, other);431}433bool operator<(const prevector<N, T, Size, Diff>& other) const {434if (size() < other.size()) {435return true;436}437if (size() > other.size()) {438return false;439}440const_iterator b1 = begin();441const_iterator b2 = other.begin();442const_iterator e1 = end();443while (b1 != e1) {444if ((*b1) < (*b2)) {445return true;446}447if ((*b2) < (*b1)) {448return false;449}450++b1;451++b2;452}453return false;454}456size_t allocated_memory() const {457if (is_direct()) {458return 0;459} else {460return ((size_t)(sizeof(T))) * _union.indirect_contents.capacity;461}462}464value_type* data() {465return item_ptr(0);466}468const value_type* data() const {469return item_ptr(0);470}471};473#endif // BITCOIN_PREVECTOR_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.