NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#ifndef __MYVECTOR_H__
#define __MYVECTOR_H__

#include <utility>
#include <vector>

template <typename DataType>
class MyVector
{
private:
/* data */
size_t theSize; // the number of data elements the vector is currently holding
size_t theCapacity; // maximum data elements the vector can hold
DataType *data; // address of the data storage

public:

static const size_t SPARE_CAPACITY = 16; // initial capacity of the vector

// default constructor
explicit MyVector(size_t initSize = 0) :
theSize{initSize},
theCapacity{initSize + SPARE_CAPACITY}
{
//Creating the Data array, to be of type DataType, and of initial size
data = new DataType[theCapacity];
}

// copy constructor
MyVector(const MyVector & rhs) :
theSize{rhs.theSize},
theCapacity{rhs.theCapacity}
{
//Creating the Data array, to be of type DataType, and of initial size
data = new DataType[theCapacity];
//Copying each element over into new vector
for(int i = 0; i < theSize; i++){
data[i] = rhs.data[i];
}
}

// move constructor
MyVector(MyVector&& rhs):
theSize{rhs.theSize},
theCapacity{rhs.theCapacity},
data{rhs.data}
{
//Vector is already copied, setting original vector to be empty
rhs.data = nullptr;
rhs.theSize = 0;
rhs.theCapacity = 0;
}

// copy constructor from STL vector implementation
MyVector(const std::vector<DataType> & rhs) :
theSize{rhs.size()},
theCapacity{rhs.size() + SPARE_CAPACITY}
{
//Creating the Data array, to be of type DataType, and of initial size
data = new DataType[theCapacity];
//Copying each element over into new vector
for(int i = 0; i < theSize; i++){
data[i] = rhs.at(i);
}
}

// destructor
~MyVector(){
//deleting vector
delete [] data;
};

// copy assignment
MyVector & operator= (const MyVector& rhs)
{
//Copy Assignment
MyVector copy = rhs;
std::swap(*this, copy);
return(*this);
}

// move assignment
MyVector & operator= (MyVector && rhs)
{
//Move Assignment
std::swap(theSize, rhs.theSize);
std::swap(theCapacity, rhs.theCapacity);
std::swap(data, rhs.data);
return(*this);
}

// change the size of the array
void resize(size_t newSize)
{
//Checking if new Size is larger than capacity
if(newSize > theCapacity){
//if it is, raising capacity
reserve(2*newSize);
}
theSize = newSize;
}

// allocate more memory for the array
void reserve(size_t newCapacity)
{
//Reserving more capacity
if(newCapacity < theSize){
return;
}
DataType *tempArray = new DataType[newCapacity];
for(int i = 0; i < theSize; i++){
tempArray[i] = std::move(data[i]);
}
theCapacity = newCapacity;
std::swap(data,tempArray);
delete[] tempArray;
}

// data access operator (without bound checking)
DataType & operator[] (size_t index)
{
//returning without checking if in bounds
return(data[index]);
}

const DataType & operator[](size_t index) const
{
//returning without checking if in bounds
return(data[index]);
}

// check if the vector is empty; return TURE if the vector is empty
bool empty() const
{
return(theSize == 0);
}

// returns the size of the vector
size_t size() const
{
return(theSize);
}

// returns the capacity of the vector
size_t capacity() const
{
return(theCapacity);
}

// insert an data element to the end of the vector
void push_back(const DataType & x)
{
//pushing an element onto vector
if(theSize == theCapacity){
reserve(2 * (theSize + 1));
}
theSize += 1;
data[theSize] = x;

}

void push_back(DataType && x)
{
//pushing an element onto vector
if(theSize == theCapacity){
reserve(2 * (theSize + 1));
}
theSize += 1;
data[theSize] = std::move(x);

}

// append a vector as indicated by the parameter to the current vector
MyVector<DataType>& append(MyVector<DataType> && rhs)
{
if(theSize + rhs.theSize >= theCapacity){
reserve(2 * (theSize + rhs.theSize));
}
for(int i = theSize; i < (theSize + rhs.theSize); i++){
data[i] = rhs.data[i];
}
theSize += rhs.theSize;
return(*this);
}

// remove the last data element from the array
void pop_back()
{
theSize--;
}

// returns the last data elemtn from the array
const DataType& back() const
{
return data[theSize - 1];
}

// iterator implementation

typedef DataType* iterator;
typedef const DataType* const_iterator;

iterator begin()
{
return &data[0];
}

const_iterator begin() const
{
return &data[0];
}

iterator end()
{
return &data[size()];
}

const_iterator end() const
{
return &data[size()];
}

};


#endif // __MYVECTOR_H__Write a note in this area. It's really easy to share with others. Click here ...
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.