Created
October 19, 2016 22:08
-
-
Save tjade273/a573a982b0461f5c9adee7b542d62453 to your computer and use it in GitHub Desktop.
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
library LinkedList { | |
struct data { | |
uint80 head; | |
uint80 last; | |
uint80 count; | |
Item[] items; | |
} | |
uint80 constant None = uint80(0); | |
struct Item { | |
uint80 prev; | |
uint80 next; | |
string data; | |
} | |
/// Appends `_data` to the end of the list self. Pushes the _data. | |
function append(data storage self, string memory _data) { | |
var index = uint80(self.items.push(Item({prev: self.last, next: None, data: _data,}))); | |
if (self.last == None) | |
{ | |
if (self.head != None || self.count != 0) throw; | |
self.head = self.last = index; | |
self.count = 1; | |
} | |
else | |
{ | |
self.items[self.last - 1].next = index; | |
self.last = index; | |
self.count++; | |
} | |
} | |
function get_head_data(data storage self) returns (uint addr) { | |
var it = iterate_start(self); | |
return (iterate_get(self, it)); | |
} | |
function get_head_iterate(data storage self) returns (uint80) { | |
var it = iterate_start(self); | |
return it; | |
} | |
// Interface of Iterator | |
function iterate_start(data storage self) returns (uint80) { return self.head; } | |
function iterate_next(data storage self, uint80 _index) returns (uint80) { return self.items[_index - 1].next; } | |
function iterate_get(data storage self, uint80 _index) returns (uint storageAddr){ | |
string s = self.items[_index - 1].data; | |
uint addr; | |
assembly { | |
addr := s | |
} | |
return addr; | |
} | |
} | |
contract Link { | |
using LinkedList for LinkedList.data; | |
LinkedList.data public list; | |
function Test() { | |
list.append("abcdef"); | |
list.append("foobar"); | |
list.append("qwertyuiop"); | |
} | |
function get() returns (string) { | |
uint addr = list.get_head_data(); | |
string s; | |
assembly{ | |
s := addr | |
} | |
return (s); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment