gslice.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // The template and inlines for the -*- C++ -*- gslice class.
  2. // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2004, 2005, 2006, 2009
  3. // Free Software Foundation, Inc.
  4. //
  5. // This file is part of the GNU ISO C++ Library. This library is free
  6. // software; you can redistribute it and/or modify it under the
  7. // terms of the GNU General Public License as published by the
  8. // Free Software Foundation; either version 3, or (at your option)
  9. // any later version.
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. // Under Section 7 of GPL version 3, you are granted additional
  15. // permissions described in the GCC Runtime Library Exception, version
  16. // 3.1, as published by the Free Software Foundation.
  17. // You should have received a copy of the GNU General Public License and
  18. // a copy of the GCC Runtime Library Exception along with this program;
  19. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  20. // <http://www.gnu.org/licenses/>.
  21. /** @file gslice.h
  22. * This is an internal header file, included by other library headers.
  23. * You should not attempt to use it directly.
  24. */
  25. // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
  26. #ifndef _GSLICE_H
  27. #define _GSLICE_H 1
  28. #pragma GCC system_header
  29. _GLIBCXX_BEGIN_NAMESPACE(std)
  30. /**
  31. * @addtogroup numeric_arrays
  32. * @{
  33. */
  34. /**
  35. * @brief Class defining multi-dimensional subset of an array.
  36. *
  37. * The slice class represents a multi-dimensional subset of an array,
  38. * specified by three parameter sets: start offset, size array, and stride
  39. * array. The start offset is the index of the first element of the array
  40. * that is part of the subset. The size and stride array describe each
  41. * dimension of the slice. Size is the number of elements in that
  42. * dimension, and stride is the distance in the array between successive
  43. * elements in that dimension. Each dimension's size and stride is taken
  44. * to begin at an array element described by the previous dimension. The
  45. * size array and stride array must be the same size.
  46. *
  47. * For example, if you have offset==3, stride[0]==11, size[1]==3,
  48. * stride[1]==3, then slice[0,0]==array[3], slice[0,1]==array[6],
  49. * slice[0,2]==array[9], slice[1,0]==array[14], slice[1,1]==array[17],
  50. * slice[1,2]==array[20].
  51. */
  52. class gslice
  53. {
  54. public:
  55. /// Construct an empty slice.
  56. gslice();
  57. /**
  58. * @brief Construct a slice.
  59. *
  60. * Constructs a slice with as many dimensions as the length of the @a l
  61. * and @a s arrays.
  62. *
  63. * @param o Offset in array of first element.
  64. * @param l Array of dimension lengths.
  65. * @param s Array of dimension strides between array elements.
  66. */
  67. gslice(size_t, const valarray<size_t>&, const valarray<size_t>&);
  68. // XXX: the IS says the copy-ctor and copy-assignment operators are
  69. // synthesized by the compiler but they are just unsuitable
  70. // for a ref-counted semantic
  71. /// Copy constructor.
  72. gslice(const gslice&);
  73. /// Destructor.
  74. ~gslice();
  75. // XXX: See the note above.
  76. /// Assignment operator.
  77. gslice& operator=(const gslice&);
  78. /// Return array offset of first slice element.
  79. size_t start() const;
  80. /// Return array of sizes of slice dimensions.
  81. valarray<size_t> size() const;
  82. /// Return array of array strides for each dimension.
  83. valarray<size_t> stride() const;
  84. private:
  85. struct _Indexer
  86. {
  87. size_t _M_count;
  88. size_t _M_start;
  89. valarray<size_t> _M_size;
  90. valarray<size_t> _M_stride;
  91. valarray<size_t> _M_index; // Linear array of referenced indices
  92. _Indexer()
  93. : _M_count(1), _M_start(0), _M_size(), _M_stride(), _M_index() {}
  94. _Indexer(size_t, const valarray<size_t>&,
  95. const valarray<size_t>&);
  96. void
  97. _M_increment_use()
  98. { ++_M_count; }
  99. size_t
  100. _M_decrement_use()
  101. { return --_M_count; }
  102. };
  103. _Indexer* _M_index;
  104. template<typename _Tp> friend class valarray;
  105. };
  106. inline size_t
  107. gslice::start() const
  108. { return _M_index ? _M_index->_M_start : 0; }
  109. inline valarray<size_t>
  110. gslice::size() const
  111. { return _M_index ? _M_index->_M_size : valarray<size_t>(); }
  112. inline valarray<size_t>
  113. gslice::stride() const
  114. { return _M_index ? _M_index->_M_stride : valarray<size_t>(); }
  115. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  116. // 543. valarray slice default constructor
  117. inline
  118. gslice::gslice()
  119. : _M_index(new gslice::_Indexer()) {}
  120. inline
  121. gslice::gslice(size_t __o, const valarray<size_t>& __l,
  122. const valarray<size_t>& __s)
  123. : _M_index(new gslice::_Indexer(__o, __l, __s)) {}
  124. inline
  125. gslice::gslice(const gslice& __g)
  126. : _M_index(__g._M_index)
  127. { if (_M_index) _M_index->_M_increment_use(); }
  128. inline
  129. gslice::~gslice()
  130. {
  131. if (_M_index && _M_index->_M_decrement_use() == 0)
  132. delete _M_index;
  133. }
  134. inline gslice&
  135. gslice::operator=(const gslice& __g)
  136. {
  137. if (__g._M_index)
  138. __g._M_index->_M_increment_use();
  139. if (_M_index && _M_index->_M_decrement_use() == 0)
  140. delete _M_index;
  141. _M_index = __g._M_index;
  142. return *this;
  143. }
  144. // @} group numeric_arrays
  145. _GLIBCXX_END_NAMESPACE
  146. #endif /* _GSLICE_H */