Yet Another eXchange Tool  0.9.0
xt_stripe.c
Go to the documentation of this file.
1 
12 /*
13  * Keywords:
14  * Maintainer: Jörg Behrens <behrens@dkrz.de>
15  * Moritz Hanke <hanke@dkrz.de>
16  * Thomas Jahns <jahns@dkrz.de>
17  * URL: https://doc.redmine.dkrz.de/yaxt/html/
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions are
21  * met:
22  *
23  * Redistributions of source code must retain the above copyright notice,
24  * this list of conditions and the following disclaimer.
25  *
26  * Redistributions in binary form must reproduce the above copyright
27  * notice, this list of conditions and the following disclaimer in the
28  * documentation and/or other materials provided with the distribution.
29  *
30  * Neither the name of the DKRZ GmbH nor the names of its contributors
31  * may be used to endorse or promote products derived from this software
32  * without specific prior written permission.
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
35  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
36  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
37  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
38  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
39  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
40  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
41  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
42  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
43  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
44  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  */
46 
47 #ifdef HAVE_CONFIG_H
48 #include <config.h>
49 #endif
50 
51 #include <stdbool.h>
52 #include <stdlib.h>
53 
54 #include "xt/xt_core.h"
55 #include "xt/xt_stripe.h"
56 #include "xt_stripe_util.h"
57 
58 #include "xt_stripe_util.h"
59 #include "core/ppm_xfuncs.h"
60 #include "instr.h"
61 #include "ensure_array_size.h"
62 
63 void xt_convert_indices_to_stripes(const Xt_int *restrict indices,
64  int num_indices,
65  struct Xt_stripe **stripes,
66  int * num_stripes)
67 {
68  *stripes = NULL;
69  *num_stripes = 0;
70  xt_convert_indices_to_stripes_keep_buf(indices, num_indices,
71  stripes, num_stripes);
72 }
73 
74 void xt_convert_indices_to_stripes_keep_buf(const Xt_int *restrict indices,
75  int num_indices,
76  struct Xt_stripe **stripes,
77  int * num_stripes) {
78 
79  INSTR_DEF(instr,"xt_idxstripes_convert_to_stripes")
80  INSTR_START(instr);
81 
82  struct Xt_stripe *restrict temp_stripes = *stripes;
83  size_t temp_stripes_array_size = 0;
84  size_t num_temp_stripes = (size_t)*num_stripes * sizeof (*temp_stripes);
85 
86  if (num_indices > 0) {
87  size_t i = 0;
88 
89  while(i < (size_t)num_indices) {
90  ++num_temp_stripes;
91 
92  ENSURE_ARRAY_SIZE(temp_stripes, temp_stripes_array_size, num_temp_stripes);
93 
94  size_t j = 1;
95 
96  Xt_int stride = 1;
97  if (i + j < (size_t)num_indices) {
98  stride = (Xt_int)(indices[i + 1] - indices[i]);
99  do {
100  ++j;
101  } while ((i + j) < (size_t)num_indices
102  && indices[i] + (Xt_int)j * stride == indices[i + j]);
103  }
104  j-= ((i + j + 1 < (size_t)num_indices)
105  && ((indices[i + j - 1] == indices[i + j] - 1)
106  & (indices[i + j] == indices[i + j + 1] - 1)));
107 
108  temp_stripes[num_temp_stripes-1].start = indices[i];
109  temp_stripes[num_temp_stripes-1].stride = stride;
110  temp_stripes[num_temp_stripes-1].nstrides = (int)j;
111 
112  i = i + j;
113  }
114  }
115 
116  *stripes = xrealloc(temp_stripes, num_temp_stripes * sizeof(*temp_stripes));
117  *num_stripes = (int)num_temp_stripes;
118  INSTR_STOP(instr);
119 }
120 
121 size_t
122 xt_stripes_merge_copy(size_t num_stripes,
123  struct Xt_stripe *stripes_dst,
124  const struct Xt_stripe *stripes_src,
125  bool lookback)
126 {
127  size_t skip = 1;
128  if (num_stripes) {
129  if (lookback) {
130  Xt_int stride = stripes_src[0].stride,
131  prev_stride = stripes_dst[-1].stride,
132  start = stripes_src[0].start,
133  prev_start = stripes_dst[-1].start;
134  if (stride == prev_stride
135  && start == prev_start + stride * (Xt_int)stripes_dst[-1].nstrides) {
136  /* merge perfectly aligned stripes */
137  stripes_dst[-1].nstrides += stripes_src[0].nstrides;
138  ++skip;
139  goto copy_loop;
140  }
141  }
142  stripes_dst[0] = stripes_src[0];
143  copy_loop:
144  if (num_stripes > 1)
145  for (size_t i = 1; i < num_stripes; ++i) {
146  Xt_int stride = stripes_src[i].stride,
147  prev_stride = stripes_dst[i-skip].stride,
148  start = stripes_src[i].start,
149  prev_start = stripes_dst[i-skip].start;
150  if (stride == prev_stride
151  && start == prev_start + stride * (Xt_int)stripes_dst[i-skip].nstrides) {
152  /* merge perfectly aligned stripes */
153  stripes_dst[i-skip].nstrides += stripes_src[i].nstrides;
154  ++skip;
155  } else
156  stripes_dst[i-skip+1] = stripes_src[i];
157  }
158  }
159  return num_stripes - (skip - 1);
160 }
161 
162 
163 
164 /*
165  * Local Variables:
166  * c-basic-offset: 2
167  * coding: utf-8
168  * indent-tabs-mode: nil
169  * show-trailing-whitespace: t
170  * require-trailing-newline: t
171  * End:
172  */
#define ENSURE_ARRAY_SIZE(arrayp, curr_array_size, req_size)
#define INSTR_STOP(T)
Definition: instr.h:69
#define INSTR_DEF(T, S)
Definition: instr.h:66
#define INSTR_START(T)
Definition: instr.h:68
add versions of standard API functions not returning on error
#define xrealloc(ptr, size)
Definition: ppm_xfuncs.h:71
Xt_int stride
Definition: xt_stripe.h:56
int nstrides
Definition: xt_stripe.h:57
Xt_int start
Definition: xt_stripe.h:55
base definitions header file
XT_INT Xt_int
Definition: xt_core.h:68
size_t xt_stripes_merge_copy(size_t num_stripes, struct Xt_stripe *stripes_dst, const struct Xt_stripe *stripes_src, bool lookback)
Definition: xt_stripe.c:122
void xt_convert_indices_to_stripes(const Xt_int *restrict indices, int num_indices, struct Xt_stripe **stripes, int *num_stripes)
Definition: xt_stripe.c:63
void xt_convert_indices_to_stripes_keep_buf(const Xt_int *restrict indices, int num_indices, struct Xt_stripe **stripes, int *num_stripes)
Definition: xt_stripe.c:74