-
Notifications
You must be signed in to change notification settings - Fork 282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Common template for inline vector implementation. #245
Open
lemur73
wants to merge
1
commit into
Chia-Network:main
Choose a base branch
from
lemur73:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -27,61 +27,64 @@ | |||||
// 64 * 2^16. 2^17 values, each value can store 64 bits. | ||||||
#define kMaxSizeBits 8388608 | ||||||
|
||||||
// A stack vector of length 5, having the functions of std::vector needed for Bits. | ||||||
struct SmallVector { | ||||||
typedef uint16_t size_type; | ||||||
template <class item_type, class size_type_arg, unsigned capacity> | ||||||
class InlineVector { | ||||||
public: | ||||||
|
||||||
SmallVector() noexcept { count_ = 0; } | ||||||
static_assert(std::is_integral<item_type>::value, "InlineVector only supports integral types"); | ||||||
|
||||||
uint64_t& operator[](const uint16_t index) { return v_[index]; } | ||||||
typedef size_type_arg size_type; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the way this spells nowadays is:
Suggested change
|
||||||
|
||||||
uint64_t operator[](const uint16_t index) const { return v_[index]; } | ||||||
item_type& operator[](const size_type index) { | ||||||
assert(index < capacity); | ||||||
return v_[index]; | ||||||
} | ||||||
|
||||||
void push_back(uint64_t value) { v_[count_++] = value; } | ||||||
item_type operator[](const size_type index) const { | ||||||
assert(index < capacity); | ||||||
return v_[index]; | ||||||
} | ||||||
|
||||||
SmallVector& operator=(const SmallVector& other) | ||||||
{ | ||||||
void push_back(item_type value) { | ||||||
assert(count_ < capacity); | ||||||
v_[count_++] = value; | ||||||
} | ||||||
|
||||||
InlineVector& operator=(const InlineVector& other) & { | ||||||
count_ = other.count_; | ||||||
for (size_type i = 0; i < other.count_; i++) v_[i] = other.v_[i]; | ||||||
return (*this); | ||||||
} | ||||||
|
||||||
size_type size() const noexcept { return count_; } | ||||||
|
||||||
void resize(const size_type n) { count_ = n; } | ||||||
|
||||||
private: | ||||||
uint64_t v_[10]; | ||||||
size_type count_; | ||||||
}; | ||||||
|
||||||
// A stack vector of length 1024, having the functions of std::vector needed for Bits. | ||||||
// The max number of Bits that can be stored is 1024 * 64 | ||||||
struct ParkVector { | ||||||
typedef uint32_t size_type; | ||||||
|
||||||
ParkVector() noexcept { count_ = 0; } | ||||||
InlineVector& operator=(const std::vector<item_type>& other) & { | ||||||
assert(other.size() <= capacity); | ||||||
|
||||||
uint64_t& operator[](const uint32_t index) { return v_[index]; } | ||||||
|
||||||
uint64_t operator[](const uint32_t index) const { return v_[index]; } | ||||||
|
||||||
void push_back(uint64_t value) { v_[count_++] = value; } | ||||||
|
||||||
ParkVector& operator=(const ParkVector& other) | ||||||
{ | ||||||
count_ = other.count_; | ||||||
for (size_type i = 0; i < other.count_; i++) v_[i] = other.v_[i]; | ||||||
count_ = other.size(); | ||||||
for (size_type i = 0; i < static_cast<size_type>(other.size()); i++) v_[i] = other[i]; | ||||||
return (*this); | ||||||
} | ||||||
|
||||||
size_type size() const noexcept { return count_; } | ||||||
|
||||||
private: | ||||||
uint64_t v_[2048]; | ||||||
size_type count_; | ||||||
void resize(const size_type n) { | ||||||
assert(n <= capacity); | ||||||
count_ = n; | ||||||
} | ||||||
|
||||||
size_type max_size() const { return capacity; } | ||||||
|
||||||
private: | ||||||
item_type v_[capacity]; | ||||||
size_type count_ = 0; | ||||||
}; | ||||||
|
||||||
// A stack vector of length 10, having the functions of std::vector needed for Bits. | ||||||
using SmallVector = InlineVector<uint64_t, uint8_t, 10>; | ||||||
|
||||||
// A stack vector of length 2048, having the functions of std::vector needed for Bits. | ||||||
// The max number of Bits that can be stored is 2048 * 64 | ||||||
using ParkVector = InlineVector<uint64_t, uint16_t, 2048>; | ||||||
|
||||||
/* | ||||||
* This class represents an array of bits. These are stored in an | ||||||
* array of integers, allowing for efficient bit manipulations. The Bits class provides | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's my argument against making
item_type
a template parameter:As it stands in your patch, it's not correct. The template parameter cannot be an arbitrary type, since they're all constructed up-front they must be default constructable. In order to make this more correct, you would need something like:
Which brings me to my next point. We don't currently need this additional generality/complexity. I don't think it makes sense to make things more general/complex just for the sake of it (in fact, I would argue that keeping everything as specific as possible makes for easier-to-read programs).
Now, you say you'll need this template parameter in your next PR. So, let's punt this change until that PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I figured that out already in my next patch. Let's see if you like it ;)
Added static assert for now.