Files
carbon-lang/executable_semantics/interpreter/list_node.h
T
Jeremy G. Siek cd18e24176 Create a Dictionary abstraction over the raw Cons list. (#327)
* Create a Dictionary abstraction over the raw Cons list.

* renamed Cons and some methods of Dictionary, various other cleanup
2021-03-03 13:03:23 -05:00

64 lines
1.7 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef EXECUTABLE_SEMANTICS_INTERPRETER_LIST_NODE_H_
#define EXECUTABLE_SEMANTICS_INTERPRETER_LIST_NODE_H_
namespace Carbon {
template <class T>
struct ListNode {
ListNode(T e, ListNode* n) : curr(e), next(n) {}
const T curr;
ListNode* const next;
// ListNode cells are part of a "persistent data structure" and are thus
// immutable.
ListNode& operator=(const ListNode&) = delete;
ListNode& operator=(ListNode&&) = delete;
};
// A forward iterator over elements of a `ListNode` list.
template <class T>
struct ListNodeIterator {
using value_type = T;
using difference_type = std::ptrdiff_t;
using pointer = const T*;
using reference = const T&;
using iterator_category = std::forward_iterator_tag;
ListNodeIterator(ListNode<T>* x) : p(x) {}
ListNodeIterator(const ListNodeIterator& iter) : p(iter.p) {}
ListNodeIterator& operator++() {
p = p->next;
return *this;
}
ListNodeIterator operator++(int) {
ListNodeIterator tmp(*this);
operator++();
return tmp;
}
bool operator==(const ListNodeIterator& rhs) const { return p == rhs.p; }
bool operator!=(const ListNodeIterator& rhs) const { return p != rhs.p; }
const T& operator*() { return p->curr; }
const T* operator->() { return &p->curr; }
private:
ListNode<T>* p;
};
template <class T>
auto Length(ListNode<T>* ls) -> unsigned int {
if (ls) {
return 1 + Length(ls->next);
} else {
return 0;
}
}
} // namespace Carbon
#endif // EXECUTABLE_SEMANTICS_INTERPRETER_LIST_NODE_H_