user1095108 / ca Goto Github PK
View Code? Open in Web Editor NEWc++ circular array implementation
License: The Unlicense
c++ circular array implementation
License: The Unlicense
Hi,
I disagree with the push_front
semantics:
Line 464 in 5dae347
If it's full, the front does not move to prev, and we just overwrite the 1st element.
I always want front to move to prev (and have that as new 1st element, even if it means... overwriting the last element), so that the behaviour is analogue to push_back
and other implementations such as boost::circular_buffer
.
Hi!
I've just found a bug here:
Line 130 in 2e6ca1a
You can test it by modifying array.cpp
to the following
#include <iostream>
#include "array.hpp"
int main()
{
ca::array<int, 3> ca;
ca.push_front(1);
ca.push_back(2);
ca.push_back(3);
ca.push_front(4);
std::sort(ca.begin(), ca.end());
ca::array<int, 3> ca2;
ca2 = ca; // BUG HERE !!!!!!!!!!!!!!!!!!!!!!!!!!
ca::array<int, 3> &c = ca2; // to print EXPECTED values, modify to: ca::array<int, 3> &c = ca;
std::cout << "size: " << c.size() << std::endl;
std::cout << "full: " << c.full() << std::endl;
std::copy(c.cbegin(), c.cend(), std::ostream_iterator<int>(std::cout, "\n"));
c.insert(c.cend(), 5);
c.push_back(10);
std::cout << "size: " << c.size() << std::endl;
std::cout << "full: " << c.full() << std::endl;
ca::split(
c.cbegin(),
c.cend(),
[](auto const b, auto const e)
{
std::copy(b, e, std::ostream_iterator<int>(std::cout, "\n"));
}
);
c.erase(std::next(c.cbegin()));
std::cout << "size: " << c.size() << std::endl;
std::cout << "full: " << c.full() << std::endl;
std::for_each(
c.cbegin(),
c.cend(),
[](auto&& v) noexcept
{
std::cout << v << std::endl;
}
);
std::cout << std::distance(c.cbegin(), c.cend()) << " : " << c.size() << " " << c[0] << std::endl;
return 0;
}
Currently ca is unusable with stack-allocated buffers (where size is not know at compile time).
Example:
size_t num_el = 3;
//std::cout << "Enter number of elements: ";
//std::cin >> num_el;
int arr[num_el+1]{}; // alloca is available under gcc etc.
ca::array<int, num_el, ca::USER>ca(arr);
The problem is the 2nd template argument. For a user-defined buffer, this should not be a compile-time template argument, in my opinion; ... in order to allow what I want to do above.
(Another problem. I'm currently on a project that does not (yet) have C++20. )
Hi,
why do you use a for loop for operator[]
Line 261 in 9622c11
Isn't that super-slow for large i
?
Why not do something like this?
constexpr auto& operator[](size_type i) noexcept
{
assert(i < size());
const auto numTillEnd = N - (f_-a_); return (numTillEnd > i) ? *(f_+i) : *(a_+(i-numTillEnd));
//const auto numTillEnd = &a_[N-1] - f_; return (numTillEnd >= i) ? *(f_+i) : *(a_+(i-numTillEnd-1));
//const auto numTillEnd = &a_[N] - f_; return (numTillEnd > i) ? *(f_+i) : *(a_+(i-numTillEnd)); // maybe not safe to take &a_[N] ??
}
Thanks.
(Maybe I'm missing some constexpr magic; but I cannot believe, that constexpr would eliminate a real run-time forloop in this case)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.