an alternative to the following case












0















I have encountered a problem for which I'm unable to find an easily maintainable and readable solution to.



I'm basically writing a "master state machine"
I have a node which receives enums from 3 other slave nodes, which independently do their thing and come to a certain state and report it to the master



Slave 1 outputs one of the enums:



enum slave1 {
A,
B
}


Slave 2 outputs one of the enums:



enum slave2 {
1,
2
}


Slave 3 outputs one of the enums:



enum slave3 {
a,
b,
c
}


It is important to note that i don't have any control over the behavior, structure and outputs of the slave nodes



Now, based on the values received my master node has the following logic



val_slave_1 = getSlave1Val();
val_slave_2 = getSlave2Val();
val_slave_3 = getSlave3Val();

switch(val_slave_1):
case A:
switch(val_slave_2):
case 1:
switch(val_slave_3):
case a: {do Z}
case b: {do Y}
case c: {do X}
case 2:
switch(val_slave_3):
case a: {do W}
case b: {do V}
case c: {do U}
case B:
switch(val_slave_2):
case 1:
switch(val_slave_3):
case a: {do T}
case b: {do S}
case c: {do R}
case 2:
switch(val_slave_3):
case a: {do Q}
case b: {do P}
case c: {do O}


The advantages of this code are -




  1. Given 3 numbers I can find out exactly what behavior to expect.

  2. It's easy to debug.

  3. Do not have to maintain several booleans or if statements.


The problem with this code is that it -




  1. The current code is a combination of (2,2,3) cases permuted together, but in reality I have a lot more enums (3, 4, 7). This makes it extremely hard to read and maintain.

  2. If in the future one of the slaves changes the number of enums, say slave 2 adds another enum. I will need to add a whole lot of cases to make this work making it really hard to test

  3. If there is another independent slave (slave 4) which comes along and provides information, I'm pretty screwed.


My question to you all is that is there a better way to do this? I read a lot of places which said polymorphism is often a good way to solve switch statements but I tried making my code polymorphic and can't seem to nail down a solution. While a lot of people gave simple examples of vehicles and cats, it doesn't seem like I can apply it to my problem.



Another important note: it may be obvious but I'll still write it. It doesnt matter what order I write switch case statements in. To maintain sanity I will choose to write it in the order of most to least enums to save lines of code (I think)



The closest thread I found to this problem is -



Is there any design pattern to avoid a nested switch case?



But is there a better way to do this than maintaining a dictionary of enums to map to a function?










share|improve this question























  • What do you dislike about using a map of functions?

    – Peter Ruderman
    Nov 19 '18 at 19:57








  • 1





    Is there any commonality between the leaf-cases? Do you want your code to break if a new enum option appears, or do you want some kind of fallback? Roughly how "hot" is the code path, on a scale of 0 is "runs once when a user clicks a mouse button" (~1 Hz) to 9 is "being called per-pixel on a 60 Hz 5k display" (~1 GHz)?

    – Yakk - Adam Nevraumont
    Nov 19 '18 at 19:59













  • std::map/std::unordred_map looks like what you want. Make a struct that contains each enum as a member and then map the values to the function you want to run.

    – NathanOliver
    Nov 19 '18 at 20:08











  • @peterruderman is right. You should try using map of functions

    – klvenky
    Nov 19 '18 at 20:43











  • @PeterRuderman I guess I was looking for ways to do it with polymorphism or other concepts than a map. i don't dislike it, i was looking for cleaner alternatives but i guess my problem doesnt have a "clean solution"

    – XorThaX
    Nov 20 '18 at 16:41
















0















I have encountered a problem for which I'm unable to find an easily maintainable and readable solution to.



I'm basically writing a "master state machine"
I have a node which receives enums from 3 other slave nodes, which independently do their thing and come to a certain state and report it to the master



Slave 1 outputs one of the enums:



enum slave1 {
A,
B
}


Slave 2 outputs one of the enums:



enum slave2 {
1,
2
}


Slave 3 outputs one of the enums:



enum slave3 {
a,
b,
c
}


It is important to note that i don't have any control over the behavior, structure and outputs of the slave nodes



Now, based on the values received my master node has the following logic



val_slave_1 = getSlave1Val();
val_slave_2 = getSlave2Val();
val_slave_3 = getSlave3Val();

