OSDN Git Service

2001-02-26 Phil Edwards <pme@sources.redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / src / valarray-inst.cc
1 #include <bits/std_valarray.h>
2
3 namespace std
4 {
5   // Some explicit instanciations.
6   template void
7      __valarray_fill(size_t* __restrict__, size_t, const size_t&);
8   
9   template void
10      __valarray_copy(const size_t* __restrict__, size_t, size_t* __restrict__);
11   
12   template valarray<size_t>::valarray(size_t);
13   template valarray<size_t>::valarray(const valarray<size_t>&);
14   template valarray<size_t>::~valarray();
15   template size_t valarray<size_t>::size() const;
16   template size_t& valarray<size_t>::operator[](size_t);
17
18
19   inline size_t
20   __valarray_product(const valarray<size_t>& __a)
21   {
22     typedef const size_t* __restrict__ _Tp;
23     const size_t __n = __a.size();
24     // XXX: This ugly cast is necessary because
25     //      valarray::operator[]() const return a VALUE!
26     //      Try to get the committee to correct that gross error.
27     valarray<size_t>& __t = const_cast<valarray<size_t>&>(__a);
28     return __valarray_product(&__t[0], &__t[0] + __n);
29   }
30   
31   // Map a gslice, described by its multidimensional LENGTHS
32   // and corresponding STRIDES, to a linear array of INDEXES
33   // for the purpose of indexing a flat, one-dimensional array
34   // representation of a gslice_array.
35   void
36   __gslice_to_index(size_t __o, const valarray<size_t>& __l,
37                     const valarray<size_t>& __s, valarray<size_t>& __i)
38   {
39     // There are as much as dimensions as there are strides.
40     size_t __n = __l.size();
41
42     // Get a buffer to hold current multi-index as we go through
43     // the gslice for the purpose of computing its linear-image.
44     size_t* const __t = static_cast<size_t*>
45       (__builtin_alloca(__n * sizeof (size_t)));
46     __valarray_fill(__t, __n, size_t(0));
47
48     // Note that this should match the product of all numbers appearing
49     // in __l which describes the multidimensional sizes of the
50     // the generalized slice.
51     const size_t __z = __i.size();
52     
53     for (size_t __j = 0; __j < __z; ++__j)
54       {
55         // Compute the linear-index image of (t_0, ... t_{n-1}).
56         // Normaly, we should use inner_product<>(), but we do it the
57         // the hard way here to avoid link-time can of worms.
58         size_t __a = __o;
59         for (size_t __k = 0; __k < __n; ++__k)
60           __a += __s[__k] * __t[__k];
61
62         __i[__j] = __a;
63
64         // Process the next multi-index.  The loop ought to be
65         // backward since we're making a lexicagraphical visit.
66         ++__t[__n-1];
67         for (size_t __k=__n-1; __k; --__k)
68           {
69             if (__t[__k] >= __l[__k])
70               {
71                 __t[__k] = 0;
72                 ++__t[__k-1];
73               }
74           }
75       }
76   }
77   
78   gslice::_Indexer::_Indexer(size_t __o, const valarray<size_t>& __l,
79                              const valarray<size_t>& __s)
80       : _M_count(1), _M_start(__o), _M_size(__l), _M_stride(__s),
81         _M_index(__l.size() == 0 ? 0 : __valarray_product(__l))
82   { __gslice_to_index(__o, __l, __s, _M_index); }
83   
84 }