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.

221 lines
8.4 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2002-2012 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. /**@file gmm_sub_index.h
  27. @author Yves Renard <Yves.Renard@insa-lyon.fr>
  28. @date October 13, 2002.
  29. @brief sub-indices.
  30. */
  31. #ifndef GMM_SUB_INDEX_H__
  32. #define GMM_SUB_INDEX_H__
  33. #include "gmm_def.h"
  34. namespace gmm {
  35. /* ******************************************************************** */
  36. /* sub indices */
  37. /* ******************************************************************** */
  38. struct basic_index : public std::vector<size_t> {
  39. mutable size_type nb_ref;
  40. // size_type key1; faire la somme des composantes
  41. // const basic_index *rind; rindex s'il existe
  42. size_t operator[](size_type i) const {
  43. return (i < size()) ? std::vector<size_t>::operator[](i) : size_type(-1);
  44. }
  45. basic_index() : nb_ref(1) {}
  46. basic_index(size_type j) : std::vector<size_t>(j), nb_ref(1) {}
  47. template <typename IT> basic_index(IT b, IT e)
  48. : std::vector<size_t>(e-b), nb_ref(1) { std::copy(b, e, begin()); }
  49. basic_index(const basic_index *pbi) : nb_ref(1) {
  50. const_iterator it = pbi->begin(), ite = pbi->end();
  51. size_type i = 0;
  52. for ( ; it != ite; ++it) i = std::max(i, *it);
  53. resize(i+1); std::fill(begin(), end(), size_type(-1));
  54. for (it = pbi->begin(), i = 0; it != ite; ++it, ++i)
  55. std::vector<size_t>::operator[](*it) = i;
  56. }
  57. void swap(size_type i, size_type j) {
  58. std::swap(std::vector<size_t>::operator[](i),
  59. std::vector<size_t>::operator[](j));
  60. }
  61. };
  62. typedef basic_index *pbasic_index;
  63. struct index_generator {
  64. template <typename IT> static pbasic_index create_index(IT begin, IT end)
  65. { return new basic_index(begin, end); }
  66. static pbasic_index create_rindex(pbasic_index pbi)
  67. { return new basic_index(pbi); }
  68. static void attach(pbasic_index pbi) { if (pbi) pbi->nb_ref++; }
  69. static void unattach(pbasic_index pbi)
  70. { if (pbi && --(pbi->nb_ref) == 0) delete pbi; }
  71. };
  72. struct sub_index {
  73. size_type first_, last_;
  74. typedef basic_index base_type;
  75. typedef base_type::const_iterator const_iterator;
  76. mutable pbasic_index ind;
  77. mutable pbasic_index rind;
  78. void comp_extr(void) {
  79. std::vector<size_t>::const_iterator it = ind->begin(), ite = ind->end();
  80. if (it != ite) { first_=last_= *it; ++it; } else { first_=last_= 0; }
  81. for (; it != ite; ++it)
  82. { first_ = std::min(first_, *it); last_ = std::max(last_, *it); }
  83. }
  84. // inline void test_rind(void) const
  85. // { if (!rind) rind = index_generator::create_rindex(ind); }
  86. size_type size(void) const { return ind->size(); }
  87. size_type first(void) const { return first_; }
  88. size_type last(void) const { return last_; }
  89. size_type index(size_type i) const { return (*ind)[i]; }
  90. size_type rindex(size_type i) const {
  91. // test_rind();
  92. if (i < rind->size()) return (*rind)[i]; else return size_type(-1);
  93. }
  94. const_iterator begin(void) const { return ind->begin(); }
  95. const_iterator end(void) const { return ind->end(); }
  96. const_iterator rbegin(void) const {/*test_rind();*/ return rind->begin(); }
  97. const_iterator rend(void) const {/*test_rind();*/ return rind->end(); }
  98. sub_index() : ind(0), rind(0) {}
  99. template <typename IT> sub_index(IT it, IT ite)
  100. : ind(index_generator::create_index(it, ite)),
  101. rind(index_generator::create_rindex(ind)) { comp_extr(); }
  102. template <typename CONT> sub_index(const CONT &c)
  103. : ind(index_generator::create_index(c.begin(), c.end())),
  104. rind(index_generator::create_rindex(ind))
  105. { comp_extr(); }
  106. ~sub_index()
  107. { index_generator::unattach(rind); index_generator::unattach(ind); }
  108. sub_index(const sub_index &si) : first_(si.first_), last_(si.last_),
  109. ind(si.ind), rind(si.rind)
  110. { index_generator::attach(rind); index_generator::attach(ind); }
  111. sub_index &operator =(const sub_index &si) {
  112. index_generator::unattach(rind); index_generator::unattach(ind);
  113. ind = si.ind; rind = si.rind; index_generator::attach(rind);
  114. index_generator::attach(ind);
  115. first_ = si.first_; last_ = si.last_;
  116. return *this;
  117. }
  118. };
  119. struct unsorted_sub_index : public sub_index {
  120. typedef basic_index base_type;
  121. typedef base_type::const_iterator const_iterator;
  122. template <typename IT> unsorted_sub_index(IT it, IT ite)
  123. : sub_index(it, ite) {}
  124. template <typename CONT> unsorted_sub_index(const CONT &c)
  125. : sub_index(c) {}
  126. unsorted_sub_index() {}
  127. unsorted_sub_index(const unsorted_sub_index &si) : sub_index((sub_index &)(si)) { }
  128. unsorted_sub_index &operator =(const unsorted_sub_index &si)
  129. { sub_index::operator =(si); return *this; }
  130. void swap(size_type i, size_type j) {
  131. GMM_ASSERT2(ind->nb_ref <= 1, "Operation not allowed on this index");
  132. if (rind) rind->swap((*ind)[i], (*ind)[j]);
  133. ind->swap(i, j);
  134. }
  135. };
  136. inline std::ostream &operator << (std::ostream &o, const sub_index &si) {
  137. o << "sub_index(";
  138. if (si.size() != 0) o << si.index(0);
  139. for (size_type i = 1; i < si.size(); ++i) o << ", " << si.index(i);
  140. o << ")";
  141. return o;
  142. }
  143. struct sub_interval {
  144. size_type min, max;
  145. size_type size(void) const { return max - min; }
  146. size_type first(void) const { return min; }
  147. size_type last(void) const { return max; }
  148. size_type index(size_type i) const { return min + i; }
  149. size_type step(void) const { return 1; }
  150. size_type rindex(size_type i) const
  151. { if (i >= min && i < max) return i - min; return size_type(-1); }
  152. sub_interval(size_type mi, size_type l) : min(mi), max(mi+l) {}
  153. sub_interval() {}
  154. };
  155. inline std::ostream &operator << (std::ostream &o, const sub_interval &si)
  156. { o << "sub_interval(" << si.min << ", " << si.size() << ")"; return o; }
  157. struct sub_slice {
  158. size_type min, max, N;
  159. size_type size(void) const { return (max - min) / N; }
  160. size_type first(void) const { return min; }
  161. size_type last(void) const { return (min == max) ? max : max+1-N; }
  162. size_type step(void) const { return N; }
  163. size_type index(size_type i) const { return min + N * i; }
  164. size_type rindex(size_type i) const {
  165. if (i >= min && i < max)
  166. { size_type j = (i - min); if (j % N == 0) return j / N; }
  167. return size_type(-1);
  168. }
  169. sub_slice(size_type mi, size_type l, size_type n)
  170. : min(mi), max(mi+l*n), N(n) {}
  171. sub_slice(void) {}
  172. };
  173. inline std::ostream &operator << (std::ostream &o, const sub_slice &si) {
  174. o << "sub_slice(" << si.min << ", " << si.size() << ", " << si.step()
  175. << ")"; return o;
  176. }
  177. template<class SUBI> struct index_is_sorted
  178. { typedef linalg_true bool_type; };
  179. template<> struct index_is_sorted<unsorted_sub_index>
  180. { typedef linalg_false bool_type; };
  181. }
  182. #endif // GMM_SUB_INDEX_H__