You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

526 lines
22 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2000-2015 Yves Renard
  4. This file is a part of GETFEM++
  5. Getfem++ is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU Lesser General Public License as published
  7. by the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version along with the GCC Runtime Library
  9. Exception either version 3.1 or (at your option) any later version.
  10. This program is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  13. License and GCC Runtime Library Exception for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with this program; if not, write to the Free Software Foundation,
  16. Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  17. As a special exception, you may use this file as it is a part of a free
  18. software library without restriction. Specifically, if other files
  19. instantiate templates or use macros or inline functions from this file,
  20. or you compile this file and link it with other files to produce an
  21. executable, this file does not by itself cause the resulting executable
  22. to be covered by the GNU Lesser General Public License. This exception
  23. does not however invalidate any other reasons why the executable file
  24. might be covered by the GNU Lesser General Public License.
  25. ===========================================================================*/
  26. #ifndef GMM_REF_H__
  27. #define GMM_REF_H__
  28. /** @file gmm_ref.h
  29. @author Yves Renard <Yves.Renard@insa-lyon.fr>
  30. @date August 26, 2000.
  31. * @brief Provide some simple pseudo-containers.
  32. *
  33. * WARNING : modifiying the container infirm the validity of references.
  34. */
  35. #include <iterator>
  36. #include "gmm_except.h"
  37. namespace gmm {
  38. /* ********************************************************************* */
  39. /* Simple reference. */
  40. /* ********************************************************************* */
  41. template<typename ITER> class tab_ref {
  42. protected :
  43. ITER begin_, end_;
  44. public :
  45. typedef typename std::iterator_traits<ITER>::value_type value_type;
  46. typedef typename std::iterator_traits<ITER>::pointer pointer;
  47. typedef typename std::iterator_traits<ITER>::pointer const_pointer;
  48. typedef typename std::iterator_traits<ITER>::reference reference;
  49. typedef typename std::iterator_traits<ITER>::reference const_reference;
  50. typedef typename std::iterator_traits<ITER>::difference_type
  51. difference_type;
  52. typedef ITER iterator;
  53. typedef ITER const_iterator;
  54. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  55. typedef std::reverse_iterator<iterator> reverse_iterator;
  56. typedef size_t size_type;
  57. bool empty(void) const { return begin_ == end_; }
  58. size_type size(void) const { return end_ - begin_; }
  59. const iterator &begin(void) { return begin_; }
  60. const const_iterator &begin(void) const { return begin_; }
  61. const iterator &end(void) { return end_; }
  62. const const_iterator &end(void) const { return end_; }
  63. reverse_iterator rbegin(void) { return reverse_iterator(end()); }
  64. const_reverse_iterator rbegin(void) const
  65. { return const_reverse_iterator(end()); }
  66. reverse_iterator rend(void) { return reverse_iterator(begin()); }
  67. const_reverse_iterator rend(void) const
  68. { return const_reverse_iterator(begin()); }
  69. reference front(void) { return *begin(); }
  70. const_reference front(void) const { return *begin(); }
  71. reference back(void) { return *(--(end())); }
  72. const_reference back(void) const { return *(--(end())); }
  73. void pop_front(void) { ++begin_; }
  74. const_reference operator [](size_type ii) const { return *(begin_ + ii);}
  75. reference operator [](size_type ii) { return *(begin_ + ii); }
  76. tab_ref(void) {}
  77. tab_ref(const ITER &b, const ITER &e) : begin_(b), end_(e) {}
  78. };
  79. /* ********************************************************************* */
  80. /* Reference with index. */
  81. /* ********************************************************************* */
  82. // template<typename ITER> struct tab_ref_index_iterator_
  83. // : public dynamic_array<size_t>::const_iterator
  84. // {
  85. // typedef typename std::iterator_traits<ITER>::value_type value_type;
  86. // typedef typename std::iterator_traits<ITER>::pointer pointer;
  87. // typedef typename std::iterator_traits<ITER>::reference reference;
  88. // typedef typename std::iterator_traits<ITER>::difference_type
  89. // difference_type;
  90. // typedef std::random_access_iterator_tag iterator_category;
  91. // typedef size_t size_type;
  92. // typedef dynamic_array<size_type>::const_iterator dnas_iterator_;
  93. // typedef tab_ref_index_iterator_<ITER> iterator;
  94. // ITER piter;
  95. // iterator operator ++(int)
  96. // { iterator tmp = *this; ++(*((dnas_iterator_ *)(this))); return tmp; }
  97. // iterator operator --(int)
  98. // { iterator tmp = *this; --(*((dnas_iterator_ *)(this))); return tmp; }
  99. // iterator &operator ++()
  100. // { ++(*((dnas_iterator_ *)(this))); return *this; }
  101. // iterator &operator --()
  102. // { --(*((dnas_iterator_ *)(this))); return *this; }
  103. // iterator &operator +=(difference_type i)
  104. // { (*((dnas_iterator_ *)(this))) += i; return *this; }
  105. // iterator &operator -=(difference_type i)
  106. // { (*((dnas_iterator_ *)(this))) -= i; return *this; }
  107. // iterator operator +(difference_type i) const
  108. // { iterator it = *this; return (it += i); }
  109. // iterator operator -(difference_type i) const
  110. // { iterator it = *this; return (it -= i); }
  111. // difference_type operator -(const iterator &i) const
  112. // { return *((dnas_iterator_ *)(this)) - *((dnas_iterator_ *)(&i)); }
  113. // reference operator *() const
  114. // { return *(piter + *((*((dnas_iterator_ *)(this))))); }
  115. // reference operator [](int ii)
  116. // { return *(piter + *((*((dnas_iterator_ *)(this+ii))))); }
  117. // bool operator ==(const iterator &i) const
  118. // {
  119. // return ((piter) == ((i.piter))
  120. // && *((dnas_iterator_ *)(this)) == *((*((dnas_iterator_ *)(this)))));
  121. // }
  122. // bool operator !=(const iterator &i) const
  123. // { return !(i == *this); }
  124. // bool operator < (const iterator &i) const
  125. // {
  126. // return ((piter) == ((i.piter))
  127. // && *((dnas_iterator_ *)(this)) < *((*((dnas_iterator_ *)(this)))));
  128. // }
  129. // tab_ref_index_iterator_(void) {}
  130. // tab_ref_index_iterator_(const ITER &iter, const dnas_iterator_ &dnas_iter)
  131. // : dnas_iterator_(dnas_iter), piter(iter) {}
  132. // };
  133. // template<typename ITER> class tab_ref_index
  134. // {
  135. // public :
  136. // typedef typename std::iterator_traits<ITER>::value_type value_type;
  137. // typedef typename std::iterator_traits<ITER>::pointer pointer;
  138. // typedef typename std::iterator_traits<ITER>::pointer const_pointer;
  139. // typedef typename std::iterator_traits<ITER>::reference reference;
  140. // typedef typename std::iterator_traits<ITER>::reference const_reference;
  141. // typedef typename std::iterator_traits<ITER>::difference_type
  142. // difference_type;
  143. // typedef size_t size_type;
  144. // typedef tab_ref_index_iterator_<ITER> iterator;
  145. // typedef iterator const_iterator;
  146. // typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  147. // typedef std::reverse_iterator<iterator> reverse_iterator;
  148. // protected :
  149. // ITER begin_;
  150. // dynamic_array<size_type> index_;
  151. // public :
  152. // bool empty(void) const { return index_.empty(); }
  153. // size_type size(void) const { return index_.size(); }
  154. // iterator begin(void) { return iterator(begin_, index_.begin()); }
  155. // const_iterator begin(void) const
  156. // { return iterator(begin_, index_.begin()); }
  157. // iterator end(void) { return iterator(begin_, index_.end()); }
  158. // const_iterator end(void) const { return iterator(begin_, index_.end()); }
  159. // reverse_iterator rbegin(void) { return reverse_iterator(end()); }
  160. // const_reverse_iterator rbegin(void) const
  161. // { return const_reverse_iterator(end()); }
  162. // reverse_iterator rend(void) { return reverse_iterator(begin()); }
  163. // const_reverse_iterator rend(void) const
  164. // { return const_reverse_iterator(begin()); }
  165. // reference front(void) { return *(begin_ +index_[0]); }
  166. // const_reference front(void) const { return *(begin_ +index_[0]); }
  167. // reference back(void) { return *(--(end())); }
  168. // const_reference back(void) const { return *(--(end())); }
  169. // tab_ref_index(void) {}
  170. // tab_ref_index(const ITER &b, const dynamic_array<size_type> &ind)
  171. // { begin_ = b; index_ = ind; }
  172. // // to be changed in a const_reference ?
  173. // value_type operator [](size_type ii) const
  174. // { return *(begin_ + index_[ii]);}
  175. // reference operator [](size_type ii) { return *(begin_ + index_[ii]); }
  176. // };
  177. /// iterator over a gmm::tab_ref_index_ref<ITER,ITER_INDEX>
  178. template<typename ITER, typename ITER_INDEX>
  179. struct tab_ref_index_ref_iterator_
  180. {
  181. typedef typename std::iterator_traits<ITER>::value_type value_type;
  182. typedef typename std::iterator_traits<ITER>::pointer pointer;
  183. typedef typename std::iterator_traits<ITER>::reference reference;
  184. typedef typename std::iterator_traits<ITER>::difference_type
  185. difference_type;
  186. typedef std::random_access_iterator_tag iterator_category;
  187. typedef tab_ref_index_ref_iterator_<ITER, ITER_INDEX> iterator;
  188. typedef size_t size_type;
  189. ITER piter;
  190. ITER_INDEX iter_index;
  191. iterator operator ++(int)
  192. { iterator tmp = *this; ++iter_index; return tmp; }
  193. iterator operator --(int)
  194. { iterator tmp = *this; --iter_index; return tmp; }
  195. iterator &operator ++() { ++iter_index; return *this; }
  196. iterator &operator --() { --iter_index; return *this; }
  197. iterator &operator +=(difference_type i)
  198. { iter_index += i; return *this; }
  199. iterator &operator -=(difference_type i)
  200. { iter_index -= i; return *this; }
  201. iterator operator +(difference_type i) const
  202. { iterator it = *this; return (it += i); }
  203. iterator operator -(difference_type i) const
  204. { iterator it = *this; return (it -= i); }
  205. difference_type operator -(const iterator &i) const
  206. { return iter_index - i.iter_index; }
  207. reference operator *() const
  208. { return *(piter + *iter_index); }
  209. reference operator [](size_type ii) const
  210. { return *(piter + *(iter_index+ii)); }
  211. bool operator ==(const iterator &i) const
  212. { return ((piter) == ((i.piter)) && iter_index == i.iter_index); }
  213. bool operator !=(const iterator &i) const { return !(i == *this); }
  214. bool operator < (const iterator &i) const
  215. { return ((piter) == ((i.piter)) && iter_index < i.iter_index); }
  216. tab_ref_index_ref_iterator_(void) {}
  217. tab_ref_index_ref_iterator_(const ITER &iter,
  218. const ITER_INDEX &dnas_iter)
  219. : piter(iter), iter_index(dnas_iter) {}
  220. };
  221. /**
  222. convenience template function for quick obtention of a indexed iterator
  223. without having to specify its (long) typename
  224. */
  225. template<typename ITER, typename ITER_INDEX>
  226. tab_ref_index_ref_iterator_<ITER,ITER_INDEX>
  227. index_ref_iterator(ITER it, ITER_INDEX it_i) {
  228. return tab_ref_index_ref_iterator_<ITER,ITER_INDEX>(it, it_i);
  229. }
  230. /** indexed array reference (given a container X, and a set of indexes I,
  231. this class provides a pseudo-container Y such that
  232. @code Y[i] = X[I[i]] @endcode
  233. */
  234. template<typename ITER, typename ITER_INDEX> class tab_ref_index_ref {
  235. public :
  236. typedef std::iterator_traits<ITER> traits_type;
  237. typedef typename traits_type::value_type value_type;
  238. typedef typename traits_type::pointer pointer;
  239. typedef typename traits_type::pointer const_pointer;
  240. typedef typename traits_type::reference reference;
  241. typedef typename traits_type::reference const_reference;
  242. typedef typename traits_type::difference_type difference_type;
  243. typedef size_t size_type;
  244. typedef tab_ref_index_ref_iterator_<ITER, ITER_INDEX> iterator;
  245. typedef iterator const_iterator;
  246. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  247. typedef std::reverse_iterator<iterator> reverse_iterator;
  248. protected :
  249. ITER begin_;
  250. ITER_INDEX index_begin_, index_end_;
  251. public :
  252. bool empty(void) const { return index_begin_ == index_end_; }
  253. size_type size(void) const { return index_end_ - index_begin_; }
  254. iterator begin(void) { return iterator(begin_, index_begin_); }
  255. const_iterator begin(void) const
  256. { return iterator(begin_, index_begin_); }
  257. iterator end(void) { return iterator(begin_, index_end_); }
  258. const_iterator end(void) const { return iterator(begin_, index_end_); }
  259. reverse_iterator rbegin(void) { return reverse_iterator(end()); }
  260. const_reverse_iterator rbegin(void) const
  261. { return const_reverse_iterator(end()); }
  262. reverse_iterator rend(void) { return reverse_iterator(begin()); }
  263. const_reverse_iterator rend(void) const
  264. { return const_reverse_iterator(begin()); }
  265. reference front(void) { return *(begin_ + *index_begin_); }
  266. const_reference front(void) const { return *(begin_ + *index_begin_); }
  267. reference back(void) { return *(--(end())); }
  268. const_reference back(void) const { return *(--(end())); }
  269. void pop_front(void) { ++index_begin_; }
  270. tab_ref_index_ref(void) {}
  271. tab_ref_index_ref(const ITER &b, const ITER_INDEX &bi,
  272. const ITER_INDEX &ei)
  273. : begin_(b), index_begin_(bi), index_end_(ei) {}
  274. // to be changed in a const_reference ?
  275. const_reference operator [](size_type ii) const
  276. { return *(begin_ + index_begin_[ii]);}
  277. reference operator [](size_type ii)
  278. { return *(begin_ + index_begin_[ii]); }
  279. };
  280. /* ********************************************************************* */
  281. /* Reference on regularly spaced elements. */
  282. /* ********************************************************************* */
  283. template<typename ITER> struct tab_ref_reg_spaced_iterator_ {
  284. typedef typename std::iterator_traits<ITER>::value_type value_type;
  285. typedef typename std::iterator_traits<ITER>::pointer pointer;
  286. typedef typename std::iterator_traits<ITER>::reference reference;
  287. typedef typename std::iterator_traits<ITER>::difference_type
  288. difference_type;
  289. typedef typename std::iterator_traits<ITER>::iterator_category
  290. iterator_category;
  291. typedef size_t size_type;
  292. typedef tab_ref_reg_spaced_iterator_<ITER> iterator;
  293. ITER it;
  294. size_type N, i;
  295. iterator operator ++(int) { iterator tmp = *this; i++; return tmp; }
  296. iterator operator --(int) { iterator tmp = *this; i--; return tmp; }
  297. iterator &operator ++() { i++; return *this; }
  298. iterator &operator --() { i--; return *this; }
  299. iterator &operator +=(difference_type ii) { i+=ii; return *this; }
  300. iterator &operator -=(difference_type ii) { i-=ii; return *this; }
  301. iterator operator +(difference_type ii) const
  302. { iterator itt = *this; return (itt += ii); }
  303. iterator operator -(difference_type ii) const
  304. { iterator itt = *this; return (itt -= ii); }
  305. difference_type operator -(const iterator &ii) const
  306. { return (N ? (it - ii.it) / N : 0) + i - ii.i; }
  307. reference operator *() const { return *(it + i*N); }
  308. reference operator [](size_type ii) const { return *(it + (i+ii)*N); }
  309. bool operator ==(const iterator &ii) const
  310. { return (*this - ii) == difference_type(0); }
  311. bool operator !=(const iterator &ii) const
  312. { return (*this - ii) != difference_type(0); }
  313. bool operator < (const iterator &ii) const
  314. { return (*this - ii) < difference_type(0); }
  315. tab_ref_reg_spaced_iterator_(void) {}
  316. tab_ref_reg_spaced_iterator_(const ITER &iter, size_type n, size_type ii)
  317. : it(iter), N(n), i(ii) { }
  318. };
  319. /**
  320. convenience template function for quick obtention of a strided iterator
  321. without having to specify its (long) typename
  322. */
  323. template<typename ITER> tab_ref_reg_spaced_iterator_<ITER>
  324. reg_spaced_iterator(ITER it, size_t stride) {
  325. return tab_ref_reg_spaced_iterator_<ITER>(it, stride);
  326. }
  327. /**
  328. provide a "strided" view a of container
  329. */
  330. template<typename ITER> class tab_ref_reg_spaced {
  331. public :
  332. typedef typename std::iterator_traits<ITER>::value_type value_type;
  333. typedef typename std::iterator_traits<ITER>::pointer pointer;
  334. typedef typename std::iterator_traits<ITER>::pointer const_pointer;
  335. typedef typename std::iterator_traits<ITER>::reference reference;
  336. typedef typename std::iterator_traits<ITER>::reference const_reference;
  337. typedef typename std::iterator_traits<ITER>::difference_type
  338. difference_type;
  339. typedef size_t size_type;
  340. typedef tab_ref_reg_spaced_iterator_<ITER> iterator;
  341. typedef iterator const_iterator;
  342. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  343. typedef std::reverse_iterator<iterator> reverse_iterator;
  344. protected :
  345. ITER begin_;
  346. size_type N, size_;
  347. public :
  348. bool empty(void) const { return size_ == 0; }
  349. size_type size(void) const { return size_; }
  350. iterator begin(void) { return iterator(begin_, N, 0); }
  351. const_iterator begin(void) const { return iterator(begin_, N, 0); }
  352. iterator end(void) { return iterator(begin_, N, size_); }
  353. const_iterator end(void) const { return iterator(begin_, N, size_); }
  354. reverse_iterator rbegin(void) { return reverse_iterator(end()); }
  355. const_reverse_iterator rbegin(void) const
  356. { return const_reverse_iterator(end()); }
  357. reverse_iterator rend(void) { return reverse_iterator(begin()); }
  358. const_reverse_iterator rend(void) const
  359. { return const_reverse_iterator(begin()); }
  360. reference front(void) { return *begin_; }
  361. const_reference front(void) const { return *begin_; }
  362. reference back(void) { return *(begin_ + N * (size_-1)); }
  363. const_reference back(void) const { return *(begin_ + N * (size_-1)); }
  364. void pop_front(void) { begin_ += N; }
  365. tab_ref_reg_spaced(void) {}
  366. tab_ref_reg_spaced(const ITER &b, size_type n, size_type s)
  367. : begin_(b), N(n), size_(s) {}
  368. const_reference operator [](size_type ii) const
  369. { return *(begin_ + ii * N);}
  370. reference operator [](size_type ii) { return *(begin_ + ii * N); }
  371. };
  372. /// iterator over a tab_ref_with_selection
  373. template<typename ITER, typename COND>
  374. struct tab_ref_with_selection_iterator_ : public ITER {
  375. typedef typename std::iterator_traits<ITER>::value_type value_type;
  376. typedef typename std::iterator_traits<ITER>::pointer pointer;
  377. typedef typename std::iterator_traits<ITER>::reference reference;
  378. typedef typename std::iterator_traits<ITER>::difference_type
  379. difference_type;
  380. typedef std::forward_iterator_tag iterator_category;
  381. typedef tab_ref_with_selection_iterator_<ITER, COND> iterator;
  382. const COND cond;
  383. void forward(void) { while (!(cond)(*this)) ITER::operator ++(); }
  384. iterator &operator ++()
  385. { ITER::operator ++(); forward(); return *this; }
  386. iterator operator ++(int)
  387. { iterator tmp = *this; ++(*this); return tmp; }
  388. tab_ref_with_selection_iterator_(void) {}
  389. tab_ref_with_selection_iterator_(const ITER &iter, const COND c)
  390. : ITER(iter), cond(c) {}
  391. };
  392. /**
  393. given a container X and a predicate P, provide pseudo-container Y
  394. of all elements of X such that P(X[i]).
  395. */
  396. template<typename ITER, typename COND> class tab_ref_with_selection {
  397. protected :
  398. ITER begin_, end_;
  399. COND cond;
  400. public :
  401. typedef typename std::iterator_traits<ITER>::value_type value_type;
  402. typedef typename std::iterator_traits<ITER>::pointer pointer;
  403. typedef typename std::iterator_traits<ITER>::pointer const_pointer;
  404. typedef typename std::iterator_traits<ITER>::reference reference;
  405. typedef typename std::iterator_traits<ITER>::reference const_reference;
  406. typedef size_t size_type;
  407. typedef tab_ref_with_selection_iterator_<ITER, COND> iterator;
  408. typedef iterator const_iterator;
  409. iterator begin(void) const
  410. { iterator it(begin_, cond); it.forward(); return it; }
  411. iterator end(void) const { return iterator(end_, cond); }
  412. bool empty(void) const { return begin_ == end_; }
  413. value_type front(void) const { return *begin(); }
  414. void pop_front(void) { ++begin_; begin_ = begin(); }
  415. COND &condition(void) { return cond; }
  416. const COND &condition(void) const { return cond; }
  417. tab_ref_with_selection(void) {}
  418. tab_ref_with_selection(const ITER &b, const ITER &e, const COND &c)
  419. : begin_(b), end_(e), cond(c) { begin_ = begin(); }
  420. };
  421. }
  422. #endif /* GMM_REF_H__ */