Skip to content

Instantly share code, notes, and snippets.

@tjade273
Created October 19, 2016 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjade273/a573a982b0461f5c9adee7b542d62453 to your computer and use it in GitHub Desktop.
Save tjade273/a573a982b0461f5c9adee7b542d62453 to your computer and use it in GitHub Desktop.
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