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

Refactor to remove Head and Tail address #129

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
29 changes: 13 additions & 16 deletions src/DoubleLinkedList.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ library DoubleLinkedList {

struct List {
mapping(address => Account) accounts;
address head;
address tail;
}

/* ERRORS */
Expand Down Expand Up @@ -48,14 +46,14 @@ library DoubleLinkedList {
/// @param list The list to get the head.
/// @return The address of the head.
function getHead(List storage list) internal view returns (address) {
return list.head;
return list.accounts[address(0)].next;
}

/// @notice Returns the address at the tail of the `list`.
/// @param list The list to get the tail.
/// @return The address of the tail.
function getTail(List storage list) internal view returns (address) {
return list.tail;
return list.accounts[address(0)].prev;
}

/// @notice Returns the next id address from the current `id`.
Expand All @@ -79,12 +77,11 @@ library DoubleLinkedList {
/// @param id The address of the account.
function remove(List storage list, address id) internal {
Copy link
Contributor

@makcandrov makcandrov May 9, 2023

Choose a reason for hiding this comment

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

this function can be optimized:

function remove(List storage list, address id) internal {
    Account storage account = list.accounts[id];
    if (id == address(0)) revert AddressIsZero();
    if (account.value == 0) revert AccountDoesNotExist();

    address prevId = account.prev;
    address nextId = account.next;
    list.accounts[prevId].next = nextId;
    list.accounts[nextId].prev = prevId;

    account.prev = address(0);
    account.next = address(0);
    account.value = 0;
}
gas diff

The line if (id == address(0)) revert AddressIsZero(); can be removed, but the error thrown would no longer be accurate in the case of a null address.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will implement but not sure we will merge the PR as mentioned above

Account memory account = list.accounts[id];
if (id == address(0)) revert AddressIsZero();
Tristan22400 marked this conversation as resolved.
Show resolved Hide resolved
if (account.value == 0) revert AccountDoesNotExist();

if (account.prev != address(0)) list.accounts[account.prev].next = account.next;
else list.head = account.next;
if (account.next != address(0)) list.accounts[account.next].prev = account.prev;
else list.tail = account.prev;
list.accounts[account.prev].next = account.next;
list.accounts[account.next].prev = account.prev;

delete list.accounts[id];
}
Expand All @@ -100,7 +97,7 @@ library DoubleLinkedList {
if (list.accounts[id].value != 0) revert AccountAlreadyInserted();

uint256 numberOfIterations;
address next = list.head; // If not added at the end of the list `id` will be inserted before `next`.
address next = list.accounts[address(0)].next; // If not added at the end of the list `id` will be inserted before `next`.

while (numberOfIterations < maxIterations && next != address(0) && list.accounts[next].value >= value) {
next = list.accounts[next].next;
Expand All @@ -112,9 +109,9 @@ library DoubleLinkedList {
// Account is not the new tail.
if (numberOfIterations < maxIterations && next != address(0)) {
// Account is the new head.
if (next == list.head) {
if (next == list.accounts[address(0)].next) {
list.accounts[id] = Account({prev: address(0), next: next, value: value});
list.head = id;
list.accounts[address(0)].next = id;
list.accounts[next].prev = id;
}
// Account is not the new head.
Expand All @@ -128,17 +125,17 @@ library DoubleLinkedList {
// Account is the new tail.
else {
// Account is the new head.
if (list.head == address(0)) {
if (list.accounts[address(0)].next == address(0)) {
list.accounts[id] = Account({prev: address(0), next: address(0), value: value});
list.head = id;
list.tail = id;
list.accounts[address(0)].next = id;
list.accounts[address(0)].prev = id;
}
// Account is not the new head.
else {
address tail = list.tail;
address tail = list.accounts[address(0)].prev;
list.accounts[id] = Account({prev: tail, next: address(0), value: value});
list.accounts[tail].next = id;
list.tail = id;
list.accounts[address(0)].prev = id;
}
}
}
Expand Down
Loading