Pure functions as a replacement of constant mappings?
I need to create a lookup table [uint -> uint] that is constant.
Constant mappings
or switch-case
are not available in solidity. Are pure
functions full of if-else
the only option?
Any better suggestion?
solidity mapping pure
add a comment |
I need to create a lookup table [uint -> uint] that is constant.
Constant mappings
or switch-case
are not available in solidity. Are pure
functions full of if-else
the only option?
Any better suggestion?
solidity mapping pure
It seems from discussion below that this question is about gas optimization.
– smarx
3 hours ago
add a comment |
I need to create a lookup table [uint -> uint] that is constant.
Constant mappings
or switch-case
are not available in solidity. Are pure
functions full of if-else
the only option?
Any better suggestion?
solidity mapping pure
I need to create a lookup table [uint -> uint] that is constant.
Constant mappings
or switch-case
are not available in solidity. Are pure
functions full of if-else
the only option?
Any better suggestion?
solidity mapping pure
solidity mapping pure
asked 6 hours ago
purpletentacle
1163
1163
It seems from discussion below that this question is about gas optimization.
– smarx
3 hours ago
add a comment |
It seems from discussion below that this question is about gas optimization.
– smarx
3 hours ago
It seems from discussion below that this question is about gas optimization.
– smarx
3 hours ago
It seems from discussion below that this question is about gas optimization.
– smarx
3 hours ago
add a comment |
2 Answers
2
active
oldest
votes
I don't see a dilemma.
You can have a mapping:
mapping(uint => uint) public myMap;
You can populate some locations in the constructor or elsewhere:
constructor() public {
myMap[1] = 101;
myMap[2] = 201;
}
That gives you a simple view
function (myMap(uint) public view returns(uint)
) that returns the number stored at an index.
You can also make view
functions full of if/else
if that's the best approach for your use-case.
Elaborate on the question with some hints about what you want to accomplish and possibly more specific guidance will be possible.
Hope it helps.
Well, clearly I want to minimize gas. What do you think would be more efficient?
– purpletentacle
3 hours ago
add a comment |
The else-if function is by far less efficient that mapping in case of sparse data if there are not special criteria for searching.
If, on the contrary, you fill all the holes in the range of interest, a simple array is the best solution.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "642"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fethereum.stackexchange.com%2fquestions%2f64925%2fpure-functions-as-a-replacement-of-constant-mappings%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I don't see a dilemma.
You can have a mapping:
mapping(uint => uint) public myMap;
You can populate some locations in the constructor or elsewhere:
constructor() public {
myMap[1] = 101;
myMap[2] = 201;
}
That gives you a simple view
function (myMap(uint) public view returns(uint)
) that returns the number stored at an index.
You can also make view
functions full of if/else
if that's the best approach for your use-case.
Elaborate on the question with some hints about what you want to accomplish and possibly more specific guidance will be possible.
Hope it helps.
Well, clearly I want to minimize gas. What do you think would be more efficient?
– purpletentacle
3 hours ago
add a comment |
I don't see a dilemma.
You can have a mapping:
mapping(uint => uint) public myMap;
You can populate some locations in the constructor or elsewhere:
constructor() public {
myMap[1] = 101;
myMap[2] = 201;
}
That gives you a simple view
function (myMap(uint) public view returns(uint)
) that returns the number stored at an index.
You can also make view
functions full of if/else
if that's the best approach for your use-case.
Elaborate on the question with some hints about what you want to accomplish and possibly more specific guidance will be possible.
Hope it helps.
Well, clearly I want to minimize gas. What do you think would be more efficient?
– purpletentacle
3 hours ago
add a comment |
I don't see a dilemma.
You can have a mapping:
mapping(uint => uint) public myMap;
You can populate some locations in the constructor or elsewhere:
constructor() public {
myMap[1] = 101;
myMap[2] = 201;
}
That gives you a simple view
function (myMap(uint) public view returns(uint)
) that returns the number stored at an index.
You can also make view
functions full of if/else
if that's the best approach for your use-case.
Elaborate on the question with some hints about what you want to accomplish and possibly more specific guidance will be possible.
Hope it helps.
I don't see a dilemma.
You can have a mapping:
mapping(uint => uint) public myMap;
You can populate some locations in the constructor or elsewhere:
constructor() public {
myMap[1] = 101;
myMap[2] = 201;
}
That gives you a simple view
function (myMap(uint) public view returns(uint)
) that returns the number stored at an index.
You can also make view
functions full of if/else
if that's the best approach for your use-case.
Elaborate on the question with some hints about what you want to accomplish and possibly more specific guidance will be possible.
Hope it helps.
answered 5 hours ago
Rob Hitchens B9lab
25.9k54278
25.9k54278
Well, clearly I want to minimize gas. What do you think would be more efficient?
– purpletentacle
3 hours ago
add a comment |
Well, clearly I want to minimize gas. What do you think would be more efficient?
– purpletentacle
3 hours ago
Well, clearly I want to minimize gas. What do you think would be more efficient?
– purpletentacle
3 hours ago
Well, clearly I want to minimize gas. What do you think would be more efficient?
– purpletentacle
3 hours ago
add a comment |
The else-if function is by far less efficient that mapping in case of sparse data if there are not special criteria for searching.
If, on the contrary, you fill all the holes in the range of interest, a simple array is the best solution.
add a comment |
The else-if function is by far less efficient that mapping in case of sparse data if there are not special criteria for searching.
If, on the contrary, you fill all the holes in the range of interest, a simple array is the best solution.
add a comment |
The else-if function is by far less efficient that mapping in case of sparse data if there are not special criteria for searching.
If, on the contrary, you fill all the holes in the range of interest, a simple array is the best solution.
The else-if function is by far less efficient that mapping in case of sparse data if there are not special criteria for searching.
If, on the contrary, you fill all the holes in the range of interest, a simple array is the best solution.
answered 2 hours ago
Rick Park
778111
778111
add a comment |
add a comment |
Thanks for contributing an answer to Ethereum Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fethereum.stackexchange.com%2fquestions%2f64925%2fpure-functions-as-a-replacement-of-constant-mappings%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It seems from discussion below that this question is about gas optimization.
– smarx
3 hours ago