array.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. public:
  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. //! Contruct an array of size s
  40. //! \param s size of the array
  41. Array(size_t s=0);
  42. //! Construct an array from an initialization list
  43. Array(const initializer_list<T>& list);
  44. //! The copy contructor
  45. Array(const Array<T>&);
  46. //! The move constructor
  47. Array(Array<T>&&);
  48. //! Construct an array from a stack
  49. Array(stack<T>& stack);
  50. //! Construct an array from a list
  51. Array(const list<T>& list);
  52. //! Construct an array from a set
  53. Array(const set<T>& set);
  54. //! Destructor
  55. ~Array();
  56. //! Assignemnet operator with copy
  57. //! \param array Array to copy
  58. Array& operator=(const Array& array);
  59. //! Assignement operator with move
  60. //! \param array Array to move
  61. Array& operator=(Array&& array);
  62. //! Test is the array is empty
  63. //! \return true if empty flase otherwise
  64. bool is_empty() const;
  65. //! Return the size of the array$
  66. size_t size() const;
  67. //! Display the array in a string
  68. string to_string() const;
  69. //! Return a pointer to the begin of the array
  70. const T* begin() const;
  71. //!* Return a pointer a pointer just after the end of the array
  72. const T* end() const;
  73. //! Write the a value at a given index
  74. //! \param i index where write
  75. //! \param v value to write
  76. void write(size_t i,const T& v);
  77. //! Return the value at a given index
  78. //! \param i index where read
  79. //! \param return a reference to the value at index i
  80. T& read(size_t i) const;
  81. //! Return the value at a given index
  82. //! \param i index where read
  83. //! \param return a reference to the value at index i
  84. const T& at(size_t i) const;
  85. //! Return the value at a given index
  86. //! \param i index where read
  87. //! \param return a reference to the value at index i
  88. const T& operator[](size_t i) const;
  89. //! Provide access to the the value at index i
  90. //! \param i index where look
  91. //! \param return a reference to the value at index i
  92. T& at(size_t i);
  93. //! Provide access to the the value at index i
  94. //! \param i index where look
  95. //! \param return a reference to the value at index i
  96. T& operator[](size_t i);
  97. //! Operator<
  98. bool operator<(const Array& array) const;
  99. //! Append an array to the current one
  100. Array append(const Array& array) const;
  101. };
  102. //***********************
  103. //* Auxiliary functions *
  104. //***********************
  105. //! Comparison function for int16_t
  106. //! \param x a int16_t
  107. //! \param y a int16_t
  108. //! \return -1 is x<y, 0 if x==y and 1 is x>y
  109. int cmp(int16_t x,int16_t y);
  110. //! Comparison function for Array
  111. //! \param A an Array
  112. //! \param B an Array
  113. //! \return -1 is A<B, 0 if A==B and 1 is A>B
  114. template<class T> int cmp(const Array<T>& A,const Array<T>& B);
  115. //! Operator<< for Array
  116. template<class T> ostream& operator<<(std::ostream& os,const Array<T>&);
  117. //! to_string function for Array
  118. template<class T> string to_string(const Array<T>&);
  119. //*************************
  120. //* Function declarations *
  121. //*************************
  122. //----------------------
  123. // Array::Array(size_t)
  124. //----------------------
  125. template<class T> inline
  126. Array<T>::Array(size_t _s):s(_s),array(new T[_s]){}
  127. //-------------------------
  128. // Array::Array(T*,size_t)
  129. //-------------------------
  130. template<class T> inline
  131. Array<T>::Array(T* a,size_t _s):s(_s),array(a){}
  132. //----------------------------------------
  133. // Array::Array(const initializer_list<T>
  134. //----------------------------------------
  135. template<class T>
  136. Array<T>::Array(const initializer_list<T> &list):s((int)list.size()),array(new T[s]){
  137. T* ptr=array;
  138. for(auto it=list.begin();it!=list.end();++it){
  139. *ptr=*it;
  140. ++ptr;
  141. }
  142. }
  143. //------------------------------
  144. // Array::Array(const Array<T>&
  145. //------------------------------
  146. template<class T> inline
  147. Array<T>::Array(const Array<T>& a):s(a.s),array(new T[s]){
  148. memcpy(array,a.array,s*sizeof(T));
  149. }
  150. //-------------------
  151. // Array::Array<T>&&
  152. //-------------------
  153. template<class T> inline
  154. Array<T>::Array(Array<T>&& a):s(a.s),array(a.array){
  155. a.s=0;
  156. a.array=nullptr;
  157. }
  158. //------------------------------
  159. // Array::Array(const list<T>&)
  160. //------------------------------
  161. template<class T>
  162. Array<T>::Array(const list<T>& l):s((int)l.size()),array(new T[s]){
  163. T* ptr=array;
  164. for(typename list<T>::const_iterator it=l.begin();it!=l.end();++it){
  165. *ptr=*it;
  166. ++ptr;
  167. }
  168. }
  169. //-------------------------
  170. // Array::Array(stack<T>&)
  171. //-------------------------
  172. template<class T>
  173. Array<T>::Array(stack<T>& stack):s(stack.size()),array(new T[s]){
  174. T* ptr=array;
  175. while(not stack.empty()){
  176. *ptr=stack.top();
  177. stack.pop();
  178. ++ptr;
  179. }
  180. }
  181. //-----------------------------
  182. // Array::Array(const set<T>&)
  183. //-----------------------------
  184. template<class T>
  185. Array<T>::Array(const set<T>& l):s(l.size()),array(new T[s]){
  186. T* ptr=array;
  187. for(typename set<T>::const_iterator it=l.begin();it!=l.end();++it){
  188. *ptr=*it;
  189. ++ptr;
  190. }
  191. }
  192. //-----------------
  193. // Array::~Array()
  194. //-----------------
  195. template<class T> inline
  196. Array<T>::~Array(){
  197. if(array!=nullptr) delete[] array;
  198. }
  199. //-----------------------------------
  200. // Array::operator=(const Array<T>&)
  201. //-----------------------------------
  202. template<class T> Array<T>&
  203. Array<T>::operator=(const Array<T>& a){
  204. if(this!=&a){
  205. if(s!=a.s){
  206. if(array!=nullptr) delete[] array;
  207. array=new T[a.s];
  208. }
  209. s=a.s;
  210. memcpy(array,a.array,s*sizeof(T));
  211. }
  212. return *this;
  213. }
  214. //------------------------------
  215. // Array::operator=(Array<T>&&)
  216. //------------------------------
  217. template<class T> Array<T>&
  218. Array<T>::operator=(Array<T>&& a){
  219. if(this!=&a){
  220. if(array!=nullptr) delete[] array;
  221. s=a.s;
  222. a.s=0;
  223. array=a.array;
  224. a.array=nullptr;
  225. }
  226. return *this;
  227. }
  228. //-------------------
  229. // Array::is_empty()
  230. //-------------------
  231. template<class T> inline bool
  232. Array<T>::is_empty() const{
  233. return s==0;
  234. }
  235. //---------------
  236. // Array::size()
  237. //---------------
  238. template<class T> inline size_t
  239. Array<T>::size() const{
  240. return s;
  241. }
  242. //------------------
  243. // Array::to_string()
  244. //------------------
  245. template<class T> string
  246. Array<T>::to_string() const{
  247. if(s==0) return "()";
  248. string res="("+std::to_string(read(0));
  249. for(size_t i=1;i<s;++i){
  250. res+=',';
  251. res+=std::to_string(read(i));
  252. }
  253. return res+")";
  254. }
  255. //----------------
  256. // Array::begin()
  257. //----------------
  258. template<class T> inline const T*
  259. Array<T>::begin() const{
  260. return array;
  261. }
  262. //--------------
  263. // Array::end()
  264. //--------------
  265. template<class T> inline const T*
  266. Array<T>::end() const{
  267. return array+s;
  268. }
  269. //-------------------------------
  270. // Array::write(size_t,const T&)
  271. //-------------------------------
  272. template<class T> inline void
  273. Array<T>::write(size_t i,const T& v){
  274. assert(i<s);
  275. array[i]=v;
  276. }
  277. //---------------------
  278. // Array::read(size_t)
  279. //---------------------
  280. template<class T> inline T&
  281. Array<T>::read(size_t i) const{
  282. assert(i<s);
  283. return array[i];
  284. }
  285. //-------------------
  286. // Array::at(size_t)
  287. //-------------------
  288. template<class T> inline const T&
  289. Array<T>::at(size_t i) const{
  290. assert(i<s);
  291. return array[i];
  292. }
  293. template<class T> inline T&
  294. Array<T>::at(size_t i){
  295. assert(i<s);
  296. return array[i];
  297. }
  298. //---------------------------
  299. // Array::operator[](size_t)
  300. //---------------------------
  301. template<class T> inline const T&
  302. Array<T>::operator[](size_t i) const{
  303. assert(i<s);
  304. return array[i];
  305. }
  306. template<class T> inline T&
  307. Array<T>::operator[](size_t i){
  308. assert(i<s);
  309. return array[i];
  310. }
  311. //-----------------------------
  312. // Array::append(const Array&)
  313. //-----------------------------
  314. template<class T> Array<T>
  315. Array<T>::append(const Array<T>& arr) const{
  316. Array<T> res(s+arr.s);
  317. memcpy(res.array,array,s*sizeof(T));
  318. memcpy(&res.array[s],arr.array,arr.s*sizeof(T));
  319. return res;
  320. }
  321. //--------------------------------
  322. // Array::operator<(const Array&)
  323. //--------------------------------
  324. template<class T> bool
  325. Array<T>::operator<(const Array<T>& arr) const{
  326. if(s==arr.s){
  327. for(size_t i=0;i<s;++i){
  328. if(array[i]!=arr.array[i]) return array[i]<arr.array[i];
  329. }
  330. return false;
  331. }
  332. return s<arr.s;
  333. }
  334. //***********************
  335. //* Auxiliary functions *
  336. //***********************
  337. //--------------------------
  338. // cmp(int16_t x,int16_t y)
  339. //--------------------------
  340. inline int
  341. cmp(int16_t x,int16_t y){
  342. if(x<y) return -1;
  343. if(x==y) return 0;
  344. return 1;
  345. }
  346. //------------------------------------
  347. // cmp(const Array& A,const Array& B)
  348. //------------------------------------
  349. template<class T> int
  350. cmp(const Array<T>& A,const Array<T>& B){
  351. if(A.s==B.s){
  352. for(size_t i=0;i<A.s;++i){
  353. int c=cmp(A.read(i),B.read(i));
  354. if(c!=0) return c;
  355. }
  356. return 0;
  357. }
  358. if(A.s<B.s) return -1;
  359. return 1;
  360. }
  361. //----------------------------
  362. // to_string(const Array<T>&)
  363. //----------------------------
  364. template<class T> inline string
  365. to_string(const Array<T>& arr){
  366. return arr.to_string();
  367. }
  368. //-------------------------------------------
  369. // operator<<(ostram&,const Array<uint8_t>&)
  370. //-------------------------------------------
  371. inline ostream&
  372. operator<<(ostream& os,const Array<uint8_t>& a){
  373. os<<'[';
  374. if(not a.is_empty()){
  375. const uint8_t* ptr=a.begin();
  376. os<<(int)*ptr;
  377. for(++ptr;ptr!=a.end();++ptr) os<<','<<(int)*ptr;
  378. }
  379. return os<<']';
  380. }
  381. //------------------------------------------
  382. // operator<<(ostram&,const Array<int8_t>&)
  383. //------------------------------------------
  384. inline ostream&
  385. operator<<(ostream& os,const Array<int8_t>& a){
  386. os<<'[';
  387. if(not a.is_empty()){
  388. const int8_t* ptr=a.begin();
  389. os<<(int)*ptr;
  390. for(++ptr;ptr!=a.end();++ptr) os<<','<<(int)*ptr;
  391. }
  392. return os<<']';
  393. }
  394. //------------------------------------------
  395. // operator<<(ostram&,const Array<int8_t>&)
  396. //------------------------------------------
  397. template<class T> ostream&
  398. operator<<(ostream& os,const Array<T>& a){
  399. os<<'[';
  400. if(not a.is_empty()){
  401. const T* ptr=a.begin();
  402. os<<*ptr;
  403. for(++ptr;ptr!=a.end();++ptr) os<<','<<*ptr;
  404. }
  405. return os<<']';
  406. }
  407. #endif