Skip to content

Commit 0541bd5

Browse files
fix: change unique_ptr to cs106l::unique_ptr
1 parent 893fabf commit 0541bd5

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

assign7/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ Now that we have a `unique_ptr` implementation, let's use it! Take a look at `ma
131131
int main()
132132
{
133133
134-
auto head = make_unique<ListNode<int>>(1);
135-
head->next = make_unique<ListNode<int>>(2);
136-
head->next->next = make_unique<ListNode<int>>(3);
134+
auto head = cs106l::make_unique<ListNode<int>>(1);
135+
head->next = cs106l::make_unique<ListNode<int>>(2);
136+
head->next->next = cs106l::make_unique<ListNode<int>>(3);
137137
138138
// memory of head:
139139
//
@@ -158,11 +158,11 @@ Notice that we didn't have to make any calls to `delete`! The RAII behaviour of
158158
> ##### `short_answer.txt`
159159
> **Q3:** This method of recursive deallocation through RAII works great for small lists, but may pose a problem for longer lists. Why? Hint: what is the limit for how "deep" a recursive function's call stack can grow?
160160
161-
**Your task is to implement the function `create_list` which converts a `std::vector<T>` into a `unique_ptr<ListNode<T>>`.** The order of elements in the vector should be preserved in the list, and `nullptr` should be returned for an empty vector. There are many ways you could go about this; one is to construct the list in reverse (starting at the tail and working towards the head). Here is an algorithm you should follow in your implementation:
161+
**Your task is to implement the function `create_list` which converts a `std::vector<T>` into a `unique_ptr<ListNode<T>>`.** The order of elements in the vector should be preserved in the list, and `nullptr` should be returned for an empty vector. There are many ways you could go about this; one is to construct the list in reverse (starting at the tail and working towards the head). **Note that you must use the `cs106l::unique_ptr` under the `cs106l` namespace, and not the `std::unique_ptr`!** Here is an algorithm you should follow in your implementation:
162162
163-
1. Initialize a `unique_ptr<ListNode<T>> head = nullptr`.
163+
1. Initialize a `cs106l::unique_ptr<ListNode<T>> head = nullptr`.
164164
2. Iterate through the `std::vector` **backwards.** For each element in the vector:
165-
- 2a. Create a new `unique_ptr<ListNode<T>> node` whose value is the element in the vector.
165+
- 2a. Create a new `cs106l::unique_ptr<ListNode<T>> node` whose value is the element in the vector.
166166
- 2b. Set `node->next` to `head`.
167167
- 2c. Set `head` to `node`
168168
3. Finally, return `head`

assign7/autograder/utils.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ void test_linked_list_example() {
7070
void test_destructor() {
7171
MemoryDiagnostics::MemoryGuard guard("Failed to deallocate memory in unique_ptr destructor!");
7272
for (size_t i = 0; i < 1000; ++i) {
73-
auto ptr = ::make_unique<std::string>("hello");
73+
auto ptr = cs106l::make_unique<std::string>("hello");
7474
}
7575

7676
// Ensure that deallocation of empty unique_ptr is successful
7777
for (size_t i = 0; i < 1000; ++i) {
78-
unique_ptr<std::string> ptr;
78+
cs106l::unique_ptr<std::string> ptr;
7979
}
8080
}
8181

@@ -85,7 +85,7 @@ void test_move_constructor() {
8585
"lecture on move semantics!");
8686
size_t sum = 0;
8787
for (size_t i = 0; i < 1000; ++i) {
88-
auto ptr = ::make_unique<std::string>(kReallyLongString);
88+
auto ptr = cs106l::make_unique<std::string>(kReallyLongString);
8989
auto size = ptr->size();
9090
auto other = std::move(ptr);
9191
ptr.~unique_ptr(); // Forcibly destroy old pointer
@@ -107,8 +107,8 @@ void test_move_assignment() {
107107
"the existing data before assigning the private fields? It may help to review the lecture on "
108108
"move semantics!");
109109
for (size_t i = 0; i < 1000; ++i) {
110-
auto ptr1 = ::make_unique<std::string>("hello");
111-
auto ptr2 = ::make_unique<std::string>("world");
110+
auto ptr1 = cs106l::make_unique<std::string>("hello");
111+
auto ptr2 = cs106l::make_unique<std::string>("world");
112112
ptr1 = std::move(ptr2);
113113
if (ptr2) {
114114
std::cerr << "Move assignment failed to set other pointer to nullptr! It may help to review "
@@ -125,7 +125,7 @@ void test_move_self_assignment() {
125125
"to review the lecture on move semantics!");
126126
size_t sum = 0;
127127
for (size_t i = 0; i < 1000; ++i) {
128-
auto ptr = ::make_unique<std::string>(kReallyLongString);
128+
auto ptr = cs106l::make_unique<std::string>(kReallyLongString);
129129
ptr = std::move(ptr);
130130

131131
// If we don't check for self-assignment in the move constructor,
@@ -160,7 +160,7 @@ const std::unordered_map<std::string, std::function<void()>> test_functions = {
160160
{"memory_leak_exit_code",
161161
[]() { std::cout << MemoryDiagnostics::MemoryGuard::get_exit_code() << "\n"; }},
162162
{"destructor", test_destructor},
163-
{"copy", test_copy<unique_ptr<std::string>>},
163+
{"copy", test_copy<cs106l::unique_ptr<std::string>>},
164164
{"move_constructor", test_move_constructor},
165165
{"move_assignment", test_move_assignment},
166166
{"move_self_assignment", test_move_self_assignment},

assign7/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ template <typename T> struct ListNode {
2424
T value;
2525

2626
/** @brief The smart pointer to the next node. May be null. */
27-
unique_ptr<ListNode<T>> next;
27+
cs106l::unique_ptr<ListNode<T>> next;
2828

2929
/**
3030
* @brief Constructs a single element linked list, setting `next` to `nullptr`.
@@ -50,7 +50,7 @@ template <typename T> struct ListNode {
5050
* @param values The values to store in the list.
5151
* @return A `unique_ptr` to the head of the list.
5252
*/
53-
template <typename T> unique_ptr<ListNode<T>> create_list(const std::vector<T>& values) {
53+
template <typename T> cs106l::unique_ptr<ListNode<T>> create_list(const std::vector<T>& values) {
5454
/* STUDENT TODO: Implement this method */
5555
throw std::runtime_error("Not implemented: createList");
5656
}
@@ -63,7 +63,7 @@ template <typename T> unique_ptr<ListNode<T>> create_list(const std::vector<T>&
6363
* @paragraph func The function to apply to each element.
6464
*/
6565
template <typename T, typename Func>
66-
void map_list(const unique_ptr<ListNode<T>>& head, const Func& func) {
66+
void map_list(const cs106l::unique_ptr<ListNode<T>>& head, const Func& func) {
6767
if (!head)
6868
return;
6969
func(head->value);

assign7/unique_ptr.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <cstddef>
44
#include <utility>
55

6+
namespace cs106l {
7+
68
/**
79
* @brief A smart pointer that owns an object and deletes it when it goes out of scope.
810
* @tparam T The type of the object to manage.
@@ -101,6 +103,9 @@ template <typename T> class unique_ptr {
101103
* @tparam Args The types of the arguments to pass to the constructor of T.
102104
* @param args The arguments to pass to the constructor of T.
103105
*/
104-
template <typename T, typename... Args> unique_ptr<T> make_unique(Args&&... args) {
106+
template <typename T, typename... Args>
107+
unique_ptr<T> make_unique(Args&&... args) {
105108
return unique_ptr<T>(new T(std::forward<Args>(args)...));
109+
}
110+
106111
}

0 commit comments

Comments
 (0)