libstdc++
|
00001 // The template and inlines for the -*- C++ -*- gslice class. 00002 00003 // Copyright (C) 1997-2013 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file bits/gslice.h 00026 * This is an internal header file, included by other library headers. 00027 * Do not attempt to use it directly. @headername{valarray} 00028 */ 00029 00030 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr> 00031 00032 #ifndef _GSLICE_H 00033 #define _GSLICE_H 1 00034 00035 #pragma GCC system_header 00036 00037 namespace std _GLIBCXX_VISIBILITY(default) 00038 { 00039 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00040 00041 /** 00042 * @addtogroup numeric_arrays 00043 * @{ 00044 */ 00045 00046 /** 00047 * @brief Class defining multi-dimensional subset of an array. 00048 * 00049 * The slice class represents a multi-dimensional subset of an array, 00050 * specified by three parameter sets: start offset, size array, and stride 00051 * array. The start offset is the index of the first element of the array 00052 * that is part of the subset. The size and stride array describe each 00053 * dimension of the slice. Size is the number of elements in that 00054 * dimension, and stride is the distance in the array between successive 00055 * elements in that dimension. Each dimension's size and stride is taken 00056 * to begin at an array element described by the previous dimension. The 00057 * size array and stride array must be the same size. 00058 * 00059 * For example, if you have offset==3, stride[0]==11, size[1]==3, 00060 * stride[1]==3, then slice[0,0]==array[3], slice[0,1]==array[6], 00061 * slice[0,2]==array[9], slice[1,0]==array[14], slice[1,1]==array[17], 00062 * slice[1,2]==array[20]. 00063 */ 00064 class gslice 00065 { 00066 public: 00067 /// Construct an empty slice. 00068 gslice(); 00069 00070 /** 00071 * @brief Construct a slice. 00072 * 00073 * Constructs a slice with as many dimensions as the length of the @a l 00074 * and @a s arrays. 00075 * 00076 * @param __o Offset in array of first element. 00077 * @param __l Array of dimension lengths. 00078 * @param __s Array of dimension strides between array elements. 00079 */ 00080 gslice(size_t __o, const valarray<size_t>& __l, 00081 const valarray<size_t>& __s); 00082 00083 // XXX: the IS says the copy-ctor and copy-assignment operators are 00084 // synthesized by the compiler but they are just unsuitable 00085 // for a ref-counted semantic 00086 /// Copy constructor. 00087 gslice(const gslice&); 00088 00089 /// Destructor. 00090 ~gslice(); 00091 00092 // XXX: See the note above. 00093 /// Assignment operator. 00094 gslice& operator=(const gslice&); 00095 00096 /// Return array offset of first slice element. 00097 size_t start() const; 00098 00099 /// Return array of sizes of slice dimensions. 00100 valarray<size_t> size() const; 00101 00102 /// Return array of array strides for each dimension. 00103 valarray<size_t> stride() const; 00104 00105 private: 00106 struct _Indexer 00107 { 00108 size_t _M_count; 00109 size_t _M_start; 00110 valarray<size_t> _M_size; 00111 valarray<size_t> _M_stride; 00112 valarray<size_t> _M_index; // Linear array of referenced indices 00113 00114 _Indexer() 00115 : _M_count(1), _M_start(0), _M_size(), _M_stride(), _M_index() {} 00116 00117 _Indexer(size_t, const valarray<size_t>&, 00118 const valarray<size_t>&); 00119 00120 void 00121 _M_increment_use() 00122 { ++_M_count; } 00123 00124 size_t 00125 _M_decrement_use() 00126 { return --_M_count; } 00127 }; 00128 00129 _Indexer* _M_index; 00130 00131 template<typename _Tp> friend class valarray; 00132 }; 00133 00134 inline size_t 00135 gslice::start() const 00136 { return _M_index ? _M_index->_M_start : 0; } 00137 00138 inline valarray<size_t> 00139 gslice::size() const 00140 { return _M_index ? _M_index->_M_size : valarray<size_t>(); } 00141 00142 inline valarray<size_t> 00143 gslice::stride() const 00144 { return _M_index ? _M_index->_M_stride : valarray<size_t>(); } 00145 00146 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00147 // 543. valarray slice default constructor 00148 inline 00149 gslice::gslice() 00150 : _M_index(new gslice::_Indexer()) {} 00151 00152 inline 00153 gslice::gslice(size_t __o, const valarray<size_t>& __l, 00154 const valarray<size_t>& __s) 00155 : _M_index(new gslice::_Indexer(__o, __l, __s)) {} 00156 00157 inline 00158 gslice::gslice(const gslice& __g) 00159 : _M_index(__g._M_index) 00160 { if (_M_index) _M_index->_M_increment_use(); } 00161 00162 inline 00163 gslice::~gslice() 00164 { 00165 if (_M_index && _M_index->_M_decrement_use() == 0) 00166 delete _M_index; 00167 } 00168 00169 inline gslice& 00170 gslice::operator=(const gslice& __g) 00171 { 00172 if (__g._M_index) 00173 __g._M_index->_M_increment_use(); 00174 if (_M_index && _M_index->_M_decrement_use() == 0) 00175 delete _M_index; 00176 _M_index = __g._M_index; 00177 return *this; 00178 } 00179 00180 // @} group numeric_arrays 00181 00182 _GLIBCXX_END_NAMESPACE_VERSION 00183 } // namespace 00184 00185 #endif /* _GSLICE_H */