Created
October 2, 2016 22:20
-
-
Save noeleon930/afcb8a1661fb39c655ca61cba123adc8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| pragma solidity ^0.4.2; | |
| library Set { | |
| struct Data { mapping(uint => bool) flags; } | |
| function insert(Data storage self, uint value) returns (bool) { | |
| if (self.flags[value]) | |
| return false; // value already there | |
| self.flags[value] = true; | |
| return true; | |
| } | |
| function remove(Data storage self, uint value) returns (bool) { | |
| if (!self.flags[value]) | |
| return false; // not there | |
| self.flags[value] = false; | |
| return true; | |
| } | |
| function contains(Data storage self, uint value) returns (bool) { | |
| return self.flags[value]; | |
| } | |
| } | |
| contract C { | |
| using Set for Set.Data; // 這裡有了一些變化 | |
| Set.Data knownValues; // 會使 kownValues 預設使用自己作為 self (像是 python 中的做法) | |
| function register(uint value) { | |
| // 在這裡,只要是 Set.Data 型態的變數都會擁有相對應可被自動填入的方法們 | |
| // 像是 insert, remove, contains | |
| // 下列等同於 Set.insert(knownValues, value) | |
| if (!knownValues.insert(value)) | |
| throw; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment