#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{ public: //! 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); //! 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; //! Display the array in a string string to_string() 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); //! Operator< bool operator<(const Array& array) const; //! Append an array to the current one Array append(const Array& array) const; }; //*********************** //* Auxiliary functions * //*********************** //! Comparison function for int16_t //! \param x a int16_t //! \param y a int16_t //! \return -1 is xy int cmp(int16_t x,int16_t y); //! Comparison function for Array //! \param A an Array //! \param B an Array //! \return -1 is AB template int cmp(const Array& A,const Array& B); //! Operator<< for Array template ostream& operator<<(std::ostream& os,const Array&); //! to_string function for Array template string to_string(const Array&); //************************* //* Function declarations * //************************* //---------------------- // Array::Array(size_t) //---------------------- template inline Array::Array(size_t _s):s(_s),array(new T[_s]){} //------------------------- // Array::Array(T*,size_t) //------------------------- template inline Array::Array(T* a,size_t _s):s(_s),array(a){} //---------------------------------------- // Array::Array(const initializer_list //---------------------------------------- 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; } } //------------------------------ // Array::Array(const Array& //------------------------------ template inline Array::Array(const Array& a):s(a.s),array(new T[s]){ memcpy(array,a.array,s*sizeof(T)); } //------------------- // Array::Array&& //------------------- template inline Array::Array(Array&& a):s(a.s),array(a.array){ a.s=0; a.array=nullptr; } //------------------------------ // Array::Array(const list&) //------------------------------ 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; } } //------------------------- // Array::Array(stack&) //------------------------- 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; } } //----------------------------- // Array::Array(const set&) //----------------------------- 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; } } //----------------- // Array::~Array() //----------------- template inline Array::~Array(){ if(array!=nullptr) delete[] array; } //----------------------------------- // Array::operator=(const 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; } //------------------------------ // Array::operator=(Array&&) //------------------------------ 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; } //------------------- // Array::is_empty() //------------------- template inline bool Array::is_empty() const{ return s==0; } //--------------- // Array::size() //--------------- template inline size_t Array::size() const{ return s; } //------------------ // Array::to_string() //------------------ template string Array::to_string() const{ if(s==0) return "()"; string res="("+std::to_string(read(0)); for(size_t i=1;i inline const T* Array::begin() const{ return array; } //-------------- // Array::end() //-------------- template inline const T* Array::end() const{ return array+s; } //------------------------------- // Array::write(size_t,const T&) //------------------------------- 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 Array Array::append(const Array& arr) const{ Array res(s+arr.s); memcpy(res.array,array,s*sizeof(T)); memcpy(&res.array[s],arr.array,arr.s*sizeof(T)); return res; } //-------------------------------- // Array::operator<(const Array&) //-------------------------------- template bool Array::operator<(const Array& arr) const{ if(s==arr.s){ for(size_t i=0;i int cmp(const Array& A,const Array& B){ if(A.s==B.s){ for(size_t i=0;i&) //---------------------------- template inline string to_string(const Array& arr){ return arr.to_string(); } //------------------------------------------- // operator<<(ostram&,const Array&) //------------------------------------------- inline ostream& operator<<(ostream& os,const Array& 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<<']'; } //------------------------------------------ // operator<<(ostram&,const Array&) //------------------------------------------ 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<<']'; } //------------------------------------------ // operator<<(ostram&,const Array&) //------------------------------------------ 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