strided.cc 7.19 KB
Newer Older
thomas.forbriger's avatar
thomas.forbriger committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*! \file strided_def.h
 * \brief shape of a strided array (definitions) (prototypes)
 * 
 * ----------------------------------------------------------------------------
 * 
 * $Id: strided.cc,v 1.1 2002-12-15 19:32:52 forbrig Exp $
 * \author Thomas Forbriger
 * \since 08/12/2002
 * 
 * shape of a strided array (definitions) (prototypes)
 * \sa aff::Strided
 *
 * \note
 * Never include this header directly. Here you find code factored out from
 * the aff::Strided class. This code may be compiled into a binary library.
 * 
 * Copyright (c) 2002 by Thomas Forbriger (IMG Frankfurt) 
 * 
 * REVISIONS and CHANGES 
 *  - 08/12/2002   V1.0   copied from libcontxx
 *  - 11/12/2002   V1.1   major revision (thof)
 *                        - removed template parameter
 *                        - calculates base for index operator of
 *                          each dimensionality
 *  - 12/12/2002   V1.2   (thof)
 *                        - added shrink and collapse
 *  - 13/12/2002   V1.3   (thof)
 *                        - reworked shape definition and mapping
 *                        - added straightforward implementations for
 *                          subarrays and slicing
 * 
 * ============================================================================
 */

#ifndef AFF_STRIDED_H_VERSION 
#error "include this only through strided.h"
#endif

// include guard
#ifndef AFF_STRIDED_DEF_H_VERSION

#define AFF_STRIDED_DEF_H_VERSION \
  "AFF_STRIDED_DEF_H   V1.3"
#define AFF_STRIDED_DEF_H_CVSID \
  "$Id: strided.cc,v 1.1 2002-12-15 19:32:52 forbrig Exp $"

#include<aff/lib/error.h>

namespace aff {

#ifdef AFF_PREBUILT
namespace prebuilt {
#endif

#ifdef AFF_PREBUILT
#ifndef AFF_COMPILING_LIBRARY
#error "definition read in prebuilt mode and not during library compilation"
#endif
#endif

  /*----------------------------------------------------------------------*/

  //! instantiate static member (otherwise the linker won't find it)
  const Tdim Strided::Mmax_dimen; 
  
  /*----------------------------------------------------------------------*/

  //! construct do given size and first index
  Strided::Strided(const TSizeVec& size, 
                   const Tsubscript& first,
                   const Tsubscript& shift)
  {
    TSizeVec one(1);
    AFF_assert((!inline_anylarger(one,size)),
               "ERROR (Strided): size must be at least one");
    Mfirst=first;
    Mstride[0]=1;
    Mlast[0]=Mfirst[0]+size[0]-1;
    for(Tdim i=1; i<Mmax_dimen; i++) 
    { 
      Mstride[i]=Mstride[i-1]*size[i-1]; 
      Mlast[i]=Mfirst[i]+size[i]-1;
    }
    calculate_base(shift);
  }

  /*----------------------------------------------------------------------*/

  //! construct do given first and last index
  Strided::Strided(const TIndexVec& first, const TIndexVec& last, 
                   const Tsubscript& shift)
  {
    AFF_assert((!inline_anylarger(first,last)),
               "ERROR (Strided): first index must be smaller than last");
    Mfirst=first;
    Mlast=last;
    Mstride[0]=1;
    for(Tdim i=1; i<Mmax_dimen; i++) 
    { 
      Mstride[i]=Mstride[i-1]*(1+Mlast[i-1]-Mfirst[i-1]); 
    }
    calculate_base(shift);
  }

  /*----------------------------------------------------------------------*/

  //! construct do given first and last index
  void Strided::calculate_base(const Tsubscript& offset) 
  {
    Mbase[0]=offset-Mfirst[0]*Mstride[0];
    for (Tdim i=1; i<Mmax_dimen; i++) 
    {
      Mbase[i]=Mbase[i-1]-Mfirst[i]*Mstride[i];
    }
  }

  /*----------------------------------------------------------------------*/

