xref: /openbsd-src/gnu/lib/libstdc++/libstdc++/include/bits/gslice.h (revision 03a78d155d6fff5698289342b62759a75b20d130)
1*03a78d15Sespie // The template and inlines for the -*- C++ -*- gslice class.
2*03a78d15Sespie 
3*03a78d15Sespie // Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4*03a78d15Sespie //
5*03a78d15Sespie // This file is part of the GNU ISO C++ Library.  This library is free
6*03a78d15Sespie // software; you can redistribute it and/or modify it under the
7*03a78d15Sespie // terms of the GNU General Public License as published by the
8*03a78d15Sespie // Free Software Foundation; either version 2, or (at your option)
9*03a78d15Sespie // any later version.
10*03a78d15Sespie 
11*03a78d15Sespie // This library is distributed in the hope that it will be useful,
12*03a78d15Sespie // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*03a78d15Sespie // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*03a78d15Sespie // GNU General Public License for more details.
15*03a78d15Sespie 
16*03a78d15Sespie // You should have received a copy of the GNU General Public License along
17*03a78d15Sespie // with this library; see the file COPYING.  If not, write to the Free
18*03a78d15Sespie // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19*03a78d15Sespie // USA.
20*03a78d15Sespie 
21*03a78d15Sespie // As a special exception, you may use this file as part of a free software
22*03a78d15Sespie // library without restriction.  Specifically, if other files instantiate
23*03a78d15Sespie // templates or use macros or inline functions from this file, or you compile
24*03a78d15Sespie // this file and link it with other files to produce an executable, this
25*03a78d15Sespie // file does not by itself cause the resulting executable to be covered by
26*03a78d15Sespie // the GNU General Public License.  This exception does not however
27*03a78d15Sespie // invalidate any other reasons why the executable file might be covered by
28*03a78d15Sespie // the GNU General Public License.
29*03a78d15Sespie 
30*03a78d15Sespie // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
31*03a78d15Sespie 
32*03a78d15Sespie /** @file gslice.h
33*03a78d15Sespie  *  This is an internal header file, included by other library headers.
34*03a78d15Sespie  *  You should not attempt to use it directly.
35*03a78d15Sespie  */
36*03a78d15Sespie 
37*03a78d15Sespie #ifndef _CPP_BITS_GSLICE_H
38*03a78d15Sespie #define _CPP_BITS_GSLICE_H 1
39*03a78d15Sespie 
40*03a78d15Sespie #pragma GCC system_header
41*03a78d15Sespie 
42*03a78d15Sespie namespace std {
43*03a78d15Sespie 
44*03a78d15Sespie     class gslice
45*03a78d15Sespie     {
46*03a78d15Sespie     public:
47*03a78d15Sespie         gslice ();
48*03a78d15Sespie         gslice (size_t, const valarray<size_t>&, const valarray<size_t>&);
49*03a78d15Sespie         // XXX: the IS says the copy-ctor and copy-assignment operators are
50*03a78d15Sespie         //      synthetized by the compiler but they are just unsuitable
51*03a78d15Sespie         //      for a ref-counted semantic
52*03a78d15Sespie         gslice(const gslice&);
53*03a78d15Sespie         ~gslice();
54*03a78d15Sespie 
55*03a78d15Sespie         // XXX: See the note above.
56*03a78d15Sespie         gslice& operator= (const gslice&);
57*03a78d15Sespie 
58*03a78d15Sespie         size_t           start () const;
59*03a78d15Sespie         valarray<size_t> size () const;
60*03a78d15Sespie         valarray<size_t> stride () const;
61*03a78d15Sespie 
62*03a78d15Sespie     private:
63*03a78d15Sespie         struct _Indexer {
64*03a78d15Sespie             size_t _M_count;
65*03a78d15Sespie             size_t _M_start;
66*03a78d15Sespie             valarray<size_t> _M_size;
67*03a78d15Sespie             valarray<size_t> _M_stride;
68*03a78d15Sespie             valarray<size_t> _M_index;
69*03a78d15Sespie             _Indexer(size_t, const valarray<size_t>&,
70*03a78d15Sespie                      const valarray<size_t>&);
_M_increment_use_Indexer71*03a78d15Sespie             void _M_increment_use() { ++_M_count; }
_M_decrement_use_Indexer72*03a78d15Sespie             size_t _M_decrement_use() { return --_M_count; }
73*03a78d15Sespie         };
74*03a78d15Sespie 
75*03a78d15Sespie         _Indexer* _M_index;
76*03a78d15Sespie 
77*03a78d15Sespie         template<typename _Tp> friend class valarray;
78*03a78d15Sespie     };
79*03a78d15Sespie 
80*03a78d15Sespie     inline size_t
start()81*03a78d15Sespie     gslice::start () const
82*03a78d15Sespie     { return _M_index ? _M_index->_M_start : 0; }
83*03a78d15Sespie 
84*03a78d15Sespie     inline valarray<size_t>
size()85*03a78d15Sespie     gslice::size () const
86*03a78d15Sespie     { return _M_index ? _M_index->_M_size : valarray<size_t>(); }
87*03a78d15Sespie 
88*03a78d15Sespie     inline valarray<size_t>
stride()89*03a78d15Sespie     gslice::stride () const
90*03a78d15Sespie     { return _M_index ? _M_index->_M_stride : valarray<size_t>(); }
91*03a78d15Sespie 
gslice()92*03a78d15Sespie     inline gslice::gslice () : _M_index(0) {}
93*03a78d15Sespie 
94*03a78d15Sespie     inline
gslice(size_t __o,const valarray<size_t> & __l,const valarray<size_t> & __s)95*03a78d15Sespie     gslice::gslice(size_t __o, const valarray<size_t>& __l,
96*03a78d15Sespie                    const valarray<size_t>& __s)
97*03a78d15Sespie             : _M_index(new gslice::_Indexer(__o, __l, __s)) {}
98*03a78d15Sespie 
99*03a78d15Sespie     inline
gslice(const gslice & __g)100*03a78d15Sespie     gslice::gslice(const gslice& __g) : _M_index(__g._M_index)
101*03a78d15Sespie     { if (_M_index) _M_index->_M_increment_use(); }
102*03a78d15Sespie 
103*03a78d15Sespie     inline
~gslice()104*03a78d15Sespie     gslice::~gslice()
105*03a78d15Sespie     { if (_M_index && _M_index->_M_decrement_use() == 0) delete _M_index; }
106*03a78d15Sespie 
107*03a78d15Sespie     inline gslice&
108*03a78d15Sespie     gslice::operator= (const gslice& __g)
109*03a78d15Sespie     {
110*03a78d15Sespie         if (__g._M_index) __g._M_index->_M_increment_use();
111*03a78d15Sespie         if (_M_index && _M_index->_M_decrement_use() == 0) delete _M_index;
112*03a78d15Sespie         _M_index = __g._M_index;
113*03a78d15Sespie         return *this;
114*03a78d15Sespie     }
115*03a78d15Sespie 
116*03a78d15Sespie 
117*03a78d15Sespie } // std::
118*03a78d15Sespie 
119*03a78d15Sespie 
120*03a78d15Sespie #endif /* _CPP_BITS_GSLICE_H */
121*03a78d15Sespie 
122*03a78d15Sespie // Local Variables:
123*03a78d15Sespie // mode:c++
124*03a78d15Sespie // End:
125