File: ni_support.h

package info (click to toggle)
python-numarray 1.5.2-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,668 kB
  • ctags: 11,384
  • sloc: ansic: 113,864; python: 22,422; makefile: 197; sh: 11
file content (323 lines) | stat: -rw-r--r-- 15,441 bytes parent folder | download | duplicates (2)
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/* Copyright (C) 2003-2005 Peter J. Verveer
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met: 
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials provided
 *    with the distribution.
 *
 * 3. The name of the author may not be used to endorse or promote
 *    products derived from this software without specific prior
 *    written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.      
 */

#ifndef NI_SUPPORT_H
#define NI_SUPPORT_H

#include "nd_image.h"
#include <stdlib.h>
#include <float.h>
#include <limits.h>
#include <assert.h>

/* The different boundary conditions. The mirror condition is not used
   by the python code, but C code is kept around in case we might wish
   to add it. */
typedef enum {
  NI_EXTEND_FIRST = 0,
  NI_EXTEND_NEAREST = 0,
  NI_EXTEND_WRAP,
  NI_EXTEND_REFLECT,
  NI_EXTEND_MIRROR,
  NI_EXTEND_CONSTANT,
  NI_EXTEND_LAST = NI_EXTEND_CONSTANT,
  NI_EXTEND_DEFAULT = NI_EXTEND_MIRROR
} NI_ExtendMode;

/******************************************************************/
/* Iterators */
/******************************************************************/

/******************************************************************/
/* Iterators */
/******************************************************************/

/* the iterator structure: */
typedef struct {
  int rank_m1;
  maybelong dimensions[MAXDIM];
  maybelong coordinates[MAXDIM];
  maybelong strides[MAXDIM];
  maybelong backstrides[MAXDIM];
} NI_Iterator;

/* initialize iterations over single array elements: */
int NI_InitPointIterator(PyArrayObject*, NI_Iterator*);

/* initialize iterations over an arbritrary sub-space: */
int NI_SubspaceIterator(NI_Iterator*, UInt32);

/* initialize iteration over array lines: */
int NI_LineIterator(NI_Iterator*, int);

/* reset an iterator */
#define NI_ITERATOR_RESET(iterator)              \
{                                                \
  int _ii;                                       \
  for(_ii = 0; _ii <= (iterator).rank_m1; _ii++) \
    (iterator).coordinates[_ii] = 0;             \
}

/* go to the next point in a single array */
#define NI_ITERATOR_NEXT(iterator, pointer)                         \
{                                                                   \
  int _ii;                                                          \
  for(_ii = (iterator).rank_m1; _ii >= 0; _ii--)                    \
    if ((iterator).coordinates[_ii] < (iterator).dimensions[_ii]) { \
      (iterator).coordinates[_ii]++;                                \
      pointer += (iterator).strides[_ii];                           \
      break;                                                        \
    } else {                                                        \
      (iterator).coordinates[_ii] = 0;                              \
      pointer -= (iterator).backstrides[_ii];                       \
    }                                                               \
}

/* go to the next point in two arrays of the same size */
#define NI_ITERATOR_NEXT2(iterator1, iterator2,  pointer1, pointer2)  \
{                                                                     \
  int _ii;                                                            \
  for(_ii = (iterator1).rank_m1; _ii >= 0; _ii--)                     \
    if ((iterator1).coordinates[_ii] < (iterator1).dimensions[_ii]) { \
      (iterator1).coordinates[_ii]++;                                 \
      pointer1 += (iterator1).strides[_ii];                           \
      pointer2 += (iterator2).strides[_ii];                           \
      break;                                                          \
    } else {                                                          \
      (iterator1).coordinates[_ii] = 0;                               \
      pointer1 -= (iterator1).backstrides[_ii];                       \
      pointer2 -= (iterator2).backstrides[_ii];                       \
    }                                                                 \
}

