Select Case expressions

New to FreeBASIC? Post your questions here.
Post Reply
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Select Case expressions

Post by fzabkar »

How can I improve the coding of Case Else? I can't seem to find a suitable example in the FB docs, assuming there is one.

Code: Select all

Dim bfilptr As UByte Ptr

Select Case bfilptr[i]

Case 48 To 64   :   bfilptr[i] -= 6
Case 35         :   bfilptr[i] = 59
Case 65         :   bfilptr[i] = 32
Case 66         :   bfilptr[i] = 63
Case Is > 98    :   bfilptr[i] -= 5
Case Else

    If ( bfilptr[i] <> 10 ) AndAlso ( bfilptr[i] <> 32 ) AndAlso ( bfilptr[i] <> 46 ) Then
        
        bfilptr[i] -= 9
        
    End If

End Select
PaulSquires
Posts: 1002
Joined: Jul 14, 2005 23:41

Re: Select Case expressions

Post by PaulSquires »

How about this (basically, do nothing if the values are 10, 32, 46)

Code: Select all

Select Case bfilptr[i]

Case 48 To 64   :   bfilptr[i] -= 6
Case 35         :   bfilptr[i] = 59
Case 65         :   bfilptr[i] = 32
Case 66         :   bfilptr[i] = 63
Case Is > 98    :   bfilptr[i] -= 5
Case 10, 32, 46
Case Else
        bfilptr[i] -= 9
End Select
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: Select Case expressions

Post by fzabkar »

Many thanks. In fact the same answer occurred to me a couple of minutes ago.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Select Case expressions

Post by D.J.Peters »

"case as const" will speed it up !
(internal a fast jump table are builded)

Joshy

Code: Select all

if bfilptr[i]>98 then
  bfilptr[i] -= 5
else  
  select case as const bfilptr[i]
  case 10
  case 32
  case 35         :   bfilptr[i] = 59
  case 46 
  case 48 To 64   :   bfilptr[i] -= 6
  case 65         :   bfilptr[i] = 32
  case 66         :   bfilptr[i] = 63
  case else
    bfilptr[i] -= 9
  end select
end if  
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: Select Case expressions

Post by fzabkar »

What about this?

Code: Select all

  Select Case As Const bfilptr[i]
  Case 10
  Case 32
  Case 35         :   bfilptr[i] = 59
  Case 46 
  Case 48 To 64   :   bfilptr[i] -= 6
  Case 65         :   bfilptr[i] = 32
  Case 66         :   bfilptr[i] = 63
  Case 99 To 255  :   bfilptr[i] -= 5
  Case Else       :   bfilptr[i] -= 9
  End Select
Is there any reason why I can't put 10, 32, 46 on the same line?
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Select Case expressions

Post by MrSwiss »

fzabkar wrote:What about this?
More than one 'case' on the same line:

Code: Select all

Select Case As Const bfilptr[i]
  Case 10, 32, 46	' do nothing
  Case 35         :   bfilptr[i] = 59
  Case 48 To 64   :   bfilptr[i] -= 6
  Case 65         :   bfilptr[i] = 32
  Case 66         :   bfilptr[i] = 63
  Case 99 To 255  :   bfilptr[i] -= 5
  Case Else       :   bfilptr[i] -= 9
End Select
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: Select Case expressions

Post by fzabkar »

MrSwiss wrote:
fzabkar wrote:What about this?
More than one 'case' on the same line:
I was wondering whether separate lines, or the numerical order of the constants, affected the optimisation of the jump table.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Select Case expressions

Post by MrSwiss »

fzabkar wrote:I was wondering whether separate lines, or the numerical order of the constants, affected the optimisation of the jump table.
No, it doesn't. Since you'll not know which constant is called the most.
The jump table itself optimizes the Select Case [As Const] statement.

BTW: max. size of jumptable is: 8K entries (current FBC)
Post Reply