YAP 7.1.0
base_dbtries.c
1/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2%
3% $Date: 2010-06-30 17:05:24 +0200 (Wed, 30 Jun 2010) $
4% $Revision: 1 $
5%
6% This file is part of YAP & ProbLog
7% http://www.dcc.fc.up.pt/~vsc/Yap/index.html
8% http://dtai.cs.kuleuven.be/problog
9%
10% ProbLog was developed at Katholieke Universiteit Leuven &
11% University of Porto
12%
13% Copyright 2010 Katholieke Universiteit Leuven & University of Porto
14%
15% Main authors of this file: Mantadelis Theofrastos, Ricardo Rocha
16%
17%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
18%
19% Artistic License 2.0
20%
21% Copyright (c) 2000-2006, The Perl Foundation.
22%
23% Everyone is permitted to copy and distribute verbatim copies of this
24% license document, but changing it is not allowed. Preamble
25%
26% This license establishes the terms under which a given free software
27% Package may be copied, modified, distributed, and/or
28% redistributed. The intent is that the Copyright Holder maintains some
29% artistic control over the development of that Package while still
30% keeping the Package available as open source and free software.
31%
32% You are always permitted to make arrangements wholly outside of this
33% license directly with the Copyright Holder of a given Package. If the
34% terms of this license do not permit the full use that you propose to
35% make of the Package, you should contact the Copyright Holder and seek
36% a different licensing arrangement. Definitions
37%
38% "Copyright Holder" means the individual(s) or organization(s) named in
39% the copyright notice for the entire Package.
40%
41% "Contributor" means any party that has contributed code or other
42% material to the Package, in accordance with the Copyright Holder's
43% procedures.
44%
45% "You" and "your" means any person who would like to copy, distribute,
46% or modify the Package.
47%
48% "Package" means the collection of files distributed by the Copyright
49% Holder, and derivatives of that collection and/or of those files. A
50% given Package may consist of either the Standard Version, or a
51% Modified Version.
52%
53% "Distribute" means providing a copy of the Package or making it
54% accessible to anyone else, or in the case of a company or
55% organization, to others outside of your company or organization.
56%
57% "Distributor Fee" means any fee that you charge for Distributing this
58% Package or providing support for this Package to another party. It
59% does not mean licensing fees.
60%
61% "Standard Version" refers to the Package if it has not been modified,
62% or has been modified only in ways explicitly requested by the
63% Copyright Holder.
64%
65% "Modified Version" means the Package, if it has been changed, and such
66% changes were not explicitly requested by the Copyright Holder.
67%
68% "Original License" means this Artistic License as Distributed with the
69% Standard Version of the Package, in its current version or as it may
70% be modified by The Perl Foundation in the future.
71%
72% "Source" form means the source code, documentation source, and
73% configuration files for the Package.
74%
75% "Compiled" form means the compiled bytecode, object code, binary, or
76% any other form resulting from mechanical transformation or translation
77% of the Source form.
78%
79%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80%
81% Permission for Use and Modification Without Distribution
82%
83% (1) You are permitted to use the Standard Version and create and use
84% Modified Versions for any purpose without restriction, provided that
85% you do not Distribute the Modified Version.
86%
87% Permissions for Redistribution of the Standard Version
88%
89% (2) You may Distribute verbatim copies of the Source form of the
90% Standard Version of this Package in any medium without restriction,
91% either gratis or for a Distributor Fee, provided that you duplicate
92% all of the original copyright notices and associated disclaimers. At
93% your discretion, such verbatim copies may or may not include a
94% Compiled form of the Package.
95%
96% (3) You may apply any bug fixes, portability changes, and other
97% modifications made available from the Copyright Holder. The resulting
98% Package will still be considered the Standard Version, and as such
99% will be subject to the Original License.
100%
101% Distribution of Modified Versions of the Package as Source
102%
103% (4) You may Distribute your Modified Version as Source (either gratis
104% or for a Distributor Fee, and with or without a Compiled form of the
105% Modified Version) provided that you clearly document how it differs
106% from the Standard Version, including, but not limited to, documenting
107% any non-standard features, executables, or modules, and provided that
108% you do at least ONE of the following:
109%
110% (a) make the Modified Version available to the Copyright Holder of the
111% Standard Version, under the Original License, so that the Copyright
112% Holder may include your modifications in the Standard Version. (b)
113% ensure that installation of your Modified Version does not prevent the
114% user installing or running the Standard Version. In addition, the
115% modified Version must bear a name that is different from the name of
116% the Standard Version. (c) allow anyone who receives a copy of the
117% Modified Version to make the Source form of the Modified Version
118% available to others under (i) the Original License or (ii) a license
119% that permits the licensee to freely copy, modify and redistribute the
120% Modified Version using the same licensing terms that apply to the copy
121% that the licensee received, and requires that the Source form of the
122% Modified Version, and of any works derived from it, be made freely
123% available in that license fees are prohibited but Distributor Fees are
124% allowed.
125%
126% Distribution of Compiled Forms of the Standard Version or
127% Modified Versions without the Source
128%
129% (5) You may Distribute Compiled forms of the Standard Version without
130% the Source, provided that you include complete instructions on how to
131% get the Source of the Standard Version. Such instructions must be
132% valid at the time of your distribution. If these instructions, at any
133% time while you are carrying out such distribution, become invalid, you
134% must provide new instructions on demand or cease further
135% distribution. If you provide valid instructions or cease distribution
136% within thirty days after you become aware that the instructions are
137% invalid, then you do not forfeit any of your rights under this
138% license.
139%
140% (6) You may Distribute a Modified Version in Compiled form without the
141% Source, provided that you comply with Section 4 with respect to the
142% Source of the Modified Version.
143%
144% Aggregating or Linking the Package
145%
146% (7) You may aggregate the Package (either the Standard Version or
147% Modified Version) with other packages and Distribute the resulting
148% aggregation provided that you do not charge a licensing fee for the
149% Package. Distributor Fees are permitted, and licensing fees for other
150% components in the aggregation are permitted. The terms of this license
151% apply to the use and Distribution of the Standard or Modified Versions
152% as included in the aggregation.
153%
154% (8) You are permitted to link Modified and Standard Versions with
155% other works, to embed the Package in a larger work of your own, or to
156% build stand-alone binary or bytecode versions of applications that
157% include the Package, and Distribute the result without restriction,
158% provided the result does not expose a direct interface to the Package.
159%
160% Items That are Not Considered Part of a Modified Version
161%
162% (9) Works (including, but not limited to, modules and scripts) that
163% merely extend or make use of the Package, do not, by themselves, cause
164% the Package to be a Modified Version. In addition, such works are not
165% considered parts of the Package itself, and are not subject to the
166% terms of this license.
167%
168% General Provisions
169%
170% (10) Any use, modification, and distribution of the Standard or
171% Modified Versions is governed by this Artistic License. By using,
172% modifying or distributing the Package, you accept this license. Do not
173% use, modify, or distribute the Package, if you do not accept this
174% license.
175%
176% (11) If your Modified Version has been derived from a Modified Version
177% made by someone other than you, you are nevertheless required to
178% ensure that your Modified Version complies with the requirements of
179% this license.
180%
181% (12) This license does not grant you the right to use any trademark,
182% service mark, tradename, or logo of the Copyright Holder.
183%
184% (13) This license includes the non-exclusive, worldwide,
185% free-of-charge patent license to make, have made, use, offer to sell,
186% sell, import and otherwise transfer the Package with respect to any
187% patent claims licensable by the Copyright Holder that are necessarily
188% infringed by the Package. If you institute patent litigation
189% (including a cross-claim or counterclaim) against any party alleging
190% that the Package constitutes direct or contributory patent
191% infringement, then this Artistic License to you shall terminate on the
192% date that such litigation is filed.
193%
194% (14) Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT
195% HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED
196% WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
197% PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT
198% PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT
199% HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT,
200% INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
201% OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
202%
203%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
204
205#include <stdlib.h>
206
207/* -------------------------- */
208/* Local Procedures */
209/* -------------------------- */
210
211static void simplification_reduction(TrEntry trie);
212static TrNode depth_reduction(TrEntry trie, TrNode depth_node, YAP_Int opt_level);
213static TrNode breadth_reduction(TrEntry trie, TrNode breadth_node, YAP_Int opt_level);
214static inline int compare_label_nodes(TrData data1, TrData data2);
215static inline void move_after(TrData data_source, TrData data_dest);
216static inline void move_last_data_after(TrData moveto_data);
217static inline void set_depth_breadth_reduction_current_data(TrData data);
218
219
220/* -------------------------- */
221/* Local Variables */
222/* -------------------------- */
223
224static TrData CURRENT_DEPTH_BREADTH_DATA;
225
226
227/* -------------------------- */
228/* API */
229/* -------------------------- */
230
231
232YAP_Term trie_depth_breadth(TrEntry trie, TrEntry db_trie, YAP_Int opt_level, YAP_Int start_counter, YAP_Int *end_counter) {
233 TrNode depth_node, breadth_node, nested_trie;
234 TrEntry otrie = CURRENT_TRIE;
235 core_set_label_counter(start_counter);
236 CURRENT_TRIE = db_trie;
237 core_set_trie_db_return_term(YAP_MkAtomTerm(YAP_LookupAtom("false")));
238 core_initialize_depth_breadth_trie(TrEntry_trie(db_trie), &depth_node, &breadth_node);
239 set_depth_breadth_reduction_current_data(NULL);
240 /* We only need to simplify the trie once! */
241 /* This can be a 10% overhead for sld cases :-( */
242// printf("simplification\n"); trie_print(trie);
243 if (TrNode_child(TrEntry_trie(trie)))
244 simplification_reduction(trie);
245 while (TrNode_child(TrEntry_trie(trie))) {
246// printf("depth\n"); trie_print(trie);
247 nested_trie = depth_reduction(trie, depth_node, opt_level);
248 if (nested_trie) {
249 set_depth_breadth_reduction_current_data(get_data_from_trie_node(nested_trie));
250 core_finalize_depth_breadth_trie(depth_node, breadth_node);
251 *end_counter = core_get_label_counter();
252 CURRENT_TRIE = otrie;
253 return YAP_MkApplTerm((YAP_Functor)(~ApplTag & TrNode_entry(TrNode_parent(nested_trie))), 1, &TrNode_entry(nested_trie));
254 }
255// printf("breadth\n"); trie_print(trie);
256 nested_trie = breadth_reduction(trie, breadth_node, opt_level);
257 if (nested_trie) {
258 set_depth_breadth_reduction_current_data(get_data_from_trie_node(nested_trie));
259 core_finalize_depth_breadth_trie(depth_node, breadth_node);
260 *end_counter = core_get_label_counter();
261 CURRENT_TRIE = otrie;
262 return YAP_MkApplTerm((YAP_Functor)(~ApplTag & TrNode_entry(TrNode_parent(nested_trie))), 1, &TrNode_entry(nested_trie));
263 }
264 }
265 core_finalize_depth_breadth_trie(depth_node, breadth_node);
266 *end_counter = core_get_label_counter();
267 CURRENT_TRIE = otrie;
268 return core_get_trie_db_return_term();
269}
270
271
272
273YAP_Int trie_get_db_opt_level_count(YAP_Int opt_level) {
274 return core_db_trie_get_optimization_level_count(opt_level);
275}
276
277
278
279TrData trie_get_depth_breadth_reduction_current_data(void) {
280 return CURRENT_DEPTH_BREADTH_DATA;
281}
282
283
284
285void trie_replace_nested_trie(TrEntry trie, YAP_Int nested_trie_id, YAP_Term new_term) {
286 TrEntry otrie = CURRENT_TRIE;
287 CURRENT_TRIE = trie;
288 core_depth_breadth_trie_replace_nested_trie(TrNode_child(TrEntry_trie(trie)), nested_trie_id, new_term, &trie_data_construct, &trie_data_destruct);
289 CURRENT_TRIE = otrie;
290 return;
291}
292
293
294
295YAP_Int trie_get_db_opt_min_prefix(void) {
296 return core_get_trie_db_opt_min_prefix();
297}
298
299
300
301void trie_set_db_opt_min_prefix(YAP_Int min_prefix) {
302 core_set_trie_db_opt_min_prefix(min_prefix);
303 return;
304}
305
306
307/* -------------------------- */
308/* Local Procedures */
309/* -------------------------- */
310
311
312static inline
313void set_depth_breadth_reduction_current_data(TrData data) {
314 CURRENT_DEPTH_BREADTH_DATA = data;
315 return;
316}
317
318
319static
320void simplification_reduction(TrEntry trie) {
321 TrNode node;
322 TrData stop_data, new_data, data = NULL;
323 stop_data = TrData_previous(TrEntry_first_data(trie));
324 data = TrEntry_traverse_data(trie) = TrEntry_last_data(trie);
325 while (data && (data != stop_data) && (TrData_trie(data))) {
326 node = core_simplification_reduction(TRIE_ENGINE, TrData_leaf(data), &trie_data_destruct);
327 if (node) {
328 new_trie_data(new_data, trie, node);
329 PUT_DATA_IN_LEAF_TRIE_NODE(node, new_data);
330 data = NULL;
331 }
332 if (data && TrEntry_traverse_data(trie) && TrEntry_traverse_data(trie) != stop_data && !TrData_trie(data)) {
333 data = TrData_previous(data);
334 TrEntry_traverse_data(trie) = data;
335 } else if (data && TrEntry_traverse_data(trie) && TrEntry_traverse_data(trie) != stop_data && data == TrEntry_traverse_data(trie)) {
336 data = TrData_previous(data);
337 TrEntry_traverse_data(trie) = data;
338 } else
339 data = TrEntry_traverse_data(trie);
340 }
341}
342
343
344static
345TrNode depth_reduction(TrEntry trie, TrNode depth_node, YAP_Int opt_level) {
346 TrNode node;
347 TrData stop_data, new_data, data = NULL;
348
349 stop_data = TrData_previous(TrEntry_first_data(trie));
350 data = TrEntry_traverse_data(trie) = TrEntry_last_data(trie);
351 while (data && (data != stop_data) && (TrData_trie(data))) {
352 node = core_depth_reduction(TRIE_ENGINE, TrData_leaf(data), depth_node, opt_level, &trie_data_construct, &trie_data_destruct, &trie_data_copy, &trie_data_order_correction);
353 if (node && IS_FUNCTOR_NODE(TrNode_parent(node)) && (strcmp(YAP_AtomName(YAP_NameOfFunctor((YAP_Functor)(~ApplTag & TrNode_entry(TrNode_parent(node))))), NESTED_TRIE_TERM) == 0)) {
354 //nested trie stop procedure return nested trie node
355 return node;
356 }
357 if (node) {
358 new_trie_data(new_data, trie, node);
359 PUT_DATA_IN_LEAF_TRIE_NODE(node, new_data);
360 data = NULL;
361 }
362 if (data && TrEntry_traverse_data(trie) && TrEntry_traverse_data(trie) != stop_data && !TrData_trie(data)) {
363 data = TrData_previous(data);
364 TrEntry_traverse_data(trie) = data;
365 } else if (data && TrEntry_traverse_data(trie) && TrEntry_traverse_data(trie) != stop_data && data == TrEntry_traverse_data(trie)) {
366 data = TrData_previous(data);
367 TrEntry_traverse_data(trie) = data;
368 } else
369 data = TrEntry_traverse_data(trie);
370 }
371 return NULL;
372}
373
374
375static
376TrNode breadth_reduction(TrEntry trie, TrNode breadth_node, YAP_Int opt_level) {
377 TrNode node;
378 TrData stop_data, new_data, data = NULL;
379
380 stop_data = TrData_previous(TrEntry_first_data(trie));
381 data = TrEntry_traverse_data(trie) = TrEntry_last_data(trie);
382 while (data && (data != stop_data) && (TrData_trie(data))) {
383 node = core_breadth_reduction(TRIE_ENGINE, TrData_leaf(data), breadth_node, opt_level, &trie_data_construct, &trie_data_destruct, &trie_data_copy, &trie_data_order_correction);
384 if (node && IS_FUNCTOR_NODE(TrNode_parent(node)) && (strcmp(YAP_AtomName(YAP_NameOfFunctor((YAP_Functor)(~ApplTag & TrNode_entry(TrNode_parent(node))))), NESTED_TRIE_TERM) == 0)) {
385 //nested trie stop procedure return nested trie node
386 return node;
387 }
388 if (node) {
389 new_trie_data(new_data, trie, node);
390 PUT_DATA_IN_LEAF_TRIE_NODE(node, new_data);
391 data = NULL;
392 }
393 if (data && TrEntry_traverse_data(trie) && TrEntry_traverse_data(trie) != stop_data && !TrData_trie(data)) {
394 data = TrData_previous(data);
395 TrEntry_traverse_data(trie) = data;
396 } else if (data && TrEntry_traverse_data(trie) && TrEntry_traverse_data(trie) != stop_data && data == TrEntry_traverse_data(trie)) {
397 data = TrData_previous(data);
398 TrEntry_traverse_data(trie) = data;
399 } else
400 data = TrEntry_traverse_data(trie);
401 }
402 return NULL;
403}
404
405
406static inline
407void move_last_data_after(TrData moveto_data) {
408 TrEntry trie = CURRENT_TRIE;
409 TrData last_data = TrEntry_last_data(trie);
410 TrEntry_last_data(trie) = TrData_previous(last_data);
411 TrData_next(TrData_previous(last_data)) = TrData_next(last_data);
412 if (moveto_data == TrData_previous(TrEntry_first_data(trie))) {
413 TrData_next(last_data) = TrEntry_first_data(trie);
414 TrEntry_first_data(trie) = last_data;
415 } else {
416 TrData_next(last_data) = TrData_next(moveto_data);
417 TrData_next(moveto_data) = last_data;
418 }
419 TrData_previous(last_data) = moveto_data;
420 TrData_previous(TrData_next(last_data)) = last_data;
421 return;
422}
423
424
425static inline
426void move_after(TrData data_source, TrData data_dest) {
427 TrEntry trie = CURRENT_TRIE;
428 if (data_source == TrEntry_first_data(trie))
429 TrEntry_first_data(trie) = TrData_next(data_source);
430 else
431 TrData_next(TrData_previous(data_source)) = TrData_next(data_source);
432 if (data_source == TrEntry_last_data(trie))
433 TrEntry_last_data(trie) = TrData_previous(data_source);
434 else
435 TrData_previous(TrData_next(data_source)) = TrData_previous(data_source);
436
437 if (data_dest == TrData_previous(TrEntry_first_data(trie))) {
438 TrData_next(data_source) = TrEntry_first_data(trie);
439 TrData_previous(TrEntry_first_data(trie)) = data_source;
440 TrEntry_first_data(trie) = data_source;
441 } else {
442 TrData_next(data_source) = TrData_next(data_dest);
443 if (data_dest == TrEntry_last_data(trie))
444 TrEntry_last_data(trie) = data_source;
445 else
446 TrData_previous(TrData_next(data_dest)) = data_source;
447 TrData_next(data_dest) = data_source;
448 }
449 TrData_previous(data_source) = data_dest;
450 return;
451}
452
453
454void trie_data_order_correction(void) {
455 TrEntry trie = CURRENT_TRIE;
456 TrData inserted_data = TrEntry_last_data(trie);
457 TrData moved_data = TrData_previous(inserted_data);
458 TrData moveto_data = TrData_previous(moved_data);
459
460 while((moveto_data != TrData_previous(TrEntry_first_data(trie))) && (compare_label_nodes(moveto_data, inserted_data) == 1)) {
461 if (compare_label_nodes(moveto_data, moved_data) == 2)
462 moved_data = moveto_data;
463 moveto_data = TrData_previous(moveto_data);
464 }
465 move_last_data_after(moveto_data);
466
467 do {
468 moveto_data = TrData_next(inserted_data);
469 while(compare_label_nodes(moveto_data, moved_data) == 2)
470 moveto_data = TrData_next(moveto_data);
471 inserted_data = moved_data;
472 moved_data = TrData_next(moved_data);
473 if (moved_data != moveto_data)
474 move_after(inserted_data, TrData_previous(moveto_data));
475 } while(moved_data);
476 return;
477}
478
479
480static inline
481int compare_label_nodes(TrData data1, TrData data2) {
482 YAP_Term t1 = TrNode_entry(TrData_leaf(data1)), t2 = TrNode_entry(TrData_leaf(data2));
483 YAP_Int i1 = atol(YAP_AtomName(YAP_AtomOfTerm(t1)) + 1), i2 = atol(YAP_AtomName(YAP_AtomOfTerm(t2)) + 1);
484 if (i1 == i2) return 0;
485 if (i1 > i2) return 1;
486 return 2;
487}
Definition: base_itries.h:27