Yet Another eXchange Tool 0.11.4
Loading...
Searching...
No Matches
xt_idxstripes_pos_ext_map.h
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://dkrz-sw.gitlab-pages.dkrz.de/yaxt/
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/* this code maps and optionally appends to an array of stripes */
48static
49#ifdef XT_IDXSTRIPES_POS_EXT_MAP_COUNT
50#define get_mapped_stripes get_mapped_stripes_count
51size_t
52#else
53#define get_mapped_stripes map_ext2stripes
54void
55#endif
56get_mapped_stripes(size_t num_ext,
57 struct Xt_pos_ext *restrict pos_exts,
58 Xt_idxstripes idxstripes_src
60 , struct Xt_stripe *restrict result_stripes
61#endif
62)
63{
64 size_t num_result_stripes = 0;
65 size_t stripes_src_ofs = 0, pos = 0;
66 struct Xt_stripe result_stripe = {
67 .start = XT_INT_MIN, .stride = -1, .nstrides = 0 };
68 Xt_int result_stripe_next = XT_INT_MIN - 1;
69 const struct Xt_stripe *src_stripes = idxstripes_src->stripes;
70 for (size_t i = 0; i < num_ext; ++i) {
71 size_t start_pos = (size_t)pos_exts[i].start;
72 int size = pos_exts[i].size;
73 if (start_pos < pos) {
74 do {
75 --stripes_src_ofs;
76 pos -= (size_t)src_stripes[stripes_src_ofs].nstrides;
77 } while (start_pos < pos);
78 } else {
79 while (start_pos >= pos + (size_t)src_stripes[stripes_src_ofs].nstrides) {
80 pos += (size_t)src_stripes[stripes_src_ofs].nstrides;
81 ++stripes_src_ofs;
82 }
83 }
84 assert(stripes_src_ofs < (size_t)idxstripes_src->num_stripes);
85 /* add overlap with current stripe to result set */
86 if (size > 0) {
87 build_incr_overlap:;
88 struct Xt_stripe overlapping_stripe = src_stripes[stripes_src_ofs];
89 size_t remaining_nstrides
90 = pos + (size_t)overlapping_stripe.nstrides - start_pos;
91 int intersection_size = MIN((int)remaining_nstrides, size);
92 Xt_int intersection_stride = overlapping_stripe.stride,
93 intersection_stripe_start = (Xt_int)(
94 overlapping_stripe.start +
95 (Xt_int)((size_t)overlapping_stripe.nstrides - remaining_nstrides)
96 * intersection_stride);
97 if ((intersection_stride == result_stripe.stride
98 || intersection_size == 1)
99 && intersection_stripe_start == result_stripe_next) {
100 result_stripe.nstrides += intersection_size;
101 intersection_stride = result_stripe.stride;
102 } else if (result_stripe.nstrides == 1
103 && result_stripe.start
104 == intersection_stripe_start - intersection_stride)
105 {
106 result_stripe.nstrides += intersection_size;
107 result_stripe.stride = intersection_stride;
108 } else if (result_stripe.nstrides == 1
109 && intersection_size == 1) {
110 result_stripe.nstrides += intersection_size;
111 result_stripe.stride
112 = intersection_stride
113 = (Xt_int)(intersection_stripe_start - result_stripe.start);
114 } else {
115#ifndef XT_IDXSTRIPES_POS_EXT_MAP_COUNT
116 if (num_result_stripes)
117 result_stripes[num_result_stripes-1] = result_stripe;
118#endif
119 ++num_result_stripes;
120 result_stripe = (struct Xt_stripe){
121 .start = intersection_stripe_start,
122 .stride = intersection_stride,
123 .nstrides = intersection_size };
124 }
125 result_stripe_next = (Xt_int)(intersection_stripe_start
126 + intersection_stride * intersection_size);
127 if (size -= intersection_size) {
128 start_pos += (size_t)intersection_size;
129 pos += (size_t)overlapping_stripe.nstrides;
130 ++stripes_src_ofs;
131 goto build_incr_overlap;
132 }
133 } else {
134 build_decr_overlap:;
135 struct Xt_stripe overlapping_stripe = src_stripes[stripes_src_ofs];
136 size_t remaining_nstrides
137 = start_pos - pos;
138 int intersection_size = MIN((int)remaining_nstrides, abs(size));
139 Xt_int intersection_stride = (Xt_int)-overlapping_stripe.stride,
140 intersection_stripe_start = (Xt_int)(
141 overlapping_stripe.start +
142 (Xt_int)remaining_nstrides * (-intersection_stride));
143 if ((intersection_stride == result_stripe.stride
144 || intersection_size == 1)
145 && intersection_stripe_start == result_stripe_next) {
146 result_stripe.nstrides += intersection_size;
147 intersection_stride = result_stripe.stride;
148 } else if (result_stripe.nstrides == 1
149 && result_stripe.start
150 == intersection_stripe_start - intersection_stride)
151 {
152 result_stripe.nstrides += intersection_size;
153 result_stripe.stride = intersection_stride;
154 } else if (result_stripe.nstrides == 1
155 && intersection_size == 1) {
156 result_stripe.nstrides += intersection_size;
157 result_stripe.stride
158 = intersection_stride
159 = (Xt_int)(intersection_stripe_start - result_stripe.start);
160 } else {
161#ifndef XT_IDXSTRIPES_POS_EXT_MAP_COUNT
162 if (num_result_stripes)
163 result_stripes[num_result_stripes-1] = result_stripe;
164#endif
165 ++num_result_stripes;
166 result_stripe = (struct Xt_stripe){
167 .start = intersection_stripe_start,
168 .stride = intersection_stride,
169 .nstrides = intersection_size };
170 }
171 result_stripe_next = (Xt_int)(intersection_stripe_start
172 + intersection_stride * intersection_size);
173 if (size += intersection_size) {
174 start_pos -= (size_t)intersection_size;
175 --stripes_src_ofs;
176 pos -= (size_t)src_stripes[stripes_src_ofs].nstrides;
177 goto build_decr_overlap;
178 }
179 }
180 }
181#ifdef XT_IDXSTRIPES_POS_EXT_MAP_COUNT
182 return num_result_stripes;
183#else
184 result_stripes[num_result_stripes-1] = result_stripe;
185#endif
186}
187
188#undef get_mapped_stripes
189
190/*
191 * Local Variables:
192 * c-basic-offset: 2
193 * coding: utf-8
194 * indent-tabs-mode: nil
195 * show-trailing-whitespace: t
196 * require-trailing-newline: t
197 * End:
198 */
Xt_int stride
Definition xt_stripe.h:56
int nstrides
Definition xt_stripe.h:57
Xt_int start
Definition xt_stripe.h:55
#define XT_INT_MIN
Definition xt_core.h:78
XT_INT Xt_int
Definition xt_core.h:72
#define MIN(a, b)
#define XT_IDXSTRIPES_POS_EXT_MAP_COUNT
#define get_mapped_stripes