mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
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:
@@ -18,8 +18,8 @@ cc_library(
|
||||
hdrs = [
|
||||
"action.h",
|
||||
"assoc_list.h",
|
||||
"cons_list.h",
|
||||
"interpreter.h",
|
||||
"list_node.h",
|
||||
"stack.h",
|
||||
"typecheck.h",
|
||||
"value.h",
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
#include "executable_semantics/ast/expression.h"
|
||||
#include "executable_semantics/ast/statement.h"
|
||||
#include "executable_semantics/interpreter/cons_list.h"
|
||||
#include "executable_semantics/interpreter/stack.h"
|
||||
#include "executable_semantics/interpreter/value.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
// 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_CONS_LIST_H_
|
||||
#define EXECUTABLE_SEMANTICS_INTERPRETER_CONS_LIST_H_
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
template <class T>
|
||||
struct Stack;
|
||||
|
||||
template <class T>
|
||||
struct Cons {
|
||||
friend struct Stack<T>;
|
||||
|
||||
private:
|
||||
Cons(T e, Cons* n) : curr(e), next(n) {}
|
||||
|
||||
const T curr;
|
||||
Cons* const next;
|
||||
|
||||
// Cons cells are part of a "persistent data structure" and are thus
|
||||
// immutable.
|
||||
Cons& operator=(const Cons&) = delete;
|
||||
Cons& operator=(Cons&&) = delete;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
auto Length(Cons<T>* ls) -> unsigned int {
|
||||
if (ls) {
|
||||
return 1 + Length(ls->next);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
#endif // EXECUTABLE_SEMANTICS_INTERPRETER_CONS_LIST_H_
|
||||
@@ -0,0 +1,60 @@
|
||||
// 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_DICTIONARY_H_
|
||||
#define EXECUTABLE_SEMANTICS_INTERPRETER_DICTIONARY_H_
|
||||
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#include "executable_semantics/interpreter/list_node.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
// A persistent dictionary with a simple implementation.
|
||||
// Copying the dictionary is O(1) time.
|
||||
template <class K, class V>
|
||||
class Dictionary {
|
||||
public:
|
||||
// Create an empty dictionary.
|
||||
Dictionary() { head = nullptr; }
|
||||
|
||||
// Return the value associated with the given key.
|
||||
// Time complexity: O(n) where n is the number of times
|
||||
// any value has been set across all keys.
|
||||
auto Get(const K& key) -> std::optional<V> {
|
||||
for (auto kv : *this) {
|
||||
if (kv.first == key) {
|
||||
return kv.second;
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
// Associate the value v with key k in the dictionary.
|
||||
// Time complexity: O(1).
|
||||
auto Set(const K& k, const V& v) -> void {
|
||||
head = new ListNode<std::pair<K, V>>(std::make_pair(k, v), head);
|
||||
}
|
||||
|
||||
typedef ListNodeIterator<std::pair<K, V>> Iterator;
|
||||
|
||||
// The position of the first element of the dictionary
|
||||
// or `end()` if the dictionary is empty.
|
||||
auto begin() const -> Iterator { return Iterator(head); }
|
||||
|
||||
// The position one past that of the last element.
|
||||
auto end() const -> Iterator { return Iterator(nullptr); }
|
||||
|
||||
private:
|
||||
Dictionary(ListNode<std::pair<K, V>>* h) : head(h) {}
|
||||
|
||||
ListNode<std::pair<K, V>>* head;
|
||||
};
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
#endif // EXECUTABLE_SEMANTICS_INTERPRETER_DICTIONARY_H_
|
||||
@@ -0,0 +1,63 @@
|
||||
// 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_
|
||||
@@ -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
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
#include <vector>
|
||||
|
||||
#include "executable_semantics/ast/function_definition.h"
|
||||
#include "executable_semantics/interpreter/cons_list.h"
|
||||
#include "executable_semantics/interpreter/interpreter.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
Reference in New Issue
Block a user