switch(val_slave_1):
case A:
switch(val_slave_2):
case 1:
switch(val_slave_3):
case a: {do Z}
case b: {do Y}
case c: {do X}
case 2:
switch(val_slave_3):
case a: {do W}
case b: {do V}
case c: {do U}
case B:
switch(val_slave_2):
case 1:
switch(val_slave_3):
case a: {do T}
case b: {do S}
case c: {do R}
case 2:
switch(val_slave_3):
case a: {do Q}
case b: {do P}
case c: {do O}


The advantages of this code are -




  1. Given 3 numbers I can find out exactly what behavior to expect.

  2. It's easy to debug.

  3. Do not have to maintain several booleans or if statements.


The problem with this code is that it -




  1. The current code is a combination of (2,2,3) cases permuted together, but in reality I have a lot more enums (3, 4, 7). This makes it extremely hard to read and maintain.

  2. If in the future one of the slaves changes the number of enums, say slave 2 adds another enum. I will need to add a whole lot of cases to make this work making it really hard to test

  3. If there is another independent slave (slave 4) which comes along and provides information, I'm pretty screwed.


My question to you all is that is there a better way to do this? I read a lot of places which said polymorphism is often a good way to solve switch statements but I tried making my code polymorphic and can't seem to nail down a solution. While a lot of people gave simple examples of vehicles and cats, it doesn't seem like I can apply it to my problem.



Another important note: it may be obvious but I'll still write it. It doesnt matter what order I write switch case statements in. To maintain sanity I will choose to write it in the order of most to least enums to save lines of code (I think)



The closest thread I found to this problem is -



Is there any design pattern to avoid a nested switch case?



But is there a better way to do this than maintaining a dictionary of enums to map to a function?










share|improve this question























  • What do you dislike about using a map of functions?

    – Peter Ruderman
    Nov 19 '18 at 19:57








  • 1





    Is there any commonality between the leaf-cases? Do you want your code to break if a new enum option appears, or do you want some kind of fallback? Roughly how "hot" is the code path, on a scale of 0 is "runs once when a user clicks a mouse button" (~1 Hz) to 9 is "being called per-pixel on a 60 Hz 5k display" (~1 GHz)?

    – Yakk - Adam Nevraumont
    Nov 19 '18 at 19:59













  • std::map/std::unordred_map looks like what you want. Make a struct that contains each enum as a member and then map the values to the function you want to run.

    – NathanOliver
    Nov 19 '18 at 20:08











  • @peterruderman is right. You should try using map of functions

    – klvenky
    Nov 19 '18 at 20:43











  • @PeterRuderman I guess I was looking for ways to do it with polymorphism or other concepts than a map. i don't dislike it, i was looking for cleaner alternatives but i guess my problem doesnt have a "clean solution"

    – XorThaX
    Nov 20 '18 at 16:41














0












0








0








I have encountered a problem for which I'm unable to find an easily maintainable and readable solution to.



I'm basically writing a "master state machine"
I have a node which receives enums from 3 other slave nodes, which independently do their thing and come to a certain state and report it to the master



Slave 1 outputs one of the enums:



enum slave1 {
A,
B
}


Slave 2 outputs one of the enums:



enum slave2 {
1,
2
}


Slave 3 outputs one of the enums:



enum slave3 {
a,
b,
c
}


It is important to note that i don't have any control over the behavior, structure and outputs of the slave nodes



Now, based on the values received my master node has the following logic



val_slave_1 = getSlave1Val();
val_slave_2 = getSlave2Val();
val_slave_3 = getSlave3Val();

switch(val_slave_1):
case A:
switch(val_slave_2):
case 1:
switch(val_slave_3):
case a: {do Z}
case b: {do Y}
case c: {do X}
case 2:
switch(val_slave_3):
case a: {do W}
case b: {do V}
case c: {do U}
case B:
switch(val_slave_2):
case 1:
switch(val_slave_3):
case a: {do T}
case b: {do S}
case c: {do R}
case 2:
switch(val_slave_3):
case a: {do Q}
case b: {do P}
case c: {do O}


The advantages of this code are -




  1. Given 3 numbers I can find out exactly what behavior to expect.

  2. It's easy to debug.

  3. Do not have to maintain several booleans or if statements.