  //! calculate total size
  Tsize Strided::size() const
  {
    // could be done more elegant by inlining - but this will do it here
    TSizeVec size(Mlast);
    for (int i=0; i<Mmax_dimen; i++) { size[i]-=(Mfirst[i]-1); }
    return (inline_product(size));
  }

  /*----------------------------------------------------------------------*/
  
  //! subarray
  Strided& Strided::shrink(const TIndexVec& last)
  { 
    AFF_assert((!inline_anysmaller(last,Mfirst)),
               "ERROR (Strided::shrink): index must not be smaller than first");
    AFF_assert((!inline_anylarger(last,Mlast)),
               "ERROR (Strided::shrink): index must not be larger than last");
    Mlast=last;
    return(*this);
  }

  /*----------------------------------------------------------------------*/
  
  //! subarray
  Strided& Strided::shrink(const TIndexVec& first, const TIndexVec& last)
  {
    AFF_assert((!(inline_anysmaller(last,Mfirst)
                  ||inline_anylarger(last,Mlast))),
               "ERROR (Strided::shrink): illegal last index vector");
    AFF_assert((!(inline_anysmaller(first,Mfirst)
                  ||inline_anylarger(first,Mlast))),
               "ERROR (Strided::shrink): illegal first index vector");
    AFF_assert((!inline_anysmaller(last,first)),
               "ERROR (Strided::shrink): last must not be smaller than first");
    Tsubscript offset=this->offset(first);
    Mfirst=first;
    Mlast=last;
    this->calculate_base(offset);
    return(*this);
  }

  /*----------------------------------------------------------------------*/
  
  //! subarray
  Strided& Strided::shrink(const Tdim& i, 
                           const Tsubscript& first, 
                           const Tsubscript& last)
  {
    AFF_assert((i<Mmax_dimen),
               "ERROR (Strided::shrink): illegal dimension");
    AFF_assert(((Mfirst[i]<=first)&&(first<=last)&&(last<=Mlast[i])),
               "ERROR (Strided::shrink): index range erres");
    // take the long way home
    TIndexVec newfirst(Mfirst);
    newfirst[i]=first;
    Tsubscript offset=this->offset(newfirst);
    Mfirst[i]=first;
    Mlast[i]=last;
    this->calculate_base(offset);
    return(*this);
  }

  /*----------------------------------------------------------------------*/
  
  //! subarray
  Strided& Strided::shrink(const Tdim& i, 
                           const Tsubscript& last)
  {
    AFF_assert((i<Mmax_dimen),
               "ERROR (Strided::shrink): illegal dimension");
    AFF_assert(((Mfirst[i]<=last)&&(last<=Mlast[i])),
               "ERROR (Strided::shrink): illegal index value");
    Mlast[i]=last;
    return(*this);
  }

  /*----------------------------------------------------------------------*/
  
  /*! \brief slice
   * \todo
   * This is implementation of slicing is straightforward but rather
   * inefficient. If you do a lot of slicing it might be worth to review and
   * rework the code.
   */
  Strided& Strided::collapse(const Tdim& i, const Tsubscript& index)
  {
    AFF_assert((i<Mmax_dimen),
               "ERROR (Strided::collapse): illegal dimension");
    AFF_assert(((Mfirst[i]<=index)&&(index<=Mlast[i])),
               "ERROR (Strided::collapse): index range error");
    this->shrink(i,index,index); 
    Tsubscript offset=this->first_offset();
    Tdim j=i+1;
    while (j<Mmax_dimen)
    {
      Mfirst[j-1]=Mfirst[j];
      Mlast[j-1]=Mlast[j];
      Mstride[j-1]=Mstride[j];
      j++;
    }
    Mfirst[Mmax_dimen]=1;
    Mlast[Mmax_dimen]=1;
    Mstride[Mmax_dimen]=this->size(Mmax_dimen-2)*Mstride[Mmax_dimen-2];
    this->calculate_base(offset);
    return(*this);
  }

#ifdef AFF_PREBUILT
} // namespace prebuilt
#endif

} // namespace aff

#endif // AFF_STRIDED_DEF_H_VERSION (includeguard)

/* ----- END OF strided_def.h ----- */