Cutting off some dimension tables from your code

Blitz3D Forums/Blitz3D Tutorials/Cutting off some dimension tables from your code

_33(Posted 2008) [#1]
I've noticed that a lot of times, people just create dimension tables for everything and nothing. I'll just drop in a neat trick to save up the task of creating tables for nothing. basically what happens often enough is that you look at the stuff you have to do, which could be just giving some choices to the user, or having descriptives for a numeric type, or enumeration of things that are textually stored. In all those and some other cases, the usual task would be to create a dimension table, then fill them up with constant strings. Now your program, on execution, will have both the task of assigning strings one by one, and hold a table that contains all of the same data as a global dimension table. Thus, you just duplicated the content for the sake of keeping a table of constants.

Usually, this looks like the following:
Dim ControllerType$(127)

ControllerType$(0)  = "Bank Select (coarse)"
ControllerType$(1)   = "Modulation Wheel (coarse)"
ControllerType$(2)   = "Breath controller (coarse)"
ControllerType$(4)   = "Foot Pedal (coarse)"
ControllerType$(5)   = "Portamento Time (coarse)"
ControllerType$(6)   = "Data Entry (coarse)"
ControllerType$(7)   = "Volume (coarse)"
ControllerType$(8)   = "Balance (coarse)"
ControllerType$(10)  = "Pan position (coarse)"
ControllerType$(11)  = "Expression (coarse)"
ControllerType$(12)  = "Effect Control 1 (coarse)"
ControllerType$(13)  = "Effect Control 2 (coarse)"
ControllerType$(16)  = "General Purpose Slider 1"
ControllerType$(17)  = "General Purpose Slider 2"
ControllerType$(18)  = "General Purpose Slider 3"
ControllerType$(19)  = "General Purpose Slider 4"
ControllerType$(32)  = "Bank Select (fine)"
ControllerType$(33)  = "Modulation Wheel (fine)"
ControllerType$(34)  = "Breath controller (fine)"
ControllerType$(36)  = "Foot Pedal (fine)"
ControllerType$(37)  = "Portamento Time (fine)"
ControllerType$(38)  = "Data Entry (fine)"
ControllerType$(39)  = "Volume (fine)"
ControllerType$(40)  = "Balance (fine)"
ControllerType$(42)  = "Pan position (fine)"
ControllerType$(43  )  = "Expression (fine)"
ControllerType$(44  )  = "Effect Control 1 (fine)"
ControllerType$(45  )  = "Effect Control 2 (fine)"
ControllerType$(64  )  = "Hold Pedal (on/off)"
ControllerType$(65  )  = "Portamento (on/off)"
ControllerType$(66 )  = "Sustenuto Pedal (on/off)"
ControllerType$(67 )  = "Soft Pedal (on/off)"
ControllerType$(68 )  = "Legato Pedal (on/off)"
ControllerType$(69 )  = "Hold 2 Pedal (on/off)"
ControllerType$(70 )  = "Sound Variation"
ControllerType$(71 )  = "Sound Timbre"
ControllerType$(72 )  = "Sound Release Time"
ControllerType$(73 )  = "Sound Attack Time"
ControllerType$(74 )  = "Sound Brightness"
ControllerType$(75 )  = "Sound Control 6"
ControllerType$(76 )  = "Sound Control 7"
ControllerType$(77 )  = "Sound Control 8"
ControllerType$(78 )  = "Sound Control 9"
ControllerType$(79 )  = "Sound Control 10"
ControllerType$(80 )  = "General Purpose Button 1 (on/off)"
ControllerType$(81 )  = "General Purpose Button 2 (on/off)"
ControllerType$(82 )  = "General Purpose Button 3 (on/off)"
ControllerType$(83 )  = "General Purpose Button 4 (on/off)"
ControllerType$(91 )  = "Effects Level"
ControllerType$(92 )  = "Tremulo Level"
ControllerType$(93 )  = "Chorus Level"
ControllerType$(94 )  = "Celeste Level"
ControllerType$(95 )  = "Phaser Level"
ControllerType$(96 )  = "Data Button increment"
ControllerType$(97 )  = "Data Button decrement"
ControllerType$(98 )  = "Non-registered Parameter (fine)"
ControllerType$(99 )  = "Non-registered Parameter (coarse)"
ControllerType$(100)  = "Registered Parameter (fine)"
ControllerType$(101)  = "Registered Parameter (coarse)"
ControllerType$(120)  = "All Sound Off"
ControllerType$(121)  = "All Controllers Off"
ControllerType$(122)  = "Local Keyboard (on/off)"
ControllerType$(123)  = "All Notes Off"
ControllerType$(124)  = "Omni Mode Off"
ControllerType$(125)  = "Omni Mode On"
ControllerType$(126)  = "Mono Operation"
ControllerType$(127)  = "Poly Operation"


Yet there is a simpler way, that won't need any other change to your program, won't need to define a table, and won't duplilcate the data within your program execution. What you'd do basically is create a Function to replace the dimension table, using the dimension name, but instead of filling up a table, you'll just create a Case statement.

So here's the same example, but using this new technique:
Function ControllerType$(value%)
   Select value
   Case 0 : Return "Bank Select (coarse)"
   Case 1 : Return "Modulation Wheel (coarse)"
   Case 2 : Return "Breath controller (coarse)"
   Case 4 : Return "Foot Pedal (coarse)"
   Case 5 : Return "Portamento Time (coarse)"
   Case 6 : Return "Data Entry (coarse)"
   Case 7 : Return "Volume (coarse)"
   Case 8 : Return "Balance (coarse)"
   Case 10 : Return "Pan position (coarse)"
   Case 11 : Return "Expression (coarse)"
   Case 12 : Return "Effect Control 1 (coarse)"
   Case 13 : Return "Effect Control 2 (coarse)"
   Case 16 : Return "General Purpose Slider 1"
   Case 17 : Return "General Purpose Slider 2"
   Case 18 : Return "General Purpose Slider 3"
   Case 19 : Return "General Purpose Slider 4"
   Case 32 : Return "Bank Select (fine)"
   Case 33 : Return "Modulation Wheel (fine)"
   Case 34 : Return "Breath controller (fine)"
   Case 36 : Return "Foot Pedal (fine)"
   Case 37 : Return "Portamento Time (fine)"
   Case 38 : Return "Data Entry (fine)"
   Case 39 : Return "Volume (fine)"
   Case 40 : Return "Balance (fine)"
   Case 42 : Return "Pan position (fine)"
   Case 43 : Return "Expression (fine)"
   Case 44 : Return "Effect Control 1 (fine)"
   Case 45 : Return "Effect Control 2 (fine)"
   Case 64 : Return "Hold Pedal (on/off)"
   Case 65 : Return "Portamento (on/off)"
   Case 66 : Return "Sustenuto Pedal (on/off)"
   Case 67 : Return "Soft Pedal (on/off)"
   Case 68 : Return "Legato Pedal (on/off)"
   Case 69 : Return "Hold 2 Pedal (on/off)"
   Case 70 : Return "Sound Variation"
   Case 71 : Return "Sound Timbre"
   Case 72 : Return "Sound Release Time"
   Case 73 : Return "Sound Attack Time"
   Case 74 : Return "Sound Brightness"
   Case 75 : Return "Sound Control 6"
   Case 76 : Return "Sound Control 7"
   Case 77 : Return "Sound Control 8"
   Case 78 : Return "Sound Control 9"
   Case 79 : Return "Sound Control 10"
   Case 80 : Return "General Purpose Button 1 (on/off)"
   Case 81 : Return "General Purpose Button 2 (on/off)"
   Case 82 : Return "General Purpose Button 3 (on/off)"
   Case 83 : Return "General Purpose Button 4 (on/off)"
   Case 91 : Return "Effects Level"
   Case 92 : Return "Tremulo Level"
   Case 93 : Return "Chorus Level"
   Case 94 : Return "Celeste Level"
   Case 95 : Return "Phaser Level"
   Case 96 : Return "Data Button increment"
   Case 97 : Return "Data Button decrement"
   Case 98 : Return "Non-registered Parameter (fine)"
   Case 99 : Return "Non-registered Parameter (coarse)"
   Case 100: Return "Registered Parameter (fine)"
   Case 101: Return "Registered Parameter (coarse)"
   Case 120: Return "All Sound Off"
   Case 121: Return "All Controllers Off"
   Case 122: Return "Local Keyboard (on/off)"
   Case 123: Return "All Notes Off"
   Case 124: Return "Omni Mode Off"
   Case 125: Return "Omni Mode On"
   Case 126: Return "Mono Operation"
   Case 127: Return "Poly Operation"
   Default : Return "unknown"
   End Select
End Function


That's it! Hopefully, this will help people getting a step closer to efficient code. (no pun intended)


H&K(Posted 2008) [#2]
Shoot me down if Im wrong, but wouldnt that mean that to return "Poly Operation" (for example) your code has had to do 127 comparision operations?


_33(Posted 2008) [#3]
H&K, yes, but, why would you need to have it THAT FAST when it is a text string that you'll only need to retrieve once a while for a user interface (stuff like that) ? In such a case, I think that using a table is absolutely not justified. You really have to look at your requirements in order to use a table or not to. And I don't think Blitz's Select / Case is THAT SLOW that you prefer to use a table instead for fetching constant strings.


_PJ_(Posted 2015) [#4]
You can speed this up a great deal by minimising the number of checks required.

Function ControllerType$(value%)
If (Value<32)

   Select value
   Case 0 : Return "Bank Select (coarse)"
   Case 1 : Return "Modulation Wheel (coarse)"
   Case 2 : Return "Breath controller (coarse)"
   Case 4 : Return "Foot Pedal (coarse)"
   Case 5 : Return "Portamento Time (coarse)"
   Case 6 : Return "Data Entry (coarse)"
   Case 7 : Return "Volume (coarse)"
   Case 8 : Return "Balance (coarse)"
   Case 10 : Return "Pan position (coarse)"
   Case 11 : Return "Expression (coarse)"
   Case 12 : Return "Effect Control 1 (coarse)"
   Case 13 : Return "Effect Control 2 (coarse)"
   Case 16 : Return "General Purpose Slider 1"
   Case 17 : Return "General Purpose Slider 2"
   Case 18 : Return "General Purpose Slider 3"
   Case 19 : Return "General Purpose Slider 4"
   Default : Return "unknown"
   End Select
End If
If (Value<64)
   Select value
   Case 32 : Return "Bank Select (fine)"
   Case 33 : Return "Modulation Wheel (fine)"
   Case 34 : Return "Breath controller (fine)"
   Case 36 : Return "Foot Pedal (fine)"
   Case 37 : Return "Portamento Time (fine)"
   Case 38 : Return "Data Entry (fine)"
   Case 39 : Return "Volume (fine)"
   Case 40 : Return "Balance (fine)"
   Case 42 : Return "Pan position (fine)"
   Case 43 : Return "Expression (fine)"
   Case 44 : Return "Effect Control 1 (fine)"
   Case 45 : Return "Effect Control 2 (fine)"
   Default : Return "unknown"
   End Select
End If 

If (value<91)
   Select value
   Case 64 : Return "Hold Pedal (on/off)"
   Case 65 : Return "Portamento (on/off)"
   Case 66 : Return "Sustenuto Pedal (on/off)"
   Case 67 : Return "Soft Pedal (on/off)"
   Case 68 : Return "Legato Pedal (on/off)"
   Case 69 : Return "Hold 2 Pedal (on/off)"
   Case 70 : Return "Sound Variation"
   Case 71 : Return "Sound Timbre"
   Case 72 : Return "Sound Release Time"
   Case 73 : Return "Sound Attack Time"
   Case 74 : Return "Sound Brightness"
   Case 75 : Return "Sound Control 6"
   Case 76 : Return "Sound Control 7"
   Case 77 : Return "Sound Control 8"
   Case 78 : Return "Sound Control 9"
   Case 79 : Return "Sound Control 10"
   Case 80 : Return "General Purpose Button 1 (on/off)"
   Case 81 : Return "General Purpose Button 2 (on/off)"
   Case 82 : Return "General Purpose Button 3 (on/off)"
   Case 83 : Return "General Purpose Button 4 (on/off)"
   Default : Return "unknown"
   End Select
End If
Select Value
   Case 91 : Return "Effects Level"
   Case 92 : Return "Tremulo Level"
   Case 93 : Return "Chorus Level"
   Case 94 : Return "Celeste Level"
   Case 95 : Return "Phaser Level"
   Case 96 : Return "Data Button increment"
   Case 97 : Return "Data Button decrement"
   Case 98 : Return "Non-registered Parameter (fine)"
   Case 99 : Return "Non-registered Parameter (coarse)"
   Case 100: Return "Registered Parameter (fine)"
   Case 101: Return "Registered Parameter (coarse)"
   Case 120: Return "All Sound Off"
   Case 121: Return "All Controllers Off"
   Case 122: Return "Local Keyboard (on/off)"
   Case 123: Return "All Notes Off"
   Case 124: Return "Omni Mode Off"
   Case 125: Return "Omni Mode On"
   Case 126: Return "Mono Operation"
   Case 127: Return "Poly Operation"
   Default : Return "unknown"
  End Select