Coder Social home page Coder Social logo

ca's People

Contributors

user1095108 avatar

Watchers

 avatar  avatar  avatar

Forkers

user706

ca's Issues

push_front semantics

Hi,

I disagree with the push_front semantics:

ca/array.hpp

Line 464 in 5dae347

*(full() ? f_ : f_ = prev(f_)) = std::forward<decltype(v)>(v);

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.

Bug in operator=

Hi!

I've just found a bug here:

ca/array.hpp

Line 130 in 2e6ca1a

constexpr array& operator=(array const& o)

You can test it by modifying array.cpp to the following

click for code
#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;
}

stack-allocated buffer (size specified during runtime, and then fixed)

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. )

operator[] with for loop

Hi,

why do you use a for loop for operator[]

ca/array.hpp

Line 261 in 9622c11

auto n(f_); for (; i; --i) n = S::next(a_, n); return *n;

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.