switch-statement

map vs switch performance in go

拟墨画扇 提交于 2020-03-19 02:23:09
问题 Consider this benchmark, where we compare map access vs switch var code = []int32{0, 10, 100, 100, 0, 10, 0, 10, 100, 14, 1000, 100, 1000, 0, 0, 10, 100, 1000, 10, 0, 1000, 12} var mapCode = map[int32]int32{ 0: 1, 10: 2, 100: 3, 1000: 4, } func BenchmarkMap(b *testing.B) { success := int32(0) fail := int32(0) for n := 0; n < b.N; n++ { // for each value in code array, do a specific action for _, v := range code { c, ok := mapCode[v] if !ok { fail++ } else { success += c } } } } func

Make compiler assume that all cases are handled in switch without default

天大地大妈咪最大 提交于 2020-02-27 23:08:36
问题 Let's start with some code. This is an extremely simplified version of my program. #include <stdint.h> volatile uint16_t dummyColorRecepient; void updateColor(const uint8_t iteration) { uint16_t colorData; switch(iteration) { case 0: colorData = 123; break; case 1: colorData = 234; break; case 2: colorData = 345; break; } dummyColorRecepient = colorData; } // dummy main function int main() { uint8_t iteration = 0; while (true) { updateColor(iteration); if (++iteration == 3) iteration = 0; } }

What is the preferred way to indent cases in a switch? [closed]

主宰稳场 提交于 2020-02-27 04:11:16
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . As I was writing another switch in Eclipse, I once again came across a rather weird (to me, at least) default indentation, which is

Odd compiler error when using Obj-C objects in a switch statement

自古美人都是妖i 提交于 2020-02-24 06:01:25
问题 I get a compiler error when using an Objective-C object within a switch statement: switch (myConstant) { case 0: UIViewController *myController = [[[UIViewController alloc] init] autorelease]; [self.navigationController pushViewController:myViewController animated:YES]; break; case 1: // stuff break; default: break; } The error states: Expected expression before 'UIViewController' 'myViewController' undeclared (first use in this function) I understand that the second error is a direct result

JSLint and the “Expected to see a statement and instead saw a block.” error

丶灬走出姿态 提交于 2020-02-23 04:30:42
问题 I have picked up the habit of wrapping all of my case statements in curly brackets from programming in C because of this but JSLint is throwing a fit. It stops validating at that point. My question is: Is this such a bad practice in JS? Do I not have to worry about the scope issue because JS has function scope (I understand how that would be the case, I just want a good reason not to be 'consistent' on this)? (I know that different languages call for different practices, but i am trying to be

how to avoid “cascading if\case”-statements in a factory class

[亡魂溺海] 提交于 2020-02-05 03:48:28
问题 I have to create an object based on a specific situation. I read, that the solution could be the Factory Pattern, but in my case it has a lot of disadvantages. For instance: I have an application that manages animals. At some point a client gives to me a list of animals that must be created. A solution by using the factory pattern should be: //PRODUCTS public interface Animal { String getCall(); } public class Dog implements Animal { public String getCall() { return "Bau"; } } public class

how to avoid “cascading if\case”-statements in a factory class

半世苍凉 提交于 2020-02-05 03:48:07
问题 I have to create an object based on a specific situation. I read, that the solution could be the Factory Pattern, but in my case it has a lot of disadvantages. For instance: I have an application that manages animals. At some point a client gives to me a list of animals that must be created. A solution by using the factory pattern should be: //PRODUCTS public interface Animal { String getCall(); } public class Dog implements Animal { public String getCall() { return "Bau"; } } public class

C switch statement with do-while interleaved [duplicate]

烈酒焚心 提交于 2020-02-04 01:49:47
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How does Duff's device work? I am trying to understand how this is working. Any help would be appreciated. #include<stdio.h> void duff(int count) { int n=(count+7)/8; printf("n=%d count =%d\n",n,count%8); switch(count%8){ case 0: do{ printf("case 0\n"); case 7: printf("case 7\n"); case 6: printf("case 6\n"); case 5: printf("case 5\n"); case 4: printf("case 4\n"); case 3: printf("case 3\n"); case 2: printf("case

Switch case jumps to wrong case in javascript (how to properly use the break command)

我的未来我决定 提交于 2020-01-30 09:51:28
问题 My code is not so long so I am pasting all of it here. The code is not complete but when I run it it first jumps to case "start" which it is supposed to, and then jumps to case "end". I can see it because it prints both blocks' console log texts. Why is it jumping to the "end" case? <html> <body> <script> function stepStream(stream,step){ switch (stream[step]){ case "start": console.log("Started reading stream..."); case "end": var success = "Finished reading dataStream."; console.log(success

How to substitute switch statement with better solution - clean code hints

偶尔善良 提交于 2020-01-30 06:43:06
问题 I created a code, which have to convert ContentDataType into MIME types. For example - ContentDataType is a simple String like ImageJPEG and now I use MediaType.IMAGE_JPEG_VALUE to convert it into image/jpeg . But I use switch to do this. This is a code: public static String createContentType(ContentDataType contentDataType) { String contentType; switch (contentDataType) { case IMAGE_JPG: contentType = MediaType.IMAGE_JPEG_VALUE; break; //next media types } return contentType; } What is a