-
-
Notifications
You must be signed in to change notification settings - Fork 164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
XXX [mycpp] add LinkedList #1769
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
#ifndef MYCPP_GC_LINKED_LIST_H | ||
#define MYCPP_GC_LINKED_LIST_H | ||
|
||
#include "mycpp/common.h" // DCHECK | ||
#include "mycpp/gc_obj.h" // ObjHeader, maskbit | ||
|
||
template <typename T> | ||
class LinkedListNode { | ||
public: | ||
LinkedListNode() = delete; // A list node with no object makes no sense. | ||
LinkedListNode(T obj) : obj_(obj), prev_(nullptr), next_(nullptr) { | ||
} | ||
|
||
T obj() const { | ||
return obj_; | ||
} | ||
|
||
LinkedListNode* prev() const { | ||
return prev_; | ||
} | ||
LinkedListNode* next() const { | ||
return next_; | ||
} | ||
|
||
// Replace the nodes predecessor with the given node. | ||
void set_prev(LinkedListNode* node) { | ||
prev_ = node; | ||
} | ||
|
||
// Replace the nodes sucessor with the given node. | ||
void set_next(LinkedListNode* node) { | ||
next_ = node; | ||
} | ||
|
||
static constexpr ObjHeader obj_header() { | ||
return ObjHeader::ClassFixed(field_mask(), sizeof(LinkedListNode<T>)); | ||
} | ||
|
||
static constexpr uint32_t field_mask() { | ||
return maskbit(offsetof(LinkedListNode, obj_)) | | ||
maskbit(offsetof(LinkedListNode, prev_)) | | ||
maskbit(offsetof(LinkedListNode, next_)); | ||
} | ||
|
||
private: | ||
T obj_; | ||
LinkedListNode* prev_; | ||
LinkedListNode* next_; | ||
}; | ||
|
||
template <typename T> | ||
class LinkedList { | ||
public: | ||
LinkedList() : front_(nullptr), back_(nullptr) { | ||
} | ||
|
||
LinkedListNode<T>* front() const { | ||
return front_; | ||
} | ||
LinkedListNode<T>* back() const { | ||
return back_; | ||
} | ||
|
||
// Remove and return the first element in the list. | ||
LinkedListNode<T>* PopFront() { | ||
if (front_ == nullptr) { | ||
return nullptr; | ||
} | ||
|
||
LinkedListNode<T>* node = front_; | ||
LinkedListNode<T>* next = front_->next(); | ||
if (next != nullptr) { | ||
next->set_prev(nullptr); | ||
} | ||
front_ = next; | ||
if (back_ == node) { | ||
back_ = front_; | ||
} | ||
|
||
node->set_prev(nullptr); | ||
node->set_next(nullptr); | ||
return node; | ||
} | ||
|
||
// Remove and return the last element in the list. | ||
LinkedListNode<T>* PopBack() { | ||
if (back_ == nullptr) { | ||
return nullptr; | ||
} | ||
|
||
LinkedListNode<T>* node = back_; | ||
LinkedListNode<T>* prev = back_->prev(); | ||
if (prev != nullptr) { | ||
prev->set_next(nullptr); | ||
} | ||
back_ = prev; | ||
if (front_ == node) { | ||
front_ = back_; | ||
} | ||
|
||
node->set_prev(nullptr); | ||
node->set_next(nullptr); | ||
return node; | ||
} | ||
|
||
// Push the given node onto the front of the list. | ||
void PushFront(LinkedListNode<T>* node) { | ||
node->set_prev(nullptr); | ||
node->set_next(front_); | ||
if (front_ != nullptr) { | ||
front_->set_prev(node); | ||
} | ||
front_ = node; | ||
if (back_ == nullptr) { | ||
back_ = front_; | ||
} | ||
} | ||
|
||
// Push the given node onto the back of the list. | ||
void PushBack(LinkedListNode<T>* node) { | ||
node->set_prev(back_); | ||
node->set_next(nullptr); | ||
if (back_ != nullptr) { | ||
back_->set_next(node); | ||
} | ||
back_ = node; | ||
if (front_ == nullptr) { | ||
front_ = back_; | ||
} | ||
} | ||
|
||
// Remove the given node from the list. | ||
void Remove(LinkedListNode<T>* node) { | ||
// TODO: in debug mode should we DCHECK that node is actually in the list? | ||
LinkedListNode<T>* prev = node->prev(); | ||
LinkedListNode<T>* next = node->next(); | ||
|
||
if (prev != nullptr) { | ||
prev->set_next(next); | ||
} | ||
if (next != nullptr) { | ||
next->set_prev(prev); | ||
} | ||
|
||
node->set_prev(nullptr); | ||
node->set_next(nullptr); | ||
|
||
if (front_ == node) { | ||
front_ = next; | ||
} | ||
if (back_ == node) { | ||
back_ = prev; | ||
} | ||
} | ||
|
||
static constexpr ObjHeader obj_header() { | ||
return ObjHeader::ClassFixed(field_mask(), sizeof(LinkedList<T>)); | ||
} | ||
|
||
static constexpr uint32_t field_mask() { | ||
return maskbit(offsetof(LinkedList, front_)) | | ||
maskbit(offsetof(LinkedList, back_)); | ||
} | ||
|
||
private: | ||
LinkedListNode<T>* front_; | ||
LinkedListNode<T>* back_; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#include "mycpp/gc_linked_list.h" | ||
|
||
#include <vector> | ||
|
||
#include "mycpp/gc_alloc.h" // gHeap, Alloc | ||
#include "vendor/greatest.h" | ||
|
||
GREATEST_MAIN_DEFS(); | ||
|
||
// helpers | ||
template <typename T> | ||
LinkedListNode<T>* NewListNode(T obj) { | ||
return Alloc<LinkedListNode<T>>(obj); | ||
} | ||
template <typename T> | ||
void clear(LinkedList<T>* l) { | ||
while (l->front() != nullptr) { | ||
l->PopFront(); | ||
} | ||
} | ||
|
||
// On success returns the length of the expected sequence. Otherwise returns the | ||
// index of the first mismatch. | ||
template <typename T> | ||
int check_sequence(LinkedList<T>* l, const std::vector<T>& expected) { | ||
auto cur = l->front(); | ||
auto it = expected.begin(); | ||
for (; cur != nullptr && it != expected.end(); cur = cur->next(), it++) { | ||
if (cur->obj() != *it) { | ||
break; | ||
} | ||
} | ||
return it - expected.begin(); | ||
} | ||
|
||
TEST list_basics() { | ||
LinkedList<int>* l = Alloc<LinkedList<int>>(); | ||
ASSERT_EQ(l->front(), nullptr); | ||
ASSERT_EQ(l->back(), nullptr); | ||
|
||
{ | ||
l->PushFront(NewListNode(1)); | ||
l->PushFront(NewListNode(2)); | ||
l->PushFront(NewListNode(3)); | ||
|
||
ASSERT(l->front() != nullptr); | ||
ASSERT(l->front()->obj() == 3); | ||
|
||
ASSERT(l->back() != nullptr); | ||
ASSERT(l->back()->obj() == 1); | ||
|
||
l->PopFront(); | ||
|
||
ASSERT(l->front() != nullptr); | ||
ASSERT(l->front()->obj() == 2); | ||
|
||
ASSERT(l->back() != nullptr); | ||
ASSERT(l->back()->obj() == 1); | ||
} | ||
|
||
clear(l); | ||
ASSERT_EQ(l->front(), nullptr); | ||
ASSERT_EQ(l->back(), nullptr); | ||
|
||
{ | ||
l->PushBack(NewListNode(1)); | ||
l->PushBack(NewListNode(2)); | ||
l->PushBack(NewListNode(3)); | ||
|
||
ASSERT(l->front() != nullptr); | ||
ASSERT(l->front()->obj() == 1); | ||
|
||
ASSERT(l->back() != nullptr); | ||
ASSERT(l->back()->obj() == 3); | ||
|
||
l->PopBack(); | ||
|
||
ASSERT(l->front() != nullptr); | ||
ASSERT(l->front()->obj() == 1); | ||
|
||
ASSERT(l->back() != nullptr); | ||
ASSERT(l->back()->obj() == 2); | ||
} | ||
|
||
PASS(); | ||
} | ||
|
||
TEST list_rearrange() { | ||
LinkedList<int>* l = Alloc<LinkedList<int>>(); | ||
|
||
auto nodeA = Alloc<LinkedListNode<int>>(1); | ||
auto nodeB = Alloc<LinkedListNode<int>>(2); | ||
auto nodeC = Alloc<LinkedListNode<int>>(3); | ||
auto nodeD = Alloc<LinkedListNode<int>>(4); | ||
auto nodeE = Alloc<LinkedListNode<int>>(5); | ||
|
||
// start with: 1 2 3 4 5 | ||
l->PushBack(nodeA); | ||
l->PushBack(nodeB); | ||
l->PushBack(nodeC); | ||
l->PushBack(nodeD); | ||
l->PushBack(nodeE); | ||
ASSERT_EQ(check_sequence(l, {1, 2, 3, 4, 5}), 5); | ||
|
||
// Move 4 to the front. | ||
l->Remove(nodeD); | ||
ASSERT_EQ(check_sequence(l, {1, 2, 3, 5}), 4); | ||
l->PushFront(nodeD); | ||
ASSERT_EQ(check_sequence(l, {4, 1, 2, 3, 5}), 5); | ||
|
||
PASS(); | ||
} | ||
|
||
int main(int argc, char** argv) { | ||
gHeap.Init(); | ||
|
||
GREATEST_MAIN_BEGIN(); | ||
|
||
RUN_TEST(list_basics); | ||
RUN_TEST(list_rearrange); | ||
|
||
gHeap.CleanProcessExit(); | ||
|
||
GREATEST_MAIN_END(); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is tricky because whether this bit is set depends on whether T is a pointer or not
In the unit tests, it looks like it's not a pointer, which I think means bad things will happen here
For Slab and Tuple{2,3,4} I think we're using
std::is_pointer<T>
at compile time to conditionally set the mask! That took a bit of figuring out ...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch