#Implementing switch case

1 messages ยท Page 1 of 1 (latest)

lethal swift
#

Sorry for the beginner question, Iโ€™m pretty new here valcurious. The compiler is giving me 4 different syntax errors that all seem nonsensical, how would i correctly do this without several nested if else statements?

enum ImageFormats : u32 {
  I4 = 0,
  I8 = 1,
  IA4 = 2,
  IA8 = 3,
  RGB565 = 4,
  RGB5A3 = 5,
  RGBA8 = 6,
  C4 = 8,
  C8 = 9,
  C14X2 = 10,
  CMPR = 14,
};

fn calculateImageSizeBytes(ImageFormats format, u16 width, u16 height) {
    u16 blockWidth;
    u16 blockHeight;
    u32 bytesPerBlock;

    switch (format) {
        case I4:
        case C4:
        case CMPR:
            blockWidth = 8;
            blockHeight = 8;
            bytesPerBlock = 32;
            break;

        case I8:
        case IA4:
        case C8:
            blockWidth = 8;
            blockHeight = 4;
            bytesPerBlock = 32;
            break;

        case IA8:
        case RGB565:
        case RGB5A3:
        case C14X2:
            blockWidth = 4;
            blockHeight = 4;
            bytesPerBlock = 32;
            break;

        case RGBA8:
            blockWidth = 4;
            blockHeight = 4;
            bytesPerBlock = 64;
            break;
    
    }

   u32 total_blocks_width = width / blockWidth;
      if (width % blockWidth > 0) {
         total_blocks_width += 1;
      }
         
   u32 total_blocks_height = height / blockHeight;
      if (height % blockHeight > 0) {
          total_blocks_height += 1;
      }    
   u32 totalBytes = total_blocks_width * total_blocks_height * bytesPerBlock;

    return totalBytes;
    
}
verbal whale
#

Pattern language doesnt have a switch statement and implements the superior rustlike match statement instead

lethal swift
#

is there a better way to do this?

#

I must be doing something wrong. it states that blockWidth is 0 and thus resulting in a divide by zero error. Trying to debug with pragma debug causes the evaluation to hang and a memory leak opens

verbal whale
#

pragma debug is not for debugging. what shoulf blockWidth be?

#

to debug place a breakpoint and run as normally

#

you are not using blockWidth to divide in the code shown.

#

A way I would clean that and make it faster is b y defining constants using const

lethal swift
#

it seems like blockwidth was assigned a value 0

verbal whale
#

try passing it as a reference using ref

#

like this ref auto width

lethal swift
#

results in either
error: Expected value, got Keyword (ref).
or

E: error: Invalid function statement.
E:   -->   in <Source Code>:45:32
E: 45 |  (ImageFormats::RGBA8): ref auto blockWidth =
verbal whale
#

no, i meant in the function parameter definition

lethal swift
#

blockWidth isnt passed to the function though?

verbal whale
#

i thought the problem was with width

lethal swift
#

so an error is thrown because it tries to divide 32 by 0. a value wasnt assigned to blockWidth by the match statement

verbal whale
#

you dont jave a default clause. Are you sure it is being assigned a value?

#

you dont need one case per variable

lethal swift
#

format is correctly assigned a value,
in this case, CMPR

verbal whale
#

the thing is the way you hhave it it may not execute the assignent

lethal swift
verbal whale
#

the syntax for match is

match (variavle) {
  (case 1) : {
      statements here
  } 
...
}
#

issimilar to how switch works

lethal swift
#

ah, the documentation's examples didn't use case

verbal whale
#

maybe it is not using {} if only one statement is needed but the case must always exist

lethal swift
#

now it states my enums arent declared anymore glace_wasted

    match (format) {
        (case ImageFormats::I4/ImageFormats::C4/ImageFormats::CMPR): {
        blockWidth = 8;
        blockHeight = 8;
        bytesPerBlock = 32;
        };

        (case ImageFormats::I8/ImageFormats::IA4/ImageFormats::C8):  {
        blockWidth = 8;
        blockHeight = 4;
        bytesPerBlock = 32;
        };

        (case ImageFormats::IA8/ImageFormats::RGB565/ImageFormats::RGB5A3/ImageFormats::C14X2): { 
        blockWidth = 4;
        blockHeight = 4;
        bytesPerBlock = 32;
        };

        (case ImageFormats::RGBA8): {
        blockWidth = 4;
        blockHeight = 4;
        bytesPerBlock = 64;
        }
    }
verbal whale
#

no, no case sorry

#

that was just a description of what goes inside the parenthesis

#

you had the case syntax correctly, just too maky of them

#

many

lethal swift
#

back to dividing by zero, i have no clue why blockWidth isnt being assigned a value

#

does it have something to do with scope?

#

none of them are being assigned, blockWidth is just the first one it tries

verbal whale
#

use std::print("{}",blockWidth); after being assigned and before it is used.

lethal swift
#

so it looks like the code path isn't being taken. the print statement isn't met and neither is my breakpoint

verbal whale
#

what value does format have?

lethal swift
#

in this case, 14

#

which is a valid entry in the enum

enum ImageFormats : u32 {
  I4 = 0,
  I8 = 1,
  IA4 = 2,
  IA8 = 3,
  RGB565 = 4,
  RGB5A3 = 5,
  RGBA8 = 6,
  C4 = 8,
  C8 = 9,
  C14X2 = 10,
  CMPR = 14,
};
verbal whale
#

but you are checking for a value like

ImageFormats::I8/ImageFormats::IA4/ImageFormats::C8
#

which is dividing the constants

lethal swift
#

okay it works if i place the enum entry in its own satement:

        (ImageFormats::CMPR): {
        blockWidth = 8;
        blockHeight = 8;
        bytesPerBlock = 32;
        }
#

something must be wrong with putting multiple values together with /

verbal whale
#

ypu problably mean to use | for or and not / for divide

lethal swift
#

oh.. this is a pipe...

#

its italic so it looks like a forwardslash

verbal whale
#

ah, I remember seeing that, apparently it is a font issue and there is nothing we can do

lethal swift
#

thanks for your help, i'm sorry for how long this took woweeThump

verbal whale
#

maybe remove that text and add a picture would help