#ifndef Braids_array #define Braids_array #include #include /** * This file is part of Gomu. * * Copyright 2016 by Jean Fromentin * * Gomu is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Gomu is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Gomu. If not, see . */ #include #include #include #include #include using namespace std; //! Class for array template class Array{ protected: //! Size of the array size_t s; //! Internal data of the array T* array; //! Construct an array from a c++ array a of size s and own it Array(T* a,size_t s); public: //! Contruct an array of size s //! \param s size of the array Array(size_t s=0); //! Construct an array from an initialization list Array(const initializer_list& list); //! The copy contructor Array(const Array&); //! The move constructor Array(Array&&); //! Construct an array from a stack Array(stack& stack); //! Construct an array from a list Array(const list& list); //! Construct an array from a set Array(const set& set); //! Destructor ~Array(); //! Assignemnet operator with copy //! \param array Array to copy Array& operator=(const Array& array); //! Assignement operator with move //! \param array Array to move Array& operator=(Array&& array); //! Test is the array is empty //! \return true if empty flase otherwise bool is_empty() const; //! Return the size of the array$ size_t size() const; //! Return a pointer to the begin of the array const T* begin() const; //!* Return a pointer a pointer just after the end of the array const T* end() const; //! Write the a value at a given index //! \param i index where write //! \param v value to write void write(size_t i,const T& v); //! Return the value at a given index //! \param i index where read //! \param return a reference to the value at index i T& read(size_t i) const; //! Return the value at a given index //! \param i index where read //! \param return a reference to the value at index i const T& at(size_t i) const; //! Return the value at a given index //! \param i index where read //! \param return a reference to the value at index i const T& operator[](size_t i) const; //! Provide access to the the value at index i //! \param i index where look //! \param return a reference to the value at index i T& at(size_t i); //! Provide access to the the value at index i //! \param i index where look //! \param return a reference to the value at index i T& operator[](size_t i); //! Comparison function bool operator<(const Array& array) const; //! Display function template friend std::ostream& operator<<(std::ostream& os,const Array&); }; //************************* //* Function declarations * //************************* template inline Array::Array(size_t _s):s(_s),array(new T[_s]){} template inline Array::Array(T* a,size_t _s):s(_s),array(a){} template Array::Array(const initializer_list &list):s((int)list.size()),array(new T[s]){ T* ptr=array; for(auto it=list.begin();it!=list.end();++it){ *ptr=*it; ++ptr; } } template inline Array::Array(const Array& a):s(a.s),array(new T[s]){ memcpy(array,a.array,s*sizeof(T)); } template inline Array::Array(Array&& a):s(a.s),array(a.array){ a.s=0; a.array=nullptr; } template Array::Array(const list& l):s((int)l.size()),array(new T[s]){ T* ptr=array; for(typename list::const_iterator it=l.begin();it!=l.end();++it){ *ptr=*it; ++ptr; } } template Array::Array(stack& stack):s(stack.size()),array(new T[s]){ T* ptr=array; while(not stack.empty()){ *ptr=stack.top(); stack.pop(); ++ptr; } } template Array::Array(const set& l):s(l.size()),array(new T[s]){ T* ptr=array; for(typename set::const_iterator it=l.begin();it!=l.end();++it){ *ptr=*it; ++ptr; } } template inline Array::~Array(){ if(array!=nullptr) delete[] array; } template Array& Array::operator=(const Array& a){ if(this!=&a){ if(s!=a.s){ if(array!=nullptr) delete[] array; array=new T[a.s]; } s=a.s; memcpy(array,a.array,s*sizeof(T)); } return *this; } template Array& Array::operator=(Array&& a){ if(this!=&a){ if(array!=nullptr) delete[] array; s=a.s; a.s=0; array=a.array; a.array=nullptr; } return *this; } template inline bool Array::is_empty() const{ return s==0; } template inline size_t Array::size() const{ return s; } template inline const T* Array::begin() const{ return array; } template inline const T* Array::end() const{ return array+s; } template inline void Array::write(size_t i,const T& v){ assert(i inline T& Array::read(size_t i) const{ assert(i inline const T& Array::at(size_t i) const{ assert(i inline T& Array::at(size_t i){ assert(i inline const T& Array::operator[](size_t i) const{ assert(i inline T& Array::operator[](size_t i){ assert(i bool Array::operator<(const Array& arr) const{ if(s==arr.s){ for(size_t i=0;i& a){ os<<'['; if(not a.is_empty()){ const uint8_t* ptr=a.begin(); os<<(int)*ptr; for(++ptr;ptr!=a.end();++ptr) os<<','<<(int)*ptr; } return os<<']'; } inline ostream& operator<<(ostream& os,const Array& a){ os<<'['; if(not a.is_empty()){ const int8_t* ptr=a.begin(); os<<(int)*ptr; for(++ptr;ptr!=a.end();++ptr) os<<','<<(int)*ptr; } return os<<']'; } template ostream& operator<<(ostream& os,const Array& a){ os<<'['; if(not a.is_empty()){ const T* ptr=a.begin(); os<<*ptr; for(++ptr;ptr!=a.end();++ptr) os<<','<<*ptr; } return os<<']'; } #endif