It is currently Thu Jul 18, 2019 9:44 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Dim Array with Data
PostPosted: Sat Jun 15, 2019 12:19 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 1759
Location: Germany
Create an array with start values and change the values via DataSection.
At time not for strings.

Update v1.04
- Bugfix

Update v1.05
- Change: Remove parameter type
- Change: ArrayName now with type of array
- Added: DimWithString(...) and EndDimWithString()
- Added: LoadArrayString(...) and EndLoadArrayString()

Code:
;-TOP

; Comment : Dim Array with Data
; Author  : mk-soft
; Version : v1.05
; Created : 15.06.2019
; Updated :

; *********************************************************

EnableExplicit

CompilerIf Not Defined(__ArrayAddr, #PB_Variable)
  Threaded __ArrayAddr
  Threaded __ArraySize
  Threaded __ArrayIndex
  Threaded *__ArrayAddr.string
CompilerEndIf

; ----

Macro DimWithData(ArrayName, Size)
  Dim ArrayName(Size)
  __ArrayAddr = @ArrayName()
  DataSection
  __Begin_NewArrayData_#MacroExpandedCount:
EndMacro
 
Macro EndDimWithData()
  __End_NewArrayData_#MacroExpandedCount:
  EndDataSection
  CopyMemory(?__Begin_NewArrayData_#MacroExpandedCount, __ArrayAddr, ?__End_NewArrayData_#MacroExpandedCount - ?__Begin_NewArrayData_#MacroExpandedCount)
EndMacro

; ----

Macro LoadArrayData(ArrayName)
  __ArrayAddr = @ArrayName()
  DataSection
  __Begin_ArrayData_#MacroExpandedCount:
EndMacro

Macro EndLoadArrayData()
  __End_ArrayData_#MacroExpandedCount:
  EndDataSection
  CopyMemory(?__Begin_ArrayData_#MacroExpandedCount, __ArrayAddr, ?__End_ArrayData_#MacroExpandedCount - ?__Begin_ArrayData_#MacroExpandedCount)
EndMacro

; ----

Macro DimWithString(ArrayName, Size)
  Dim ArrayName(Size)
  *__ArrayAddr = @ArrayName()
  __ArraySize = ArraySize(ArrayName())
  DataSection
  __Begin_NewStringArray_#MacroExpandedCount:
EndMacro
 
Macro EndDimWithString()
  __End_NewStringArray_#MacroExpandedCount:
  EndDataSection
  Restore __Begin_NewStringArray_#MacroExpandedCount
  For __ArrayIndex = 0 To __ArraySize
    Read.s *__ArrayAddr\s
    *__ArrayAddr + SizeOf(integer)
  Next
EndMacro

; ----

Macro LoadArrayString(ArrayName)
  *__ArrayAddr = @ArrayName()
  __ArraySize = ArraySize(ArrayName())
  DataSection
  __Begin_StringArray_#MacroExpandedCount:
EndMacro

Macro EndLoadArrayString()
  __End_StringArray_#MacroExpandedCount:
  EndDataSection
  Restore __Begin_StringArray_#MacroExpandedCount
  For __ArrayIndex = 0 To __ArraySize
    Read.s *__ArrayAddr\s
    *__ArrayAddr + SizeOf(integer)
  Next
EndMacro

; *********************************************************

;-Example

CompilerIf #PB_Compiler_IsMainFile
 
  Define index
 
  Debug "Init Array..."
  Global DimWithData(Values.f, 5) ; <-- Don´t forget ArrayName with type float (.f)
    Data.f 1.1, 1.2, 1.3, 1.4, 1.5, 1.6
  EndDimWithData()
 
  For index = 0 To 5
    Debug StrF(Values(index), 2)
  Next
  Debug "----"
 
  Debug "Change Data 2"
  ReDim Values(7)
  LoadArrayData(Values)
    Data.f 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8
  EndLoadArrayData()
 
  For index = 0 To 7
    Debug StrF(Values(index), 2)
  Next
  Debug "----"
 
  Debug "Change Data 3"
  ReDim Values(3)
  LoadArrayData(Values)
    Data.f 3.1, 3.2, 3.3, 3.4
  EndLoadArrayData()
 
  For index = 0 To 3
    Debug StrF(Values(index), 2)
  Next
  Debug "----"
 
  Debug "Init String Array..."
  Global DimWithString(sVal.s, 3) ; <-- Don´t forget ArrayName with type string (.s)
    Data.s "Hello " , "World!", "I Like Purebasic", ""
  EndDimWithString()
 
  For index = 0 To 3
    Debug "Index " + index + " = " + sVal(index)
  Next
  Debug "----"
 
  Debug "Update String Array..."
  LoadArrayString(sVal)
    Data.s "One" , "Two", "Three", "Four"
  EndLoadArrayString()
 
  For index = 0 To 3
    Debug "Index " + index + " = " + sVal(index)
  Next
  Debug "----"
 
 
CompilerEndIf

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / Event-Designer
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Last edited by mk-soft on Sat Jun 15, 2019 7:04 pm, edited 2 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Dim Array with Data
PostPosted: Sat Jun 15, 2019 12:46 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 1759
Location: Germany
Code:
;-Example Multi-Array

CompilerIf #PB_Compiler_IsMainFile
 
  Define a, b
 
  Dim iVal(4, 1)
 
  LoadArrayData(iVal)
  Data.i 11, 12
  Data.i 21, 22
  Data.i 31, 32
  Data.i 41, 42
  EndLoadArrayData()
 
  For a = 0 To 4
    For b = 0 To 1
      Debug "" + a + ":" + b + " = " + iVal(a,b)
    Next
  Next
 
CompilerEndIf

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / Event-Designer
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: Dim Array with Data
PostPosted: Sat Jun 15, 2019 1:37 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Apr 26, 2003 2:15 pm
Posts: 805
Location: Cuernavaca, Mexico
Very nice source and examples. Thanks :D

_________________
- It was too lonely at the top.


Top
 Profile  
Reply with quote  
 Post subject: Re: Dim Array with Data
PostPosted: Sat Jun 15, 2019 1:55 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 1759
Location: Germany
Thanks blueb,

But is not for Strings. I work on it...

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / Event-Designer
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: Dim Array with Data
PostPosted: Sat Jun 15, 2019 3:38 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Tue Sep 22, 2009 10:41 pm
Posts: 415
Done
We can remove "Data.I" prefix.


Top
 Profile  
Reply with quote  
 Post subject: Re: Dim Array with Data
PostPosted: Sat Jun 15, 2019 7:05 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 1759
Location: Germany
Olliv wrote:
Done
We can remove "Data.I" prefix.

:wink:

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / Event-Designer
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Last edited by mk-soft on Sat Jun 15, 2019 7:12 pm, edited 2 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Dim Array with Data
PostPosted: Sat Jun 15, 2019 7:06 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 1759
Location: Germany
Update v1.05
- Change: Remove parameter type
- Change: ArrayName now with type of array
- Added: DimWithString(...) and EndDimWithString()
- Added: LoadArrayString(...) and EndLoadArrayString()

Now Strings supported :wink:

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / Event-Designer
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye