Skip to content

Commit

Permalink
refactor: simplify loop in insertSorted
Browse files Browse the repository at this point in the history
  • Loading branch information
QGarchery committed Jul 12, 2024
1 parent 2a403e1 commit 5cfa1a6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
15 changes: 6 additions & 9 deletions certora/applyHarnessFifo.patch
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
diff -ruN DoubleLinkedList.sol DoubleLinkedList.sol
--- DoubleLinkedList.sol 2024-07-11 13:38:21.976522997 +0200
+++ DoubleLinkedList.sol 2024-07-11 13:40:22.469518264 +0200
--- DoubleLinkedList.sol 2024-07-12 09:49:49.899261374 +0200
+++ DoubleLinkedList.sol 2024-07-12 09:53:11.964303440 +0200
@@ -16,6 +16,8 @@

struct List {
Expand All @@ -10,15 +10,12 @@ diff -ruN DoubleLinkedList.sol DoubleLinkedList.sol
}

/* ERRORS */
@@ -100,6 +102,7 @@
address next = list.accounts[address(0)].next; // `id` will be inserted before `next`.

while (numberOfIterations < maxIterations && next != address(0) && list.accounts[next].value >= value) {
@@ -101,10 +103,12 @@
uint256 numberOfIterations;
for (; numberOfIterations < maxIterations; numberOfIterations++) {
if (next == address(0) || list.accounts[next].value < value) break;
+ list.insertedAfter = next; // HARNESS
next = list.accounts[next].next;
unchecked {
++numberOfIterations;
@@ -107,6 +110,7 @@
}

if (numberOfIterations == maxIterations) next = address(0);
Expand Down
16 changes: 7 additions & 9 deletions certora/applyHarnessSimple.patch
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
diff -ruN DoubleLinkedList.sol DoubleLinkedList.sol
--- DoubleLinkedList.sol 2024-07-11 13:38:21.976522997 +0200
+++ DoubleLinkedList.sol 2024-07-11 13:40:08.765859975 +0200
--- DoubleLinkedList.sol 2024-07-12 09:49:49.899261374 +0200
+++ DoubleLinkedList.sol 2024-07-12 09:52:16.333105454 +0200
@@ -16,6 +16,8 @@

struct List {
Expand All @@ -10,7 +10,7 @@ diff -ruN DoubleLinkedList.sol DoubleLinkedList.sol
}

/* ERRORS */
@@ -90,23 +92,19 @@
@@ -90,21 +92,20 @@
/// @param list The list to search in.
/// @param id The address of the account.
/// @param value The value of the account.
Expand All @@ -21,16 +21,14 @@ diff -ruN DoubleLinkedList.sol DoubleLinkedList.sol
if (id == address(0)) revert AddressIsZero();
if (list.accounts[id].value != 0) revert AccountAlreadyInserted();

- uint256 numberOfIterations;
address next = list.accounts[address(0)].next; // `id` will be inserted before `next`.

- while (numberOfIterations < maxIterations && next != address(0) && list.accounts[next].value >= value) {
+ while (next != address(0) && list.accounts[next].value >= value) {
- uint256 numberOfIterations;
- for (; numberOfIterations < maxIterations; numberOfIterations++) {
+ for (;;) {
if (next == address(0) || list.accounts[next].value < value) break;
+ list.insertedAfter = next; // HARNESS
next = list.accounts[next].next;
- unchecked {
- ++numberOfIterations;
- }
}

- if (numberOfIterations == maxIterations) next = address(0);
Expand Down
8 changes: 3 additions & 5 deletions src/DoubleLinkedList.sol
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,12 @@ library DoubleLinkedList {
if (id == address(0)) revert AddressIsZero();
if (list.accounts[id].value != 0) revert AccountAlreadyInserted();

uint256 numberOfIterations;
address next = list.accounts[address(0)].next; // `id` will be inserted before `next`.

while (numberOfIterations < maxIterations && next != address(0) && list.accounts[next].value >= value) {
uint256 numberOfIterations;
for (; numberOfIterations < maxIterations; numberOfIterations++) {
if (next == address(0) || list.accounts[next].value < value) break;
next = list.accounts[next].next;
unchecked {
++numberOfIterations;
}
}

if (numberOfIterations == maxIterations) next = address(0);
Expand Down

0 comments on commit 5cfa1a6

Please sign in to comment.