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
This commit is contained in:
Jeremy G. Siek
2021-03-03 13:03:23 -05:00
committed by GitHub
parent 1204f2bdcc
commit cd18e24176
7 changed files with 131 additions and 47 deletions
+6 -4
View File
@@ -9,6 +9,8 @@
#include <cstddef>
#include <iterator>
#include "executable_semantics/interpreter/list_node.h"
namespace Carbon {
/// A persistent stack data structure.
@@ -24,7 +26,7 @@ struct Stack {
using reference = const T&;
using iterator_category = std::forward_iterator_tag;
Iterator(Cons<T>* x) : p(x) {}
Iterator(ListNode<T>* x) : p(x) {}
Iterator(const Iterator& mit) : p(mit.p) {}
Iterator& operator++() {
p = p->next;
@@ -41,7 +43,7 @@ struct Stack {
const T* operator->() { return &p->curr; }
private:
Cons<T>* p;
ListNode<T>* p;
};
/// The position of the first/`Top()` element, or `end()` if
@@ -58,7 +60,7 @@ struct Stack {
Stack(T x) : Stack() { Push(x); }
/// Pushes `x` onto the top of the stack.
void Push(T x) { head = new Cons<T>(x, head); }
void Push(T x) { head = new ListNode<T>(x, head); }
/// Returns a copy of `*this`, with `x` pushed onto the top.
auto Pushing(T x) const -> Stack {
@@ -128,7 +130,7 @@ struct Stack {
private:
/// An linked list of cells containing the elements of self.
Cons<T>* head;
ListNode<T>* head;
};
} // namespace Carbon