Making vector push-front improved to O(1) / 8192












-3















After working on it, my regenerating reserve vector may not be so bad after all. (In the question "44326712/speeding-vector-push-back", I presented a vector with a regenerating reserve, faster at push_back when reserve can't be predicted).



Deque may be cool, but the vector is faster. So, I ameliorated my Vector, with two reserves: one before the begin and another after the end, thus, enabling push_front in a vector very quickly (push_back is still better than a normal vector).



With a RESA of 8192, and 1000 push_front, I had this results (computer is a Lenovo Think Center):



Duration of old push_front == 199 ticks (insert(v.begin () , s))



Duration of new push_front == 5 ticks



What do you think about it?



#ifndef __VECTOR2_HPP
#define __VECTOR2_HPP
#include <vector>
#include <iostream>
#include "Concept.hpp"
#include <exception>
#include <algorithm>

template <typename T>
class Vector : public std::vector<T> {
public :
class Error : public std::exception {
public :
Error (std::string val = "") throw () : val_ (val) {}
~Error () throw () {}
const char* what () throw () {
std::string s ("Vector Error :");
s += val_;
return s.c_str();
}
private :
std::string val_;
};
typename std::vector<T>::iterator begin () {
typename std::vector<T>::iterator i (std::vector<T>::begin ());
i += lresa_;
return i;
}
typename std::vector<T>::iterator end () {
typename std::vector<T>::iterator i (std::vector<T>::end ());
i -= rresa_;
return i;
}
typename std::vector<T>::const_iterator begin () const {
typename std::vector<T>::const_iterator i (std::vector<T>::begin ());
i += lresa_;
return i;
}
typename std::vector<T>::const_iterator end () const {
typename std::vector<T>::const_iterator i (std::vector<T>::end ());
i -= rresa_;
return i;
}
typename std::vector<T>::reverse_iterator rbegin () {
typename std::vector<T>::reverse_iterator i (std::vector<T>::rbegin ());
i += rresa_;
return i;
}
typename std::vector<T>::reverse_iterator rend () {
typename std::vector<T>::reverse_iterator i (std::vector<T>::rend ());
i -= lresa_;
return i;
}
typename std::vector<T>::const_reverse_iterator rbegin () const {
typename std::vector<T>::const_reverse_iterator i (std::vector<T>::rbegin ());
i += rresa_;
return i;
}
typename std::vector<T>::const_reverse_iterator rend () const {
typename std::vector<T>::const_reverse_iterator i (std::vector<T>::rend ());
i -= lresa_;
return i;
}
Vector () : std::vector<T> (2*(size_t) Concept::RESA, T()), lresa_ ((size_t) Concept::RESA), rresa_ ((size_t) Concept::RESA) {
}
size_t size () const {
size_t s (std::vector<T>::size ());
s -= lresa_;
s -= rresa_;
return s;
}
Vector (int n, T t0) : std::vector<T> (n + 2*(size_t) Concept::RESA, t0), lresa_ ((size_t) Concept::RESA), rresa_ ((size_t) Concept::RESA) {
}
Vector (const Vector& v) : std::vector<T> (v), lresa_ (v.lresa_), rresa_ (v.rresa_) {
}
bool operator == (const Vector& v) const {
return std::equal (begin (), end (), v.begin ());
}
bool operator != (const Vector& v) const {
return !operator == (v);
}
T& operator (const size_t &s) {
return std::vector<T>::operator (s+lresa_);
}
void push_front (const T& t) {
if (!lresa_) {
std::vector<T>::insert (std::vector<T>::begin (), (size_t) Concept::RESA, t);
lresa_ = (size_t) Concept::RESA;
}
typename std::vector<T>::iterator i (std::vector<T>::begin ());
i += lresa_ -1;
*i = t;
--lresa_;
}
void push_back (const T& t) {
if (!rresa_) {
std::vector<T>::insert (std::vector<T>::end (), (size_t) Concept::RESA, t);
rresa_ = (size_t) Concept::RESA;
}
typename std::vector<T>::iterator i (std::vector<T>::end ());
i -= rresa_;
*i = t;
--rresa_;
}
//can be optimized,but that s not the topic
Vector& operator = (const Vector& v) {
std::vector<T>::operator = (v);
lresa_ = v.lresa_;
rresa_ = v.rresa_;
return *this;
}
//intelligent resize to do
//at the moment just stupid
void resize (size_t n) {
std::vector<T>::resize (n + lresa_ + rresa_);
}
private :
size_t lresa_;
size_t rresa_;
};
#endif









share|improve this question









New contributor




Saint-Martin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 2





    It cannot be O(1) as you sometime need to grow the vector and move items.. Maybe amortized O(1)?

    – Phil1970
    5 hours ago






  • 1





    Folks: Be nice to one another and to everyone else. Make critique not about the person, but about the code.

    – Vogel612
    5 hours ago






  • 2





    It would help to add links to your previous question(s). Also, it's unclear what Concept.hpp is; without it, we can't tell what Concept::RESA is. ("RESA" is not an obvious acronym; and then you use it again in lresa_ and rresa_. I don't know what those identifiers are meant to signify.)

    – Quuxplusone
    4 hours ago











  • Concept::RESA is a constant that should be smaller than stack size. It's the only thing declared in Concept.hpp. you may set it to 4 and test and then set it to 8K

    – Saint-Martin
    3 hours ago











  • lresa_ stands for left reserve and rresa_ stands for right reserve

    – Saint-Martin
    3 hours ago
















-3















After working on it, my regenerating reserve vector may not be so bad after all. (In the question "44326712/speeding-vector-push-back", I presented a vector with a regenerating reserve, faster at push_back when reserve can't be predicted).



Deque may be cool, but the vector is faster. So, I ameliorated my Vector, with two reserves: one before the begin and another after the end, thus, enabling push_front in a vector very quickly (push_back is still better than a normal vector).



With a RESA of 8192, and 1000 push_front, I had this results (computer is a Lenovo Think Center):



Duration of old push_front == 199 ticks (insert(v.begin () , s))



Duration of new push_front == 5 ticks



What do you think about it?



#ifndef __VECTOR2_HPP
#define __VECTOR2_HPP
#include <vector>
#include <iostream>
#include "Concept.hpp"
#include <exception>
#include <algorithm>

template <typename T>
class Vector : public std::vector<T> {
public :
class Error : public std::exception {
public :
Error (std::string val = "") throw () : val_ (val) {}
~Error () throw () {}
const char* what () throw () {
std::string s ("Vector Error :");
s += val_;
return s.c_str();
}
private :
std::string val_;
};
typename std::vector<T>::iterator begin () {
typename std::vector<T>::iterator i (std::vector<T>::begin ());
i += lresa_;
return i;
}
typename std::vector<T>::iterator end () {
typename std::vector<T>::iterator i (std::vector<T>::end ());
i -= rresa_;
return i;
}
typename std::vector<T>::const_iterator begin () const {
typename std::vector<T>::const_iterator i (std::vector<T>::begin ());
i += lresa_;
return i;
}
typename std::vector<T>::const_iterator end () const {
typename std::vector<T>::const_iterator i (std::vector<T>::end ());
i -= rresa_;
return i;
}
typename std::vector<T>::reverse_iterator rbegin () {
typename std::vector<T>::reverse_iterator i (std::vector<T>::rbegin ());
i += rresa_;
return i;
}
typename std::vector<T>::reverse_iterator rend () {
typename std::vector<T>::reverse_iterator i (std::vector<T>::rend ());
i -= lresa_;
return i;
}
typename std::vector<T>::const_reverse_iterator rbegin () const {
typename std::vector<T>::const_reverse_iterator i (std::vector<T>::rbegin ());
i += rresa_;
return i;
}
typename std::vector<T>::const_reverse_iterator rend () const {
typename std::vector<T>::const_reverse_iterator i (std::vector<T>::rend ());
i -= lresa_;
return i;
}
Vector () : std::vector<T> (2*(size_t) Concept::RESA, T()), lresa_ ((size_t) Concept::RESA), rresa_ ((size_t) Concept::RESA) {
}
size_t size () const {
size_t s (std::vector<T>::size ());
s -= lresa_;
s -= rresa_;
return s;
}
Vector (int n, T t0) : std::vector<T> (n + 2*(size_t) Concept::RESA, t0), lresa_ ((size_t) Concept::RESA), rresa_ ((size_t) Concept::RESA) {
}
Vector (const Vector& v) : std::vector<T> (v), lresa_ (v.lresa_), rresa_ (v.rresa_) {
}
bool operator == (const Vector& v) const {
return std::equal (begin (), end (), v.begin ());
}
bool operator != (const Vector& v) const {
return !operator == (v);
}
T& operator (const size_t &s) {
return std::vector<T>::operator (s+lresa_);
}
void push_front (const T& t) {
if (!lresa_) {
std::vector<T>::insert (std::vector<T>::begin (), (size_t) Concept::RESA, t);
lresa_ = (size_t) Concept::RESA;
}
typename std::vector<T>::iterator i (std::vector<T>::begin ());
i += lresa_ -1;
*i = t;
--lresa_;
}
void push_back (const T& t) {
if (!rresa_) {
std::vector<T>::insert (std::vector<T>::end (), (size_t) Concept::RESA, t);
rresa_ = (size_t) Concept::RESA;
}
typename std::vector<T>::iterator i (std::vector<T>::end ());
i -= rresa_;
*i = t;
--rresa_;
}
//can be optimized,but that s not the topic
Vector& operator = (const Vector& v) {
std::vector<T>::operator = (v);
lresa_ = v.lresa_;
rresa_ = v.rresa_;
return *this;
}
//intelligent resize to do
//at the moment just stupid
void resize (size_t n) {
std::vector<T>::resize (n + lresa_ + rresa_);
}
private :
size_t lresa_;
size_t rresa_;
};
#endif









share|improve this question









New contributor




Saint-Martin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 2





    It cannot be O(1) as you sometime need to grow the vector and move items.. Maybe amortized O(1)?

    – Phil1970
    5 hours ago






  • 1





    Folks: Be nice to one another and to everyone else. Make critique not about the person, but about the code.

    – Vogel612
    5 hours ago






  • 2





    It would help to add links to your previous question(s). Also, it's unclear what Concept.hpp is; without it, we can't tell what Concept::RESA is. ("RESA" is not an obvious acronym; and then you use it again in lresa_ and rresa_. I don't know what those identifiers are meant to signify.)

    – Quuxplusone
    4 hours ago











  • Concept::RESA is a constant that should be smaller than stack size. It's the only thing declared in Concept.hpp. you may set it to 4 and test and then set it to 8K

    – Saint-Martin
    3 hours ago











  • lresa_ stands for left reserve and rresa_ stands for right reserve

    – Saint-Martin
    3 hours ago














-3












-3








-3








After working on it, my regenerating reserve vector may not be so bad after all. (In the question "44326712/speeding-vector-push-back", I presented a vector with a regenerating reserve, faster at push_back when reserve can't be predicted).



Deque may be cool, but the vector is faster. So, I ameliorated my Vector, with two reserves: one before the begin and another after the end, thus, enabling push_front in a vector very quickly (push_back is still better than a normal vector).



With a RESA of 8192, and 1000 push_front, I had this results (computer is a Lenovo Think Center):



Duration of old push_front == 199 ticks (insert(v.begin () , s))



Duration of new push_front == 5 ticks



What do you think about it?



#ifndef __VECTOR2_HPP
#define __VECTOR2_HPP
#include <vector>
#include <iostream>
#include "Concept.hpp"
#include <exception>
#include <algorithm>

template <typename T>
class Vector : public std::vector<T> {
public :
class Error : public std::exception {
public :
Error (std::string val = "") throw () : val_ (val) {}
~Error () throw () {}
const char* what () throw () {
std::string s ("Vector Error :");
s += val_;
return s.c_str();
}
private :
std::string val_;
};
typename std::vector<T>::iterator begin () {
typename std::vector<T>::iterator i (std::vector<T>::begin ());
i += lresa_;
return i;
}
typename std::vector<T>::iterator end () {
typename std::vector<T>::iterator i (std::vector<T>::end ());
i -= rresa_;
return i;
}
typename std::vector<T>::const_iterator begin () const {
typename std::vector<T>::const_iterator i (std::vector<T>::begin ());
i += lresa_;
return i;
}
typename std::vector<T>::const_iterator end () const {
typename std::vector<T>::const_iterator i (std::vector<T>::end ());
i -= rresa_;
return i;
}
typename std::vector<T>::reverse_iterator rbegin () {
typename std::vector<T>::reverse_iterator i (std::vector<T>::rbegin ());
i += rresa_;
return i;
}
typename std::vector<T>::reverse_iterator rend () {
typename std::vector<T>::reverse_iterator i (std::vector<T>::rend ());
i -= lresa_;
return i;
}
typename std::vector<T>::const_reverse_iterator rbegin () const {
typename std::vector<T>::const_reverse_iterator i (std::vector<T>::rbegin ());
i += rresa_;
return i;
}
typename std::vector<T>::const_reverse_iterator rend () const {
typename std::vector<T>::const_reverse_iterator i (std::vector<T>::rend ());
i -= lresa_;
return i;
}
Vector () : std::vector<T> (2*(size_t) Concept::RESA, T()), lresa_ ((size_t) Concept::RESA), rresa_ ((size_t) Concept::RESA) {
}
size_t size () const {
size_t s (std::vector<T>::size ());
s -= lresa_;
s -= rresa_;
return s;
}
Vector (int n, T t0) : std::vector<T> (n + 2*(size_t) Concept::RESA, t0), lresa_ ((size_t) Concept::RESA), rresa_ ((size_t) Concept::RESA) {
}
Vector (const Vector& v) : std::vector<T> (v), lresa_ (v.lresa_), rresa_ (v.rresa_) {
}
bool operator == (const Vector& v) const {
return std::equal (begin (), end (), v.begin ());
}
bool operator != (const Vector& v) const {
return !operator == (v);
}
T& operator (const size_t &s) {
return std::vector<T>::operator (s+lresa_);
}
void push_front (const T& t) {
if (!lresa_) {
std::vector<T>::insert (std::vector<T>::begin (), (size_t) Concept::RESA, t);
lresa_ = (size_t) Concept::RESA;
}
typename std::vector<T>::iterator i (std::vector<T>::begin ());
i += lresa_ -1;
*i = t;
--lresa_;
}
void push_back (const T& t) {
if (!rresa_) {
std::vector<T>::insert (std::vector<T>::end (), (size_t) Concept::RESA, t);
rresa_ = (size_t) Concept::RESA;
}
typename std::vector<T>::iterator i (std::vector<T>::end ());
i -= rresa_;
*i = t;
--rresa_;
}
//can be optimized,but that s not the topic
Vector& operator = (const Vector& v) {
std::vector<T>::operator = (v);
lresa_ = v.lresa_;
rresa_ = v.rresa_;
return *this;
}
//intelligent resize to do
//at the moment just stupid
void resize (size_t n) {
std::vector<T>::resize (n + lresa_ + rresa_);
}
private :
size_t lresa_;
size_t rresa_;
};
#endif









share|improve this question









New contributor




Saint-Martin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












After working on it, my regenerating reserve vector may not be so bad after all. (In the question "44326712/speeding-vector-push-back", I presented a vector with a regenerating reserve, faster at push_back when reserve can't be predicted).



Deque may be cool, but the vector is faster. So, I ameliorated my Vector, with two reserves: one before the begin and another after the end, thus, enabling push_front in a vector very quickly (push_back is still better than a normal vector).



With a RESA of 8192, and 1000 push_front, I had this results (computer is a Lenovo Think Center):



Duration of old push_front == 199 ticks (insert(v.begin () , s))



Duration of new push_front == 5 ticks



What do you think about it?



#ifndef __VECTOR2_HPP
#define __VECTOR2_HPP
#include <vector>
#include <iostream>
#include "Concept.hpp"
#include <exception>
#include <algorithm>

template <typename T>
class Vector : public std::vector<T> {
public :
class Error : public std::exception {
public :
Error (std::string val = "") throw () : val_ (val) {}
~Error () throw () {}
const char* what () throw () {
std::string s ("Vector Error :");
s += val_;
return s.c_str();
}
private :
std::string val_;
};
typename std::vector<T>::iterator begin () {
typename std::vector<T>::iterator i (std::vector<T>::begin ());
i += lresa_;
return i;
}
typename std::vector<T>::iterator end () {
typename std::vector<T>::iterator i (std::vector<T>::end ());
i -= rresa_;
return i;
}
typename std::vector<T>::const_iterator begin () const {
typename std::vector<T>::const_iterator i (std::vector<T>::begin ());
i += lresa_;
return i;
}
typename std::vector<T>::const_iterator end () const {
typename std::vector<T>::const_iterator i (std::vector<T>::end ());
i -= rresa_;
return i;
}
typename std::vector<T>::reverse_iterator rbegin () {
typename std::vector<T>::reverse_iterator i (std::vector<T>::rbegin ());
i += rresa_;
return i;
}
typename std::vector<T>::reverse_iterator rend () {
typename std::vector<T>::reverse_iterator i (std::vector<T>::rend ());
i -= lresa_;
return i;
}
typename std::vector<T>::const_reverse_iterator rbegin () const {
typename std::vector<T>::const_reverse_iterator i (std::vector<T>::rbegin ());
i += rresa_;
return i;
}
typename std::vector<T>::const_reverse_iterator rend () const {
typename std::vector<T>::const_reverse_iterator i (std::vector<T>::rend ());
i -= lresa_;
return i;
}
Vector () : std::vector<T> (2*(size_t) Concept::RESA, T()), lresa_ ((size_t) Concept::RESA), rresa_ ((size_t) Concept::RESA) {
}
size_t size () const {
size_t s (std::vector<T>::size ());
s -= lresa_;
s -= rresa_;
return s;
}
Vector (int n, T t0) : std::vector<T> (n + 2*(size_t) Concept::RESA, t0), lresa_ ((size_t) Concept::RESA), rresa_ ((size_t) Concept::RESA) {
}
Vector (const Vector& v) : std::vector<T> (v), lresa_ (v.lresa_), rresa_ (v.rresa_) {
}
bool operator == (const Vector& v) const {
return std::equal (begin (), end (), v.begin ());
}
bool operator != (const Vector& v) const {
return !operator == (v);
}
T& operator (const size_t &s) {
return std::vector<T>::operator (s+lresa_);
}
void push_front (const T& t) {
if (!lresa_) {
std::vector<T>::insert (std::vector<T>::begin (), (size_t) Concept::RESA, t);
lresa_ = (size_t) Concept::RESA;
}
typename std::vector<T>::iterator i (std::vector<T>::begin ());
i += lresa_ -1;
*i = t;
--lresa_;
}
void push_back (const T& t) {
if (!rresa_) {
std::vector<T>::insert (std::vector<T>::end (), (size_t) Concept::RESA, t);
rresa_ = (size_t) Concept::RESA;
}
typename std::vector<T>::iterator i (std::vector<T>::end ());
i -= rresa_;
*i = t;
--rresa_;
}
//can be optimized,but that s not the topic
Vector& operator = (const Vector& v) {
std::vector<T>::operator = (v);
lresa_ = v.lresa_;
rresa_ = v.rresa_;
return *this;
}
//intelligent resize to do
//at the moment just stupid
void resize (size_t n) {
std::vector<T>::resize (n + lresa_ + rresa_);
}
private :
size_t lresa_;
size_t rresa_;
};
#endif






c++ c++11






share|improve this question









New contributor




Saint-Martin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Saint-Martin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 33 mins ago









Jamal

30.3k11116226




30.3k11116226






New contributor




Saint-Martin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 6 hours ago









Saint-MartinSaint-Martin

13




13




New contributor




Saint-Martin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Saint-Martin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Saint-Martin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 2





    It cannot be O(1) as you sometime need to grow the vector and move items.. Maybe amortized O(1)?

    – Phil1970
    5 hours ago






  • 1





    Folks: Be nice to one another and to everyone else. Make critique not about the person, but about the code.

    – Vogel612
    5 hours ago






  • 2





    It would help to add links to your previous question(s). Also, it's unclear what Concept.hpp is; without it, we can't tell what Concept::RESA is. ("RESA" is not an obvious acronym; and then you use it again in lresa_ and rresa_. I don't know what those identifiers are meant to signify.)

    – Quuxplusone
    4 hours ago











  • Concept::RESA is a constant that should be smaller than stack size. It's the only thing declared in Concept.hpp. you may set it to 4 and test and then set it to 8K

    – Saint-Martin
    3 hours ago











  • lresa_ stands for left reserve and rresa_ stands for right reserve

    – Saint-Martin
    3 hours ago














  • 2





    It cannot be O(1) as you sometime need to grow the vector and move items.. Maybe amortized O(1)?

    – Phil1970
    5 hours ago






  • 1





    Folks: Be nice to one another and to everyone else. Make critique not about the person, but about the code.

    – Vogel612
    5 hours ago






  • 2





    It would help to add links to your previous question(s). Also, it's unclear what Concept.hpp is; without it, we can't tell what Concept::RESA is. ("RESA" is not an obvious acronym; and then you use it again in lresa_ and rresa_. I don't know what those identifiers are meant to signify.)

    – Quuxplusone
    4 hours ago











  • Concept::RESA is a constant that should be smaller than stack size. It's the only thing declared in Concept.hpp. you may set it to 4 and test and then set it to 8K

    – Saint-Martin
    3 hours ago











  • lresa_ stands for left reserve and rresa_ stands for right reserve

    – Saint-Martin
    3 hours ago








2




2





It cannot be O(1) as you sometime need to grow the vector and move items.. Maybe amortized O(1)?

– Phil1970
5 hours ago





It cannot be O(1) as you sometime need to grow the vector and move items.. Maybe amortized O(1)?

– Phil1970
5 hours ago




1




1





Folks: Be nice to one another and to everyone else. Make critique not about the person, but about the code.

– Vogel612
5 hours ago





Folks: Be nice to one another and to everyone else. Make critique not about the person, but about the code.

– Vogel612
5 hours ago




2




2





It would help to add links to your previous question(s). Also, it's unclear what Concept.hpp is; without it, we can't tell what Concept::RESA is. ("RESA" is not an obvious acronym; and then you use it again in lresa_ and rresa_. I don't know what those identifiers are meant to signify.)

– Quuxplusone
4 hours ago





It would help to add links to your previous question(s). Also, it's unclear what Concept.hpp is; without it, we can't tell what Concept::RESA is. ("RESA" is not an obvious acronym; and then you use it again in lresa_ and rresa_. I don't know what those identifiers are meant to signify.)

– Quuxplusone
4 hours ago













Concept::RESA is a constant that should be smaller than stack size. It's the only thing declared in Concept.hpp. you may set it to 4 and test and then set it to 8K

– Saint-Martin
3 hours ago





Concept::RESA is a constant that should be smaller than stack size. It's the only thing declared in Concept.hpp. you may set it to 4 and test and then set it to 8K

– Saint-Martin
3 hours ago













lresa_ stands for left reserve and rresa_ stands for right reserve

– Saint-Martin
3 hours ago





lresa_ stands for left reserve and rresa_ stands for right reserve

– Saint-Martin
3 hours ago










1 Answer
1






active

oldest

votes


















1














Start by adding a type for you base class:



typedef std::vector<T> base;


This can be used to simplify many of your declarations within your code.



Your various begin/end functions can be simplified



auto begin() {
return base::begin() + lresa_;
}


Unlike vector, your reserved areas contain default constructed objects. While this is not an issue for basic types like int, for more complicated types this can result in a performance hit and additional memory consumption.



Your operator== compares the contents of the reserve areas, which can cause incorrect comparison results. An easy way this can happen is if an element is added then removed (although you do not have any form of remove). Your reserve can be used as a remove proxy, and will leave valid constructed objects in your reserve area if the vector is shrunk.



You lack a operator const function.



How will you handle a move constructor?



You can improve your spacing/formatting. Having a space between a function name and the parenthesis is pretty uncommon and (IMHO) makes it harder to read. The overall indentation level is a bit shallow (3 or 4 spaces is more typical).






share|improve this answer























    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "196"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    Saint-Martin is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211398%2fmaking-vector-push-front-improved-to-o1-8192%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Start by adding a type for you base class:



    typedef std::vector<T> base;


    This can be used to simplify many of your declarations within your code.



    Your various begin/end functions can be simplified



    auto begin() {
    return base::begin() + lresa_;
    }


    Unlike vector, your reserved areas contain default constructed objects. While this is not an issue for basic types like int, for more complicated types this can result in a performance hit and additional memory consumption.



    Your operator== compares the contents of the reserve areas, which can cause incorrect comparison results. An easy way this can happen is if an element is added then removed (although you do not have any form of remove). Your reserve can be used as a remove proxy, and will leave valid constructed objects in your reserve area if the vector is shrunk.



    You lack a operator const function.



    How will you handle a move constructor?



    You can improve your spacing/formatting. Having a space between a function name and the parenthesis is pretty uncommon and (IMHO) makes it harder to read. The overall indentation level is a bit shallow (3 or 4 spaces is more typical).






    share|improve this answer




























      1














      Start by adding a type for you base class:



      typedef std::vector<T> base;


      This can be used to simplify many of your declarations within your code.



      Your various begin/end functions can be simplified



      auto begin() {
      return base::begin() + lresa_;
      }


      Unlike vector, your reserved areas contain default constructed objects. While this is not an issue for basic types like int, for more complicated types this can result in a performance hit and additional memory consumption.



      Your operator== compares the contents of the reserve areas, which can cause incorrect comparison results. An easy way this can happen is if an element is added then removed (although you do not have any form of remove). Your reserve can be used as a remove proxy, and will leave valid constructed objects in your reserve area if the vector is shrunk.



      You lack a operator const function.



      How will you handle a move constructor?



      You can improve your spacing/formatting. Having a space between a function name and the parenthesis is pretty uncommon and (IMHO) makes it harder to read. The overall indentation level is a bit shallow (3 or 4 spaces is more typical).






      share|improve this answer


























        1












        1








        1







        Start by adding a type for you base class:



        typedef std::vector<T> base;


        This can be used to simplify many of your declarations within your code.



        Your various begin/end functions can be simplified



        auto begin() {
        return base::begin() + lresa_;
        }


        Unlike vector, your reserved areas contain default constructed objects. While this is not an issue for basic types like int, for more complicated types this can result in a performance hit and additional memory consumption.



        Your operator== compares the contents of the reserve areas, which can cause incorrect comparison results. An easy way this can happen is if an element is added then removed (although you do not have any form of remove). Your reserve can be used as a remove proxy, and will leave valid constructed objects in your reserve area if the vector is shrunk.



        You lack a operator const function.



        How will you handle a move constructor?



        You can improve your spacing/formatting. Having a space between a function name and the parenthesis is pretty uncommon and (IMHO) makes it harder to read. The overall indentation level is a bit shallow (3 or 4 spaces is more typical).






        share|improve this answer













        Start by adding a type for you base class:



        typedef std::vector<T> base;


        This can be used to simplify many of your declarations within your code.



        Your various begin/end functions can be simplified



        auto begin() {
        return base::begin() + lresa_;
        }


        Unlike vector, your reserved areas contain default constructed objects. While this is not an issue for basic types like int, for more complicated types this can result in a performance hit and additional memory consumption.



        Your operator== compares the contents of the reserve areas, which can cause incorrect comparison results. An easy way this can happen is if an element is added then removed (although you do not have any form of remove). Your reserve can be used as a remove proxy, and will leave valid constructed objects in your reserve area if the vector is shrunk.



        You lack a operator const function.



        How will you handle a move constructor?



        You can improve your spacing/formatting. Having a space between a function name and the parenthesis is pretty uncommon and (IMHO) makes it harder to read. The overall indentation level is a bit shallow (3 or 4 spaces is more typical).







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 2 hours ago









        1201ProgramAlarm1201ProgramAlarm

        3,0602823




        3,0602823






















            Saint-Martin is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            Saint-Martin is a new contributor. Be nice, and check out our Code of Conduct.













            Saint-Martin is a new contributor. Be nice, and check out our Code of Conduct.












            Saint-Martin is a new contributor. Be nice, and check out our Code of Conduct.
















            Thanks for contributing an answer to Code Review Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211398%2fmaking-vector-push-front-improved-to-o1-8192%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

            is 'sed' thread safe

            How to make a Squid Proxy server?