/* go to the next point in three arrays of the same size */
#define NI_ITERATOR_NEXT3(iterator1, iterator2,  iterator3,           \
                          pointer1, pointer2, pointer3)               \
{                                                                     \
  int _ii;                                                            \
  for(_ii = (iterator1).rank_m1; _ii >= 0; _ii--)                     \
    if ((iterator1).coordinates[_ii] < (iterator1).dimensions[_ii]) { \
      (iterator1).coordinates[_ii]++;                                 \
      pointer1 += (iterator1).strides[_ii];                           \
      pointer2 += (iterator2).strides[_ii];                           \
      pointer3 += (iterator3).strides[_ii];                           \
      break;                                                          \
    } else {                                                          \
      (iterator1).coordinates[_ii] = 0;                               \
      pointer1 -= (iterator1).backstrides[_ii];                       \
      pointer2 -= (iterator2).backstrides[_ii];                       \
      pointer3 -= (iterator3).backstrides[_ii];                       \
    }                                                                 \
}

/* go to an arbitrary point in a single array */
#define NI_ITERATOR_GOTO(iterator, destination, base, pointer) \
{                                                              \
  int _ii;                                                     \
  pointer = base;                                              \
  for(_ii = (iterator).rank_m1; _ii >= 0; _ii--) {             \
    pointer += destination[_ii] * (iterator).strides[_ii];     \
    (iterator).coordinates[_ii] = destination[_ii];            \
  }                                                            \
}

/******************************************************************/
/* Line buffers */
/******************************************************************/

/* the linebuffer structure: */
typedef struct { 
  double *buffer_data;
  maybelong buffer_lines, line_length, line_stride;
  maybelong size1, size2, array_lines, next_line;
  NI_Iterator iterator;
  char* array_data;
  NumarrayType array_type;
  NI_ExtendMode extend_mode;
  double extend_value;
} NI_LineBuffer;

/* Get the next line being processed: */
#define NI_GET_LINE(_buffer, _line)                                      \
  ((_buffer).buffer_data + (_line) * ((_buffer).line_length +            \
                                      (_buffer).size1 + (_buffer).size2))
/* Allocate line buffer data */
int NI_AllocateLineBuffer(PyArrayObject*, int, maybelong, maybelong,
                          maybelong*, maybelong, double**);

/* Initialize a line buffer */
int NI_InitLineBuffer(PyArrayObject*, int, maybelong, maybelong, maybelong, 
                      double*, NI_ExtendMode, double, NI_LineBuffer*);

/* Extend a line in memory to implement boundary conditions: */
int NI_ExtendLine(double*, int, int, int, NI_ExtendMode, double);

/* Copy a line from an array to a buffer: */
int NI_ArrayToLineBuffer(NI_LineBuffer*, maybelong*, int*);

/* Copy a line from a buffer to an array: */
int NI_LineBufferToArray(NI_LineBuffer*);

/******************************************************************/
/* Multi-dimensional filter support functions */
/******************************************************************/

/* the filter iterator structure: */
typedef struct {
  maybelong strides[MAXDIM], backstrides[MAXDIM];
  maybelong bound1[MAXDIM], bound2[MAXDIM];
} NI_FilterIterator;

/* Initialize a filter iterator: */
int NI_InitFilterIterator(int, maybelong*, maybelong, maybelong*,
                                          maybelong*, NI_FilterIterator*);

/* Calculate the offsets to the filter points, for all border regions and
   the interior of the array: */
int NI_InitFilterOffsets(PyArrayObject*, Bool*, maybelong*,
          maybelong*, NI_ExtendMode, maybelong**, maybelong*, maybelong**);

/* Move to the next point in an array, possible changing the filter
   offsets, to adapt to boundary conditions: */
#define NI_FILTER_NEXT(iteratorf, iterator1, pointerf, pointer1)  \
{                                                                 \
  int _ii;                                                        \
  for(_ii = (iterator1).rank_m1; _ii >= 0; _ii--) {               \
    maybelong _pp = (iterator1).coordinates[_ii];                 \
    if (_pp < (iterator1).dimensions[_ii]) {                      \
      if (_pp < (iteratorf).bound1[_ii] ||                        \
                                  _pp >= (iteratorf).bound2[_ii]) \
        pointerf += (iteratorf).strides[_ii];                     \
      (iterator1).coordinates[_ii]++;                             \
      pointer1 += (iterator1).strides[_ii];                       \
      break;                                                      \
    } else {                                                      \
      (iterator1).coordinates[_ii] = 0;                           \
      pointer1 -= (iterator1).backstrides[_ii];                   \
      pointerf -= (iteratorf).backstrides[_ii];                   \
    }                                                             \
  }                                                               \
}