The problem with this code is that it -




  1. The current code is a combination of (2,2,3) cases permuted together, but in reality I have a lot more enums (3, 4, 7). This makes it extremely hard to read and maintain.

  2. If in the future one of the slaves changes the number of enums, say slave 2 adds another enum. I will need to add a whole lot of cases to make this work making it really hard to test

  3. If there is another independent slave (slave 4) which comes along and provides information, I'm pretty screwed.


My question to you all is that is there a better way to do this? I read a lot of places which said polymorphism is often a good way to solve switch statements but I tried making my code polymorphic and can't seem to nail down a solution. While a lot of people gave simple examples of vehicles and cats, it doesn't seem like I can apply it to my problem.



Another important note: it may be obvious but I'll still write it. It doesnt matter what order I write switch case statements in. To maintain sanity I will choose to write it in the order of most to least enums to save lines of code (I think)



The closest thread I found to this problem is -



Is there any design pattern to avoid a nested switch case?



But is there a better way to do this than maintaining a dictionary of enums to map to a function?










share|improve this question














I have encountered a problem for which I'm unable to find an easily maintainable and readable solution to.



I'm basically writing a "master state machine"
I have a node which receives enums from 3 other slave nodes, which independently do their thing and come to a certain state and report it to the master



Slave 1 outputs one of the enums:



enum slave1 {
A,
B
}


Slave 2 outputs one of the enums:



enum slave2 {
1,
2
}


Slave 3 outputs one of the enums:



enum slave3 {
a,
b,
c
}


It is important to note that i don't have any control over the behavior, structure and outputs of the slave nodes



Now, based on the values received my master node has the following logic



val_slave_1 = getSlave1Val();
val_slave_2 = getSlave2Val();
val_slave_3 = getSlave3Val();

switch(val_slave_1):
case A:
switch(val_slave_2):
case 1:
switch(val_slave_3):
case a: {do Z}
case b: {do Y}
case c: {do X}
case 2:
switch(val_slave_3):
case a: {do W}
case b: {do V}
case c: {do U}
case B:
switch(val_slave_2):
case 1:
switch(val_slave_3):
case a: {do T}
case b: {do S}
case c: {do R}
case 2:
switch(val_slave_3):
case a: {do Q}
case b: {do P}
case c: {do O}


The advantages of this code are -




  1. Given 3 numbers I can find out exactly what behavior to expect.

  2. It's easy to debug.

  3. Do not have to maintain several booleans or if statements.


The problem with this code is that it -




  1. The current code is a combination of (2,2,3) cases permuted together, but in reality I have a lot more enums (3, 4, 7). This makes it extremely hard to read and maintain.

  2. If in the future one of the slaves changes the number of enums, say slave 2 adds another enum. I will need to add a whole lot of cases to make this work making it really hard to test

  3. If there is another independent slave (slave 4) which comes along and provides information, I'm pretty screwed.


My question to you all is that is there a better way to do this? I read a lot of places which said polymorphism is often a good way to solve switch statements but I tried making my code polymorphic and can't seem to nail down a solution. While a lot of people gave simple examples of vehicles and cats, it doesn't seem like I can apply it to my problem.



Another important note: it may be obvious but I'll still write it. It doesnt matter what order I write switch case statements in. To maintain sanity I will choose to write it in the order of most to least enums to save lines of code (I think)



The closest thread I found to this problem is -



Is there any design pattern to avoid a nested switch case?



But is there a better way to do this than maintaining a dictionary of enums to map to a function?







java c++ nested switch-statement polymorphism






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 19:50









XorThaXXorThaX

1




