Merge bitcoin/bitcoin#29369: refactor: Allow CScript construction from any std::input_iterator

fa7b9b99a2 refactor: Require std::input_iterator for all InputIterator in prevector (MarcoFalke)
d444441900 refactor: Allow CScript construction from any std::input_iterator (MarcoFalke)

Pull request description:

  Currently only (pre)vector iterators and raw pointers are accepted. However, this makes it harder to construct from input iterators provided by other classes, such as `std::span`.

  Fix that.

ACKs for top commit:
  delta1:
    reACK fa7b9b9
  achow101:
    ACK fa7b9b99a2
  hodlinator:
    ACK fa7b9b99a2
  ryanofsky:
    Code review ACK fa7b9b99a2

Tree-SHA512: 2760861f8bce42fb27dc0825e61621cb157f1ac3619a0834df38eb8319b6dcf9de43d90397a4c160f43340880c1553df638848e9057a27c792214331243ef4a5
This commit is contained in:
Ava Chow 2024-08-27 14:09:56 -04:00
commit 99b06b7f1d
No known key found for this signature in database
GPG key ID: 17565732E08E5E41
2 changed files with 12 additions and 14 deletions

View file

@ -5,13 +5,13 @@
#ifndef BITCOIN_PREVECTOR_H
#define BITCOIN_PREVECTOR_H
#include <assert.h>
#include <cstdlib>
#include <stdint.h>
#include <string.h>
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <iterator>
#include <type_traits>
#include <utility>
@ -50,7 +50,6 @@ public:
T* ptr{};
public:
typedef Diff difference_type;
typedef T value_type;
typedef T* pointer;
typedef T& reference;
using element_type = T;
@ -102,7 +101,6 @@ public:
const T* ptr{};
public:
typedef Diff difference_type;
typedef const T value_type;
typedef const T* pointer;
typedef const T& reference;
using element_type = const T;
@ -212,7 +210,7 @@ private:
std::fill_n(dst, count, value);
}
template<typename InputIterator>
template <std::input_iterator InputIterator>
void fill(T* dst, InputIterator first, InputIterator last) {
while (first != last) {
new(static_cast<void*>(dst)) T(*first);
@ -231,7 +229,7 @@ public:
fill(item_ptr(0), n, val);
}
template<typename InputIterator>
template <std::input_iterator InputIterator>
void assign(InputIterator first, InputIterator last) {
size_type n = last - first;
clear();
@ -254,7 +252,7 @@ public:
fill(item_ptr(0), n, val);
}
template<typename InputIterator>
template <std::input_iterator InputIterator>
prevector(InputIterator first, InputIterator last) {
size_type n = last - first;
change_capacity(n);
@ -383,7 +381,7 @@ public:
fill(item_ptr(p), count, value);
}
template<typename InputIterator>
template <std::input_iterator InputIterator>
void insert(iterator pos, InputIterator first, InputIterator last) {
size_type p = pos - begin();
difference_type count = last - first;

View file

@ -429,11 +429,11 @@ protected:
}
return *this;
}
public:
CScript() = default;
CScript(const_iterator pbegin, const_iterator pend) : CScriptBase(pbegin, pend) { }
CScript(std::vector<unsigned char>::const_iterator pbegin, std::vector<unsigned char>::const_iterator pend) : CScriptBase(pbegin, pend) { }
CScript(const unsigned char* pbegin, const unsigned char* pend) : CScriptBase(pbegin, pend) { }
template <std::input_iterator InputIterator>
CScript(InputIterator first, InputIterator last) : CScriptBase{first, last} { }
SERIALIZE_METHODS(CScript, obj) { READWRITE(AsBase<CScriptBase>(obj)); }