/* Move to the next point in two arrays, possible changing the pointer
   to the filter offsets when moving into a different region in the
   array: */
#define NI_FILTER_NEXT2(iteratorf, iterator1, iterator2,    \
                        pointerf, pointer1, pointer2)       \
{                                                           \
  int _ii;                                                  \
  for(_ii = (iterator1).rank_m1; _ii >= 0; _ii--) {         \
    maybelong _pp = (iterator1).coordinates[_ii];           \
    if (_pp < (iterator1).dimensions[_ii]) {                \
      if (_pp < (iteratorf).bound1[_ii] ||                  \
                            _pp >= (iteratorf).bound2[_ii]) \
        pointerf += (iteratorf).strides[_ii];               \
      (iterator1).coordinates[_ii]++;                       \
      pointer1 += (iterator1).strides[_ii];                 \
      pointer2 += (iterator2).strides[_ii];                 \
      break;                                                \
    } else {                                                \
      (iterator1).coordinates[_ii] = 0;                     \
      pointer1 -= (iterator1).backstrides[_ii];             \
      pointer2 -= (iterator2).backstrides[_ii];             \
      pointerf -= (iteratorf).backstrides[_ii];             \
    }                                                       \
  }                                                         \
}

/* Move to the next point in three arrays, possible changing the pointer
   to the filter offsets when moving into a different region in the
   array: */
#define NI_FILTER_NEXT3(iteratorf, iterator1, iterator2, iterator3,  \
                        pointerf, pointer1, pointer2, pointer3)      \
{                                                                    \
  int _ii;                                                           \
  for(_ii = (iterator1).rank_m1; _ii >= 0; _ii--) {                  \
    maybelong _pp = (iterator1).coordinates[_ii];                    \
    if (_pp < (iterator1).dimensions[_ii]) {                         \
      if (_pp < (iteratorf).bound1[_ii] ||                           \
                                     _pp >= (iteratorf).bound2[_ii]) \
        pointerf += (iteratorf).strides[_ii];                        \
      (iterator1).coordinates[_ii]++;                                \
      pointer1 += (iterator1).strides[_ii];                          \
      pointer2 += (iterator2).strides[_ii];                          \
      pointer3 += (iterator3).strides[_ii];                          \
      break;                                                         \
    } else {                                                         \
      (iterator1).coordinates[_ii] = 0;                              \
      pointer1 -= (iterator1).backstrides[_ii];                      \
      pointer2 -= (iterator2).backstrides[_ii];                      \
      pointer3 -= (iterator3).backstrides[_ii];                      \
      pointerf -= (iteratorf).backstrides[_ii];                      \
    }                                                                \
  }                                                                  \
}

/* Move the pointer to the filter offsets according to the given
  coordinates: */
#define NI_FILTER_GOTO(iteratorf, iterator, fbase, pointerf) \
{                                                            \
  int _ii;                                                   \
  maybelong _jj;                                             \
  pointerf = fbase;                                          \
  for(_ii = iterator.rank_m1; _ii >= 0; _ii--) {             \
    maybelong _pp = iterator.coordinates[_ii];               \
    maybelong b1 = (iteratorf).bound1[_ii];                  \
    maybelong b2 = (iteratorf).bound2[_ii];                  \
    if (_pp < b1) {                                          \
        _jj = _pp;                                           \
    } else if (_pp > b2 && b2 >= b1) {                       \
        _jj = _pp + b1 - b2;                                 \
    } else {                                                 \
        _jj = b1;                                            \
    }                                                        \
    pointerf += (iteratorf).strides[_ii] * _jj;              \
  }                                                          \
}

typedef struct {
    maybelong *coordinates;
    int size;
    void *next;
} NI_CoordinateBlock;

typedef struct {
    int block_size, rank;
    void *blocks;
} NI_CoordinateList;

NI_CoordinateList* NI_InitCoordinateList(int, int);
int NI_CoordinateListStealBlocks(NI_CoordinateList*, NI_CoordinateList*);
NI_CoordinateBlock* NI_CoordinateListAddBlock(NI_CoordinateList*);
NI_CoordinateBlock* NI_CoordinateListDeleteBlock(NI_CoordinateList*);
void NI_FreeCoordinateList(NI_CoordinateList*);

#endif