1













  • What do you dislike about using a map of functions?

    – Peter Ruderman
    Nov 19 '18 at 19:57








  • 1





    Is there any commonality between the leaf-cases? Do you want your code to break if a new enum option appears, or do you want some kind of fallback? Roughly how "hot" is the code path, on a scale of 0 is "runs once when a user clicks a mouse button" (~1 Hz) to 9 is "being called per-pixel on a 60 Hz 5k display" (~1 GHz)?

    – Yakk - Adam Nevraumont
    Nov 19 '18 at 19:59













  • std::map/std::unordred_map looks like what you want. Make a struct that contains each enum as a member and then map the values to the function you want to run.

    – NathanOliver
    Nov 19 '18 at 20:08











  • @peterruderman is right. You should try using map of functions

    – klvenky
    Nov 19 '18 at 20:43











  • @PeterRuderman I guess I was looking for ways to do it with polymorphism or other concepts than a map. i don't dislike it, i was looking for cleaner alternatives but i guess my problem doesnt have a "clean solution"

    – XorThaX
    Nov 20 '18 at 16:41



















  • What do you dislike about using a map of functions?

    – Peter Ruderman
    Nov 19 '18 at 19:57








  • 1





    Is there any commonality between the leaf-cases? Do you want your code to break if a new enum option appears, or do you want some kind of fallback? Roughly how "hot" is the code path, on a scale of 0 is "runs once when a user clicks a mouse button" (~1 Hz) to 9 is "being called per-pixel on a 60 Hz 5k display" (~1 GHz)?

    – Yakk - Adam Nevraumont
    Nov 19 '18 at 19:59













  • std::map/std::unordred_map looks like what you want. Make a struct that contains each enum as a member and then map the values to the function you want to run.

    – NathanOliver
    Nov 19 '18 at 20:08











  • @peterruderman is right. You should try using map of functions

    – klvenky
    Nov 19 '18 at 20:43











  • @PeterRuderman I guess I was looking for ways to do it with polymorphism or other concepts than a map. i don't dislike it, i was looking for cleaner alternatives but i guess my problem doesnt have a "clean solution"

    – XorThaX
    Nov 20 '18 at 16:41

















What do you dislike about using a map of functions?

– Peter Ruderman
Nov 19 '18 at 19:57







What do you dislike about using a map of functions?

– Peter Ruderman
Nov 19 '18 at 19:57






1




1





Is there any commonality between the leaf-cases? Do you want your code to break if a new enum option appears, or do you want some kind of fallback? Roughly how "hot" is the code path, on a scale of 0 is "runs once when a user clicks a mouse button" (~1 Hz) to 9 is "being called per-pixel on a 60 Hz 5k display" (~1 GHz)?

– Yakk - Adam Nevraumont
Nov 19 '18 at 19:59







Is there any commonality between the leaf-cases? Do you want your code to break if a new enum option appears, or do you want some kind of fallback? Roughly how "hot" is the code path, on a scale of 0 is "runs once when a user clicks a mouse button" (~1 Hz) to 9 is "being called per-pixel on a 60 Hz 5k display" (~1 GHz)?

– Yakk - Adam Nevraumont
Nov 19 '18 at 19:59















std::map/std::unordred_map looks like what you want. Make a struct that contains each enum as a member and then map the values to the function you want to run.

– NathanOliver
Nov 19 '18 at 20:08





std::map/std::unordred_map looks like what you want. Make a struct that contains each enum as a member and then map the values to the function you want to run.

– NathanOliver
Nov 19 '18 at 20:08













@peterruderman is right. You should try using map of functions

– klvenky
Nov 19 '18 at 20:43





@peterruderman is right. You should try using map of functions

– klvenky
Nov 19 '18 at 20:43













@PeterRuderman I guess I was looking for ways to do it with polymorphism or other concepts than a map. i don't dislike it, i was looking for cleaner alternatives but i guess my problem doesnt have a "clean solution"

– XorThaX
Nov 20 '18 at 16:41





@PeterRuderman I guess I was looking for ways to do it with polymorphism or other concepts than a map. i don't dislike it, i was looking for cleaner alternatives but i guess my problem doesnt have a "clean solution"

– XorThaX
Nov 20 '18 at 16:41












1 Answer
1






active

oldest

votes


















1














Because your enum values are small, a map from enum combinations to functions could just be an array. When you have 3, 4, and 7 values (i.e. 2, 2, and 3 bits, respectively), you could use a single byte for indexing into an array of function pointers. Something like this:



using Handler = void (*)();
std::array<Handler, 128> handlers = { doA, doB, doB, doG, nullptr, ..., doFoo };

int v1 = slave1(); // 0-6
int v2 = slave2(); // 0-3
int v3 = slave3(); // 0-2

int index = (v2 << 5) | (v1 * 3 + v3);
handlers[index]();


If your enum values are not continuous from 0 to n, you might have to remap them to that.



You might want to come up with a clever way of populating the array, since manually recalculating the indexes when something changes might take a while. One way I can think of right now is a constexpr function that takes a number of structs that each contain the enum values and a function pointer, populating an array from those. The structs would calculate the index, the function would just assign function pointers from indexes. Or something like that.






