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.

224 lines
8.3 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2002-2017 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(0) { comp_extr(); }
  102. template <typename CONT> sub_index(const CONT &c)
  103. : ind(index_generator::create_index(c.begin(), c.end())),
  104. rind(0) { comp_extr(); }
  105. ~sub_index() {
  106. index_generator::unattach(rind);
  107. index_generator::unattach(ind);
  108. }
  109. sub_index(const sub_index &si) : first_(si.first_), last_(si.last_),
  110. ind(si.ind), rind(si.rind)
  111. { index_generator::attach(rind); index_generator::attach(ind); }
  112. sub_index &operator =(const sub_index &si) {
  113. index_generator::unattach(rind);
  114. index_generator::unattach(ind);
  115. ind = si.ind; rind = si.rind;
  116. index_generator::attach(rind);
  117. index_generator::attach(ind);
  118. first_ = si.first_; last_ = si.last_;
  119. return *this;
  120. }
  121. };
  122. struct unsorted_sub_index : public sub_index {
  123. typedef basic_index base_type;
  124. typedef base_type::const_iterator const_iterator;
  125. template <typename IT> unsorted_sub_index(IT it, IT ite)
  126. : sub_index(it, ite) {}
  127. template <typename CONT> unsorted_sub_index(const CONT &c)
  128. : sub_index(c) {}
  129. unsorted_sub_index() {}
  130. unsorted_sub_index(const unsorted_sub_index &si) : sub_index((const sub_index &)(si)) { }
  131. unsorted_sub_index &operator =(const unsorted_sub_index &si)
  132. { sub_index::operator =(si); return *this; }
  133. void swap(size_type i, size_type j) {
  134. GMM_ASSERT2(ind->nb_ref <= 1, "Operation not allowed on this index");
  135. if (rind) rind->swap((*ind)[i], (*ind)[j]);
  136. ind->swap(i, j);
  137. }
  138. };
  139. inline std::ostream &operator << (std::ostream &o, const sub_index &si) {
  140. o << "sub_index(";
  141. if (si.size() != 0) o << si.index(0);
  142. for (size_type i = 1; i < si.size(); ++i) o << ", " << si.index(i);
  143. o << ")";
  144. return o;
  145. }
  146. struct sub_interval {
  147. size_type min, max;
  148. size_type size(void) const { return max - min; }
  149. size_type first(void) const { return min; }
  150. size_type last(void) const { return max; }
  151. size_type index(size_type i) const { return min + i; }
  152. size_type step(void) const { return 1; }
  153. size_type rindex(size_type i) const
  154. { if (i >= min && i < max) return i - min; return size_type(-1); }
  155. sub_interval(size_type mi, size_type l) : min(mi), max(mi+l) {}
  156. sub_interval() {}
  157. };
  158. inline std::ostream &operator << (std::ostream &o, const sub_interval &si)
  159. { o << "sub_interval(" << si.min << ", " << si.size() << ")"; return o; }
  160. struct sub_slice {
  161. size_type min, max, N;
  162. size_type size(void) const { return (max - min) / N; }
  163. size_type first(void) const { return min; }
  164. size_type last(void) const { return (min == max) ? max : max+1-N; }
  165. size_type step(void) const { return N; }
  166. size_type index(size_type i) const { return min + N * i; }
  167. size_type rindex(size_type i) const {
  168. if (i >= min && i < max)
  169. { size_type j = (i - min); if (j % N == 0) return j / N; }
  170. return size_type(-1);
  171. }
  172. sub_slice(size_type mi, size_type l, size_type n)
  173. : min(mi), max(mi+l*n), N(n) {}
  174. sub_slice(void) {}
  175. };
  176. inline std::ostream &operator << (std::ostream &o, const sub_slice &si) {
  177. o << "sub_slice(" << si.min << ", " << si.size() << ", " << si.step()
  178. << ")"; return o;
  179. }
  180. template<class SUBI> struct index_is_sorted
  181. { typedef linalg_true bool_type; };
  182. template<> struct index_is_sorted<unsorted_sub_index>
  183. { typedef linalg_false bool_type; };
  184. }
  185. #endif // GMM_SUB_INDEX_H__