pl_search_cpp 1.7
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 <list>
29#include <sstream>
30#include <string>
31
32#include "term.hpp"
33#include "typedefs.hpp"
34
40 // CList objects replace Prolog lists for efficiency
41 // The list passed to the constructor is copied, so it can be safely modified
42 // after the CList is created. If a Prolog-like list is required, a user-defined
43 // class that implements the Term interface should be used.
44
45namespace pl_search {
46
53 class CList : public Term {
54 protected:
55 std::list<TermPtr> elements;
56
57 public:
62 CList(std::list<TermPtr> elems) : elements(elems) {}
63
68 std::string repr() const override {
69 std::ostringstream oss;
70 oss << "[";
71 for (auto it = elements.begin(); it != elements.end(); ++it) {
72 if (it != elements.begin()) {
73 oss << ", ";
74 }
75 oss << (*it)->dereference()->repr();
76 }
77 oss << "]";
78 return oss.str();
79 }
80
86 bool isEqualTo(Term& other) const override {
87 if (CList* list = dynamic_cast<CList*>(&other)) {
88 return elements == list->elements;
89 }
90 return false;
91 }
92
98 bool isLessThan(Term& other) const override;
99
100
111 const std::list<TermPtr> getElements() const { return elements; }
112 };
113
114} // namespace pl_search
115
116#endif // PL_SEARCH_CLIST_HPP
Represents a list of terms.
Definition clist.hpp:53
CList(std::list< TermPtr > elems)
Constructs a CList with the given elements.
Definition clist.hpp:62
std::list< TermPtr > elements
The elements of the list.
Definition clist.hpp:55
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:68
bool isEqualTo(Term &other) const override
Checks if the term is equal to another term.
Definition clist.hpp:86
const std::list< TermPtr > getElements() const
Gets the elements of the list.
Definition clist.hpp:111
Abstract base class for terms that approximate Prolog terms.
Definition term.hpp:50
Definition choice_iterator.hpp:16
Definition of the Term class.
Definition of common typedefs used in the pl_search library.