share|improve this answer


























  • This answer strikes me as brittle and error prone as written.

    – Peter Ruderman
    Nov 19 '18 at 20:30











  • If you manually populate the array like I did in the example snippet, then yes I agree. Which is why I suggested something more expressive like using a function that populates the array from descriptive entries in arbitrary order.

    – Pezo
    Nov 19 '18 at 20:37











  • aren't unordered maps O(1) look up. Is there a reason why arrays would be better than maps?

    – XorThaX
    Nov 20 '18 at 16:44











  • unordered_map is still a node based data structure, so reading out a value has an extra lookup compared to an array (in the best case). If performance is critical, an array is a better option.

    – Peter Ruderman
    Nov 20 '18 at 16:50











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381670%2fan-alternative-to-the-following-case%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Because your enum values are small, a map from enum combinations to functions could just be an array. When you have 3, 4, and 7 values (i.e. 2, 2, and 3 bits, respectively), you could use a single byte for indexing into an array of function pointers. Something like this:



using Handler = void (*)();
std::array<Handler, 128> handlers = { doA, doB, doB, doG, nullptr, ..., doFoo };

int v1 = slave1(); // 0-6
int v2 = slave2(); // 0-3
int v3 = slave3(); // 0-2

int index = (v2 << 5) | (v1 * 3 + v3);
handlers[index]();


If your enum values are not continuous from 0 to n, you might have to remap them to that.



You might want to come up with a clever way of populating the array, since manually recalculating the indexes when something changes might take a while. One way I can think of right now is a constexpr function that takes a number of structs that each contain the enum values and a function pointer, populating an array from those. The structs would calculate the index, the function would just assign function pointers from indexes. Or something like that.






share|improve this answer


























  • This answer strikes me as brittle and error prone as written.

    – Peter Ruderman
    Nov 19 '18 at 20:30











  • If you manually populate the array like I did in the example snippet, then yes I agree. Which is why I suggested something more expressive like using a function that populates the array from descriptive entries in arbitrary order.

    – Pezo
    Nov 19 '18 at 20:37











  • aren't unordered maps O(1) look up. Is there a reason why arrays would be better than maps?

    – XorThaX
    Nov 20 '18 at 16:44











  • unordered_map is still a node based data structure, so reading out a value has an extra lookup compared to an array (in the best case). If performance is critical, an array is a better option.

    – Peter Ruderman
    Nov 20 '18 at 16:50
















1














Because your enum values are small, a map from enum combinations to functions could just be an array. When you have 3, 4, and 7 values (i.e. 2, 2, and 3 bits, respectively), you could use a single byte for indexing into an array of function pointers. Something like this:



using Handler = void (*)();
std::array<Handler, 128> handlers = { doA, doB, doB, doG, nullptr, ..., doFoo };

int v1 = slave1(); // 0-6
int v2 = slave2(); // 0-3
int v3 = slave3(); // 0-2

int index = (v2 << 5) | (v1 * 3 + v3);
handlers[index]();


If your enum values are not continuous from 0 to n, you might have to remap them to that.



You might want to come up with a clever way of populating the array, since manually recalculating the indexes when something changes might take a while. One way I can think of right now is a constexpr function that takes a number of structs that each contain the enum values and a function pointer, populating an array from those. The structs would calculate the index, the function would just assign function pointers from indexes. Or something like that.






share|improve this answer


























  • This answer strikes me as brittle and error prone as written.

    – Peter Ruderman
    Nov 19 '18 at 20:30











  • If you manually populate the array like I did in the example snippet, then yes I agree. Which is why I suggested something more expressive like using a function that populates the array from descriptive entries in arbitrary order.

    – Pezo
    Nov 19 '18 at 20:37











  • aren't unordered maps O(1) look up. Is there a reason why arrays would be better than maps?

    – XorThaX
    Nov 20 '18 at 16:44











  • unordered_map is still a node based data structure, so reading out a value has an extra lookup compared to an array (in the best case). If performance is critical, an array is a better option.

    – Peter Ruderman
    Nov 20 '18 at 16:50














1












1








1







Because your enum values are small, a map from enum combinations to functions could just be an array. When you have 3, 4, and 7 values (i.e. 2, 2, and 3 bits, respectively), you could use a single byte for indexing into an array of function pointers. Something like this:



using Handler = void (*)();
std::array<Handler, 128> handlers = { doA, doB, doB, doG, nullptr, ..., doFoo };

