Yet Another eXchange Tool  0.9.0
xt_idxlist_intersection.c
Go to the documentation of this file.
1 
12 /*
13  * Maintainer: Jörg Behrens <behrens@dkrz.de>
14  * Moritz Hanke <hanke@dkrz.de>
15  * Thomas Jahns <jahns@dkrz.de>
16  * URL: https://doc.redmine.dkrz.de/yaxt/html/
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions are
20  * met:
21  *
22  * Redistributions of source code must retain the above copyright notice,
23  * this list of conditions and the following disclaimer.
24  *
25  * Redistributions in binary form must reproduce the above copyright
26  * notice, this list of conditions and the following disclaimer in the
27  * documentation and/or other materials provided with the distribution.
28  *
29  * Neither the name of the DKRZ GmbH nor the names of its contributors
30  * may be used to endorse or promote products derived from this software
31  * without specific prior written permission.
32  *
33  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
34  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
35  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
36  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
37  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
38  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
39  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
40  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
41  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
42  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
43  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  */
45 #ifdef HAVE_CONFIG_H
46 #include <config.h>
47 #endif
48 
49 #include <stdlib.h>
50 
51 #include "instr.h"
52 #include "core/ppm_xfuncs.h"
53 #include "xt/xt_core.h"
54 #include "xt/xt_idxlist.h"
55 #include "xt_idxlist_internal.h"
56 #include "xt_idxlist_unpack.h"
57 #include "xt/xt_idxempty.h"
58 #include "xt_idxempty_internal.h"
60 #include "xt/xt_idxvec.h"
61 #include "xt_idxvec_internal.h"
62 #include "xt_idxsection_internal.h"
63 #include "xt_idxstripes_internal.h"
64 
65 typedef Xt_idxlist (*intersection_get)(Xt_idxlist idxlist_src,
66  Xt_idxlist idxlist_dst);
67 
68 #define empty_isect ((intersection_get)(void (*)(void))xt_idxempty_new)
69 
70 static const intersection_get
75  { empty_isect, xt_default_isect, /* xt_idxlist_collection_get_intersection, */
83 };
84 
86 {
87 }
88 
91  Xt_idxlist idxlist_dst)
92 {
94  [idxlist_dst->vtable->idxlist_pack_code](idxlist_src, idxlist_dst);
95 }
96 
99  Xt_idxlist idxlist_dst)
100 {
101 
102  INSTR_DEF(instr_fallback,"xt_idxlist_get_intersection.fallback")
103 
104  // if the get_intersection routine was not able to compute the intersection
105  INSTR_START(instr_fallback);
106 
107  int num_indices_src, num_indices_dst;
108 
109  num_indices_src = xt_idxlist_get_num_indices(idxlist_src);
110  num_indices_dst = xt_idxlist_get_num_indices(idxlist_dst);
111 
112  if (num_indices_src == 0 || num_indices_dst == 0)
113  return xt_idxempty_new();
114 
115  Xt_idxlist intersection;
116  if (num_indices_src < CHEAP_VECTOR_SIZE
117  && num_indices_dst < CHEAP_VECTOR_SIZE) {
118  Xt_int *indices_src
119  = xmalloc(((size_t)num_indices_src + (size_t)num_indices_dst)
120  * sizeof (indices_src[0])),
121  *indices_dst = indices_src + num_indices_src;
122 
123  xt_idxlist_get_indices(idxlist_src, indices_src);
124  xt_idxlist_get_indices(idxlist_dst, indices_dst);
125 
126  Xt_idxlist idxvec_src = xt_idxvec_prealloc_new(indices_src, num_indices_src),
127  idxvec_dst = xt_idxvec_prealloc_new(indices_dst, num_indices_dst);
128 
129  intersection
130  = xt_idxvec_get_intersection(idxvec_src, idxvec_dst);
131 
132  xt_idxlist_delete(idxvec_src);
133  xt_idxlist_delete(idxvec_dst);
134  free(indices_src);
135  } else {
136  int num_stripes_src, num_stripes_dst;
137  struct Xt_stripe *stripes_src, *stripes_dst;
138  xt_idxlist_get_index_stripes(idxlist_src, &stripes_src, &num_stripes_src);
139  xt_idxlist_get_index_stripes(idxlist_dst, &stripes_dst, &num_stripes_dst);
140  Xt_idxlist idxstripes_src = xt_idxstripes_prealloc_new(stripes_src,
141  num_stripes_src),
142  idxstripes_dst = xt_idxstripes_prealloc_new(stripes_dst,
143  num_stripes_dst);
144  intersection
145  = xt_idxstripes_get_intersection(idxstripes_src, idxstripes_dst);
146  xt_idxlist_delete(idxstripes_dst);
147  xt_idxlist_delete(idxstripes_src);
148  free(stripes_dst);
149  free(stripes_src);
150  }
151 
152  INSTR_STOP(instr_fallback);
153  return intersection;
154 }
155 
156 /*
157  * Local Variables:
158  * c-basic-offset: 2
159  * coding: utf-8
160  * indent-tabs-mode: nil
161  * show-trailing-whitespace: t
162  * require-trailing-newline: t
163  * End:
164  */
165 
#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 xmalloc(size)
Definition: ppm_xfuncs.h:70
const struct xt_idxlist_vtable * vtable
base definitions header file
XT_INT Xt_int
Definition: xt_core.h:68
struct Xt_idxlist_ * Xt_idxlist
Definition: xt_core.h:80
Xt_idxlist xt_idxempty_new(void)
Definition: xt_idxempty.c:165
index list declaration
int xt_idxlist_get_num_indices(Xt_idxlist idxlist)
Definition: xt_idxlist.c:98
void xt_idxlist_get_indices(Xt_idxlist idxlist, Xt_int *indices)
Definition: xt_idxlist.c:102
void xt_idxlist_get_index_stripes(Xt_idxlist idxlist, struct Xt_stripe **stripes, int *num_stripes)
Definition: xt_idxlist.c:118
void xt_idxlist_delete(Xt_idxlist idxlist)
Definition: xt_idxlist.c:74
Provide non-public declarations common to all index lists.
@ CHEAP_VECTOR_SIZE
static const intersection_get intersection_get_matrix[num_idxlist_classes][num_idxlist_classes]
Xt_idxlist xt_default_isect(Xt_idxlist idxlist_src, Xt_idxlist idxlist_dst)
void xt_idxlist_intersection_init(void)
#define empty_isect
Xt_idxlist xt_idxlist_get_intersection(Xt_idxlist idxlist_src, Xt_idxlist idxlist_dst)
Xt_idxlist(* intersection_get)(Xt_idxlist idxlist_src, Xt_idxlist idxlist_dst)
@ num_idxlist_classes
Xt_idxlist xt_idxsection_get_intersection_with_other_idxlist(Xt_idxlist src_idxsection, Xt_idxlist dst_idxlist)
Xt_idxlist xt_idxsection_get_intersection(Xt_idxlist idxlist_src, Xt_idxlist idxlist_dst)
Xt_idxlist xt_idxstripes_prealloc_new(const struct Xt_stripe *stripes, int num_stripes)
Xt_idxlist xt_idxstripes_get_intersection(Xt_idxlist idxlist_src, Xt_idxlist idxlist_dst)
Xt_idxlist xt_idxvec_prealloc_new(const Xt_int *idxvec, int num_indices)
Definition: xt_idxvec.c:192
Xt_idxlist xt_idxvec_get_intersection(Xt_idxlist idxlist_src, Xt_idxlist idxlist_dst)
Definition: xt_idxvec.c:492