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

Iterable Mapping: Insert Clearer Implementation and Internal/View Functions #92

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all 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
23 changes: 12 additions & 11 deletions library/iterable_mapping.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ library IterableMapping
}
struct IndexValue { uint keyIndex; uint value; }
struct KeyFlag { uint key; bool deleted; }
function insert(itmap storage self, uint key, uint value) returns (bool replaced)
function insert(itmap storage self, uint key, uint value) internal returns (bool replaced)
{
uint keyIndex = self.data[key].keyIndex;
self.data[key].value = value;
if (keyIndex > 0)
return true;
else
{
keyIndex = self.keys.length++;
self.data[key].keyIndex = keyIndex + 1;
self.keys[keyIndex].key = key;
keyIndex = ++self.keys.length;
self.data[key].keyIndex = keyIndex;
self.keys[keyIndex-1].key = key;
self.size++;
return false;
}
}
function remove(itmap storage self, uint key) returns (bool success)
function remove(itmap storage self, uint key) internal returns (bool success)
{
uint keyIndex = self.data[key].keyIndex;
if (keyIndex == 0)
Expand All @@ -33,33 +33,33 @@ library IterableMapping
self.keys[keyIndex - 1].deleted = true;
self.size --;
}
function contains(itmap storage self, uint key) returns (bool)
function contains(itmap storage self, uint key) internal view returns (bool)
{
return self.data[key].keyIndex > 0;
}
function iterate_start(itmap storage self) returns (uint keyIndex)
function iterate_start(itmap storage self) internal returns (uint keyIndex)
{
return iterate_next(self, uint(-1));
}
function iterate_valid(itmap storage self, uint keyIndex) returns (bool)
function iterate_valid(itmap storage self, uint keyIndex) internal view returns (bool)
{
return keyIndex < self.keys.length;
}
function iterate_next(itmap storage self, uint keyIndex) returns (uint r_keyIndex)
function iterate_next(itmap storage self, uint keyIndex) internal returns (uint r_keyIndex)
{
keyIndex++;
while (keyIndex < self.keys.length && self.keys[keyIndex].deleted)
keyIndex++;
return keyIndex;
}
function iterate_get(itmap storage self, uint keyIndex) returns (uint key, uint value)
function iterate_get(itmap storage self, uint keyIndex) internal view returns (uint key, uint value)
{
key = self.keys[keyIndex].key;
value = self.data[key].value;
}
}

// How to use it:
/* How to use it:
contract User
{
// Just a struct holding our data.
Expand All @@ -82,3 +82,4 @@ contract User
}
}
}
*/