int v1 = slave1(); // 0-6
int v2 = slave2(); // 0-3
int v3 = slave3(); // 0-2

int index = (v2 << 5) | (v1 * 3 + v3);
handlers[index]();


If your enum values are not continuous from 0 to n, you might have to remap them to that.



You might want to come up with a clever way of populating the array, since manually recalculating the indexes when something changes might take a while. One way I can think of right now is a constexpr function that takes a number of structs that each contain the enum values and a function pointer, populating an array from those. The structs would calculate the index, the function would just assign function pointers from indexes. Or something like that.






share|improve this answer















Because your enum values are small, a map from enum combinations to functions could just be an array. When you have 3, 4, and 7 values (i.e. 2, 2, and 3 bits, respectively), you could use a single byte for indexing into an array of function pointers. Something like this:



using Handler = void (*)();
std::array<Handler, 128> handlers = { doA, doB, doB, doG, nullptr, ..., doFoo };

int v1 = slave1(); // 0-6
int v2 = slave2(); // 0-3
int v3 = slave3(); // 0-2

int index = (v2 << 5) | (v1 * 3 + v3);
handlers[index]();


If your enum values are not continuous from 0 to n, you might have to remap them to that.



You might want to come up with a clever way of populating the array, since manually recalculating the indexes when something changes might take a while. One way I can think of right now is a constexpr function that takes a number of structs that each contain the enum values and a function pointer, populating an array from those. The structs would calculate the index, the function would just assign function pointers from indexes. Or something like that.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 '18 at 20:38

























answered Nov 19 '18 at 20:21









PezoPezo

969512




969512













  • This answer strikes me as brittle and error prone as written.

    – Peter Ruderman
    Nov 19 '18 at 20:30











  • If you manually populate the array like I did in the example snippet, then yes I agree. Which is why I suggested something more expressive like using a function that populates the array from descriptive entries in arbitrary order.

    – Pezo
    Nov 19 '18 at 20:37











  • aren't unordered maps O(1) look up. Is there a reason why arrays would be better than maps?

    – XorThaX
    Nov 20 '18 at 16:44











  • unordered_map is still a node based data structure, so reading out a value has an extra lookup compared to an array (in the best case). If performance is critical, an array is a better option.

    – Peter Ruderman
    Nov 20 '18 at 16:50



















  • This answer strikes me as brittle and error prone as written.

    – Peter Ruderman
    Nov 19 '18 at 20:30











  • If you manually populate the array like I did in the example snippet, then yes I agree. Which is why I suggested something more expressive like using a function that populates the array from descriptive entries in arbitrary order.

    – Pezo
    Nov 19 '18 at 20:37











  • aren't unordered maps O(1) look up. Is there a reason why arrays would be better than maps?

    – XorThaX
    Nov 20 '18 at 16:44











  • unordered_map is still a node based data structure, so reading out a value has an extra lookup compared to an array (in the best case). If performance is critical, an array is a better option.

    – Peter Ruderman
    Nov 20 '18 at 16:50

















This answer strikes me as brittle and error prone as written.

– Peter Ruderman
Nov 19 '18 at 20:30





This answer strikes me as brittle and error prone as written.

– Peter Ruderman
Nov 19 '18 at 20:30













If you manually populate the array like I did in the example snippet, then yes I agree. Which is why I suggested something more expressive like using a function that populates the array from descriptive entries in arbitrary order.

– Pezo
Nov 19 '18 at 20:37





If you manually populate the array like I did in the example snippet, then yes I agree. Which is why I suggested something more expressive like using a function that populates the array from descriptive entries in arbitrary order.

– Pezo
Nov 19 '18 at 20:37













aren't unordered maps O(1) look up. Is there a reason why arrays would be better than maps?

– XorThaX
Nov 20 '18 at 16:44





aren't unordered maps O(1) look up. Is there a reason why arrays would be better than maps?

– XorThaX
Nov 20 '18 at 16:44













unordered_map is still a node based data structure, so reading out a value has an extra lookup compared to an array (in the best case). If performance is critical, an array is a better option.

– Peter Ruderman
Nov 20 '18 at 16:50





unordered_map is still a node based data structure, so reading out a value has an extra lookup compared to an array (in the best case). If performance is critical, an array is a better option.

– Peter Ruderman
Nov 20 '18 at 16:50


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381670%2fan-alternative-to-the-following-case%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents