pl_search_cpp 1.4
Loading...
Searching...
No Matches
choice_iterator.hpp
Go to the documentation of this file.
1/*
2MIT License
3
4...license text...
5
6*/
7
8#ifndef PL_SEARCH_CHOICE_ITERATOR_HPP
9#define PL_SEARCH_CHOICE_ITERATOR_HPP
10
11#include "engine.hpp"
12#include "typedefs.hpp"
13
14#include <vector>
15
16namespace pl_search {
17class Engine;
18class PVar;
19
24public:
29 virtual bool has_next() = 0;
30
35 virtual bool make_choice() = 0;
36
42 virtual bool test_choice() { return true; }
43
47 virtual ~ChoiceIterator() = default;
48};
49
54public:
61 VarChoiceIterator(Engine *engine, PVarPtr v, std::vector<TermPtr> ch)
62 : engine(engine), var(v), choices(ch), index(0) {}
63
68 bool has_next() override { return index < choices.size(); }
69
75 virtual bool make_choice() override {
76 TermPtr t = choices[index++];
77 return engine->unify(var, t) && test_choice();
78 }
79
80protected:
82 int index;
84 std::vector<TermPtr> choices;
85};
86
87} // namespace pl_search
88
89#endif // PL_SEARCH_CHOICE_ITERATOR_HPP
Base class for choice iterators.
Definition choice_iterator.hpp:23
virtual bool test_choice()
Tests a choice which would tyically involve constraint deductions based on the choice.
Definition choice_iterator.hpp:42
virtual bool has_next()=0
Checks if there are more choices available.
virtual ~ChoiceIterator()=default
Virtual destructor for proper cleanup.
virtual bool make_choice()=0
Makes a choice.
The Engine class manages the execution of predicates and backtracking.
Definition engine.hpp:69
bool unify(TermPtr t1, TermPtr t2)
Unifies two terms.
Definition engine.cpp:76
Represents a Prolog variable.
Definition pvar.hpp:44
Choice iterator for variables.
Definition choice_iterator.hpp:53
virtual bool make_choice() override
Makes a choice.
Definition choice_iterator.hpp:75
VarChoiceIterator(Engine *engine, PVarPtr v, std::vector< TermPtr > ch)
Constructs a VarChoiceIterator.
Definition choice_iterator.hpp:61
std::vector< TermPtr > choices
Reference to the vector of choices.
Definition choice_iterator.hpp:84
int index
Current index in the choices vector.
Definition choice_iterator.hpp:82
PVarPtr var
Pointer to the variable.
Definition choice_iterator.hpp:83
bool has_next() override
Checks if there are more choices available.
Definition choice_iterator.hpp:68
Engine * engine
Pointer to the Engine.
Definition choice_iterator.hpp:81
Definition of the Engine class and related structures.
Definition choice_iterator.hpp:16
std::shared_ptr< PVar > PVarPtr
Definition typedefs.hpp:44
std::shared_ptr< Term > TermPtr
Typedefs for shared pointers to Terms and subclasses.
Definition typedefs.hpp:43
Definition of common typedefs used in the pl_search library.