|
| 1 | +# Lists |
| 2 | + |
| 3 | +This is an Ethereum project that implements an ordered doubly linked list (https://en.wikipedia.org/wiki/Linked_list). |
| 4 | + |
| 5 | +# Terminology |
| 6 | +Object: Objects are linked to each other and together they are a Linked List |
| 7 | +Head: The first object in the list. No other object points to the head. |
| 8 | +Tail: The last object in the list. The tail points to no other object. |
| 9 | +Data: In this contract the data is an Ethereum address, which can be used for any purpose. |
| 10 | + |
| 11 | +# Implementation |
| 12 | +Solidity doesn't support recursive type references, as in: |
| 13 | +``` |
| 14 | +struct Object { |
| 15 | + Object nextObject; |
| 16 | +} |
| 17 | +``` |
| 18 | +This renders a traditional implementation of Linked Lists impossible. In this implementation all Objects are given an id and stored in a mapping instead. This results in an extra inefficiency of doing an extra mapping lookup when following a link between objects. |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +`OrderedList.sol`: Doubly Linked List, sorted by `rank` descending from the head. |
| 23 | +* constructor: Creates an empty list. |
| 24 | +* `function get(uint256 _id)`: Retrieves the Object denoted by `_id`. |
| 25 | +* `function findRank(uint256 _rank)`: Return the id of the first Object with a lower or equal `_rank`, starting from the head. |
| 26 | +* `function insert(uint256 _rank, address _data)`: Insert a new Object immediately before the one with the closest lower `_rank`. |
| 27 | +* `function remove(uint256 _id)`: Remove the Object denoted by `_id` from the List. |
| 28 | +* `function _addHead(address _data)`: Insert a new Object as the new Head with `_data` in the data field. |
| 29 | +* `function _addTail(address _data)`: Insert a new Object as the new Tail with `_data` in the data field. |
| 30 | +* `function _insertAfter(uint256 _prevId, address _data)`: Insert a new Object after the Object denoted by `_id` with `_data` in the data field. |
| 31 | +* `function _insertBefore(uint256 _nextId, address _data)`: Insert a new Object before the Object denoted by `_id` with `_data` in the data field. |
| 32 | +* `function _setHead(uint256 _id)`: Internal function to update the Head pointer. |
| 33 | +* `function _createObject(address _data)`: Internal function to create an unlinked Object. |
| 34 | +* `function _link(uint256 _prevId, uint256 _nextId)`: Internal function to link an Object to another. |
| 35 | + |
0 commit comments