Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- builtin_aggregate(GeneratorPred, CollectorPred, !Accumulator) :-
- % Save some of the Mercury virtual machine registers
- impure get_registers(HeapPtr, SolutionsHeapPtr, TrailPtr),
- impure start_all_soln_neg_context,
- % Initialize the accumulator
- % /* Mutvar := !.Accumulator */
- impure new_mutvar(!.Accumulator, Mutvar),
- (
- % Get a solution.
- GeneratorPred(Answer0),
- % Check that the generator didn't leave any delayed goals outstanding.
- impure check_for_floundering(TrailPtr),
- % Update the accumulator.
- % /* Mutvar := CollectorPred(MutVar) */
- impure swap_heap_and_solutions_heap,
- impure partial_deep_copy(HeapPtr, Answer0, Answer),
- impure get_mutvar(Mutvar, Acc0),
- impure non_cc_call(CollectorPred, Answer, Acc0, Acc1),
- impure set_mutvar(Mutvar, Acc1),
- impure swap_heap_and_solutions_heap,
- % Force backtracking, so that we get the next solution.
- % This will automatically reset the heap and trail.
- fail
- ;
- % There are no more solutions.
- impure end_all_soln_neg_context_no_more,
- % So now we just need to copy the final value of the accumulator
- % from the solutions heap back onto the ordinary heap, and then we can
- % reset the solutions heap pointer. We also need to discard the trail
- % ticket created by get_registers/3.
- % /* !:Accumulator := Mutvar */
- impure get_mutvar(Mutvar, !:Accumulator),
- impure partial_deep_copy(SolutionsHeapPtr, !Accumulator),
- impure reset_solutions_heap(SolutionsHeapPtr),
- impure discard_trail_ticket
- ).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement