Skip to content
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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mycpp/NINJA_subgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def DefineTargets(ru):
'mycpp/gc_mylib_test.cc',
'mycpp/gc_dict_test.cc',
'mycpp/gc_list_test.cc',
'mycpp/gc_linked_list_test.cc',
'mycpp/gc_str_test.cc',
'mycpp/gc_tuple_test.cc',
'mycpp/small_str_test.cc',
Expand Down
170 changes: 170 additions & 0 deletions mycpp/gc_linked_list.h
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_)) |
Copy link
Contributor

@andychu andychu Dec 18, 2023

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 ...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch

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
126 changes: 126 additions & 0 deletions mycpp/gc_linked_list_test.cc
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;
}
1 change: 1 addition & 0 deletions mycpp/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// Python-like compound data structures
#include "mycpp/gc_tuple.h"
#include "mycpp/gc_list.h"
#include "mycpp/gc_linked_list.h"
#include "mycpp/gc_dict.h"

#include "mycpp/gc_mylib.h" // Python-like file I/O, etc.
Expand Down