pl_search_cpp 2.1
Loading...
Searching...
No Matches
trail.hpp
Go to the documentation of this file.
1/*
2MIT License
3
4Copyright (c) 2025 [Peter Robinson]
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24
25
26#ifndef PL_SEARCH_TRAIL_HPP
27#define PL_SEARCH_TRAIL_HPP
28
33#include "term.hpp"
34#include "clist.hpp"
35#include "patom.hpp"
36#include "pfloat.hpp"
37#include "pint.hpp"
38#include "pvar.hpp"
39#include "typedefs.hpp"
40
41#include <memory>
42#include <stack>
43
44namespace pl_search {
45
50
64 class Trail {
65
66 protected:
67 std::stack<std::shared_ptr<trail_entry>> trail_stack;
68
69 public:
70
71 friend class TailTest;
72
78 auto entry = std::make_shared<trail_entry>();
79 entry->var = v;
80 entry->value = v->value; // v-> value is always nullptr except for UpdatablePVar variables
81 trail_stack.push(entry);
82 }
83
88 void unbind_to(int index) {
89 while (trail_stack.size() > index) {
90 auto entry = trail_stack.top();
91 entry->var->reset(entry->value);
92 trail_stack.pop();
93 }
94 }
95
100 int size() const { return trail_stack.size(); }
101
102 // for testing
103
105 return trail_stack.top()->var;
106 }
107
108 };
109
110} // namespace pl_search
111
112#endif // PL_SEARCH_TRAIL_HPP
The Trail class is used to manage variable bindings during the execution of Prolog-like predicates.
Definition trail.hpp:64
std::stack< std::shared_ptr< trail_entry > > trail_stack
The stack of trail entries.
Definition trail.hpp:67
PVarPtr top()
Definition trail.hpp:104
friend class TailTest
Definition trail.hpp:71
void trail_var(PVarPtr v)
Trails a variable.
Definition trail.hpp:77
void unbind_to(int index)
Unbind (reset) variables back to a certain point in the trail.
Definition trail.hpp:88
int size() const
Returns the number of trail entries in the trail (top of stack)
Definition trail.hpp:100
Definition of the CList class.
Definition clist.hpp:45
std::shared_ptr< PVar > PVarPtr
Definition typedefs.hpp:45
std::shared_ptr< Term > TermPtr
Typedefs for shared pointers to Terms and subclasses.
Definition typedefs.hpp:44
Definition of the PAtom class.
Definition of the PFloat class.
Definition of the PInt class.
Definition of the PVar class.
Definition trail.hpp:46
PVarPtr var
Definition trail.hpp:47
TermPtr value
Definition trail.hpp:48
Definition of the Term class.
Definition of common typedefs used in the pl_search library.