pl_search_cpp 1.4
Loading...
Searching...
No Matches
clist.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#ifndef PL_SEARCH_CLIST_HPP
26#define PL_SEARCH_CLIST_HPP
27
28#include "term.hpp"
29#include "typedefs.hpp"
30
31#include <list>
32#include <sstream>
33#include <string>
34
40// CList objects replace Prolog lists for efficiency
41// WARNING: Because the internal list is mutable care must be taken when using
42// CList objects. For example, it might be necessary to copy the list before
43// passing it to a function that might modify it or when binding a variable to
44// a CList object.
45// If a Prolog-like list is required, a user-defined class that implements the
46// Term interface should be used (for example ConsList).
47
48namespace pl_search {
49
58class CList : public Term {
59protected:
60 std::list<TermPtr> &elements;
61
62public:
67 CList(std::list<TermPtr> &elems) : elements(elems) {}
68
73 std::string repr() const override {
74 std::ostringstream oss;
75 oss << "[";
76 for (auto it = elements.begin(); it != elements.end(); ++it) {
77 if (it != elements.begin()) {
78 oss << ", ";
79 }
80 oss << (*it)->repr();
81 }
82 oss << "]";
83 return oss.str();
84 }
85
91 bool isEqualTo(Term &other) const override {
92 if (CList *list = dynamic_cast<CList *>(&other)) {
93 return elements == list->elements;
94 }
95 return false;
96 }
97
103 bool isLessThan(Term &other) const override;
104
110 void addElement(TermPtr element) { elements.push_back(element); }
111
116 const std::list<TermPtr> &getElements() const { return elements; }
117};
118
119} // namespace pl_search
120
121#endif // PL_SEARCH_CLIST_HPP
Represents a list of terms.
Definition clist.hpp:58
CList(std::list< TermPtr > &elems)
Constructs a CList with the given elements.
Definition clist.hpp:67
const std::list< TermPtr > & getElements() const
Returns the elements of the list.
Definition clist.hpp:116
bool isLessThan(Term &other) const override
< operator for a CList and a Term
Definition term.cpp:141
std::string repr() const override
Returns a string representation of the list.
Definition clist.hpp:73
std::list< TermPtr > & elements
The elements of the list.
Definition clist.hpp:60
bool isEqualTo(Term &other) const override
Checks if the term is equal to another term.
Definition clist.hpp:91
void addElement(TermPtr element)
Adds an element to the list.
Definition clist.hpp:110
Abstract base class for terms that approximate Prolog terms.
Definition term.hpp:50
Definition choice_iterator.hpp:16
std::shared_ptr< Term > TermPtr
Typedefs for shared pointers to Terms and subclasses.
Definition typedefs.hpp:43
Definition of the Term class.
Definition of common typedefs used in the pl_search library.