Boost logo

Boost :

Subject: Re: [boost] GSoC '15 Enhanced Vector and Deque
From: Pranav Vaish (pranav.vaish_at_[hidden])
Date: 2015-03-14 07:39:15


On Fri, Mar 13, 2015 at 11:04 PM Rob Stewart <rob.stewart_at_[hidden]>
wrote:

> On March 12, 2015 2:39:58 PM EDT, Pranav Vaish <pranav.vaish_at_[hidden]>
> wrote:
> > Hello,
> >
> > My name is Pranav. I had sent an email on this mailing list earlier
> > regarding some doubt in the implementation of the programming
> > competency
> > test for this project. I have gone ahead and written some code.
> >
> > The code is incomplete. Iterator code has not yet been written and
> > container methods and some error handling is left. I would appreciate
> > it if
> > someone could review my code and tell me what else I will need to
> > implement.
>
> I've provided a few comments, but only as a bystander. I'm not invoked in
> GSoC.
>
Thanks. I very much appreciate them.

>
> > #ifndef BOOST_VECTOR
> > #define BOOST_VECTOR
> > #include<exception>
> > #include<iostream>
> >
> > struct exception_push_back_capacity_exceed : public std::exception
>
> That's quite the name. Wouldn't "capacity_exceeded" do

Changed it. Thanks

>
>
> {
> > const char * what () const throw ()
> > {
> > return "Vector Capacity Exceeded";
>
> Why title case?
>
> Not sure what that means. Could you elaborate slightly?

> > }
> > };
> >
> > namespace boost
> > {
> > template<typename T> class vector
> > {
> > public:
> > vector(int);
> > ~vector();
> > void push_back(T);
> > T operator[](const int& pos)
> > {
> > return *(mem_start+pos);
> > }
> >
> > private:
> > T* mem_start;
> > int size;
> > int capacity;
>
> I suggest a making convention to differentiate data members from other
> variables. I use a trailing underscore.
>
> This is something new I learned thanks to you. Amazing idea.

> > };
> >
> > template<typename T> vector<T>::vector(int capacity_constr)
>
> "_constr" is a strange convention to disambiguate parameters from other
> variables. I use a leading underscore.
>
> > {
> > size=0;
> > capacity=capacity_constr;
> > mem_start = new T[capacity];
>
> I'd prefer to see use of an initializer list.
>
> I was thinking the same first but I would like to change the code to throw
an exception if the value given is negative. So, I've just left it as it is
for now.

> > }
> [snip]
> > template<typename T> void vector<T>::push_back(T val)
> > {
> > try
> > {
> > if(size==(this->capacity))
> > {
> > throw exception_push_back_capacity_exceed();
> > }
> > mem_start[size++]=val;
> > }
> > catch(exception_push_back_capacity_exceed& e)
> > {
> > std::cout<<e.what()<<std::endl;
> > }
> > }
> > }
>
> Why in the world would you throw an exception from a conditional just to
> catch it after one statement and report an error? Why doesn't the caller
> get the exception?
>
> You could, of course, give the programmer the ability to ascertain the
> capacity and simply make staying within it a precondition of push_back().

I hadn't really used exceptions in C++ before. It did seem silly though. I
plan to add more exceptions so I've just left it the way it is. I'll have a
look at it in the end when I'm done implementing most of the features.

I have written the iterator class and most of it's methods. Again error
handling is left for the ++ and -- operators. Please do have a look at this
as well.

#ifndef BOOST_VECTOR
#define BOOST_VECTOR
#include<exception>
#include<iostream>

struct capacity_exceed : public std::exception
{
const char * what () const throw ()
{
return "Vector Capacity Exceeded";
}
};

namespace boost
{
template<typename T> class vector
{
public:
explicit vector(int);
~vector();
void push_back(const T&);
int size();
bool empty();
               T &operator[](const int& _pos)
               {
                    return *(mem_start_+_pos);
               }

class iterator
{
public:
iterator(int _index,vector<T>* _parent)
{
index_=_index;
parent_pointer_=_parent;
}
T& operator*()
{
return (*parent_pointer_)[index_];
}
T* operator->()
{
return parent_pointer_;
}
iterator operator++()
{
index_++;
return *this;
}
iterator operator++(int)
{
iterator orig_=*this;
index_++;
return orig_;
}

iterator operator--()
{
index_--;
return *this;
}
iterator operator--(int)
{
iterator orig_=*this;
index_--;
return orig_;
}

bool operator ==(const iterator& _comp)
{
if((_comp.index_==this->index_) and
(_comp.parent_pointer_==this->parent_pointer_))
return true;
else
return false;
}
bool operator !=(const iterator& _comp)
{
if((_comp.index_==this->index_) and
(_comp.parent_pointer_==this->parent_pointer_))
return false;
else
return true;
}

private:
int index_;
vector<T>* parent_pointer_;
};
iterator begin()
{
return iterator(0,this);
}
iterator end()
{
return iterator(size_,this);
}

private:
T* mem_start_;
int size_;
int capacity_;
};

template<typename T> vector<T>::vector(int _constr)
{
          size_=0;
capacity_=_constr;
mem_start_ = new T[capacity_];
}

template<typename T> vector<T>::~vector()
{
delete [] mem_start_;
}

template<typename T> void vector<T>::push_back(const T& _val)
{
try
{
if(size_==(this->capacity_))
{
throw capacity_exceed();
}
mem_start_[size_++]=_val;
}
catch(capacity_exceed& e)
{
               std::cout<<e.what()<<std::endl;
}
}

template<typename T> int vector<T>::size()
{
return size_;
}

template<typename T> bool vector<T>::empty()
{
if (size_==0)
return true;
else
return false;
}
}
#endif

Thanks for the suggestions Rob.

----
Pranav Vaish

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk