array.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #ifndef Braids_array
  2. #define Braids_array
  3. #include <iostream>
  4. #include <list>
  5. /**
  6. * This file is part of Gomu.
  7. *
  8. * Copyright 2016 by Jean Fromentin <jean.fromentin@math.cnrs.fr>
  9. *
  10. * Gomu is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * Gomu is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with Gomu. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. #include <set>
  24. #include <stack>
  25. #include <initializer_list>
  26. #include <string.h>
  27. #include <cassert>
  28. using namespace std;
  29. //! Class for array
  30. template <class T>
  31. class Array{
  32. protected:
  33. //! Size of the array
  34. size_t s;
  35. //! Internal data of the array
  36. T* array;
  37. //! Construct an array from a c++ array a of size s and own it
  38. Array(T* a,size_t s);
  39. public:
  40. //! Contruct an array of size s
  41. //! \param s size of the array
  42. Array(size_t s=0);
  43. //! Construct an array from an initialization list
  44. Array(const initializer_list<T>& list);
  45. //! The copy contructor
  46. Array(const Array<T>&);
  47. //! The move constructor
  48. Array(Array<T>&&);
  49. //! Construct an array from a stack
  50. Array(stack<T>& stack);
  51. //! Construct an array from a list
  52. Array(const list<T>& list);
  53. //! Construct an array from a set
  54. Array(const set<T>& set);
  55. //! Destructor
  56. ~Array();
  57. //! Assignemnet operator with copy
  58. //! \param array Array to copy
  59. Array& operator=(const Array& array);
  60. //! Assignement operator with move
  61. //! \param array Array to move
  62. Array& operator=(Array&& array);
  63. //! Test is the array is empty
  64. //! \return true if empty flase otherwise
  65. bool is_empty() const;
  66. //! Return the size of the array$
  67. size_t size() const;
  68. //! Return a pointer to the begin of the array
  69. const T* begin() const;
  70. //!* Return a pointer a pointer just after the end of the array
  71. const T* end() const;
  72. //! Write the a value at a given index
  73. //! \param i index where write
  74. //! \param v value to write
  75. void write(size_t i,const T& v);
  76. //! Return the value at a given index
  77. //! \param i index where read
  78. //! \param return a reference to the value at index i
  79. T& read(size_t i) const;
  80. //! Return the value at a given index
  81. //! \param i index where read
  82. //! \param return a reference to the value at index i
  83. const T& at(size_t i) const;
  84. //! Return the value at a given index
  85. //! \param i index where read
  86. //! \param return a reference to the value at index i
  87. const T& operator[](size_t i) const;
  88. //! Provide access to the the value at index i
  89. //! \param i index where look
  90. //! \param return a reference to the value at index i
  91. T& at(size_t i);
  92. //! Provide access to the the value at index i
  93. //! \param i index where look
  94. //! \param return a reference to the value at index i
  95. T& operator[](size_t i);
  96. //! Comparison function
  97. bool operator<(const Array& array) const;
  98. //! Display function
  99. template<class U> friend std::ostream& operator<<(std::ostream& os,const Array<U>&);
  100. };
  101. //*************************
  102. //* Function declarations *
  103. //*************************
  104. template<class T> inline
  105. Array<T>::Array(size_t _s):s(_s),array(new T[_s]){}
  106. template<class T> inline
  107. Array<T>::Array(T* a,size_t _s):s(_s),array(a){}
  108. template<class T>
  109. Array<T>::Array(const initializer_list<T> &list):s((int)list.size()),array(new T[s]){
  110. T* ptr=array;
  111. for(auto it=list.begin();it!=list.end();++it){
  112. *ptr=*it;
  113. ++ptr;
  114. }
  115. }
  116. template<class T> inline
  117. Array<T>::Array(const Array<T>& a):s(a.s),array(new T[s]){
  118. memcpy(array,a.array,s*sizeof(T));
  119. }
  120. template<class T> inline
  121. Array<T>::Array(Array<T>&& a):s(a.s),array(a.array){
  122. a.s=0;
  123. a.array=nullptr;
  124. }
  125. template<class T>
  126. Array<T>::Array(const list<T>& l):s((int)l.size()),array(new T[s]){
  127. T* ptr=array;
  128. for(typename list<T>::const_iterator it=l.begin();it!=l.end();++it){
  129. *ptr=*it;
  130. ++ptr;
  131. }
  132. }
  133. template<class T>
  134. Array<T>::Array(stack<T>& stack):s(stack.size()),array(new T[s]){
  135. T* ptr=array;
  136. while(not stack.empty()){
  137. *ptr=stack.top();
  138. stack.pop();
  139. ++ptr;
  140. }
  141. }
  142. template<class T>
  143. Array<T>::Array(const set<T>& l):s(l.size()),array(new T[s]){
  144. T* ptr=array;
  145. for(typename set<T>::const_iterator it=l.begin();it!=l.end();++it){
  146. *ptr=*it;
  147. ++ptr;
  148. }
  149. }
  150. template<class T> inline
  151. Array<T>::~Array(){
  152. if(array!=nullptr) delete[] array;
  153. }
  154. template<class T> Array<T>&
  155. Array<T>::operator=(const Array<T>& a){
  156. if(this!=&a){
  157. if(s!=a.s){
  158. if(array!=nullptr) delete[] array;
  159. array=new T[a.s];
  160. }
  161. s=a.s;
  162. memcpy(array,a.array,s*sizeof(T));
  163. }
  164. return *this;
  165. }
  166. template<class T> Array<T>&
  167. Array<T>::operator=(Array<T>&& a){
  168. if(this!=&a){
  169. if(array!=nullptr) delete[] array;
  170. s=a.s;
  171. a.s=0;
  172. array=a.array;
  173. a.array=nullptr;
  174. }
  175. return *this;
  176. }
  177. template<class T> inline bool
  178. Array<T>::is_empty() const{
  179. return s==0;
  180. }
  181. template<class T> inline size_t
  182. Array<T>::size() const{
  183. return s;
  184. }
  185. template<class T> inline const T*
  186. Array<T>::begin() const{
  187. return array;
  188. }
  189. template<class T> inline const T*
  190. Array<T>::end() const{
  191. return array+s;
  192. }
  193. template<class T> inline void
  194. Array<T>::write(size_t i,const T& v){
  195. assert(i<s);
  196. array[i]=v;
  197. }
  198. template<class T> inline T&
  199. Array<T>::read(size_t i) const{
  200. assert(i<s);
  201. return array[i];
  202. }
  203. template<class T> inline const T&
  204. Array<T>::at(size_t i) const{
  205. assert(i<s);
  206. return array[i];
  207. }
  208. template<class T> inline T&
  209. Array<T>::at(size_t i){
  210. assert(i<s);
  211. return array[i];
  212. }
  213. template<class T> inline const T&
  214. Array<T>::operator[](size_t i) const{
  215. assert(i<s);
  216. return array[i];
  217. }
  218. template<class T> inline T&
  219. Array<T>::operator[](size_t i){
  220. assert(i<s);
  221. return array[i];
  222. }
  223. template<class T> bool
  224. Array<T>::operator<(const Array<T>& arr) const{
  225. if(s==arr.s){
  226. for(size_t i=0;i<s;++i){
  227. if(array[i]!=arr.array[i]) return array[i]<arr.array[i];
  228. }
  229. return false;
  230. }
  231. return s<arr.s;
  232. }
  233. inline ostream&
  234. operator<<(ostream& os,const Array<uint8_t>& a){
  235. os<<'[';
  236. if(not a.is_empty()){
  237. const uint8_t* ptr=a.begin();
  238. os<<(int)*ptr;
  239. for(++ptr;ptr!=a.end();++ptr) os<<','<<(int)*ptr;
  240. }
  241. return os<<']';
  242. }
  243. inline ostream&
  244. operator<<(ostream& os,const Array<int8_t>& a){
  245. os<<'[';
  246. if(not a.is_empty()){
  247. const int8_t* ptr=a.begin();
  248. os<<(int)*ptr;
  249. for(++ptr;ptr!=a.end();++ptr) os<<','<<(int)*ptr;
  250. }
  251. return os<<']';
  252. }
  253. template<class T> ostream&
  254. operator<<(ostream& os,const Array<T>& a){
  255. os<<'[';
  256. if(not a.is_empty()){
  257. const T* ptr=a.begin();
  258. os<<*ptr;
  259. for(++ptr;ptr!=a.end();++ptr) os<<','<<*ptr;
  260. }
  261. return os<<']';
  262. }
  263. #endif