Common template for inline vector implementation.#245
Common template for inline vector implementation.#245lemur73 wants to merge 1 commit intoChia-Network:mainfrom
Conversation
|
This pull request fixes 1 alert when merging 51a5eee into 632c9d7 - view on LGTM.com fixed alerts:
|
|
This pull request fixes 1 alert when merging 9fa9f9f into 632c9d7 - view on LGTM.com fixed alerts:
|
|
this mostly looks like an improvement. However, when you make the element type a template argument I think you're going a bit too far. your One thing these classes need more urgently are tests though. |
src/bits.hpp
Outdated
| template <class item_type, class size_type_arg, unsigned capacity> | ||
| class InlineVector { | ||
| public: | ||
| typedef size_type_arg size_type; |
There was a problem hiding this comment.
the way this spells nowadays is:
| typedef size_type_arg size_type; | |
| using size_type = size_type_arg; |
src/bits.hpp
Outdated
|
|
||
| SmallVector& operator=(const SmallVector& other) | ||
| { | ||
| InlineVector& operator=(const InlineVector& other) { |
There was a problem hiding this comment.
while you're at it, this function should probably be ref-qualified.
| InlineVector& operator=(const InlineVector& other) { | |
| InlineVector& operator=(const InlineVector& other) & { |
src/bits.hpp
Outdated
| typedef uint32_t size_type; | ||
|
|
||
| ParkVector() noexcept { count_ = 0; } | ||
| InlineVector& operator=(const std::vector<item_type>& other) { |
There was a problem hiding this comment.
and so should this probably:
| InlineVector& operator=(const std::vector<item_type>& other) { | |
| InlineVector& operator=(const std::vector<item_type>& other) & { |
There was a problem hiding this comment.
One thing these classes need more urgently are tests though.
Updated with fixes from your comments and test.
PTAL
src/bits.hpp
Outdated
|
|
||
| // 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 | ||
| typedef InlineVector<uint64_t, uint16_t, 2048> ParkVector; |
There was a problem hiding this comment.
the modern way (and more readable in my opinion) is to use a using declaration.
| typedef InlineVector<uint64_t, uint16_t, 2048> ParkVector; | |
| using ParkVector = InlineVector<uint64_t, uint16_t, 2048>; |
There was a problem hiding this comment.
the modern way (and more readable in my opinion) is to use a
usingdeclaration.
Unfortunately, this doesn't work. Gives an error:
~/chiapos_up/src/bits.hpp:79: error: expected nested-name-specifier
using InlineVector<uint64_t, uint8_t, 10> SmallVector;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
There was a problem hiding this comment.
try this instead:
using ParkVector = InlineVector<uint64_t, uint16_t, 2048>;
There was a problem hiding this comment.
That worked, thanks.
Updated and uploaded.
Well, I have some use of this template argument in my next commits. Just prefer to keep uploads smaller to make it easy to understand and review.
I'll upload tests. |
src/bits.hpp
Outdated
|
|
||
| private: | ||
| item_type v_[capacity]; | ||
| size_type count_; |
There was a problem hiding this comment.
If you make this change, you don't have to define the constructor
| size_type count_; | |
| size_type count_ = 0; |
|
This pull request introduces 1 alert and fixes 1 when merging e94cf3f into 632c9d7 - view on LGTM.com new alerts:
fixed alerts:
|
|
This pull request fixes 1 alert when merging 8c67004 into 632c9d7 - view on LGTM.com fixed alerts:
|
|
This pull request fixes 1 alert when merging c71e3eb into 632c9d7 - view on LGTM.com fixed alerts:
|
| SmallVector() noexcept { count_ = 0; } | ||
|
|
||
| uint64_t& operator[](const uint16_t index) { return v_[index]; } | ||
| template <class item_type, class size_type_arg, unsigned capacity> |
There was a problem hiding this comment.
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:
static_assert(std::is_integral_t<item_type>, "InlineVector inly supports integral types");
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.
The template parameter cannot be an arbitrary type, since they're all constructed up-front they must be default constructable.
Yeah, I figured that out already in my next patch. Let's see if you like it ;)
Added static assert for now.
|
I think everything looks good apart from my last comment |
Allows simplier using inline vectors of different sizes and types. Signed-off-by: Ostap Slavking <[email protected]> If you like my commits cheer me with some chia here: xch16es77qggpwgzucqmmvhvgcueckmt9g3e7exa46vhmucz7z8j2a2spm7c7e
|
'This PR has been flagged as stale due to no activity for over 60 |
Allows simplier using inline vectors of different sizes and types.
Signed-off-by: Ostap Slavking [email protected]
If you like my commits cheer me with some chia here:
xch16es77qggpwgzucqmmvhvgcueckmt9g3e7exa46vhmucz7z8j2a2spm7c7e